|
| 1 | +/** |
| 2 | +# Copyright 2024 NVIDIA CORPORATION |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | +**/ |
| 16 | + |
| 17 | +package config |
| 18 | + |
| 19 | +import ( |
| 20 | + "testing" |
| 21 | + |
| 22 | + "github.com/stretchr/testify/require" |
| 23 | +) |
| 24 | + |
| 25 | +func TestFeatures(t *testing.T) { |
| 26 | + testCases := []struct { |
| 27 | + description string |
| 28 | + features features |
| 29 | + expected map[featureName]bool |
| 30 | + envs []getenver |
| 31 | + }{ |
| 32 | + { |
| 33 | + description: "empty features", |
| 34 | + expected: map[featureName]bool{ |
| 35 | + FeatureAllowAdditionalGIDs: false, |
| 36 | + FeatureGDRCopy: false, |
| 37 | + FeatureGDS: false, |
| 38 | + FeatureMOFED: false, |
| 39 | + FeatureNVSWITCH: false, |
| 40 | + }, |
| 41 | + }, |
| 42 | + { |
| 43 | + description: "envvar sets features if enabled", |
| 44 | + expected: map[featureName]bool{ |
| 45 | + FeatureAllowAdditionalGIDs: true, |
| 46 | + FeatureGDRCopy: false, |
| 47 | + FeatureGDS: false, |
| 48 | + FeatureMOFED: false, |
| 49 | + FeatureNVSWITCH: false, |
| 50 | + }, |
| 51 | + envs: []getenver{ |
| 52 | + mockEnver{ |
| 53 | + "NVIDIA_ALLOW_ADDITIONAL_GIDS": "enabled", |
| 54 | + }, |
| 55 | + }, |
| 56 | + }, |
| 57 | + { |
| 58 | + description: "envvar sets features if true", |
| 59 | + expected: map[featureName]bool{ |
| 60 | + FeatureAllowAdditionalGIDs: true, |
| 61 | + FeatureGDRCopy: false, |
| 62 | + FeatureGDS: false, |
| 63 | + FeatureMOFED: false, |
| 64 | + FeatureNVSWITCH: false, |
| 65 | + }, |
| 66 | + envs: []getenver{ |
| 67 | + mockEnver{ |
| 68 | + "NVIDIA_ALLOW_ADDITIONAL_GIDS": "true", |
| 69 | + }, |
| 70 | + }, |
| 71 | + }, |
| 72 | + { |
| 73 | + description: "feature sets feature", |
| 74 | + features: features{ |
| 75 | + AllowAdditionalGIDs: ptr(featureEnabled), |
| 76 | + }, |
| 77 | + }, |
| 78 | + } |
| 79 | + |
| 80 | + for _, tc := range testCases { |
| 81 | + t.Run(tc.description, func(t *testing.T) { |
| 82 | + for n, v := range tc.expected { |
| 83 | + t.Run(tc.description+"-"+string(n), func(t *testing.T) { |
| 84 | + require.EqualValues(t, v, tc.features.IsEnabled(n, tc.envs...)) |
| 85 | + }) |
| 86 | + } |
| 87 | + |
| 88 | + }) |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +func TestFeature(t *testing.T) { |
| 93 | + testCases := []struct { |
| 94 | + description string |
| 95 | + feature *feature |
| 96 | + envvar string |
| 97 | + envs []getenver |
| 98 | + expected bool |
| 99 | + }{ |
| 100 | + { |
| 101 | + description: "nil feature is false", |
| 102 | + feature: nil, |
| 103 | + expected: false, |
| 104 | + }, |
| 105 | + { |
| 106 | + description: "feature enables", |
| 107 | + feature: ptr(featureEnabled), |
| 108 | + expected: true, |
| 109 | + }, |
| 110 | + { |
| 111 | + description: "feature disabled", |
| 112 | + feature: ptr(featureDisabled), |
| 113 | + expected: false, |
| 114 | + }, |
| 115 | + { |
| 116 | + description: "envvar overrides feature disabled", |
| 117 | + feature: ptr(featureDisabled), |
| 118 | + envvar: "FEATURE", |
| 119 | + envs: []getenver{ |
| 120 | + mockEnver{"FEATURE": "enabled"}, |
| 121 | + }, |
| 122 | + expected: true, |
| 123 | + }, |
| 124 | + { |
| 125 | + description: "envvar overrides feature enabled", |
| 126 | + feature: ptr(featureEnabled), |
| 127 | + envvar: "FEATURE", |
| 128 | + envs: []getenver{ |
| 129 | + mockEnver{"FEATURE": "disabled"}, |
| 130 | + }, |
| 131 | + expected: false, |
| 132 | + }, |
| 133 | + } |
| 134 | + |
| 135 | + for _, tc := range testCases { |
| 136 | + t.Run(tc.description, func(t *testing.T) { |
| 137 | + require.EqualValues(t, tc.expected, tc.feature.isEnabled(tc.envvar, tc.envs...)) |
| 138 | + }) |
| 139 | + } |
| 140 | +} |
| 141 | + |
| 142 | +type mockEnver map[string]string |
| 143 | + |
| 144 | +func (m mockEnver) Getenv(k string) string { |
| 145 | + return m[k] |
| 146 | +} |
| 147 | + |
| 148 | +func ptr[T any](x T) *T { |
| 149 | + return &x |
| 150 | +} |
0 commit comments