Some checks failed
SonarQube / SonarQube (push) Failing after 5m28s
E2e test on main branch push / server and client test (map[name:admin port:8080]) (push) Failing after 3m33s
Server and lib tests / Library test (js) (push) Failing after 1m36s
Server and lib tests / Library test (vue) (push) Successful in 2m24s
Server and lib tests / Web client test (admin) (push) Successful in 2m52s
Server and lib tests / Web client test (compose) (push) Successful in 3m23s
Server and lib tests / Web client test (discovery) (push) Successful in 2m49s
Server and lib tests / Web client test (one) (push) Successful in 2m52s
Server and lib tests / Web client test (privacy) (push) Successful in 2m44s
Server and lib tests / Web client test (reporter) (push) Successful in 2m56s
Server and lib tests / Web client test (workflow) (push) Successful in 3m0s
Server and lib tests / Server tests (push) Failing after 4m55s
E2e test on main branch push / notify-failure (push) Failing after 9s
Server and lib tests / Send matrix testing notification (push) Successful in 2s
Mark stale issues and pull requests / stale (push) Successful in 40s
157 lines
3.6 KiB
Go
157 lines
3.6 KiB
Go
package label
|
|
|
|
import (
|
|
"reflect"
|
|
"testing"
|
|
|
|
"github.com/cortezaproject/corteza/server/pkg/label/types"
|
|
)
|
|
|
|
func TestChanged(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
old map[string]types.LabelValue
|
|
new map[string]types.LabelValue
|
|
want bool
|
|
}{
|
|
{
|
|
"2x nil",
|
|
nil,
|
|
nil,
|
|
false,
|
|
},
|
|
{
|
|
"2x empty",
|
|
map[string]types.LabelValue{},
|
|
map[string]types.LabelValue{},
|
|
false,
|
|
},
|
|
{
|
|
"nil & empty",
|
|
nil,
|
|
map[string]types.LabelValue{},
|
|
false,
|
|
},
|
|
{
|
|
"same single value",
|
|
map[string]types.LabelValue{"a": {Val: "a"}},
|
|
map[string]types.LabelValue{"a": {Val: "a"}},
|
|
false,
|
|
},
|
|
{
|
|
"same multi value",
|
|
map[string]types.LabelValue{"a": {Values: []string{"1", "2"}}},
|
|
map[string]types.LabelValue{"a": {Values: []string{"1", "2"}}},
|
|
false,
|
|
},
|
|
{
|
|
"diff single value",
|
|
map[string]types.LabelValue{"a": {Val: "a"}},
|
|
map[string]types.LabelValue{"a": {Val: "b"}},
|
|
true,
|
|
},
|
|
{
|
|
"diff multi value",
|
|
map[string]types.LabelValue{"a": {Values: []string{"1", "2"}}},
|
|
map[string]types.LabelValue{"a": {Values: []string{"1", "3"}}},
|
|
true,
|
|
},
|
|
{
|
|
"diff key removed",
|
|
map[string]types.LabelValue{"a": {Val: "b"}},
|
|
map[string]types.LabelValue{},
|
|
true,
|
|
},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
if got := Changed(tt.old, tt.new); got != tt.want {
|
|
t.Errorf("Changed() = %v, want %v", got, tt.want)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestParseStrings(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
labels []string
|
|
want map[string]types.LabelValue
|
|
wantErr bool
|
|
}{
|
|
{
|
|
"set of pairs",
|
|
[]string{"aa=b"},
|
|
map[string]types.LabelValue{"aa": {Val: "b"}},
|
|
false,
|
|
},
|
|
{
|
|
"empty json",
|
|
[]string{`{}`},
|
|
map[string]types.LabelValue{},
|
|
false,
|
|
},
|
|
{
|
|
"json",
|
|
[]string{`{"aa":"b"}`},
|
|
map[string]types.LabelValue{"aa": {Val: "b"}},
|
|
false,
|
|
},
|
|
{
|
|
"value with colons and slashes (single)",
|
|
[]string{"ref_namespace=corteza::compose:namespace/123"},
|
|
map[string]types.LabelValue{"ref_namespace": {Val: "corteza::compose:namespace/123"}},
|
|
false,
|
|
},
|
|
{
|
|
"value with colons and slashes (array)",
|
|
[]string{`ref_namespace=["corteza::compose:namespace/123","corteza::compose:namespace/456"]`},
|
|
map[string]types.LabelValue{"ref_namespace": {Values: []string{"corteza::compose:namespace/123", "corteza::compose:namespace/456"}}},
|
|
false,
|
|
},
|
|
{
|
|
"multiple reference labels",
|
|
[]string{
|
|
"ref_namespace=corteza::compose:namespace/123",
|
|
"ref_module=corteza::compose:module/123/456",
|
|
},
|
|
map[string]types.LabelValue{
|
|
"ref_namespace": {Val: "corteza::compose:namespace/123"},
|
|
"ref_module": {Val: "corteza::compose:module/123/456"},
|
|
},
|
|
false,
|
|
},
|
|
{
|
|
"quoted string value (strips quotes)",
|
|
[]string{`ref_module="corteza::compose:module/1239888/408"`},
|
|
map[string]types.LabelValue{"ref_module": {Val: "corteza::compose:module/1239888/408"}},
|
|
false,
|
|
},
|
|
{
|
|
"mixed: array and quoted string",
|
|
[]string{
|
|
`ref_namespace=["corteza::compose:namespace/123"]`,
|
|
`ref_module="corteza::compose:module/1239888/408"`,
|
|
},
|
|
map[string]types.LabelValue{
|
|
"ref_namespace": {Values: []string{"corteza::compose:namespace/123"}},
|
|
"ref_module": {Val: "corteza::compose:module/1239888/408"},
|
|
},
|
|
false,
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
got, err := ParseStrings(tt.labels)
|
|
if (err != nil) != tt.wantErr {
|
|
t.Errorf("ParseStrings() error = %v, wantErr %v", err, tt.wantErr)
|
|
return
|
|
}
|
|
if !reflect.DeepEqual(got, tt.want) {
|
|
t.Errorf("ParseStrings() got = %v, want %v", got, tt.want)
|
|
}
|
|
})
|
|
}
|
|
}
|