-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Fix target in digest tagger generation #9826 #9867
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -153,3 +153,68 @@ CMD [ "true" ] | |
| } | ||
| }) | ||
| } | ||
|
|
||
| func TestGenerateTag_DifferentTarget(t *testing.T) { | ||
| runCtx := &runcontext.RunContext{} | ||
| dockerfilePath := filepath.Join(t.TempDir(), "Dockerfile") | ||
| if err := os.WriteFile(dockerfilePath, []byte("FROM busybox\nCMD [\"ps\", \"faux\"]\n"), 0644); err != nil { | ||
| t.Fatalf("failed to write dockerfile: %v", err) | ||
| } | ||
|
|
||
| digestExample, _ := NewInputDigestTagger(runCtx, graph.ToArtifactGraph(runCtx.Artifacts())) | ||
| tag1, err := digestExample.GenerateTag(context.Background(), latest.Artifact{ | ||
| Workspace: t.TempDir(), | ||
| ArtifactType: latest.ArtifactType{ | ||
| DockerArtifact: &latest.DockerArtifact{ | ||
| DockerfilePath: dockerfilePath, | ||
| Target: "target1", | ||
| }, | ||
| }, | ||
| }) | ||
| if err != nil { | ||
| t.Fatalf("GenerateTag failed for target1: %v", err) | ||
| } | ||
|
|
||
| digestExample, _ = NewInputDigestTagger(runCtx, graph.ToArtifactGraph(runCtx.Artifacts())) | ||
| tag2, err := digestExample.GenerateTag(context.Background(), latest.Artifact{ | ||
| Workspace: t.TempDir(), | ||
| ArtifactType: latest.ArtifactType{ | ||
| DockerArtifact: &latest.DockerArtifact{ | ||
| DockerfilePath: dockerfilePath, | ||
| Target: "target2", | ||
| }, | ||
| }, | ||
| }) | ||
| if err != nil { | ||
| t.Fatalf("GenerateTag failed for target2: %v", err) | ||
| } | ||
|
|
||
| if tag1 == tag2 { | ||
| t.Errorf("expected different tags for different targets, got same: %s", tag1) | ||
| } | ||
| } | ||
|
Comment on lines
+157
to
+195
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This test has a couple of issues in its setup:
func TestGenerateTag_DifferentTarget(t *testing.T) {
runCtx := &runcontext.RunContext{}
dir := t.TempDir()
dockerfilePath := filepath.Join(dir, "Dockerfile")
if err := os.WriteFile(dockerfilePath, []byte("FROM busybox\nCMD [\"ps\", \"faux\"]\n"), 0644); err != nil {
t.Fatalf("failed to write dockerfile: %v", err)
}
digestExample, _ := NewInputDigestTagger(runCtx, graph.ToArtifactGraph(runCtx.Artifacts()))
tag1, err := digestExample.GenerateTag(context.Background(), latest.Artifact{
Workspace: dir,
ArtifactType: latest.ArtifactType{
DockerArtifact: &latest.DockerArtifact{
DockerfilePath: dockerfilePath,
Target: "target1",
},
},
})
if err != nil {
t.Fatalf("GenerateTag failed for target1: %v", err)
}
tag2, err := digestExample.GenerateTag(context.Background(), latest.Artifact{
Workspace: dir,
ArtifactType: latest.ArtifactType{
DockerArtifact: &latest.DockerArtifact{
DockerfilePath: dockerfilePath,
Target: "target2",
},
},
})
if err != nil {
t.Fatalf("GenerateTag failed for target2: %v", err)
}
if tag1 == tag2 {
t.Errorf("expected different tags for different targets, got same: %s", tag1)
}
} |
||
|
|
||
| func TestGenerateTag_NoTarget(t *testing.T) { | ||
| runCtx := &runcontext.RunContext{} | ||
| dockerfilePath := filepath.Join(t.TempDir(), "Dockerfile") | ||
| if err := os.WriteFile(dockerfilePath, []byte("FROM busybox\nCMD [\"ps\", \"faux\"]\n"), 0644); err != nil { | ||
| t.Fatalf("failed to write dockerfile: %v", err) | ||
| } | ||
|
|
||
| digestExample, _ := NewInputDigestTagger(runCtx, graph.ToArtifactGraph(runCtx.Artifacts())) | ||
| tag, err := digestExample.GenerateTag(context.Background(), latest.Artifact{ | ||
| Workspace: t.TempDir(), | ||
| ArtifactType: latest.ArtifactType{ | ||
| DockerArtifact: &latest.DockerArtifact{ | ||
| DockerfilePath: dockerfilePath, | ||
| // No Target field set | ||
| }, | ||
| }, | ||
| }) | ||
| if err != nil { | ||
| t.Fatalf("GenerateTag failed when no target: %v", err) | ||
| } | ||
| if tag == "" { | ||
| t.Errorf("expected a non-empty tag when no target is set") | ||
| } | ||
| } | ||
|
Comment on lines
+197
to
+220
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Similar to func TestGenerateTag_NoTarget(t *testing.T) {
runCtx := &runcontext.RunContext{}
dir := t.TempDir()
dockerfilePath := filepath.Join(dir, "Dockerfile")
if err := os.WriteFile(dockerfilePath, []byte("FROM busybox\nCMD [\"ps\", \"faux\"]\n"), 0644); err != nil {
t.Fatalf("failed to write dockerfile: %v", err)
}
digestExample, _ := NewInputDigestTagger(runCtx, graph.ToArtifactGraph(runCtx.Artifacts()))
tag, err := digestExample.GenerateTag(context.Background(), latest.Artifact{
Workspace: dir,
ArtifactType: latest.ArtifactType{
DockerArtifact: &latest.DockerArtifact{
DockerfilePath: dockerfilePath,
// No Target field set
},
},
})
if err != nil {
t.Fatalf("GenerateTag failed when no target: %v", err)
}
if tag == "" {
t.Errorf("expected a non-empty tag when no target is set")
}
} |
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The logic for handling
DockerArtifactandKanikoArtifactis identical, leading to code duplication. To improve maintainability, you can refactor this to avoid repetition.Assuming that an artifact can only be of one type (e.g., either
DockerArtifactorKanikoArtifact, but not both), which is suggested by theoneOf=artifactstruct tag, you can use anif/else ifstructure to determine thedockerfilePathandtargetfirst, and then process them in a single block.