|
| 1 | +package common |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + configv1 "github.com/openshift/api/config/v1" |
| 7 | + "github.com/openshift/library-go/pkg/operator/configobserver/featuregates" |
| 8 | + "github.com/stretchr/testify/assert" |
| 9 | + "testing" |
| 10 | + "time" |
| 11 | +) |
| 12 | + |
| 13 | +const ( |
| 14 | + FeatureGatesTestExistingEnaFeatureGate1 configv1.FeatureGateName = "testExistingFeatureGate-enabled-1" |
| 15 | + FeatureGatesTestExistingEnaFeatureGate2 configv1.FeatureGateName = "testExistingFeatureGate-enabled-2" |
| 16 | + FeatureGatesTestExistingDisFeatureGate1 configv1.FeatureGateName = "testExistingFeatureGate-disabled-1" |
| 17 | +) |
| 18 | + |
| 19 | +type FeatureGatesHandlerStub struct { |
| 20 | + testing *testing.T |
| 21 | + featureGates featuregates.FeatureGate |
| 22 | + featuresChan chan struct{} |
| 23 | + initTime time.Duration |
| 24 | + fetchError error |
| 25 | +} |
| 26 | + |
| 27 | +func NewFeatureGatesHandlerStub(t *testing.T, initTime time.Duration, fetchError error) *FeatureGatesHandlerStub { |
| 28 | + return &FeatureGatesHandlerStub{ |
| 29 | + testing: t, |
| 30 | + featureGates: featuregates.NewFeatureGate( |
| 31 | + []configv1.FeatureGateName{ |
| 32 | + FeatureGatesTestExistingEnaFeatureGate1, |
| 33 | + FeatureGatesTestExistingEnaFeatureGate2, |
| 34 | + }, |
| 35 | + []configv1.FeatureGateName{ |
| 36 | + FeatureGatesTestExistingDisFeatureGate1, |
| 37 | + }), |
| 38 | + featuresChan: make(chan struct{}), |
| 39 | + initTime: initTime, |
| 40 | + fetchError: fetchError, |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +func (s *FeatureGatesHandlerStub) StartStub() { |
| 45 | + time.AfterFunc(s.initTime, func() { |
| 46 | + s.featuresChan <- struct{}{} |
| 47 | + }) |
| 48 | +} |
| 49 | + |
| 50 | +func (s *FeatureGatesHandlerStub) SetChangeHandler(_ featuregates.FeatureGateChangeHandlerFunc) { |
| 51 | + // The handler does not override the default FeatureGateChangeHandlerFunc |
| 52 | + s.testing.Fatal("SetChangeHandler should not be called by the feature gates handler") |
| 53 | +} |
| 54 | + |
| 55 | +func (s *FeatureGatesHandlerStub) Run(_ context.Context) { |
| 56 | + // Run is never called from the handler. The entity that creates the FeatureGateAccess is responsible |
| 57 | + // for properly initializing it and creating the Go rutine. |
| 58 | + s.testing.Fatal("Run should not be called by the feature gates handler") |
| 59 | +} |
| 60 | + |
| 61 | +func (s *FeatureGatesHandlerStub) InitialFeatureGatesObserved() <-chan struct{} { |
| 62 | + return s.featuresChan |
| 63 | +} |
| 64 | + |
| 65 | +func (s *FeatureGatesHandlerStub) CurrentFeatureGates() (featuregates.FeatureGate, error) { |
| 66 | + return s.featureGates, s.fetchError |
| 67 | +} |
| 68 | + |
| 69 | +func (s *FeatureGatesHandlerStub) AreInitialFeatureGatesObserved() bool { |
| 70 | + // The handler does not rely on AreInitialFeatureGatesObserved as it uses the channel |
| 71 | + // to get the notification of the FeatureGates ready. |
| 72 | + s.testing.Fatal("AreInitialFeatureGatesObserved should not be called by the feature gates handler") |
| 73 | + return false |
| 74 | +} |
| 75 | + |
| 76 | +func TestFeatureGateHandlerAccess(t *testing.T) { |
| 77 | + fgStub := NewFeatureGatesHandlerStub(t, 100*time.Millisecond, nil) |
| 78 | + handler := NewFeatureGatesAccessHandler(fgStub) |
| 79 | + assert.NotNil(t, handler) |
| 80 | + assert.Empty(t, handler.KnownFeatures()) |
| 81 | + fgStub.StartStub() |
| 82 | + assert.NoError(t, handler.Connect(context.Background())) |
| 83 | + |
| 84 | + checkFeatureGates(t, handler) |
| 85 | + |
| 86 | + // Check that a non-existing FG is reported as so and disabled by default |
| 87 | + const nonExistingFeatureGate = "test-non-existing" |
| 88 | + assert.False(t, handler.Exists(nonExistingFeatureGate)) |
| 89 | + assert.False(t, handler.Enabled(nonExistingFeatureGate)) |
| 90 | +} |
| 91 | + |
| 92 | +func TestFeatureGateHandlerAccessTimeout(t *testing.T) { |
| 93 | + fgStub := NewFeatureGatesHandlerStub(t, 100*time.Millisecond, nil) |
| 94 | + handler := NewFeatureGatesAccessHandler(fgStub) |
| 95 | + assert.NotNil(t, handler) |
| 96 | + // Set a really low timeout to make connection fail by timeout |
| 97 | + handler.connectTimeout = 10 * time.Millisecond |
| 98 | + fgStub.StartStub() |
| 99 | + |
| 100 | + err := handler.Connect(context.Background()) |
| 101 | + assert.ErrorContains(t, err, "timed out") |
| 102 | +} |
| 103 | + |
| 104 | +func TestFeatureGateHandlerAccessCancel(t *testing.T) { |
| 105 | + fgStub := NewFeatureGatesHandlerStub(t, 100*time.Millisecond, nil) |
| 106 | + handler := NewFeatureGatesAccessHandler(fgStub) |
| 107 | + assert.NotNil(t, handler) |
| 108 | + fgStub.StartStub() |
| 109 | + |
| 110 | + ctx, cancel := context.WithCancel(context.Background()) |
| 111 | + time.AfterFunc(10*time.Millisecond, func() { cancel() }) |
| 112 | + err := handler.Connect(ctx) |
| 113 | + assert.ErrorContains(t, err, "context canceled") |
| 114 | +} |
| 115 | + |
| 116 | +func TestFeatureGateHandlerAccessFetchError(t *testing.T) { |
| 117 | + fetchError := errors.New("fetch error") |
| 118 | + fgStub := NewFeatureGatesHandlerStub(t, 1*time.Millisecond, fetchError) |
| 119 | + handler := NewFeatureGatesAccessHandler(fgStub) |
| 120 | + assert.NotNil(t, handler) |
| 121 | + fgStub.StartStub() |
| 122 | + |
| 123 | + err := handler.Connect(context.Background()) |
| 124 | + assert.ErrorContains(t, err, fetchError.Error()) |
| 125 | +} |
| 126 | + |
| 127 | +func TestFeatureGateHandlerHardcoded(t *testing.T) { |
| 128 | + handler := NewFeatureGatesHardcodedHandler([]configv1.FeatureGateName{ |
| 129 | + FeatureGatesTestExistingEnaFeatureGate1, |
| 130 | + FeatureGatesTestExistingEnaFeatureGate2, |
| 131 | + }, |
| 132 | + []configv1.FeatureGateName{ |
| 133 | + FeatureGatesTestExistingDisFeatureGate1, |
| 134 | + }) |
| 135 | + assert.NotNil(t, handler) |
| 136 | + checkFeatureGates(t, handler) |
| 137 | +} |
| 138 | + |
| 139 | +func TestFeatureGateHandlerCR(t *testing.T) { |
| 140 | + fg := buildDummyFeatureGate() |
| 141 | + handler, err := NewFeatureGatesCRHandlerImpl(&fg, "v1") |
| 142 | + assert.NotNil(t, handler) |
| 143 | + assert.NoError(t, err) |
| 144 | + checkFeatureGates(t, handler) |
| 145 | +} |
| 146 | + |
| 147 | +func TestFeatureGateHandlerCRParseError(t *testing.T) { |
| 148 | + fg := buildDummyFeatureGate() |
| 149 | + _, err := NewFeatureGatesCRHandlerImpl(&fg, "v2") |
| 150 | + assert.ErrorContains(t, err, "unable to determine features") |
| 151 | +} |
| 152 | + |
| 153 | +func buildDummyFeatureGate() configv1.FeatureGate { |
| 154 | + fg := configv1.FeatureGate{ |
| 155 | + Status: configv1.FeatureGateStatus{ |
| 156 | + FeatureGates: []configv1.FeatureGateDetails{ |
| 157 | + { |
| 158 | + Version: "v1", |
| 159 | + Enabled: []configv1.FeatureGateAttributes{ |
| 160 | + {Name: FeatureGatesTestExistingEnaFeatureGate1}, |
| 161 | + {Name: FeatureGatesTestExistingEnaFeatureGate2}, |
| 162 | + }, |
| 163 | + Disabled: []configv1.FeatureGateAttributes{ |
| 164 | + {Name: FeatureGatesTestExistingDisFeatureGate1}, |
| 165 | + }, |
| 166 | + }, |
| 167 | + }, |
| 168 | + }, |
| 169 | + } |
| 170 | + return fg |
| 171 | +} |
| 172 | + |
| 173 | +func checkFeatureGates(t *testing.T, handler *FeatureGatesHandlerImpl) { |
| 174 | + // Check that all the known FGs are reported as known |
| 175 | + assert.True(t, handler.Exists(FeatureGatesTestExistingEnaFeatureGate1)) |
| 176 | + assert.True(t, handler.Exists(FeatureGatesTestExistingEnaFeatureGate2)) |
| 177 | + assert.True(t, handler.Exists(FeatureGatesTestExistingDisFeatureGate1)) |
| 178 | + |
| 179 | + // Check that all the known FGs are reported as enabled/disabled |
| 180 | + assert.True(t, handler.Enabled(FeatureGatesTestExistingEnaFeatureGate1)) |
| 181 | + assert.True(t, handler.Enabled(FeatureGatesTestExistingEnaFeatureGate2)) |
| 182 | + assert.False(t, handler.Enabled(FeatureGatesTestExistingDisFeatureGate1)) |
| 183 | +} |
0 commit comments