From 8d3578052afe10f3b24aa1588b402ab8f85d2870 Mon Sep 17 00:00:00 2001 From: Ben Jackson Date: Thu, 28 Apr 2022 10:58:05 +1000 Subject: [PATCH] test: refactor marshaling --- cmd/template_routes.go | 15 +----- internal/lagoon/lagoon.go | 20 +++++++ internal/lagoon/lagoon_test.go | 53 +++++++++++-------- .../lagoon/test-resources/lagoon-booleans.yml | 2 +- .../lagoon-stringbooleans-combo.yml | 2 +- .../test-resources/lagoon-stringbooleans.yml | 2 +- 6 files changed, 57 insertions(+), 37 deletions(-) diff --git a/cmd/template_routes.go b/cmd/template_routes.go index ffc44d6e..e6b38c63 100644 --- a/cmd/template_routes.go +++ b/cmd/template_routes.go @@ -4,7 +4,6 @@ import ( "encoding/base64" "encoding/json" "fmt" - "os" "github.com/spf13/cobra" "github.com/uselagoon/build-deploy-tool/internal/helpers" @@ -100,19 +99,9 @@ var routeGeneration = &cobra.Command{ // read the .lagoon.yml file var lYAML lagoon.YAML - rawYAML, err := os.ReadFile(lagoonYml) - if err != nil { - return fmt.Errorf("couldn't read file %v: %v", lagoonYml, err) - } - err = yaml.Unmarshal(rawYAML, &lYAML) - if err != nil { - return fmt.Errorf("couldn't unmarshal %v: %v", lagoonYml, err) - } - // because lagoonyaml is not really good yaml, unmarshal polysite into an unknown struct to check lPolysite := make(map[string]interface{}) - err = yaml.Unmarshal(rawYAML, &lPolysite) - if err != nil { - return fmt.Errorf("couldn't unmarshal %v: %v", lagoonYml, err) + if err := lagoon.UnmarshalLagoonYAML(lagoonYml, &lYAML, &lPolysite); err != nil { + return fmt.Errorf("couldn't read file %v: %v", lagoonYml, err) } // get or generate the values file for generating route templates diff --git a/internal/lagoon/lagoon.go b/internal/lagoon/lagoon.go index 808fee47..751f1f7e 100644 --- a/internal/lagoon/lagoon.go +++ b/internal/lagoon/lagoon.go @@ -1,5 +1,12 @@ package lagoon +import ( + "fmt" + "os" + + "sigs.k8s.io/yaml" +) + // ProductionRoutes represents an active/standby configuration. type ProductionRoutes struct { Active *Environment `json:"active"` @@ -20,3 +27,16 @@ type YAML struct { Environments Environments `json:"environments"` ProductionRoutes *ProductionRoutes `json:"production_routes"` } + +// UnmarshalLagoonYAML unmarshal the lagoon.yml file into a YAML and map for consumption. +func UnmarshalLagoonYAML(file string, l *YAML, p *map[string]interface{}) error { + rawYAML, err := os.ReadFile(file) + if err != nil { + return fmt.Errorf("couldn't read %v: %v", file, err) + } + // lagoon.yml + yaml.Unmarshal(rawYAML, l) + // polysite + yaml.Unmarshal(rawYAML, p) + return nil +} diff --git a/internal/lagoon/lagoon_test.go b/internal/lagoon/lagoon_test.go index ce48b568..977617d6 100644 --- a/internal/lagoon/lagoon_test.go +++ b/internal/lagoon/lagoon_test.go @@ -1,28 +1,35 @@ package lagoon import ( - "fmt" - "os" "reflect" "testing" "github.com/uselagoon/build-deploy-tool/internal/helpers" - "sigs.k8s.io/yaml" ) -func TestLagoonYAMLUnmarshal(t *testing.T) { +func TestUnmarshalLagoonYAML(t *testing.T) { + type args struct { + file string + l *YAML + p *map[string]interface{} + } tests := []struct { - name string - yaml string - want *YAML + name string + args args + want *YAML + wantErr bool }{ { name: "test-booleans-represented-as-strings", - yaml: "test-resources/lagoon-stringbooleans.yml", + args: args{ + file: "test-resources/lagoon-stringbooleans.yml", + l: &YAML{}, + p: &map[string]interface{}{}, + }, want: &YAML{ DockerComposeYAML: "docker-compose.yml", Environments: Environments{ - "master": Environment{ + "main": Environment{ Routes: []map[string][]Route{ { "nginx": { @@ -82,11 +89,15 @@ func TestLagoonYAMLUnmarshal(t *testing.T) { }, { name: "test-booleans-represented-as-booleans", - yaml: "test-resources/lagoon-booleans.yml", + args: args{ + file: "test-resources/lagoon-booleans.yml", + l: &YAML{}, + p: &map[string]interface{}{}, + }, want: &YAML{ DockerComposeYAML: "docker-compose.yml", Environments: Environments{ - "master": Environment{ + "main": Environment{ Routes: []map[string][]Route{ { "nginx": { @@ -146,11 +157,15 @@ func TestLagoonYAMLUnmarshal(t *testing.T) { }, { name: "test-booleans-represented-as-strings-and-booleans", - yaml: "test-resources/lagoon-stringbooleans-combo.yml", + args: args{ + file: "test-resources/lagoon-stringbooleans-combo.yml", + l: &YAML{}, + p: &map[string]interface{}{}, + }, want: &YAML{ DockerComposeYAML: "docker-compose.yml", Environments: Environments{ - "master": Environment{ + "main": Environment{ Routes: []map[string][]Route{ { "nginx": { @@ -209,17 +224,13 @@ func TestLagoonYAMLUnmarshal(t *testing.T) { }, }, } - for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - rawYAML, err := os.ReadFile(tt.yaml) - if err != nil { - panic(fmt.Errorf("couldn't read %v: %v", tt.yaml, err)) + if err := UnmarshalLagoonYAML(tt.args.file, tt.args.l, tt.args.p); (err != nil) != tt.wantErr { + t.Errorf("UnmarshalLagoonYAML() error = %v, wantErr %v", err, tt.wantErr) } - got := &YAML{} - yaml.Unmarshal(rawYAML, got) - if !reflect.DeepEqual(got, tt.want) { - t.Errorf("Unmarshal() = got %v, want %v", got, tt.want) + if !reflect.DeepEqual(tt.args.l, tt.want) { + t.Errorf("Unmarshal() = got %v, want %v", tt.args.l, tt.want) } }) } diff --git a/internal/lagoon/test-resources/lagoon-booleans.yml b/internal/lagoon/test-resources/lagoon-booleans.yml index bb9a45c4..58036ca3 100644 --- a/internal/lagoon/test-resources/lagoon-booleans.yml +++ b/internal/lagoon/test-resources/lagoon-booleans.yml @@ -1,6 +1,6 @@ docker-compose-yaml: docker-compose.yml environments: - master: + main: routes: - nginx: - a.example.com: diff --git a/internal/lagoon/test-resources/lagoon-stringbooleans-combo.yml b/internal/lagoon/test-resources/lagoon-stringbooleans-combo.yml index 01d43a75..4ec7f97e 100644 --- a/internal/lagoon/test-resources/lagoon-stringbooleans-combo.yml +++ b/internal/lagoon/test-resources/lagoon-stringbooleans-combo.yml @@ -1,6 +1,6 @@ docker-compose-yaml: docker-compose.yml environments: - master: + main: routes: - nginx: - a.example.com: diff --git a/internal/lagoon/test-resources/lagoon-stringbooleans.yml b/internal/lagoon/test-resources/lagoon-stringbooleans.yml index e1e8bc3e..30819988 100644 --- a/internal/lagoon/test-resources/lagoon-stringbooleans.yml +++ b/internal/lagoon/test-resources/lagoon-stringbooleans.yml @@ -1,6 +1,6 @@ docker-compose-yaml: docker-compose.yml environments: - master: + main: routes: - nginx: - a.example.com: