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
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",
}))
}
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
46 changes: 46 additions & 0 deletions server/model/step_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// 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 model

import (
"testing"

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

func TestStepStatus(t *testing.T) {
step := &Step{
State: StatusPending,
}

assert.Equal(t, step.Running(), true)
step.State = StatusRunning
assert.Equal(t, step.Running(), true)

step.Failure = FailureIgnore
step.State = StatusError
assert.Equal(t, step.Failing(), false)
step.State = StatusFailure
assert.Equal(t, step.Failing(), false)
step.Failure = FailureFail
step.State = StatusError
assert.Equal(t, step.Failing(), true)
step.State = StatusFailure
assert.Equal(t, step.Failing(), true)
step.State = StatusPending
assert.Equal(t, step.Failing(), false)
step.State = StatusSuccess
assert.Equal(t, step.Failing(), false)
}
61 changes: 48 additions & 13 deletions server/store/datastore/agent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package datastore
import (
"testing"

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

Expand All @@ -29,28 +30,62 @@ func TestAgentFindByToken(t *testing.T) {
Name: "test",
Token: "secret-token",
}
if err := store.AgentCreate(agent); err != nil {
t.Errorf("Unexpected error: insert agent: %s", err)
return
}
err := store.AgentCreate(agent)
assert.Nil(t, err)
qwerty287 marked this conversation as resolved.
Show resolved Hide resolved

_agent, err := store.AgentFindByToken(agent.Token)
if err != nil {
t.Error(err)
return
}
if got, want := _agent.ID, int64(1); got != want {
t.Errorf("Want config id %d, got %d", want, got)
}
assert.Equal(t, int64(1), _agent.ID)

_agent, err = store.AgentFindByToken("")
if err == nil || err.Error() != "Please provide a token" {
t.Errorf("Expected to get an error for an empty token, but got %s", err)
return
assert.ErrorIs(t, err, ErrNoTokenProvided)
assert.Nil(t, _agent)
}

func TestAgentFindByID(t *testing.T) {
store, closer := newTestStore(t, new(model.Agent))
defer closer()

agent := &model.Agent{
ID: int64(1),
Name: "test",
Token: "secret-token",
}
err := store.AgentCreate(agent)
assert.Nil(t, err)

if _agent != nil {
t.Errorf("Expected to not find an agent")
return
_agent, err := store.AgentFind(agent.ID)
assert.Nil(t, err)
assert.Equal(t, "secret-token", _agent.Token)
}

func TestAgentList(t *testing.T) {
store, closer := newTestStore(t, new(model.Agent))
defer closer()

agent1 := &model.Agent{
ID: int64(1),
Name: "test-1",
Token: "secret-token-1",
}
agent2 := &model.Agent{
ID: int64(2),
Name: "test-2",
Token: "secret-token-2",
}
err := store.AgentCreate(agent1)
assert.Nil(t, err)
err = store.AgentCreate(agent2)
assert.Nil(t, err)

agents, err := store.AgentList(&model.ListOptions{All: true})
assert.Nil(t, err)
assert.Equal(t, 2, len(agents))

agents, err = store.AgentList(&model.ListOptions{Page: 1, PerPage: 1})
assert.Nil(t, err)
assert.Equal(t, 1, len(agents))
}
Loading