add test cases

Signed-off-by: Jess Frazelle <acidburn@microsoft.com>
This commit is contained in:
Jess Frazelle 2018-03-06 09:50:01 -05:00
parent 0cb8777fb1
commit e55cbf0fa3
No known key found for this signature in database
GPG key ID: 18F3685C0022BFF3
4 changed files with 120 additions and 15 deletions

37
delete_test.go Normal file
View file

@ -0,0 +1,37 @@
package main
import "testing"
func TestDelete(t *testing.T) {
// Make sure we have busybox in list.
out, err := run("ls")
if err != nil {
t.Fatalf("output: %s, error: %v", string(out), err)
}
expected := `Repositories for localhost:5000
REPO TAGS
busybox latest
alpine latest
`
if out != expected {
t.Fatalf("expected: %s\ngot: %s", expected, out)
}
// Remove busybox image.
if out, err := run("rm", "busybox"); err != nil {
t.Fatalf("output: %s, error: %v", string(out), err)
}
// Make sure there is no busybox in list.
out, err = run("ls")
if err != nil {
t.Fatalf("output: %s, error: %v", string(out), err)
}
expected = `Repositories for localhost:5000
REPO TAGS
alpine latest
`
if out != expected {
t.Fatalf("expected: %s\ngot: %s", expected, out)
}
}

View file

@ -1,10 +1,10 @@
package repoutils
import (
"errors"
"fmt"
"strings"
"github.com/docker/distribution/reference"
"github.com/docker/docker-ce/components/cli/cli/config"
"github.com/docker/docker/api/types"
)
@ -12,6 +12,8 @@ import (
const (
// DefaultDockerRegistry is the default docker registry address.
DefaultDockerRegistry = "https://registry-1.docker.io"
latestTagSuffix = ":latest"
)
// GetAuthConfig returns the docker registry AuthConfig.
@ -97,22 +99,21 @@ func GetAuthConfig(username, password, registry string) (types.AuthConfig, error
}
// GetRepoAndRef parses the repo name and reference.
func GetRepoAndRef(arg string) (repo, ref string, err error) {
if arg == "" {
return "", "", errors.New("pass the name of the repository")
func GetRepoAndRef(image string) (repo, ref string, err error) {
if image == "" {
return "", "", reference.ErrNameEmpty
}
image = addLatestTagSuffix(image)
var parts []string
if strings.Contains(arg, "@") {
parts = strings.Split(arg, "@")
} else if strings.Contains(arg, ":") {
parts = strings.Split(arg, ":")
} else {
parts = []string{arg}
if strings.Contains(image, "@") {
parts = strings.Split(image, "@")
} else if strings.Contains(image, ":") {
parts = strings.Split(image, ":")
}
repo = parts[0]
ref = "latest"
if len(parts) > 1 {
ref = parts[1]
}
@ -120,6 +121,14 @@ func GetRepoAndRef(arg string) (repo, ref string, err error) {
return
}
// addLatestTagSuffix adds :latest to the image if it does not have a tag
func addLatestTagSuffix(image string) string {
if !strings.Contains(image, ":") {
return image + latestTagSuffix
}
return image
}
func setDefaultRegistry(auth types.AuthConfig) types.AuthConfig {
if auth.ServerAddress == "docker.io" {
auth.ServerAddress = DefaultDockerRegistry

View file

@ -1 +1,58 @@
package repoutils
import (
"testing"
"github.com/docker/distribution/reference"
)
func TestGetRepoAndRef(t *testing.T) {
imageTestcases := []struct {
// input is the repository name or name component testcase
input string
// err is the error expected from Parse, or nil
err error
// repository is the string representation for the reference
repository string
// ref the reference
ref string
}{
{
input: "alpine",
repository: "alpine",
ref: "latest",
},
{
input: "docker:dind",
repository: "docker",
ref: "dind",
},
{
input: "",
err: reference.ErrNameEmpty,
},
{
input: "chrome@sha256:2a6c8ad38c41ae5122d76be59b34893d7fa1bdfaddd85bf0e57d0d16c0f7f91e",
repository: "chrome",
ref: "sha256:2a6c8ad38c41ae5122d76be59b34893d7fa1bdfaddd85bf0e57d0d16c0f7f91e",
},
}
for _, testcase := range imageTestcases {
repo, ref, err := GetRepoAndRef(testcase.input)
if err != nil {
if err.Error() != testcase.err.Error() {
t.Fatalf("%q: expected err (%v), got err (%v)", testcase.input, testcase.err, err)
}
continue
}
if testcase.repository != repo {
t.Fatalf("%q: expected repo (%s), got repo (%s)", testcase.input, testcase.repository, repo)
}
if testcase.ref != ref {
t.Fatalf("%q: expected ref (%s), got ref (%s)", testcase.input, testcase.ref, ref)
}
}
}

View file

@ -72,7 +72,11 @@ func StartRegistry(dcli *client.Client, config, username, password string) (stri
return r.ID, addr, err
}
if err := prefillRegistry(dcli, "localhost"+port, username, password); err != nil {
if err := prefillRegistry("alpine:latest", dcli, "localhost"+port, username, password); err != nil {
return r.ID, addr, err
}
if err := prefillRegistry("busybox:latest", dcli, "localhost"+port, username, password); err != nil {
return r.ID, addr, err
}
@ -99,9 +103,7 @@ func dockerLogin(addr, username, password string) error {
}
// prefillRegistry adds images to a registry.
func prefillRegistry(dcli *client.Client, addr, username, password string) error {
image := "alpine:latest"
func prefillRegistry(image string, dcli *client.Client, addr, username, password string) error {
if err := pullDockerImage(dcli, image); err != nil {
return err
}