Skip to content

Commit 1b645b8

Browse files
RoboticPrismfacebook-github-bot
authored andcommitted
Create mutable variable store (#514)
Summary: Pull Request resolved: #514 Create a variable store that we can mutate between test steps. Move WorkDir into this variable store so that we can mutate the work dir for a future cd step. In the future if we want to carry any other information between steps in a mutable fashion, all we need to do is add a new variable to this struct. Reviewed By: inesusvet Differential Revision: D63457210 fbshipit-source-id: 56beb18b6f5eb66f60c6946d527ca97d9cd4e0b4
1 parent 62814c1 commit 1b645b8

10 files changed

Lines changed: 35 additions & 19 deletions

File tree

pkg/blocks/context.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,15 @@ type TTPExecutionConfig struct {
4141
Stderr io.Writer
4242
}
4343

44+
// TTPExecutionVars - mutable store to carry variables between steps
45+
type TTPExecutionVars struct {
46+
WorkDir string
47+
}
48+
4449
// TTPExecutionContext - holds config and context for the currently executing TTP
4550
type TTPExecutionContext struct {
4651
Cfg TTPExecutionConfig
47-
WorkDir string
52+
Vars *TTPExecutionVars
4853
StepResults *StepResultsRecord
4954
actionResultsChan chan *ActResult
5055
errorsChan chan error
@@ -54,6 +59,7 @@ type TTPExecutionContext struct {
5459
// NewTTPExecutionContext creates a new TTPExecutionContext with empty config and created channels
5560
func NewTTPExecutionContext() TTPExecutionContext {
5661
return TTPExecutionContext{
62+
Vars: &TTPExecutionVars{WorkDir: "/"},
5763
StepResults: NewStepResultsRecord(),
5864
actionResultsChan: make(chan *ActResult, 1),
5965
errorsChan: make(chan error, 1),

pkg/blocks/editstep.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ func (s *EditStep) Validate(execCtx TTPExecutionContext) error {
7171
fileSystem := s.FileSystem
7272

7373
if fileSystem == nil {
74-
_, err := FetchAbs(targetPath, execCtx.WorkDir)
74+
_, err := FetchAbs(targetPath, execCtx.Vars.WorkDir)
7575
if err != nil {
7676
return err
7777
}
@@ -130,12 +130,12 @@ func (s *EditStep) Execute(execCtx TTPExecutionContext) (*ActResult, error) {
130130
if fileSystem == nil {
131131
fileSystem = afero.NewOsFs()
132132
var err error
133-
targetPath, err = FetchAbs(targetPath, execCtx.WorkDir)
133+
targetPath, err = FetchAbs(targetPath, execCtx.Vars.WorkDir)
134134
if err != nil {
135135
return nil, err
136136
}
137137
if backupPath != "" {
138-
backupPath, err = FetchAbs(backupPath, execCtx.WorkDir)
138+
backupPath, err = FetchAbs(backupPath, execCtx.Vars.WorkDir)
139139
if err != nil {
140140
return nil, err
141141
}

pkg/blocks/editstep_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ edits:
261261
for _, tc := range testCases {
262262
t.Run(tc.name, func(t *testing.T) {
263263
var editStep EditStep
264-
var execCtx TTPExecutionContext
264+
execCtx := NewTTPExecutionContext()
265265

266266
// parse the step
267267
err := yaml.Unmarshal([]byte(tc.content), &editStep)

pkg/blocks/executor.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ func (e *ScriptExecutor) Execute(ctx context.Context, execCtx TTPExecutionContex
108108

109109
cmd := e.buildCommand(ctx)
110110
cmd.Env = expandedEnvAsList
111-
cmd.Dir = execCtx.WorkDir
111+
cmd.Dir = execCtx.Vars.WorkDir
112112
cmd.Stdin = strings.NewReader(body)
113113

114114
return streamAndCapture(*cmd, execCtx.Cfg.Stdout, execCtx.Cfg.Stderr)
@@ -138,7 +138,7 @@ func (e *FileExecutor) Execute(ctx context.Context, execCtx TTPExecutionContext)
138138
}
139139

140140
cmd.Env = expandedEnvAsList
141-
cmd.Dir = execCtx.WorkDir
141+
cmd.Dir = execCtx.Vars.WorkDir
142142
return streamAndCapture(*cmd, execCtx.Cfg.Stdout, execCtx.Cfg.Stderr)
143143
}
144144

pkg/blocks/expectstep.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ func (s *ExpectStep) prepareCommand(ctx context.Context, execCtx TTPExecutionCon
258258
/* #nosec G204 */
259259
cmd := exec.CommandContext(ctx, s.Executor, "-c", inline)
260260
cmd.Env = envAsList
261-
cmd.Dir = execCtx.WorkDir
261+
cmd.Dir = execCtx.Vars.WorkDir
262262

263263
return cmd
264264
}

pkg/blocks/expectstep_test.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,9 @@ func createTestScript(t *testing.T, scriptContent string) (string, string) {
5757

5858
func NewTestTTPExecutionContext(workDir string) TTPExecutionContext {
5959
return TTPExecutionContext{
60-
WorkDir: workDir,
60+
Vars: &TTPExecutionVars{
61+
WorkDir: workDir,
62+
},
6163
}
6264
}
6365

@@ -668,7 +670,11 @@ func TestExpectSSH(t *testing.T) {
668670
mockStep := new(MockExpectStep)
669671
mockStep.On("Execute", mock.Anything).Return(tc.mockRet, tc.mockErr)
670672

671-
execCtx := TTPExecutionContext{WorkDir: "."}
673+
execCtx := TTPExecutionContext{
674+
Vars: &TTPExecutionVars{
675+
WorkDir: ".",
676+
},
677+
}
672678
fmt.Println("Executing command:", tc.step.Expect.Inline)
673679
_, err := mockStep.Execute(execCtx)
674680
if (err != nil) != tc.wantErr {
@@ -744,7 +750,9 @@ func TestCleanup(t *testing.T) {
744750
}
745751

746752
execCtx := TTPExecutionContext{
747-
WorkDir: ".",
753+
Vars: &TTPExecutionVars{
754+
WorkDir: ".",
755+
},
748756
}
749757

750758
_, err := s.Cleanup(execCtx)

pkg/blocks/fetchuri.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ func (f *FetchURIStep) Validate(execCtx TTPExecutionContext) error {
9191
}
9292

9393
// Retrieve the absolute path to the file.
94-
absLocal, err := FetchAbs(f.Location, execCtx.WorkDir)
94+
absLocal, err := FetchAbs(f.Location, execCtx.Vars.WorkDir)
9595
if err != nil {
9696
logging.L().Error(zap.Error(err))
9797
return err
@@ -135,7 +135,7 @@ func (f *FetchURIStep) fetchURI(execCtx TTPExecutionContext) error {
135135
if appFs == nil {
136136
var err error
137137
appFs = afero.NewOsFs()
138-
absLocal, err = FetchAbs(f.Location, execCtx.WorkDir)
138+
absLocal, err = FetchAbs(f.Location, execCtx.Vars.WorkDir)
139139
if err != nil {
140140
return err
141141
}

pkg/blocks/fetchuri_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ steps:
181181
err := yaml.Unmarshal([]byte(tc.content), &ttps)
182182
assert.NoError(t, err)
183183

184-
var execCtx TTPExecutionContext
184+
execCtx := NewTTPExecutionContext()
185185
err = ttps.Validate(execCtx)
186186
if tc.wantError {
187187
assert.Error(t, err)
@@ -202,7 +202,7 @@ location: /tmp/test.html
202202
overwrite: true
203203
`
204204
var s FetchURIStep
205-
var execCtx TTPExecutionContext
205+
execCtx := NewTTPExecutionContext()
206206
err := yaml.Unmarshal([]byte(content), &s)
207207
require.NoError(t, err)
208208

pkg/blocks/filestep.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,14 +74,14 @@ func (f *FileStep) Validate(execCtx TTPExecutionContext) error {
7474
}
7575

7676
// If FilePath is set, ensure that the file exists.
77-
fullpath, err := FindFilePath(f.FilePath, execCtx.WorkDir, nil)
77+
fullpath, err := FindFilePath(f.FilePath, execCtx.Vars.WorkDir, nil)
7878
if err != nil {
7979
logging.L().Error(zap.Error(err))
8080
return err
8181
}
8282

8383
// Retrieve the absolute path to the file.
84-
f.FilePath, err = FetchAbs(fullpath, execCtx.WorkDir)
84+
f.FilePath, err = FetchAbs(fullpath, execCtx.Vars.WorkDir)
8585
if err != nil {
8686
logging.L().Error(zap.Error(err))
8787
return err

pkg/blocks/loader.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,8 +151,10 @@ func LoadTTP(ttpFilePath string, fsys afero.Fs, execCfg *TTPExecutionConfig, arg
151151
}
152152

153153
execCtx := TTPExecutionContext{
154-
Cfg: *execCfg,
155-
WorkDir: ttp.WorkDir,
154+
Cfg: *execCfg,
155+
Vars: &TTPExecutionVars{
156+
WorkDir: ttp.WorkDir,
157+
},
156158
StepResults: NewStepResultsRecord(),
157159
actionResultsChan: make(chan *ActResult, 1),
158160
errorsChan: make(chan error, 1),

0 commit comments

Comments
 (0)