Skip to content

Commit

Permalink
test: refactor marshaling
Browse files Browse the repository at this point in the history
  • Loading branch information
shreddedbacon committed Apr 28, 2022
1 parent 35ff304 commit 8d35780
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 37 deletions.
15 changes: 2 additions & 13 deletions cmd/template_routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"encoding/base64"
"encoding/json"
"fmt"
"os"

"github.com/spf13/cobra"
"github.com/uselagoon/build-deploy-tool/internal/helpers"
Expand Down Expand Up @@ -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
Expand Down
20 changes: 20 additions & 0 deletions internal/lagoon/lagoon.go
Original file line number Diff line number Diff line change
@@ -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"`
Expand All @@ -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
}
53 changes: 32 additions & 21 deletions internal/lagoon/lagoon_test.go
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down Expand Up @@ -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": {
Expand Down Expand Up @@ -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": {
Expand Down Expand Up @@ -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)
}
})
}
Expand Down
2 changes: 1 addition & 1 deletion internal/lagoon/test-resources/lagoon-booleans.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
docker-compose-yaml: docker-compose.yml
environments:
master:
main:
routes:
- nginx:
- a.example.com:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
docker-compose-yaml: docker-compose.yml
environments:
master:
main:
routes:
- nginx:
- a.example.com:
Expand Down
2 changes: 1 addition & 1 deletion internal/lagoon/test-resources/lagoon-stringbooleans.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
docker-compose-yaml: docker-compose.yml
environments:
master:
main:
routes:
- nginx:
- a.example.com:
Expand Down

0 comments on commit 8d35780

Please sign in to comment.