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

Migrate away from goblin #4624

Merged
merged 8 commits into from
Dec 30, 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
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ require (
github.com/docker/go-units v0.5.0
github.com/drone/envsubst v1.0.3
github.com/expr-lang/expr v1.16.9
github.com/franela/goblin v0.0.0-20211003143422-0a4f594942bf
github.com/fsnotify/fsnotify v1.8.0
github.com/gdgvda/cron v0.3.0
github.com/getkin/kin-openapi v0.128.1-0.20241224102021-cea0a13b906a
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,6 @@ github.com/fatih/color v1.18.0 h1:S8gINlzdQ840/4pfAwic/ZE0djQEH3wM94VfqLTZcOM=
github.com/fatih/color v1.18.0/go.mod h1:4FelSpRwEGDpQ12mAdzqdOukCy4u8WUtOY6lkT/6HfU=
github.com/felixge/httpsnoop v1.0.4 h1:NFTV2Zj1bL4mc9sqWACXbQFVBBg2W3GPvqp8/ESS2Wg=
github.com/felixge/httpsnoop v1.0.4/go.mod h1:m8KPJKqk1gH5J9DgRY2ASl2lWCfGKXixSwevea8zH2U=
github.com/franela/goblin v0.0.0-20211003143422-0a4f594942bf h1:NrF81UtW8gG2LBGkXFQFqlfNnvMt9WdB46sfdJY4oqc=
github.com/franela/goblin v0.0.0-20211003143422-0a4f594942bf/go.mod h1:VzmDKDJVZI3aJmnRI9VjAn9nJ8qPPsN1fqzr9dqInIo=
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ=
github.com/fsnotify/fsnotify v1.8.0 h1:dAwr6QBTBZIkG8roQaJjGof0pp0EeF+tNV7YBP3F/8M=
Expand Down
10 changes: 5 additions & 5 deletions pipeline/backend/kubernetes/kubernetes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ func TestGettingConfig(t *testing.T) {
assert.Equal(t, "default", engine.config.Namespace)
assert.Equal(t, "hdd", engine.config.StorageClass)
assert.Equal(t, "1G", engine.config.VolumeSize)
assert.Equal(t, false, engine.config.StorageRwx)
assert.Equal(t, 1, len(engine.config.PodLabels))
assert.Equal(t, 1, len(engine.config.PodAnnotations))
assert.Equal(t, 1, len(engine.config.ImagePullSecretNames))
assert.Equal(t, false, engine.config.SecurityContext.RunAsNonRoot)
assert.False(t, engine.config.StorageRwx)
assert.Len(t, engine.config.PodLabels, 1)
assert.Len(t, engine.config.PodAnnotations, 1)
assert.Len(t, engine.config.ImagePullSecretNames, 1)
assert.False(t, engine.config.SecurityContext.RunAsNonRoot)
}
4 changes: 2 additions & 2 deletions pipeline/backend/kubernetes/secrets_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func TestNativeSecretsEnabled(t *testing.T) {
nsp := newNativeSecretsProcessor(&config{
NativeSecretsAllowFromStep: true,
}, nil)
assert.Equal(t, true, nsp.isEnabled())
assert.True(t, nsp.isEnabled())
}

func TestNativeSecretsDisabled(t *testing.T) {
Expand All @@ -54,7 +54,7 @@ func TestNativeSecretsDisabled(t *testing.T) {
},
},
})
assert.Equal(t, false, nsp.isEnabled())
assert.False(t, nsp.isEnabled())

err := nsp.process()
assert.NoError(t, err)
Expand Down
52 changes: 22 additions & 30 deletions pipeline/frontend/yaml/matrix/matrix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,42 +17,34 @@ package matrix
import (
"testing"

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

func TestMatrix(t *testing.T) {
g := goblin.Goblin(t)
g.Describe("Calculate matrix", func() {
axis, _ := ParseString(fakeMatrix)
axis, _ := ParseString(fakeMatrix)
assert.Len(t, axis, 24)

g.It("Should calculate permutations", func() {
g.Assert(len(axis)).Equal(24)
})

g.It("Should not duplicate permutations", func() {
set := map[string]bool{}
for _, perm := range axis {
set[perm.String()] = true
}
g.Assert(len(set)).Equal(24)
})
set := map[string]bool{}
for _, perm := range axis {
set[perm.String()] = true
}
assert.Len(t, set, 24)
}

g.It("Should return empty array if no matrix", func() {
axis, err := ParseString("")
g.Assert(err).IsNil()
g.Assert(len(axis) == 0).IsTrue()
})
func TestMatrixEmpty(t *testing.T) {
axis, err := ParseString("")
assert.NoError(t, err)
assert.Empty(t, axis)
}

g.It("Should return included axis", func() {
axis, err := ParseString(fakeMatrixInclude)
g.Assert(err).IsNil()
g.Assert(len(axis)).Equal(2)
g.Assert(axis[0]["go_version"]).Equal("1.5")
g.Assert(axis[1]["go_version"]).Equal("1.6")
g.Assert(axis[0]["python_version"]).Equal("3.4")
g.Assert(axis[1]["python_version"]).Equal("3.4")
})
})
func TestMatrixIncluded(t *testing.T) {
axis, err := ParseString(fakeMatrixInclude)
assert.NoError(t, err)
assert.Len(t, axis, 2)
assert.Equal(t, "1.5", axis[0]["go_version"])
assert.Equal(t, "1.6", axis[1]["go_version"])
assert.Equal(t, "3.4", axis[0]["python_version"])
assert.Equal(t, "3.4", axis[1]["python_version"])
}

var fakeMatrix = `
Expand Down
227 changes: 102 additions & 125 deletions pipeline/frontend/yaml/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,127 +15,112 @@
package yaml

import (
"slices"
"testing"

"github.com/franela/goblin"
"github.com/stretchr/testify/assert"

"go.woodpecker-ci.org/woodpecker/v3/pipeline/frontend/metadata"
yaml_base_types "go.woodpecker-ci.org/woodpecker/v3/pipeline/frontend/yaml/types/base"
)

func TestParse(t *testing.T) {
g := goblin.Goblin(t)

g.Describe("Parser", func() {
g.Describe("Given a yaml file", func() {
g.It("Should unmarshal a string", func() {
out, err := ParseString(sampleYaml)
if err != nil {
g.Fail(err)
}

g.Assert(slices.Contains(out.When.Constraints[0].Event, "tester")).Equal(true)

g.Assert(out.Workspace.Base).Equal("/go")
g.Assert(out.Workspace.Path).Equal("src/github.com/octocat/hello-world")
g.Assert(out.Volumes.WorkflowVolumes[0].Name).Equal("custom")
g.Assert(out.Volumes.WorkflowVolumes[0].Driver).Equal("blockbridge")
g.Assert(out.Networks.WorkflowNetworks[0].Name).Equal("custom")
g.Assert(out.Networks.WorkflowNetworks[0].Driver).Equal("overlay")
g.Assert(out.Services.ContainerList[0].Name).Equal("database")
g.Assert(out.Services.ContainerList[0].Image).Equal("mysql")
g.Assert(out.Steps.ContainerList[0].Name).Equal("test")
g.Assert(out.Steps.ContainerList[0].Image).Equal("golang")
g.Assert(out.Steps.ContainerList[0].Commands).Equal(yaml_base_types.StringOrSlice{"go install", "go test"})
g.Assert(out.Steps.ContainerList[1].Name).Equal("build")
g.Assert(out.Steps.ContainerList[1].Image).Equal("golang")
g.Assert(out.Steps.ContainerList[1].Commands).Equal(yaml_base_types.StringOrSlice{"go build"})
g.Assert(out.Steps.ContainerList[2].Name).Equal("notify")
g.Assert(out.Steps.ContainerList[2].Image).Equal("slack")
// g.Assert(out.Steps.ContainerList[2].NetworkMode).Equal("container:name")
g.Assert(out.Labels["com.example.team"]).Equal("frontend")
g.Assert(out.Labels["com.example.type"]).Equal("build")
g.Assert(out.DependsOn[0]).Equal("lint")
g.Assert(out.DependsOn[1]).Equal("test")
g.Assert(out.RunsOn[0]).Equal("success")
g.Assert(out.RunsOn[1]).Equal("failure")
g.Assert(out.SkipClone).Equal(false)
})

g.It("Should handle simple yaml anchors", func() {
out, err := ParseString(simpleYamlAnchors)
if err != nil {
g.Fail(err)
}
g.Assert(out.Steps.ContainerList[0].Name).Equal("notify_success")
g.Assert(out.Steps.ContainerList[0].Image).Equal("plugins/slack")
})

g.It("Should unmarshal variables", func() {
out, err := ParseString(sampleVarYaml)
if err != nil {
g.Fail(err)
}
g.Assert(out.Steps.ContainerList[0].Name).Equal("notify_fail")
g.Assert(out.Steps.ContainerList[0].Image).Equal("plugins/slack")
g.Assert(out.Steps.ContainerList[1].Name).Equal("notify_success")
g.Assert(out.Steps.ContainerList[1].Image).Equal("plugins/slack")
t.Run("Should unmarshal a string", func(t *testing.T) {
out, err := ParseString(sampleYaml)
assert.NoError(t, err)

assert.Contains(t, out.When.Constraints[0].Event, "tester")

assert.Equal(t, "/go", out.Workspace.Base)
assert.Equal(t, "src/github.com/octocat/hello-world", out.Workspace.Path)
assert.Equal(t, "custom", out.Volumes.WorkflowVolumes[0].Name)
assert.Equal(t, "blockbridge", out.Volumes.WorkflowVolumes[0].Driver)
assert.Equal(t, "custom", out.Networks.WorkflowNetworks[0].Name)
assert.Equal(t, "overlay", out.Networks.WorkflowNetworks[0].Driver)
assert.Equal(t, "database", out.Services.ContainerList[0].Name)
assert.Equal(t, "mysql", out.Services.ContainerList[0].Image)
assert.Equal(t, "test", out.Steps.ContainerList[0].Name)
assert.Equal(t, "golang", out.Steps.ContainerList[0].Image)
assert.Equal(t, yaml_base_types.StringOrSlice{"go install", "go test"}, out.Steps.ContainerList[0].Commands)
assert.Equal(t, "build", out.Steps.ContainerList[1].Name)
assert.Equal(t, "golang", out.Steps.ContainerList[1].Image)
assert.Equal(t, yaml_base_types.StringOrSlice{"go build"}, out.Steps.ContainerList[1].Commands)
assert.Equal(t, "notify", out.Steps.ContainerList[2].Name)
assert.Equal(t, "slack", out.Steps.ContainerList[2].Image)
assert.Equal(t, "frontend", out.Labels["com.example.team"])
assert.Equal(t, "build", out.Labels["com.example.type"])
assert.Equal(t, "lint", out.DependsOn[0])
assert.Equal(t, "test", out.DependsOn[1])
assert.Equal(t, ("success"), out.RunsOn[0])
assert.Equal(t, ("failure"), out.RunsOn[1])
assert.False(t, out.SkipClone)
})

g.Assert(len(out.Steps.ContainerList[0].When.Constraints)).Equal(0)
g.Assert(out.Steps.ContainerList[1].Name).Equal("notify_success")
g.Assert(out.Steps.ContainerList[1].Image).Equal("plugins/slack")
g.Assert(out.Steps.ContainerList[1].When.Constraints[0].Event).Equal(yaml_base_types.StringOrSlice{"success"})
})
t.Run("Should handle simple yaml anchors", func(t *testing.T) {
out, err := ParseString(simpleYamlAnchors)
assert.NoError(t, err)
assert.Equal(t, "notify_success", out.Steps.ContainerList[0].Name)
assert.Equal(t, "plugins/slack", out.Steps.ContainerList[0].Image)
})

matchConfig, err := ParseString(sampleYaml)
if err != nil {
g.Fail(err)
}
t.Run("Should unmarshal variables", func(t *testing.T) {
out, err := ParseString(sampleVarYaml)
assert.NoError(t, err)
assert.Equal(t, "notify_fail", out.Steps.ContainerList[0].Name)
assert.Equal(t, "plugins/slack", out.Steps.ContainerList[0].Image)
assert.Equal(t, "notify_success", out.Steps.ContainerList[1].Name)
assert.Equal(t, "plugins/slack", out.Steps.ContainerList[1].Image)

assert.Empty(t, out.Steps.ContainerList[0].When.Constraints)
assert.Equal(t, "notify_success", out.Steps.ContainerList[1].Name)
assert.Equal(t, "plugins/slack", out.Steps.ContainerList[1].Image)
assert.Equal(t, yaml_base_types.StringOrSlice{"success"}, out.Steps.ContainerList[1].When.Constraints[0].Event)
})
}

g.It("Should match event tester", func() {
match, err := matchConfig.When.Match(metadata.Metadata{
Curr: metadata.Pipeline{
Event: "tester",
},
}, false, nil)
g.Assert(match).Equal(true)
g.Assert(err).IsNil()
})
func TestMatch(t *testing.T) {
matchConfig, err := ParseString(sampleYaml)
assert.NoError(t, err)

t.Run("Should match event tester", func(t *testing.T) {
match, err := matchConfig.When.Match(metadata.Metadata{
Curr: metadata.Pipeline{
Event: "tester",
},
}, false, nil)
assert.True(t, match)
assert.NoError(t, err)
})

g.It("Should match event tester2", func() {
match, err := matchConfig.When.Match(metadata.Metadata{
Curr: metadata.Pipeline{
Event: "tester2",
},
}, false, nil)
g.Assert(match).Equal(true)
g.Assert(err).IsNil()
})
t.Run("Should match event tester2", func(t *testing.T) {
match, err := matchConfig.When.Match(metadata.Metadata{
Curr: metadata.Pipeline{
Event: "tester2",
},
}, false, nil)
assert.True(t, match)
assert.NoError(t, err)
})

g.It("Should match branch tester", func() {
match, err := matchConfig.When.Match(metadata.Metadata{
Curr: metadata.Pipeline{
Commit: metadata.Commit{
Branch: "tester",
},
},
}, true, nil)
g.Assert(match).Equal(true)
g.Assert(err).IsNil()
})
t.Run("Should match branch tester", func(t *testing.T) {
match, err := matchConfig.When.Match(metadata.Metadata{
Curr: metadata.Pipeline{
Commit: metadata.Commit{
Branch: "tester",
},
},
}, true, nil)
assert.True(t, match)
assert.NoError(t, err)
})

g.It("Should not match event push", func() {
match, err := matchConfig.When.Match(metadata.Metadata{
Curr: metadata.Pipeline{
Event: "push",
},
}, false, nil)
g.Assert(match).Equal(false)
g.Assert(err).IsNil()
})
})
t.Run("Should not match event push", func(t *testing.T) {
match, err := matchConfig.When.Match(metadata.Metadata{
Curr: metadata.Pipeline{
Event: "push",
},
}, false, nil)
assert.False(t, match)
assert.NoError(t, err)
})
}

Expand Down Expand Up @@ -262,27 +247,19 @@ steps:
`

func TestSlice(t *testing.T) {
g := goblin.Goblin(t)

g.Describe("Parser", func() {
g.It("should marshal a not set slice to nil", func() {
out, err := ParseString(sampleSliceYaml)
if err != nil {
g.Fail(err)
}
t.Run("should marshal a not set slice to nil", func(t *testing.T) {
out, err := ParseString(sampleSliceYaml)
assert.NoError(t, err)

g.Assert(out.Steps.ContainerList[0].DependsOn).IsNil()
g.Assert(len(out.Steps.ContainerList[0].DependsOn)).Equal(0)
})
assert.Nil(t, out.Steps.ContainerList[0].DependsOn)
assert.Empty(t, out.Steps.ContainerList[0].DependsOn)
})

g.It("should marshal an empty slice", func() {
out, err := ParseString(sampleSliceYaml)
if err != nil {
g.Fail(err)
}
t.Run("should marshal an empty slice", func(t *testing.T) {
out, err := ParseString(sampleSliceYaml)
assert.NoError(t, err)

g.Assert(out.Steps.ContainerList[1].DependsOn).IsNotNil()
g.Assert(len(out.Steps.ContainerList[1].DependsOn)).Equal(0)
})
assert.NotNil(t, out.Steps.ContainerList[1].DependsOn)
assert.Empty(t, (out.Steps.ContainerList[1].DependsOn))
})
}
Loading