LEFT | RIGHT |
1 package machiner_test | 1 package machiner_test |
2 | 2 |
3 import ( | 3 import ( |
4 . "launchpad.net/gocheck" | 4 . "launchpad.net/gocheck" |
5 "launchpad.net/juju-core/container" | 5 "launchpad.net/juju-core/container" |
6 "launchpad.net/juju-core/juju/testing" | 6 "launchpad.net/juju-core/juju/testing" |
7 "launchpad.net/juju-core/state" | 7 "launchpad.net/juju-core/state" |
8 coretesting "launchpad.net/juju-core/testing" | 8 coretesting "launchpad.net/juju-core/testing" |
9 "launchpad.net/juju-core/worker/machiner" | 9 "launchpad.net/juju-core/worker/machiner" |
10 stdtesting "testing" | 10 stdtesting "testing" |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
51 | 51 |
52 m0, err := s.State.AddMachine() | 52 m0, err := s.State.AddMachine() |
53 c.Assert(err, IsNil) | 53 c.Assert(err, IsNil) |
54 | 54 |
55 m1, err := s.State.AddMachine() | 55 m1, err := s.State.AddMachine() |
56 c.Assert(err, IsNil) | 56 c.Assert(err, IsNil) |
57 | 57 |
58 err = ud0.AssignToMachine(m0) | 58 err = ud0.AssignToMachine(m0) |
59 c.Assert(err, IsNil) | 59 c.Assert(err, IsNil) |
60 | 60 |
61 » dataDir := c.MkDir() | 61 » dcontainer := newDummyContainer() |
62 | 62 » machiner := machiner.NewMachinerWithContainer(m0, dcontainer) |
63 » action := make(chan string, 5) | |
64 » *machiner.Deploy = func(cfg container.Config, u *state.Unit) error { | |
65 » » c.Check(cfg.DataDir, Equals, dataDir) | |
66 » » action <- "+" + u.Name() | |
67 » » return nil | |
68 » } | |
69 » *machiner.Destroy = func(cfg container.Config, u *state.Unit) error { | |
70 » » c.Check(cfg.DataDir, Equals, dataDir) | |
71 » » action <- "-" + u.Name() | |
72 » » return nil | |
73 » } | |
74 » machiner := machiner.NewMachiner(m0, dataDir) | |
75 | 63 |
76 tests := []struct { | 64 tests := []struct { |
77 change func() | 65 change func() |
78 actions []string | 66 actions []string |
79 }{ | 67 }{ |
80 { | 68 { |
81 func() {}, | 69 func() {}, |
82 []string{"+d0/0"}, | 70 []string{"+d0/0"}, |
83 }, { | 71 }, { |
84 func() { | 72 func() { |
(...skipping 18 matching lines...) Expand all Loading... |
103 err := ud0.AssignToMachine(m1) | 91 err := ud0.AssignToMachine(m1) |
104 c.Assert(err, IsNil) | 92 c.Assert(err, IsNil) |
105 }, | 93 }, |
106 nil, | 94 nil, |
107 }, | 95 }, |
108 } | 96 } |
109 for i, t := range tests { | 97 for i, t := range tests { |
110 c.Logf("test %d", i) | 98 c.Logf("test %d", i) |
111 t.change() | 99 t.change() |
112 for _, a := range t.actions { | 100 for _, a := range t.actions { |
113 » » » checkAction(c, action, a) | 101 » » » dcontainer.checkAction(c, a) |
114 } | 102 } |
115 » » checkAction(c, action, "") | 103 » » dcontainer.checkAction(c, "") |
116 } | 104 } |
117 | 105 |
118 err = machiner.Stop() | 106 err = machiner.Stop() |
119 c.Assert(err, IsNil) | 107 c.Assert(err, IsNil) |
120 } | 108 } |
121 | 109 |
122 func checkAction(c *C, action <-chan string, expect string) { | 110 type dummyContainer struct { |
| 111 » action chan string |
| 112 } |
| 113 |
| 114 var _ container.Container = (*dummyContainer)(nil) |
| 115 |
| 116 func newDummyContainer() *dummyContainer { |
| 117 » return &dummyContainer{ |
| 118 » » make(chan string, 5), |
| 119 » } |
| 120 } |
| 121 |
| 122 func (d *dummyContainer) Deploy(u *state.Unit) error { |
| 123 » d.action <- "+" + u.Name() |
| 124 » return nil |
| 125 } |
| 126 |
| 127 func (d *dummyContainer) Destroy(u *state.Unit) error { |
| 128 » d.action <- "-" + u.Name() |
| 129 » return nil |
| 130 } |
| 131 |
| 132 func (d *dummyContainer) ToolsDir(u *state.Unit) string { |
| 133 » return "/dummy/tools" |
| 134 } |
| 135 |
| 136 func (d *dummyContainer) checkAction(c *C, action string) { |
123 timeout := 500 * time.Millisecond | 137 timeout := 500 * time.Millisecond |
124 » if expect == "" { | 138 » if action == "" { |
125 timeout = 200 * time.Millisecond | 139 timeout = 200 * time.Millisecond |
126 } | 140 } |
127 select { | 141 select { |
128 » case a := <-action: | 142 » case a := <-d.action: |
129 » » c.Assert(a, Equals, expect) | 143 » » c.Assert(a, Equals, action) |
130 case <-time.After(timeout): | 144 case <-time.After(timeout): |
131 » » if expect != "" { | 145 » » if action != "" { |
132 » » » c.Fatalf("expected action %v got nothing", expect) | 146 » » » c.Fatalf("expected action %v got nothing", action) |
133 } | 147 } |
134 } | 148 } |
135 } | 149 } |
LEFT | RIGHT |