Mise à jour des templates de VM et nettoyage des images inutilisées

This commit is contained in:
2018-05-16 15:08:27 +02:00
parent 8ac2048fd6
commit 9cd443bef6
3 changed files with 497 additions and 76 deletions

40
stepper/stepper.go Normal file
View File

@ -0,0 +1,40 @@
package stepper
import (
"context"
)
const (
Error Status = iota
Completed Status = iota
Canceled Status = iota
)
var (
ErrUnsupportedState = "unsupported state"
)
type Status int
type Step func(ctx context.Context, cancelFunc context.CancelFunc, state interface{}) error
func Run(ctx context.Context, state interface{}, steps ...Step) (Status, error) {
ctx, cancel := context.WithCancel(ctx)
defer cancel()
for _, s := range steps {
select {
case <-ctx.Done():
return Canceled, nil
default:
err := s(ctx, cancel, state)
if err != nil {
return Error, err
}
}
}
return Completed, nil
}

146
stepper/stepper_test.go Normal file
View File

@ -0,0 +1,146 @@
package stepper
import (
"context"
"errors"
"testing"
)
func TestBasicSteps(t *testing.T) {
ctx := context.Background()
state := make(map[string]bool)
step1 := func(ctx context.Context, cancel context.CancelFunc, state interface{}) error {
st, ok := state.(map[string]bool)
if !ok {
t.Error(ErrUnsupportedState)
}
st["step1"] = true
return nil
}
step2 := func(ctx context.Context, cancel context.CancelFunc, state interface{}) error {
st, ok := state.(map[string]bool)
if !ok {
t.Error(ErrUnsupportedState)
}
st["step2"] = true
return nil
}
status, err := Run(ctx, state, step1, step2)
if err != nil {
t.Error(err)
}
if g, e := status, Completed; g != e {
t.Errorf("status: expected '%v', got '%v'", e, g)
}
if g, e := state["step1"], true; g != e {
t.Errorf("state[\"step1\"]: expected '%v', got '%v'", e, g)
}
if g, e := state["step2"], true; g != e {
t.Errorf("state[\"step2\"]: expected '%v', got '%v'", e, g)
}
}
func TestStepsCancel(t *testing.T) {
ctx := context.Background()
state := make(map[string]bool)
step1 := func(ctx context.Context, cancel context.CancelFunc, state interface{}) error {
st, ok := state.(map[string]bool)
if !ok {
t.Error(ErrUnsupportedState)
}
st["step1"] = true
return nil
}
step2 := func(ctx context.Context, cancel context.CancelFunc, state interface{}) error {
cancel()
return nil
}
step3 := func(ctx context.Context, cancel context.CancelFunc, state interface{}) error {
st, ok := state.(map[string]bool)
if !ok {
t.Error(ErrUnsupportedState)
}
st["step3"] = true
return nil
}
status, err := Run(ctx, state, step1, step2, step3)
if err != nil {
t.Error(err)
}
if g, e := status, Canceled; g != e {
t.Errorf("status: expected '%v', got '%v'", e, g)
}
if g, e := state["step1"], true; g != e {
t.Errorf("state[\"step1\"]: expected '%v', got '%v'", e, g)
}
if g, e := state["step3"], false; g != e {
t.Errorf("state[\"step3\"]: expected '%v', got '%v'", e, g)
}
}
func TestStepsError(t *testing.T) {
stepErr := errors.New("test error")
ctx := context.Background()
state := make(map[string]bool)
step1 := func(ctx context.Context, cancel context.CancelFunc, state interface{}) error {
st, ok := state.(map[string]bool)
if !ok {
t.Error(ErrUnsupportedState)
}
st["step1"] = true
return nil
}
step2 := func(ctx context.Context, cancel context.CancelFunc, state interface{}) error {
return stepErr
}
step3 := func(ctx context.Context, cancel context.CancelFunc, state interface{}) error {
st, ok := state.(map[string]bool)
if !ok {
t.Error(ErrUnsupportedState)
}
st["step3"] = true
return nil
}
status, err := Run(ctx, state, step1, step2, step3)
if err == nil {
t.Errorf("err should not be nil")
}
if g, e := err, stepErr; g != e {
t.Errorf("err: expected '%v', got '%v'", e, g)
}
if g, e := status, Error; g != e {
t.Errorf("status: expected '%v', got '%v'", e, g)
}
if g, e := state["step1"], true; g != e {
t.Errorf("state[\"step1\"]: expected '%v', got '%v'", e, g)
}
if g, e := state["step3"], false; g != e {
t.Errorf("state[\"step3\"]: expected '%v', got '%v'", e, g)
}
}