woodpecker-docker-buildx/plugin/impl_test.go
6543 fca44ab7ae Fix func to determine default login (#29)
only add default login if auth profided or no login entry exist

Reviewed-on: https://codeberg.org/woodpecker-plugins/plugin-docker-buildx/pulls/29
Reviewed-by: qwerty287 <qwerty287@noreply.codeberg.org>
2022-10-11 11:38:36 +02:00

78 lines
2.1 KiB
Go

package plugin
import (
"testing"
"codeberg.org/6543/go-yaml2json"
"github.com/stretchr/testify/assert"
"github.com/urfave/cli/v2"
)
var defaultSettings = Settings{
Daemon: Daemon{
StoragePath: "/var/lib/docker",
},
Build: Build{
Context: ".",
Tags: *cli.NewStringSlice("latest"),
LabelsAuto: true,
Pull: true,
},
DefaultLogin: Login{
Registry: "https://index.docker.io/v1/",
},
LoginsRaw: "[]",
Cleanup: true,
}
func TestDefaultLogin(t *testing.T) {
s := defaultSettings
assert.NoError(t, newSettingsOnly(&s).Validate())
if assert.Len(t, s.Logins, 1) {
assert.EqualValues(t, defaultSettings.DefaultLogin.Registry, s.Logins[0].Registry)
}
// only use login to auth to registrys
loginsRaw, err := yaml2json.Convert([]byte(`
- registry: https://index.docker.io/v1/
username: docker_username
password: docker_password
- registry: https://codeberg.org
username: cb_username
password: cb_password`))
assert.NoError(t, err)
s.LoginsRaw = string(loginsRaw)
assert.NoError(t, newSettingsOnly(&s).Validate())
if assert.Len(t, s.Logins, 2) {
assert.EqualValues(t, defaultSettings.DefaultLogin.Registry, s.Logins[0].Registry)
}
// mixed login settings ('logins' and 'username', 'password' are used)
s = defaultSettings
loginsRaw, err = yaml2json.Convert([]byte(`
- registry: https://codeberg.org
username: cb_username
password: cb_password`))
assert.NoError(t, err)
s.LoginsRaw = string(loginsRaw)
s.DefaultLogin.Username = "docker_username"
s.DefaultLogin.Password = "docker_password"
assert.NoError(t, newSettingsOnly(&s).Validate())
if assert.Len(t, s.Logins, 2) {
assert.EqualValues(t, defaultSettings.DefaultLogin.Registry, s.Logins[0].Registry)
}
// ignore default registry
s = defaultSettings
loginsRaw, err = yaml2json.Convert([]byte(`
- registry: https://codeberg.org
username: cb_username
password: cb_password`))
assert.NoError(t, err)
s.LoginsRaw = string(loginsRaw)
assert.NoError(t, newSettingsOnly(&s).Validate())
if assert.Len(t, s.Logins, 1) {
assert.EqualValues(t, "https://codeberg.org", s.Logins[0].Registry)
}
}