| 1 |
// Copyright 2026 The NATS Authors |
| 2 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 |
// you may not use this file except in compliance with the License. |
| 4 |
// You may obtain a copy of the License at |
| 5 |
// |
| 6 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 |
// |
| 8 |
// Unless required by applicable law or agreed to in writing, software |
| 9 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 |
// See the License for the specific language governing permissions and |
| 12 |
// limitations under the License. |
| 13 |
|
| 14 |
package server |
| 15 |
|
| 16 |
import ( |
| 17 |
"reflect" |
| 18 |
"testing" |
| 19 |
) |
| 20 |
|
| 21 |
func TestGetFeatureFlag(t *testing.T) { |
| 22 |
featureFlags["test_flag"] = true |
| 23 |
t.Cleanup(func() { delete(featureFlags, "test_flag") }) |
| 24 |
|
| 25 |
// Unset feature flags gets defaults. |
| 26 |
o := &Options{} |
| 27 |
require_Equal(t, o.getFeatureFlag(""), false) |
| 28 |
require_Equal(t, o.getFeatureFlag("test_flag"), true) |
| 29 |
|
| 30 |
// User disables the feature flag, it takes precedence. |
| 31 |
o.FeatureFlags = map[string]bool{"test_flag": false} |
| 32 |
require_Equal(t, o.getFeatureFlag("test_flag"), false) |
| 33 |
|
| 34 |
// User enables the feature flag, but we don't support it anymore. |
| 35 |
// It's allowed for the user to provide stale flags, but we shouldn't return they're enabled. |
| 36 |
delete(featureFlags, "test_flag") |
| 37 |
o.FeatureFlags = map[string]bool{"test_flag": true} |
| 38 |
require_Equal(t, o.getFeatureFlag("test_flag"), false) |
| 39 |
} |
| 40 |
|
| 41 |
func TestFeatureFlagsConfigParseNonBool(t *testing.T) { |
| 42 |
conf := createConfFile(t, []byte(` |
| 43 |
feature_flags { |
| 44 |
opt_in_flag: "true" |
| 45 |
} |
| 46 |
`)) |
| 47 |
|
| 48 |
_, err := ProcessConfigFile(conf) |
| 49 |
require_Error(t, err) |
| 50 |
require_Contains(t, err.Error(), `error parsing feature flag "opt_in_flag": expected bool, got string`) |
| 51 |
} |
| 52 |
|
| 53 |
func TestGetMergedFeatureFlags(t *testing.T) { |
| 54 |
previous := featureFlags |
| 55 |
featureFlags = make(map[string]bool) |
| 56 |
featureFlags["test_flag"] = true |
| 57 |
featureFlags["test_flag_default"] = true |
| 58 |
t.Cleanup(func() { featureFlags = previous }) |
| 59 |
|
| 60 |
o := &Options{} |
| 61 |
o.FeatureFlags = map[string]bool{"test_flag": false, "unknown_flag": false} |
| 62 |
|
| 63 |
// The merged flags should only contain known flags, with the user's values taking precedence. |
| 64 |
merged := o.getMergedFeatureFlags() |
| 65 |
expected := map[string]bool{"test_flag": false, "test_flag_default": true} |
| 66 |
if !reflect.DeepEqual(expected, merged) { |
| 67 |
t.Fatalf("Feature flags are incorrect.\nexpected: %+v\ngot: %+v", expected, merged) |
| 68 |
} |
| 69 |
} |