-
Notifications
You must be signed in to change notification settings - Fork 2k
/
Copy pathFeatureFlagTests.java
147 lines (127 loc) · 5.87 KB
/
FeatureFlagTests.java
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
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/
package org.opensearch.common.util;
import org.opensearch.common.SuppressForbidden;
import org.opensearch.common.settings.Setting;
import org.opensearch.common.settings.Settings;
import org.opensearch.test.OpenSearchTestCase;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Rule;
import org.junit.rules.TestRule;
import org.junit.runners.model.Statement;
import java.io.IOException;
import static org.opensearch.common.util.FeatureFlags.FEATURE_FLAG_PREFIX;
/**
* It is easy for the state of feature flags to persist across tests running on multiple threads as:
* - Feature flags are globally available
* - Feature flags may be loaded from JVM system properties
* Here we test underlying implementation (FeatureFlags.FeatureFlagsImpl) to avoid polluting global scope.
* Other tests should use the set()/reset() helpers provided in FeatureFlags.TestUtils.
*/
public class FeatureFlagTests extends OpenSearchTestCase {
private static final String TEST_FEATURE = FEATURE_FLAG_PREFIX + "testfeat.enabled";
private static final Setting<Boolean> TEST_FEATURE_FLAG = Setting.boolSetting(TEST_FEATURE, false, Setting.Property.NodeScope);
private static final FeatureFlags.FeatureFlagsImpl featureFlagsImpl = new FeatureFlags.FeatureFlagsImpl();
// Make featureFlagsImpl static to mock global feature flags as used by other test cases.
// Synchronize on featureFlagsImpl to avoid race conditions when tests run in parallel.
@Rule
public TestRule synchronizationRule = (base, description) -> new Statement() {
@Override
public void evaluate() throws Throwable {
synchronized (featureFlagsImpl) {
base.evaluate();
}
}
};
@BeforeClass
public static void setupClass() {
FeatureFlags.TestUtils.addFlag(TEST_FEATURE_FLAG);
}
@AfterClass
public static void teardownClass() {
FeatureFlags.TestUtils.removeFlag(TEST_FEATURE_FLAG);
}
@SuppressForbidden(reason = "Unit test on isolated feature flag")
@After
public void teardown() throws IOException {
synchronized (TEST_FEATURE) {
System.clearProperty(TEST_FEATURE);
featureFlagsImpl.reset();
}
}
public void testFeatureFlagsNotInitialized() {
assertFalse(featureFlagsImpl.isEnabled(TEST_FEATURE));
}
public void testFeatureFlagsFromDefault() {
featureFlagsImpl.initializeFeatureFlags();
assertFalse(featureFlagsImpl.isEnabled(TEST_FEATURE));
}
public void testFeatureFlagFromEmpty() {
featureFlagsImpl.initializeFeatureFlags(Settings.EMPTY);
assertFalse(featureFlagsImpl.isEnabled(TEST_FEATURE));
}
public void testFeatureFlagFromSettings() {
// init from settings
featureFlagsImpl.initializeFeatureFlags(Settings.builder().put(TEST_FEATURE, true).build());
assertTrue(featureFlagsImpl.isEnabled(TEST_FEATURE));
// overwrite with new settings
featureFlagsImpl.initializeFeatureFlags(Settings.builder().put(TEST_FEATURE, false).build());
assertFalse(featureFlagsImpl.isEnabled(TEST_FEATURE));
}
public void testNonBooleanFeatureFlag() {
String javaVersionProperty = "java.version";
assertNotNull(System.getProperty(javaVersionProperty));
assertFalse(featureFlagsImpl.isEnabled(javaVersionProperty));
}
public void testFeatureFlagsEmptySettings() {
featureFlagsImpl.initializeFeatureFlags(Settings.EMPTY);
assertFalse(featureFlagsImpl.isEnabled(TEST_FEATURE));
}
public void testFeatureFlagsTestUtilsSetSingleFlag() {
featureFlagsImpl.set(TEST_FEATURE, false);
assertFalse(featureFlagsImpl.isEnabled(TEST_FEATURE));
}
@SuppressForbidden(reason = "Unit test on isolated feature flag")
public void testFeatureFlagFromSystemProperty() {
System.setProperty(TEST_FEATURE, "true");
featureFlagsImpl.initializeFeatureFlags();
assertTrue(featureFlagsImpl.isEnabled(TEST_FEATURE));
}
@SuppressForbidden(reason = "Unit test on isolated feature flag")
public void testFeatureFlagSettingOverwritesSystemProperties() {
System.setProperty(TEST_FEATURE, "true");
featureFlagsImpl.initializeFeatureFlags(Settings.builder().put(TEST_FEATURE, false).build());
assertFalse(featureFlagsImpl.isEnabled(TEST_FEATURE));
System.setProperty(TEST_FEATURE, "true");
featureFlagsImpl.initializeFeatureFlags();
assertTrue(featureFlagsImpl.isEnabled(TEST_FEATURE));
featureFlagsImpl.initializeFeatureFlags(Settings.builder().put(TEST_FEATURE, false).build());
assertFalse(featureFlagsImpl.isEnabled(TEST_FEATURE));
}
@SuppressForbidden(reason = "Unit test on isolated feature flag")
public void testFeatureDoesNotExist() {
final String DNE_FF = FEATURE_FLAG_PREFIX + "doesntexist";
assertFalse(featureFlagsImpl.isEnabled(DNE_FF));
System.setProperty(DNE_FF, "true");
featureFlagsImpl.initializeFeatureFlags();
assertFalse(featureFlagsImpl.isEnabled(DNE_FF));
featureFlagsImpl.initializeFeatureFlags(Settings.builder().put(DNE_FF, true).build());
assertFalse(featureFlagsImpl.isEnabled(DNE_FF));
}
@SuppressForbidden(reason = "Unit test on isolated feature flag")
public void testFeatureFlagsSysPropTestUtilsReset() {
assertFalse(featureFlagsImpl.isEnabled(TEST_FEATURE));
System.setProperty(TEST_FEATURE, "true");
featureFlagsImpl.initializeFeatureFlags();
assertTrue(featureFlagsImpl.isEnabled(TEST_FEATURE));
featureFlagsImpl.reset();
assertFalse(featureFlagsImpl.isEnabled(TEST_FEATURE));
}
}