Skip to content

Commit

Permalink
[K10-2159] Update Kanister Controller test for Delete ActionSet (#5371)
Browse files Browse the repository at this point in the history
* Pass child context to ActionSets

* Add cancelFunc chan

* Add ActionSet Delete: cancelFunc

* Remove TODO comment

* Address review comments
  • Loading branch information
DeepikaDixit authored and Ilya Kislenko committed Apr 9, 2019
1 parent 5eb0413 commit bce6463
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 5 deletions.
3 changes: 2 additions & 1 deletion pkg/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,8 @@ func (c *Controller) runAction(ctx context.Context, as *crv1alpha1.ActionSet, aI
return err
}
ns, name := as.GetNamespace(), as.GetName()
t, _ := tomb.WithContext(ctx) // TODO: @Deepika Ignoring child context currently
var t *tomb.Tomb
t, ctx = tomb.WithContext(ctx)
c.actionSetTombMap.Store(as.Name, t)
t.Go(func() error {
for i, p := range phases {
Expand Down
23 changes: 19 additions & 4 deletions pkg/controller/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,10 @@ func (s *ControllerSuite) TestExecActionSet(c *C) {
funcNames: []string{testutil.OutputFuncName},
name: "OutputFunc",
},
{
funcNames: []string{testutil.CancelFuncName},
name: "CancelFunc",
},
} {
var err error
// Add a blueprint with a mocked kanister function.
Expand Down Expand Up @@ -313,6 +317,7 @@ func (s *ControllerSuite) TestExecActionSet(c *C) {
c.Assert(err, IsNil, Commentf("Failed case: %s", tc.name))

final := crv1alpha1.StateComplete
cancel := false
Loop:
for _, fn := range tc.funcNames {
switch fn {
Expand All @@ -326,16 +331,26 @@ func (s *ControllerSuite) TestExecActionSet(c *C) {
c.Assert(testutil.ArgFuncArgs(), DeepEquals, map[string]interface{}{"key": "myValue"}, Commentf("Failed case: %s", tc.name))
case testutil.OutputFuncName:
c.Assert(testutil.OutputFuncOut(), DeepEquals, map[string]interface{}{"key": "myValue"}, Commentf("Failed case: %s", tc.name))
case testutil.CancelFuncName:
err = s.crCli.ActionSets(s.namespace).Delete(as.GetName(), nil)
c.Assert(err, IsNil)
c.Assert(testutil.CancelFuncOut().Error(), DeepEquals, "context canceled")
cancel = true
}
}

err = s.waitOnActionSetState(c, as, final)
c.Assert(err, IsNil, Commentf("Failed case: %s", tc.name))

if !cancel {
err = s.waitOnActionSetState(c, as, final)
c.Assert(err, IsNil, Commentf("Failed case: %s", tc.name))
}
err = s.crCli.Blueprints(s.namespace).Delete(bp.GetName(), nil)
c.Assert(err, IsNil)
err = s.crCli.ActionSets(s.namespace).Delete(as.GetName(), nil)
c.Assert(err, IsNil)
if !cancel {
c.Assert(err, IsNil)
} else {
c.Assert(err, NotNil)
}
}
}
}
Expand Down
7 changes: 7 additions & 0 deletions pkg/testutil/func.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ var (
waitFuncCh chan struct{}
argFuncCh chan map[string]interface{}
outputFuncCh chan map[string]interface{}
cancelFuncCh chan error
)

func failFunc(context.Context, param.TemplateParams, map[string]interface{}) (map[string]interface{}, error) {
Expand All @@ -47,6 +48,7 @@ func outputFunc(ctx context.Context, tp param.TemplateParams, args map[string]in

func cancelFunc(ctx context.Context, tp param.TemplateParams, args map[string]interface{}) (map[string]interface{}, error) {
<-ctx.Done()
cancelFuncCh <- ctx.Err()
return nil, ctx.Err()
}

Expand All @@ -55,6 +57,7 @@ func init() {
waitFuncCh = make(chan struct{})
argFuncCh = make(chan map[string]interface{})
outputFuncCh = make(chan map[string]interface{})
cancelFuncCh = make(chan error)
registerMockKanisterFunc(FailFuncName, failFunc)
registerMockKanisterFunc(WaitFuncName, waitFunc)
registerMockKanisterFunc(ArgFuncName, argsFunc)
Expand Down Expand Up @@ -100,3 +103,7 @@ func OutputFuncOut() map[string]interface{} {
func (mf *mockKanisterFunc) RequiredArgs() []string {
return nil
}

func CancelFuncOut() error {
return <-cancelFuncCh
}

0 comments on commit bce6463

Please sign in to comment.