Skip to content

Commit

Permalink
Test
Browse files Browse the repository at this point in the history
  • Loading branch information
yitsushi committed Nov 16, 2021
1 parent e48e1d7 commit 6ab67a2
Show file tree
Hide file tree
Showing 15 changed files with 224 additions and 46 deletions.
2 changes: 2 additions & 0 deletions core/plans/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@ package plans
const (
MicroVMDeletePlanName = "microvm_delete"
MicroVMCreateOrUpdatePlanName = "microvm_create_update"

microVMBootTime = 30
)
2 changes: 1 addition & 1 deletion core/plans/microvm_create_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func (p *microvmCreateOrUpdatePlan) Create(ctx context.Context) ([]planner.Proce
}

// MicroVM provider start
if err := p.addStep(ctx, microvm.NewStartStep(p.vm, ports.Provider)); err != nil {
if err := p.addStep(ctx, microvm.NewStartStep(p.vm, ports.Provider, microVMBootTime)); err != nil {
return nil, fmt.Errorf("adding microvm start step: %w", err)
}

Expand Down
4 changes: 4 additions & 0 deletions core/steps/event/publish_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,12 @@ func TestNewPublish(t *testing.T) {
// now than crying later if the system does not do what we want.
shouldDo, _ := step.ShouldDo(ctx)
subSteps, err := step.Do(ctx)
verifyErr := step.Verify(ctx)

g.Expect(shouldDo).To(g.BeTrue())
g.Expect(subSteps).To(g.BeEmpty())
g.Expect(err).To(g.BeNil())
g.Expect(verifyErr).ToNot(g.BeNil())
}

func TestNewPublish_eventServiceFailure(t *testing.T) {
Expand All @@ -67,7 +69,9 @@ func TestNewPublish_eventServiceFailure(t *testing.T) {
step := event.NewPublish(testTopic, evt, eventService)

subSteps, err := step.Do(ctx)
verifyErr := step.Verify(ctx)

g.Expect(subSteps).To(g.BeEmpty())
g.Expect(err).ToNot(g.BeNil())
g.Expect(verifyErr).ToNot(g.BeNil())
}
10 changes: 10 additions & 0 deletions core/steps/microvm/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,13 @@ func TestNewCreateStep(t *testing.T) {

shouldDo, shouldErr := step.ShouldDo(ctx)
subSteps, doErr := step.Do(ctx)
verifyErr := step.Verify(ctx)

g.Expect(shouldDo).To(g.BeTrue())
g.Expect(shouldErr).To(g.BeNil())
g.Expect(subSteps).To(g.BeEmpty())
g.Expect(doErr).To(g.BeNil())
g.Expect(verifyErr).ToNot(g.BeNil())
}

func TestNewCreateStep_StateCheck(t *testing.T) {
Expand Down Expand Up @@ -96,9 +98,11 @@ func TestNewCreateStep_StateCheck(t *testing.T) {
Return(testCase.State, nil)

shouldDo, shouldErr := step.ShouldDo(ctx)
verifyErr := step.Verify(ctx)

g.Expect(shouldDo).To(g.Equal(testCase.ExpectToRun))
g.Expect(shouldErr).To(g.BeNil())
g.Expect(verifyErr).ToNot(g.BeNil())
}
}

Expand All @@ -119,9 +123,11 @@ func TestNewCreateStep_StateCheckError(t *testing.T) {
Return(ports.MicroVMStateUnknown, errors.New("i have no idea"))

shouldDo, shouldErr := step.ShouldDo(ctx)
verifyErr := step.Verify(ctx)

g.Expect(shouldDo).To(g.BeFalse())
g.Expect(shouldErr).ToNot(g.BeNil())
g.Expect(verifyErr).ToNot(g.BeNil())
}

func TestNewCreateStep_VMIsNotDefined(t *testing.T) {
Expand All @@ -137,9 +143,11 @@ func TestNewCreateStep_VMIsNotDefined(t *testing.T) {
step := microvm.NewCreateStep(vm, microVMService)

subSteps, err := step.Do(ctx)
verifyErr := step.Verify(ctx)

g.Expect(subSteps).To(g.BeEmpty())
g.Expect(err).To(g.MatchError(internalerr.ErrSpecRequired))
g.Expect(verifyErr).ToNot(g.BeNil())
}

func TestNewCreateStep_ServiceCreateError(t *testing.T) {
Expand All @@ -159,7 +167,9 @@ func TestNewCreateStep_ServiceCreateError(t *testing.T) {
Return(errors.New("ensuring state dir: ...."))

subSteps, err := step.Do(ctx)
verifyErr := step.Verify(ctx)

g.Expect(subSteps).To(g.BeEmpty())
g.Expect(err).ToNot(g.BeNil())
g.Expect(verifyErr).ToNot(g.BeNil())
}
10 changes: 10 additions & 0 deletions core/steps/microvm/delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,13 @@ func TestNewDeleteStep(t *testing.T) {

shouldDo, shouldErr := step.ShouldDo(ctx)
subSteps, doErr := step.Do(ctx)
verifyErr := step.Verify(ctx)

g.Expect(shouldDo).To(g.BeTrue())
g.Expect(shouldErr).To(g.BeNil())
g.Expect(subSteps).To(g.BeEmpty())
g.Expect(doErr).To(g.BeNil())
g.Expect(verifyErr).ToNot(g.BeNil())
}

func TestNewDeleteStep_StateCheck(t *testing.T) {
Expand Down Expand Up @@ -97,9 +99,11 @@ func TestNewDeleteStep_StateCheck(t *testing.T) {
Return(testCase.State, nil)

shouldDo, shouldErr := step.ShouldDo(ctx)
verifyErr := step.Verify(ctx)

g.Expect(shouldDo).To(g.Equal(testCase.ExpectToRun))
g.Expect(shouldErr).To(g.BeNil())
g.Expect(verifyErr).ToNot(g.BeNil())
}
}

Expand All @@ -120,9 +124,11 @@ func TestNewDeleteStep_StateCheckError(t *testing.T) {
Return(ports.MicroVMStateUnknown, errors.New("i have no idea"))

shouldDo, shouldErr := step.ShouldDo(ctx)
verifyErr := step.Verify(ctx)

g.Expect(shouldDo).To(g.BeFalse())
g.Expect(shouldErr).ToNot(g.BeNil())
g.Expect(verifyErr).ToNot(g.BeNil())
}

func TestNewDeleteStep_VMIsNotDefined(t *testing.T) {
Expand All @@ -138,9 +144,11 @@ func TestNewDeleteStep_VMIsNotDefined(t *testing.T) {
step := microvm.NewDeleteStep(vm, microVMService)

subSteps, err := step.Do(ctx)
verifyErr := step.Verify(ctx)

g.Expect(subSteps).To(g.BeEmpty())
g.Expect(err).To(g.MatchError(internalerr.ErrSpecRequired))
g.Expect(verifyErr).ToNot(g.BeNil())
}

func TestNewDeleteStep_ServiceDeleteError(t *testing.T) {
Expand All @@ -160,7 +168,9 @@ func TestNewDeleteStep_ServiceDeleteError(t *testing.T) {
Return(errors.New("ensuring state dir: ...."))

subSteps, err := step.Do(ctx)
verifyErr := step.Verify(ctx)

g.Expect(subSteps).To(g.BeEmpty())
g.Expect(err).ToNot(g.BeNil())
g.Expect(verifyErr).ToNot(g.BeNil())
}
20 changes: 12 additions & 8 deletions core/steps/microvm/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,22 @@ import (
"github.com/weaveworks/flintlock/pkg/planner"
)

const waitToBoot = 5

func NewStartStep(vm *models.MicroVM, vmSvc ports.MicroVMService) planner.Procedure {
func NewStartStep(
vm *models.MicroVM,
vmSvc ports.MicroVMService,
bootTime int,
) planner.Procedure {
return &startStep{
vm: vm,
vmSvc: vmSvc,
vm: vm,
vmSvc: vmSvc,
bootTime: bootTime,
}
}

type startStep struct {
vm *models.MicroVM
vmSvc ports.MicroVMService
vm *models.MicroVM
vmSvc ports.MicroVMService
bootTime int
}

// Name is the name of the procedure/operation.
Expand Down Expand Up @@ -73,7 +77,7 @@ func (s *startStep) Verify(ctx context.Context) error {
"vmid": s.vm.ID,
})
logger.Debug("waiting for the microvm to start")
time.Sleep(waitToBoot * time.Second)
time.Sleep(time.Duration(s.bootTime) * time.Second)
logger.Debug("verify microvm is started")

state, err := s.vmSvc.State(ctx, s.vm.ID.String())
Expand Down
57 changes: 51 additions & 6 deletions core/steps/microvm/start_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,25 +44,32 @@ func TestNewStartStep(t *testing.T) {
ctx := context.Background()
vm := testVMToStart()

step := microvm.NewStartStep(vm, microVMService)
step := microvm.NewStartStep(vm, microVMService, 1)

microVMService.
EXPECT().
State(ctx, vm.ID.String()).
Return(ports.MicroVMStateConfigured, nil)

microVMService.
EXPECT().
State(ctx, vm.ID.String()).
Return(ports.MicroVMStateRunning, nil)

microVMService.
EXPECT().
Start(ctx, vm.ID.String()).
Return(nil)

shouldDo, shouldErr := step.ShouldDo(ctx)
subSteps, doErr := step.Do(ctx)
verifyErr := step.Verify(ctx)

g.Expect(shouldDo).To(g.BeTrue())
g.Expect(shouldErr).To(g.BeNil())
g.Expect(subSteps).To(g.BeEmpty())
g.Expect(doErr).To(g.BeNil())
g.Expect(verifyErr).To(g.BeNil())
}

func TestNewStartStep_StateCheck(t *testing.T) {
Expand All @@ -87,7 +94,7 @@ func TestNewStartStep_StateCheck(t *testing.T) {
ctx := context.Background()
vm := testVMToStart()

step := microvm.NewStartStep(vm, microVMService)
step := microvm.NewStartStep(vm, microVMService, 1)

for _, testCase := range stateTestCases {
microVMService.
Expand All @@ -100,7 +107,6 @@ func TestNewStartStep_StateCheck(t *testing.T) {
g.Expect(shouldDo).To(g.Equal(testCase.ExpectToRun))
g.Expect(shouldErr).To(g.BeNil())
}

}

func TestNewStartStep_StateCheckError(t *testing.T) {
Expand All @@ -112,7 +118,7 @@ func TestNewStartStep_StateCheckError(t *testing.T) {
ctx := context.Background()
vm := testVMToStart()

step := microvm.NewStartStep(vm, microVMService)
step := microvm.NewStartStep(vm, microVMService, 1)

microVMService.
EXPECT().
Expand All @@ -135,7 +141,7 @@ func TestNewStartStep_VMIsNotDefined(t *testing.T) {
microVMService := mock.NewMockMicroVMService(mockCtrl)
ctx := context.Background()

step := microvm.NewStartStep(vm, microVMService)
step := microvm.NewStartStep(vm, microVMService, 1)

subSteps, err := step.Do(ctx)

Expand All @@ -152,7 +158,7 @@ func TestNewStartStep_ServiceStartError(t *testing.T) {
vm := testVMToStart()
ctx := context.Background()

step := microvm.NewStartStep(vm, microVMService)
step := microvm.NewStartStep(vm, microVMService, 1)

microVMService.
EXPECT().
Expand All @@ -164,3 +170,42 @@ func TestNewStartStep_ServiceStartError(t *testing.T) {
g.Expect(subSteps).To(g.BeEmpty())
g.Expect(err).ToNot(g.BeNil())
}

func TestNewStartStep_unableToBoot(t *testing.T) {
g.RegisterTestingT(t)
mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish()

microVMService := mock.NewMockMicroVMService(mockCtrl)
vm := testVMToStart()
ctx := context.Background()

step := microvm.NewStartStep(vm, microVMService, 1)

microVMService.
EXPECT().
Start(ctx, vm.ID.String()).
Return(nil)

microVMService.
EXPECT().
State(ctx, vm.ID.String()).
Return(ports.MicroVMStateUnknown, nil)

subSteps, err := step.Do(ctx)
verifyErr := step.Verify(ctx)

g.Expect(subSteps).To(g.BeEmpty())
g.Expect(err).To(g.BeNil())
g.Expect(verifyErr).To(g.MatchError(internalerr.ErrUnableToBoot))

microVMService.
EXPECT().
State(ctx, vm.ID.String()).
Return(ports.MicroVMStateUnknown, errors.New("nope"))

verifyErr = step.Verify(ctx)

g.Expect(verifyErr).ToNot(g.BeNil())
g.Expect(verifyErr).ToNot(g.MatchError(internalerr.ErrUnableToBoot))
}
Loading

0 comments on commit 6ab67a2

Please sign in to comment.