Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 40 additions & 25 deletions cmd/bundle/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import (
)

var _ = Describe("Bundle Loader", func() {
const exampleImage = "ghcr.io/shipwright-io/sample-go/source-bundle:latest"
const publicExampleImage = "ghcr.io/shipwright-io/sample-go/source-bundle:latest"

run := func(args ...string) error {
log.SetOutput(GinkgoWriter)
Expand Down Expand Up @@ -76,6 +76,18 @@ var _ = Describe("Bundle Loader", func() {
f(u.Host)
}

withTestBundleImage := func(f func(tag name.Tag)) {
withTempRegistry(func(endpoint string) {
tag, err := name.NewTag(fmt.Sprintf("%s/namespace/unit-test-cmd-bundle-%s:latest", endpoint, rand.String(5)))
Expect(err).ToNot(HaveOccurred())

_, err = bundle.PackAndPush(tag, filepath.Join("..", "..", "test", "bundle"))
Expect(err).ToNot(HaveOccurred())

f(tag)
})
}

filecontent := func(path string) string {
// #nosec G304 ok in tests
data, err := os.ReadFile(path)
Expand Down Expand Up @@ -135,29 +147,30 @@ var _ = Describe("Bundle Loader", func() {

Context("Pulling image anonymously", func() {
It("should pull and unbundle an image from a public registry", func() {
withTempDir(func(target string) {
Expect(run(
"--image", exampleImage,
"--target", target,
)).To(Succeed())
withTestBundleImage(func(tag name.Tag) {
withTempDir(func(target string) {
Expect(run(
"--image", tag.String(),
"--target", target,
)).To(Succeed())

Expect(filepath.Join(target, "LICENSE")).To(BeAnExistingFile())
Expect(filepath.Join(target, "README.md")).To(BeAnExistingFile())
})
})
})

It("should store image digest into file specified in --result-file-image-digest flags", func() {
withTempDir(func(target string) {
withTempFile("image-digest", func(filename string) {
Expect(run(
"--image", exampleImage,
"--target", target,
"--result-file-image-digest", filename,
)).To(Succeed())

tag, err := name.NewTag(exampleImage)
Expect(err).ToNot(HaveOccurred())
withTestBundleImage(func(tag name.Tag) {
withTempDir(func(target string) {
withTempFile("image-digest", func(filename string) {
Expect(run(
"--image", tag.String(),
"--target", target,
"--result-file-image-digest", filename,
)).To(Succeed())

Expect(filecontent(filename)).To(Equal(getImageDigest(tag).String()))
Expect(filecontent(filename)).To(Equal(getImageDigest(tag).String()))
})
})
})
})
Expand Down Expand Up @@ -201,7 +214,7 @@ var _ = Describe("Bundle Loader", func() {
"source",
)

src, err := name.ParseReference(exampleImage)
src, err := name.ParseReference(publicExampleImage)
Expect(err).ToNot(HaveOccurred())

dst, err := name.ParseReference(testImage)
Expand Down Expand Up @@ -318,12 +331,14 @@ var _ = Describe("Bundle Loader", func() {

Context("Using show listing flag", func() {
It("should run without issues", func() {
withTempDir(func(target string) {
Expect(run(
"--image", exampleImage,
"--target", target,
"--show-listing",
)).To(Succeed())
withTestBundleImage(func(tag name.Tag) {
withTempDir(func(target string) {
Expect(run(
"--image", tag.String(),
"--target", target,
"--show-listing",
)).To(Succeed())
})
})
})
})
Expand Down
17 changes: 14 additions & 3 deletions cmd/git/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,18 @@ var (
displayURL string
)

var (
runGitCommand = func(ctx context.Context, args ...string) ([]byte, error) {
// #nosec G204 arguments are well-defined by the code
// #nosec G702 arguments are well-defined by the code
cmd := exec.CommandContext(ctx, "git", args...)
cmd.Stdin = nil

return cmd.CombinedOutput()
}
testConnection = util.TestConnection
)

// ExitError is an error which has an exit code to be used in os.Exit() to
// return both an exit code and an error message
type ExitError struct {
Expand Down Expand Up @@ -172,7 +184,7 @@ func runGitClone(ctx context.Context) error {

// check the endpoint, if hostname extraction fails, ignore that failure
if hostname, port, err := shpgit.ExtractHostnamePort(flagValues.url); err == nil {
if !util.TestConnection(hostname, port, 9) {
if !testConnection(hostname, port, 9) {
log.Printf("Warning: a connection test to %s:%d failed. The operation will likely fail.\n", hostname, port)
}
}
Expand Down Expand Up @@ -494,9 +506,8 @@ func git(ctx context.Context, args ...string) (string, error) {
if err := os.Setenv("GIT_TERMINAL_PROMPT", "0"); err != nil {
return "", err
}
cmd.Stdin = nil

out, err := cmd.CombinedOutput()
out, err := runGitCommand(ctx, fullArgs...)

var output string
if out != nil {
Expand Down
3 changes: 1 addition & 2 deletions cmd/git/main_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
// SPDX-License-Identifier: Apache-2.0

package main_test
package main

import (
"fmt"
Expand All @@ -12,7 +12,6 @@ import (
. "github.com/onsi/gomega"
"github.com/onsi/gomega/types"

. "github.com/shipwright-io/build/cmd/git"
shpgit "github.com/shipwright-io/build/pkg/git"
)

Expand Down
Loading
Loading