github.com/nuvolaris/nuv@v0.0.0-20240511174247-a74e3a52bfd8/config/config_map_builder_test.go (about) 1 // Licensed to the Apache Software Foundation (ASF) under one 2 // or more contributor license agreements. See the NOTICE file 3 // distributed with this work for additional information 4 // regarding copyright ownership. The ASF licenses this file 5 // to you under the Apache License, Version 2.0 (the 6 // "License"); you may not use this file except in compliance 7 // with the License. You may obtain a copy of the License at 8 // 9 // http://www.apache.org/licenses/LICENSE-2.0 10 // 11 // Unless required by applicable law or agreed to in writing, 12 // software distributed under the License is distributed on an 13 // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 // KIND, either express or implied. See the License for the 15 // specific language governing permissions and limitations 16 // under the License. 17 18 package config 19 20 import ( 21 "os" 22 "path/filepath" 23 "testing" 24 25 "github.com/stretchr/testify/require" 26 ) 27 28 func TestConfigMapBuilder(t *testing.T) { 29 t.Run("build without plugins", func(t *testing.T) { 30 tmpDir := t.TempDir() 31 32 configJsonPath := createFakeConfigFile(t, "config.json", tmpDir, ` 33 { 34 "key": "value", 35 "nested": { 36 "key": 123 37 } 38 }`) 39 nuvRootPath := createFakeConfigFile(t, "nuvroot.json", tmpDir, ` 40 { 41 "version": "0.3.0", 42 "config": { 43 "nuvroot": "value", 44 "another": { 45 "key": 123 46 } 47 } 48 }`) 49 50 testCases := []struct { 51 name string 52 configJson string 53 nuvRoot string 54 want ConfigMap 55 err error 56 }{ 57 { 58 name: "should return empty configmap when no files are added", 59 configJson: "", 60 nuvRoot: "", 61 want: ConfigMap{ 62 config: map[string]interface{}{}, 63 nuvRootConfig: map[string]interface{}{}, 64 pluginNuvRootConfigs: map[string]map[string]interface{}{}, 65 }, 66 err: nil, 67 }, 68 { 69 name: "should return with config when a valid config.json is added", 70 configJson: configJsonPath, 71 nuvRoot: "", 72 want: ConfigMap{ 73 nuvRootConfig: map[string]interface{}{}, 74 config: map[string]interface{}{ 75 "key": "value", 76 "nested": map[string]interface{}{ 77 "key": 123.0, 78 }, 79 }, 80 configPath: configJsonPath, 81 pluginNuvRootConfigs: map[string]map[string]interface{}{}, 82 }, 83 }, 84 { 85 name: "should return with nuvroot when a valid nuvroot.json is added", 86 configJson: "", 87 nuvRoot: nuvRootPath, 88 want: ConfigMap{ 89 nuvRootConfig: map[string]interface{}{ 90 "nuvroot": "value", 91 "another": map[string]interface{}{ 92 "key": 123.0, 93 }, 94 }, 95 config: map[string]interface{}{}, 96 pluginNuvRootConfigs: map[string]map[string]interface{}{}, 97 }, 98 }, 99 { 100 name: "should return with both when both config.json and nuvroot.json are added", 101 configJson: configJsonPath, 102 nuvRoot: nuvRootPath, 103 want: ConfigMap{ 104 config: map[string]interface{}{ 105 "key": "value", 106 "nested": map[string]interface{}{ 107 "key": 123.0, 108 }, 109 }, 110 nuvRootConfig: map[string]interface{}{ 111 "nuvroot": "value", 112 "another": map[string]interface{}{ 113 "key": 123.0, 114 }, 115 }, 116 configPath: configJsonPath, 117 pluginNuvRootConfigs: map[string]map[string]interface{}{}, 118 }, 119 }, 120 } 121 122 for _, tc := range testCases { 123 t.Run(tc.name, func(t *testing.T) { 124 125 got, err := NewConfigMapBuilder(). 126 WithConfigJson(tc.configJson). 127 WithNuvRoot(tc.nuvRoot). 128 Build() 129 130 // if we expect an error but got none 131 if tc.err != nil && err == nil { 132 t.Errorf("want error %e, got %e", tc.err, err) 133 } 134 135 // if we expect no error but got one 136 if tc.err == nil && err != nil { 137 t.Errorf("want no error, but got %e", err) 138 } 139 140 require.Equal(t, tc.want, got) 141 }) 142 } 143 }) 144 145 t.Run("build with plugins", func(t *testing.T) { 146 tmpDir := t.TempDir() 147 148 configJsonPath := createFakeConfigFile(t, "config.json", tmpDir, ` 149 { 150 "key": "value", 151 "nested": { 152 "key": 123 153 } 154 }`) 155 nuvRootPath := createFakeConfigFile(t, "nuvroot.json", tmpDir, ` 156 { 157 "version": "0.3.0", 158 "config": { 159 "nuvroot": "value", 160 "another": { 161 "key": 123 162 } 163 } 164 }`) 165 166 pluginNuvRoot := createFakeConfigFile(t, "nuvroot.json", tmpDir, ` 167 { 168 "version": "0.3.0", 169 "config": { 170 "nuvroot": "value", 171 "another": { 172 "key": 123 173 } 174 } 175 }`) 176 177 testCases := []struct { 178 name string 179 configJson string 180 nuvRoot string 181 pluginNuvRoots map[string]string 182 want ConfigMap 183 err error 184 }{ 185 { 186 name: "should return configmap containing plugin nuvroot", 187 configJson: configJsonPath, 188 nuvRoot: nuvRootPath, 189 pluginNuvRoots: map[string]string{ 190 "plugin": pluginNuvRoot, 191 }, 192 193 want: ConfigMap{ 194 config: map[string]interface{}{ 195 "key": "value", 196 "nested": map[string]interface{}{ 197 "key": 123.0, 198 }, 199 }, 200 nuvRootConfig: map[string]interface{}{ 201 "nuvroot": "value", 202 "another": map[string]interface{}{ 203 "key": 123.0, 204 }, 205 }, 206 configPath: configJsonPath, 207 pluginNuvRootConfigs: map[string]map[string]interface{}{ 208 "plugin": { 209 "nuvroot": "value", 210 "another": map[string]interface{}{ 211 "key": 123.0, 212 }, 213 }, 214 }, 215 }, 216 217 err: nil, 218 }, 219 } 220 221 for _, tc := range testCases { 222 t.Run(tc.name, func(t *testing.T) { 223 224 got, err := NewConfigMapBuilder(). 225 WithConfigJson(tc.configJson). 226 WithNuvRoot(tc.nuvRoot). 227 WithPluginNuvRoots(tc.pluginNuvRoots). 228 Build() 229 230 // if we expect an error but got none 231 if tc.err != nil && err == nil { 232 t.Errorf("want error %e, got %e", tc.err, err) 233 } 234 235 // if we expect no error but got one 236 if tc.err == nil && err != nil { 237 t.Errorf("want no error, but got %e", err) 238 } 239 240 require.Equal(t, tc.want, got) 241 }) 242 } 243 }) 244 } 245 246 func createFakeConfigFile(t *testing.T, name, dir, content string) string { 247 t.Helper() 248 path := filepath.Join(dir, name) 249 err := os.WriteFile(path, []byte(content), 0644) 250 require.NoError(t, err) 251 return path 252 }