github.com/cloud-green/juju@v0.0.0-20151002100041-a00291338d3d/worker/dependency/reporter_test.go (about) 1 // Copyright 2015 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package dependency_test 5 6 import ( 7 "time" 8 9 jc "github.com/juju/testing/checkers" 10 gc "gopkg.in/check.v1" 11 12 coretesting "github.com/juju/juju/testing" 13 "github.com/juju/juju/worker/dependency" 14 ) 15 16 type ReportSuite struct { 17 engineFixture 18 } 19 20 var _ = gc.Suite(&ReportSuite{}) 21 22 func (s *ReportSuite) TestReportStarted(c *gc.C) { 23 report := s.engine.Report() 24 c.Check(report, jc.DeepEquals, map[string]interface{}{ 25 "state": "started", 26 "error": nil, 27 "manifolds": map[string]interface{}{}, 28 }) 29 } 30 31 func (s *ReportSuite) TestReportStopped(c *gc.C) { 32 s.engine.Kill() 33 err := s.engine.Wait() 34 c.Check(err, jc.ErrorIsNil) 35 report := s.engine.Report() 36 c.Check(report, jc.DeepEquals, map[string]interface{}{ 37 "state": "stopped", 38 "error": nil, 39 "manifolds": map[string]interface{}{}, 40 }) 41 } 42 43 func (s *ReportSuite) TestReportStopping(c *gc.C) { 44 mh1 := newErrorIgnoringManifoldHarness() 45 err := s.engine.Install("task", mh1.Manifold()) 46 c.Assert(err, jc.ErrorIsNil) 47 defer func() { 48 s.engine.Kill() 49 mh1.InjectError(c, nil) 50 err := s.engine.Wait() 51 c.Check(err, jc.ErrorIsNil) 52 }() 53 mh1.AssertOneStart(c) 54 55 // It may take a short time for the main loop to notice 56 // the change and stop the "task" worker. 57 s.engine.Kill() 58 var isTaskStopping = func(report map[string]interface{}) bool { 59 manifolds := report["manifolds"].(map[string]interface{}) 60 task := manifolds["task"].(map[string]interface{}) 61 switch taskState := task["state"]; taskState { 62 case "started": 63 return false 64 case "stopping": 65 return true 66 default: 67 c.Fatalf("unexpected task state: %v", taskState) 68 } 69 panic("unreachable") 70 } 71 72 var report map[string]interface{} 73 for i := 0; i < 3; i++ { 74 report = s.engine.Report() 75 if isTaskStopping(report) { 76 break 77 } 78 time.Sleep(coretesting.ShortWait) 79 } 80 c.Check(report, jc.DeepEquals, map[string]interface{}{ 81 "state": "stopping", 82 "error": nil, 83 "manifolds": map[string]interface{}{ 84 "task": map[string]interface{}{ 85 "state": "stopping", 86 "error": nil, 87 "inputs": ([]string)(nil), 88 "resource-log": []map[string]interface{}{}, 89 "report": map[string]interface{}{ 90 "key1": "hello there", 91 }, 92 }, 93 }, 94 }) 95 } 96 97 func (s *ReportSuite) TestReportInputs(c *gc.C) { 98 mh1 := newManifoldHarness() 99 err := s.engine.Install("task", mh1.Manifold()) 100 c.Assert(err, jc.ErrorIsNil) 101 mh1.AssertStart(c) 102 103 mh2 := newManifoldHarness("task") 104 err = s.engine.Install("another task", mh2.Manifold()) 105 c.Assert(err, jc.ErrorIsNil) 106 mh2.AssertStart(c) 107 108 report := s.engine.Report() 109 c.Check(report, jc.DeepEquals, map[string]interface{}{ 110 "state": "started", 111 "error": nil, 112 "manifolds": map[string]interface{}{ 113 "task": map[string]interface{}{ 114 "state": "started", 115 "error": nil, 116 "inputs": ([]string)(nil), 117 "resource-log": []map[string]interface{}{}, 118 "report": map[string]interface{}{ 119 "key1": "hello there", 120 }, 121 }, 122 "another task": map[string]interface{}{ 123 "state": "started", 124 "error": nil, 125 "inputs": []string{"task"}, 126 "resource-log": []map[string]interface{}{{ 127 "name": "task", 128 "type": "<nil>", 129 "error": nil, 130 }}, 131 "report": map[string]interface{}{ 132 "key1": "hello there", 133 }, 134 }, 135 }, 136 }) 137 } 138 func (s *ReportSuite) TestReportError(c *gc.C) { 139 mh1 := newManifoldHarness("missing") 140 manifold := mh1.Manifold() 141 err := s.engine.Install("task", manifold) 142 c.Assert(err, jc.ErrorIsNil) 143 mh1.AssertNoStart(c) 144 145 s.engine.Kill() 146 err = s.engine.Wait() 147 c.Check(err, jc.ErrorIsNil) 148 149 report := s.engine.Report() 150 c.Check(report, jc.DeepEquals, map[string]interface{}{ 151 "state": "stopped", 152 "error": nil, 153 "manifolds": map[string]interface{}{ 154 "task": map[string]interface{}{ 155 "state": "stopped", 156 "error": dependency.ErrMissing, 157 "inputs": []string{"missing"}, 158 "resource-log": []map[string]interface{}{{ 159 "name": "missing", 160 "type": "<nil>", 161 "error": dependency.ErrMissing, 162 }}, 163 "report": (map[string]interface{})(nil), 164 }, 165 }, 166 }) 167 }