File: featureflag_test.go

package info (click to toggle)
gitlab-shell 14.35.0%2Bds1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 23,652 kB
  • sloc: ruby: 1,129; makefile: 583; sql: 391; sh: 384
file content (184 lines) | stat: -rw-r--r-- 4,318 bytes parent folder | download | duplicates (5)
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
package featureflag

import (
	"fmt"
	"testing"

	"github.com/stretchr/testify/require"
	"google.golang.org/grpc/metadata"
)

func TestFeatureFlag_FromMetadataKey(t *testing.T) {
	defer func(old map[string]FeatureFlag) {
		flagsByName = old
	}(flagsByName)

	defaultEnabled := FeatureFlag{
		Name:        "default_enabled",
		OnByDefault: true,
	}
	defaultDisabled := FeatureFlag{
		Name:        "default_disabled",
		OnByDefault: false,
	}

	flagsByName = map[string]FeatureFlag{
		defaultEnabled.Name:  defaultEnabled,
		defaultDisabled.Name: defaultDisabled,
	}

	for _, tc := range []struct {
		desc         string
		metadataKey  string
		expectedErr  error
		expectedFlag FeatureFlag
	}{
		{
			desc:        "empty key",
			metadataKey: "",
			expectedErr: fmt.Errorf("not a feature flag: \"\""),
		},
		{
			desc:        "invalid prefix",
			metadataKey: "invalid-prefix",
			expectedErr: fmt.Errorf("not a feature flag: \"invalid-prefix\""),
		},
		{
			desc:         "default enabled flag",
			metadataKey:  defaultEnabled.MetadataKey(),
			expectedFlag: defaultEnabled,
		},
		{
			desc:         "default disabled flag",
			metadataKey:  defaultDisabled.MetadataKey(),
			expectedFlag: defaultDisabled,
		},
		{
			desc:        "undefined flag",
			metadataKey: "gitaly-feature-not-defined",
			expectedFlag: FeatureFlag{
				Name: "not_defined",
			},
		},
	} {
		t.Run(tc.desc, func(t *testing.T) {
			flag, err := FromMetadataKey(tc.metadataKey)
			require.Equal(t, tc.expectedErr, err)
			require.Equal(t, tc.expectedFlag, flag)
		})
	}
}

func TestFeatureFlag_enabled(t *testing.T) {
	for _, tc := range []struct {
		desc        string
		flag        string
		headers     map[string]string
		shouldPanic bool
		enabled     bool
		onByDefault bool
	}{
		{
			desc:        "empty name",
			flag:        "",
			shouldPanic: true,
		},
		{
			desc:        "flag name too short",
			flag:        "a",
			shouldPanic: true,
		},
		{
			desc:        "flag name start with number",
			flag:        "0_flag",
			shouldPanic: true,
		},
		{
			desc:        "flag name start with underscore",
			flag:        "_flag",
			shouldPanic: true,
		},
		{
			desc:        "flag name end with underscore",
			flag:        "flag_",
			shouldPanic: true,
		},
		{
			desc:        "flag name with uppercase",
			flag:        "Flag",
			shouldPanic: true,
		},
		{
			desc:        "flag name with dashes",
			flag:        "flag-with-dash",
			shouldPanic: true,
		},
		{
			desc:        "flag name with characters disallowed by grpc metadata key",
			flag:        "flag_with_colon:_and_slash/",
			shouldPanic: true,
		},
		{
			desc:        "flag name with underscores",
			flag:        "flag_under_score",
			headers:     map[string]string{"gitaly-feature-flag-under-score": "true"},
			shouldPanic: false,
			enabled:     true,
			onByDefault: false,
		},
		{
			desc:        "no headers",
			flag:        "flag",
			headers:     nil,
			shouldPanic: false,
			enabled:     false,
			onByDefault: false,
		},
		{
			desc:        "no 'gitaly-feature' prefix in flag name",
			flag:        "flag",
			headers:     map[string]string{"flag": "true"},
			shouldPanic: false,
			enabled:     false,
			onByDefault: false,
		},
		{
			desc:        "not valid header value",
			flag:        "flag",
			headers:     map[string]string{"gitaly-feature-flag": "TRUE"},
			shouldPanic: false,
			enabled:     false,
			onByDefault: false,
		},
		{
			desc:        "flag explicitly disabled",
			flag:        "flag",
			headers:     map[string]string{"gitaly-feature-flag": "false"},
			shouldPanic: false,
			enabled:     false,
			onByDefault: true,
		},
		{
			desc:        "flag enabled by default but missing",
			flag:        "flag",
			headers:     map[string]string{},
			shouldPanic: false,
			enabled:     true,
			onByDefault: true,
		},
	} {
		t.Run(tc.desc, func(t *testing.T) {
			ctx := metadata.NewIncomingContext(createContext(), metadata.New(tc.headers))

			var ff FeatureFlag
			ffInitFunc := func() { ff = NewFeatureFlag(tc.flag, "", "", tc.onByDefault) }
			if tc.shouldPanic {
				require.PanicsWithValue(t, "invalid feature flag name.", ffInitFunc)
				return
			}
			require.NotPanics(t, ffInitFunc)
			require.Equal(t, tc.enabled, ff.IsEnabled(ctx))
			require.Equal(t, tc.enabled, !ff.IsDisabled(ctx))
		})
	}
}