From 6f37ae8f4bb50d1bc71aabca12ef61964fd8a04b Mon Sep 17 00:00:00 2001 From: MabinGo Date: Fri, 21 Aug 2015 20:39:01 +0800 Subject: [PATCH 1/3] Init dockyard testsuite Signed-off-by: MabinGo --- test/docker/docker_cli_login_test.go | 5 + test/docker/docker_cli_pull_test.go | 196 +++++++++++++++++++++++++++ test/docker/docker_cli_push_test.go | 87 ++++++++++++ test/setting.go | 9 ++ test/utils.go | 21 +++ 5 files changed, 318 insertions(+) create mode 100644 test/docker/docker_cli_login_test.go create mode 100644 test/docker/docker_cli_pull_test.go create mode 100644 test/docker/docker_cli_push_test.go create mode 100644 test/setting.go create mode 100644 test/utils.go diff --git a/test/docker/docker_cli_login_test.go b/test/docker/docker_cli_login_test.go new file mode 100644 index 0000000..1c11956 --- /dev/null +++ b/test/docker/docker_cli_login_test.go @@ -0,0 +1,5 @@ +package main + +import () + +//it will be supported soon diff --git a/test/docker/docker_cli_pull_test.go b/test/docker/docker_cli_pull_test.go new file mode 100644 index 0000000..59cc888 --- /dev/null +++ b/test/docker/docker_cli_pull_test.go @@ -0,0 +1,196 @@ +package main + +import ( + "fmt" + "os/exec" + "strings" + "testing" + + "github.com/containerops/dockyard/test" +) + +func TestPullInit(t *testing.T) { + var cmd *exec.Cmd + var err error + var out string + + reponame := "busybox" + repotags := []string{"latest", "1.0", "2.0"} + repoBase := reponame + ":" + repotags[0] + + if err = exec.Command(test.DockerBinary, "inspect", repoBase).Run(); err != nil { + cmd = exec.Command(test.DockerBinary, "pull", repoBase) + if out, err = test.ParseCmdCtx(cmd); err != nil { + t.Fatalf("Pull testing preparation is failed: [Info]%v, [Error]%v", out, err) + } + } + + repoDest := test.Domains + "/" + test.UserName + "/" + reponame + for _, v := range repotags { + tag := repoDest + ":" + v + cmd = exec.Command(test.DockerBinary, "tag", "-f", repoBase, tag) + if out, err = test.ParseCmdCtx(cmd); err != nil { + t.Fatalf("Tag %v failed: [Info]%v, [Error]%v", repoBase, out, err) + } + cmd = exec.Command(test.DockerBinary, "push", tag) + if out, err = test.ParseCmdCtx(cmd); err != nil { + t.Fatalf("Push all tags %v failed: [Info]%v, [Error]%v", tag, out, err) + } + } +} + +func getCurrentVersion() (string, string, error) { + cmd := exec.Command(test.DockerBinary, "-v") + out, err := test.ParseCmdCtx(cmd) + if err != nil { + return "", "", fmt.Errorf("Get docker version failed") + } + + curVer := strings.Split(strings.Split(out, ",")[0], " ")[2] + val := test.Compare(curVer, "1.6.0") + if val < 0 { + return curVer, "V1", nil + } else { + return curVer, "V2", nil + } +} + +func chkAndDelAllRepoTags(repoBase string, repotags []string) error { + for _, v := range repotags { + repotag := repoBase + ":" + v + if err := exec.Command(test.DockerBinary, "inspect", repotag).Run(); err != nil { + return fmt.Errorf(err.Error()) + } + + cmd := exec.Command(test.DockerBinary, "rmi", repotag) + if _, err := test.ParseCmdCtx(cmd); err != nil { + return fmt.Errorf(err.Error()) + } + } + return nil +} + +func TestPullRepoSingleTag(t *testing.T) { + var cmd *exec.Cmd + var err error + var out string + + reponame := "busybox" + repotags := []string{"latest"} + repoDest := test.Domains + "/" + test.UserName + "/" + reponame + + for i := 1; i <= 2; i++ { + cmd = exec.Command(test.DockerBinary, "pull", repoDest+":"+repotags[0]) + if out, err = test.ParseCmdCtx(cmd); err != nil { + t.Fatalf("Pull %v failed: [Info]%v, [Error]%v", repoDest+":"+repotags[0], out, err) + } + } + + if err := chkAndDelAllRepoTags(repoDest, repotags); err != nil { + t.Fatalf("Pull %v failed, it is not found in location. [Error]%v", repoDest, err.Error()) + } +} + +//if there are multiple tags in registry,if protocol V2 pulling is not specified tag, default to get latest tag,different from protocol V1 +func TestPullV2RepoWithoutTags(t *testing.T) { + var cmd *exec.Cmd + var err error + var out string + + reponame := "busybox" + repoDest := test.Domains + "/" + test.UserName + "/" + reponame + + _, registry, err := getCurrentVersion() + if err != nil { + t.Fatal(err.Error()) + } + + if registry == "V2" { + for i := 1; i <= 2; i++ { + cmd = exec.Command(test.DockerBinary, "pull", repoDest) + if out, err = test.ParseCmdCtx(cmd); err != nil { + t.Fatalf("Pull %v failed: [Info]%v, [Error]%v", repoDest, out, err) + } + /* + if !strings.Contains(out, "Using default tag: latest") { + t.Fatalf("Not expected result") + } + */ + } + + repotags := []string{"latest"} + if err := chkAndDelAllRepoTags(repoDest, repotags); err != nil { + t.Fatalf("Pull %v failed, it is not found in location. [Error]%v", repoDest, err.Error()) + } + } +} + +func TestPullRepoMultipleTags(t *testing.T) { + var cmd *exec.Cmd + var err error + var out string + + reponame := "busybox" + repotags := []string{"latest", "1.0", "2.0"} + repoDest := test.Domains + "/" + test.UserName + "/" + reponame + + curVer, registry, err := getCurrentVersion() + if err != nil { + t.Fatal(err.Error()) + } + + if !strings.Contains(curVer, "1.6") { + //Pull Repository Multiple Tags with option "-a",protocol V1 and V2 are all the same except Docker 1.6.x + for i := 1; i <= 2; i++ { + cmd = exec.Command(test.DockerBinary, "pull", "-a", repoDest) + if out, err = test.ParseCmdCtx(cmd); err != nil { + t.Fatalf("Pull all tags of %v failed: [Info]%v, [Error]%v", repoDest, out, err) + } + } + if err := chkAndDelAllRepoTags(repoDest, repotags); err != nil { + t.Fatalf("Pull all tags of %v failed, it is not found in location. [Error]%v", repoDest, err.Error()) + } + + if registry == "V1" { + //Docker daemon support to pull Repository Multiple Tags without option "-a" + for i := 1; i <= 2; i++ { + cmd = exec.Command(test.DockerBinary, "pull", repoDest) + if out, err = test.ParseCmdCtx(cmd); err != nil { + t.Fatalf("Pull all tags of %v failed: [Info]%v, [Error]%v", repoDest, out, err) + } + } + if err := chkAndDelAllRepoTags(repoDest, repotags); err != nil { + t.Fatalf("Pull all tags of %v failed, it is not found in location. [Error]%v", repoDest, err.Error()) + } + + //if there are multiple tags in registry,pull specified tag will get all tags of repository + for i := 1; i <= 2; i++ { + cmd = exec.Command(test.DockerBinary, "pull", repoDest+":"+repotags[0]) + if out, err = test.ParseCmdCtx(cmd); err != nil { + t.Fatalf("Pull all tags of %v failed: [Info]%v, [Error]%v", repoDest, out, err) + } + } + if err := chkAndDelAllRepoTags(repoDest, repotags); err != nil { + t.Fatalf("Pull all tags of %v failed, it is not found in location. [Error]%v", repoDest, err.Error()) + } + } + } else { + //There is a bug about "pull -a" in Docker 1.6.x,it will be change from protocol V2 to V1 if using "pull -a" + } +} + +func TestPullNonExistentRepo(t *testing.T) { + repoDest := test.Domains + "/" + test.UserName + "/" + "nonexistentrepo" + cmd := exec.Command(test.DockerBinary, "pull", repoDest) + if out, err := test.ParseCmdCtx(cmd); err == nil { + t.Fatalf("Pull %v failed: [Info]%v, [Error]%v", repoDest, out, err) + } +} + +/* +func Example_random() { + fmt.Println("mabintest") + //output: + //mabintest +} +*/ diff --git a/test/docker/docker_cli_push_test.go b/test/docker/docker_cli_push_test.go new file mode 100644 index 0000000..ba3a699 --- /dev/null +++ b/test/docker/docker_cli_push_test.go @@ -0,0 +1,87 @@ +package main + +import ( + "os/exec" + "testing" + + "github.com/containerops/dockyard/test" +) + +func TestPushInit(t *testing.T) { + repoBase := "busybox:latest" + + if err := exec.Command(test.DockerBinary, "inspect", repoBase).Run(); err != nil { + cmd := exec.Command(test.DockerBinary, "pull", repoBase) + if out, err := test.ParseCmdCtx(cmd); err != nil { + t.Fatalf("Push testing preparation is failed: [Info]%v, [Error]%v", out, err) + } + } +} + +func TestPushRepoWithSingleTag(t *testing.T) { + var cmd *exec.Cmd + var err error + var out string + + reponame := "busybox" + repotag := "latest" + repoBase := reponame + ":" + repotag + + repoDest := test.Domains + "/" + test.UserName + "/" + repoBase + cmd = exec.Command(test.DockerBinary, "tag", "-f", repoBase, repoDest) + if out, err = test.ParseCmdCtx(cmd); err != nil { + t.Fatalf("Tag %v failed: [Info]%v, [Error]%v", repoBase, out, err) + } + + //push the same repository with specified tag more than once to cover related code processing branch + for i := 1; i <= 2; i++ { + cmd = exec.Command(test.DockerBinary, "push", repoDest) + if out, err = test.ParseCmdCtx(cmd); err != nil { + t.Fatalf("Push %v failed: [Info]%v, [Error]%v", repoDest, out, err) + } + } + + cmd = exec.Command(test.DockerBinary, "rmi", repoDest) + out, err = test.ParseCmdCtx(cmd) +} + +func TestPushRepoWithMultipleTags(t *testing.T) { + var cmd *exec.Cmd + var err error + var out string + + reponame := "busybox" + repotags := []string{"latest", "1.0", "2.0"} + repoBase := reponame + ":" + repotags[0] //pull busybox:latest from docker hub + + repoDest := test.Domains + "/" + test.UserName + "/" + reponame + for _, v := range repotags { + tag := repoDest + ":" + v + cmd = exec.Command(test.DockerBinary, "tag", "-f", repoBase, tag) + if out, err = test.ParseCmdCtx(cmd); err != nil { + t.Fatalf("Tag %v failed: [Info]%v, [Error]%v", repoBase, out, err) + } + } + + //push the same repository with multiple tags more than once to cover related code processing branch + for i := 1; i <= 2; i++ { + cmd = exec.Command(test.DockerBinary, "push", repoDest) + if out, err = test.ParseCmdCtx(cmd); err != nil { + t.Fatalf("Push all tags %v failed: [Info]%v, [Error]%v", repoDest, out, err) + } + } + + for _, v := range repotags { + tag := repoDest + ":" + v + cmd = exec.Command(test.DockerBinary, "rmi", tag) + out, err = test.ParseCmdCtx(cmd) + } +} + +/* +func Example_random() { + fmt.Println("mabintest") + //output: + //mabintest +} +*/ diff --git a/test/setting.go b/test/setting.go new file mode 100644 index 0000000..2ca6a02 --- /dev/null +++ b/test/setting.go @@ -0,0 +1,9 @@ +package test + +import () + +var DockerBinary string = "docker" +var Domains string = "containerops.me" +var UserName string = "mabin" + +var RktBinary string = "rkt" diff --git a/test/utils.go b/test/utils.go new file mode 100644 index 0000000..aa5e256 --- /dev/null +++ b/test/utils.go @@ -0,0 +1,21 @@ +package test + +import ( + "os/exec" +) + +func ParseCmdCtx(cmd *exec.Cmd) (output string, err error) { + out, err := cmd.CombinedOutput() + output = string(out) + return output, err +} + +func Compare(a, b string) int { + if a == b { + return 0 + } + if a < b { + return -1 + } + return +1 +} From 33ce5b94c4754948856161a432c4d8de0126b7ab Mon Sep 17 00:00:00 2001 From: MabinGo Date: Sat, 22 Aug 2015 22:25:40 +0800 Subject: [PATCH 2/3] Fix bug about listen mode setting is invalid in runtime.conf --- handler/blob.go | 9 ++++++--- handler/manifests.go | 3 ++- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/handler/blob.go b/handler/blob.go index ccb6350..09283af 100644 --- a/handler/blob.go +++ b/handler/blob.go @@ -45,7 +45,8 @@ func PostBlobsV2Handler(ctx *macaron.Context, log *logs.BeeLogger) (int, []byte) uuid := utils.MD5(uuid.NewV4().String()) state := utils.MD5(fmt.Sprintf("%s/%s/%s", namespace, repository, time.Now().UnixNano()/int64(time.Millisecond))) - random := fmt.Sprintf("https://%s/v2/%s/%s/blobs/uploads/%s?_state=%s", + random := fmt.Sprintf("%s://%s/v2/%s/%s/blobs/uploads/%s?_state=%s", + setting.ListenMode, setting.Domains, namespace, repository, @@ -88,7 +89,8 @@ func PatchBlobsV2Handler(ctx *macaron.Context, log *logs.BeeLogger) (int, []byte } state := utils.MD5(fmt.Sprintf("%s/%s/%s", namespace, repository, time.Now().UnixNano()/int64(time.Millisecond))) - random := fmt.Sprintf("https://%s/v2/%s/%s/blobs/uploads/%s?_state=%s", + random := fmt.Sprintf("%s://%s/v2/%s/%s/blobs/uploads/%s?_state=%s", + setting.ListenMode, setting.Domains, namespace, repository, @@ -131,7 +133,8 @@ func PutBlobsV2Handler(ctx *macaron.Context, log *logs.BeeLogger) (int, []byte) return http.StatusBadRequest, result } - random := fmt.Sprintf("https://%s/v2/%s/%s/blobs/%s", + random := fmt.Sprintf("%s://%s/v2/%s/%s/blobs/%s", + setting.ListenMode, setting.Domains, ctx.Params(":namespace"), ctx.Params(":repository"), diff --git a/handler/manifests.go b/handler/manifests.go index ea89966..0e72465 100644 --- a/handler/manifests.go +++ b/handler/manifests.go @@ -45,7 +45,8 @@ func PutManifestsV2Handler(ctx *macaron.Context, log *logs.BeeLogger) (int, []by return http.StatusBadRequest, result } - random := fmt.Sprintf("https://%v/v2/%v/%v/manifests/%v", + random := fmt.Sprintf("%s://%s/v2/%s/%s/manifests/%s", + setting.ListenMode, setting.Domains, namespace, repository, From 94a5c0d8dab5b2fbe31ce12bcfa3fb721568a753 Mon Sep 17 00:00:00 2001 From: MabinGo Date: Sat, 22 Aug 2015 22:47:01 +0800 Subject: [PATCH 3/3] Modify pull Test suite --- test/docker/docker_cli_pull_test.go | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/test/docker/docker_cli_pull_test.go b/test/docker/docker_cli_pull_test.go index 59cc888..d779049 100644 --- a/test/docker/docker_cli_pull_test.go +++ b/test/docker/docker_cli_pull_test.go @@ -99,6 +99,7 @@ func TestPullV2RepoWithoutTags(t *testing.T) { reponame := "busybox" repoDest := test.Domains + "/" + test.UserName + "/" + reponame + repotags := []string{"latest", "1.0", "2.0"} _, registry, err := getCurrentVersion() if err != nil { @@ -106,20 +107,33 @@ func TestPullV2RepoWithoutTags(t *testing.T) { } if registry == "V2" { + for _, v := range repotags { + tag := repoDest + ":" + v + if err = exec.Command(test.DockerBinary, "inspect", tag).Run(); err == nil { + cmd = exec.Command(test.DockerBinary, "rmi", tag) + if out, err = test.ParseCmdCtx(cmd); err != nil { + t.Fatalf("Pull testing preparation is failed: [Info]%v, [Error]%v", out, err) + } + } + } + for i := 1; i <= 2; i++ { cmd = exec.Command(test.DockerBinary, "pull", repoDest) if out, err = test.ParseCmdCtx(cmd); err != nil { t.Fatalf("Pull %v failed: [Info]%v, [Error]%v", repoDest, out, err) } - /* - if !strings.Contains(out, "Using default tag: latest") { - t.Fatalf("Not expected result") + } + + for _, v := range repotags { + if v != "latest" { + tag := repoDest + ":" + v + if err = exec.Command(test.DockerBinary, "inspect", tag).Run(); err == nil { + t.Fatalf("Not expect result") } - */ + } } - repotags := []string{"latest"} - if err := chkAndDelAllRepoTags(repoDest, repotags); err != nil { + if err := chkAndDelAllRepoTags(repoDest, []string{"latest"}); err != nil { t.Fatalf("Pull %v failed, it is not found in location. [Error]%v", repoDest, err.Error()) } }