Skip to content

Commit

Permalink
[K10-2064] Fix flaky kanister test (#5157)
Browse files Browse the repository at this point in the history
* K10-2064: Fix flaky kanister test

* Simply fail func test
  • Loading branch information
pavannd1 authored and Ilya Kislenko committed Mar 8, 2019
1 parent 2befac5 commit e70f7ef
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 11 deletions.
23 changes: 15 additions & 8 deletions pkg/controller/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,6 @@ func (s *ControllerSuite) waitOnActionSetState(c *C, as *crv1alpha1.ActionSet, s
if as.Status.State == state {
return true, nil
}
if state == crv1alpha1.StateRunning && as.Status.State == crv1alpha1.StateComplete {
return true, nil
}
// These are non-terminal states.
if as.Status.State == crv1alpha1.StatePending || as.Status.State == crv1alpha1.StateRunning {
return false, nil
Expand Down Expand Up @@ -254,30 +251,39 @@ func (s *ControllerSuite) TestExecActionSet(c *C) {
for _, tc := range []struct {
funcNames []string
args [][]string
name string
}{
{
funcNames: []string{testutil.WaitFuncName},
name: "WaitFunc",
},
{
funcNames: []string{testutil.WaitFuncName, testutil.WaitFuncName},
name: "WaitWait",
},
{
funcNames: []string{testutil.FailFuncName},
name: "FailFunc",
},
{
funcNames: []string{testutil.WaitFuncName, testutil.FailFuncName},
name: "WaitFail",
},
{
funcNames: []string{testutil.FailFuncName, testutil.WaitFuncName},
name: "FailWait",
},
{
funcNames: []string{testutil.ArgFuncName},
name: "ArgFunc",
},
{
funcNames: []string{testutil.ArgFuncName, testutil.FailFuncName},
name: "ArgFail",
},
{
funcNames: []string{testutil.OutputFuncName},
name: "OutputFunc",
},
} {
var err error
Expand All @@ -301,29 +307,30 @@ func (s *ControllerSuite) TestExecActionSet(c *C) {
as := testutil.NewTestActionSet(s.namespace, bp.GetName(), pok, n, s.namespace)
as = testutil.ActionSetWithConfigMap(as, s.confimap.GetName())
as, err = s.crCli.ActionSets(s.namespace).Create(as)
c.Assert(err, IsNil)
c.Assert(err, IsNil, Commentf("Failed case: %s", tc.name))

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

final := crv1alpha1.StateComplete
Loop:
for _, fn := range tc.funcNames {
switch fn {
case testutil.FailFuncName:
final = crv1alpha1.StateFailed
c.Assert(testutil.FailFuncError().Error(), DeepEquals, "Kanister function failed", Commentf("Failed case: %s", tc.name))
break Loop
case testutil.WaitFuncName:
testutil.ReleaseWaitFunc()
case testutil.ArgFuncName:
c.Assert(testutil.ArgFuncArgs(), DeepEquals, map[string]interface{}{"key": "myValue"})
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"})
c.Assert(testutil.OutputFuncOut(), DeepEquals, map[string]interface{}{"key": "myValue"}, Commentf("Failed case: %s", tc.name))
}
}

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

err = s.crCli.Blueprints(s.namespace).Delete(bp.GetName(), nil)
c.Assert(err, IsNil)
Expand Down
10 changes: 9 additions & 1 deletion pkg/testutil/func.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,16 @@ const (
)

var (
failFuncCh chan error
waitFuncCh chan struct{}
argFuncCh chan map[string]interface{}
outputFuncCh chan map[string]interface{}
)

func failFunc(context.Context, param.TemplateParams, map[string]interface{}) (map[string]interface{}, error) {
return nil, errors.New("Kanister Function Failed")
err := errors.New("Kanister function failed")
failFuncCh <- err
return nil, err
}

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

func init() {
failFuncCh = make(chan error)
waitFuncCh = make(chan struct{})
argFuncCh = make(chan map[string]interface{})
outputFuncCh = make(chan map[string]interface{})
Expand Down Expand Up @@ -69,6 +73,10 @@ func (mf *mockKanisterFunc) Name() string {
return mf.name
}

func FailFuncError() error {
return <-failFuncCh
}

func ReleaseWaitFunc() {
waitFuncCh <- struct{}{}
}
Expand Down
7 changes: 5 additions & 2 deletions pkg/testutil/func_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,11 @@ func (s *FuncSuite) TearDownSuite(c *C) {

func (s *FuncSuite) TestFailFunc(c *C) {
ctx := context.Background()
_, err := failFunc(ctx, param.TemplateParams{}, nil)
c.Assert(err, NotNil)
go func() {
_, err := failFunc(ctx, param.TemplateParams{}, nil)
c.Assert(err, NotNil)
}()
c.Assert(FailFuncError().Error(), Equals, "Kanister function failed")
}

func (s *FuncSuite) TestWaitFunc(c *C) {
Expand Down

0 comments on commit e70f7ef

Please sign in to comment.