github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/cmd/jujud/util/util_test.go (about) 1 // Copyright 2014 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package util 5 6 import ( 7 stderrors "errors" 8 9 "github.com/juju/errors" 10 jc "github.com/juju/testing/checkers" 11 gc "gopkg.in/check.v1" 12 13 "github.com/juju/juju/apiserver/params" 14 coretesting "github.com/juju/juju/testing" 15 "github.com/juju/juju/worker" 16 "github.com/juju/juju/worker/upgrader" 17 ) 18 19 var ( 20 _ = gc.Suite(&toolSuite{}) 21 ) 22 23 type toolSuite struct { 24 coretesting.BaseSuite 25 } 26 27 func (*toolSuite) TestErrorImportance(c *gc.C) { 28 29 errorImportanceTests := []error{ 30 nil, 31 stderrors.New("foo"), 32 &upgrader.UpgradeReadyError{}, 33 worker.ErrTerminateAgent, 34 } 35 36 for i, err0 := range errorImportanceTests { 37 for j, err1 := range errorImportanceTests { 38 c.Assert(MoreImportant(err0, err1), gc.Equals, i > j) 39 40 // Should also work if errors are wrapped. 41 c.Assert(MoreImportant(errors.Trace(err0), errors.Trace(err1)), gc.Equals, i > j) 42 } 43 } 44 } 45 46 var isFatalTests = []struct { 47 err error 48 isFatal bool 49 }{ 50 { 51 err: worker.ErrTerminateAgent, 52 isFatal: true, 53 }, { 54 err: errors.Trace(worker.ErrTerminateAgent), 55 isFatal: true, 56 }, { 57 err: &upgrader.UpgradeReadyError{}, 58 isFatal: true, 59 }, { 60 err: ¶ms.Error{ 61 Message: "blah", 62 Code: params.CodeNotProvisioned, 63 }, 64 isFatal: false, 65 }, { 66 err: &FatalError{"some fatal error"}, 67 isFatal: true, 68 }, { 69 err: stderrors.New("foo"), 70 isFatal: false, 71 }, { 72 err: ¶ms.Error{ 73 Message: "blah", 74 Code: params.CodeNotFound, 75 }, 76 isFatal: false, 77 }, 78 } 79 80 func (s *toolSuite) TestConnectionIsFatal(c *gc.C) { 81 var ( 82 errPinger testPinger = func() error { 83 return stderrors.New("ping error") 84 } 85 okPinger testPinger = func() error { 86 return nil 87 } 88 ) 89 for i, pinger := range []testPinger{errPinger, okPinger} { 90 for j, test := range isFatalTests { 91 c.Logf("test %d.%d: %s", i, j, test.err) 92 fatal := ConnectionIsFatal(logger, pinger)(test.err) 93 if test.isFatal { 94 c.Check(fatal, jc.IsTrue) 95 } else { 96 c.Check(fatal, gc.Equals, i == 0) 97 } 98 } 99 } 100 } 101 102 func (s *toolSuite) TestConnectionIsFatalWithMultipleConns(c *gc.C) { 103 var ( 104 errPinger testPinger = func() error { 105 return stderrors.New("ping error") 106 } 107 okPinger testPinger = func() error { 108 return nil 109 } 110 ) 111 112 someErr := stderrors.New("foo") 113 114 c.Assert(ConnectionIsFatal(logger, okPinger, okPinger)(someErr), 115 jc.IsFalse) 116 c.Assert(ConnectionIsFatal(logger, okPinger, okPinger, okPinger)(someErr), 117 jc.IsFalse) 118 c.Assert(ConnectionIsFatal(logger, okPinger, errPinger)(someErr), 119 jc.IsTrue) 120 c.Assert(ConnectionIsFatal(logger, okPinger, okPinger, errPinger)(someErr), 121 jc.IsTrue) 122 c.Assert(ConnectionIsFatal(logger, errPinger, okPinger, okPinger)(someErr), 123 jc.IsTrue) 124 } 125 126 func (*toolSuite) TestIsFatal(c *gc.C) { 127 128 for i, test := range isFatalTests { 129 c.Logf("test %d: %s", i, test.err) 130 c.Assert(IsFatal(test.err), gc.Equals, test.isFatal) 131 } 132 } 133 134 type testPinger func() error 135 136 func (f testPinger) Ping() error { 137 return f() 138 }