github.com/aiven/aiven-go-client@v1.36.0/service_test.go (about) 1 package aiven 2 3 import ( 4 "encoding/json" 5 "net/http" 6 "net/http/httptest" 7 "reflect" 8 "testing" 9 ) 10 11 func setupServiceTestCase(t *testing.T) (*Client, func(t *testing.T)) { 12 t.Log("setup Service test case") 13 14 const ( 15 UserName = "test@aiven.io" 16 UserPassword = "testabcd" 17 AccessToken = "some-random-token" 18 ) 19 20 ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 21 service := ServiceResponse{ 22 Service: &Service{ 23 CloudName: "google-europe-west1", 24 NodeCount: 1, 25 Plan: "hobbyist", 26 Name: "test-service", 27 Type: "kafka", 28 NodeStates: []*NodeState{ 29 { 30 Name: "test-service-1", 31 Role: "master", 32 State: "running", 33 ProgressUpdates: []ProgressUpdate{}, 34 }, 35 }, 36 }, 37 } 38 39 if r.URL.Path == "/userauth" { 40 w.Header().Set("Content-Type", "application/json") 41 w.WriteHeader(http.StatusOK) 42 err := json.NewEncoder(w).Encode(authResponse{ 43 Token: AccessToken, 44 State: "active", 45 }) 46 47 if err != nil { 48 t.Error(err) 49 } 50 return 51 } else if r.URL.Path == "/project/test-pr/service" { 52 w.Header().Set("Content-Type", "application/json") 53 w.WriteHeader(http.StatusOK) 54 55 err := json.NewEncoder(w).Encode(service) 56 57 if err != nil { 58 t.Error(err) 59 } 60 return 61 } else if r.URL.Path == "/project/test-pr/service/test-sr" { 62 w.Header().Set("Content-Type", "application/json") 63 w.WriteHeader(http.StatusOK) 64 65 err := json.NewEncoder(w).Encode(service) 66 67 if err != nil { 68 t.Error(err) 69 } 70 return 71 } else if r.URL.Path == "/project/test-pr-list/service" { 72 w.Header().Set("Content-Type", "application/json") 73 w.WriteHeader(http.StatusOK) 74 err := json.NewEncoder(w).Encode(ServiceListResponse{ 75 APIResponse: APIResponse{}, 76 Services: nil, 77 }) 78 79 if err != nil { 80 t.Error(err) 81 } 82 return 83 } else { 84 w.WriteHeader(http.StatusBadRequest) 85 } 86 })) 87 88 apiUrl = ts.URL 89 90 c, err := NewUserClient(UserName, UserPassword, "aiven-go-client-test/"+Version()) 91 if err != nil { 92 t.Fatalf("user authentication error: %s", err) 93 } 94 95 return c, func(t *testing.T) { 96 t.Log("teardown ElasticsearchACLs test case") 97 } 98 } 99 100 func TestServicesHandler_Create(t *testing.T) { 101 c, _ := setupServiceTestCase(t) 102 103 type fields struct { 104 client *Client 105 } 106 type args struct { 107 project string 108 req CreateServiceRequest 109 } 110 tests := []struct { 111 name string 112 fields fields 113 args args 114 want *Service 115 wantErr bool 116 }{ 117 { 118 "error-expected", 119 fields{client: c}, 120 args{ 121 project: "test-pr-wrong", 122 req: CreateServiceRequest{ 123 Cloud: "google-europe-west1", 124 GroupName: "default", 125 Plan: "hobbyist", 126 ServiceName: "test-service", 127 ServiceType: "kafka", 128 }, 129 }, 130 nil, 131 true, 132 }, 133 { 134 "normal", 135 fields{client: c}, 136 args{ 137 project: "test-pr", 138 req: CreateServiceRequest{ 139 Cloud: "google-europe-west1", 140 GroupName: "default", 141 Plan: "hobbyist", 142 ServiceName: "test-service", 143 ServiceType: "kafka", 144 }, 145 }, 146 &Service{ 147 CloudName: "google-europe-west1", 148 NodeCount: 1, 149 Plan: "hobbyist", 150 Name: "test-service", 151 Type: "kafka", 152 NodeStates: []*NodeState{ 153 { 154 Name: "test-service-1", 155 Role: "master", 156 State: "running", 157 ProgressUpdates: []ProgressUpdate{}, 158 }, 159 }, 160 }, 161 false, 162 }, 163 } 164 for _, tt := range tests { 165 t.Run(tt.name, func(t *testing.T) { 166 h := &ServicesHandler{ 167 client: tt.fields.client, 168 } 169 got, err := h.Create(tt.args.project, tt.args.req) 170 if (err != nil) != tt.wantErr { 171 t.Errorf("Create() error = %v, wantErr %v", err, tt.wantErr) 172 return 173 } 174 if !reflect.DeepEqual(got, tt.want) { 175 t.Errorf("Create() got = %v, want %v", got, tt.want) 176 } 177 }) 178 } 179 } 180 181 func TestServicesHandler_Get(t *testing.T) { 182 c, _ := setupServiceTestCase(t) 183 184 type fields struct { 185 client *Client 186 } 187 type args struct { 188 project string 189 service string 190 } 191 tests := []struct { 192 name string 193 fields fields 194 args args 195 want *Service 196 wantErr bool 197 }{ 198 { 199 name: "normal", 200 fields: fields{client: c}, 201 args: args{project: "test-pr", service: "test-sr"}, 202 want: &Service{ 203 CloudName: "google-europe-west1", 204 NodeCount: 1, 205 Plan: "hobbyist", 206 Name: "test-service", 207 Type: "kafka", 208 NodeStates: []*NodeState{ 209 { 210 Name: "test-service-1", 211 Role: "master", 212 State: "running", 213 ProgressUpdates: []ProgressUpdate{}, 214 }, 215 }, 216 }, 217 wantErr: false, 218 }, 219 } 220 for _, tt := range tests { 221 t.Run(tt.name, func(t *testing.T) { 222 h := &ServicesHandler{ 223 client: tt.fields.client, 224 } 225 got, err := h.Get(tt.args.project, tt.args.service) 226 if (err != nil) != tt.wantErr { 227 t.Errorf("Get() error = %v, wantErr %v", err, tt.wantErr) 228 return 229 } 230 if !reflect.DeepEqual(got, tt.want) { 231 t.Errorf("Get() got = %v, want %v", got, tt.want) 232 } 233 }) 234 } 235 } 236 237 func TestServicesHandler_Update(t *testing.T) { 238 c, _ := setupServiceTestCase(t) 239 240 type fields struct { 241 client *Client 242 } 243 type args struct { 244 project string 245 service string 246 req UpdateServiceRequest 247 } 248 tests := []struct { 249 name string 250 fields fields 251 args args 252 want *Service 253 wantErr bool 254 }{ 255 { 256 name: "normal", 257 fields: fields{client: c}, 258 args: args{project: "test-pr", service: "test-sr", req: UpdateServiceRequest{ 259 Cloud: "google-europe-west1", 260 GroupName: "default", 261 Plan: "hobbyist", 262 }}, 263 want: &Service{ 264 CloudName: "google-europe-west1", 265 NodeCount: 1, 266 Plan: "hobbyist", 267 Name: "test-service", 268 Type: "kafka", 269 NodeStates: []*NodeState{ 270 &NodeState{ 271 Name: "test-service-1", 272 Role: "master", 273 State: "running", 274 ProgressUpdates: []ProgressUpdate{}, 275 }, 276 }, 277 }, 278 wantErr: false, 279 }, 280 } 281 for _, tt := range tests { 282 t.Run(tt.name, func(t *testing.T) { 283 h := &ServicesHandler{ 284 client: tt.fields.client, 285 } 286 got, err := h.Update(tt.args.project, tt.args.service, tt.args.req) 287 if (err != nil) != tt.wantErr { 288 t.Errorf("Update() error = %v, wantErr %v", err, tt.wantErr) 289 return 290 } 291 if !reflect.DeepEqual(got, tt.want) { 292 t.Errorf("Update() got = %v, want %v", got, tt.want) 293 } 294 }) 295 } 296 } 297 298 func TestServicesHandler_Delete(t *testing.T) { 299 c, _ := setupServiceTestCase(t) 300 301 type fields struct { 302 client *Client 303 } 304 type args struct { 305 project string 306 service string 307 } 308 tests := []struct { 309 name string 310 fields fields 311 args args 312 wantErr bool 313 }{ 314 { 315 name: "", 316 fields: fields{client: c}, 317 args: args{ 318 project: "test-pr", 319 service: "test-sr", 320 }, 321 wantErr: false, 322 }, 323 } 324 for _, tt := range tests { 325 t.Run(tt.name, func(t *testing.T) { 326 h := &ServicesHandler{ 327 client: tt.fields.client, 328 } 329 if err := h.Delete(tt.args.project, tt.args.service); (err != nil) != tt.wantErr { 330 t.Errorf("Delete() error = %v, wantErr %v", err, tt.wantErr) 331 } 332 }) 333 } 334 } 335 336 func TestServicesHandler_List(t *testing.T) { 337 c, _ := setupServiceTestCase(t) 338 339 type fields struct { 340 client *Client 341 } 342 type args struct { 343 project string 344 } 345 tests := []struct { 346 name string 347 fields fields 348 args args 349 want []*Service 350 wantErr bool 351 }{ 352 { 353 name: "", 354 fields: fields{client: c}, 355 args: args{project: "test-pr-list"}, 356 want: nil, 357 wantErr: false, 358 }, 359 } 360 for _, tt := range tests { 361 t.Run(tt.name, func(t *testing.T) { 362 h := &ServicesHandler{ 363 client: tt.fields.client, 364 } 365 got, err := h.List(tt.args.project) 366 if (err != nil) != tt.wantErr { 367 t.Errorf("List() error = %v, wantErr %v", err, tt.wantErr) 368 return 369 } 370 if !reflect.DeepEqual(got, tt.want) { 371 t.Errorf("List() got = %v, want %v", got, tt.want) 372 } 373 }) 374 } 375 }