-
Notifications
You must be signed in to change notification settings - Fork 197
/
exec_test.go
188 lines (155 loc) · 5.32 KB
/
exec_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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package exec
import (
"bytes"
"context"
"fmt"
"os"
"sort"
"testing"
"get.porter.sh/porter/pkg/exec/builder"
"get.porter.sh/porter/pkg/test"
"get.porter.sh/porter/pkg/yaml"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestAction_UnmarshalYAML(t *testing.T) {
b, err := os.ReadFile("testdata/install-input.yaml")
require.NoError(t, err)
action := Action{}
err = yaml.Unmarshal(b, &action)
require.NoError(t, err)
require.Len(t, action.Steps, 1)
step := action.Steps[0]
assert.Equal(t, "gcloud", step.Command)
assert.Equal(t, "Install a VM and collect its ID", step.Description)
require.Len(t, step.Arguments, 3)
assert.Len(t, step.Flags, 3)
sort.Sort(step.Flags) // This returns the flags in ascending order
assert.Equal(t, builder.NewFlag("machine-type", "f1-micro"), step.Flags[0])
assert.Equal(t, builder.NewFlag("project", "porterci"), step.Flags[1])
assert.Equal(t, builder.NewFlag("zone", "us-central1-a"), step.Flags[2])
assert.Equal(t, step.EnvironmentVars, map[string]string{"SECRET": "super-secret"})
require.Len(t, step.Outputs, 1)
assert.Equal(t, Output{Name: "vms", JsonPath: "$[*].id"}, step.Outputs[0])
}
func TestMixin_ExecuteCommand(t *testing.T) {
ctx := context.Background()
step := Step{
Instruction: Instruction{
Command: "bash",
Arguments: []string{"-c", "echo Hello World"},
},
}
action := Action{
Name: "install",
Steps: []Step{step},
}
b, _ := yaml.Marshal(action)
fmt.Println(string(b))
m := NewTestMixin(t)
m.Config.In = bytes.NewReader(b)
m.Config.Setenv(test.ExpectedCommandEnv, `bash -c echo Hello World`)
err := m.Execute(ctx, ExecuteOptions{})
require.NoError(t, err)
}
func TestMixin_ErrorHandling(t *testing.T) {
testcases := []struct {
name string
handler builder.IgnoreErrorHandler
wantError string
}{
{name: "legit error", handler: builder.IgnoreErrorHandler{}, wantError: "error running command"},
{name: "all", handler: builder.IgnoreErrorHandler{All: true}},
{name: "exit code", handler: builder.IgnoreErrorHandler{ExitCodes: []int{1}}},
{name: "contains", handler: builder.IgnoreErrorHandler{Output: builder.IgnoreErrorWithOutput{Contains: []string{"already exists"}}}},
{name: "regex", handler: builder.IgnoreErrorHandler{Output: builder.IgnoreErrorWithOutput{Regex: []string{".* exists"}}}},
}
for _, tc := range testcases {
t.Run(tc.name, func(t *testing.T) {
ctx := context.Background()
step := Step{
Instruction: Instruction{
Command: "bash",
Arguments: []string{"-c", "echo Hello World"},
IgnoreErrorHandler: tc.handler,
},
}
action := Action{
Name: "install",
Steps: []Step{step},
}
b, _ := yaml.Marshal(action)
m := NewTestMixin(t)
m.Config.In = bytes.NewReader(b)
m.Config.Setenv(test.ExpectedCommandEnv, `bash -c echo Hello World`)
m.Config.Setenv(test.ExpectedCommandExitCodeEnv, "1")
m.Config.Setenv(test.ExpectedCommandErrorEnv, "thing already exists")
err := m.Execute(ctx, ExecuteOptions{})
if tc.wantError == "" {
require.NoError(t, err)
} else {
require.Error(t, err)
assert.Contains(t, err.Error(), tc.wantError)
}
})
}
}
func TestMixin_Install(t *testing.T) {
ctx := context.Background()
h := NewTestMixin(t)
h.TestConfig.TestContext.AddTestDirectory("testdata", "testdata")
action, err := h.loadAction(ctx, "testdata/install-input.yaml")
require.NoError(t, err)
assert.Len(t, action.Steps, 1)
step := action.Steps[0]
assert.Equal(t, "gcloud", step.Instruction.Command)
require.Len(t, step.Outputs, 1)
assert.Equal(t, "$[*].id", step.Outputs[0].JsonPath)
}
func TestMixin_Upgrade(t *testing.T) {
ctx := context.Background()
h := NewTestMixin(t)
h.TestConfig.TestContext.AddTestDirectory("testdata", "testdata")
action, err := h.loadAction(ctx, "testdata/upgrade-input.yaml")
require.NoError(t, err)
assert.Len(t, action.Steps, 1)
step := action.Steps[0]
assert.Equal(t, "bash", step.Instruction.Command)
require.Len(t, step.Outputs, 1)
assert.Equal(t, "config/kube.yaml", step.Outputs[0].FilePath)
}
func TestMixin_CustomAction(t *testing.T) {
ctx := context.Background()
h := NewTestMixin(t)
h.TestConfig.TestContext.AddTestDirectory("testdata", "testdata")
action, err := h.loadAction(ctx, "testdata/invoke-input.yaml")
require.NoError(t, err)
assert.Len(t, action.Steps, 1)
step := action.Steps[0]
assert.Equal(t, "bash", step.Instruction.Command)
require.Len(t, step.Outputs, 1)
assert.Equal(t, "Hello (.*)", step.Outputs[0].Regex)
}
func TestMixin_Uninstall(t *testing.T) {
ctx := context.Background()
h := NewTestMixin(t)
h.TestConfig.TestContext.AddTestDirectory("testdata", "testdata")
action, err := h.loadAction(ctx, "testdata/uninstall-input.yaml")
require.NoError(t, err)
assert.Len(t, action.Steps, 1)
step := action.Steps[0]
assert.Equal(t, "bash", step.Instruction.Command)
}
func TestMixin_SuffixArgs(t *testing.T) {
ctx := context.Background()
b, err := os.ReadFile("testdata/suffix-args-input.yaml")
require.NoError(t, err, "ReadFile failed")
var action Action
err = yaml.Unmarshal(b, &action)
require.NoError(t, err, "Unmarshal failed")
m := NewTestMixin(t)
m.Config.In = bytes.NewReader(b)
m.Config.Setenv(test.ExpectedCommandEnv, `docker build --tag getporter/porter-hello:latest .`)
err = m.Execute(ctx, ExecuteOptions{})
require.NoError(t, err)
}