From 39824f4ce8a742bbd36860398292e74c300d8fb6 Mon Sep 17 00:00:00 2001 From: qwerty287 Date: Wed, 25 Oct 2023 15:18:59 +0200 Subject: [PATCH 1/9] Some datastore tests --- cmd/server/docs/docs.go | 16 ++++----- pipeline/rpc/proto/woodpecker.pb.go | 2 +- pipeline/rpc/proto/woodpecker_grpc.pb.go | 2 +- server/api/hook.go | 12 +++---- server/api/repo.go | 4 +-- server/forge/github/parse.go | 3 -- server/forge/mocks/forge.go | 2 +- server/model/step.go | 1 - server/store/datastore/server_config_test.go | 23 ++++++++++++ server/store/datastore/step.go | 18 ---------- server/store/datastore/step_test.go | 37 +++++++++++++++++++- server/store/mocks/store.go | 16 +-------- server/store/store.go | 1 - 13 files changed, 79 insertions(+), 58 deletions(-) diff --git a/cmd/server/docs/docs.go b/cmd/server/docs/docs.go index 37e98692c1..0a9c40deed 100644 --- a/cmd/server/docs/docs.go +++ b/cmd/server/docs/docs.go @@ -1252,8 +1252,8 @@ const docTemplate = `{ } ], "responses": { - "200": { - "description": "OK" + "204": { + "description": "No Content" } } } @@ -1278,8 +1278,8 @@ const docTemplate = `{ } ], "responses": { - "200": { - "description": "OK" + "204": { + "description": "No Content" } } } @@ -1304,8 +1304,8 @@ const docTemplate = `{ } ], "responses": { - "200": { - "description": "OK" + "204": { + "description": "No Content" } } } @@ -2055,8 +2055,8 @@ const docTemplate = `{ } ], "responses": { - "200": { - "description": "OK" + "204": { + "description": "No Content" } } } diff --git a/pipeline/rpc/proto/woodpecker.pb.go b/pipeline/rpc/proto/woodpecker.pb.go index 0fead4b8e0..332fb8e8d4 100644 --- a/pipeline/rpc/proto/woodpecker.pb.go +++ b/pipeline/rpc/proto/woodpecker.pb.go @@ -16,7 +16,7 @@ // Code generated by protoc-gen-go. DO NOT EDIT. // versions: // protoc-gen-go v1.31.0 -// protoc v4.23.4 +// protoc v4.24.3 // source: woodpecker.proto package proto diff --git a/pipeline/rpc/proto/woodpecker_grpc.pb.go b/pipeline/rpc/proto/woodpecker_grpc.pb.go index b6f0f6ae14..b3c5a20053 100644 --- a/pipeline/rpc/proto/woodpecker_grpc.pb.go +++ b/pipeline/rpc/proto/woodpecker_grpc.pb.go @@ -16,7 +16,7 @@ // Code generated by protoc-gen-go-grpc. DO NOT EDIT. // versions: // - protoc-gen-go-grpc v1.3.0 -// - protoc v4.23.4 +// - protoc v4.24.3 // source: woodpecker.proto package proto diff --git a/server/api/hook.go b/server/api/hook.go index b2fab9725f..534d515f0a 100644 --- a/server/api/hook.go +++ b/server/api/hook.go @@ -54,12 +54,12 @@ 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 ) func PauseQueue(c *gin.Context) { server.Config.Services.Queue.Pause() - c.Status(http.StatusOK) + c.Status(http.StatusNoContent) } // ResumeQueue @@ -67,12 +67,12 @@ func PauseQueue(c *gin.Context) { // @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 ) func ResumeQueue(c *gin.Context) { server.Config.Services.Queue.Resume() - c.Status(http.StatusOK) + c.Status(http.StatusNoContent) } // BlockTilQueueHasRunningItem @@ -80,7 +80,7 @@ func ResumeQueue(c *gin.Context) { // @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 ) func BlockTilQueueHasRunningItem(c *gin.Context) { @@ -90,7 +90,7 @@ func BlockTilQueueHasRunningItem(c *gin.Context) { break } } - c.Status(http.StatusOK) + c.Status(http.StatusNoContent) } // PostHook diff --git a/server/api/repo.go b/server/api/repo.go index 6586e19179..7c262eef1d 100644 --- a/server/api/repo.go +++ b/server/api/repo.go @@ -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 ) // @Param repo_id path int true "the repository id" @@ -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 diff --git a/server/forge/github/parse.go b/server/forge/github/parse.go index 4311a71885..f49af8c819 100644 --- a/server/forge/github/parse.go +++ b/server/forge/github/parse.go @@ -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! diff --git a/server/forge/mocks/forge.go b/server/forge/mocks/forge.go index 2d9f544f3b..8be5e48e1e 100644 --- a/server/forge/mocks/forge.go +++ b/server/forge/mocks/forge.go @@ -1,4 +1,4 @@ -// Code generated by mockery v2.33.1. DO NOT EDIT. +// Code generated by mockery v2.36.0. DO NOT EDIT. package mocks diff --git a/server/model/step.go b/server/model/step.go index edf40e6114..5341a7cd69 100644 --- a/server/model/step.go +++ b/server/model/step.go @@ -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 diff --git a/server/store/datastore/server_config_test.go b/server/store/datastore/server_config_test.go index 04d6e2f487..cad3f7b5e2 100644 --- a/server/store/datastore/server_config_test.go +++ b/server/store/datastore/server_config_test.go @@ -43,4 +43,27 @@ func TestServerConfigGetSet(t *testing.T) { t.Errorf("Want server-config value %s, got %s", serverConfig.Value, value) return } + + serverConfig.Value = "new-wonderland" + if err := store.ServerConfigSet(serverConfig.Key, serverConfig.Value); err != nil { + t.Errorf("Unexpected error: insert secret: %s", err) + return + } + + value, err = store.ServerConfigGet(serverConfig.Key) + if err != nil { + t.Errorf("Unexpected error: delete secret: %s", err) + return + } + + if value != serverConfig.Value { + t.Errorf("Want server-config value %s, got %s", serverConfig.Value, value) + return + } + + value, err = store.ServerConfigGet("config_not_exist") + if err == nil { + t.Errorf("Unexpected: no error on missing config: %v", err) + return + } } diff --git a/server/store/datastore/step.go b/server/store/datastore/step.go index 6ef3e0bfb9..902939c5ed 100644 --- a/server/store/datastore/step.go +++ b/server/store/datastore/step.go @@ -83,24 +83,6 @@ func (s storage) StepUpdate(step *model.Step) error { return err } -func (s storage) StepClear(pipeline *model.Pipeline) error { - sess := s.engine.NewSession() - defer sess.Close() - if err := sess.Begin(); err != nil { - return err - } - - if _, err := sess.Where("step_pipeline_id = ?", pipeline.ID).Delete(new(model.Step)); err != nil { - return err - } - - if _, err := sess.Where("workflow_pipeline_id = ?", pipeline.ID).Delete(new(model.Workflow)); err != nil { - return err - } - - return sess.Commit() -} - func deleteStep(sess *xorm.Session, stepID int64) error { if _, err := sess.Where("step_id = ?", stepID).Delete(new(model.LogEntry)); err != nil { return err diff --git a/server/store/datastore/step_test.go b/server/store/datastore/step_test.go index 3fd3859692..53e22a4593 100644 --- a/server/store/datastore/step_test.go +++ b/server/store/datastore/step_test.go @@ -246,4 +246,39 @@ func TestStepByUUID(t *testing.T) { assert.Empty(t, step) } -// TODO: func TestStepCascade(t *testing.T) {} +func TestStepLoad(t *testing.T) { + store, closer := newTestStore(t, new(model.Step)) + defer closer() + + sess := store.engine.NewSession() + assert.NoError(t, store.stepCreate(sess, []*model.Step{ + { + UUID: "4db7e5fc-5312-4d02-9e14-b51b9e3242cc", + PipelineID: 1, + PID: 1, + PPID: 1, + State: "running", + Name: "build", + }, + { + UUID: "fc7c7fd6-553e-480b-8ed7-30d8563d0b79", + PipelineID: 4, + PID: 6, + PPID: 7, + Name: "build", + State: "pending", + Error: "pc load letter", + ExitCode: 255, + }, + })) + _ = sess.Close() + + step, err := store.StepLoad(1) + assert.NoError(t, err) + assert.NotEmpty(t, step) + assert.Equal(t, step.UUID, "4db7e5fc-5312-4d02-9e14-b51b9e3242cc") + + step, err = store.StepLoad(5) + assert.ErrorIs(t, err, types.RecordNotExist) + assert.Empty(t, step) +} diff --git a/server/store/mocks/store.go b/server/store/mocks/store.go index 5d3c58ffc7..88d88ba24e 100644 --- a/server/store/mocks/store.go +++ b/server/store/mocks/store.go @@ -1,4 +1,4 @@ -// Code generated by mockery v2.33.1. DO NOT EDIT. +// Code generated by mockery v2.36.0. DO NOT EDIT. package mocks @@ -1859,20 +1859,6 @@ func (_m *Store) StepChild(_a0 *model.Pipeline, _a1 int, _a2 string) (*model.Ste return r0, r1 } -// StepClear provides a mock function with given fields: _a0 -func (_m *Store) StepClear(_a0 *model.Pipeline) error { - ret := _m.Called(_a0) - - var r0 error - if rf, ok := ret.Get(0).(func(*model.Pipeline) error); ok { - r0 = rf(_a0) - } else { - r0 = ret.Error(0) - } - - return r0 -} - // StepFind provides a mock function with given fields: _a0, _a1 func (_m *Store) StepFind(_a0 *model.Pipeline, _a1 int) (*model.Step, error) { ret := _m.Called(_a0, _a1) diff --git a/server/store/store.go b/server/store/store.go index 42aa951293..113d50db14 100644 --- a/server/store/store.go +++ b/server/store/store.go @@ -141,7 +141,6 @@ type Store interface { StepChild(*model.Pipeline, int, string) (*model.Step, error) StepList(*model.Pipeline) ([]*model.Step, error) StepUpdate(*model.Step) error - StepClear(*model.Pipeline) error StepListFromWorkflowFind(*model.Workflow) ([]*model.Step, error) // Logs From 1f9d6e05734133fc0746d277220ff8839d892728 Mon Sep 17 00:00:00 2001 From: qwerty287 Date: Wed, 25 Oct 2023 15:51:44 +0200 Subject: [PATCH 2/9] More tests --- pipeline/backend/local/const_test.go | 33 +++++++++++++++ server/badges/badges_test.go | 33 +++++++++++++++ server/forge/types/meta_test.go | 47 +++++++++++++++++++++ server/model/step_test.go | 46 ++++++++++++++++++++ server/store/datastore/agent_test.go | 61 +++++++++++++++++++++------ server/store/datastore/helper_test.go | 39 +++++++++++++++++ 6 files changed, 246 insertions(+), 13 deletions(-) create mode 100644 pipeline/backend/local/const_test.go create mode 100644 server/badges/badges_test.go create mode 100644 server/forge/types/meta_test.go create mode 100644 server/model/step_test.go create mode 100644 server/store/datastore/helper_test.go diff --git a/pipeline/backend/local/const_test.go b/pipeline/backend/local/const_test.go new file mode 100644 index 0000000000..9ca9af4bdf --- /dev/null +++ b/pipeline/backend/local/const_test.go @@ -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", + })) +} diff --git a/server/badges/badges_test.go b/server/badges/badges_test.go new file mode 100644 index 0000000000..fc7f1a1c62 --- /dev/null +++ b/server/badges/badges_test.go @@ -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})) +} diff --git a/server/forge/types/meta_test.go b/server/forge/types/meta_test.go new file mode 100644 index 0000000000..4aa11f631d --- /dev/null +++ b/server/forge/types/meta_test.go @@ -0,0 +1,47 @@ +// 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 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)) +} diff --git a/server/model/step_test.go b/server/model/step_test.go new file mode 100644 index 0000000000..0f8df86502 --- /dev/null +++ b/server/model/step_test.go @@ -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) +} diff --git a/server/store/datastore/agent_test.go b/server/store/datastore/agent_test.go index 6ba037f2c0..24c7ad7573 100644 --- a/server/store/datastore/agent_test.go +++ b/server/store/datastore/agent_test.go @@ -17,6 +17,7 @@ package datastore import ( "testing" + "github.com/stretchr/testify/assert" "github.com/woodpecker-ci/woodpecker/server/model" ) @@ -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) _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)) } diff --git a/server/store/datastore/helper_test.go b/server/store/datastore/helper_test.go new file mode 100644 index 0000000000..8d5235ad92 --- /dev/null +++ b/server/store/datastore/helper_test.go @@ -0,0 +1,39 @@ +// Copyright 2021 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 datastore + +import ( + "errors" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/woodpecker-ci/woodpecker/server/store/types" +) + +func TestWrapGet(t *testing.T) { + err := wrapGet(false, nil) + assert.ErrorIs(t, err, types.RecordNotExist) + + err = wrapGet(true, errors.New("test err")) + assert.Equal(t, "TestWrapGet: test err", err.Error()) +} + +func TestWrapDelete(t *testing.T) { + err := wrapDelete(0, nil) + assert.ErrorIs(t, err, types.RecordNotExist) + + err = wrapDelete(1, errors.New("test err")) + assert.Equal(t, "TestWrapDelete: test err", err.Error()) +} From 86c4e8dd5bd4f3ad09d1376882d2bb1670925740 Mon Sep 17 00:00:00 2001 From: qwerty287 Date: Thu, 26 Oct 2023 08:58:18 +0200 Subject: [PATCH 3/9] fix copyright years --- server/forge/types/meta_test.go | 2 +- server/store/datastore/helper_test.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/server/forge/types/meta_test.go b/server/forge/types/meta_test.go index 4aa11f631d..071607ea30 100644 --- a/server/forge/types/meta_test.go +++ b/server/forge/types/meta_test.go @@ -1,4 +1,4 @@ -// Copyright 2022 Woodpecker Authors +// 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. diff --git a/server/store/datastore/helper_test.go b/server/store/datastore/helper_test.go index 8d5235ad92..ba47fb5ea8 100644 --- a/server/store/datastore/helper_test.go +++ b/server/store/datastore/helper_test.go @@ -1,4 +1,4 @@ -// Copyright 2021 Woodpecker Authors +// 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. From c0e792b0ac3b7c11e05557e2cf614cc39431d1d3 Mon Sep 17 00:00:00 2001 From: qwerty287 Date: Thu, 26 Oct 2023 09:11:33 +0200 Subject: [PATCH 4/9] fix lint --- server/store/datastore/server_config_test.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/server/store/datastore/server_config_test.go b/server/store/datastore/server_config_test.go index cad3f7b5e2..a6ff025ed7 100644 --- a/server/store/datastore/server_config_test.go +++ b/server/store/datastore/server_config_test.go @@ -66,4 +66,8 @@ func TestServerConfigGetSet(t *testing.T) { t.Errorf("Unexpected: no error on missing config: %v", err) return } + if value != "" { + t.Errorf("Unexpected: got value on missing config: %s", value) + return + } } From 40027d7d7cd5d7e6754fe9017d7cf3f9a709a439 Mon Sep 17 00:00:00 2001 From: qwerty287 Date: Thu, 26 Oct 2023 10:08:23 +0200 Subject: [PATCH 5/9] try to fix --- server/store/datastore/server_config.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server/store/datastore/server_config.go b/server/store/datastore/server_config.go index c6afed79c0..7fb063e361 100644 --- a/server/store/datastore/server_config.go +++ b/server/store/datastore/server_config.go @@ -43,7 +43,7 @@ func (s storage) ServerConfigSet(key, value string) error { return err } - _, err = s.engine.Where("key = ?", config.Key).AllCols().Update(config) + _, err = s.engine.Where("'key' = ?", config.Key).AllCols().Update(config) return err } From b8a4e25708a51c994cf0b0bab6954fd049671da4 Mon Sep 17 00:00:00 2001 From: qwerty287 Date: Thu, 26 Oct 2023 10:14:37 +0200 Subject: [PATCH 6/9] try fixing no.2 --- server/store/datastore/server_config.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server/store/datastore/server_config.go b/server/store/datastore/server_config.go index 7fb063e361..8399a97ef9 100644 --- a/server/store/datastore/server_config.go +++ b/server/store/datastore/server_config.go @@ -43,7 +43,7 @@ func (s storage) ServerConfigSet(key, value string) error { return err } - _, err = s.engine.Where("'key' = ?", config.Key).AllCols().Update(config) + _, err = s.engine.Where("key = ?", config.Key).Cols("value").Update(config) return err } From f22c75040f2bd0ddb776bd0ba67bff514c06c8c0 Mon Sep 17 00:00:00 2001 From: qwerty287 Date: Thu, 26 Oct 2023 17:40:03 +0200 Subject: [PATCH 7/9] use NoError() --- pipeline/backend/kubernetes/service_test.go | 2 +- pipeline/backend/kubernetes/volume_test.go | 10 +++++----- .../frontend/yaml/types/base/base_types_test.go | 8 ++++---- pipeline/frontend/yaml/types/network_test.go | 2 +- pipeline/frontend/yaml/types/volume_test.go | 4 ++-- server/plugins/encryption/aes_test.go | 12 ++++++------ server/store/datastore/agent_test.go | 15 ++++++++------- 7 files changed, 27 insertions(+), 26 deletions(-) diff --git a/pipeline/backend/kubernetes/service_test.go b/pipeline/backend/kubernetes/service_test.go index 6702b36d5d..2d0441d3b2 100644 --- a/pipeline/backend/kubernetes/service_test.go +++ b/pipeline/backend/kubernetes/service_test.go @@ -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)) } diff --git a/pipeline/backend/kubernetes/volume_test.go b/pipeline/backend/kubernetes/volume_test.go index d1132c46f6..fb38a6825e 100644 --- a/pipeline/backend/kubernetes/volume_test.go +++ b/pipeline/backend/kubernetes/volume_test.go @@ -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) } diff --git a/pipeline/frontend/yaml/types/base/base_types_test.go b/pipeline/frontend/yaml/types/base/base_types_test.go index 244b82667c..98accdfabf 100644 --- a/pipeline/frontend/yaml/types/base/base_types_test.go +++ b/pipeline/frontend/yaml/types/base/base_types_test.go @@ -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)) @@ -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)) @@ -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)) @@ -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)) diff --git a/pipeline/frontend/yaml/types/network_test.go b/pipeline/frontend/yaml/types/network_test.go index 96d720f6d4..bdb09bf9e9 100644 --- a/pipeline/frontend/yaml/types/network_test.go +++ b/pipeline/frontend/yaml/types/network_test.go @@ -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") } } diff --git a/pipeline/frontend/yaml/types/volume_test.go b/pipeline/frontend/yaml/types/volume_test.go index ad536b2daa..07270b1a42 100644 --- a/pipeline/frontend/yaml/types/volume_test.go +++ b/pipeline/frontend/yaml/types/volume_test.go @@ -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") } } @@ -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") } } diff --git a/server/plugins/encryption/aes_test.go b/server/plugins/encryption/aes_test.go index 658f178c69..1a2bc5abb1 100644 --- a/server/plugins/encryption/aes_test.go +++ b/server/plugins/encryption/aes_test.go @@ -24,27 +24,27 @@ import ( func TestShortMessageLongKey(t *testing.T) { aes := &aesEncryptionService{} err := aes.loadCipher(string(random.GetRandomBytes(32))) - assert.Nil(t, err) + assert.NoError(t, err) input := string(random.GetRandomBytes(4)) cipher, err := aes.Encrypt(input, "") - assert.Nil(t, err) + assert.NoError(t, err) output, err := aes.Decrypt(cipher, "") - assert.Nil(t, err) + assert.NoError(t, err) assert.Equal(t, input, output) } func TestLongMessageShortKey(t *testing.T) { aes := &aesEncryptionService{} err := aes.loadCipher(string(random.GetRandomBytes(12))) - assert.Nil(t, err) + assert.NoError(t, err) input := string(random.GetRandomBytes(1024)) cipher, err := aes.Encrypt(input, "") - assert.Nil(t, err) + assert.NoError(t, err) output, err := aes.Decrypt(cipher, "") - assert.Nil(t, err) + assert.NoError(t, err) assert.Equal(t, input, output) } diff --git a/server/store/datastore/agent_test.go b/server/store/datastore/agent_test.go index 24c7ad7573..47bb0cc8ea 100644 --- a/server/store/datastore/agent_test.go +++ b/server/store/datastore/agent_test.go @@ -18,6 +18,7 @@ import ( "testing" "github.com/stretchr/testify/assert" + "github.com/woodpecker-ci/woodpecker/server/model" ) @@ -31,7 +32,7 @@ func TestAgentFindByToken(t *testing.T) { Token: "secret-token", } err := store.AgentCreate(agent) - assert.Nil(t, err) + assert.NoError(t, err) _agent, err := store.AgentFindByToken(agent.Token) if err != nil { @@ -55,10 +56,10 @@ func TestAgentFindByID(t *testing.T) { Token: "secret-token", } err := store.AgentCreate(agent) - assert.Nil(t, err) + assert.NoError(t, err) _agent, err := store.AgentFind(agent.ID) - assert.Nil(t, err) + assert.NoError(t, err) assert.Equal(t, "secret-token", _agent.Token) } @@ -77,15 +78,15 @@ func TestAgentList(t *testing.T) { Token: "secret-token-2", } err := store.AgentCreate(agent1) - assert.Nil(t, err) + assert.NoError(t, err) err = store.AgentCreate(agent2) - assert.Nil(t, err) + assert.NoError(t, err) agents, err := store.AgentList(&model.ListOptions{All: true}) - assert.Nil(t, err) + assert.NoError(t, err) assert.Equal(t, 2, len(agents)) agents, err = store.AgentList(&model.ListOptions{Page: 1, PerPage: 1}) - assert.Nil(t, err) + assert.NoError(t, err) assert.Equal(t, 1, len(agents)) } From c7db229474b788e380de7206d7992abf30ef7989 Mon Sep 17 00:00:00 2001 From: qwerty287 Date: Fri, 27 Oct 2023 08:44:46 +0200 Subject: [PATCH 8/9] try with beans --- server/store/datastore/server_config.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/server/store/datastore/server_config.go b/server/store/datastore/server_config.go index 8399a97ef9..ac92debaa0 100644 --- a/server/store/datastore/server_config.go +++ b/server/store/datastore/server_config.go @@ -43,7 +43,9 @@ func (s storage) ServerConfigSet(key, value string) error { return err } - _, err = s.engine.Where("key = ?", config.Key).Cols("value").Update(config) + _, err = s.engine.Cols("value").Update(config, &model.ServerConfig{ + Key: key, + }) return err } From 2b6e585d9a3a8b480d7c50c7f003f10db48b06db Mon Sep 17 00:00:00 2001 From: qwerty287 <80460567+qwerty287@users.noreply.github.com> Date: Sat, 28 Oct 2023 08:21:45 +0200 Subject: [PATCH 9/9] Update server_config.go --- server/store/datastore/server_config.go | 1 + 1 file changed, 1 insertion(+) diff --git a/server/store/datastore/server_config.go b/server/store/datastore/server_config.go index ac92debaa0..6e1eee1d67 100644 --- a/server/store/datastore/server_config.go +++ b/server/store/datastore/server_config.go @@ -43,6 +43,7 @@ func (s storage) ServerConfigSet(key, value string) error { return err } + // TODO change to Where() when https://gitea.com/xorm/xorm/issues/2358 is solved _, err = s.engine.Cols("value").Update(config, &model.ServerConfig{ Key: key, })