builder: modernize TestCheckoutGit
Make the test more debuggable by logging all git command output and running each table-driven test case as a subtest. Signed-off-by: Cory Snider <csnider@mirantis.com>
This commit is contained in:
parent
c964641a0d
commit
8816c3c2aa
1 changed files with 54 additions and 86 deletions
|
@ -2,7 +2,6 @@ package git // import "github.com/docker/docker/builder/remotecontext/git"
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
"net/url"
|
"net/url"
|
||||||
|
@ -171,9 +170,7 @@ func gitGetConfig(name string) string {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestCheckoutGit(t *testing.T) {
|
func TestCheckoutGit(t *testing.T) {
|
||||||
root, err := ioutil.TempDir("", "docker-build-git-checkout")
|
root := t.TempDir()
|
||||||
assert.NilError(t, err)
|
|
||||||
defer os.RemoveAll(root)
|
|
||||||
|
|
||||||
autocrlf := gitGetConfig("core.autocrlf")
|
autocrlf := gitGetConfig("core.autocrlf")
|
||||||
if !(autocrlf == "true" || autocrlf == "false" ||
|
if !(autocrlf == "true" || autocrlf == "false" ||
|
||||||
|
@ -185,88 +182,57 @@ func TestCheckoutGit(t *testing.T) {
|
||||||
eol = "\r\n"
|
eol = "\r\n"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
must := func(out []byte, err error) {
|
||||||
|
t.Helper()
|
||||||
|
if len(out) > 0 {
|
||||||
|
t.Logf("%s", out)
|
||||||
|
}
|
||||||
|
assert.NilError(t, err)
|
||||||
|
}
|
||||||
|
|
||||||
gitDir := filepath.Join(root, "repo")
|
gitDir := filepath.Join(root, "repo")
|
||||||
_, err = git("init", gitDir)
|
must(git("-c", "init.defaultBranch=master", "init", gitDir))
|
||||||
assert.NilError(t, err)
|
must(gitWithinDir(gitDir, "config", "user.email", "test@docker.com"))
|
||||||
|
must(gitWithinDir(gitDir, "config", "user.name", "Docker test"))
|
||||||
_, err = gitWithinDir(gitDir, "config", "user.email", "test@docker.com")
|
assert.NilError(t, os.WriteFile(filepath.Join(gitDir, "Dockerfile"), []byte("FROM scratch"), 0644))
|
||||||
assert.NilError(t, err)
|
|
||||||
|
|
||||||
_, err = gitWithinDir(gitDir, "config", "user.name", "Docker test")
|
|
||||||
assert.NilError(t, err)
|
|
||||||
|
|
||||||
err = ioutil.WriteFile(filepath.Join(gitDir, "Dockerfile"), []byte("FROM scratch"), 0644)
|
|
||||||
assert.NilError(t, err)
|
|
||||||
|
|
||||||
subDir := filepath.Join(gitDir, "subdir")
|
subDir := filepath.Join(gitDir, "subdir")
|
||||||
assert.NilError(t, os.Mkdir(subDir, 0755))
|
assert.NilError(t, os.Mkdir(subDir, 0755))
|
||||||
|
assert.NilError(t, os.WriteFile(filepath.Join(subDir, "Dockerfile"), []byte("FROM scratch\nEXPOSE 5000"), 0644))
|
||||||
err = ioutil.WriteFile(filepath.Join(subDir, "Dockerfile"), []byte("FROM scratch\nEXPOSE 5000"), 0644)
|
|
||||||
assert.NilError(t, err)
|
|
||||||
|
|
||||||
if runtime.GOOS != "windows" {
|
if runtime.GOOS != "windows" {
|
||||||
if err = os.Symlink("../subdir", filepath.Join(gitDir, "parentlink")); err != nil {
|
assert.NilError(t, os.Symlink("../subdir", filepath.Join(gitDir, "parentlink")))
|
||||||
t.Fatal(err)
|
assert.NilError(t, os.Symlink("/subdir", filepath.Join(gitDir, "absolutelink")))
|
||||||
}
|
}
|
||||||
|
|
||||||
if err = os.Symlink("/subdir", filepath.Join(gitDir, "absolutelink")); err != nil {
|
must(gitWithinDir(gitDir, "add", "-A"))
|
||||||
t.Fatal(err)
|
must(gitWithinDir(gitDir, "commit", "-am", "First commit"))
|
||||||
}
|
must(gitWithinDir(gitDir, "checkout", "-b", "test"))
|
||||||
}
|
|
||||||
|
|
||||||
_, err = gitWithinDir(gitDir, "add", "-A")
|
assert.NilError(t, os.WriteFile(filepath.Join(gitDir, "Dockerfile"), []byte("FROM scratch\nEXPOSE 3000"), 0644))
|
||||||
assert.NilError(t, err)
|
assert.NilError(t, os.WriteFile(filepath.Join(subDir, "Dockerfile"), []byte("FROM busybox\nEXPOSE 5000"), 0644))
|
||||||
|
|
||||||
_, err = gitWithinDir(gitDir, "commit", "-am", "First commit")
|
must(gitWithinDir(gitDir, "add", "-A"))
|
||||||
assert.NilError(t, err)
|
must(gitWithinDir(gitDir, "commit", "-am", "Branch commit"))
|
||||||
|
must(gitWithinDir(gitDir, "checkout", "master"))
|
||||||
_, err = gitWithinDir(gitDir, "checkout", "-b", "test")
|
|
||||||
assert.NilError(t, err)
|
|
||||||
|
|
||||||
err = ioutil.WriteFile(filepath.Join(gitDir, "Dockerfile"), []byte("FROM scratch\nEXPOSE 3000"), 0644)
|
|
||||||
assert.NilError(t, err)
|
|
||||||
|
|
||||||
err = ioutil.WriteFile(filepath.Join(subDir, "Dockerfile"), []byte("FROM busybox\nEXPOSE 5000"), 0644)
|
|
||||||
assert.NilError(t, err)
|
|
||||||
|
|
||||||
_, err = gitWithinDir(gitDir, "add", "-A")
|
|
||||||
assert.NilError(t, err)
|
|
||||||
|
|
||||||
_, err = gitWithinDir(gitDir, "commit", "-am", "Branch commit")
|
|
||||||
assert.NilError(t, err)
|
|
||||||
|
|
||||||
_, err = gitWithinDir(gitDir, "checkout", "master")
|
|
||||||
assert.NilError(t, err)
|
|
||||||
|
|
||||||
// set up submodule
|
// set up submodule
|
||||||
subrepoDir := filepath.Join(root, "subrepo")
|
subrepoDir := filepath.Join(root, "subrepo")
|
||||||
_, err = git("init", subrepoDir)
|
must(git("-c", "init.defaultBranch=master", "init", subrepoDir))
|
||||||
assert.NilError(t, err)
|
must(gitWithinDir(subrepoDir, "config", "user.email", "test@docker.com"))
|
||||||
|
must(gitWithinDir(subrepoDir, "config", "user.name", "Docker test"))
|
||||||
|
|
||||||
_, err = gitWithinDir(subrepoDir, "config", "user.email", "test@docker.com")
|
assert.NilError(t, os.WriteFile(filepath.Join(subrepoDir, "subfile"), []byte("subcontents"), 0644))
|
||||||
assert.NilError(t, err)
|
|
||||||
|
|
||||||
_, err = gitWithinDir(subrepoDir, "config", "user.name", "Docker test")
|
must(gitWithinDir(subrepoDir, "add", "-A"))
|
||||||
assert.NilError(t, err)
|
must(gitWithinDir(subrepoDir, "commit", "-am", "Subrepo initial"))
|
||||||
|
|
||||||
err = ioutil.WriteFile(filepath.Join(subrepoDir, "subfile"), []byte("subcontents"), 0644)
|
|
||||||
assert.NilError(t, err)
|
|
||||||
|
|
||||||
_, err = gitWithinDir(subrepoDir, "add", "-A")
|
|
||||||
assert.NilError(t, err)
|
|
||||||
|
|
||||||
_, err = gitWithinDir(subrepoDir, "commit", "-am", "Subrepo initial")
|
|
||||||
assert.NilError(t, err)
|
|
||||||
|
|
||||||
cmd := exec.Command("git", "submodule", "add", subrepoDir, "sub") // this command doesn't work with --work-tree
|
cmd := exec.Command("git", "submodule", "add", subrepoDir, "sub") // this command doesn't work with --work-tree
|
||||||
cmd.Dir = gitDir
|
cmd.Dir = gitDir
|
||||||
assert.NilError(t, cmd.Run())
|
must(cmd.CombinedOutput())
|
||||||
|
|
||||||
_, err = gitWithinDir(gitDir, "add", "-A")
|
must(gitWithinDir(gitDir, "add", "-A"))
|
||||||
assert.NilError(t, err)
|
must(gitWithinDir(gitDir, "commit", "-am", "With submodule"))
|
||||||
|
|
||||||
_, err = gitWithinDir(gitDir, "commit", "-am", "With submodule")
|
|
||||||
assert.NilError(t, err)
|
|
||||||
|
|
||||||
type singleCase struct {
|
type singleCase struct {
|
||||||
frag string
|
frag string
|
||||||
|
@ -300,17 +266,18 @@ func TestCheckoutGit(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, c := range cases {
|
for _, c := range cases {
|
||||||
|
t.Run(c.frag, func(t *testing.T) {
|
||||||
ref, subdir := getRefAndSubdir(c.frag)
|
ref, subdir := getRefAndSubdir(c.frag)
|
||||||
r, err := cloneGitRepo(gitRepo{remote: gitDir, ref: ref, subdir: subdir})
|
r, err := cloneGitRepo(gitRepo{remote: gitDir, ref: ref, subdir: subdir})
|
||||||
|
|
||||||
if c.fail {
|
if c.fail {
|
||||||
assert.Check(t, is.ErrorContains(err, ""))
|
assert.Check(t, is.ErrorContains(err, ""))
|
||||||
continue
|
return
|
||||||
}
|
}
|
||||||
assert.NilError(t, err)
|
assert.NilError(t, err)
|
||||||
defer os.RemoveAll(r)
|
defer os.RemoveAll(r)
|
||||||
if c.submodule {
|
if c.submodule {
|
||||||
b, err := ioutil.ReadFile(filepath.Join(r, "sub/subfile"))
|
b, err := os.ReadFile(filepath.Join(r, "sub/subfile"))
|
||||||
assert.NilError(t, err)
|
assert.NilError(t, err)
|
||||||
assert.Check(t, is.Equal("subcontents", string(b)))
|
assert.Check(t, is.Equal("subcontents", string(b)))
|
||||||
} else {
|
} else {
|
||||||
|
@ -319,9 +286,10 @@ func TestCheckoutGit(t *testing.T) {
|
||||||
assert.Assert(t, os.IsNotExist(err))
|
assert.Assert(t, os.IsNotExist(err))
|
||||||
}
|
}
|
||||||
|
|
||||||
b, err := ioutil.ReadFile(filepath.Join(r, "Dockerfile"))
|
b, err := os.ReadFile(filepath.Join(r, "Dockerfile"))
|
||||||
assert.NilError(t, err)
|
assert.NilError(t, err)
|
||||||
assert.Check(t, is.Equal(c.exp, string(b)))
|
assert.Check(t, is.Equal(c.exp, string(b)))
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue