github.com/HashDataInc/packer@v1.3.2/helper/multistep/multistep_test.go (about)

     1  package multistep
     2  
     3  import "context"
     4  
     5  // A step for testing that accumulates data into a string slice in the
     6  // the state bag. It always uses the "data" key in the state bag, and will
     7  // initialize it.
     8  type TestStepAcc struct {
     9  	// The data inserted into the state bag.
    10  	Data string
    11  
    12  	// If true, it will halt at the step when it is run
    13  	Halt bool
    14  }
    15  
    16  // A step that syncs by sending a channel and expecting a response.
    17  type TestStepSync struct {
    18  	Ch chan chan bool
    19  }
    20  
    21  // A step that sleeps forever
    22  type TestStepWaitForever struct {
    23  }
    24  
    25  // A step that manually flips state to cancelling in run
    26  type TestStepInjectCancel struct {
    27  }
    28  
    29  func (s TestStepAcc) Run(_ context.Context, state StateBag) StepAction {
    30  	s.insertData(state, "data")
    31  
    32  	if s.Halt {
    33  		return ActionHalt
    34  	}
    35  
    36  	return ActionContinue
    37  }
    38  
    39  func (s TestStepAcc) Cleanup(state StateBag) {
    40  	s.insertData(state, "cleanup")
    41  }
    42  
    43  func (s TestStepAcc) insertData(state StateBag, key string) {
    44  	if _, ok := state.GetOk(key); !ok {
    45  		state.Put(key, make([]string, 0, 5))
    46  	}
    47  
    48  	data := state.Get(key).([]string)
    49  	data = append(data, s.Data)
    50  	state.Put(key, data)
    51  }
    52  
    53  func (s TestStepSync) Run(context.Context, StateBag) StepAction {
    54  	ch := make(chan bool)
    55  	s.Ch <- ch
    56  	<-ch
    57  
    58  	return ActionContinue
    59  }
    60  
    61  func (s TestStepSync) Cleanup(StateBag) {}
    62  
    63  func (s TestStepWaitForever) Run(context.Context, StateBag) StepAction {
    64  	select {}
    65  }
    66  
    67  func (s TestStepWaitForever) Cleanup(StateBag) {}
    68  
    69  func (s TestStepInjectCancel) Run(_ context.Context, state StateBag) StepAction {
    70  	r := state.Get("runner").(*BasicRunner)
    71  	r.state = stateCancelling
    72  	return ActionContinue
    73  }
    74  
    75  func (s TestStepInjectCancel) Cleanup(StateBag) {}