-
Notifications
You must be signed in to change notification settings - Fork 1
/
squadron_test.go
137 lines (119 loc) · 3.26 KB
/
squadron_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package squadron_test
import (
"context"
"encoding/json"
"errors"
"os"
"path"
"testing"
testingx "github.com/foomo/go/testing"
tagx "github.com/foomo/go/testing/tag"
"github.com/foomo/squadron"
"github.com/foomo/squadron/internal/config"
"github.com/foomo/squadron/internal/testutils"
"github.com/foomo/squadron/internal/util"
"github.com/invopop/jsonschema"
"github.com/pterm/pterm"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestConfig(t *testing.T) {
t.Parallel()
testingx.Tags(t, tagx.Short)
cwd, err := os.Getwd()
require.NoError(t, err)
reflector := new(jsonschema.Reflector)
require.NoError(t, reflector.AddGoComments("github.com/foomo/squadron", "./"))
schema := reflector.Reflect(&config.Config{})
actual, err := json.MarshalIndent(schema, "", " ")
require.NoError(t, err)
filename := path.Join(cwd, "squadron.schema.json")
expected, err := os.ReadFile(filename)
if !errors.Is(err, os.ErrNotExist) {
require.NoError(t, err)
}
if !assert.Equal(t, string(expected), string(actual)) {
require.NoError(t, os.WriteFile(filename, actual, 0600))
}
}
func TestConfigSimpleSnapshot(t *testing.T) {
testingx.Tags(t, tagx.Short)
pterm.EnableDebugMessages()
t.Setenv("PROJECT_ROOT", ".")
tests := []struct {
name string
files []string
squadron string
units []string
tags []string
}{
{
name: "blank",
files: []string{"squadron.yaml"},
},
{
name: "simple",
files: []string{"squadron.yaml"},
},
{
name: "extends",
files: []string{"squadron.yaml"},
},
{
name: "override",
files: []string{"squadron.yaml", "squadron.override.yaml"},
},
{
name: "global",
files: []string{"squadron.yaml", "squadron.override.yaml"},
},
{
name: "vars",
files: []string{"squadron.yaml", "squadron.override.yaml"},
},
{
name: "template",
files: []string{"squadron.yaml"},
},
{
name: "tags",
tags: []string{"backend", "-skip"},
files: []string{"squadron.yaml"},
},
}
for _, test := range tests {
t.Run(test.name, func(tt *testing.T) {
runTestConfig(tt, test.name, test.files, test.squadron, test.units, test.tags)
})
}
}
func runTestConfig(t *testing.T, name string, files []string, squadronName string, unitNames, tags []string) {
t.Helper()
var cwd string
ctx := context.TODO()
require.NoError(t, util.ValidatePath(".", &cwd))
for i, file := range files {
files[i] = path.Join("testdata", name, file)
}
sq := squadron.New(cwd, "default", files)
{
require.NoError(t, sq.MergeConfigFiles(ctx), "failed to merge files")
}
{
require.NoError(t, sq.FilterConfig(ctx, squadronName, unitNames, tags), "failed to filter config")
testutils.Snapshot(t, path.Join("testdata", name, "snapshop-config-norender.yaml"), sq.ConfigYAML())
}
{
require.NoError(t, sq.RenderConfig(ctx), "failed to render config")
testutils.Snapshot(t, path.Join("testdata", name, "snapshop-config.yaml"), sq.ConfigYAML())
}
{
require.NoError(t, sq.RenderConfig(ctx), "failed to render config")
testutils.Snapshot(t, path.Join("testdata", name, "snapshop-config.yaml"), sq.ConfigYAML())
}
{
out, err := sq.Template(ctx, nil, 1)
require.NoError(t, err)
testutils.Snapshot(t, path.Join("testdata", name, "snapshop-template.yaml"), out)
}
}