2021-11-13 19:18:06 +00:00
|
|
|
// 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 (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
2023-12-08 07:15:08 +00:00
|
|
|
"go.woodpecker-ci.org/woodpecker/v2/server/model"
|
2021-11-13 19:18:06 +00:00
|
|
|
)
|
|
|
|
|
2022-10-31 15:08:57 +00:00
|
|
|
func TestGetPipelineQueue(t *testing.T) {
|
2023-08-21 13:04:12 +00:00
|
|
|
store, closer := newTestStore(t, new(model.Repo), new(model.User), new(model.Perm), new(model.Pipeline), new(model.Org))
|
2022-10-31 15:08:57 +00:00
|
|
|
defer closer()
|
|
|
|
|
|
|
|
user := &model.User{
|
2024-11-22 18:12:43 +00:00
|
|
|
Login: "joe",
|
|
|
|
Email: "foo@bar.com",
|
|
|
|
AccessToken: "e42080dddf012c718e476da161d21ad5",
|
2022-10-31 15:08:57 +00:00
|
|
|
}
|
|
|
|
assert.NoError(t, store.CreateUser(user))
|
|
|
|
|
|
|
|
repo1 := &model.Repo{
|
2022-11-15 14:01:23 +00:00
|
|
|
Owner: "bradrydzewski",
|
|
|
|
Name: "test",
|
|
|
|
FullName: "bradrydzewski/test",
|
|
|
|
ForgeRemoteID: "1",
|
|
|
|
IsActive: true,
|
2022-10-31 15:08:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
assert.NoError(t, store.CreateRepo(repo1))
|
|
|
|
for _, perm := range []*model.Perm{
|
|
|
|
{UserID: user.ID, Repo: repo1, Push: true, Admin: false},
|
|
|
|
} {
|
|
|
|
assert.NoError(t, store.PermUpsert(perm))
|
|
|
|
}
|
|
|
|
pipeline1 := &model.Pipeline{
|
|
|
|
RepoID: repo1.ID,
|
|
|
|
Status: model.StatusPending,
|
|
|
|
}
|
|
|
|
assert.NoError(t, store.CreatePipeline(pipeline1))
|
|
|
|
|
|
|
|
feed, err := store.GetPipelineQueue()
|
2024-01-14 18:33:58 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Len(t, feed, 1)
|
2022-10-31 15:08:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestUserFeed(t *testing.T) {
|
2023-08-21 13:04:12 +00:00
|
|
|
store, closer := newTestStore(t, new(model.Repo), new(model.User), new(model.Perm), new(model.Pipeline), new(model.Org))
|
2022-10-31 15:08:57 +00:00
|
|
|
defer closer()
|
|
|
|
|
|
|
|
user := &model.User{
|
2024-11-22 18:12:43 +00:00
|
|
|
Login: "joe",
|
|
|
|
Email: "foo@bar.com",
|
|
|
|
AccessToken: "e42080dddf012c718e476da161d21ad5",
|
2022-10-31 15:08:57 +00:00
|
|
|
}
|
|
|
|
assert.NoError(t, store.CreateUser(user))
|
|
|
|
|
|
|
|
repo1 := &model.Repo{
|
2022-11-15 14:01:23 +00:00
|
|
|
Owner: "bradrydzewski",
|
|
|
|
Name: "test1",
|
|
|
|
FullName: "bradrydzewski/test1",
|
|
|
|
ForgeRemoteID: "1",
|
|
|
|
IsActive: true,
|
2022-10-31 15:08:57 +00:00
|
|
|
}
|
|
|
|
repo2 := &model.Repo{
|
2022-11-15 14:01:23 +00:00
|
|
|
Owner: "johndoe",
|
|
|
|
Name: "test",
|
|
|
|
FullName: "johndoe/test2",
|
|
|
|
ForgeRemoteID: "2",
|
|
|
|
IsActive: true,
|
2022-10-31 15:08:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
assert.NoError(t, store.CreateRepo(repo1))
|
|
|
|
assert.NoError(t, store.CreateRepo(repo2))
|
|
|
|
|
|
|
|
for _, perm := range []*model.Perm{
|
|
|
|
{UserID: user.ID, Repo: repo1, Push: true, Admin: false},
|
|
|
|
} {
|
|
|
|
assert.NoError(t, store.PermUpsert(perm))
|
|
|
|
}
|
|
|
|
|
|
|
|
pipeline1 := &model.Pipeline{
|
|
|
|
RepoID: repo1.ID,
|
|
|
|
Status: model.StatusFailure,
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.NoError(t, store.CreatePipeline(pipeline1))
|
|
|
|
feed, err := store.UserFeed(user)
|
2024-01-14 18:33:58 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Len(t, feed, 1)
|
2022-10-31 15:08:57 +00:00
|
|
|
}
|
|
|
|
|
2021-11-13 19:18:06 +00:00
|
|
|
func TestRepoListLatest(t *testing.T) {
|
2023-08-21 13:04:12 +00:00
|
|
|
store, closer := newTestStore(t, new(model.Repo), new(model.User), new(model.Perm), new(model.Pipeline), new(model.Org))
|
2021-11-13 19:18:06 +00:00
|
|
|
defer closer()
|
|
|
|
|
|
|
|
user := &model.User{
|
2024-11-22 18:12:43 +00:00
|
|
|
Login: "joe",
|
|
|
|
Email: "foo@bar.com",
|
|
|
|
AccessToken: "e42080dddf012c718e476da161d21ad5",
|
2021-11-13 19:18:06 +00:00
|
|
|
}
|
|
|
|
assert.NoError(t, store.CreateUser(user))
|
|
|
|
|
|
|
|
repo1 := &model.Repo{
|
2023-06-12 23:07:52 +00:00
|
|
|
ID: 1,
|
2022-11-15 14:01:23 +00:00
|
|
|
Owner: "bradrydzewski",
|
|
|
|
Name: "test",
|
|
|
|
FullName: "bradrydzewski/test",
|
|
|
|
ForgeRemoteID: "1",
|
|
|
|
IsActive: true,
|
2021-11-13 19:18:06 +00:00
|
|
|
}
|
|
|
|
repo2 := &model.Repo{
|
2023-06-12 23:07:52 +00:00
|
|
|
ID: 2,
|
2022-11-15 14:01:23 +00:00
|
|
|
Owner: "test",
|
|
|
|
Name: "test",
|
|
|
|
FullName: "test/test",
|
|
|
|
ForgeRemoteID: "2",
|
|
|
|
IsActive: true,
|
2021-11-13 19:18:06 +00:00
|
|
|
}
|
|
|
|
repo3 := &model.Repo{
|
2023-06-12 23:07:52 +00:00
|
|
|
ID: 3,
|
2022-11-15 14:01:23 +00:00
|
|
|
Owner: "octocat",
|
|
|
|
Name: "hello-world",
|
|
|
|
FullName: "octocat/hello-world",
|
|
|
|
ForgeRemoteID: "3",
|
|
|
|
IsActive: true,
|
2021-11-13 19:18:06 +00:00
|
|
|
}
|
|
|
|
assert.NoError(t, store.CreateRepo(repo1))
|
|
|
|
assert.NoError(t, store.CreateRepo(repo2))
|
|
|
|
assert.NoError(t, store.CreateRepo(repo3))
|
|
|
|
|
2021-11-18 14:42:18 +00:00
|
|
|
for _, perm := range []*model.Perm{
|
2022-09-05 15:08:51 +00:00
|
|
|
{UserID: user.ID, Repo: repo1, Push: true, Admin: false},
|
|
|
|
{UserID: user.ID, Repo: repo2, Push: true, Admin: true},
|
2021-11-18 14:42:18 +00:00
|
|
|
} {
|
|
|
|
assert.NoError(t, store.PermUpsert(perm))
|
|
|
|
}
|
2021-11-13 19:18:06 +00:00
|
|
|
|
2022-10-18 01:24:12 +00:00
|
|
|
pipeline1 := &model.Pipeline{
|
2021-11-13 19:18:06 +00:00
|
|
|
RepoID: repo1.ID,
|
|
|
|
Status: model.StatusFailure,
|
|
|
|
}
|
2022-10-18 01:24:12 +00:00
|
|
|
pipeline2 := &model.Pipeline{
|
2021-11-13 19:18:06 +00:00
|
|
|
RepoID: repo1.ID,
|
|
|
|
Status: model.StatusRunning,
|
|
|
|
}
|
2022-10-18 01:24:12 +00:00
|
|
|
pipeline3 := &model.Pipeline{
|
2021-11-13 19:18:06 +00:00
|
|
|
RepoID: repo2.ID,
|
|
|
|
Status: model.StatusKilled,
|
|
|
|
}
|
2022-10-18 01:24:12 +00:00
|
|
|
pipeline4 := &model.Pipeline{
|
2021-11-13 19:18:06 +00:00
|
|
|
RepoID: repo3.ID,
|
|
|
|
Status: model.StatusError,
|
|
|
|
}
|
2022-10-18 01:24:12 +00:00
|
|
|
assert.NoError(t, store.CreatePipeline(pipeline1))
|
|
|
|
assert.NoError(t, store.CreatePipeline(pipeline2))
|
|
|
|
assert.NoError(t, store.CreatePipeline(pipeline3))
|
|
|
|
assert.NoError(t, store.CreatePipeline(pipeline4))
|
2021-11-13 19:18:06 +00:00
|
|
|
|
2022-10-18 01:24:12 +00:00
|
|
|
pipelines, err := store.RepoListLatest(user)
|
2024-01-14 18:33:58 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Len(t, pipelines, 2)
|
|
|
|
assert.EqualValues(t, model.StatusRunning, pipelines[0].Status)
|
|
|
|
assert.Equal(t, repo1.ID, pipelines[0].RepoID)
|
|
|
|
assert.EqualValues(t, model.StatusKilled, pipelines[1].Status)
|
|
|
|
assert.Equal(t, repo2.ID, pipelines[1].RepoID)
|
2021-11-13 19:18:06 +00:00
|
|
|
}
|