Debian Codegraph @

featuregate/flag_test.go

raw
1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
3
4 package featuregate
5
6 import (
7 "flag"
8 "testing"
9
10 "github.com/stretchr/testify/assert"
11 "github.com/stretchr/testify/require"
12 )
13
14 func TestNewFlag(t *testing.T) {
15 for _, tt := range []struct {
16 name string
17 input string
18 expectedSetErr bool
19 expected map[string]bool
20 expectedStr string
21 }{
22 {
23 name: "empty item",
24 input: "",
25 expected: map[string]bool{"alpha": false, "beta": true, "deprecated": false, "stable": true},
26 expectedStr: "-alpha,beta,-deprecated,stable",
27 },
28 {
29 name: "simple enable alpha",
30 input: "alpha",
31 expected: map[string]bool{"alpha": true, "beta": true, "deprecated": false, "stable": true},
32 expectedStr: "alpha,beta,-deprecated,stable",
33 },
34 {
35 name: "plus enable alpha",
36 input: "+alpha",
37 expected: map[string]bool{"alpha": true, "beta": true, "deprecated": false, "stable": true},
38 expectedStr: "alpha,beta,-deprecated,stable",
39 },
40 {
41 name: "disabled beta",
42 input: "-beta",
43 expected: map[string]bool{"alpha": false, "beta": false, "deprecated": false, "stable": true},
44 expectedStr: "-alpha,-beta,-deprecated,stable",
45 },
46 {
47 name: "multiple items",
48 input: "-beta,alpha",
49 expected: map[string]bool{"alpha": true, "beta": false, "deprecated": false, "stable": true},
50 expectedStr: "alpha,-beta,-deprecated,stable",
51 },
52 {
53 name: "multiple items with plus",
54 input: "-beta,+alpha",
55 expected: map[string]bool{"alpha": true, "beta": false, "deprecated": false, "stable": true},
56 expectedStr: "alpha,-beta,-deprecated,stable",
57 },
58 {
59 name: "repeated items",
60 input: "alpha,-beta,-alpha",
61 expected: map[string]bool{"alpha": false, "beta": false, "deprecated": false, "stable": true},
62 expectedStr: "-alpha,-beta,-deprecated,stable",
63 },
64 {
65 name: "multiple plus items",
66 input: "+alpha,+beta",
67 expected: map[string]bool{"alpha": true, "beta": true, "deprecated": false, "stable": true},
68 expectedStr: "alpha,beta,-deprecated,stable",
69 },
70 {
71 name: "enable stable",
72 input: "stable",
73 expected: map[string]bool{"alpha": false, "beta": true, "deprecated": false, "stable": true},
74 expectedStr: "-alpha,beta,-deprecated,stable",
75 },
76 {
77 name: "disable stable",
78 input: "-stable",
79 expectedSetErr: true,
80 expected: map[string]bool{"alpha": false, "beta": true, "deprecated": false, "stable": true},
81 expectedStr: "-alpha,beta,-deprecated,stable",
82 },
83 {
84 name: "enable deprecated",
85 input: "deprecated",
86 expectedSetErr: true,
87 expected: map[string]bool{"alpha": false, "beta": true, "deprecated": false, "stable": true},
88 expectedStr: "-alpha,beta,-deprecated,stable",
89 },
90 {
91 name: "disable deprecated",
92 input: "-deprecated",
93 expected: map[string]bool{"alpha": false, "beta": true, "deprecated": false, "stable": true},
94 expectedStr: "-alpha,beta,-deprecated,stable",
95 },
96 {
97 name: "enable missing",
98 input: "missing",
99 expectedSetErr: true,
100 expected: map[string]bool{"alpha": false, "beta": true, "deprecated": false, "stable": true},
101 expectedStr: "-alpha,beta,-deprecated,stable",
102 },
103 {
104 name: "disable missing",
105 input: "missing",
106 expectedSetErr: true,
107 expected: map[string]bool{"alpha": false, "beta": true, "deprecated": false, "stable": true},
108 expectedStr: "-alpha,beta,-deprecated,stable",
109 },
110 } {
111 t.Run(tt.name, func(t *testing.T) {
112 reg := NewRegistry()
113 reg.MustRegister("alpha", StageAlpha)
114 reg.MustRegister("beta", StageBeta)
115 reg.MustRegister("deprecated", StageDeprecated, WithRegisterToVersion("1.0.0"))
116 reg.MustRegister("stable", StageStable, WithRegisterToVersion("1.0.0"))
117 fs := flag.NewFlagSet("test", flag.ContinueOnError)
118 reg.RegisterFlags(fs)
119 registrationFlag := fs.Lookup(featureGatesFlag)
120 require.NotNil(t, registrationFlag)
121 if tt.expectedSetErr {
122 require.Error(t, registrationFlag.Value.Set(tt.input))
123 } else {
124 require.NoError(t, registrationFlag.Value.Set(tt.input))
125 }
126 got := map[string]bool{}
127 reg.VisitAll(func(g *Gate) {
128 got[g.ID()] = g.IsEnabled()
129 })
130 assert.Equal(t, tt.expected, got)
131 assert.Equal(t, tt.expectedStr, registrationFlag.Value.String())
132 })
133 }
134 }
135
136 func TestFlagStringNotInitialize(t *testing.T) {
137 flag := &flagValue{}
138 assert.Empty(t, flag.String())
139 }