-
Notifications
You must be signed in to change notification settings - Fork 67
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #45 from containerops/develop
Add more test codes and fix the break when run in HTTP mode.
- Loading branch information
Showing
7 changed files
with
340 additions
and
4 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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package main | ||
|
||
import () | ||
|
||
//it will be supported soon |
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 |
---|---|---|
@@ -0,0 +1,210 @@ | ||
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 | ||
repotags := []string{"latest", "1.0", "2.0"} | ||
|
||
_, registry, err := getCurrentVersion() | ||
if err != nil { | ||
t.Fatal(err.Error()) | ||
} | ||
|
||
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) | ||
} | ||
} | ||
|
||
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") | ||
} | ||
} | ||
} | ||
|
||
if err := chkAndDelAllRepoTags(repoDest, []string{"latest"}); 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 | ||
} | ||
*/ |
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 |
---|---|---|
@@ -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 | ||
} | ||
*/ |
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 |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package test | ||
|
||
import () | ||
|
||
var DockerBinary string = "docker" | ||
var Domains string = "containerops.me" | ||
var UserName string = "mabin" | ||
|
||
var RktBinary string = "rkt" |
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 |
---|---|---|
@@ -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 | ||
} |