Skip to content

Additional tests and use of github.com/stretchr/testify/assert #366

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package dockergen

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestParseWait(t *testing.T) {
incorrectIntervals := []string{
"500x", // Incorrect min interval
"500s:4x", // Incorrect max interval
"1m:1s", // Min interval larger than max interval
}

for _, intervalString := range incorrectIntervals {
wait, err := ParseWait(intervalString)
assert.Error(t, err)
assert.Nil(t, wait)
}

correctIntervals := map[string]Wait{
"": {0, 0}, // Empty time interval string
"1ms": {1000000, 4000000}, // Correct min interval without max
"1ms:111ms": {1000000, 111000000}, // Correct min:max time interval
}

for intervalString, expectedWait := range correctIntervals {
wait, err := ParseWait(intervalString)
assert.NoError(t, err)
assert.Equal(t, &expectedWait, wait)
}
}

func TestWaitUnmarshalText(t *testing.T) {
// Correct min:max time interval
intervalBytes := []byte("1ms:2ms")
expectedWait := &Wait{1000000, 2000000}
wait := new(Wait)
err := wait.UnmarshalText(intervalBytes)
assert.NoError(t, err)
assert.Equal(t, expectedWait, wait)
}
209 changes: 103 additions & 106 deletions docker_client_test.go
Original file line number Diff line number Diff line change
@@ -1,196 +1,193 @@
package dockergen

import (
"fmt"
"io/ioutil"
"log"
"os"
"testing"

"github.com/stretchr/testify/assert"
)

func TestSplitDockerImageRepository(t *testing.T) {
registry, repository, tag := splitDockerImage("ubuntu")

if registry != "" {
t.Fail()
}
if repository != "ubuntu" {
t.Fail()
}
if tag != "" {
t.Fail()
}
assert.Equal(t, "", registry)
assert.Equal(t, "ubuntu", repository)
assert.Equal(t, "", tag)

dockerImage := DockerImage{
Registry: registry,
Repository: repository,
Tag: tag,
}
if "ubuntu" != dockerImage.String() {
t.Fail()
}
assert.Equal(t, "ubuntu", dockerImage.String())
}

func TestSplitDockerImageWithRegistry(t *testing.T) {
registry, repository, tag := splitDockerImage("custom.registry/ubuntu")

if registry != "custom.registry" {
t.Fail()
}
if repository != "ubuntu" {
t.Fail()
}
if tag != "" {
t.Fail()
}
assert.Equal(t, "custom.registry", registry)
assert.Equal(t, "ubuntu", repository)
assert.Equal(t, "", tag)

dockerImage := DockerImage{
Registry: registry,
Repository: repository,
Tag: tag,
}
if "custom.registry/ubuntu" != dockerImage.String() {
t.Fail()
}

assert.Equal(t, "custom.registry/ubuntu", dockerImage.String())
}

func TestSplitDockerImageWithRegistryAndTag(t *testing.T) {
registry, repository, tag := splitDockerImage("custom.registry/ubuntu:12.04")

if registry != "custom.registry" {
t.Fail()
}
if repository != "ubuntu" {
t.Fail()
}
if tag != "12.04" {
t.Fail()
}
assert.Equal(t, "custom.registry", registry)
assert.Equal(t, "ubuntu", repository)
assert.Equal(t, "12.04", tag)

dockerImage := DockerImage{
Registry: registry,
Repository: repository,
Tag: tag,
}
if "custom.registry/ubuntu:12.04" != dockerImage.String() {
t.Fail()
}

assert.Equal(t, "custom.registry/ubuntu:12.04", dockerImage.String())
}

func TestSplitDockerImageWithRepositoryAndTag(t *testing.T) {
registry, repository, tag := splitDockerImage("ubuntu:12.04")

if registry != "" {
t.Fail()
}
assert.Equal(t, "", registry)
assert.Equal(t, "ubuntu", repository)
assert.Equal(t, "12.04", tag)

if repository != "ubuntu" {
t.Fail()
}

if tag != "12.04" {
t.Fail()
}
dockerImage := DockerImage{
Registry: registry,
Repository: repository,
Tag: tag,
}
if "ubuntu:12.04" != dockerImage.String() {
t.Fail()
}
assert.Equal(t, "ubuntu:12.04", dockerImage.String())
}

func TestSplitDockerImageWithPrivateRegistryPath(t *testing.T) {
registry, repository, tag := splitDockerImage("localhost:8888/ubuntu/foo:12.04")

if registry != "localhost:8888" {
t.Fail()
}

if repository != "ubuntu/foo" {
t.Fail()
}
assert.Equal(t, "localhost:8888", registry)
assert.Equal(t, "ubuntu/foo", repository)
assert.Equal(t, "12.04", tag)

if tag != "12.04" {
t.Fail()
}
dockerImage := DockerImage{
Registry: registry,
Repository: repository,
Tag: tag,
}
if "localhost:8888/ubuntu/foo:12.04" != dockerImage.String() {
t.Fail()
}
assert.Equal(t, "localhost:8888/ubuntu/foo:12.04", dockerImage.String())
}
func TestSplitDockerImageWithLocalRepositoryAndTag(t *testing.T) {
registry, repository, tag := splitDockerImage("localhost:8888/ubuntu:12.04")

if registry != "localhost:8888" {
t.Fatalf("registry does not match: expected %s got %s", "localhost:8888", registry)
}

if repository != "ubuntu" {
t.Fatalf("repository does not match: expected %s got %s", "ubuntu", repository)
}
assert.Equal(t, "localhost:8888", registry)
assert.Equal(t, "ubuntu", repository)
assert.Equal(t, "12.04", tag)

if tag != "12.04" {
t.Fatalf("tag does not match: expected %s got %s", "12.04", tag)
}
dockerImage := DockerImage{
Registry: registry,
Repository: repository,
Tag: tag,
}
if "localhost:8888/ubuntu:12.04" != dockerImage.String() {
t.Fail()
}

assert.Equal(t, "localhost:8888/ubuntu:12.04", dockerImage.String())
}

func TestParseHostUnix(t *testing.T) {
proto, addr, err := parseHost("unix:///var/run/docker.sock")
if err != nil {
t.Fatalf("%s", err)
}
if proto != "unix" || addr != "/var/run/docker.sock" {
t.Fatal("failed to parse unix:///var/run/docker.sock")
}
assert.NoError(t, err)
assert.Equal(t, "unix", proto, "failed to parse unix:///var/run/docker.sock")
assert.Equal(t, "/var/run/docker.sock", addr, "failed to parse unix:///var/run/docker.sock")
}

func TestParseHostUnixDefault(t *testing.T) {
proto, addr, err := parseHost("")
if err != nil {
t.Fatalf("%s", err)
}
if proto != "unix" || addr != "/var/run/docker.sock" {
t.Fatal("failed to parse ''")
}
assert.NoError(t, err)
assert.Equal(t, "unix", proto, "failed to parse ''")
assert.Equal(t, "/var/run/docker.sock", addr, "failed to parse ''")
}

func TestParseHostUnixDefaultNoPath(t *testing.T) {
proto, addr, err := parseHost("unix://")
if err != nil {
t.Fatalf("%s", err)
}
if proto != "unix" || addr != "/var/run/docker.sock" {
t.Fatal("failed to parse unix://")
}
assert.NoError(t, err)
assert.Equal(t, "unix", proto, "failed to parse unix://")
assert.Equal(t, "/var/run/docker.sock", addr, "failed to parse unix://")
}

func TestParseHostTCP(t *testing.T) {
proto, addr, err := parseHost("tcp://127.0.0.1:4243")
if err != nil {
t.Fatalf("%s", err)
}
if proto != "tcp" || addr != "127.0.0.1:4243" {
t.Fatal("failed to parse tcp://127.0.0.1:4243")
}
assert.NoError(t, err)
assert.Equal(t, "tcp", proto, "failed to parse tcp://127.0.0.1:4243")
assert.Equal(t, "127.0.0.1:4243", addr, "failed to parse tcp://127.0.0.1:4243")
}

func TestParseHostTCPDefault(t *testing.T) {
proto, addr, err := parseHost("tcp://:4243")
if err != nil {
t.Fatalf("%s", err)
assert.NoError(t, err)
assert.Equal(t, "tcp", proto, "failed to parse tcp://:4243")
assert.Equal(t, "127.0.0.1:4243", addr, "failed to parse tcp://:4243")
}

func TestParseHostSystemd(t *testing.T) {
proto, addr, err := parseHost("fd://")
assert.NoError(t, err)
assert.Equal(t, "fd", proto, "failed to parse fd://")
assert.Equal(t, "fd://", addr, "failed to parse fd://")
}

func assertParseHostError(t *testing.T, address string) {
proto, addr, err := parseHost(address)
message := fmt.Sprintf("should have failed to parse %v", address)
assert.Error(t, err, message)
assert.Equal(t, "", proto, message)
assert.Equal(t, "", addr, message)
}

func TestParseHostTCPNoAddressError(t *testing.T) {
assertParseHostError(t, "tcp://")
}

func TestParseHostTCPIncorrectBindAddressError(t *testing.T) {
incorrectBindAdresses := []string{
"tcp://127.0.0.1:4243:80",
"tcp://127.0.0.1:",
"tcp://127.0.0.1",
}

for _, address := range incorrectBindAdresses {
assertParseHostError(t, address)
}
if proto != "tcp" || addr != "127.0.0.1:4243" {
t.Fatal("failed to parse unix:///var/run/docker.sock")
}

func TestParseHostWrongProtocolError(t *testing.T) {
assertParseHostError(t, "foo://")
}

func TestTlsEnabled(t *testing.T) {
tls := tlsEnabled("foo", "bar", "baz")
assert.False(t, tls)

filepaths := map[string]string{
"cert": "",
"caCert": "",
"key": "",
}
// Create temporary files
for key := range filepaths {
file, err := ioutil.TempFile("", key)
if err != nil {
log.Fatal(err)
}
defer os.Remove(file.Name())
filepaths[key] = file.Name()
}

tls = tlsEnabled(filepaths["cert"], filepaths["caCert"], filepaths["key"])
assert.True(t, tls)
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ require (
github.com/opencontainers/runc v0.1.1 // indirect
github.com/opencontainers/selinux v1.8.0 // indirect
github.com/sirupsen/logrus v1.8.1 // indirect
github.com/stretchr/testify v1.7.0 // indirect
github.com/stretchr/testify v1.7.0
golang.org/x/sys v0.0.0-20210331175145-43e1dd70ce54 // indirect
gotest.tools v2.2.0+incompatible // indirect
)
Loading