github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/apiserver/agent/agent_test.go (about) 1 package agent_test 2 3 import ( 4 stdtesting "testing" 5 6 "github.com/juju/names" 7 jc "github.com/juju/testing/checkers" 8 gc "gopkg.in/check.v1" 9 10 "github.com/juju/juju/apiserver/agent" 11 "github.com/juju/juju/apiserver/common" 12 "github.com/juju/juju/apiserver/params" 13 apiservertesting "github.com/juju/juju/apiserver/testing" 14 "github.com/juju/juju/instance" 15 jujutesting "github.com/juju/juju/juju/testing" 16 "github.com/juju/juju/state" 17 "github.com/juju/juju/state/multiwatcher" 18 coretesting "github.com/juju/juju/testing" 19 ) 20 21 func TestPackage(t *stdtesting.T) { 22 coretesting.MgoTestPackage(t) 23 } 24 25 var _ = gc.Suite(&agentSuite{}) 26 27 type agentSuite struct { 28 jujutesting.JujuConnSuite 29 30 resources *common.Resources 31 authorizer apiservertesting.FakeAuthorizer 32 33 machine0 *state.Machine 34 machine1 *state.Machine 35 container *state.Machine 36 } 37 38 func (s *agentSuite) SetUpTest(c *gc.C) { 39 s.JujuConnSuite.SetUpTest(c) 40 41 var err error 42 s.machine0, err = s.State.AddMachine("quantal", state.JobManageModel) 43 c.Assert(err, jc.ErrorIsNil) 44 45 s.machine1, err = s.State.AddMachine("quantal", state.JobHostUnits) 46 c.Assert(err, jc.ErrorIsNil) 47 48 template := state.MachineTemplate{ 49 Series: "quantal", 50 Jobs: []state.MachineJob{state.JobHostUnits}, 51 } 52 s.container, err = s.State.AddMachineInsideMachine(template, s.machine1.Id(), instance.LXC) 53 c.Assert(err, jc.ErrorIsNil) 54 55 s.resources = common.NewResources() 56 s.AddCleanup(func(*gc.C) { s.resources.StopAll() }) 57 58 // Create a FakeAuthorizer so we can check permissions, 59 // set up assuming machine 1 has logged in. 60 s.authorizer = apiservertesting.FakeAuthorizer{ 61 Tag: s.machine1.Tag(), 62 } 63 } 64 65 func (s *agentSuite) TestAgentFailsWithNonAgent(c *gc.C) { 66 auth := s.authorizer 67 auth.Tag = names.NewUserTag("admin") 68 api, err := agent.NewAgentAPIV2(s.State, s.resources, auth) 69 c.Assert(err, gc.NotNil) 70 c.Assert(api, gc.IsNil) 71 c.Assert(err, gc.ErrorMatches, "permission denied") 72 } 73 74 func (s *agentSuite) TestAgentSucceedsWithUnitAgent(c *gc.C) { 75 auth := s.authorizer 76 auth.Tag = names.NewUnitTag("foosball/1") 77 _, err := agent.NewAgentAPIV2(s.State, s.resources, auth) 78 c.Assert(err, jc.ErrorIsNil) 79 } 80 81 func (s *agentSuite) TestGetEntities(c *gc.C) { 82 err := s.container.Destroy() 83 c.Assert(err, jc.ErrorIsNil) 84 args := params.Entities{ 85 Entities: []params.Entity{ 86 {Tag: "machine-1"}, 87 {Tag: "machine-0"}, 88 {Tag: "machine-1-lxc-0"}, 89 {Tag: "machine-42"}, 90 }, 91 } 92 api, err := agent.NewAgentAPIV2(s.State, s.resources, s.authorizer) 93 c.Assert(err, jc.ErrorIsNil) 94 results := api.GetEntities(args) 95 c.Assert(results, gc.DeepEquals, params.AgentGetEntitiesResults{ 96 Entities: []params.AgentGetEntitiesResult{ 97 { 98 Life: "alive", 99 Jobs: []multiwatcher.MachineJob{multiwatcher.JobHostUnits}, 100 }, 101 {Error: apiservertesting.ErrUnauthorized}, 102 {Error: apiservertesting.ErrUnauthorized}, 103 {Error: apiservertesting.ErrUnauthorized}, 104 }, 105 }) 106 } 107 108 func (s *agentSuite) TestGetEntitiesContainer(c *gc.C) { 109 auth := s.authorizer 110 auth.Tag = s.container.Tag() 111 err := s.container.Destroy() 112 c.Assert(err, jc.ErrorIsNil) 113 114 api, err := agent.NewAgentAPIV2(s.State, s.resources, auth) 115 c.Assert(err, jc.ErrorIsNil) 116 args := params.Entities{ 117 Entities: []params.Entity{ 118 {Tag: "machine-1"}, 119 {Tag: "machine-0"}, 120 {Tag: "machine-1-lxc-0"}, 121 {Tag: "machine-42"}, 122 }, 123 } 124 results := api.GetEntities(args) 125 c.Assert(results, gc.DeepEquals, params.AgentGetEntitiesResults{ 126 Entities: []params.AgentGetEntitiesResult{ 127 {Error: apiservertesting.ErrUnauthorized}, 128 {Error: apiservertesting.ErrUnauthorized}, 129 { 130 Life: "dying", 131 Jobs: []multiwatcher.MachineJob{multiwatcher.JobHostUnits}, 132 ContainerType: instance.LXC, 133 }, 134 {Error: apiservertesting.ErrUnauthorized}, 135 }, 136 }) 137 } 138 139 func (s *agentSuite) TestGetEntitiesNotFound(c *gc.C) { 140 // Destroy the container first, so we can destroy its parent. 141 err := s.container.Destroy() 142 c.Assert(err, jc.ErrorIsNil) 143 err = s.container.EnsureDead() 144 c.Assert(err, jc.ErrorIsNil) 145 err = s.container.Remove() 146 c.Assert(err, jc.ErrorIsNil) 147 148 err = s.machine1.Destroy() 149 c.Assert(err, jc.ErrorIsNil) 150 err = s.machine1.EnsureDead() 151 c.Assert(err, jc.ErrorIsNil) 152 err = s.machine1.Remove() 153 c.Assert(err, jc.ErrorIsNil) 154 155 api, err := agent.NewAgentAPIV2(s.State, s.resources, s.authorizer) 156 c.Assert(err, jc.ErrorIsNil) 157 results := api.GetEntities(params.Entities{ 158 Entities: []params.Entity{{Tag: "machine-1"}}, 159 }) 160 c.Assert(err, jc.ErrorIsNil) 161 c.Assert(results, gc.DeepEquals, params.AgentGetEntitiesResults{ 162 Entities: []params.AgentGetEntitiesResult{{ 163 Error: ¶ms.Error{ 164 Code: params.CodeNotFound, 165 Message: "machine 1 not found", 166 }, 167 }}, 168 }) 169 } 170 171 func (s *agentSuite) TestSetPasswords(c *gc.C) { 172 api, err := agent.NewAgentAPIV2(s.State, s.resources, s.authorizer) 173 c.Assert(err, jc.ErrorIsNil) 174 results, err := api.SetPasswords(params.EntityPasswords{ 175 Changes: []params.EntityPassword{ 176 {Tag: "machine-0", Password: "xxx-12345678901234567890"}, 177 {Tag: "machine-1", Password: "yyy-12345678901234567890"}, 178 {Tag: "machine-42", Password: "zzz-12345678901234567890"}, 179 }, 180 }) 181 c.Assert(err, jc.ErrorIsNil) 182 c.Assert(results, gc.DeepEquals, params.ErrorResults{ 183 Results: []params.ErrorResult{ 184 {apiservertesting.ErrUnauthorized}, 185 {nil}, 186 {apiservertesting.ErrUnauthorized}, 187 }, 188 }) 189 err = s.machine1.Refresh() 190 c.Assert(err, jc.ErrorIsNil) 191 changed := s.machine1.PasswordValid("yyy-12345678901234567890") 192 c.Assert(changed, jc.IsTrue) 193 } 194 195 func (s *agentSuite) TestSetPasswordsShort(c *gc.C) { 196 api, err := agent.NewAgentAPIV2(s.State, s.resources, s.authorizer) 197 c.Assert(err, jc.ErrorIsNil) 198 results, err := api.SetPasswords(params.EntityPasswords{ 199 Changes: []params.EntityPassword{ 200 {Tag: "machine-1", Password: "yyy"}, 201 }, 202 }) 203 c.Assert(err, jc.ErrorIsNil) 204 c.Assert(results.Results, gc.HasLen, 1) 205 c.Assert(results.Results[0].Error, gc.ErrorMatches, 206 "password is only 3 bytes long, and is not a valid Agent password") 207 } 208 209 func (s *agentSuite) TestClearReboot(c *gc.C) { 210 api, err := agent.NewAgentAPIV2(s.State, s.resources, s.authorizer) 211 c.Assert(err, jc.ErrorIsNil) 212 213 err = s.machine1.SetRebootFlag(true) 214 c.Assert(err, jc.ErrorIsNil) 215 216 args := params.Entities{Entities: []params.Entity{ 217 {Tag: s.machine0.Tag().String()}, 218 {Tag: s.machine1.Tag().String()}, 219 }} 220 221 rFlag, err := s.machine1.GetRebootFlag() 222 c.Assert(err, jc.ErrorIsNil) 223 c.Assert(rFlag, jc.IsTrue) 224 225 result, err := api.ClearReboot(args) 226 c.Assert(err, jc.ErrorIsNil) 227 c.Assert(result, gc.DeepEquals, params.ErrorResults{ 228 Results: []params.ErrorResult{ 229 {apiservertesting.ErrUnauthorized}, 230 {nil}, 231 }, 232 }) 233 234 rFlag, err = s.machine1.GetRebootFlag() 235 c.Assert(err, jc.ErrorIsNil) 236 c.Assert(rFlag, jc.IsFalse) 237 }