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

Add some tests #2652

Merged
merged 13 commits into from
Oct 28, 2023
16 changes: 8 additions & 8 deletions cmd/server/docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -1252,8 +1252,8 @@ const docTemplate = `{
}
],
"responses": {
"200": {
"description": "OK"
"204": {
"description": "No Content"
}
}
}
Expand All @@ -1278,8 +1278,8 @@ const docTemplate = `{
}
],
"responses": {
"200": {
"description": "OK"
"204": {
"description": "No Content"
}
}
}
Expand All @@ -1304,8 +1304,8 @@ const docTemplate = `{
}
],
"responses": {
"200": {
"description": "OK"
"204": {
"description": "No Content"
}
}
}
Expand Down Expand Up @@ -2055,8 +2055,8 @@ const docTemplate = `{
}
],
"responses": {
"200": {
"description": "OK"
"204": {
"description": "No Content"
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion pipeline/backend/kubernetes/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,6 @@ func TestService(t *testing.T) {

s, _ := Service("foo", "bar", "baz", []string{"1", "2", "3"})
j, err := json.Marshal(s)
assert.Nil(t, err)
assert.NoError(t, err)
assert.JSONEq(t, expected, string(j))
}
10 changes: 5 additions & 5 deletions pipeline/backend/kubernetes/volume_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,19 +65,19 @@ func TestPersistentVolumeClaim(t *testing.T) {
}`

pvc, err := PersistentVolumeClaim("someNamespace", "somename", "local-storage", "1Gi", true)
assert.Nil(t, err)
assert.NoError(t, err)

j, err := json.Marshal(pvc)
assert.Nil(t, err)
assert.NoError(t, err)
assert.JSONEq(t, expectedRwx, string(j))

pvc, err = PersistentVolumeClaim("someNamespace", "somename", "local-storage", "1Gi", false)
assert.Nil(t, err)
assert.NoError(t, err)

j, err = json.Marshal(pvc)
assert.Nil(t, err)
assert.NoError(t, err)
assert.JSONEq(t, expectedRwo, string(j))

_, err = PersistentVolumeClaim("someNamespace", "some0INVALID3name", "local-storage", "1Gi", false)
assert.NotNil(t, err)
assert.Error(t, err)
}
33 changes: 33 additions & 0 deletions pipeline/backend/local/const_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Copyright 2023 Woodpecker Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package local

import (
"testing"

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

func TestGenNetRC(t *testing.T) {
assert.Equal(t, `
machine machine
login user
password pass
`, genNetRC(map[string]string{
"CI_NETRC_MACHINE": "machine",
"CI_NETRC_USERNAME": "user",
"CI_NETRC_PASSWORD": "pass",
}))
}
8 changes: 4 additions & 4 deletions pipeline/frontend/yaml/types/base/base_types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func TestStringorIntYaml(t *testing.T) {
assert.Equal(t, StringOrInt(10), s.Foo)

d, err := yaml.Marshal(&s)
assert.Nil(t, err)
assert.NoError(t, err)

s2 := StructStringorInt{}
assert.NoError(t, yaml.Unmarshal(d, &s2))
Expand All @@ -57,7 +57,7 @@ func TestStringOrSliceYaml(t *testing.T) {
assert.Equal(t, StringOrSlice{"bar", "baz"}, s.Foo)

d, err := yaml.Marshal(&s)
assert.Nil(t, err)
assert.NoError(t, err)

s2 := StructStringOrSlice{}
assert.NoError(t, yaml.Unmarshal(d, &s2))
Expand All @@ -79,7 +79,7 @@ func TestSliceOrMapYaml(t *testing.T) {
assert.Equal(t, SliceOrMap{"bar": "baz", "far": "faz"}, s.Foos)

d, err := yaml.Marshal(&s)
assert.Nil(t, err)
assert.NoError(t, err)

s2 := StructSliceorMap{}
assert.NoError(t, yaml.Unmarshal(d, &s2))
Expand All @@ -106,7 +106,7 @@ func TestStr2SliceOrMapPtrMap(t *testing.T) {
Bars: []string{},
}}
d, err := yaml.Marshal(&s)
assert.Nil(t, err)
assert.NoError(t, err)

s2 := map[string]*StructSliceorMap{}
assert.NoError(t, yaml.Unmarshal(d, &s2))
Expand Down
2 changes: 1 addition & 1 deletion pipeline/frontend/yaml/types/network_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ network2:
}
for _, network := range networks {
bytes, err := yaml.Marshal(network.networks)
assert.Nil(t, err)
assert.NoError(t, err)
assert.Equal(t, network.expected, string(bytes), "should be equal")
}
}
Expand Down
4 changes: 2 additions & 2 deletions pipeline/frontend/yaml/types/volume_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func TestMarshalVolumes(t *testing.T) {
}
for _, volume := range volumes {
bytes, err := yaml.Marshal(volume.volumes)
assert.Nil(t, err)
assert.NoError(t, err)
assert.Equal(t, volume.expected, string(bytes), "should be equal")
}
}
Expand Down Expand Up @@ -151,7 +151,7 @@ func TestUnmarshalVolumes(t *testing.T) {
for _, volume := range volumes {
actual := &Volumes{}
err := yaml.Unmarshal([]byte(volume.yaml), actual)
assert.Nil(t, err)
assert.NoError(t, err)
assert.Equal(t, volume.expected, actual, "should be equal")
}
}
2 changes: 1 addition & 1 deletion pipeline/rpc/proto/woodpecker.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pipeline/rpc/proto/woodpecker_grpc.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions server/api/hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,33 +54,33 @@ func GetQueueInfo(c *gin.Context) {
// @Summary Pause a pipeline queue
// @Router /queue/pause [post]
// @Produce plain
// @Success 200
// @Success 204
// @Tags Pipeline queues
// @Param Authorization header string true "Insert your personal access token" default(Bearer <personal access token>)
func PauseQueue(c *gin.Context) {
server.Config.Services.Queue.Pause()
c.Status(http.StatusOK)
c.Status(http.StatusNoContent)
}

// ResumeQueue
//
// @Summary Resume a pipeline queue
// @Router /queue/resume [post]
// @Produce plain
// @Success 200
// @Success 204
// @Tags Pipeline queues
// @Param Authorization header string true "Insert your personal access token" default(Bearer <personal access token>)
func ResumeQueue(c *gin.Context) {
server.Config.Services.Queue.Resume()
c.Status(http.StatusOK)
c.Status(http.StatusNoContent)
}

// BlockTilQueueHasRunningItem
//
// @Summary Block til pipeline queue has a running item
// @Router /queue/norunningpipelines [get]
// @Produce plain
// @Success 200
// @Success 204
// @Tags Pipeline queues
// @Param Authorization header string true "Insert your personal access token" default(Bearer <personal access token>)
func BlockTilQueueHasRunningItem(c *gin.Context) {
Expand All @@ -90,7 +90,7 @@ func BlockTilQueueHasRunningItem(c *gin.Context) {
break
}
}
c.Status(http.StatusOK)
c.Status(http.StatusNoContent)
}

// PostHook
Expand Down
4 changes: 2 additions & 2 deletions server/api/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,7 @@ func RepairRepo(c *gin.Context) {
// @Summary Move a repository to a new owner
// @Router /repos/{repo_id}/move [post]
// @Produce plain
// @Success 200
// @Success 204
// @Tags Repositories
// @Param Authorization header string true "Insert your personal access token" default(Bearer <personal access token>)
// @Param repo_id path int true "the repository id"
Expand Down Expand Up @@ -493,7 +493,7 @@ func MoveRepo(c *gin.Context) {
c.String(http.StatusInternalServerError, err.Error())
return
}
c.Status(http.StatusOK)
c.Status(http.StatusNoContent)
}

// GetAllRepos
Expand Down
33 changes: 33 additions & 0 deletions server/badges/badges_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Copyright 2022 Woodpecker Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package badges

import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/woodpecker-ci/woodpecker/server/model"
)

// Generate an SVG badge based on a pipeline
func TestGenerate(t *testing.T) {
assert.Equal(t, badgeNone, Generate(nil))
assert.Equal(t, badgeSuccess, Generate(&model.Pipeline{Status: model.StatusSuccess}))
assert.Equal(t, badgeFailure, Generate(&model.Pipeline{Status: model.StatusFailure}))
assert.Equal(t, badgeError, Generate(&model.Pipeline{Status: model.StatusError}))
assert.Equal(t, badgeError, Generate(&model.Pipeline{Status: model.StatusKilled}))
assert.Equal(t, badgeStarted, Generate(&model.Pipeline{Status: model.StatusPending}))
assert.Equal(t, badgeStarted, Generate(&model.Pipeline{Status: model.StatusRunning}))
}
3 changes: 0 additions & 3 deletions server/forge/github/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,6 @@ func parsePushHook(hook *github.PushEvent) (*model.Repo, *model.Pipeline, error)
if len(pipeline.Author) == 0 {
pipeline.Author = hook.GetHeadCommit().GetAuthor().GetLogin()
}
// if len(pipeline.Email) == 0 {
// TODO: default to gravatar?
// }
if strings.HasPrefix(pipeline.Ref, "refs/tags/") {
// just kidding, this is actually a tag event. Why did this come as a push
// event we'll never know!
Expand Down
2 changes: 1 addition & 1 deletion server/forge/mocks/forge.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

47 changes: 47 additions & 0 deletions server/forge/types/meta_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Copyright 2023 Woodpecker Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package types

import (
"testing"

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

func TestSortByName(t *testing.T) {
fm := []*FileMeta{
{
Name: "a",
},
{
Name: "c",
},
{
Name: "b",
},
}

assert.Equal(t, []*FileMeta{
{
Name: "a",
},
{
Name: "b",
},
{
Name: "c",
},
}, SortByName(fm))
}
1 change: 0 additions & 1 deletion server/model/step.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ type StepStore interface {
StepList(*Pipeline) ([]*Step, error)
StepCreate([]*Step) error
StepUpdate(*Step) error
StepClear(*Pipeline) error
}

// Different ways to handle failure states
Expand Down
Loading