|
9 | 9 |
|
10 | 10 | "github.com/spf13/afero"
|
11 | 11 | "github.com/stretchr/testify/require"
|
| 12 | + "golang.org/x/xerrors" |
12 | 13 |
|
13 | 14 | "github.com/coder/coder/v2/agent"
|
14 | 15 | "github.com/coder/coder/v2/agent/agenttest"
|
@@ -37,6 +38,40 @@ func (fs *testFs) Open(name string) (afero.File, error) {
|
37 | 38 | return fs.Fs.Open(name)
|
38 | 39 | }
|
39 | 40 |
|
| 41 | +func (fs *testFs) Create(name string) (afero.File, error) { |
| 42 | + if err := fs.intercept("create", name); err != nil { |
| 43 | + return nil, err |
| 44 | + } |
| 45 | + // Unlike os, afero lets you create files where directories already exist and |
| 46 | + // lets you nest them underneath files, somehow. |
| 47 | + stat, err := fs.Fs.Stat(name) |
| 48 | + if err == nil && stat.IsDir() { |
| 49 | + return nil, xerrors.New("is a directory") |
| 50 | + } |
| 51 | + stat, err = fs.Fs.Stat(filepath.Dir(name)) |
| 52 | + if err == nil && !stat.IsDir() { |
| 53 | + return nil, xerrors.New("not a directory") |
| 54 | + } |
| 55 | + return fs.Fs.Create(name) |
| 56 | +} |
| 57 | + |
| 58 | +func (fs *testFs) MkdirAll(name string, mode os.FileMode) error { |
| 59 | + if err := fs.intercept("mkdirall", name); err != nil { |
| 60 | + return err |
| 61 | + } |
| 62 | + // Unlike os, afero lets you create directories where files already exist and |
| 63 | + // lets you nest them underneath files somehow. |
| 64 | + stat, err := fs.Fs.Stat(filepath.Dir(name)) |
| 65 | + if err == nil && !stat.IsDir() { |
| 66 | + return xerrors.New("not a directory") |
| 67 | + } |
| 68 | + stat, err = fs.Fs.Stat(name) |
| 69 | + if err == nil && !stat.IsDir() { |
| 70 | + return xerrors.New("not a directory") |
| 71 | + } |
| 72 | + return fs.Fs.MkdirAll(name, mode) |
| 73 | +} |
| 74 | + |
40 | 75 | func TestReadFile(t *testing.T) {
|
41 | 76 | t.Parallel()
|
42 | 77 |
|
@@ -206,3 +241,106 @@ func TestReadFile(t *testing.T) {
|
206 | 241 | })
|
207 | 242 | }
|
208 | 243 | }
|
| 244 | + |
| 245 | +func TestWriteFile(t *testing.T) { |
| 246 | + t.Parallel() |
| 247 | + |
| 248 | + tmpdir := os.TempDir() |
| 249 | + noPermsFilePath := filepath.Join(tmpdir, "no-perms-file") |
| 250 | + noPermsDirPath := filepath.Join(tmpdir, "no-perms-dir") |
| 251 | + //nolint:dogsled |
| 252 | + conn, _, _, fs, _ := setupAgent(t, agentsdk.Manifest{}, 0, func(_ *agenttest.Client, opts *agent.Options) { |
| 253 | + opts.Filesystem = newTestFs(opts.Filesystem, func(call, file string) error { |
| 254 | + if file == noPermsFilePath || file == noPermsDirPath { |
| 255 | + return os.ErrPermission |
| 256 | + } |
| 257 | + return nil |
| 258 | + }) |
| 259 | + }) |
| 260 | + |
| 261 | + dirPath := filepath.Join(tmpdir, "directory") |
| 262 | + err := fs.MkdirAll(dirPath, 0o755) |
| 263 | + require.NoError(t, err) |
| 264 | + |
| 265 | + filePath := filepath.Join(tmpdir, "file") |
| 266 | + err = afero.WriteFile(fs, filePath, []byte("content"), 0o644) |
| 267 | + require.NoError(t, err) |
| 268 | + |
| 269 | + tests := []struct { |
| 270 | + name string |
| 271 | + path string |
| 272 | + bytes []byte |
| 273 | + errCode int |
| 274 | + error string |
| 275 | + }{ |
| 276 | + { |
| 277 | + name: "NoPath", |
| 278 | + path: "", |
| 279 | + errCode: http.StatusBadRequest, |
| 280 | + error: "\"path\" is required", |
| 281 | + }, |
| 282 | + { |
| 283 | + name: "RelativePath", |
| 284 | + path: "./relative", |
| 285 | + errCode: http.StatusBadRequest, |
| 286 | + error: "file path must be absolute", |
| 287 | + }, |
| 288 | + { |
| 289 | + name: "RelativePath", |
| 290 | + path: "also-relative", |
| 291 | + errCode: http.StatusBadRequest, |
| 292 | + error: "file path must be absolute", |
| 293 | + }, |
| 294 | + { |
| 295 | + name: "NonExistent", |
| 296 | + path: filepath.Join(tmpdir, "/nested/does-not-exist"), |
| 297 | + bytes: []byte("now it does exist"), |
| 298 | + }, |
| 299 | + { |
| 300 | + name: "IsDir", |
| 301 | + path: dirPath, |
| 302 | + errCode: http.StatusBadRequest, |
| 303 | + error: "is a directory", |
| 304 | + }, |
| 305 | + { |
| 306 | + name: "IsNotDir", |
| 307 | + path: filepath.Join(filePath, "file2"), |
| 308 | + errCode: http.StatusBadRequest, |
| 309 | + error: "not a directory", |
| 310 | + }, |
| 311 | + { |
| 312 | + name: "NoPermissionsFile", |
| 313 | + path: noPermsFilePath, |
| 314 | + errCode: http.StatusForbidden, |
| 315 | + error: "permission denied", |
| 316 | + }, |
| 317 | + { |
| 318 | + name: "NoPermissionsDir", |
| 319 | + path: filepath.Join(noPermsDirPath, "within-no-perm-dir"), |
| 320 | + errCode: http.StatusForbidden, |
| 321 | + error: "permission denied", |
| 322 | + }, |
| 323 | + } |
| 324 | + |
| 325 | + for _, tt := range tests { |
| 326 | + t.Run(tt.name, func(t *testing.T) { |
| 327 | + t.Parallel() |
| 328 | + |
| 329 | + ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong) |
| 330 | + defer cancel() |
| 331 | + |
| 332 | + err := conn.WriteFile(ctx, tt.path, tt.bytes) |
| 333 | + if tt.errCode != 0 { |
| 334 | + require.Error(t, err) |
| 335 | + cerr := coderdtest.SDKError(t, err) |
| 336 | + require.Contains(t, cerr.Error(), tt.error) |
| 337 | + require.Equal(t, tt.errCode, cerr.StatusCode()) |
| 338 | + } else { |
| 339 | + require.NoError(t, err) |
| 340 | + b, err := afero.ReadFile(fs, tt.path) |
| 341 | + require.NoError(t, err) |
| 342 | + require.Equal(t, tt.bytes, b) |
| 343 | + } |
| 344 | + }) |
| 345 | + } |
| 346 | +} |
0 commit comments