|
1
|
|
|
package rollout |
|
2
|
|
|
|
|
3
|
|
|
import ( |
|
4
|
|
|
"reflect" |
|
5
|
|
|
"testing" |
|
6
|
|
|
|
|
7
|
|
|
"github.com/satori/go.uuid" |
|
8
|
|
|
"github.com/stretchr/testify/assert" |
|
9
|
|
|
) |
|
10
|
|
|
|
|
11
|
|
|
func TestRollout_IsActive(t *testing.T) { |
|
12
|
|
|
features := []Feature{ |
|
13
|
|
|
Feature{"foo", 100, false, []string{}}, |
|
14
|
|
|
Feature{"bar", 50, true, []string{}}, |
|
15
|
|
|
Feature{"baz", 0, true, []string{"d0b7b9df"}}, |
|
16
|
|
|
} |
|
17
|
|
|
r := Create(features) |
|
18
|
|
|
|
|
19
|
|
|
id, _ := uuid.NewV4() |
|
20
|
|
|
|
|
21
|
|
|
type fields struct { |
|
22
|
|
|
features map[string]Feature |
|
23
|
|
|
} |
|
24
|
|
|
type args struct { |
|
25
|
|
|
feature string |
|
26
|
|
|
id string |
|
27
|
|
|
} |
|
28
|
|
|
tests := []struct { |
|
29
|
|
|
name string |
|
30
|
|
|
fields fields |
|
31
|
|
|
args args |
|
32
|
|
|
want bool |
|
33
|
|
|
}{ |
|
34
|
|
|
{"rollout 100%", fields{r.features}, args{"foo", id.String()}, true}, |
|
35
|
|
|
{"rollout 0%", fields{r.features}, args{"baz", id.String()}, false}, |
|
36
|
|
|
{"whitelist", fields{r.features}, args{"baz", "d0b7b9df"}, true}, |
|
37
|
|
|
{"rollout 50% out", fields{r.features}, args{"bar", "d0b7b9df-9fa8-4f72-885b-3f1cd0d705d5"}, false}, |
|
38
|
|
|
{"rollout 50% int", fields{r.features}, args{"bar", "8975b460-4446-4af2-965a-ba020092e1ca"}, true}, |
|
39
|
|
|
} |
|
40
|
|
|
for _, tt := range tests { |
|
41
|
|
|
t.Run(tt.name, func(t *testing.T) { |
|
42
|
|
|
r := Rollout{ |
|
43
|
|
|
features: tt.fields.features, |
|
44
|
|
|
} |
|
45
|
|
|
if got := r.IsActive(tt.args.feature, tt.args.id); got != tt.want { |
|
46
|
|
|
t.Errorf("Rollout.IsActive() = %v, want %v", got, tt.want) |
|
47
|
|
|
} |
|
48
|
|
|
}) |
|
49
|
|
|
} |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
func TestRollout_IsFeatureActive(t *testing.T) { |
|
53
|
|
|
features := []Feature{ |
|
54
|
|
|
Feature{"foo", 0.5, false, []string{}}, |
|
55
|
|
|
Feature{"bar", 0.5, true, []string{}}, |
|
56
|
|
|
} |
|
57
|
|
|
r := Create(features) |
|
58
|
|
|
type fields struct { |
|
59
|
|
|
features map[string]Feature |
|
60
|
|
|
} |
|
61
|
|
|
type args struct { |
|
62
|
|
|
feature string |
|
63
|
|
|
} |
|
64
|
|
|
tests := []struct { |
|
65
|
|
|
name string |
|
66
|
|
|
fields fields |
|
67
|
|
|
args args |
|
68
|
|
|
want bool |
|
69
|
|
|
}{ |
|
70
|
|
|
{"active", fields{r.features}, args{"foo"}, false}, |
|
71
|
|
|
{"deactivated", fields{r.features}, args{"bar"}, true}, |
|
72
|
|
|
} |
|
73
|
|
|
for _, tt := range tests { |
|
74
|
|
|
t.Run(tt.name, func(t *testing.T) { |
|
75
|
|
|
r := Rollout{ |
|
76
|
|
|
features: tt.fields.features, |
|
77
|
|
|
} |
|
78
|
|
|
if got := r.IsFeatureActive(tt.args.feature); got != tt.want { |
|
79
|
|
|
t.Errorf("Rollout.IsFeatureActive() = %v, want %v", got, tt.want) |
|
80
|
|
|
} |
|
81
|
|
|
}) |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
func TestRollout_Activate(t *testing.T) { |
|
86
|
|
|
features := []Feature{Feature{"foo", 0.5, false, []string{}}} |
|
87
|
|
|
r := Create(features) |
|
88
|
|
|
r.Activate("foo") |
|
89
|
|
|
assert.Equal(t, true, r.features["foo"].Active) |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
func TestRollout_Deactivate(t *testing.T) { |
|
93
|
|
|
features := []Feature{Feature{"foo", 0.5, true, []string{}}} |
|
94
|
|
|
r := Create(features) |
|
95
|
|
|
r.Deactivate("foo") |
|
96
|
|
|
assert.Equal(t, false, r.features["foo"].Active) |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
func TestRollout_Set(t *testing.T) { |
|
100
|
|
|
features := []Feature{Feature{"foo", 0.5, true, []string{}}} |
|
101
|
|
|
expected := Feature{"foo", 0.7, true, []string{}} |
|
102
|
|
|
r := Create(features) |
|
103
|
|
|
r.Set(expected) |
|
104
|
|
|
assert.Equal(t, expected, r.features["foo"]) |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
func TestCreate(t *testing.T) { |
|
108
|
|
|
f := Feature{"foo", 0.5, true, []string{}} |
|
109
|
|
|
features := []Feature{f} |
|
110
|
|
|
r := Create(features) |
|
111
|
|
|
assert.Equal(t, f, r.features["foo"]) |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
func TestRollout_Get(t *testing.T) { |
|
115
|
|
|
foo := Feature{"foo", 0.5, false, []string{}} |
|
116
|
|
|
features := []Feature{foo} |
|
117
|
|
|
r := Create(features) |
|
118
|
|
|
type fields struct { |
|
119
|
|
|
features map[string]Feature |
|
120
|
|
|
} |
|
121
|
|
|
type args struct { |
|
122
|
|
|
feature string |
|
123
|
|
|
} |
|
124
|
|
|
tests := []struct { |
|
125
|
|
|
name string |
|
126
|
|
|
fields fields |
|
127
|
|
|
args args |
|
128
|
|
|
want Feature |
|
129
|
|
|
want1 bool |
|
130
|
|
|
}{ |
|
131
|
|
|
{"feature exists", fields{r.features}, args{"foo"}, foo, true}, |
|
132
|
|
|
{"feature does not exist", fields{r.features}, args{"bar"}, Feature{}, false}, |
|
133
|
|
|
} |
|
134
|
|
|
for _, tt := range tests { |
|
135
|
|
|
t.Run(tt.name, func(t *testing.T) { |
|
136
|
|
|
r := Rollout{ |
|
137
|
|
|
features: tt.fields.features, |
|
138
|
|
|
} |
|
139
|
|
|
got, got1 := r.Get(tt.args.feature) |
|
140
|
|
|
if !reflect.DeepEqual(got, tt.want) { |
|
141
|
|
|
t.Errorf("Rollout.Get() got = %v, want %v", got, tt.want) |
|
142
|
|
|
} |
|
143
|
|
|
if got1 != tt.want1 { |
|
144
|
|
|
t.Errorf("Rollout.Get() got1 = %v, want %v", got1, tt.want1) |
|
145
|
|
|
} |
|
146
|
|
|
}) |
|
147
|
|
|
} |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
func TestRollout_GetAll(t *testing.T) { |
|
151
|
|
|
var emptyFeatures []Feature |
|
152
|
|
|
features := []Feature{ |
|
153
|
|
|
Feature{"bar", 0.5, true, []string{}}, |
|
154
|
|
|
Feature{"foo", 0.5, false, []string{}}, |
|
155
|
|
|
} |
|
156
|
|
|
r := Create(features) |
|
157
|
|
|
|
|
158
|
|
|
type fields struct { |
|
159
|
|
|
features map[string]Feature |
|
160
|
|
|
} |
|
161
|
|
|
tests := []struct { |
|
162
|
|
|
name string |
|
163
|
|
|
fields fields |
|
164
|
|
|
want []Feature |
|
165
|
|
|
}{ |
|
166
|
|
|
{"sanity", fields{r.features}, features}, |
|
167
|
|
|
{"sanity", fields{make(map[string]Feature)}, emptyFeatures}, |
|
168
|
|
|
} |
|
169
|
|
|
for _, tt := range tests { |
|
170
|
|
|
t.Run(tt.name, func(t *testing.T) { |
|
171
|
|
|
r := Rollout{ |
|
172
|
|
|
features: tt.fields.features, |
|
173
|
|
|
} |
|
174
|
|
|
if got := r.GetAll(); !reflect.DeepEqual(got, tt.want) { |
|
175
|
|
|
t.Errorf("Rollout.GetAll() = %v, want %v", got, tt.want) |
|
176
|
|
|
} |
|
177
|
|
|
}) |
|
178
|
|
|
} |
|
179
|
|
|
} |
|
180
|
|
|
|