|
| 1 | +/* |
| 2 | +Copyright © 2023-present, Meta Platforms, Inc. and affiliates |
| 3 | +Permission is hereby granted, free of charge, to any person obtaining a copy |
| 4 | +of this software and associated documentation files (the "Software"), to deal |
| 5 | +in the Software without restriction, including without limitation the rights |
| 6 | +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 7 | +copies of the Software, and to permit persons to whom the Software is |
| 8 | +furnished to do so, subject to the following conditions: |
| 9 | +The above copyright notice and this permission notice shall be included in |
| 10 | +all copies or substantial portions of the Software. |
| 11 | +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 12 | +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 13 | +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 14 | +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 15 | +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 16 | +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 17 | +THE SOFTWARE. |
| 18 | +*/ |
| 19 | + |
| 20 | +package blocks |
| 21 | + |
| 22 | +import ( |
| 23 | + "testing" |
| 24 | + |
| 25 | + "github.com/facebookincubator/ttpforge/pkg/testutils" |
| 26 | + "github.com/stretchr/testify/assert" |
| 27 | + "github.com/stretchr/testify/require" |
| 28 | +) |
| 29 | + |
| 30 | +func TestChangeDirectoryExecute(t *testing.T) { |
| 31 | + |
| 32 | + testCases := []struct { |
| 33 | + name string |
| 34 | + description string |
| 35 | + step *ChangeDirectoryStep |
| 36 | + fsysContents map[string][]byte |
| 37 | + expectedError bool |
| 38 | + startingDir string |
| 39 | + }{ |
| 40 | + { |
| 41 | + name: "Change directory to valid directory", |
| 42 | + description: "Change directory and expect successful change of workdir", |
| 43 | + step: &ChangeDirectoryStep{ |
| 44 | + Cd: "/tmp", |
| 45 | + }, |
| 46 | + fsysContents: map[string][]byte{ |
| 47 | + "/home/testuser/test": []byte("test"), |
| 48 | + "/tmp/test": []byte("test"), |
| 49 | + }, |
| 50 | + expectedError: false, |
| 51 | + startingDir: "/home/testuser/", |
| 52 | + }, |
| 53 | + { |
| 54 | + name: "Change directory to invalid directory", |
| 55 | + description: "Try to change directory to invalid directory and expect error", |
| 56 | + step: &ChangeDirectoryStep{ |
| 57 | + Cd: "/doesntexist", |
| 58 | + }, |
| 59 | + fsysContents: map[string][]byte{ |
| 60 | + "/home/testuser/test": []byte("test"), |
| 61 | + "/tmp/test": []byte("test"), |
| 62 | + }, |
| 63 | + expectedError: true, |
| 64 | + startingDir: "/home/testuser/", |
| 65 | + }, |
| 66 | + { |
| 67 | + name: "Change directory with no given directory", |
| 68 | + description: "Try to change directory to no directory and expect error", |
| 69 | + step: &ChangeDirectoryStep{ |
| 70 | + Cd: "", |
| 71 | + }, |
| 72 | + fsysContents: map[string][]byte{ |
| 73 | + "/home/testuser/test": []byte("test"), |
| 74 | + "/tmp/test": []byte("test"), |
| 75 | + }, |
| 76 | + expectedError: true, |
| 77 | + startingDir: "/home/testuser/", |
| 78 | + }, |
| 79 | + } |
| 80 | + |
| 81 | + for _, tc := range testCases { |
| 82 | + t.Run(tc.name, func(t *testing.T) { |
| 83 | + // Prep filesystem |
| 84 | + fsys, err := testutils.MakeAferoTestFs(tc.fsysContents) |
| 85 | + require.NoError(t, err) |
| 86 | + tc.step.FileSystem = fsys |
| 87 | + |
| 88 | + // validate and check error |
| 89 | + execCtx := NewTTPExecutionContext() |
| 90 | + execCtx.Vars.WorkDir = tc.startingDir |
| 91 | + err = tc.step.Validate(execCtx) |
| 92 | + |
| 93 | + if tc.expectedError && err != nil { |
| 94 | + require.Error(t, err) |
| 95 | + return |
| 96 | + } |
| 97 | + require.NoError(t, err) |
| 98 | + |
| 99 | + // execute and check error |
| 100 | + _, err = tc.step.Execute(execCtx) |
| 101 | + |
| 102 | + if tc.expectedError && err != nil { |
| 103 | + require.Error(t, err) |
| 104 | + return |
| 105 | + } |
| 106 | + require.NoError(t, err) |
| 107 | + |
| 108 | + // check current working directory |
| 109 | + assert.Equal(t, tc.step.Cd, execCtx.Vars.WorkDir) |
| 110 | + |
| 111 | + // cleanup and check error |
| 112 | + err = tc.step.GetDefaultCleanupAction().Validate(execCtx) |
| 113 | + require.NoError(t, err) |
| 114 | + _, err = tc.step.GetDefaultCleanupAction().Execute(execCtx) |
| 115 | + require.NoError(t, err) |
| 116 | + |
| 117 | + // expect working directory to be rolled back to starting directory |
| 118 | + assert.Equal(t, tc.startingDir, execCtx.Vars.WorkDir) |
| 119 | + }) |
| 120 | + } |
| 121 | +} |
0 commit comments