1package state
2
3import (
4	"github.com/google/go-cmp/cmp"
5	"testing"
6)
7
8func TestSelectReleasesWithOverrides(t *testing.T) {
9	type testcase struct {
10		subject  string
11		selector []string
12		want     []string
13	}
14
15	testcases := []testcase{
16		{
17			subject:  "multiple OR selectors (nillable label first)",
18			selector: []string{"type=bar", "name=nolabel2", "name=nolabel1"},
19			want:     []string{"nolabel1", "nolabel2", "foo"},
20		},
21		{
22			subject:  "multiple OR selectors (non-nillable label first)",
23			selector: []string{"name=foo", "type!=bar"},
24			want:     []string{"nolabel1", "nolabel2", "foo"},
25		},
26		{
27			subject:  "multiple AND conditions (nillable label first)",
28			selector: []string{"type!=bar,name!=nolabel2"},
29			want:     []string{"nolabel1"},
30		},
31		{
32			subject:  "multiple AND conditions (non-nillable label first)",
33			selector: []string{"name!=nolabel2,type!=bar"},
34			want:     []string{"nolabel1"},
35		},
36		{
37			subject:  "inequality on nillable label",
38			selector: []string{"type!=bar"},
39			want:     []string{"nolabel1", "nolabel2"},
40		},
41		{
42			subject:  "equality on nillable label",
43			selector: []string{"type=bar"},
44			want:     []string{"foo"},
45		},
46		{
47			subject:  "inequality on non-nillable label",
48			selector: []string{"name!=nolabel1"},
49			want:     []string{"nolabel2", "foo"},
50		},
51		{
52			subject:  "equality on non-nillable label",
53			selector: []string{"name=nolabel1"},
54			want:     []string{"nolabel1"},
55		},
56	}
57
58	example := []byte(`releases:
59- name: nolabel1
60  namespace: kube-system
61  chart: stable/nolabel
62- name: nolabel2
63  namespace: default
64  chart: stable/nolabel
65- name: foo
66  namespace: kube-system
67  chart: stable/foo
68  labels:
69    type: bar
70`)
71
72	state := stateTestEnv{
73		Files: map[string]string{
74			"/helmfile.yaml": string(example),
75		},
76		WorkDir: "/",
77	}.MustLoadState(t, "/helmfile.yaml", "default")
78
79	for _, tc := range testcases {
80		state.Selectors = tc.selector
81
82		rs, err := state.GetSelectedReleasesWithOverrides()
83		if err != nil {
84			t.Fatalf("%s %s: %v", tc.selector, tc.subject, err)
85		}
86
87		var got []string
88
89		for _, r := range rs {
90			got = append(got, r.Name)
91		}
92
93		if d := cmp.Diff(tc.want, got); d != "" {
94			t.Errorf("%s %s: %s", tc.selector, tc.subject, d)
95		}
96	}
97}
98