github.com/safing/portbase@v0.19.5/config/set_test.go (about) 1 //nolint:goconst 2 package config 3 4 import "testing" 5 6 func TestLayersGetters(t *testing.T) { //nolint:paralleltest 7 // reset 8 options = make(map[string]*Option) 9 10 mapData, err := JSONToMap([]byte(` 11 { 12 "monkey": "1", 13 "elephant": 2, 14 "zebras": { 15 "zebra": ["black", "white"], 16 "weird_zebra": ["black", -1] 17 }, 18 "env": { 19 "hot": true 20 } 21 } 22 `)) 23 if err != nil { 24 t.Fatal(err) 25 } 26 27 validationErrors, _ := ReplaceConfig(mapData) 28 if len(validationErrors) > 0 { 29 t.Fatalf("%d errors, first: %s", len(validationErrors), validationErrors[0].Error()) 30 } 31 32 // Test missing values 33 34 missingString := GetAsString("missing", "fallback") 35 if missingString() != "fallback" { 36 t.Error("expected fallback value: fallback") 37 } 38 39 missingStringArray := GetAsStringArray("missing", []string{"fallback"}) 40 if len(missingStringArray()) != 1 || missingStringArray()[0] != "fallback" { 41 t.Error("expected fallback value: [fallback]") 42 } 43 44 missingInt := GetAsInt("missing", -1) 45 if missingInt() != -1 { 46 t.Error("expected fallback value: -1") 47 } 48 49 missingBool := GetAsBool("missing", false) 50 if missingBool() { 51 t.Error("expected fallback value: false") 52 } 53 54 // Test value mismatch 55 56 notString := GetAsString("elephant", "fallback") 57 if notString() != "fallback" { 58 t.Error("expected fallback value: fallback") 59 } 60 61 notStringArray := GetAsStringArray("elephant", []string{"fallback"}) 62 if len(notStringArray()) != 1 || notStringArray()[0] != "fallback" { 63 t.Error("expected fallback value: [fallback]") 64 } 65 66 mixedStringArray := GetAsStringArray("zebras/weird_zebra", []string{"fallback"}) 67 if len(mixedStringArray()) != 1 || mixedStringArray()[0] != "fallback" { 68 t.Error("expected fallback value: [fallback]") 69 } 70 71 notInt := GetAsInt("monkey", -1) 72 if notInt() != -1 { 73 t.Error("expected fallback value: -1") 74 } 75 76 notBool := GetAsBool("monkey", false) 77 if notBool() { 78 t.Error("expected fallback value: false") 79 } 80 } 81 82 func TestLayersSetters(t *testing.T) { //nolint:paralleltest 83 // reset 84 options = make(map[string]*Option) 85 86 _ = Register(&Option{ 87 Name: "name", 88 Key: "monkey", 89 Description: "description", 90 ReleaseLevel: ReleaseLevelStable, 91 ExpertiseLevel: ExpertiseLevelUser, 92 OptType: OptTypeString, 93 DefaultValue: "banana", 94 ValidationRegex: "^(banana|water)$", 95 }) 96 _ = Register(&Option{ 97 Name: "name", 98 Key: "zebras/zebra", 99 Description: "description", 100 ReleaseLevel: ReleaseLevelStable, 101 ExpertiseLevel: ExpertiseLevelUser, 102 OptType: OptTypeStringArray, 103 DefaultValue: []string{"black", "white"}, 104 ValidationRegex: "^[a-z]+$", 105 }) 106 _ = Register(&Option{ 107 Name: "name", 108 Key: "elephant", 109 Description: "description", 110 ReleaseLevel: ReleaseLevelStable, 111 ExpertiseLevel: ExpertiseLevelUser, 112 OptType: OptTypeInt, 113 DefaultValue: 2, 114 ValidationRegex: "", 115 }) 116 _ = Register(&Option{ 117 Name: "name", 118 Key: "hot", 119 Description: "description", 120 ReleaseLevel: ReleaseLevelStable, 121 ExpertiseLevel: ExpertiseLevelUser, 122 OptType: OptTypeBool, 123 DefaultValue: true, 124 ValidationRegex: "", 125 }) 126 127 // correct types 128 if err := SetConfigOption("monkey", "banana"); err != nil { 129 t.Error(err) 130 } 131 if err := SetConfigOption("zebras/zebra", []string{"black", "white"}); err != nil { 132 t.Error(err) 133 } 134 if err := SetDefaultConfigOption("elephant", 2); err != nil { 135 t.Error(err) 136 } 137 if err := SetDefaultConfigOption("hot", true); err != nil { 138 t.Error(err) 139 } 140 141 // incorrect types 142 if err := SetConfigOption("monkey", []string{"black", "white"}); err == nil { 143 t.Error("should fail") 144 } 145 if err := SetConfigOption("zebras/zebra", 2); err == nil { 146 t.Error("should fail") 147 } 148 if err := SetDefaultConfigOption("elephant", true); err == nil { 149 t.Error("should fail") 150 } 151 if err := SetDefaultConfigOption("hot", "banana"); err == nil { 152 t.Error("should fail") 153 } 154 if err := SetDefaultConfigOption("hot", []byte{0}); err == nil { 155 t.Error("should fail") 156 } 157 158 // validation fail 159 if err := SetConfigOption("monkey", "dirt"); err == nil { 160 t.Error("should fail") 161 } 162 if err := SetConfigOption("zebras/zebra", []string{"Element649"}); err == nil { 163 t.Error("should fail") 164 } 165 166 // unregistered checking 167 if err := SetConfigOption("invalid", "banana"); err == nil { 168 t.Error("should fail") 169 } 170 if err := SetConfigOption("invalid", []string{"black", "white"}); err == nil { 171 t.Error("should fail") 172 } 173 if err := SetConfigOption("invalid", 2); err == nil { 174 t.Error("should fail") 175 } 176 if err := SetConfigOption("invalid", true); err == nil { 177 t.Error("should fail") 178 } 179 if err := SetConfigOption("invalid", []byte{0}); err == nil { 180 t.Error("should fail") 181 } 182 183 // delete 184 if err := SetConfigOption("monkey", nil); err != nil { 185 t.Error(err) 186 } 187 if err := SetDefaultConfigOption("elephant", nil); err != nil { 188 t.Error(err) 189 } 190 if err := SetDefaultConfigOption("invalid_delete", nil); err == nil { 191 t.Error("should fail") 192 } 193 }