github.com/axw/juju@v0.0.0-20161005053422-4bd6544d08d4/cmd/jujud/agent/introspection_test.go (about) 1 // Copyright 2016 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package agent 5 6 import ( 7 "runtime" 8 "time" 9 10 "github.com/juju/errors" 11 "github.com/juju/testing" 12 jc "github.com/juju/testing/checkers" 13 gc "gopkg.in/check.v1" 14 "gopkg.in/juju/names.v2" 15 16 "github.com/juju/juju/agent" 17 cmdutil "github.com/juju/juju/cmd/jujud/util" 18 coretesting "github.com/juju/juju/testing" 19 "github.com/juju/juju/worker" 20 "github.com/juju/juju/worker/dependency" 21 "github.com/juju/juju/worker/introspection" 22 ) 23 24 type introspectionSuite struct { 25 testing.IsolationSuite 26 } 27 28 var _ = gc.Suite(&introspectionSuite{}) 29 30 func (s *introspectionSuite) TestStartNonLinux(c *gc.C) { 31 if runtime.GOOS == "linux" { 32 c.Skip("testing for non-linux") 33 } 34 var started bool 35 36 cfg := introspectionConfig{ 37 WorkerFunc: func(_ introspection.Config) (worker.Worker, error) { 38 started = true 39 return nil, errors.New("shouldn't call start") 40 }, 41 } 42 43 err := startIntrospection(cfg) 44 c.Assert(err, jc.ErrorIsNil) 45 c.Assert(started, jc.IsFalse) 46 } 47 48 func (s *introspectionSuite) TestStartError(c *gc.C) { 49 if runtime.GOOS != "linux" { 50 c.Skip("introspection worker not supported on non-linux") 51 } 52 53 cfg := introspectionConfig{ 54 Agent: &dummyAgent{}, 55 WorkerFunc: func(_ introspection.Config) (worker.Worker, error) { 56 return nil, errors.New("boom") 57 }, 58 } 59 60 err := startIntrospection(cfg) 61 c.Check(err, gc.ErrorMatches, "boom") 62 } 63 64 func (s *introspectionSuite) TestStartSuccess(c *gc.C) { 65 if runtime.GOOS != "linux" { 66 c.Skip("introspection worker not supported on non-linux") 67 } 68 fake := &dummyWorker{ 69 done: make(chan struct{}), 70 } 71 72 config := dependency.EngineConfig{ 73 IsFatal: cmdutil.IsFatal, 74 WorstError: cmdutil.MoreImportantError, 75 } 76 engine, err := dependency.NewEngine(config) 77 c.Assert(err, jc.ErrorIsNil) 78 79 cfg := introspectionConfig{ 80 Agent: &dummyAgent{}, 81 Engine: engine, 82 WorkerFunc: func(cfg introspection.Config) (worker.Worker, error) { 83 fake.config = cfg 84 return fake, nil 85 }, 86 } 87 88 err = startIntrospection(cfg) 89 c.Assert(err, jc.ErrorIsNil) 90 91 c.Check(fake.config.Reporter, gc.Equals, engine) 92 c.Check(fake.config.SocketName, gc.Equals, "jujud-machine-42") 93 94 // Stopping the engine causes the introspection worker to stop. 95 engine.Kill() 96 97 select { 98 case <-fake.done: 99 case <-time.After(coretesting.LongWait): 100 c.Fatalf("worker did not get stopped") 101 } 102 } 103 104 type dummyAgent struct { 105 agent.Agent 106 } 107 108 func (*dummyAgent) CurrentConfig() agent.Config { 109 return &dummyConfig{} 110 } 111 112 type dummyConfig struct { 113 agent.Config 114 } 115 116 func (*dummyConfig) Tag() names.Tag { 117 return names.NewMachineTag("42") 118 } 119 120 type dummyWorker struct { 121 config introspection.Config 122 done chan struct{} 123 } 124 125 func (d *dummyWorker) Kill() { 126 close(d.done) 127 } 128 129 func (d *dummyWorker) Wait() error { 130 <-d.done 131 return nil 132 }