github.com/minio/madmin-go@v1.7.5/parse-config_test.go (about) 1 // 2 // MinIO Object Storage (c) 2022 MinIO, Inc. 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 madmin 18 19 import ( 20 "reflect" 21 "testing" 22 ) 23 24 func TestParseServerConfigOutput(t *testing.T) { 25 tests := []struct { 26 Name string 27 Config string 28 Expected []SubsysConfig 29 ExpectedErr error 30 }{ 31 { 32 Name: "single target config data only", 33 Config: "subnet license= api_key= proxy=", 34 Expected: []SubsysConfig{ 35 { 36 SubSystem: SubnetSubSys, 37 Target: "", 38 KV: []ConfigKV{ 39 { 40 Key: "license", 41 Value: "", 42 EnvOverride: nil, 43 }, 44 { 45 Key: "api_key", 46 Value: "", 47 EnvOverride: nil, 48 }, 49 { 50 Key: "proxy", 51 Value: "", 52 EnvOverride: nil, 53 }, 54 }, 55 kvIndexMap: map[string]int{ 56 "license": 0, 57 "api_key": 1, 58 "proxy": 2, 59 }, 60 }, 61 }, 62 }, 63 { 64 Name: "single target config + env", 65 Config: `# MINIO_SUBNET_API_KEY=xxx 66 # MINIO_SUBNET_LICENSE=2 67 subnet license=1 api_key= proxy=`, 68 Expected: []SubsysConfig{ 69 { 70 SubSystem: SubnetSubSys, 71 Target: "", 72 KV: []ConfigKV{ 73 { 74 Key: "api_key", 75 Value: "", 76 EnvOverride: &EnvOverride{ 77 Name: "MINIO_SUBNET_API_KEY", 78 Value: "xxx", 79 }, 80 }, 81 { 82 Key: "license", 83 Value: "1", 84 EnvOverride: &EnvOverride{ 85 Name: "MINIO_SUBNET_LICENSE", 86 Value: "2", 87 }, 88 }, 89 { 90 Key: "proxy", 91 Value: "", 92 EnvOverride: nil, 93 }, 94 }, 95 kvIndexMap: map[string]int{ 96 "license": 1, 97 "api_key": 0, 98 "proxy": 2, 99 }, 100 }, 101 }, 102 }, 103 { 104 Name: "multiple targets no env", 105 Config: `logger_webhook enable=off endpoint= auth_token= client_cert= client_key= queue_size=100000 106 logger_webhook:1 endpoint=http://localhost:8080/ auth_token= client_cert= client_key= queue_size=100000 107 `, 108 Expected: []SubsysConfig{ 109 { 110 SubSystem: LoggerWebhookSubSys, 111 Target: "", 112 KV: []ConfigKV{ 113 { 114 Key: "enable", 115 Value: "off", 116 }, 117 { 118 Key: "endpoint", 119 Value: "", 120 }, 121 { 122 Key: "auth_token", 123 Value: "", 124 }, 125 { 126 Key: "client_cert", 127 Value: "", 128 }, 129 { 130 Key: "client_key", 131 Value: "", 132 }, 133 { 134 Key: "queue_size", 135 Value: "100000", 136 }, 137 }, 138 kvIndexMap: map[string]int{ 139 "enable": 0, 140 "endpoint": 1, 141 "auth_token": 2, 142 "client_cert": 3, 143 "client_key": 4, 144 "queue_size": 5, 145 }, 146 }, 147 { 148 SubSystem: LoggerWebhookSubSys, 149 Target: "1", 150 KV: []ConfigKV{ 151 { 152 Key: "endpoint", 153 Value: "http://localhost:8080/", 154 }, 155 { 156 Key: "auth_token", 157 Value: "", 158 }, 159 { 160 Key: "client_cert", 161 Value: "", 162 }, 163 { 164 Key: "client_key", 165 Value: "", 166 }, 167 { 168 Key: "queue_size", 169 Value: "100000", 170 }, 171 }, 172 kvIndexMap: map[string]int{ 173 "endpoint": 0, 174 "auth_token": 1, 175 "client_cert": 2, 176 "client_key": 3, 177 "queue_size": 4, 178 }, 179 }, 180 }, 181 }, 182 } 183 184 for i, test := range tests { 185 r, err := ParseServerConfigOutput(test.Config) 186 if err != nil { 187 if err.Error() != test.ExpectedErr.Error() { 188 t.Errorf("Test %d (%s) got unexpected error: %v", i, test.Name, err) 189 } 190 // got an expected error. 191 continue 192 } 193 if !reflect.DeepEqual(test.Expected, r) { 194 t.Errorf("Test %d (%s) expected:\n%#v\nbut got:\n%#v\n", i, test.Name, test.Expected, r) 195 } 196 } 197 }