Skip to content

Commit

Permalink
adding test for packaging an extension
Browse files Browse the repository at this point in the history
  • Loading branch information
pacostas committed Jun 6, 2023
1 parent 08cea19 commit 0a09765
Show file tree
Hide file tree
Showing 6 changed files with 176 additions and 0 deletions.
118 changes: 118 additions & 0 deletions integration/pack_extension_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (

"os"
"os/exec"
"os/user"

"path/filepath"
"testing"

Expand Down Expand Up @@ -116,4 +118,120 @@ func testPackExtension(t *testing.T, context spec.G, it spec.S) {
})
})

context("when packaging an implementation extension", func() {
it.Before(func() {
err := cargo.NewDirectoryDuplicator().Duplicate(filepath.Join("testdata", "extension-example-cnb"), extensionDir)
Expect(err).NotTo(HaveOccurred())
})

it("creates a packaged extension", func() {
command := exec.Command(
path, "pack",
"--extension", filepath.Join(extensionDir, "extension.toml"),
"--output", filepath.Join(tmpDir, "output.tgz"),
"--version", "some-version",
)
session, err := gexec.Start(command, buffer, buffer)
Expect(err).NotTo(HaveOccurred())
Eventually(session, "5s").Should(gexec.Exit(0), func() string { return buffer.String() })

Expect(session.Out).To(gbytes.Say("Packing some-extension-name some-version..."))
Expect(session.Out).To(gbytes.Say(" Executing pre-packaging script: ./scripts/build.sh"))
Expect(session.Out).To(gbytes.Say(" hello from the pre-packaging script"))
Expect(session.Out).To(gbytes.Say(fmt.Sprintf(" Building tarball: %s", filepath.Join(tmpDir, "output.tgz"))))
Expect(session.Out).To(gbytes.Say(" bin/detect"))
Expect(session.Out).To(gbytes.Say(" bin/generate"))
Expect(session.Out).To(gbytes.Say(" bin/run"))
Expect(session.Out).To(gbytes.Say(" extension.toml"))
Expect(session.Out).To(gbytes.Say(" generated-file"))

file, err := os.Open(filepath.Join(tmpDir, "output.tgz"))
Expect(err).NotTo(HaveOccurred())

u, err := user.Current()
Expect(err).NotTo(HaveOccurred())
userName := u.Username

group, err := user.LookupGroupId(u.Gid)
Expect(err).NotTo(HaveOccurred())
groupName := group.Name

contents, hdr, err := ExtractFile(file, "extension.toml")
Expect(err).NotTo(HaveOccurred())
Expect(contents).To(MatchTOML(`api = "0.7"
[extension]
description = "some-extensin-description"
homepage = "some-extension-homepage"
id = "some-extension-id"
keywords = ["some-extension-keyword"]
name = "some-extension-name"
version = "some-version"
[[extension.licenses]]
type = "some-extension-license-type"
uri = "some-extension-license-uri"
[metadata]
include-files = ["bin/generate", "bin/detect", "bin/run", "extension.toml", "generated-file"]
pre-package = "./scripts/build.sh"
[[metadata.configurations]]
build = true
default = "16"
description = "the Node.js version"
name = "BP_NODE_VERSION"
[metadata.default-versions]
node = "18.*.*"
[[metadata.dependencies]]
id = "some-dependency"
name = "Some Dependency"
sha256 = "shasum"
source = "http://some-source-url"
stacks = ["io.buildpacks.stacks.bionic", "org.cloudfoundry.stacks.tiny", "*"]
uri = "http://some-url"
version = "1.2.3"
[[metadata.dependencies]]
id = "other-dependency"
name = "Other Dependency"
sha256 = "shasum"
source = "http://some-source-url"
stacks = ["org.cloudfoundry.stacks.tiny"]
uri = "http://other-url"
version = "4.5.6"`))
Expect(hdr.Mode).To(Equal(int64(0644)))

contents, hdr, err = ExtractFile(file, "bin/detect")
Expect(err).NotTo(HaveOccurred())
Expect(string(contents)).To(Equal("detect-contents"))
Expect(hdr.Mode).To(Equal(int64(0755)))
Expect(hdr.Uname).To(Equal(userName))
Expect(hdr.Gname).To(Equal(groupName))

contents, hdr, err = ExtractFile(file, "bin/generate")
Expect(err).NotTo(HaveOccurred())
Expect(string(contents)).To(Equal("generate-contents"))
Expect(hdr.Mode).To(Equal(int64(0755)))
Expect(hdr.Uname).To(Equal(userName))
Expect(hdr.Gname).To(Equal(groupName))

contents, hdr, err = ExtractFile(file, "bin/run")
Expect(err).NotTo(HaveOccurred())
Expect(string(contents)).To(Equal("run-contents"))
Expect(hdr.Mode).To(Equal(int64(0755)))
Expect(hdr.Uname).To(Equal(userName))
Expect(hdr.Gname).To(Equal(groupName))

contents, hdr, err = ExtractFile(file, "generated-file")
Expect(err).NotTo(HaveOccurred())
Expect(string(contents)).To(Equal("hello\n"))
Expect(hdr.Mode).To(Equal(int64(0644)))
Expect(hdr.Uname).To(Equal(userName))
Expect(hdr.Gname).To(Equal(groupName))

Expect(filepath.Join(extensionDir, "generated-file")).NotTo(BeARegularFile())
})
}
1 change: 1 addition & 0 deletions integration/testdata/extension-example-cnb/bin/detect
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
detect-contents
1 change: 1 addition & 0 deletions integration/testdata/extension-example-cnb/bin/generate
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
generate-contents
1 change: 1 addition & 0 deletions integration/testdata/extension-example-cnb/bin/run
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
run-contents
47 changes: 47 additions & 0 deletions integration/testdata/extension-example-cnb/extension.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
api = "0.7"

[extension]
id = "some-extension-id"
name = "some-extension-name"
version = "version-string"
description = "some-extensin-description"
homepage = "some-extension-homepage"
keywords = [ "some-extension-keyword" ]

[[extension.licenses]]
type = "some-extension-license-type"
uri = "some-extension-license-uri"

[metadata]
include-files = ["bin/generate", "bin/detect", "bin/run", "extension.toml", "generated-file"]
pre-package = "./scripts/build.sh"

[metadata.default-versions]
node = "18.*.*"

[[metadata.dependencies]]
id = "some-dependency"
name = "Some Dependency"
sha256 = "shasum"
stacks = ["io.buildpacks.stacks.bionic", "org.cloudfoundry.stacks.tiny", "*"]
uri = "http://some-url"
source = "http://some-source-url"
version = "1.2.3"

[[metadata.dependencies]]
id = "other-dependency"
name = "Other Dependency"
sha256 = "shasum"
stacks = ["org.cloudfoundry.stacks.tiny"]
uri = "http://other-url"
source = "http://some-source-url"
version = "4.5.6"

[[metadata.configurations]]
build = true
default = "16"
description = "the Node.js version"
name = "BP_NODE_VERSION"



8 changes: 8 additions & 0 deletions integration/testdata/extension-example-cnb/scripts/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/bin/bash
readonly PROGDIR="$(cd "$(dirname "${0}")" && pwd)"

echo "hello from the pre-packaging script"

echo "hello" > "$PROGDIR/../generated-file"

chmod 644 "$PROGDIR/../generated-file"

0 comments on commit 0a09765

Please sign in to comment.