|
1 | 1 | //
|
2 |
| -// Copyright 2022 Red Hat, Inc. |
| 2 | +// Copyright 2022-2023 Red Hat, Inc. |
3 | 3 | //
|
4 | 4 | // Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | // you may not use this file except in compliance with the License.
|
@@ -4267,6 +4267,207 @@ func Test_setDefaults(t *testing.T) {
|
4267 | 4267 | }
|
4268 | 4268 | }
|
4269 | 4269 |
|
| 4270 | +func Test_getKubernetesDefinitionFromUri(t *testing.T) { |
| 4271 | + const ( |
| 4272 | + uri1 = "127.0.0.1:8080" |
| 4273 | + httpPrefix = "http://" |
| 4274 | + localRelativeURI = "testTmp/dir/devfile.yaml" |
| 4275 | + localDeployFilePath = "testTmp/dir/deploy.yaml" |
| 4276 | + ) |
| 4277 | + |
| 4278 | + deployYamlUri := httpPrefix + uri1 + "/deploy.yaml" |
| 4279 | + |
| 4280 | + deployContent := ` |
| 4281 | +kind: Deployment |
| 4282 | +apiVersion: apps/v1 |
| 4283 | +metadata: |
| 4284 | + name: my-app |
| 4285 | +spec: |
| 4286 | + replicas: 1 |
| 4287 | + selector: |
| 4288 | + matchLabels: |
| 4289 | + app: my-app |
| 4290 | + template: |
| 4291 | + metadata: |
| 4292 | + labels: |
| 4293 | + app: my-app |
| 4294 | + spec: |
| 4295 | + containers: |
| 4296 | + - name: my-app |
| 4297 | + images: image:latest |
| 4298 | + ports: |
| 4299 | + - name: http |
| 4300 | + containerPort: 8081 |
| 4301 | + protocol: TCP |
| 4302 | + resources: |
| 4303 | + limits: |
| 4304 | + memory: "1024Mi" |
| 4305 | + cpu: "500m" |
| 4306 | +` |
| 4307 | + |
| 4308 | + // prepare for local file |
| 4309 | + err := os.MkdirAll(path.Dir(localDeployFilePath), 0755) |
| 4310 | + if err != nil { |
| 4311 | + fmt.Errorf("Test_getKubernetesDefinitionFromUri() error: failed to create folder: %v, error: %v", path.Dir(localDeployFilePath), err) |
| 4312 | + } |
| 4313 | + |
| 4314 | + err = ioutil.WriteFile(localDeployFilePath, []byte(deployContent), 0644) |
| 4315 | + if err != nil { |
| 4316 | + fmt.Errorf("Test_getKubernetesDefinitionFromUri() error: fail to write to file: %v", err) |
| 4317 | + } |
| 4318 | + |
| 4319 | + if err != nil { |
| 4320 | + t.Error(err) |
| 4321 | + } |
| 4322 | + |
| 4323 | + defer os.RemoveAll("testTmp/") |
| 4324 | + |
| 4325 | + localDevfileCtx := devfileCtx.NewDevfileCtx(localRelativeURI) |
| 4326 | + err = localDevfileCtx.SetAbsPath() |
| 4327 | + if err != nil { |
| 4328 | + t.Errorf("Test_getKubernetesDefinitionFromUri() unexpected error: %v", err) |
| 4329 | + return |
| 4330 | + } |
| 4331 | + |
| 4332 | + URLDevfileCtx := devfileCtx.NewURLDevfileCtx(httpPrefix + uri1) |
| 4333 | + |
| 4334 | + rawContent := ` |
| 4335 | +schemaVersion: 2.2.0 |
| 4336 | +metadata: |
| 4337 | + name: go |
| 4338 | + language: Go |
| 4339 | + projectType: Go |
| 4340 | + tags: |
| 4341 | + - Go |
| 4342 | +components: |
| 4343 | + - name: kubernetes-deploy |
| 4344 | + kubernetes: |
| 4345 | + uri: deploy.yaml |
| 4346 | +commands: |
| 4347 | + - id: deployk8s |
| 4348 | + apply: |
| 4349 | + component: kubernetes-deploy |
| 4350 | +` |
| 4351 | + rawDevfileContext, err := devfileCtx.NewByteContentDevfileCtx([]byte(rawContent)) |
| 4352 | + if err != nil { |
| 4353 | + t.Errorf("Test_getKubernetesDefinitionFromUri() unexpected error: %v", err) |
| 4354 | + return |
| 4355 | + } |
| 4356 | + |
| 4357 | + testServer := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 4358 | + if strings.Contains(r.URL.Path, "notexist") { |
| 4359 | + w.WriteHeader(http.StatusNotFound) |
| 4360 | + return |
| 4361 | + } |
| 4362 | + var data []byte |
| 4363 | + var err error |
| 4364 | + if strings.Contains(r.URL.Path, "deploy.yaml") { |
| 4365 | + data = []byte(deployContent) |
| 4366 | + } |
| 4367 | + if err != nil { |
| 4368 | + t.Errorf("Test_getKubernetesDefinitionFromUri() unexpected while doing yaml marshal: %v", err) |
| 4369 | + return |
| 4370 | + } |
| 4371 | + _, err = w.Write(data) |
| 4372 | + if err != nil { |
| 4373 | + t.Errorf("Test_getKubernetesDefinitionFromUri() unexpected error while writing data: %v", err) |
| 4374 | + } |
| 4375 | + })) |
| 4376 | + // create a listener with the desired port. |
| 4377 | + l, err := net.Listen("tcp", uri1) |
| 4378 | + if err != nil { |
| 4379 | + t.Errorf("Test_getKubernetesDefinitionFromUri() unexpected error while creating listener: %v", err) |
| 4380 | + return |
| 4381 | + } |
| 4382 | + |
| 4383 | + // NewUnstartedServer creates a listener. Close that listener and replace |
| 4384 | + // with the one we created. |
| 4385 | + testServer.Listener.Close() |
| 4386 | + testServer.Listener = l |
| 4387 | + |
| 4388 | + testServer.Start() |
| 4389 | + defer testServer.Close() |
| 4390 | + |
| 4391 | + notAbleToResolveURIErr := "error getting kubernetes resources definition information, unable to resolve the file uri.*" |
| 4392 | + invalidPathErr := "failed to read kubernetes resources definition from path.*" |
| 4393 | + invalidURLErr := "error getting kubernetes resources definition information" |
| 4394 | + |
| 4395 | + tests := []struct { |
| 4396 | + name string |
| 4397 | + uri string |
| 4398 | + devfileCtx devfileCtx.DevfileCtx |
| 4399 | + wantContent string |
| 4400 | + wantErr *string |
| 4401 | + }{ |
| 4402 | + { |
| 4403 | + name: "should be able to parse from relative uri on local disk", |
| 4404 | + devfileCtx: localDevfileCtx, |
| 4405 | + uri: "deploy.yaml", |
| 4406 | + wantContent: deployContent, |
| 4407 | + }, |
| 4408 | + { |
| 4409 | + name: "should be able to parse from remote deploy file from local devfile", |
| 4410 | + devfileCtx: localDevfileCtx, |
| 4411 | + uri: deployYamlUri, |
| 4412 | + wantContent: deployContent, |
| 4413 | + }, |
| 4414 | + { |
| 4415 | + name: "should fail with invalid uri from local devfile", |
| 4416 | + devfileCtx: localDevfileCtx, |
| 4417 | + uri: "invalidpath/deploy.yaml", |
| 4418 | + wantErr: &invalidPathErr, |
| 4419 | + }, |
| 4420 | + { |
| 4421 | + name: "should be able to parse from remote deploy file from remote devfile", |
| 4422 | + devfileCtx: URLDevfileCtx, |
| 4423 | + uri: deployYamlUri, |
| 4424 | + wantContent: deployContent, |
| 4425 | + }, |
| 4426 | + { |
| 4427 | + name: "should be able to parse from remote deploy file from relative path devfile", |
| 4428 | + devfileCtx: URLDevfileCtx, |
| 4429 | + uri: "deploy.yaml", |
| 4430 | + wantContent: deployContent, |
| 4431 | + }, |
| 4432 | + { |
| 4433 | + name: "should fail with invalid relative uri from remote devfile", |
| 4434 | + devfileCtx: URLDevfileCtx, |
| 4435 | + uri: "notexist/deploy.yaml", |
| 4436 | + wantErr: &invalidURLErr, |
| 4437 | + }, |
| 4438 | + { |
| 4439 | + name: "should fail to parse from relative uri from raw content", |
| 4440 | + devfileCtx: rawDevfileContext, |
| 4441 | + uri: "deploy.yaml", |
| 4442 | + wantErr: ¬AbleToResolveURIErr, |
| 4443 | + }, |
| 4444 | + { |
| 4445 | + name: "should be able to parse from remote deploy file from raw content", |
| 4446 | + devfileCtx: rawDevfileContext, |
| 4447 | + uri: deployYamlUri, |
| 4448 | + wantContent: deployContent, |
| 4449 | + }, |
| 4450 | + { |
| 4451 | + name: "should fail with invalid absolute URL from raw content", |
| 4452 | + devfileCtx: rawDevfileContext, |
| 4453 | + uri: httpPrefix + uri1 + "/notexist/deploy.yaml", |
| 4454 | + wantErr: &invalidURLErr, |
| 4455 | + }, |
| 4456 | + } |
| 4457 | + for _, tt := range tests { |
| 4458 | + t.Run(tt.name, func(t *testing.T) { |
| 4459 | + got, err := getKubernetesDefinitionFromUri(tt.uri, tt.devfileCtx) |
| 4460 | + if (err != nil) != (tt.wantErr != nil) { |
| 4461 | + t.Errorf("Test_getKubernetesDefinitionFromUri() unexpected error: %v, wantErr %v", err, *tt.wantErr) |
| 4462 | + } else if err == nil { |
| 4463 | + assert.Equal(t, tt.wantContent, string(got), "Test_getKubernetesDefinitionFromUri() error: the deploy content should matched") |
| 4464 | + } else if err != nil { |
| 4465 | + assert.Regexp(t, *tt.wantErr, err.Error(), "Test_getKubernetesDefinitionFromUri(): Error message should match") |
| 4466 | + } |
| 4467 | + }) |
| 4468 | + } |
| 4469 | +} |
| 4470 | + |
4270 | 4471 | // getUnsetBooleanDevfileObj returns a DevfileData object that contains unset boolean properties
|
4271 | 4472 | func getUnsetBooleanDevfileTestData(apiVersion string) (devfileData data.DevfileData, err error) {
|
4272 | 4473 | devfileData = &v2.DevfileV2{
|
|
0 commit comments