Skip to content
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

Use assert for tests #3201

Merged
merged 4 commits into from
Jan 14, 2024
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
25 changes: 12 additions & 13 deletions cli/internal/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,20 @@

package internal

import "testing"
import (
"testing"

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

func TestParseKeyPair(t *testing.T) {
s := []string{"FOO=bar", "BAR=", "BAZ=qux=quux", "INVALID"}
p := ParseKeyPair(s)
if p["FOO"] != "bar" {
t.Errorf("Wanted %q, got %q.", "bar", p["FOO"])
}
if p["BAZ"] != "qux=quux" {
t.Errorf("Wanted %q, got %q.", "qux=quux", p["BAZ"])
}
if _, exists := p["BAR"]; !exists {
t.Error("Missing a key with no value. Keys with empty values are also valid.")
}
if _, exists := p["INVALID"]; exists {
t.Error("Keys without an equal sign suffix are invalid.")
}
assert.Equal(t, "bar", p["FOO"])
assert.Equal(t, "qux=quux", p["BAZ"])
val, exists := p["BAR"]
assert.Empty(t, val)
assert.True(t, exists, "missing a key with no value, keys with empty values are also valid")
_, exists = p["INVALID"]
assert.False(t, exists, "keys without an equal sign suffix are invalid")
}
6 changes: 3 additions & 3 deletions cmd/agent/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@ func TestReadAgentIDFileNotExists(t *testing.T) {
func TestReadAgentIDFileExists(t *testing.T) {
tmpF, errTmpF := os.CreateTemp("", "tmp_")
if !assert.NoError(t, errTmpF) {
t.FailNow()
return
}
defer os.Remove(tmpF.Name())

// there is an existing config
errWrite := os.WriteFile(tmpF.Name(), []byte(`{"agent_id":3}`), 0o644)
if !assert.NoError(t, errWrite) {
t.FailNow()
return
}

// read existing config
Expand All @@ -50,7 +50,7 @@ func TestReadAgentIDFileExists(t *testing.T) {

tmpF2, errTmpF := os.CreateTemp("", "tmp_")
if !assert.NoError(t, errTmpF) {
t.FailNow()
return
}
defer os.Remove(tmpF2.Name())

Expand Down
26 changes: 8 additions & 18 deletions cmd/agent/health_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import (
"testing"
"time"

"github.com/stretchr/testify/assert"

"go.woodpecker-ci.org/woodpecker/v2/agent"
)

Expand All @@ -27,35 +29,23 @@ func TestHealthy(t *testing.T) {

s.Add("1", time.Hour, "octocat/hello-world", "42")

if got, want := s.Metadata["1"].ID, "1"; got != want {
t.Errorf("got ID %s, want %s", got, want)
}
if got, want := s.Metadata["1"].Timeout, time.Hour; got != want {
t.Errorf("got duration %v, want %v", got, want)
}
if got, want := s.Metadata["1"].Repo, "octocat/hello-world"; got != want {
t.Errorf("got repository name %s, want %s", got, want)
}
assert.Equal(t, "1", s.Metadata["1"].ID)
assert.Equal(t, time.Hour, s.Metadata["1"].Timeout)
assert.Equal(t, "octocat/hello-world", s.Metadata["1"].Repo)

s.Metadata["1"] = agent.Info{
Timeout: time.Hour,
Started: time.Now().UTC(),
}
if s.Healthy() == false {
t.Error("want healthy status when timeout not exceeded, got false")
}
assert.True(t, s.Healthy(), "want healthy status when timeout not exceeded, got false")

s.Metadata["1"] = agent.Info{
Started: time.Now().UTC().Add(-(time.Minute * 30)),
}
if s.Healthy() == false {
t.Error("want healthy status when timeout+buffer not exceeded, got false")
}
assert.True(t, s.Healthy(), "want healthy status when timeout+buffer not exceeded, got false")

s.Metadata["1"] = agent.Info{
Started: time.Now().UTC().Add(-(time.Hour + time.Minute)),
}
if s.Healthy() == true {
t.Error("want unhealthy status when timeout+buffer not exceeded, got true")
}
assert.False(t, s.Healthy(), "want unhealthy status when timeout+buffer not exceeded, got true")
}
4 changes: 1 addition & 3 deletions pipeline/backend/docker/convert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,7 @@ func TestSplitVolumeParts(t *testing.T) {
for _, test := range testdata {
results, err := splitVolumeParts(test.from)
if test.success != (err == nil) {
if reflect.DeepEqual(results, test.to) != test.success {
t.Errorf("Expect %q matches %q is %v", test.from, results, test.to)
}
assert.Equal(t, test.success, reflect.DeepEqual(results, test.to))
}
}
}
Expand Down
12 changes: 4 additions & 8 deletions pipeline/error_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,21 @@ package pipeline

import (
"testing"

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

func TestExitError(t *testing.T) {
err := ExitError{
UUID: "14534321",
Code: 255,
}
got, want := err.Error(), "uuid=14534321: exit code 255"
if got != want {
t.Errorf("Want error message %q, got %q", want, got)
}
assert.Equal(t, "uuid=14534321: exit code 255", err.Error())
}

func TestOomError(t *testing.T) {
err := OomError{
UUID: "14534321",
}
got, want := err.Error(), "uuid=14534321: received oom kill"
if got != want {
t.Errorf("Want error message %q, got %q", want, got)
}
assert.Equal(t, "uuid=14534321: received oom kill", err.Error())
}
4 changes: 1 addition & 3 deletions pipeline/errors/error_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,6 @@ func TestHasBlockingErrors(t *testing.T) {
}

for _, test := range tests {
if pipeline_errors.HasBlockingErrors(test.err) != test.expected {
t.Error("Should only return true if there are blocking errors")
}
assert.Equal(t, test.expected, pipeline_errors.HasBlockingErrors(test.err))
}
}
134 changes: 37 additions & 97 deletions pipeline/frontend/yaml/compiler/option_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@
package compiler

import (
"reflect"
"testing"

"github.com/stretchr/testify/assert"

"go.woodpecker-ci.org/woodpecker/v2/pipeline/frontend/metadata"
)

Expand All @@ -28,12 +29,8 @@ func TestWithWorkspace(t *testing.T) {
"src/github.com/octocat/hello-world",
),
)
if compiler.base != "/pipeline" {
t.Errorf("WithWorkspace must set the base directory")
}
if compiler.path != "src/github.com/octocat/hello-world" {
t.Errorf("WithWorkspace must set the path directory")
}
assert.Equal(t, "/pipeline", compiler.base)
assert.Equal(t, "src/github.com/octocat/hello-world", compiler.path)
}

func TestWithEscalated(t *testing.T) {
Expand All @@ -43,9 +40,8 @@ func TestWithEscalated(t *testing.T) {
"docker-dev",
),
)
if compiler.escalated[0] != "docker" || compiler.escalated[1] != "docker-dev" {
t.Errorf("WithEscalated must whitelist privileged images")
}
assert.Equal(t, "docker", compiler.escalated[0])
assert.Equal(t, "docker-dev", compiler.escalated[1])
}

func TestWithVolumes(t *testing.T) {
Expand All @@ -55,9 +51,8 @@ func TestWithVolumes(t *testing.T) {
"/foo:/foo",
),
)
if compiler.volumes[0] != "/tmp:/tmp" || compiler.volumes[1] != "/foo:/foo" {
t.Errorf("TestWithVolumes must set default volumes")
}
assert.Equal(t, "/tmp:/tmp", compiler.volumes[0])
assert.Equal(t, "/foo:/foo", compiler.volumes[1])
}

func TestWithNetworks(t *testing.T) {
Expand All @@ -67,9 +62,8 @@ func TestWithNetworks(t *testing.T) {
"overlay_bar",
),
)
if compiler.networks[0] != "overlay_1" || compiler.networks[1] != "overlay_bar" {
t.Errorf("TestWithNetworks must set networks from parameters")
}
assert.Equal(t, "overlay_1", compiler.networks[0])
assert.Equal(t, "overlay_bar", compiler.networks[1])
}

func TestWithResourceLimit(t *testing.T) {
Expand All @@ -83,30 +77,16 @@ func TestWithResourceLimit(t *testing.T) {
"0,2-5",
),
)
if compiler.reslimit.MemSwapLimit != 1 {
t.Errorf("TestWithResourceLimit must set MemSwapLimit from parameters")
}
if compiler.reslimit.MemLimit != 2 {
t.Errorf("TestWithResourceLimit must set MemLimit from parameters")
}
if compiler.reslimit.ShmSize != 3 {
t.Errorf("TestWithResourceLimit must set ShmSize from parameters")
}
if compiler.reslimit.CPUQuota != 4 {
t.Errorf("TestWithResourceLimit must set CPUQuota from parameters")
}
if compiler.reslimit.CPUShares != 5 {
t.Errorf("TestWithResourceLimit must set CPUShares from parameters")
}
if compiler.reslimit.CPUSet != "0,2-5" {
t.Errorf("TestWithResourceLimit must set CPUSet from parameters")
}
assert.EqualValues(t, 1, compiler.reslimit.MemSwapLimit)
assert.EqualValues(t, 2, compiler.reslimit.MemLimit)
assert.EqualValues(t, 3, compiler.reslimit.ShmSize)
assert.EqualValues(t, 4, compiler.reslimit.CPUQuota)
assert.EqualValues(t, 5, compiler.reslimit.CPUShares)
assert.Equal(t, "0,2-5", compiler.reslimit.CPUSet)
}

func TestWithPrefix(t *testing.T) {
if New(WithPrefix("someprefix_")).prefix != "someprefix_" {
t.Errorf("WithPrefix must set the prefix")
}
assert.Equal(t, "someprefix_", New(WithPrefix("someprefix_")).prefix)
}

func TestWithMetadata(t *testing.T) {
Expand All @@ -122,28 +102,16 @@ func TestWithMetadata(t *testing.T) {
compiler := New(
WithMetadata(metadata),
)
if !reflect.DeepEqual(compiler.metadata, metadata) {
t.Errorf("WithMetadata must set compiler the metadata")
}

if compiler.env["CI_REPO_NAME"] != metadata.Repo.Name {
t.Errorf("WithMetadata must set CI_REPO_NAME")
}
if compiler.env["CI_REPO_URL"] != metadata.Repo.ForgeURL {
t.Errorf("WithMetadata must set CI_REPO_URL")
}
if compiler.env["CI_REPO_CLONE_URL"] != metadata.Repo.CloneURL {
t.Errorf("WithMetadata must set CI_REPO_CLONE_URL")
}
assert.Equal(t, metadata, compiler.metadata)
assert.Equal(t, metadata.Repo.Name, compiler.env["CI_REPO_NAME"])
assert.Equal(t, metadata.Repo.ForgeURL, compiler.env["CI_REPO_URL"])
assert.Equal(t, metadata.Repo.CloneURL, compiler.env["CI_REPO_CLONE_URL"])
}

func TestWithLocal(t *testing.T) {
if New(WithLocal(true)).local == false {
t.Errorf("WithLocal true must enable the local flag")
}
if New(WithLocal(false)).local == true {
t.Errorf("WithLocal false must disable the local flag")
}
assert.True(t, New(WithLocal(true)).local)
assert.False(t, New(WithLocal(false)).local)
}

func TestWithNetrc(t *testing.T) {
Expand All @@ -154,15 +122,9 @@ func TestWithNetrc(t *testing.T) {
"github.com",
),
)
if compiler.cloneEnv["CI_NETRC_USERNAME"] != "octocat" {
t.Errorf("WithNetrc should set CI_NETRC_USERNAME")
}
if compiler.cloneEnv["CI_NETRC_PASSWORD"] != "password" {
t.Errorf("WithNetrc should set CI_NETRC_PASSWORD")
}
if compiler.cloneEnv["CI_NETRC_MACHINE"] != "github.com" {
t.Errorf("WithNetrc should set CI_NETRC_MACHINE")
}
assert.Equal(t, "octocat", compiler.cloneEnv["CI_NETRC_USERNAME"])
assert.Equal(t, "password", compiler.cloneEnv["CI_NETRC_PASSWORD"])
assert.Equal(t, "github.com", compiler.cloneEnv["CI_NETRC_MACHINE"])
}

func TestWithProxy(t *testing.T) {
Expand All @@ -187,9 +149,7 @@ func TestWithProxy(t *testing.T) {
}),
)
for key, value := range testdata {
if compiler.env[key] != value {
t.Errorf("WithProxy should set %s=%s", key, value)
}
assert.Equal(t, value, compiler.env[key])
}
}

Expand All @@ -202,54 +162,34 @@ func TestWithEnviron(t *testing.T) {
},
),
)
if compiler.env["RACK_ENV"] != "development" {
t.Errorf("WithEnviron should set RACK_ENV")
}
if compiler.env["SHOW"] != "true" {
t.Errorf("WithEnviron should set SHOW")
}
assert.Equal(t, "development", compiler.env["RACK_ENV"])
assert.Equal(t, "true", compiler.env["SHOW"])
}

func TestWithVolumeCacher(t *testing.T) {
compiler := New(
WithVolumeCacher("/cache"),
)
cacher, ok := compiler.cacher.(*volumeCacher)
if !ok {
t.Errorf("Expected volume cacher configured")
}
if got, want := cacher.base, "/cache"; got != want {
t.Errorf("Expected volume cacher with base %s, got %s", want, got)
}
assert.True(t, ok)
assert.Equal(t, "/cache", cacher.base)
}

func TestWithDefaultCloneImage(t *testing.T) {
compiler := New(
WithDefaultCloneImage("not-an-image"),
)
if compiler.defaultCloneImage != "not-an-image" {
t.Errorf("Expected default clone image 'not-an-image' not found")
}
assert.Equal(t, "not-an-image", compiler.defaultCloneImage)
}

func TestWithS3Cacher(t *testing.T) {
compiler := New(
WithS3Cacher("some-access-key", "some-secret-key", "some-region", "some-bucket"),
)
cacher, ok := compiler.cacher.(*s3Cacher)
if !ok {
t.Errorf("Expected s3 cacher configured")
}
if got, want := cacher.bucket, "some-bucket"; got != want {
t.Errorf("Expected s3 cacher with bucket %s, got %s", want, got)
}
if got, want := cacher.access, "some-access-key"; got != want {
t.Errorf("Expected s3 cacher with access key %s, got %s", want, got)
}
if got, want := cacher.region, "some-region"; got != want {
t.Errorf("Expected s3 cacher with region %s, got %s", want, got)
}
if got, want := cacher.secret, "some-secret-key"; got != want {
t.Errorf("Expected s3 cacher with secret key %s, got %s", want, got)
}
assert.True(t, ok)
assert.Equal(t, "some-bucket", cacher.bucket)
assert.Equal(t, "some-access-key", cacher.access)
assert.Equal(t, "some-region", cacher.region)
assert.Equal(t, "some-secret-key", cacher.secret)
}
Loading
Loading