2018-02-20 00:24:10 +02:00
|
|
|
// Copyright 2018 Drone.IO Inc.
|
2018-03-21 15:02:17 +02:00
|
|
|
//
|
2018-02-20 00:24:10 +02:00
|
|
|
// 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
|
2018-03-21 15:02:17 +02:00
|
|
|
//
|
2018-02-20 00:24:10 +02:00
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
2018-03-21 15:02:17 +02:00
|
|
|
//
|
2018-02-20 00:24:10 +02:00
|
|
|
// 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.
|
|
|
|
|
2017-04-06 14:51:01 +02:00
|
|
|
package datastore
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2021-11-13 21:18:06 +02:00
|
|
|
"github.com/stretchr/testify/assert"
|
2024-07-03 15:33:11 +02:00
|
|
|
"github.com/stretchr/testify/require"
|
2021-11-13 21:18:06 +02:00
|
|
|
|
2024-12-22 11:44:34 +02:00
|
|
|
"go.woodpecker-ci.org/woodpecker/v3/server/model"
|
|
|
|
"go.woodpecker-ci.org/woodpecker/v3/server/store/types"
|
2017-04-06 14:51:01 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestRegistryFind(t *testing.T) {
|
2021-11-13 21:18:06 +02:00
|
|
|
store, closer := newTestStore(t, new(model.Registry))
|
|
|
|
defer closer()
|
2017-04-06 14:51:01 +02:00
|
|
|
|
2021-11-13 21:18:06 +02:00
|
|
|
err := store.RegistryCreate(&model.Registry{
|
2017-04-06 14:51:01 +02:00
|
|
|
RepoID: 1,
|
|
|
|
Address: "index.docker.io",
|
|
|
|
Username: "foo",
|
|
|
|
Password: "bar",
|
|
|
|
})
|
2024-01-14 20:33:58 +02:00
|
|
|
assert.NoError(t, err)
|
2017-04-06 14:51:01 +02:00
|
|
|
|
2021-11-13 21:18:06 +02:00
|
|
|
registry, err := store.RegistryFind(&model.Repo{ID: 1}, "index.docker.io")
|
2024-01-14 20:33:58 +02:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.EqualValues(t, 1, registry.RepoID)
|
|
|
|
assert.Equal(t, "index.docker.io", registry.Address)
|
|
|
|
assert.Equal(t, "foo", registry.Username)
|
|
|
|
assert.Equal(t, "bar", registry.Password)
|
2017-04-06 14:51:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestRegistryList(t *testing.T) {
|
2021-11-13 21:18:06 +02:00
|
|
|
store, closer := newTestStore(t, new(model.Registry))
|
|
|
|
defer closer()
|
2017-04-06 14:51:01 +02:00
|
|
|
|
2021-11-13 21:18:06 +02:00
|
|
|
assert.NoError(t, store.RegistryCreate(&model.Registry{
|
2017-04-06 14:51:01 +02:00
|
|
|
RepoID: 1,
|
|
|
|
Address: "index.docker.io",
|
|
|
|
Username: "foo",
|
|
|
|
Password: "bar",
|
2021-11-13 21:18:06 +02:00
|
|
|
}))
|
|
|
|
assert.NoError(t, store.RegistryCreate(&model.Registry{
|
2017-04-06 14:51:01 +02:00
|
|
|
RepoID: 1,
|
|
|
|
Address: "foo.docker.io",
|
|
|
|
Username: "foo",
|
|
|
|
Password: "bar",
|
2021-11-13 21:18:06 +02:00
|
|
|
}))
|
2017-04-06 14:51:01 +02:00
|
|
|
|
2024-07-03 15:33:11 +02:00
|
|
|
list, err := store.RegistryList(&model.Repo{ID: 1}, false, &model.ListOptions{Page: 1, PerPage: 50})
|
2024-01-14 20:33:58 +02:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Len(t, list, 2)
|
2017-04-06 14:51:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestRegistryUpdate(t *testing.T) {
|
2021-11-13 21:18:06 +02:00
|
|
|
store, closer := newTestStore(t, new(model.Registry))
|
|
|
|
defer closer()
|
2017-04-06 14:51:01 +02:00
|
|
|
|
|
|
|
registry := &model.Registry{
|
|
|
|
RepoID: 1,
|
|
|
|
Address: "index.docker.io",
|
|
|
|
Username: "foo",
|
|
|
|
Password: "bar",
|
|
|
|
}
|
2024-01-14 20:33:58 +02:00
|
|
|
assert.NoError(t, store.RegistryCreate(registry))
|
2017-04-06 14:51:01 +02:00
|
|
|
registry.Password = "qux"
|
2024-01-14 20:33:58 +02:00
|
|
|
assert.NoError(t, store.RegistryUpdate(registry))
|
2021-11-13 21:18:06 +02:00
|
|
|
updated, err := store.RegistryFind(&model.Repo{ID: 1}, "index.docker.io")
|
2024-01-14 20:33:58 +02:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, "qux", updated.Password)
|
2017-04-06 14:51:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestRegistryIndexes(t *testing.T) {
|
2021-11-13 21:18:06 +02:00
|
|
|
store, closer := newTestStore(t, new(model.Registry))
|
|
|
|
defer closer()
|
2017-04-06 14:51:01 +02:00
|
|
|
|
2024-01-14 20:33:58 +02:00
|
|
|
assert.NoError(t, store.RegistryCreate(&model.Registry{
|
2017-04-06 14:51:01 +02:00
|
|
|
RepoID: 1,
|
|
|
|
Address: "index.docker.io",
|
|
|
|
Username: "foo",
|
|
|
|
Password: "bar",
|
2024-01-14 20:33:58 +02:00
|
|
|
}))
|
2017-04-06 14:51:01 +02:00
|
|
|
|
|
|
|
// fail due to duplicate addr
|
2024-01-14 20:33:58 +02:00
|
|
|
assert.Error(t, store.RegistryCreate(&model.Registry{
|
2017-04-06 14:51:01 +02:00
|
|
|
RepoID: 1,
|
|
|
|
Address: "index.docker.io",
|
|
|
|
Username: "baz",
|
|
|
|
Password: "qux",
|
2024-01-14 20:33:58 +02:00
|
|
|
}))
|
2017-04-06 14:51:01 +02:00
|
|
|
}
|
2022-10-18 01:48:04 +02:00
|
|
|
|
|
|
|
func TestRegistryDelete(t *testing.T) {
|
|
|
|
store, closer := newTestStore(t, new(model.Registry), new(model.Repo))
|
|
|
|
defer closer()
|
|
|
|
|
|
|
|
reg1 := &model.Registry{
|
|
|
|
RepoID: 1,
|
|
|
|
Address: "index.docker.io",
|
|
|
|
Username: "foo",
|
|
|
|
Password: "bar",
|
|
|
|
}
|
|
|
|
if !assert.NoError(t, store.RegistryCreate(reg1)) {
|
2024-01-14 20:33:58 +02:00
|
|
|
return
|
2022-10-18 01:48:04 +02:00
|
|
|
}
|
|
|
|
|
2024-07-03 15:33:11 +02:00
|
|
|
assert.NoError(t, store.RegistryDelete(reg1))
|
|
|
|
assert.ErrorIs(t, store.RegistryDelete(reg1), types.RecordNotExist)
|
|
|
|
}
|
|
|
|
|
|
|
|
func createTestRegistries(t *testing.T, store *storage) {
|
|
|
|
assert.NoError(t, store.RegistryCreate(&model.Registry{
|
|
|
|
OrgID: 12,
|
|
|
|
Address: "my.regsitry.local",
|
|
|
|
}))
|
|
|
|
assert.NoError(t, store.RegistryCreate(&model.Registry{
|
|
|
|
RepoID: 1,
|
|
|
|
Address: "private.registry.local",
|
|
|
|
}))
|
|
|
|
assert.NoError(t, store.RegistryCreate(&model.Registry{
|
|
|
|
RepoID: 1,
|
|
|
|
Address: "very-private.registry.local",
|
|
|
|
}))
|
|
|
|
assert.NoError(t, store.RegistryCreate(&model.Registry{
|
|
|
|
Address: "index.docker.io",
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestOrgRegistryFind(t *testing.T) {
|
|
|
|
store, closer := newTestStore(t, new(model.Registry))
|
|
|
|
defer closer()
|
|
|
|
|
|
|
|
err := store.RegistryCreate(&model.Registry{
|
|
|
|
OrgID: 12,
|
|
|
|
Address: "my.regsitry.local",
|
|
|
|
Username: "username",
|
|
|
|
Password: "password",
|
|
|
|
})
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
registry, err := store.OrgRegistryFind(12, "my.regsitry.local")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.EqualValues(t, 12, registry.OrgID)
|
|
|
|
assert.Equal(t, "my.regsitry.local", registry.Address)
|
|
|
|
assert.Equal(t, "username", registry.Username)
|
|
|
|
assert.Equal(t, "password", registry.Password)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestOrgRegistryList(t *testing.T) {
|
|
|
|
store, closer := newTestStore(t, new(model.Registry))
|
|
|
|
defer closer()
|
|
|
|
|
|
|
|
createTestRegistries(t, store)
|
|
|
|
|
|
|
|
list, err := store.OrgRegistryList(12, &model.ListOptions{All: true})
|
|
|
|
assert.NoError(t, err)
|
|
|
|
require.Len(t, list, 1)
|
|
|
|
|
|
|
|
assert.True(t, list[0].IsOrganization())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGlobalRegistryFind(t *testing.T) {
|
|
|
|
store, closer := newTestStore(t, new(model.Registry))
|
|
|
|
defer closer()
|
|
|
|
|
|
|
|
err := store.RegistryCreate(&model.Registry{
|
|
|
|
Address: "my.regsitry.local",
|
|
|
|
Username: "username",
|
|
|
|
Password: "password",
|
|
|
|
})
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
registry, err := store.GlobalRegistryFind("my.regsitry.local")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, "my.regsitry.local", registry.Address)
|
|
|
|
assert.Equal(t, "username", registry.Username)
|
|
|
|
assert.Equal(t, "password", registry.Password)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGlobalRegistryList(t *testing.T) {
|
|
|
|
store, closer := newTestStore(t, new(model.Registry))
|
|
|
|
defer closer()
|
|
|
|
|
|
|
|
createTestRegistries(t, store)
|
|
|
|
|
|
|
|
list, err := store.GlobalRegistryList(&model.ListOptions{All: true})
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Len(t, list, 1)
|
|
|
|
|
|
|
|
assert.True(t, list[0].IsGlobal())
|
2022-10-18 01:48:04 +02:00
|
|
|
}
|