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