summaryrefslogtreecommitdiff
path: root/src/mongo/idl/feature_flag_test.cpp
blob: 83e55e764748d1742be004ec2567b5e8f72f7114 (plain)
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/**
 *    Copyright (C) 2020-present MongoDB, Inc.
 *
 *    This program is free software: you can redistribute it and/or modify
 *    it under the terms of the Server Side Public License, version 1,
 *    as published by MongoDB, Inc.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    Server Side Public License for more details.
 *
 *    You should have received a copy of the Server Side Public License
 *    along with this program. If not, see
 *    <http://www.mongodb.com/licensing/server-side-public-license>.
 *
 *    As a special exception, the copyright holders give permission to link the
 *    code of portions of this program with the OpenSSL library under certain
 *    conditions as described in each individual source file and distribute
 *    linked combinations including the program with the OpenSSL library. You
 *    must comply with the Server Side Public License in all respects for
 *    all of the code used other than as permitted herein. If you modify file(s)
 *    with this exception, you may extend this exception to your version of the
 *    file(s), but you are not obligated to do so. If you do not wish to do so,
 *    delete this exception statement from your version. If you delete this
 *    exception statement from all source files in the program, then also delete
 *    it in the license file.
 */

#include "mongo/bson/bsonobjbuilder.h"
#include "mongo/platform/basic.h"

#include "mongo/idl/feature_flag_test_gen.h"
#include "mongo/idl/server_parameter_test_util.h"
#include "mongo/unittest/bson_test_util.h"
#include "mongo/unittest/unittest.h"

namespace mongo {

namespace {

ServerParameter* getServerParameter(const std::string& name) {
    const auto& spMap = ServerParameterSet::getGlobal()->getMap();
    const auto& spIt = spMap.find(name);
    ASSERT(spIt != spMap.end());

    auto* sp = spIt->second;
    ASSERT(sp);
    return sp;
}

class FeatureFlagTest : public unittest::Test {

protected:
    void setUp() override;

protected:
    ServerParameter* _featureFlagBlender{nullptr};
    ServerParameter* _featureFlagSpoon{nullptr};
};


void FeatureFlagTest::setUp() {
    // Set common flags which test the version string to true
    _featureFlagBlender = getServerParameter("featureFlagBlender");
    ASSERT_OK(_featureFlagBlender->setFromString("true"));

    _featureFlagSpoon = getServerParameter("featureFlagSpoon");
    ASSERT_OK(_featureFlagSpoon->setFromString("true"));

    ASSERT(feature_flags::gFeatureFlagBlender.isEnabledAndIgnoreFCV() == true);
    ASSERT(feature_flags::gFeatureFlagSpoon.isEnabledAndIgnoreFCV() == true);

    Test::setUp();
}

// Sanity check feature flags
TEST(IDLFeatureFlag, Basic) {
    // false is set by "default" attribute in the IDL file.
    ASSERT(feature_flags::gFeatureFlagToaster.isEnabledAndIgnoreFCV() == false);

    auto* featureFlagToaster = getServerParameter("featureFlagToaster");
    ASSERT_OK(featureFlagToaster->setFromString("true"));
    ASSERT(feature_flags::gFeatureFlagToaster.isEnabledAndIgnoreFCV() == true);
    ASSERT_NOT_OK(featureFlagToaster->setFromString("alpha"));

    ASSERT(feature_flags::gFeatureFlagToaster.getVersion() ==
           ServerGlobalParams::FeatureCompatibility::Version::kVersion50);
}

// Verify getVersion works correctly when enabled and not enabled
TEST_F(FeatureFlagTest, Version) {
    ASSERT(feature_flags::gFeatureFlagBlender.getVersion() ==
           ServerGlobalParams::FeatureCompatibility::Version::kVersion49);

    // NOTE: if you are hitting this assertion, the version in feature_flag_test.idl may need to be
    // changed to match the current kLastLTS
    // (Generic FCV reference): feature flag test
    ASSERT(feature_flags::gFeatureFlagSpoon.getVersion() ==
           ServerGlobalParams::FeatureCompatibility::kLastLTS);

    ASSERT_OK(_featureFlagBlender->setFromString("false"));
    ASSERT(feature_flags::gFeatureFlagBlender.isEnabledAndIgnoreFCV() == false);
    ASSERT_NOT_OK(_featureFlagBlender->setFromString("alpha"));

    ASSERT_THROWS(feature_flags::gFeatureFlagBlender.getVersion(), AssertionException);
}

// Test feature flag server parameters are serialized correctly
TEST_F(FeatureFlagTest, ServerStatus) {
    {
        ASSERT_OK(_featureFlagBlender->setFromString("true"));
        ASSERT(feature_flags::gFeatureFlagBlender.isEnabledAndIgnoreFCV() == true);

        BSONObjBuilder builder;

        _featureFlagBlender->append(nullptr, builder, "blender");

        ASSERT_BSONOBJ_EQ(builder.obj(),
                          BSON("blender" << BSON("value" << true << "version"
                                                         << "4.9")));
    }

    {
        ASSERT_OK(_featureFlagBlender->setFromString("false"));
        ASSERT(feature_flags::gFeatureFlagBlender.isEnabledAndIgnoreFCV() == false);

        BSONObjBuilder builder;

        _featureFlagBlender->append(nullptr, builder, "blender");

        ASSERT_BSONOBJ_EQ(builder.obj(), BSON("blender" << BSON("value" << false)));
    }
}

// Test feature flags are enabled and not enabled based on fcv
TEST_F(FeatureFlagTest, IsEnabledTrue) {
    // Test FCV checks with enabled flag
    // Test newest version
    serverGlobalParams.mutableFeatureCompatibility.setVersion(
        ServerGlobalParams::FeatureCompatibility::Version::kVersion50);

    ASSERT_TRUE(
        feature_flags::gFeatureFlagBlender.isEnabled(serverGlobalParams.featureCompatibility));
    ASSERT_TRUE(
        feature_flags::gFeatureFlagSpoon.isEnabled(serverGlobalParams.featureCompatibility));

    // Test oldest version
    // (Generic FCV reference): feature flag test
    serverGlobalParams.mutableFeatureCompatibility.setVersion(
        ServerGlobalParams::FeatureCompatibility::kLastLTS);

    ASSERT_FALSE(
        feature_flags::gFeatureFlagBlender.isEnabled(serverGlobalParams.featureCompatibility));
    ASSERT_TRUE(
        feature_flags::gFeatureFlagSpoon.isEnabled(serverGlobalParams.featureCompatibility));
}

// Test feature flags are disabled regardless of fcv
TEST_F(FeatureFlagTest, IsEnabledFalse) {
    // Test FCV checks with disabled flag
    // Test newest version
    ASSERT_OK(_featureFlagBlender->setFromString("false"));
    ASSERT_OK(_featureFlagSpoon->setFromString("false"));

    serverGlobalParams.mutableFeatureCompatibility.setVersion(
        ServerGlobalParams::FeatureCompatibility::Version::kVersion50);

    ASSERT_FALSE(
        feature_flags::gFeatureFlagBlender.isEnabled(serverGlobalParams.featureCompatibility));
    ASSERT_FALSE(
        feature_flags::gFeatureFlagSpoon.isEnabled(serverGlobalParams.featureCompatibility));

    // Test oldest version
    // (Generic FCV reference): feature flag test
    serverGlobalParams.mutableFeatureCompatibility.setVersion(
        ServerGlobalParams::FeatureCompatibility::kLastLTS);

    ASSERT_FALSE(
        feature_flags::gFeatureFlagBlender.isEnabled(serverGlobalParams.featureCompatibility));
    ASSERT_FALSE(
        feature_flags::gFeatureFlagSpoon.isEnabled(serverGlobalParams.featureCompatibility));
}

// Test that the RAIIServerParameterControllerForTest works correctly on a feature flag.
TEST_F(FeatureFlagTest, RAIIFeatureFlagController) {
    // Set false feature flag to true
    ASSERT_OK(_featureFlagBlender->setFromString("false"));
    {
        RAIIServerParameterControllerForTest controller("featureFlagBlender", true);
        ASSERT_TRUE(
            feature_flags::gFeatureFlagBlender.isEnabled(serverGlobalParams.featureCompatibility));
    }
    ASSERT_FALSE(
        feature_flags::gFeatureFlagBlender.isEnabled(serverGlobalParams.featureCompatibility));

    // Set true feature flag to false
    ASSERT_OK(_featureFlagBlender->setFromString("true"));
    {
        RAIIServerParameterControllerForTest controller("featureFlagBlender", false);
        ASSERT_FALSE(
            feature_flags::gFeatureFlagBlender.isEnabled(serverGlobalParams.featureCompatibility));
    }
    ASSERT_TRUE(
        feature_flags::gFeatureFlagBlender.isEnabled(serverGlobalParams.featureCompatibility));
}


}  // namespace
}  // namespace mongo