-
Notifications
You must be signed in to change notification settings - Fork 95
/
nerdctl_config_applier_test.go
250 lines (226 loc) · 7.38 KB
/
nerdctl_config_applier_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
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
241
242
243
244
245
246
247
248
249
250
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
package config
import (
"errors"
"fmt"
"io/fs"
"testing"
"github.com/golang/mock/gomock"
"github.com/spf13/afero"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/runfinch/finch/pkg/mocks"
)
// disclaimer: this key has never been and should never be used for anything
var fakeSSHKey = `
-----BEGIN OPENSSH PRIVATE KEY-----
b3BlbnNzaC1rZXktdjEAAAAABG5vbmUAAAAEbm9uZQAAAAAAAAABAAAAMwAAAAtzc2gtZW
QyNTUxOQAAACAfR367TtAGV+abvj4pRDcFdU2enKE+iC4qF3LNJF9eyQAAAKjEIxhXxCMY
VwAAAAtzc2gtZWQyNTUxOQAAACAfR367TtAGV+abvj4pRDcFdU2enKE+iC4qF3LNJF9eyQ
AAAEANzWA32dcyDqkfg7zbzt7D76PTyyaX0n1/goKJNPLYyB9HfrtO0AZX5pu+PilENwV1
TZ6coT6ILioXcs0kX17JAAAAI2FsdmFqdXNAODg2NjVhMGJmN2NhLmFudC5hbWF6b24uY2
9tAQI=
-----END OPENSSH PRIVATE KEY-----`
func Test_updateEnvironment(t *testing.T) {
t.Parallel()
testCases := []struct {
name string
user string
mockSvc func(t *testing.T, fs afero.Fs)
postRunCheck func(t *testing.T, fs afero.Fs)
want error
}{
{
name: "happy path",
user: "mock_user",
mockSvc: func(t *testing.T, fs afero.Fs) {
require.NoError(t, afero.WriteFile(fs, "/home/mock_user.linux/.bashrc", []byte(""), 0o644))
},
postRunCheck: func(t *testing.T, fs afero.Fs) {
fileBytes, err := afero.ReadFile(fs, "/home/mock_user.linux/.bashrc")
require.NoError(t, err)
assert.Equal(t,
[]byte("\nexport DOCKER_CONFIG=\"/Users/mock_user/.finch\""+
"\n[ -L /usr/local/bin/docker-credential-ecr-login ] || sudo ln -s "+
"/Users/mock_user/.finch/cred-helpers/docker-credential-ecr-login /usr/local/bin/"+
"\n"+"[ -L /root/.aws ] || sudo ln -fs /Users/mock_user/.aws /root/.aws"), fileBytes)
},
want: nil,
},
{
name: "happy path, file already exists and already contains expected variables",
user: "mock_user",
mockSvc: func(t *testing.T, fs afero.Fs) {
require.NoError(
t,
afero.WriteFile(
fs,
"/home/mock_user.linux/.bashrc",
[]byte("export DOCKER_CONFIG=\"/Users/mock_user/.finch\""+"\n"+"[ -L /usr/local/bin/docker-credential-ecr-login ] "+
"|| sudo ln -s /Users/mock_user/.finch/cred-helpers/docker-credential-ecr-login /usr/local/bin/"+
"\n"+"[ -L /root/.aws ] || sudo ln -fs /Users/mock_user/.aws /root/.aws"),
0o644,
),
)
},
postRunCheck: func(t *testing.T, fs afero.Fs) {
fileBytes, err := afero.ReadFile(fs, "/home/mock_user.linux/.bashrc")
require.NoError(t, err)
assert.Equal(t, []byte(`export DOCKER_CONFIG="/Users/mock_user/.finch"`+"\n"+
"[ -L /usr/local/bin/docker-credential-ecr-login ] "+
"|| sudo ln -s /Users/mock_user/.finch/cred-helpers/docker-credential-ecr-login /usr/local/bin/"+
"\n"+"[ -L /root/.aws ] || sudo ln -fs /Users/mock_user/.aws /root/.aws"), fileBytes)
},
want: nil,
},
{
name: ".bashrc file doesn't exist",
user: "mock_user",
mockSvc: func(t *testing.T, fs afero.Fs) {},
postRunCheck: func(t *testing.T, fs afero.Fs) {},
want: fmt.Errorf(
"failed to read config file: %w",
&fs.PathError{Op: "open", Path: "/home/mock_user.linux/.bashrc", Err: errors.New("file does not exist")},
),
},
}
for _, tc := range testCases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
fs := afero.NewMemMapFs()
tc.mockSvc(t, fs)
got := updateEnvironment(fs, tc.user)
require.Equal(t, tc.want, got)
tc.postRunCheck(t, fs)
})
}
}
func Test_updateNerdctlConfig(t *testing.T) {
t.Parallel()
testCases := []struct {
name string
user string
rootless bool
mockSvc func(t *testing.T, fs afero.Fs)
postRunCheck func(t *testing.T, fs afero.Fs)
want error
}{
{
name: "happy path, rootless",
user: "mock_user",
rootless: true,
mockSvc: func(t *testing.T, fs afero.Fs) {},
postRunCheck: func(t *testing.T, fs afero.Fs) {
fileBytes, err := afero.ReadFile(fs, "/home/mock_user.linux/.config/nerdctl/nerdctl.toml")
require.NoError(t, err)
assert.Equal(t, []byte(`namespace = "finch"`+"\n"), fileBytes)
},
want: nil,
},
{
name: "happy path, rootful",
user: "mock_user",
rootless: false,
mockSvc: func(t *testing.T, fs afero.Fs) {},
postRunCheck: func(t *testing.T, fs afero.Fs) {
fileBytes, err := afero.ReadFile(fs, "/etc/nerdctl/nerdctl.toml")
require.NoError(t, err)
assert.Equal(t, []byte(`namespace = "finch"`+"\n"), fileBytes)
},
want: nil,
},
{
name: "happy path, config already exists",
user: "mock_user",
rootless: true,
mockSvc: func(t *testing.T, fs afero.Fs) {
err := afero.WriteFile(fs, "/home/mock_user.linux/.config/nerdctl/nerdctl.toml",
[]byte(`namespace = "something-else"`), 0o644)
require.NoError(t, err)
},
postRunCheck: func(t *testing.T, fs afero.Fs) {
fileBytes, err := afero.ReadFile(fs, "/home/mock_user.linux/.config/nerdctl/nerdctl.toml")
require.NoError(t, err)
assert.Equal(t, []byte(`namespace = "finch"`+"\n"), fileBytes)
},
want: nil,
},
{
name: "config contains invalid TOML",
user: "mock_user",
rootless: true,
mockSvc: func(t *testing.T, fs afero.Fs) {
err := afero.WriteFile(fs, "/home/mock_user.linux/.config/nerdctl/nerdctl.toml", []byte("{not toml}"), 0o644)
require.NoError(t, err)
},
postRunCheck: func(t *testing.T, fs afero.Fs) {},
want: fmt.Errorf(
"failed to unmarshal config file %s: %w",
"/home/mock_user.linux/.config/nerdctl/nerdctl.toml",
errors.New("(1, 1): parsing error: keys cannot contain { character"),
),
},
}
for _, tc := range testCases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
fs := afero.NewMemMapFs()
tc.mockSvc(t, fs)
got := updateNerdctlConfig(fs, tc.user, tc.rootless)
require.Equal(t, tc.want, got)
tc.postRunCheck(t, fs)
})
}
}
func TestNerdctlConfigApplier_Apply(t *testing.T) {
t.Parallel()
testCases := []struct {
name string
path string
remoteAddr string
mockSvc func(t *testing.T, fs afero.Fs, d *mocks.Dialer)
want error
}{
{
name: "private key path doesn't exist",
path: "/private-key",
remoteAddr: "",
mockSvc: func(t *testing.T, fs afero.Fs, d *mocks.Dialer) {
},
want: fmt.Errorf(
"failed to create ssh client config: %w",
fmt.Errorf(
"failed to open private key file: %w",
&fs.PathError{Op: "open", Path: "/private-key", Err: errors.New("file does not exist")},
),
),
},
{
name: "dialer fails to create the ssh connection",
path: "/private-key",
remoteAddr: "deadbeef",
mockSvc: func(t *testing.T, fs afero.Fs, d *mocks.Dialer) {
err := afero.WriteFile(fs, "/private-key", []byte(fakeSSHKey), 0o600)
require.NoError(t, err)
d.EXPECT().Dial("tcp", "deadbeef", gomock.Any()).Return(nil, fmt.Errorf("some error"))
},
want: fmt.Errorf("failed to setup ssh client: %w", fmt.Errorf("some error")),
},
}
for _, tc := range testCases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
ctrl := gomock.NewController(t)
fs := afero.NewMemMapFs()
d := mocks.NewDialer(ctrl)
lima := mocks.NewMockLimaWrapper(ctrl)
tc.mockSvc(t, fs, d)
got := NewNerdctlApplier(d, fs, tc.path, lima).Apply(tc.remoteAddr)
assert.Equal(t, tc.want, got)
})
}
}