github.com/minio/console@v1.3.0/integration/config_test.go (about) 1 // This file is part of MinIO Console Server 2 // Copyright (c) 2022 MinIO, Inc. 3 // 4 // This program is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU Affero General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // This program is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU Affero General Public License for more details. 13 // 14 // You should have received a copy of the GNU Affero General Public License 15 // along with this program. If not, see <http://www.gnu.org/licenses/>. 16 17 package integration 18 19 import ( 20 "bytes" 21 "encoding/json" 22 "fmt" 23 "log" 24 "net/http" 25 "testing" 26 "time" 27 28 "github.com/stretchr/testify/assert" 29 ) 30 31 func Test_ConfigAPI(t *testing.T) { 32 assert := assert.New(t) 33 34 tests := []struct { 35 name string 36 expectedStatus int 37 expectedError error 38 }{ 39 { 40 name: "Config - Valid", 41 expectedStatus: 200, 42 expectedError: nil, 43 }, 44 } 45 46 client := &http.Client{ 47 Timeout: 3 * time.Second, 48 } 49 50 for _, tt := range tests { 51 t.Run(tt.name, func(_ *testing.T) { 52 request, err := http.NewRequest("GET", "http://localhost:9090/api/v1/configs", nil) 53 if err != nil { 54 log.Println(err) 55 return 56 } 57 request.Header.Add("Cookie", fmt.Sprintf("token=%s", token)) 58 request.Header.Add("Content-Type", "application/json") 59 response, err := client.Do(request) 60 if err != nil { 61 log.Println(err) 62 return 63 } 64 if response != nil { 65 assert.Equal(tt.expectedStatus, response.StatusCode, tt.name+" Failed") 66 } 67 }) 68 } 69 } 70 71 func Test_GetConfigAPI(t *testing.T) { 72 assert := assert.New(t) 73 74 type args struct { 75 name string 76 } 77 tests := []struct { 78 name string 79 args args 80 expectedStatus int 81 expectedError error 82 }{ 83 { 84 name: "Get Config - Valid", 85 args: args{ 86 name: "storage_class", 87 }, 88 expectedStatus: 200, 89 expectedError: nil, 90 }, 91 { 92 name: "Get Config - Invalid", 93 args: args{ 94 name: "asdf", 95 }, 96 expectedStatus: 404, 97 expectedError: nil, 98 }, 99 } 100 101 for _, tt := range tests { 102 t.Run(tt.name, func(_ *testing.T) { 103 client := &http.Client{ 104 Timeout: 3 * time.Second, 105 } 106 107 request, err := http.NewRequest( 108 "GET", fmt.Sprintf("http://localhost:9090/api/v1/configs/%s", tt.args.name), nil) 109 if err != nil { 110 log.Println(err) 111 return 112 } 113 request.Header.Add("Cookie", fmt.Sprintf("token=%s", token)) 114 request.Header.Add("Content-Type", "application/json") 115 response, err := client.Do(request) 116 if err != nil { 117 log.Println(err) 118 return 119 } 120 if response != nil { 121 assert.Equal(tt.expectedStatus, response.StatusCode, tt.name+" Failed") 122 } 123 }) 124 } 125 } 126 127 func Test_SetConfigAPI(t *testing.T) { 128 assert := assert.New(t) 129 130 type args struct { 131 name string 132 keyValues []map[string]interface{} 133 } 134 tests := []struct { 135 name string 136 args args 137 expectedStatus int 138 expectedError error 139 }{ 140 { 141 name: "Set Config - Valid", 142 args: args{ 143 name: "region", 144 keyValues: []map[string]interface{}{{"key": "name", "value": "testServer"}, {"key": "region", "value": "us-west-1"}}, 145 }, 146 expectedStatus: 200, 147 expectedError: nil, 148 }, 149 { 150 name: "Set Config - Invalid", 151 args: args{ 152 name: "regiontest", 153 keyValues: []map[string]interface{}{{"key": "name", "value": "testServer"}, {"key": "region", "value": "us-west-1"}}, 154 }, 155 expectedStatus: 500, 156 expectedError: nil, 157 }, 158 } 159 160 for _, tt := range tests { 161 t.Run(tt.name, func(_ *testing.T) { 162 client := &http.Client{ 163 Timeout: 3 * time.Second, 164 } 165 166 requestDataPolicy := map[string]interface{}{} 167 168 requestDataPolicy["key_values"] = tt.args.keyValues 169 170 requestDataJSON, _ := json.Marshal(requestDataPolicy) 171 requestDataBody := bytes.NewReader(requestDataJSON) 172 request, err := http.NewRequest( 173 "PUT", fmt.Sprintf("http://localhost:9090/api/v1/configs/%s", tt.args.name), requestDataBody) 174 if err != nil { 175 log.Println(err) 176 return 177 } 178 request.Header.Add("Cookie", fmt.Sprintf("token=%s", token)) 179 request.Header.Add("Content-Type", "application/json") 180 response, err := client.Do(request) 181 if err != nil { 182 log.Println(err) 183 return 184 } 185 if response != nil { 186 assert.Equal(tt.expectedStatus, response.StatusCode, tt.name+" Failed") 187 } 188 }) 189 } 190 } 191 192 func Test_ResetConfigAPI(t *testing.T) { 193 assert := assert.New(t) 194 195 type args struct { 196 name string 197 } 198 tests := []struct { 199 name string 200 args args 201 expectedStatus int 202 expectedError error 203 }{ 204 { 205 name: "Reset Config - Valid", 206 args: args{ 207 name: "region", 208 }, 209 expectedStatus: 200, 210 expectedError: nil, 211 }, 212 { 213 name: "Reset Config - Invalid", 214 args: args{ 215 name: "regiontest", 216 }, 217 expectedStatus: 500, 218 expectedError: nil, 219 }, 220 } 221 222 for _, tt := range tests { 223 t.Run(tt.name, func(_ *testing.T) { 224 client := &http.Client{ 225 Timeout: 3 * time.Second, 226 } 227 228 requestDataPolicy := map[string]interface{}{} 229 230 requestDataJSON, _ := json.Marshal(requestDataPolicy) 231 requestDataBody := bytes.NewReader(requestDataJSON) 232 request, err := http.NewRequest( 233 "POST", fmt.Sprintf("http://localhost:9090/api/v1/configs/%s/reset", tt.args.name), requestDataBody) 234 if err != nil { 235 log.Println(err) 236 return 237 } 238 request.Header.Add("Cookie", fmt.Sprintf("token=%s", token)) 239 request.Header.Add("Content-Type", "application/json") 240 response, err := client.Do(request) 241 if err != nil { 242 log.Println(err) 243 return 244 } 245 if response != nil { 246 assert.Equal(tt.expectedStatus, response.StatusCode, tt.name+" Failed") 247 } 248 }) 249 } 250 }