-
Notifications
You must be signed in to change notification settings - Fork 46
Expand file tree
/
Copy pathapp_test.go
More file actions
240 lines (209 loc) · 5.88 KB
/
Copy pathapp_test.go
File metadata and controls
240 lines (209 loc) · 5.88 KB
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
// Copyright 2025 Tetrate
// SPDX-License-Identifier: Apache-2.0
package cmd_test
import (
"bytes"
"io"
"os"
"os/user"
"path/filepath"
"testing"
"github.com/stretchr/testify/require"
"github.com/urfave/cli/v2"
rootcmd "github.com/tetratelabs/func-e/internal/cmd"
"github.com/tetratelabs/func-e/internal/envoy"
"github.com/tetratelabs/func-e/internal/globals"
"github.com/tetratelabs/func-e/internal/test"
"github.com/tetratelabs/func-e/internal/version"
)
func TestFuncEValidateArgs(t *testing.T) {
tests := []struct {
name string
args []string
expectedErr string
}{
{
name: "--envoy-versions-url not a URL",
args: []string{"func-e", "--envoy-versions-url", "/not/url"},
expectedErr: `"/not/url" is not a valid Envoy versions URL`,
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
err := runTestCommand(t, &globals.GlobalOpts{}, tc.args)
require.EqualError(t, err, tc.expectedErr)
})
}
}
func TestHomeDir(t *testing.T) {
type testCase struct {
name string
args []string
setup func()
expected string
}
u, err := user.Current()
require.NoError(t, err)
alt1 := filepath.Join(u.HomeDir, "alt1")
alt2 := filepath.Join(u.HomeDir, "alt2")
tests := []testCase{ // we don't test default as that depends on the runtime env
{
name: "default is ~/.func-e",
args: []string{"func-e"},
expected: filepath.Join(u.HomeDir, ".func-e"),
},
{
name: "FUNC_E_HOME env",
args: []string{"func-e"},
setup: func() {
t.Setenv("FUNC_E_HOME", alt1)
},
expected: alt1,
},
{
name: "--home-dir arg",
args: []string{"func-e", "--home-dir", alt1},
expected: alt1,
},
{
name: "prioritizes --home-dir arg over FUNC_E_HOME env",
args: []string{"func-e", "--home-dir", alt1},
setup: func() {
t.Setenv("FUNC_E_HOME", alt2)
},
expected: alt1,
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
if tc.setup != nil {
tc.setup()
}
o := &globals.GlobalOpts{}
err := runTestCommand(t, o, tc.args)
require.NoError(t, err)
require.Equal(t, tc.expected, o.HomeDir)
})
}
}
func TestPlatformArg(t *testing.T) {
type testCase struct {
name string
args []string
setup func()
expected version.Platform
}
tests := []testCase{
{
name: "FUNC_E_PLATFORM env",
args: []string{"func-e"},
setup: func() {
t.Setenv("FUNC_E_PLATFORM", "linux/amd64")
},
expected: version.Platform("linux/amd64"),
},
{
name: "--platform flag",
args: []string{"func-e", "--platform", "darwin/amd64"},
expected: version.Platform("darwin/amd64"),
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
if tc.setup != nil {
tc.setup()
}
o := &globals.GlobalOpts{}
err := runTestCommand(t, o, tc.args)
require.NoError(t, err)
require.Equal(t, tc.expected, o.Platform)
})
}
}
func TestEnvoyVersionsURL(t *testing.T) {
type testCase struct {
name string
args []string
setup func()
expected string
}
tests := []testCase{ // we don't test default as that depends on the runtime env
{
name: "default is https://archive.tetratelabs.io/envoy/envoy-versions.json",
args: []string{"func-e"},
expected: "https://archive.tetratelabs.io/envoy/envoy-versions.json",
},
{
name: "ENVOY_VERSIONS_URL env",
args: []string{"func-e"},
setup: func() {
t.Setenv("ENVOY_VERSIONS_URL", "http://ENVOY_VERSIONS_URL/env")
},
expected: "http://ENVOY_VERSIONS_URL/env",
},
{
name: "--envoy-versions-url flag",
args: []string{"func-e", "--envoy-versions-url", "http://versions/arg"},
expected: "http://versions/arg",
},
{
name: "prioritizes --envoy-versions-url arg over ENVOY_VERSIONS_URL env",
args: []string{"func-e", "--envoy-versions-url", "http://versions/arg"},
setup: func() {
t.Setenv("ENVOY_VERSIONS_URL", "http://ENVOY_VERSIONS_URL/env")
},
expected: "http://versions/arg",
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
if tc.setup != nil {
tc.setup()
}
o := &globals.GlobalOpts{}
err := runTestCommand(t, o, tc.args)
require.NoError(t, err)
require.Equal(t, tc.expected, o.EnvoyVersionsURL)
})
}
}
// newApp initializes a command with buffers for stdout and stderr.
func newApp(o *globals.GlobalOpts) (c *cli.App, stdout, stderr *bytes.Buffer) {
stdout = new(bytes.Buffer)
stderr = new(bytes.Buffer)
c = rootcmd.NewApp(o)
c.Name = "func-e"
c.Writer = stdout
c.ErrWriter = stderr
o.Out = stdout
return c, stdout, stderr
}
func runTestCommand(t *testing.T, o *globals.GlobalOpts, args []string) error {
c, stdout, stderr := newApp(o)
c.Commands = append(c.Commands, &cli.Command{Name: "test", Action: func(_ *cli.Context) error {
return nil
}})
err := c.Run(append(args, "test"))
// Main handles logging of errors, so we expect nothing in stdout or stderr even in error case
require.Empty(t, stdout)
require.Empty(t, stderr)
return err
}
// setupTest returns globals.GlobalOpts and a tear-down function.
// The tear-down functions reverts side-effects such as temp directories and a fake Envoy versions server.
func setupTest(t *testing.T) *globals.GlobalOpts {
result := globals.GlobalOpts{}
result.EnvoyVersion = version.LastKnownEnvoy
result.Out = io.Discard // ignore logging by default
result.EnvoyOut = io.Discard
result.EnvoyErr = io.Discard
tempDir := t.TempDir()
result.HomeDir = filepath.Join(tempDir, "envoy_home")
err := os.Mkdir(result.HomeDir, 0o700)
require.NoError(t, err, `error creating directory: %s`, result.HomeDir)
versionsServer := test.RequireEnvoyVersionsTestServer(t, version.LastKnownEnvoy)
result.EnvoyVersionsURL = versionsServer.URL + "/envoy-versions.json"
result.GetEnvoyVersions = envoy.NewGetVersions(result.EnvoyVersionsURL, result.Platform, result.Version)
t.Cleanup(versionsServer.Close)
return &result
}