From 355c2ca0de0e9489c7365d5707aaddf31d35e844 Mon Sep 17 00:00:00 2001 From: Ben Jackson Date: Fri, 20 May 2022 09:44:01 +1000 Subject: [PATCH 1/2] test: add a test for autogenerated prefixes --- cmd/identify_ingress.go | 18 ++++++++++++ cmd/identify_ingress_test.go | 28 +++++++++++++++++++ .../template-ingress/prefixes-lagoon.yml | 18 ++++++++++++ 3 files changed, 64 insertions(+) create mode 100644 cmd/test-resources/template-ingress/prefixes-lagoon.yml diff --git a/cmd/identify_ingress.go b/cmd/identify_ingress.go index 82f41e2d..f84b846c 100644 --- a/cmd/identify_ingress.go +++ b/cmd/identify_ingress.go @@ -65,6 +65,12 @@ func generateRoutes(lagoonEnvVars []lagoon.EnvironmentVariable, for i := 1; i <= rangeVal; i++ { remainders = append(remainders, fmt.Sprintf("%s%s", prefix, autogenRoutes.Routes[i].Domain)) autogen = append(autogen, fmt.Sprintf("%s%s", prefix, autogenRoutes.Routes[i].Domain)) + for a := 0; a < len(autogenRoutes.Routes[i].AlternativeNames); a++ { + autogen = append(autogen, fmt.Sprintf("%s%s", prefix, autogenRoutes.Routes[i].AlternativeNames[a])) + } + } + for a := 0; a < len(autogenRoutes.Routes[0].AlternativeNames); a++ { + autogen = append(autogen, fmt.Sprintf("%s%s", prefix, autogenRoutes.Routes[0].AlternativeNames[a])) } primary = fmt.Sprintf("%s%s", prefix, autogenRoutes.Routes[0].Domain) } @@ -83,6 +89,12 @@ func generateRoutes(lagoonEnvVars []lagoon.EnvironmentVariable, rangeVal := len(mainRoutes.Routes) - 1 for i := 1; i <= rangeVal; i++ { remainders = append(remainders, fmt.Sprintf("%s%s", prefix, mainRoutes.Routes[i].Domain)) + for a := 0; a < len(mainRoutes.Routes[i].AlternativeNames); a++ { + remainders = append(autogen, fmt.Sprintf("%s%s", prefix, mainRoutes.Routes[i].AlternativeNames[a])) + } + } + for a := 0; a < len(mainRoutes.Routes[0].AlternativeNames); a++ { + remainders = append(autogen, fmt.Sprintf("%s%s", prefix, mainRoutes.Routes[0].AlternativeNames[a])) } primary = fmt.Sprintf("%s%s", prefix, mainRoutes.Routes[0].Domain) } @@ -101,6 +113,12 @@ func generateRoutes(lagoonEnvVars []lagoon.EnvironmentVariable, rangeVal := len(activeStanbyRoutes.Routes) - 1 for i := 1; i <= rangeVal; i++ { remainders = append(remainders, fmt.Sprintf("%s%s", prefix, activeStanbyRoutes.Routes[i].Domain)) + for a := 0; a < len(activeStanbyRoutes.Routes[i].AlternativeNames); a++ { + remainders = append(autogen, fmt.Sprintf("%s%s", prefix, activeStanbyRoutes.Routes[i].AlternativeNames[a])) + } + } + for a := 0; a < len(activeStanbyRoutes.Routes[0].AlternativeNames); a++ { + remainders = append(autogen, fmt.Sprintf("%s%s", prefix, activeStanbyRoutes.Routes[0].AlternativeNames[a])) } primary = fmt.Sprintf("%s%s", prefix, activeStanbyRoutes.Routes[0].Domain) } diff --git a/cmd/identify_ingress_test.go b/cmd/identify_ingress_test.go index b1e5e098..cd0b690b 100644 --- a/cmd/identify_ingress_test.go +++ b/cmd/identify_ingress_test.go @@ -271,6 +271,34 @@ func TestIdentifyRoute(t *testing.T) { wantRemain: []string{}, wantautoGen: []string{"https://node-example-project-no-ingress.example.com"}, }, + { + name: "test12 no custom ingress", + args: args{ + alertContact: "alertcontact", + statusPageID: "statuspageid", + projectName: "example-project", + environmentName: "no-ingress", + environmentType: "production", + buildType: "branch", + standbyEnvironment: "no-ingress", + lagoonVersion: "v2.7.x", + branch: "main2", + projectVars: `[{"name":"LAGOON_SYSTEM_ROUTER_PATTERN","value":"${service}-${project}-${environment}.example.com","scope":"internal_system"}]`, + envVars: `[]`, + secretPrefix: "fastly-api-", + lagoonYAML: "test-resources/template-ingress/prefixes-lagoon.yml", + templatePath: "test-resources/template-ingress/output", + }, + want: "https://node-example-project-no-ingress.example.com", + wantRemain: []string{}, + wantautoGen: []string{ + "https://node-example-project-no-ingress.example.com", + "https://www.node-example-project-no-ingress.example.com", + "https://en.node-example-project-no-ingress.example.com", + "https://de.node-example-project-no-ingress.example.com", + "https://fi.node-example-project-no-ingress.example.com", + }, + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { diff --git a/cmd/test-resources/template-ingress/prefixes-lagoon.yml b/cmd/test-resources/template-ingress/prefixes-lagoon.yml new file mode 100644 index 00000000..43c15455 --- /dev/null +++ b/cmd/test-resources/template-ingress/prefixes-lagoon.yml @@ -0,0 +1,18 @@ +docker-compose-yaml: test-resources/template-ingress/docker-compose.yml + +routes: + autogenerate: + prefixes: + - www + - en + - de + - fi + +environment_variables: + git_sha: "true" + +environments: + main: + routes: + - node: + - example.com From 48b4004c7f3a30adf44a5f2aa44643fc9258522c Mon Sep 17 00:00:00 2001 From: Ben Jackson Date: Fri, 20 May 2022 10:04:04 +1000 Subject: [PATCH 2/2] refactor: alternative names checking --- cmd/identify_ingress.go | 45 +++++++++++++++++------------------- cmd/identify_ingress_test.go | 9 ++++++-- 2 files changed, 28 insertions(+), 26 deletions(-) diff --git a/cmd/identify_ingress.go b/cmd/identify_ingress.go index f84b846c..1a69b64c 100644 --- a/cmd/identify_ingress.go +++ b/cmd/identify_ingress.go @@ -60,19 +60,18 @@ func generateRoutes(lagoonEnvVars []lagoon.EnvironmentVariable, } // get the first route from the list of routes if len(autogenRoutes.Routes) > 0 { - rangeVal := len(autogenRoutes.Routes) - 1 - autogen = append(autogen, fmt.Sprintf("%s%s", prefix, autogenRoutes.Routes[0].Domain)) - for i := 1; i <= rangeVal; i++ { - remainders = append(remainders, fmt.Sprintf("%s%s", prefix, autogenRoutes.Routes[i].Domain)) + for i := 0; i < len(autogenRoutes.Routes); i++ { autogen = append(autogen, fmt.Sprintf("%s%s", prefix, autogenRoutes.Routes[i].Domain)) + if i == 0 { + primary = fmt.Sprintf("%s%s", prefix, autogenRoutes.Routes[i].Domain) + } else { + remainders = append(remainders, fmt.Sprintf("%s%s", prefix, autogenRoutes.Routes[i].Domain)) + } for a := 0; a < len(autogenRoutes.Routes[i].AlternativeNames); a++ { + remainders = append(remainders, fmt.Sprintf("%s%s", prefix, autogenRoutes.Routes[i].AlternativeNames[a])) autogen = append(autogen, fmt.Sprintf("%s%s", prefix, autogenRoutes.Routes[i].AlternativeNames[a])) } } - for a := 0; a < len(autogenRoutes.Routes[0].AlternativeNames); a++ { - autogen = append(autogen, fmt.Sprintf("%s%s", prefix, autogenRoutes.Routes[0].AlternativeNames[a])) - } - primary = fmt.Sprintf("%s%s", prefix, autogenRoutes.Routes[0].Domain) } // handle routes from the .lagoon.yml and the API specifically @@ -86,17 +85,16 @@ func generateRoutes(lagoonEnvVars []lagoon.EnvironmentVariable, if primary != "" { remainders = append(remainders, primary) } - rangeVal := len(mainRoutes.Routes) - 1 - for i := 1; i <= rangeVal; i++ { - remainders = append(remainders, fmt.Sprintf("%s%s", prefix, mainRoutes.Routes[i].Domain)) + for i := 0; i < len(mainRoutes.Routes); i++ { + if i == 0 { + primary = fmt.Sprintf("%s%s", prefix, mainRoutes.Routes[i].Domain) + } else { + remainders = append(remainders, fmt.Sprintf("%s%s", prefix, mainRoutes.Routes[i].Domain)) + } for a := 0; a < len(mainRoutes.Routes[i].AlternativeNames); a++ { - remainders = append(autogen, fmt.Sprintf("%s%s", prefix, mainRoutes.Routes[i].AlternativeNames[a])) + remainders = append(remainders, fmt.Sprintf("%s%s", prefix, mainRoutes.Routes[i].AlternativeNames[a])) } } - for a := 0; a < len(mainRoutes.Routes[0].AlternativeNames); a++ { - remainders = append(autogen, fmt.Sprintf("%s%s", prefix, mainRoutes.Routes[0].AlternativeNames[a])) - } - primary = fmt.Sprintf("%s%s", prefix, mainRoutes.Routes[0].Domain) } if activeEnv || standbyEnv { @@ -110,17 +108,16 @@ func generateRoutes(lagoonEnvVars []lagoon.EnvironmentVariable, if primary != "" { remainders = append(remainders, primary) } - rangeVal := len(activeStanbyRoutes.Routes) - 1 - for i := 1; i <= rangeVal; i++ { - remainders = append(remainders, fmt.Sprintf("%s%s", prefix, activeStanbyRoutes.Routes[i].Domain)) + for i := 0; i < len(activeStanbyRoutes.Routes); i++ { + if i == 0 { + primary = fmt.Sprintf("%s%s", prefix, activeStanbyRoutes.Routes[i].Domain) + } else { + remainders = append(remainders, fmt.Sprintf("%s%s", prefix, activeStanbyRoutes.Routes[i].Domain)) + } for a := 0; a < len(activeStanbyRoutes.Routes[i].AlternativeNames); a++ { - remainders = append(autogen, fmt.Sprintf("%s%s", prefix, activeStanbyRoutes.Routes[i].AlternativeNames[a])) + remainders = append(remainders, fmt.Sprintf("%s%s", prefix, activeStanbyRoutes.Routes[i].AlternativeNames[a])) } } - for a := 0; a < len(activeStanbyRoutes.Routes[0].AlternativeNames); a++ { - remainders = append(autogen, fmt.Sprintf("%s%s", prefix, activeStanbyRoutes.Routes[0].AlternativeNames[a])) - } - primary = fmt.Sprintf("%s%s", prefix, activeStanbyRoutes.Routes[0].Domain) } } diff --git a/cmd/identify_ingress_test.go b/cmd/identify_ingress_test.go index cd0b690b..a62f1f99 100644 --- a/cmd/identify_ingress_test.go +++ b/cmd/identify_ingress_test.go @@ -289,8 +289,13 @@ func TestIdentifyRoute(t *testing.T) { lagoonYAML: "test-resources/template-ingress/prefixes-lagoon.yml", templatePath: "test-resources/template-ingress/output", }, - want: "https://node-example-project-no-ingress.example.com", - wantRemain: []string{}, + want: "https://node-example-project-no-ingress.example.com", + wantRemain: []string{ + "https://www.node-example-project-no-ingress.example.com", + "https://en.node-example-project-no-ingress.example.com", + "https://de.node-example-project-no-ingress.example.com", + "https://fi.node-example-project-no-ingress.example.com", + }, wantautoGen: []string{ "https://node-example-project-no-ingress.example.com", "https://www.node-example-project-no-ingress.example.com",