diff --git a/cmd/template_ingress_test.go b/cmd/template_ingress_test.go index 0004e846..0b8dd957 100644 --- a/cmd/template_ingress_test.go +++ b/cmd/template_ingress_test.go @@ -232,7 +232,7 @@ func TestTemplateRoutes(t *testing.T) { want: "../test-resources/template-ingress/test10-results", }, { - name: "test10 standby no values", + name: "test11 standby no values", args: args{ alertContact: "alertcontact", statusPageID: "statuspageid", @@ -250,6 +250,25 @@ func TestTemplateRoutes(t *testing.T) { }, want: "../test-resources/template-ingress/test11-results", }, + { + name: "test12 check LAGOON_ROUTES_JSON generates ingress", + args: args{ + alertContact: "alertcontact", + statusPageID: "statuspageid", + projectName: "example-project", + environmentName: "main", + environmentType: "production", + buildType: "branch", + lagoonVersion: "v2.7.x", + branch: "main", + projectVars: `[{"name":"LAGOON_ROUTES_JSON","value":"eyJyb3V0ZXMiOlt7ImRvbWFpbiI6InRlc3QxLmV4YW1wbGUuY29tIiwic2VydmljZSI6Im5naW54IiwidGxzLWFjbWUiOmZhbHNlLCJtb25pdG9yaW5nLXBhdGgiOiIvYnlwYXNzLWNhY2hlIn1dfQo=","scope":"build"},{"name":"LAGOON_SYSTEM_ROUTER_PATTERN","value":"${service}-${project}-${environment}.example.com","scope":"internal_system"},{"name":"LAGOON_FASTLY_SERVICE_IDS","value":"example.com:service-id:true:annotationscom","scope":"build"}]`, + envVars: `[]`, + secretPrefix: "fastly-api-", + lagoonYAML: "../test-resources/template-ingress/test12/lagoon.yml", + templatePath: "../test-resources/template-ingress/output", + }, + want: "../test-resources/template-ingress/test12-results", + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { diff --git a/internal/generator/ingress_test.go b/internal/generator/ingress_test.go index f3119c72..dbe262a5 100644 --- a/internal/generator/ingress_test.go +++ b/internal/generator/ingress_test.go @@ -71,6 +71,7 @@ func Test_generateAndMerge(t *testing.T) { want lagoon.RoutesV2 wantErr bool }{ + // TODO: test just API { name: "test1 - generate routes from lagoon yaml and merge ones from api onto them", args: args{ @@ -156,6 +157,51 @@ func Test_generateAndMerge(t *testing.T) { }, }, }, + { + name: "test2 - don't generate routes from lagoon yaml and only merge ones from api onto them", + args: args{ + buildValues: BuildValues{ + Branch: "main", + }, + lagoonYAML: lagoon.YAML{}, + api: lagoon.RoutesV2{ + Routes: []lagoon.RouteV2{ + { + Domain: "test1.example.com", + Service: "nginx", + TLSAcme: helpers.BoolPtr(false), + MonitoringPath: "/bypass-cache", + }, + { + Domain: "a.example.com", + Service: "nginx", + TLSAcme: helpers.BoolPtr(false), + MonitoringPath: "/bypass-cache", + }, + }, + }, + }, + want: lagoon.RoutesV2{ + Routes: []lagoon.RouteV2{ + { + Domain: "test1.example.com", + Service: "nginx", + TLSAcme: helpers.BoolPtr(false), + MonitoringPath: "/bypass-cache", + Insecure: helpers.StrPtr("Redirect"), + Annotations: map[string]string{}, + }, + { + Domain: "a.example.com", + Service: "nginx", + TLSAcme: helpers.BoolPtr(false), + Annotations: map[string]string{}, + Insecure: helpers.StrPtr("Redirect"), + MonitoringPath: "/bypass-cache", + }, + }, + }, + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { @@ -293,24 +339,79 @@ func Test_generateAutogenRoutes(t *testing.T) { tests := []struct { name string args args + want lagoon.RoutesV2 wantErr bool }{ - // TODO: Add test cases. + { + name: "test1", + args: args{ + envVars: []lagoon.EnvironmentVariable{ + { + Name: "LAGOON_SYSTEM_ROUTER_PATTERN", + Value: "${service}-${project}-${environment}.example.com", + Scope: "internal_system", + }, + }, + lagoonYAML: &lagoon.YAML{}, + buildValues: &BuildValues{ + Project: "example-com", + BuildType: "branch", + Environment: "main", + EnvironmentType: "development", + Namespace: "example-com-main", + Services: map[string]ServiceValues{ + "nginx": { + Name: "nginx", + Type: "nginx", + AutogeneratedRoutesEnabled: true, + AutogeneratedRoutesTLSAcme: true, + }, + }, + }, + autogenRoutes: &lagoon.RoutesV2{}, + }, + want: lagoon.RoutesV2{ + Routes: []lagoon.RouteV2{ + { + Domain: "nginx-example-com-main.example.com", + Service: "nginx", + Autogenerated: true, + TLSAcme: helpers.BoolPtr(true), + IngressName: "nginx", + Insecure: helpers.StrPtr("Allow"), + AlternativeNames: []string{}, + Labels: map[string]string{ + "app.kubernetes.io/instance": "nginx", + "app.kubernetes.io/name": "autogenerated-ingress", + "helm.sh/chart": "autogenerated-ingress-0.1.0", + "lagoon.sh/autogenerated": "true", + "lagoon.sh/service": "nginx", + "lagoon.sh/service-type": "nginx", + }, + }, + }, + }, + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := generateAutogenRoutes(tt.args.envVars, tt.args.lagoonYAML, tt.args.buildValues, tt.args.autogenRoutes); (err != nil) != tt.wantErr { t.Errorf("generateAutogenRoutes() error = %v, wantErr %v", err, tt.wantErr) } + lValues, _ := json.Marshal(tt.args.autogenRoutes) + wValues, _ := json.Marshal(tt.want) + if !reflect.DeepEqual(string(lValues), string(wValues)) { + t.Errorf("generateAutogenRoutes() = %v, want %v", string(lValues), string(wValues)) + } }) } } func Test_generateRoutes(t *testing.T) { type args struct { - lagoonEnvVars []lagoon.EnvironmentVariable + envVars []lagoon.EnvironmentVariable buildValues BuildValues - lYAML lagoon.YAML + lagoonYAML lagoon.YAML autogenRoutes *lagoon.RoutesV2 mainRoutes *lagoon.RoutesV2 activeStanbyRoutes *lagoon.RoutesV2 @@ -324,11 +425,103 @@ func Test_generateRoutes(t *testing.T) { want2 []string wantErr bool }{ - // TODO: Add test cases. + { + name: "test1", + args: args{ + envVars: []lagoon.EnvironmentVariable{ + { + Name: "LAGOON_SYSTEM_ROUTER_PATTERN", + Value: "${service}-${project}-${environment}.example.com", + Scope: "internal_system", + }, + }, + lagoonYAML: lagoon.YAML{}, + buildValues: BuildValues{ + Project: "example-com", + BuildType: "branch", + Environment: "main", + EnvironmentType: "development", + Namespace: "example-com-main", + Services: map[string]ServiceValues{ + "nginx": { + Name: "nginx", + Type: "nginx", + AutogeneratedRoutesEnabled: true, + AutogeneratedRoutesTLSAcme: true, + }, + }, + }, + autogenRoutes: &lagoon.RoutesV2{}, + mainRoutes: &lagoon.RoutesV2{}, + activeStanbyRoutes: &lagoon.RoutesV2{}, + }, + want: "https://nginx-example-com-main.example.com", + want1: []string{"https://nginx-example-com-main.example.com"}, + want2: []string{"https://nginx-example-com-main.example.com"}, + }, + { + name: "test2", + args: args{ + envVars: []lagoon.EnvironmentVariable{ + { + Name: "LAGOON_SYSTEM_ROUTER_PATTERN", + Value: "${service}-${project}-${environment}.example.com", + Scope: "internal_system", + }, + }, + lagoonYAML: lagoon.YAML{ + Environments: lagoon.Environments{ + "main": lagoon.Environment{ + Routes: []map[string][]lagoon.Route{ + { + "nginx": { + { + Ingresses: map[string]lagoon.Ingress{ + "a.example.com": { + TLSAcme: helpers.BoolPtr(true), + }, + }, + }, + { + Name: "b.example.com", + }, + { + Name: "c.example.com", + }, + }, + }, + }, + }, + }, + }, + buildValues: BuildValues{ + Project: "example-com", + BuildType: "branch", + Environment: "main", + Branch: "main", + EnvironmentType: "development", + Namespace: "example-com-main", + Services: map[string]ServiceValues{ + "nginx": { + Name: "nginx", + Type: "nginx", + AutogeneratedRoutesEnabled: true, + AutogeneratedRoutesTLSAcme: true, + }, + }, + }, + autogenRoutes: &lagoon.RoutesV2{}, + mainRoutes: &lagoon.RoutesV2{}, + activeStanbyRoutes: &lagoon.RoutesV2{}, + }, + want: "https://a.example.com", + want1: []string{"https://nginx-example-com-main.example.com", "https://a.example.com", "https://b.example.com", "https://c.example.com"}, + want2: []string{"https://nginx-example-com-main.example.com"}, + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - got, got1, got2, err := generateRoutes(tt.args.lagoonEnvVars, tt.args.buildValues, tt.args.lYAML, tt.args.autogenRoutes, tt.args.mainRoutes, tt.args.activeStanbyRoutes, tt.args.debug) + got, got1, got2, err := generateRoutes(tt.args.envVars, tt.args.buildValues, tt.args.lagoonYAML, tt.args.autogenRoutes, tt.args.mainRoutes, tt.args.activeStanbyRoutes, tt.args.debug) if (err != nil) != tt.wantErr { t.Errorf("generateRoutes() error = %v, wantErr %v", err, tt.wantErr) return diff --git a/test-resources/template-ingress/test12-results/test1.example.com.yaml b/test-resources/template-ingress/test12-results/test1.example.com.yaml new file mode 100644 index 00000000..a23c0658 --- /dev/null +++ b/test-resources/template-ingress/test12-results/test1.example.com.yaml @@ -0,0 +1,49 @@ +--- +apiVersion: networking.k8s.io/v1 +kind: Ingress +metadata: + annotations: + fastly.amazee.io/watch: "false" + ingress.kubernetes.io/ssl-redirect: "true" + kubernetes.io/tls-acme: "false" + lagoon.sh/branch: main + lagoon.sh/version: v2.7.x + monitor.stakater.com/enabled: "true" + monitor.stakater.com/overridePath: /bypass-cache + nginx.ingress.kubernetes.io/ssl-redirect: "true" + uptimerobot.monitor.stakater.com/alert-contacts: alertcontact + uptimerobot.monitor.stakater.com/interval: "60" + uptimerobot.monitor.stakater.com/status-pages: statuspageid + creationTimestamp: null + labels: + app.kubernetes.io/instance: test1.example.com + app.kubernetes.io/managed-by: Helm + app.kubernetes.io/name: custom-ingress + dioscuri.amazee.io/migrate: "false" + helm.sh/chart: custom-ingress-0.1.0 + lagoon.sh/autogenerated: "false" + lagoon.sh/buildType: branch + lagoon.sh/environment: main + lagoon.sh/environmentType: production + lagoon.sh/project: example-project + lagoon.sh/service: test1.example.com + lagoon.sh/service-type: custom-ingress + name: test1.example.com +spec: + rules: + - host: test1.example.com + http: + paths: + - backend: + service: + name: nginx + port: + name: http + path: / + pathType: Prefix + tls: + - hosts: + - test1.example.com + secretName: test1.example.com-tls +status: + loadBalancer: {} diff --git a/test-resources/template-ingress/test12/docker-compose.yml b/test-resources/template-ingress/test12/docker-compose.yml new file mode 100644 index 00000000..85386270 --- /dev/null +++ b/test-resources/template-ingress/test12/docker-compose.yml @@ -0,0 +1,20 @@ +version: '2' +services: + node: + networks: + - amazeeio-network + - default + build: + context: . + dockerfile: node.dockerfile + labels: + lagoon.type: node + volumes: + - .:/app:delegated + environment: + - LAGOON_LOCALDEV_HTTP_PORT=3000 + - LAGOON_ROUTE=http://node.docker.amazee.io + +networks: + amazeeio-network: + external: true \ No newline at end of file diff --git a/test-resources/template-ingress/test12/lagoon.yml b/test-resources/template-ingress/test12/lagoon.yml new file mode 100644 index 00000000..9d44d43c --- /dev/null +++ b/test-resources/template-ingress/test12/lagoon.yml @@ -0,0 +1,10 @@ +docker-compose-yaml: ../test-resources/template-ingress/test1/docker-compose.yml + +environment_variables: + git_sha: "true" + +# environments: +# main: +# routes: +# - node: +# - example.com