Skip to content

Commit

Permalink
Merge pull request #45 from containerops/develop
Browse files Browse the repository at this point in the history
Add more test codes and fix the break when run in HTTP mode.
  • Loading branch information
Quanyi Ma committed Aug 24, 2015
2 parents ea1558d + ba88c66 commit 34a8a42
Show file tree
Hide file tree
Showing 7 changed files with 340 additions and 4 deletions.
9 changes: 6 additions & 3 deletions handler/blob.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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"),
Expand Down
3 changes: 2 additions & 1 deletion handler/manifests.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
5 changes: 5 additions & 0 deletions test/docker/docker_cli_login_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package main

import ()

//it will be supported soon
210 changes: 210 additions & 0 deletions test/docker/docker_cli_pull_test.go
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
}
*/
87 changes: 87 additions & 0 deletions test/docker/docker_cli_push_test.go
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
}
*/
9 changes: 9 additions & 0 deletions test/setting.go
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"
21 changes: 21 additions & 0 deletions test/utils.go
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
}

0 comments on commit 34a8a42

Please sign in to comment.