Skip to content

Commit

Permalink
tests: Convert internal/ tests to subtests (#316)
Browse files Browse the repository at this point in the history
  • Loading branch information
cognifloyd committed Apr 22, 2022
1 parent 05443ce commit 873ae4e
Show file tree
Hide file tree
Showing 13 changed files with 271 additions and 135 deletions.
8 changes: 7 additions & 1 deletion internal/build/snapshot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,24 +73,28 @@ func TestBuild_Snapshot(t *testing.T) {
}

tests := []struct {
name string
build *library.Build
client *vela.Client
err error
repo *library.Repo
}{
{
name: "build with error",
build: b,
client: _client,
err: errors.New("unable to create network"),
repo: r,
},
{
name: "nil build with error",
build: nil,
client: _client,
err: errors.New("unable to create network"),
repo: r,
},
{
name: "nil everything",
build: nil,
client: nil,
err: nil,
Expand All @@ -100,6 +104,8 @@ func TestBuild_Snapshot(t *testing.T) {

// run test
for _, test := range tests {
Snapshot(test.build, test.client, test.err, nil, test.repo)
t.Run(test.name, func(t *testing.T) {
Snapshot(test.build, test.client, test.err, nil, test.repo)
})
}
}
11 changes: 10 additions & 1 deletion internal/build/upload_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,42 +82,49 @@ func TestBuild_Upload(t *testing.T) {
}

tests := []struct {
name string
build *library.Build
client *vela.Client
err error
repo *library.Repo
}{
{
name: "build with error",
build: _build,
client: _client,
err: errors.New("unable to create network"),
repo: _repo,
},
{
name: "canceled build with error",
build: &_canceled,
client: _client,
err: errors.New("unable to create network"),
repo: _repo,
},
{
name: "errored build with error",
build: &_error,
client: _client,
err: errors.New("unable to create network"),
repo: _repo,
},
{
name: "pending build with error",
build: &_pending,
client: _client,
err: errors.New("unable to create network"),
repo: _repo,
},
{
name: "nil build with error",
build: nil,
client: _client,
err: errors.New("unable to create network"),
repo: _repo,
},
{
name: "everything nil",
build: nil,
client: nil,
err: nil,
Expand All @@ -127,6 +134,8 @@ func TestBuild_Upload(t *testing.T) {

// run test
for _, test := range tests {
Upload(test.build, test.client, test.err, nil, test.repo)
t.Run(test.name, func(t *testing.T) {
Upload(test.build, test.client, test.err, nil, test.repo)
})
}
}
60 changes: 41 additions & 19 deletions internal/image/image_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,93 +12,113 @@ import (
func TestImage_Parse(t *testing.T) {
// setup tests
tests := []struct {
name string
image string
want string
}{
{
name: "image only",
image: "golang",
want: "docker.io/library/golang:latest",
},
{
name: "image and tag",
image: "golang:latest",
want: "docker.io/library/golang:latest",
},
{
name: "repo and image",
image: "library/golang",
want: "docker.io/library/golang:latest",
},
{
name: "repo image and tag",
image: "library/golang:1.14",
want: "docker.io/library/golang:1.14",
},
{
name: "hub repo and image",
image: "docker.io/library/golang",
want: "docker.io/library/golang:latest",
},
{
name: "hub repo image and tag",
image: "docker.io/library/golang:latest",
want: "docker.io/library/golang:latest",
},
{
name: "alt hub with repo and image",
image: "index.docker.io/library/golang",
want: "docker.io/library/golang:latest",
},
{
name: "alt hub with repo image and tag",
image: "index.docker.io/library/golang:latest",
want: "docker.io/library/golang:latest",
},
{
name: "gcr hub with repo and image",
image: "gcr.io/library/golang",
want: "gcr.io/library/golang:latest",
},
{
name: "gcr hub with repo image and tag",
image: "gcr.io/library/golang:latest",
want: "gcr.io/library/golang:latest",
},
{
name: "garbage in garbage out",
image: "!@#$%^&*()",
want: "!@#$%^&*()",
},
}

// run tests
for _, test := range tests {
got := Parse(test.image)
t.Run(test.name, func(t *testing.T) {
got := Parse(test.image)

if !strings.EqualFold(got, test.want) {
t.Errorf("Parse is %s want %s", got, test.want)
}
if !strings.EqualFold(got, test.want) {
t.Errorf("Parse is %s want %s", got, test.want)
}
})
}
}

func TestImage_ParseWithError(t *testing.T) {
// setup tests
tests := []struct {
name string
failure bool
image string
want string
}{
{
name: "image only",
failure: false,
image: "golang",
want: "docker.io/library/golang:latest",
},
{
name: "image and tag",
failure: false,
image: "golang:latest",
want: "docker.io/library/golang:latest",
},
{
name: "image and tag",
failure: false,
image: "golang:1.14",
want: "docker.io/library/golang:1.14",
},
{
name: "fails with bad image",
failure: true,
image: "!@#$%^&*()",
want: "!@#$%^&*()",
},
{
name: "fails with image sha",
failure: true,
image: "1a3f5e7d9c1b3a5f7e9d1c3b5a7f9e1d3c5b7a9f1e3d5d7c9b1a3f5e7d9c1b3a",
want: "sha256:1a3f5e7d9c1b3a5f7e9d1c3b5a7f9e1d3c5b7a9f1e3d5d7c9b1a3f5e7d9c1b3a",
Expand All @@ -107,27 +127,29 @@ func TestImage_ParseWithError(t *testing.T) {

// run tests
for _, test := range tests {
got, err := ParseWithError(test.image)
t.Run(test.name, func(t *testing.T) {
got, err := ParseWithError(test.image)

if test.failure {
if err == nil {
t.Errorf("ParseWithError should have returned err")
}

if test.failure {
if err == nil {
t.Errorf("ParseWithError should have returned err")
if !strings.EqualFold(got, test.want) {
t.Errorf("ParseWithError is %s want %s", got, test.want)
}

return // continue to next test
}

if err != nil {
t.Errorf("ParseWithError returned err: %v", err)
}

if !strings.EqualFold(got, test.want) {
t.Errorf("ParseWithError is %s want %s", got, test.want)
}

continue
}

if err != nil {
t.Errorf("ParseWithError returned err: %v", err)
}

if !strings.EqualFold(got, test.want) {
t.Errorf("ParseWithError is %s want %s", got, test.want)
}
})
}
}

Expand Down
25 changes: 15 additions & 10 deletions internal/service/environment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,20 +93,23 @@ func TestService_Environment(t *testing.T) {

// setup tests
tests := []struct {
name string
failure bool
build *library.Build
container *pipeline.Container
repo *library.Repo
service *library.Service
}{
{
name: "success",
failure: false,
build: b,
container: c,
repo: r,
service: s,
},
{
name: "nil failure",
failure: true,
build: nil,
container: nil,
Expand All @@ -117,18 +120,20 @@ func TestService_Environment(t *testing.T) {

// run tests
for _, test := range tests {
err := Environment(test.container, test.build, test.repo, test.service, "v0.0.0")
t.Run(test.name, func(t *testing.T) {
err := Environment(test.container, test.build, test.repo, test.service, "v0.0.0")

if test.failure {
if err == nil {
t.Errorf("Environment should have returned err")
}
if test.failure {
if err == nil {
t.Errorf("Environment should have returned err")
}

continue
}
return // continue to next test
}

if err != nil {
t.Errorf("Environment returned err: %v", err)
}
if err != nil {
t.Errorf("Environment returned err: %v", err)
}
})
}
}
Loading

0 comments on commit 873ae4e

Please sign in to comment.