|
| 1 | +// Copyright 2022 The Codefresh Authors. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package routing_test |
| 16 | + |
| 17 | +import ( |
| 18 | + "bytes" |
| 19 | + "log" |
| 20 | + "os" |
| 21 | + "testing" |
| 22 | + |
| 23 | + "github.com/stretchr/testify/assert" |
| 24 | + |
| 25 | + "github.com/codefresh-io/cli-v2/pkg/util/routing" |
| 26 | + v1 "k8s.io/api/networking/v1" |
| 27 | + yamlutil "k8s.io/apimachinery/pkg/util/yaml" |
| 28 | +) |
| 29 | + |
| 30 | +// TestCreateInternalRouterRoute calls routing.CreateInternalRouterRoute, checking |
| 31 | +// that a correct route is returned. |
| 32 | +func TestCreateInternalRouterRoute(t *testing.T) { |
| 33 | + tests := map[string]struct { |
| 34 | + routeOpts *routing.CreateRouteOpts |
| 35 | + wantFilePath string |
| 36 | + getExpected func() interface{} |
| 37 | + }{ |
| 38 | + string(routing.IngressControllerALB): { |
| 39 | + routeOpts: &routing.CreateRouteOpts{ |
| 40 | + RuntimeName: "test-runtime", |
| 41 | + Namespace: "test-runtime", |
| 42 | + IngressClass: "alb", |
| 43 | + Hostname: "testing.foo.bar.com", |
| 44 | + IngressController: routing.GetIngressController(string(routing.IngressControllerALB)), |
| 45 | + Annotations: nil, |
| 46 | + GatewayName: "", |
| 47 | + GatewayNamespace: "", |
| 48 | + }, |
| 49 | + getExpected: func() interface{} { |
| 50 | + ingress, err := yamlToIngress("testdata/alb-ingress.yaml") |
| 51 | + if err != nil { |
| 52 | + log.Fatal(err) |
| 53 | + } |
| 54 | + |
| 55 | + return ingress |
| 56 | + }, |
| 57 | + }, |
| 58 | + string(routing.IngressControllerNginxCommunity): { |
| 59 | + routeOpts: &routing.CreateRouteOpts{ |
| 60 | + RuntimeName: "test-runtime", |
| 61 | + Namespace: "test-runtime", |
| 62 | + IngressClass: "nginx", |
| 63 | + Hostname: "testing.foo.bar.com", |
| 64 | + IngressController: routing.GetIngressController(string(routing.IngressControllerNginxCommunity)), |
| 65 | + Annotations: nil, |
| 66 | + GatewayName: "", |
| 67 | + GatewayNamespace: "", |
| 68 | + }, |
| 69 | + getExpected: func() interface{} { |
| 70 | + ingress, err := yamlToIngress("testdata/nginx-ingress.yaml") |
| 71 | + if err != nil { |
| 72 | + log.Fatal(err) |
| 73 | + } |
| 74 | + |
| 75 | + return ingress |
| 76 | + }, |
| 77 | + }, |
| 78 | + } |
| 79 | + |
| 80 | + for tname, tt := range tests { |
| 81 | + t.Run(tname, func(t *testing.T) { |
| 82 | + _, received := routing.CreateInternalRouterRoute(tt.routeOpts, false, true, false) |
| 83 | + assert.Equal(t, tt.getExpected(), received) |
| 84 | + }) |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +func yamlToIngress(path string) (*v1.Ingress, error) { |
| 89 | + var want *v1.Ingress |
| 90 | + ingressFile, err := os.ReadFile(path) |
| 91 | + if err != nil { |
| 92 | + return nil, err |
| 93 | + } |
| 94 | + |
| 95 | + d := yamlutil.NewYAMLOrJSONDecoder(bytes.NewReader(ingressFile), 100) |
| 96 | + err = d.Decode(&want) |
| 97 | + if err != nil { |
| 98 | + return nil, err |
| 99 | + } |
| 100 | + |
| 101 | + return want, nil |
| 102 | +} |
0 commit comments