-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscope_test.go
More file actions
222 lines (203 loc) · 7.13 KB
/
Copy pathscope_test.go
File metadata and controls
222 lines (203 loc) · 7.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
package features
import (
"testing"
"github.com/cego/gitte/config"
"github.com/cego/gitte/state"
)
func TestProjectMatchesOverrideScope_NilOverride(t *testing.T) {
if ProjectMatchesOverrideScope("proj", "gitlab.cego.dk", "cego/monolith", nil) {
t.Error("expected false for nil override")
}
}
func TestProjectMatchesOverrideScope_ExplicitProjects(t *testing.T) {
override := &state.ScopeOverride{
Projects: []string{"monolith", "mysql"},
}
if !ProjectMatchesOverrideScope("monolith", "gitlab.cego.dk", "cego/monolith", override) {
t.Error("expected monolith to match")
}
if ProjectMatchesOverrideScope("redis", "gitlab.cego.dk", "cego/redis", override) {
t.Error("expected redis to not match")
}
}
func TestProjectMatchesOverrideScope_GitlabGroup(t *testing.T) {
override := &state.ScopeOverride{
GitlabGroups: []state.ScopeOverrideGroup{
{Host: "gitlab.cego.dk", Group: "cego"},
},
}
if !ProjectMatchesOverrideScope("monolith", "gitlab.cego.dk", "cego/monolith", override) {
t.Error("expected cego/monolith to match group cego")
}
if ProjectMatchesOverrideScope("promo", "gitlab.cego.dk", "spilnu/promo", override) {
t.Error("expected spilnu/promo to not match group cego")
}
}
func TestProjectMatchesOverrideScope_GitlabGroupDelimiter(t *testing.T) {
override := &state.ScopeOverride{
GitlabGroups: []state.ScopeOverrideGroup{
{Host: "gitlab.cego.dk", Group: "spilnu"},
},
}
if ProjectMatchesOverrideScope("foo", "gitlab.cego.dk", "spilnu-legacy/foo", override) {
t.Error("expected spilnu-legacy/foo to not match group spilnu")
}
if !ProjectMatchesOverrideScope("promo", "gitlab.cego.dk", "spilnu/services/promo", override) {
t.Error("expected spilnu/services/promo to match group spilnu")
}
}
func TestProjectMatchesOverrideScope_ExcludeProjects(t *testing.T) {
override := &state.ScopeOverride{
GitlabGroups: []state.ScopeOverrideGroup{
{Host: "gitlab.cego.dk", Group: "cego", ExcludeProjects: []string{"mysql"}},
},
}
if !ProjectMatchesOverrideScope("monolith", "gitlab.cego.dk", "cego/monolith", override) {
t.Error("expected monolith to match (not excluded)")
}
if ProjectMatchesOverrideScope("mysql", "gitlab.cego.dk", "cego/mysql", override) {
t.Error("expected mysql to be excluded")
}
}
func TestProjectMatchesOverrideScope_GithubOrg(t *testing.T) {
override := &state.ScopeOverride{
GithubOrgs: []state.ScopeOverrideOrg{
{Host: "github.com", Org: "cego"},
},
}
if !ProjectMatchesOverrideScope("gitte", "github.com", "cego/gitte", override) {
t.Error("expected cego/gitte to match org cego")
}
if ProjectMatchesOverrideScope("other", "github.com", "other-org/repo", override) {
t.Error("expected other-org/repo to not match org cego")
}
}
func TestProjectMatchesOverrideScope_EmptyOverride(t *testing.T) {
override := &state.ScopeOverride{}
if ProjectMatchesOverrideScope("proj", "gitlab.cego.dk", "cego/proj", override) {
t.Error("expected false for empty override")
}
}
func TestBuildScopeTree_GroupsProjectsByHostAndNamespace(t *testing.T) {
projects := map[string]ScopeProject{
"monolith": {Host: "gitlab.cego.dk", Path: "cego/monolith"},
"mysql": {Host: "gitlab.cego.dk", Path: "cego/mysql"},
"promo": {Host: "gitlab.cego.dk", Path: "spilnu/services/promo"},
}
rows := BuildScopeTree(projects)
if len(rows) == 0 {
t.Fatal("expected rows")
}
if rows[0].Kind != ScopeRowHost {
t.Errorf("expected first row to be host, got %v", rows[0].Kind)
}
if rows[0].Label != "gitlab.cego.dk" {
t.Errorf("expected label gitlab.cego.dk, got %q", rows[0].Label)
}
}
func TestCheckedStateToOverride_AllChecked(t *testing.T) {
checked := map[string]bool{"monolith": true, "mysql": true}
projects := map[string]ScopeProject{
"monolith": {Host: "gitlab.cego.dk", Path: "cego/monolith"},
"mysql": {Host: "gitlab.cego.dk", Path: "cego/mysql"},
}
override := CheckedStateToOverride(checked, projects)
if override != nil {
t.Error("expected nil override when all checked")
}
}
func TestCheckedStateToOverride_NoneChecked(t *testing.T) {
checked := map[string]bool{"monolith": false, "mysql": false}
projects := map[string]ScopeProject{
"monolith": {Host: "gitlab.cego.dk", Path: "cego/monolith"},
"mysql": {Host: "gitlab.cego.dk", Path: "cego/mysql"},
}
override := CheckedStateToOverride(checked, projects)
if override == nil {
t.Fatal("expected non-nil override when none checked")
}
if len(override.Projects) != 0 || len(override.GitlabGroups) != 0 || len(override.GithubOrgs) != 0 {
t.Error("expected empty override")
}
}
func TestCheckedStateToOverride_PartialGroup(t *testing.T) {
checked := map[string]bool{"monolith": true, "mysql": false}
projects := map[string]ScopeProject{
"monolith": {Host: "gitlab.cego.dk", Path: "cego/monolith"},
"mysql": {Host: "gitlab.cego.dk", Path: "cego/mysql"},
}
override := CheckedStateToOverride(checked, projects)
if override == nil {
t.Fatal("expected non-nil override")
}
if len(override.GitlabGroups) != 1 {
t.Fatalf("expected 1 gitlab group, got %d", len(override.GitlabGroups))
}
g := override.GitlabGroups[0]
if g.Group != "cego" {
t.Errorf("expected group cego, got %q", g.Group)
}
if len(g.ExcludeProjects) != 1 || g.ExcludeProjects[0] != "mysql" {
t.Errorf("expected exclude [mysql], got %v", g.ExcludeProjects)
}
}
func TestProjectsInGateScope_EmptyScopeMatchesAll(t *testing.T) {
cfg := &config.GitteConfig{
Projects: map[string]config.ProjectConfig{
"monolith": {Remote: "git@gitlab.cego.dk:cego/monolith.git"},
"promo": {Remote: "git@gitlab.cego.dk:spilnu/services/promo.git"},
},
}
gate := config.FeatureGate{} // no scope => all projects
got := ProjectsInGateScope(cfg, gate)
if len(got) != 2 {
t.Fatalf("expected 2 projects, got %d", len(got))
}
if got["promo"].Path != "spilnu/services/promo" {
t.Errorf("expected parsed path for promo, got %q", got["promo"].Path)
}
}
func TestProjectsInGateScope_RestrictedByGitlabGroup(t *testing.T) {
cfg := &config.GitteConfig{
Projects: map[string]config.ProjectConfig{
"monolith": {Remote: "git@gitlab.cego.dk:cego/monolith.git"},
"promo": {Remote: "git@gitlab.cego.dk:spilnu/services/promo.git"},
"broken": {Remote: "not-a-valid-remote"},
},
}
gate := config.FeatureGate{
Scope: config.FeatureScope{
GitlabGroups: []config.GitlabScope{
{Host: "gitlab.cego.dk", Group: "spilnu"},
},
},
}
got := ProjectsInGateScope(cfg, gate)
if len(got) != 1 {
t.Fatalf("expected 1 project in scope, got %d", len(got))
}
if _, ok := got["promo"]; !ok {
t.Error("expected promo to be in scope")
}
if _, ok := got["monolith"]; ok {
t.Error("expected monolith to be out of scope")
}
}
func TestOverrideToCheckedState(t *testing.T) {
override := &state.ScopeOverride{
GitlabGroups: []state.ScopeOverrideGroup{
{Host: "gitlab.cego.dk", Group: "cego", ExcludeProjects: []string{"mysql"}},
},
}
projects := map[string]ScopeProject{
"monolith": {Host: "gitlab.cego.dk", Path: "cego/monolith"},
"mysql": {Host: "gitlab.cego.dk", Path: "cego/mysql"},
}
checked := OverrideToCheckedState(override, projects)
if !checked["monolith"] {
t.Error("expected monolith checked")
}
if checked["mysql"] {
t.Error("expected mysql unchecked")
}
}