2022-08-20 18:25:44 +08:00
|
|
|
package build
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"os"
|
2022-12-16 06:30:24 +08:00
|
|
|
"path"
|
2022-08-20 18:25:44 +08:00
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
2022-12-13 18:20:04 +08:00
|
|
|
"github.com/docker/buildx/util/gitutil"
|
2023-12-19 16:23:23 +08:00
|
|
|
"github.com/moby/buildkit/client"
|
2022-12-13 18:20:04 +08:00
|
|
|
specs "github.com/opencontainers/image-spec/specs-go/v1"
|
2022-08-20 18:25:44 +08:00
|
|
|
"github.com/stretchr/testify/assert"
|
2022-12-16 06:30:24 +08:00
|
|
|
"github.com/stretchr/testify/require"
|
2022-08-20 18:25:44 +08:00
|
|
|
)
|
|
|
|
|
2022-12-16 06:30:24 +08:00
|
|
|
func setupTest(tb testing.TB) {
|
2022-12-13 18:20:04 +08:00
|
|
|
gitutil.Mktmp(tb)
|
2022-12-16 06:30:24 +08:00
|
|
|
|
|
|
|
c, err := gitutil.New()
|
|
|
|
require.NoError(tb, err)
|
2022-12-16 04:16:37 +08:00
|
|
|
gitutil.GitInit(c, tb)
|
2022-12-16 06:30:24 +08:00
|
|
|
|
2022-08-20 18:25:44 +08:00
|
|
|
df := []byte("FROM alpine:latest\n")
|
2022-12-13 18:20:04 +08:00
|
|
|
assert.NoError(tb, os.WriteFile("Dockerfile", df, 0644))
|
2022-12-16 06:30:24 +08:00
|
|
|
|
2022-12-16 04:16:37 +08:00
|
|
|
gitutil.GitAdd(c, tb, "Dockerfile")
|
|
|
|
gitutil.GitCommit(c, tb, "initial commit")
|
2023-01-24 23:16:57 +08:00
|
|
|
gitutil.GitSetRemote(c, tb, "origin", "git@github.com:docker/buildx.git")
|
2022-12-16 06:30:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetGitAttributesNotGitRepo(t *testing.T) {
|
2023-12-19 16:23:23 +08:00
|
|
|
_, _, err := getGitAttributes(context.Background(), t.TempDir(), "Dockerfile")
|
2022-12-16 08:51:03 +08:00
|
|
|
assert.NoError(t, err)
|
2022-12-16 06:30:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetGitAttributesBadGitRepo(t *testing.T) {
|
|
|
|
tmp := t.TempDir()
|
|
|
|
require.NoError(t, os.MkdirAll(path.Join(tmp, ".git"), 0755))
|
|
|
|
|
2023-12-19 16:23:23 +08:00
|
|
|
_, _, err := getGitAttributes(context.Background(), tmp, "Dockerfile")
|
2022-12-16 06:30:24 +08:00
|
|
|
assert.Error(t, err)
|
2022-12-13 18:20:04 +08:00
|
|
|
}
|
2022-08-20 18:25:44 +08:00
|
|
|
|
2022-12-13 18:20:04 +08:00
|
|
|
func TestGetGitAttributesNoContext(t *testing.T) {
|
2022-12-16 06:30:24 +08:00
|
|
|
setupTest(t)
|
2022-08-20 18:25:44 +08:00
|
|
|
|
2023-12-19 16:23:23 +08:00
|
|
|
gitattrs, _, err := getGitAttributes(context.Background(), "", "Dockerfile")
|
2022-12-16 06:30:24 +08:00
|
|
|
assert.NoError(t, err)
|
2022-12-13 18:20:04 +08:00
|
|
|
assert.Empty(t, gitattrs)
|
2022-08-20 18:25:44 +08:00
|
|
|
}
|
|
|
|
|
2022-12-13 18:20:04 +08:00
|
|
|
func TestGetGitAttributes(t *testing.T) {
|
|
|
|
cases := []struct {
|
|
|
|
name string
|
|
|
|
envGitLabels string
|
|
|
|
envGitInfo string
|
|
|
|
expected []string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "default",
|
|
|
|
envGitLabels: "",
|
|
|
|
envGitInfo: "",
|
|
|
|
expected: []string{
|
|
|
|
"vcs:revision",
|
2022-12-16 06:30:24 +08:00
|
|
|
"vcs:source",
|
2022-12-13 18:20:04 +08:00
|
|
|
},
|
|
|
|
},
|
2022-12-14 16:45:13 +08:00
|
|
|
{
|
|
|
|
name: "none",
|
|
|
|
envGitLabels: "false",
|
|
|
|
envGitInfo: "false",
|
|
|
|
expected: []string{},
|
|
|
|
},
|
2022-12-13 18:20:04 +08:00
|
|
|
{
|
|
|
|
name: "gitinfo",
|
|
|
|
envGitLabels: "false",
|
|
|
|
envGitInfo: "true",
|
|
|
|
expected: []string{
|
|
|
|
"vcs:revision",
|
2022-12-16 06:30:24 +08:00
|
|
|
"vcs:source",
|
2022-12-13 18:20:04 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "gitlabels",
|
|
|
|
envGitLabels: "true",
|
|
|
|
envGitInfo: "false",
|
|
|
|
expected: []string{
|
|
|
|
"label:" + DockerfileLabel,
|
|
|
|
"label:" + specs.AnnotationRevision,
|
2022-12-16 06:30:24 +08:00
|
|
|
"label:" + specs.AnnotationSource,
|
2022-12-13 18:20:04 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "both",
|
|
|
|
envGitLabels: "true",
|
|
|
|
envGitInfo: "",
|
|
|
|
expected: []string{
|
|
|
|
"label:" + DockerfileLabel,
|
|
|
|
"label:" + specs.AnnotationRevision,
|
2022-12-16 06:30:24 +08:00
|
|
|
"label:" + specs.AnnotationSource,
|
2022-12-13 18:20:04 +08:00
|
|
|
"vcs:revision",
|
2022-12-16 06:30:24 +08:00
|
|
|
"vcs:source",
|
2022-12-13 18:20:04 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range cases {
|
|
|
|
tt := tt
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2022-12-16 06:30:24 +08:00
|
|
|
setupTest(t)
|
2022-12-13 18:20:04 +08:00
|
|
|
if tt.envGitLabels != "" {
|
|
|
|
t.Setenv("BUILDX_GIT_LABELS", tt.envGitLabels)
|
|
|
|
}
|
|
|
|
if tt.envGitInfo != "" {
|
|
|
|
t.Setenv("BUILDX_GIT_INFO", tt.envGitInfo)
|
|
|
|
}
|
2023-12-19 16:23:23 +08:00
|
|
|
gitattrs, _, err := getGitAttributes(context.Background(), ".", "Dockerfile")
|
2022-12-16 06:30:24 +08:00
|
|
|
require.NoError(t, err)
|
2022-12-13 18:20:04 +08:00
|
|
|
for _, e := range tt.expected {
|
|
|
|
assert.Contains(t, gitattrs, e)
|
|
|
|
assert.NotEmpty(t, gitattrs[e])
|
|
|
|
if e == "label:"+DockerfileLabel {
|
|
|
|
assert.Equal(t, "Dockerfile", gitattrs[e])
|
2022-12-16 06:30:24 +08:00
|
|
|
} else if e == "label:"+specs.AnnotationSource || e == "vcs:source" {
|
|
|
|
assert.Equal(t, "git@github.com:docker/buildx.git", gitattrs[e])
|
2022-12-13 18:20:04 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2022-08-20 18:25:44 +08:00
|
|
|
}
|
|
|
|
|
2022-12-13 18:20:04 +08:00
|
|
|
func TestGetGitAttributesDirty(t *testing.T) {
|
2022-12-16 06:30:24 +08:00
|
|
|
setupTest(t)
|
2023-02-24 20:18:23 +08:00
|
|
|
t.Setenv("BUILDX_GIT_CHECK_DIRTY", "true")
|
2022-08-20 18:25:44 +08:00
|
|
|
|
|
|
|
// make a change to test dirty flag
|
|
|
|
df := []byte("FROM alpine:edge\n")
|
2022-12-16 06:30:24 +08:00
|
|
|
require.NoError(t, os.Mkdir("dir", 0755))
|
|
|
|
require.NoError(t, os.WriteFile(filepath.Join("dir", "Dockerfile"), df, 0644))
|
2022-12-13 18:20:04 +08:00
|
|
|
|
|
|
|
t.Setenv("BUILDX_GIT_LABELS", "true")
|
2023-12-19 16:23:23 +08:00
|
|
|
gitattrs, _, _ := getGitAttributes(context.Background(), ".", "Dockerfile")
|
2022-12-16 06:30:24 +08:00
|
|
|
assert.Equal(t, 5, len(gitattrs))
|
|
|
|
|
2022-12-13 18:20:04 +08:00
|
|
|
assert.Contains(t, gitattrs, "label:"+DockerfileLabel)
|
|
|
|
assert.Equal(t, "Dockerfile", gitattrs["label:"+DockerfileLabel])
|
2022-12-16 06:30:24 +08:00
|
|
|
assert.Contains(t, gitattrs, "label:"+specs.AnnotationSource)
|
|
|
|
assert.Equal(t, "git@github.com:docker/buildx.git", gitattrs["label:"+specs.AnnotationSource])
|
2022-12-13 18:20:04 +08:00
|
|
|
assert.Contains(t, gitattrs, "label:"+specs.AnnotationRevision)
|
|
|
|
assert.True(t, strings.HasSuffix(gitattrs["label:"+specs.AnnotationRevision], "-dirty"))
|
2022-12-16 06:30:24 +08:00
|
|
|
|
|
|
|
assert.Contains(t, gitattrs, "vcs:source")
|
|
|
|
assert.Equal(t, "git@github.com:docker/buildx.git", gitattrs["vcs:source"])
|
2022-12-13 18:20:04 +08:00
|
|
|
assert.Contains(t, gitattrs, "vcs:revision")
|
|
|
|
assert.True(t, strings.HasSuffix(gitattrs["vcs:revision"], "-dirty"))
|
2022-08-20 18:25:44 +08:00
|
|
|
}
|
2023-12-19 16:23:23 +08:00
|
|
|
|
|
|
|
func TestLocalDirs(t *testing.T) {
|
|
|
|
setupTest(t)
|
|
|
|
|
|
|
|
so := &client.SolveOpt{
|
|
|
|
FrontendAttrs: map[string]string{},
|
|
|
|
}
|
|
|
|
|
|
|
|
_, addVCSLocalDir, err := getGitAttributes(context.Background(), ".", "Dockerfile")
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, addVCSLocalDir)
|
|
|
|
|
2024-03-06 17:05:03 +08:00
|
|
|
require.NoError(t, setLocalMount("context", ".", so, addVCSLocalDir))
|
2023-12-19 16:23:23 +08:00
|
|
|
require.Contains(t, so.FrontendAttrs, "vcs:localdir:context")
|
|
|
|
assert.Equal(t, ".", so.FrontendAttrs["vcs:localdir:context"])
|
2024-03-06 17:05:03 +08:00
|
|
|
|
|
|
|
require.NoError(t, setLocalMount("dockerfile", ".", so, addVCSLocalDir))
|
2023-12-19 16:23:23 +08:00
|
|
|
require.Contains(t, so.FrontendAttrs, "vcs:localdir:dockerfile")
|
|
|
|
assert.Equal(t, ".", so.FrontendAttrs["vcs:localdir:dockerfile"])
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLocalDirsSub(t *testing.T) {
|
|
|
|
gitutil.Mktmp(t)
|
|
|
|
|
|
|
|
c, err := gitutil.New()
|
|
|
|
require.NoError(t, err)
|
|
|
|
gitutil.GitInit(c, t)
|
|
|
|
|
|
|
|
df := []byte("FROM alpine:latest\n")
|
|
|
|
assert.NoError(t, os.MkdirAll("app", 0755))
|
|
|
|
assert.NoError(t, os.WriteFile("app/Dockerfile", df, 0644))
|
|
|
|
|
|
|
|
gitutil.GitAdd(c, t, "app/Dockerfile")
|
|
|
|
gitutil.GitCommit(c, t, "initial commit")
|
|
|
|
gitutil.GitSetRemote(c, t, "origin", "git@github.com:docker/buildx.git")
|
|
|
|
|
|
|
|
so := &client.SolveOpt{
|
|
|
|
FrontendAttrs: map[string]string{},
|
|
|
|
}
|
|
|
|
|
|
|
|
_, addVCSLocalDir, err := getGitAttributes(context.Background(), ".", "app/Dockerfile")
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, addVCSLocalDir)
|
|
|
|
|
2024-03-06 17:05:03 +08:00
|
|
|
require.NoError(t, setLocalMount("context", ".", so, addVCSLocalDir))
|
2023-12-19 16:23:23 +08:00
|
|
|
require.Contains(t, so.FrontendAttrs, "vcs:localdir:context")
|
|
|
|
assert.Equal(t, ".", so.FrontendAttrs["vcs:localdir:context"])
|
2024-03-06 17:05:03 +08:00
|
|
|
|
|
|
|
require.NoError(t, setLocalMount("dockerfile", "app", so, addVCSLocalDir))
|
2023-12-19 16:23:23 +08:00
|
|
|
require.Contains(t, so.FrontendAttrs, "vcs:localdir:dockerfile")
|
|
|
|
assert.Equal(t, "app", so.FrontendAttrs["vcs:localdir:dockerfile"])
|
|
|
|
}
|