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"
|
|
|
|
|
2021-09-27 19:51:55 +02:00
|
|
|
"github.com/woodpecker-ci/woodpecker/server/model"
|
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",
|
|
|
|
Email: "foo@bar.com",
|
|
|
|
Token: "12345",
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Unexpected error: insert registry: %s", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-11-13 21:18:06 +02:00
|
|
|
registry, err := store.RegistryFind(&model.Repo{ID: 1}, "index.docker.io")
|
2017-04-06 14:51:01 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if got, want := registry.RepoID, int64(1); got != want {
|
|
|
|
t.Errorf("Want repo id %d, got %d", want, got)
|
|
|
|
}
|
|
|
|
if got, want := registry.Address, "index.docker.io"; got != want {
|
|
|
|
t.Errorf("Want registry address %s, got %s", want, got)
|
|
|
|
}
|
|
|
|
if got, want := registry.Username, "foo"; got != want {
|
|
|
|
t.Errorf("Want registry username %s, got %s", want, got)
|
|
|
|
}
|
|
|
|
if got, want := registry.Password, "bar"; got != want {
|
|
|
|
t.Errorf("Want registry password %s, got %s", want, got)
|
|
|
|
}
|
|
|
|
if got, want := registry.Email, "foo@bar.com"; got != want {
|
|
|
|
t.Errorf("Want registry email %s, got %s", want, got)
|
|
|
|
}
|
|
|
|
if got, want := registry.Token, "12345"; got != want {
|
|
|
|
t.Errorf("Want registry token %s, got %s", want, got)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
2021-11-13 21:18:06 +02:00
|
|
|
list, err := store.RegistryList(&model.Repo{ID: 1})
|
2017-04-06 14:51:01 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if got, want := len(list), 2; got != want {
|
|
|
|
t.Errorf("Want %d registries, got %d", want, got)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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",
|
|
|
|
}
|
2021-11-13 21:18:06 +02:00
|
|
|
if err := store.RegistryCreate(registry); err != nil {
|
2017-04-06 14:51:01 +02:00
|
|
|
t.Errorf("Unexpected error: insert registry: %s", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
registry.Password = "qux"
|
2021-11-13 21:18:06 +02:00
|
|
|
if err := store.RegistryUpdate(registry); err != nil {
|
2017-04-06 14:51:01 +02:00
|
|
|
t.Errorf("Unexpected error: update registry: %s", err)
|
|
|
|
return
|
|
|
|
}
|
2021-11-13 21:18:06 +02:00
|
|
|
updated, err := store.RegistryFind(&model.Repo{ID: 1}, "index.docker.io")
|
2017-04-06 14:51:01 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if got, want := updated.Password, "qux"; got != want {
|
|
|
|
t.Errorf("Want registry password %s, got %s", want, got)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
2021-11-13 21:18:06 +02:00
|
|
|
if err := store.RegistryCreate(&model.Registry{
|
2017-04-06 14:51:01 +02:00
|
|
|
RepoID: 1,
|
|
|
|
Address: "index.docker.io",
|
|
|
|
Username: "foo",
|
|
|
|
Password: "bar",
|
|
|
|
}); err != nil {
|
|
|
|
t.Errorf("Unexpected error: insert registry: %s", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// fail due to duplicate addr
|
2021-11-13 21:18:06 +02:00
|
|
|
if err := store.RegistryCreate(&model.Registry{
|
2017-04-06 14:51:01 +02:00
|
|
|
RepoID: 1,
|
|
|
|
Address: "index.docker.io",
|
|
|
|
Username: "baz",
|
|
|
|
Password: "qux",
|
|
|
|
}); err == nil {
|
2021-10-08 18:35:56 +02:00
|
|
|
t.Errorf("Unexpected error: duplicate address")
|
2017-04-06 14:51:01 +02:00
|
|
|
}
|
|
|
|
}
|