forked from docker/buildx
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
as per docker#1290 Signed-off-by: Christian Dupuis <cd@atomist.com>
- Loading branch information
Showing
2 changed files
with
220 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
package commands | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"os" | ||
"os/exec" | ||
"path/filepath" | ||
"strings" | ||
"testing" | ||
|
||
ocispecs "github.com/opencontainers/image-spec/specs-go/v1" | ||
) | ||
|
||
var repoDir string | ||
|
||
func setupTest(tb testing.TB) func(tb testing.TB) { | ||
repoDir = tb.TempDir() | ||
cmd := exec.Command("git", "init") | ||
cmd.Dir = repoDir | ||
err := cmd.Run() | ||
if err != nil { | ||
tb.Errorf("failed to init git repo: %v", err) | ||
} | ||
|
||
df := []byte("FROM alpine:latest\n") | ||
err = os.WriteFile(filepath.Join(repoDir, "Dockerfile"), df, 0644) | ||
if err != nil { | ||
tb.Errorf("failed to write file: %v", err) | ||
} | ||
|
||
cmd = exec.Command("git", "add", "Dockerfile") | ||
cmd.Dir = repoDir | ||
err = cmd.Run() | ||
if err != nil { | ||
tb.Errorf("failed to add file: %v", err) | ||
} | ||
|
||
cmd = exec.Command("git", "config", "user.name", "buildx") | ||
cmd.Dir = repoDir | ||
err = cmd.Run() | ||
if err != nil { | ||
tb.Errorf("failed to set git user.name: %v", err) | ||
} | ||
|
||
cmd = exec.Command("git", "config", "user.email", "buildx@docker.com") | ||
cmd.Dir = repoDir | ||
err = cmd.Run() | ||
if err != nil { | ||
tb.Errorf("failed to set git user.email: %v", err) | ||
} | ||
|
||
cmd = exec.Command("git", "commit", "-m", "Initial commit") | ||
cmd.Dir = repoDir | ||
out, err := cmd.Output() | ||
if err != nil { | ||
tb.Errorf("failed to commit: %v %s", err, out) | ||
} | ||
|
||
return func(tb testing.TB) { | ||
os.Unsetenv("BUILDX_GIT_LABELS") | ||
os.RemoveAll(repoDir) | ||
} | ||
} | ||
|
||
func TestAddGitProvenanceDataWithoutEnv(t *testing.T) { | ||
defer setupTest(t)(t) | ||
labels, err := addGitProvenance(context.Background(), repoDir, filepath.Join(repoDir, "Dockerfile")) | ||
if err != nil { | ||
t.Error("No error expected") | ||
} | ||
if labels != nil { | ||
t.Error("No labels expected") | ||
} | ||
} | ||
|
||
func TestAddGitProvenanceDataWithoutLabels(t *testing.T) { | ||
defer setupTest(t)(t) | ||
os.Setenv("BUILDX_GIT_LABELS", "full") | ||
labels, err := addGitProvenance(context.Background(), repoDir, filepath.Join(repoDir, "Dockerfile")) | ||
if err != nil { | ||
t.Errorf("No error expected: %v", err) | ||
} | ||
if len(labels) != 2 { | ||
t.Error("Exactly 2 git provenance labels expected") | ||
} | ||
dockerfileLabel := strings.Split(labels[1], "=") | ||
if dockerfileLabel[0] != DockerfileLabel || dockerfileLabel[1] != "Dockerfile" { | ||
t.Error("Expected a dockerfile path provenance label") | ||
} | ||
shaLabel := strings.Split(labels[0], "=") | ||
cmd := exec.Command("git", "rev-parse", "HEAD") | ||
cmd.Dir = repoDir | ||
out, _ := cmd.Output() | ||
if shaLabel[0] != ocispecs.AnnotationRevision || shaLabel[1] != strings.TrimSpace(string(out)) { | ||
t.Error("Expected a sha provenance label") | ||
} | ||
} | ||
|
||
func TestAddGitProvenanceDataWithLabels(t *testing.T) { | ||
defer setupTest(t)(t) | ||
// make a change to test dirty flag | ||
df := []byte("FROM alpine:edge\n") | ||
os.Mkdir(filepath.Join(repoDir, "dir"), 0755) | ||
os.WriteFile(filepath.Join(repoDir, "dir", "Dockerfile"), df, 0644) | ||
// add a remote | ||
cmd := exec.Command("git", "remote", "add", "origin", "git@github.com:docker/buildx.git") | ||
cmd.Dir = repoDir | ||
cmd.Run() | ||
|
||
os.Setenv("BUILDX_GIT_LABELS", "full") | ||
labels, err := addGitProvenance(context.Background(), repoDir, filepath.Join(repoDir, "Dockerfile")) | ||
if err != nil { | ||
t.Errorf("No error expected: %v", err) | ||
} | ||
if len(labels) != 3 { | ||
t.Error("Exactly 3 git provenance labels expected") | ||
} | ||
dockerfileLabel := strings.Split(labels[2], "=") | ||
if dockerfileLabel[0] != DockerfileLabel || dockerfileLabel[1] != "Dockerfile" { | ||
t.Error("Expected a dockerfile path provenance label") | ||
} | ||
shaLabel := strings.Split(labels[0], "=") | ||
cmd = exec.Command("git", "rev-parse", "HEAD") | ||
cmd.Dir = repoDir | ||
out, _ := cmd.Output() | ||
if shaLabel[0] != ocispecs.AnnotationRevision || shaLabel[1] != fmt.Sprintf("%s-dirty", strings.TrimSpace(string(out))) { | ||
t.Error("Expected a sha provenance label") | ||
} | ||
originLabel := strings.Split(labels[1], "=") | ||
if originLabel[0] != ocispecs.AnnotationSource || originLabel[1] != "git@github.com:docker/buildx.git" { | ||
t.Error("Expected a origin provenance label") | ||
} | ||
} |