-
Notifications
You must be signed in to change notification settings - Fork 485
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
build: refactor setting git info to local mounts
This is a preparation to shared local sources for bake targets and makes it possible to have equality check between locals from different targets. Signed-off-by: Tonis Tiigi <[email protected]>
- Loading branch information
1 parent
3005743
commit d8f26f7
Showing
4 changed files
with
107 additions
and
74 deletions.
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
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 |
---|---|---|
|
@@ -31,24 +31,26 @@ func setupTest(tb testing.TB) { | |
} | ||
|
||
func TestGetGitAttributesNotGitRepo(t *testing.T) { | ||
_, _, err := getGitAttributes(context.Background(), t.TempDir(), "Dockerfile") | ||
_, err := getGitAttributes(context.Background(), t.TempDir(), "Dockerfile") | ||
assert.NoError(t, err) | ||
} | ||
|
||
func TestGetGitAttributesBadGitRepo(t *testing.T) { | ||
tmp := t.TempDir() | ||
require.NoError(t, os.MkdirAll(path.Join(tmp, ".git"), 0755)) | ||
|
||
_, _, err := getGitAttributes(context.Background(), tmp, "Dockerfile") | ||
_, err := getGitAttributes(context.Background(), tmp, "Dockerfile") | ||
assert.Error(t, err) | ||
} | ||
|
||
func TestGetGitAttributesNoContext(t *testing.T) { | ||
setupTest(t) | ||
|
||
gitattrs, _, err := getGitAttributes(context.Background(), "", "Dockerfile") | ||
addGitAttrs, err := getGitAttributes(context.Background(), "", "Dockerfile") | ||
assert.NoError(t, err) | ||
assert.Empty(t, gitattrs) | ||
var so client.SolveOpt | ||
addGitAttrs(&so) | ||
assert.Empty(t, so.FrontendAttrs) | ||
} | ||
|
||
func TestGetGitAttributes(t *testing.T) { | ||
|
@@ -115,15 +117,17 @@ func TestGetGitAttributes(t *testing.T) { | |
if tt.envGitInfo != "" { | ||
t.Setenv("BUILDX_GIT_INFO", tt.envGitInfo) | ||
} | ||
gitattrs, _, err := getGitAttributes(context.Background(), ".", "Dockerfile") | ||
addGitAttrs, err := getGitAttributes(context.Background(), ".", "Dockerfile") | ||
require.NoError(t, err) | ||
var so client.SolveOpt | ||
addGitAttrs(&so) | ||
for _, e := range tt.expected { | ||
assert.Contains(t, gitattrs, e) | ||
assert.NotEmpty(t, gitattrs[e]) | ||
assert.Contains(t, so.FrontendAttrs, e) | ||
assert.NotEmpty(t, so.FrontendAttrs[e]) | ||
if e == "label:"+DockerfileLabel { | ||
assert.Equal(t, "Dockerfile", gitattrs[e]) | ||
assert.Equal(t, "Dockerfile", so.FrontendAttrs[e]) | ||
} else if e == "label:"+specs.AnnotationSource || e == "vcs:source" { | ||
assert.Equal(t, "[email protected]:docker/buildx.git", gitattrs[e]) | ||
assert.Equal(t, "[email protected]:docker/buildx.git", so.FrontendAttrs[e]) | ||
} | ||
} | ||
}) | ||
|
@@ -140,20 +144,25 @@ func TestGetGitAttributesDirty(t *testing.T) { | |
require.NoError(t, os.WriteFile(filepath.Join("dir", "Dockerfile"), df, 0644)) | ||
|
||
t.Setenv("BUILDX_GIT_LABELS", "true") | ||
gitattrs, _, _ := getGitAttributes(context.Background(), ".", "Dockerfile") | ||
assert.Equal(t, 5, len(gitattrs)) | ||
|
||
assert.Contains(t, gitattrs, "label:"+DockerfileLabel) | ||
assert.Equal(t, "Dockerfile", gitattrs["label:"+DockerfileLabel]) | ||
assert.Contains(t, gitattrs, "label:"+specs.AnnotationSource) | ||
assert.Equal(t, "[email protected]:docker/buildx.git", gitattrs["label:"+specs.AnnotationSource]) | ||
assert.Contains(t, gitattrs, "label:"+specs.AnnotationRevision) | ||
assert.True(t, strings.HasSuffix(gitattrs["label:"+specs.AnnotationRevision], "-dirty")) | ||
|
||
assert.Contains(t, gitattrs, "vcs:source") | ||
assert.Equal(t, "[email protected]:docker/buildx.git", gitattrs["vcs:source"]) | ||
assert.Contains(t, gitattrs, "vcs:revision") | ||
assert.True(t, strings.HasSuffix(gitattrs["vcs:revision"], "-dirty")) | ||
addGitAttrs, err := getGitAttributes(context.Background(), ".", "Dockerfile") | ||
require.NoError(t, err) | ||
|
||
var so client.SolveOpt | ||
addGitAttrs(&so) | ||
|
||
assert.Equal(t, 5, len(so.FrontendAttrs)) | ||
|
||
assert.Contains(t, so.FrontendAttrs, "label:"+DockerfileLabel) | ||
assert.Equal(t, "Dockerfile", so.FrontendAttrs["label:"+DockerfileLabel]) | ||
assert.Contains(t, so.FrontendAttrs, "label:"+specs.AnnotationSource) | ||
assert.Equal(t, "[email protected]:docker/buildx.git", so.FrontendAttrs["label:"+specs.AnnotationSource]) | ||
assert.Contains(t, so.FrontendAttrs, "label:"+specs.AnnotationRevision) | ||
assert.True(t, strings.HasSuffix(so.FrontendAttrs["label:"+specs.AnnotationRevision], "-dirty")) | ||
|
||
assert.Contains(t, so.FrontendAttrs, "vcs:source") | ||
assert.Equal(t, "[email protected]:docker/buildx.git", so.FrontendAttrs["vcs:source"]) | ||
assert.Contains(t, so.FrontendAttrs, "vcs:revision") | ||
assert.True(t, strings.HasSuffix(so.FrontendAttrs["vcs:revision"], "-dirty")) | ||
} | ||
|
||
func TestLocalDirs(t *testing.T) { | ||
|
@@ -163,15 +172,17 @@ func TestLocalDirs(t *testing.T) { | |
FrontendAttrs: map[string]string{}, | ||
} | ||
|
||
_, addVCSLocalDir, err := getGitAttributes(context.Background(), ".", "Dockerfile") | ||
addGitAttrs, err := getGitAttributes(context.Background(), ".", "Dockerfile") | ||
require.NoError(t, err) | ||
require.NotNil(t, addVCSLocalDir) | ||
|
||
require.NoError(t, setLocalMount("context", ".", so, addVCSLocalDir)) | ||
require.NoError(t, setLocalMount("context", ".", so)) | ||
require.NoError(t, setLocalMount("dockerfile", ".", so)) | ||
|
||
addGitAttrs(so) | ||
|
||
require.Contains(t, so.FrontendAttrs, "vcs:localdir:context") | ||
assert.Equal(t, ".", so.FrontendAttrs["vcs:localdir:context"]) | ||
|
||
require.NoError(t, setLocalMount("dockerfile", ".", so, addVCSLocalDir)) | ||
require.Contains(t, so.FrontendAttrs, "vcs:localdir:dockerfile") | ||
assert.Equal(t, ".", so.FrontendAttrs["vcs:localdir:dockerfile"]) | ||
} | ||
|
@@ -194,16 +205,17 @@ func TestLocalDirsSub(t *testing.T) { | |
so := &client.SolveOpt{ | ||
FrontendAttrs: map[string]string{}, | ||
} | ||
require.NoError(t, setLocalMount("context", ".", so)) | ||
require.NoError(t, setLocalMount("dockerfile", "app", so)) | ||
|
||
_, addVCSLocalDir, err := getGitAttributes(context.Background(), ".", "app/Dockerfile") | ||
addGitAttrs, err := getGitAttributes(context.Background(), ".", "app/Dockerfile") | ||
require.NoError(t, err) | ||
require.NotNil(t, addVCSLocalDir) | ||
|
||
require.NoError(t, setLocalMount("context", ".", so, addVCSLocalDir)) | ||
addGitAttrs(so) | ||
|
||
require.Contains(t, so.FrontendAttrs, "vcs:localdir:context") | ||
assert.Equal(t, ".", so.FrontendAttrs["vcs:localdir:context"]) | ||
|
||
require.NoError(t, setLocalMount("dockerfile", "app", so, addVCSLocalDir)) | ||
require.Contains(t, so.FrontendAttrs, "vcs:localdir:dockerfile") | ||
assert.Equal(t, "app", so.FrontendAttrs["vcs:localdir:dockerfile"]) | ||
} |
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