func (s *firewallerBaseSuite) testLife( c *gc.C, facade interface { Life(args params.Entities) (params.LifeResults, error) }, ) { // Unassign unit 1 from its machine, so we can change its life cycle. err := s.units[1].UnassignFromMachine() c.Assert(err, jc.ErrorIsNil) err = s.machines[1].EnsureDead() c.Assert(err, jc.ErrorIsNil) s.assertLife(c, 0, state.Alive) s.assertLife(c, 1, state.Dead) s.assertLife(c, 2, state.Alive) args := addFakeEntities(params.Entities{Entities: []params.Entity{ {Tag: s.machines[0].Tag().String()}, {Tag: s.machines[1].Tag().String()}, {Tag: s.machines[2].Tag().String()}, }}) result, err := facade.Life(args) c.Assert(err, jc.ErrorIsNil) c.Assert(result, jc.DeepEquals, params.LifeResults{ Results: []params.LifeResult{ {Life: "alive"}, {Life: "dead"}, {Life: "alive"}, {Error: apiservertesting.NotFoundError("machine 42")}, {Error: apiservertesting.NotFoundError(`unit "foo/0"`)}, {Error: apiservertesting.NotFoundError(`service "bar"`)}, {Error: apiservertesting.ErrUnauthorized}, {Error: apiservertesting.ErrUnauthorized}, {Error: apiservertesting.ErrUnauthorized}, }, }) // Remove a machine and make sure it's detected. err = s.machines[1].Remove() c.Assert(err, jc.ErrorIsNil) err = s.machines[1].Refresh() c.Assert(err, jc.Satisfies, errors.IsNotFound) args = params.Entities{ Entities: []params.Entity{ {Tag: s.machines[1].Tag().String()}, }, } result, err = facade.Life(args) c.Assert(err, jc.ErrorIsNil) c.Assert(result, jc.DeepEquals, params.LifeResults{ Results: []params.LifeResult{ {Error: apiservertesting.NotFoundError("machine 1")}, }, }) }
func (s *withoutStateServerSuite) TestLifeAsEnvironManager(c *gc.C) { err := s.machines[1].EnsureDead() c.Assert(err, gc.IsNil) err = s.machines[1].Refresh() c.Assert(err, gc.IsNil) c.Assert(s.machines[0].Life(), gc.Equals, state.Alive) c.Assert(s.machines[1].Life(), gc.Equals, state.Dead) c.Assert(s.machines[2].Life(), gc.Equals, state.Alive) args := params.Entities{Entities: []params.Entity{ {Tag: s.machines[0].Tag().String()}, {Tag: s.machines[1].Tag().String()}, {Tag: s.machines[2].Tag().String()}, {Tag: "machine-42"}, {Tag: "unit-foo-0"}, {Tag: "service-bar"}, }} result, err := s.provisioner.Life(args) c.Assert(err, gc.IsNil) c.Assert(result, gc.DeepEquals, params.LifeResults{ Results: []params.LifeResult{ {Life: "alive"}, {Life: "dead"}, {Life: "alive"}, {Error: apiservertesting.NotFoundError("machine 42")}, {Error: apiservertesting.ErrUnauthorized}, {Error: apiservertesting.ErrUnauthorized}, }, }) // Remove the subordinate and make sure it's detected. err = s.machines[1].Remove() c.Assert(err, gc.IsNil) err = s.machines[1].Refresh() c.Assert(err, jc.Satisfies, errors.IsNotFound) result, err = s.provisioner.Life(params.Entities{ Entities: []params.Entity{ {Tag: s.machines[1].Tag().String()}, }, }) c.Assert(err, gc.IsNil) c.Assert(result, gc.DeepEquals, params.LifeResults{ Results: []params.LifeResult{ {Error: apiservertesting.NotFoundError("machine 1")}, }, }) }
func (s *withoutStateServerSuite) TestProvisioningInfoPermissions(c *gc.C) { // Login as a machine agent for machine 0. anAuthorizer := s.authorizer anAuthorizer.EnvironManager = false anAuthorizer.Tag = s.machines[0].Tag() aProvisioner, err := provisioner.NewProvisionerAPI(s.State, s.resources, anAuthorizer) c.Assert(err, gc.IsNil) c.Assert(aProvisioner, gc.NotNil) args := params.Entities{Entities: []params.Entity{ {Tag: s.machines[0].Tag().String()}, {Tag: s.machines[0].Tag().String() + "-lxc-0"}, {Tag: "machine-42"}, {Tag: s.machines[1].Tag().String()}, {Tag: "service-bar"}, }} // Only machine 0 and containers therein can be accessed. results, err := aProvisioner.ProvisioningInfo(args) c.Assert(results, gc.DeepEquals, params.ProvisioningInfoResults{ Results: []params.ProvisioningInfoResult{ {Result: ¶ms.ProvisioningInfo{ Series: "quantal", Networks: []string{}, Jobs: []params.MachineJob{params.JobHostUnits}, }}, {Error: apiservertesting.NotFoundError("machine 0/lxc/0")}, {Error: apiservertesting.ErrUnauthorized}, {Error: apiservertesting.ErrUnauthorized}, {Error: apiservertesting.ErrUnauthorized}, }, }) }
func (s *withoutStateServerSuite) TestDistributionGroupMachineAgentAuth(c *gc.C) { anAuthorizer := s.authorizer anAuthorizer.Tag = names.NewMachineTag("1") anAuthorizer.EnvironManager = false provisioner, err := provisioner.NewProvisionerAPI(s.State, s.resources, anAuthorizer) c.Check(err, gc.IsNil) args := params.Entities{Entities: []params.Entity{ {Tag: "machine-0"}, {Tag: "machine-1"}, {Tag: "machine-42"}, {Tag: "machine-0-lxc-99"}, {Tag: "machine-1-lxc-99"}, {Tag: "machine-1-lxc-99-lxc-100"}, }} result, err := provisioner.DistributionGroup(args) c.Assert(err, gc.IsNil) c.Assert(result, gc.DeepEquals, params.DistributionGroupResults{ Results: []params.DistributionGroupResult{ {Error: apiservertesting.ErrUnauthorized}, {Result: []instance.Id{}}, {Error: apiservertesting.ErrUnauthorized}, // only a machine agent for the container or its // parent may access it. {Error: apiservertesting.ErrUnauthorized}, {Error: apiservertesting.NotFoundError("machine 1/lxc/99")}, {Error: apiservertesting.ErrUnauthorized}, }, }) }
func (s *withoutStateServerSuite) TestSeries(c *gc.C) { // Add a machine with different series. foobarMachine, err := s.State.AddMachine("foobar", state.JobHostUnits) c.Assert(err, gc.IsNil) args := params.Entities{Entities: []params.Entity{ {Tag: s.machines[0].Tag().String()}, {Tag: foobarMachine.Tag().String()}, {Tag: s.machines[2].Tag().String()}, {Tag: "machine-42"}, {Tag: "unit-foo-0"}, {Tag: "service-bar"}, }} result, err := s.provisioner.Series(args) c.Assert(err, gc.IsNil) c.Assert(result, gc.DeepEquals, params.StringResults{ Results: []params.StringResult{ {Result: s.machines[0].Series()}, {Result: foobarMachine.Series()}, {Result: s.machines[2].Series()}, {Error: apiservertesting.NotFoundError("machine 42")}, {Error: apiservertesting.ErrUnauthorized}, {Error: apiservertesting.ErrUnauthorized}, }, }) }
func (s *withoutStateServerSuite) TestStatus(c *gc.C) { err := s.machines[0].SetStatus(params.StatusStarted, "blah", nil) c.Assert(err, gc.IsNil) err = s.machines[1].SetStatus(params.StatusStopped, "foo", nil) c.Assert(err, gc.IsNil) err = s.machines[2].SetStatus(params.StatusError, "not really", map[string]interface{}{"foo": "bar"}) c.Assert(err, gc.IsNil) args := params.Entities{Entities: []params.Entity{ {Tag: s.machines[0].Tag().String()}, {Tag: s.machines[1].Tag().String()}, {Tag: s.machines[2].Tag().String()}, {Tag: "machine-42"}, {Tag: "unit-foo-0"}, {Tag: "service-bar"}, }} result, err := s.provisioner.Status(args) c.Assert(err, gc.IsNil) c.Assert(result, gc.DeepEquals, params.StatusResults{ Results: []params.StatusResult{ {Status: params.StatusStarted, Info: "blah", Data: map[string]interface{}{}}, {Status: params.StatusStopped, Info: "foo", Data: map[string]interface{}{}}, {Status: params.StatusError, Info: "not really", Data: map[string]interface{}{"foo": "bar"}}, {Error: apiservertesting.NotFoundError("machine 42")}, {Error: apiservertesting.ErrUnauthorized}, {Error: apiservertesting.ErrUnauthorized}, }, }) }
func (s *withoutStateServerSuite) TestEnsureDead(c *gc.C) { err := s.machines[1].EnsureDead() c.Assert(err, gc.IsNil) s.assertLife(c, 0, state.Alive) s.assertLife(c, 1, state.Dead) s.assertLife(c, 2, state.Alive) args := params.Entities{Entities: []params.Entity{ {Tag: s.machines[0].Tag().String()}, {Tag: s.machines[1].Tag().String()}, {Tag: s.machines[2].Tag().String()}, {Tag: "machine-42"}, {Tag: "unit-foo-0"}, {Tag: "service-bar"}, }} result, err := s.provisioner.EnsureDead(args) c.Assert(err, gc.IsNil) c.Assert(result, gc.DeepEquals, params.ErrorResults{ Results: []params.ErrorResult{ {nil}, {nil}, {nil}, {apiservertesting.NotFoundError("machine 42")}, {apiservertesting.ErrUnauthorized}, {apiservertesting.ErrUnauthorized}, }, }) // Verify the changes. s.assertLife(c, 0, state.Dead) s.assertLife(c, 1, state.Dead) s.assertLife(c, 2, state.Dead) }
func (s *withoutStateServerSuite) TestInstanceId(c *gc.C) { // Provision 2 machines first. err := s.machines[0].SetProvisioned("i-am", "fake_nonce", nil) c.Assert(err, gc.IsNil) hwChars := instance.MustParseHardware("arch=i386", "mem=4G") err = s.machines[1].SetProvisioned("i-am-not", "fake_nonce", &hwChars) c.Assert(err, gc.IsNil) args := params.Entities{Entities: []params.Entity{ {Tag: s.machines[0].Tag().String()}, {Tag: s.machines[1].Tag().String()}, {Tag: s.machines[2].Tag().String()}, {Tag: "machine-42"}, {Tag: "unit-foo-0"}, {Tag: "service-bar"}, }} result, err := s.provisioner.InstanceId(args) c.Assert(err, gc.IsNil) c.Assert(result, gc.DeepEquals, params.StringResults{ Results: []params.StringResult{ {Result: "i-am"}, {Result: "i-am-not"}, {Error: apiservertesting.NotProvisionedError("2")}, {Error: apiservertesting.NotFoundError("machine 42")}, {Error: apiservertesting.ErrUnauthorized}, {Error: apiservertesting.ErrUnauthorized}, }, }) }
func (s *firewallerSuite) TestGetMachineActiveNetworks(c *gc.C) { s.openPorts(c) args := addFakeEntities(params.Entities{Entities: []params.Entity{ {Tag: s.machines[0].Tag().String()}, {Tag: s.machines[1].Tag().String()}, {Tag: s.machines[2].Tag().String()}, {Tag: s.service.Tag().String()}, {Tag: s.units[0].Tag().String()}, }}) networkTag := names.NewNetworkTag(network.DefaultPublic) expectResults := []string{networkTag.String()} result, err := s.firewaller.GetMachineActiveNetworks(args) c.Assert(err, jc.ErrorIsNil) c.Assert(result, jc.DeepEquals, params.StringsResults{ Results: []params.StringsResult{ {Result: expectResults}, {Result: nil, Error: nil}, {Result: expectResults}, {Error: apiservertesting.ErrUnauthorized}, {Error: apiservertesting.ErrUnauthorized}, {Error: apiservertesting.NotFoundError("machine 42")}, {Error: apiservertesting.ErrUnauthorized}, {Error: apiservertesting.ErrUnauthorized}, {Error: apiservertesting.ErrUnauthorized}, {Error: apiservertesting.ErrUnauthorized}, {Error: apiservertesting.ErrUnauthorized}, }, }) }
func (s *imageManagerSuite) TestDeleteImages(c *gc.C) { s.addImage(c, "image") args := params.ImageFilterParams{ Images: []params.ImageSpec{ { Kind: "lxc", Series: "trusty", Arch: "amd64", }, { Kind: "lxc", Series: "precise", Arch: "amd64", }, }, } results, err := s.imagemanager.DeleteImages(args) c.Assert(err, jc.ErrorIsNil) c.Assert(results, gc.DeepEquals, params.ErrorResults{ Results: []params.ErrorResult{ {Error: nil}, {Error: apiservertesting.NotFoundError("image lxc/precise/amd64")}, }, }) stor := s.State.ImageStorage() _, _, err = stor.Image("lxc", "trusty", "amd64") c.Assert(err, gc.ErrorMatches, ".*-lxc-trusty-amd64 image metadata not found") }
func (s *withoutControllerSuite) TestRemove(c *gc.C) { err := s.machines[1].EnsureDead() c.Assert(err, jc.ErrorIsNil) s.assertLife(c, 0, state.Alive) s.assertLife(c, 1, state.Dead) s.assertLife(c, 2, state.Alive) args := params.Entities{Entities: []params.Entity{ {Tag: s.machines[0].Tag().String()}, {Tag: s.machines[1].Tag().String()}, {Tag: s.machines[2].Tag().String()}, {Tag: "machine-42"}, {Tag: "unit-foo-0"}, {Tag: "service-bar"}, }} result, err := s.provisioner.Remove(args) c.Assert(err, jc.ErrorIsNil) c.Assert(result, gc.DeepEquals, params.ErrorResults{ Results: []params.ErrorResult{ {¶ms.Error{Message: `cannot remove entity "machine-0": still alive`}}, {nil}, {¶ms.Error{Message: `cannot remove entity "machine-2": still alive`}}, {apiservertesting.NotFoundError("machine 42")}, {apiservertesting.ErrUnauthorized}, {apiservertesting.ErrUnauthorized}, }, }) // Verify the changes. s.assertLife(c, 0, state.Alive) err = s.machines[2].Refresh() c.Assert(err, jc.ErrorIsNil) s.assertLife(c, 2, state.Alive) }
func (s *withoutStateServerSuite) TestConstraints(c *gc.C) { // Add a machine with some constraints. cons := constraints.MustParse("cpu-cores=123", "mem=8G", "networks=net3,^net4") template := state.MachineTemplate{ Series: "quantal", Jobs: []state.MachineJob{state.JobHostUnits}, Constraints: cons, } consMachine, err := s.State.AddOneMachine(template) c.Assert(err, gc.IsNil) machine0Constraints, err := s.machines[0].Constraints() c.Assert(err, gc.IsNil) args := params.Entities{Entities: []params.Entity{ {Tag: s.machines[0].Tag().String()}, {Tag: consMachine.Tag().String()}, {Tag: "machine-42"}, {Tag: "unit-foo-0"}, {Tag: "service-bar"}, }} result, err := s.provisioner.Constraints(args) c.Assert(err, gc.IsNil) c.Assert(result, gc.DeepEquals, params.ConstraintsResults{ Results: []params.ConstraintsResult{ {Constraints: machine0Constraints}, {Constraints: template.Constraints}, {Error: apiservertesting.NotFoundError("machine 42")}, {Error: apiservertesting.ErrUnauthorized}, {Error: apiservertesting.ErrUnauthorized}, }, }) }
func (s *toolsSuite) TestTools(c *gc.C) { getCanRead := func() (common.AuthFunc, error) { return func(tag names.Tag) bool { return tag == names.NewMachineTag("0") || tag == names.NewMachineTag("42") }, nil } tg := common.NewToolsGetter(s.State, s.State, s.State, sprintfURLGetter("tools:%s"), getCanRead) c.Assert(tg, gc.NotNil) err := s.machine0.SetAgentVersion(current) c.Assert(err, jc.ErrorIsNil) args := params.Entities{ Entities: []params.Entity{ {Tag: "machine-0"}, {Tag: "machine-1"}, {Tag: "machine-42"}, }} result, err := tg.Tools(args) c.Assert(err, jc.ErrorIsNil) c.Assert(result.Results, gc.HasLen, 3) c.Assert(result.Results[0].Error, gc.IsNil) c.Assert(result.Results[0].Tools, gc.NotNil) c.Assert(result.Results[0].Tools.Version, gc.DeepEquals, current) c.Assert(result.Results[0].Tools.URL, gc.Equals, "tools:"+current.String()) c.Assert(result.Results[0].DisableSSLHostnameVerification, jc.IsTrue) c.Assert(result.Results[1].Error, gc.DeepEquals, apiservertesting.ErrUnauthorized) c.Assert(result.Results[2].Error, gc.DeepEquals, apiservertesting.NotFoundError("machine 42")) }
func (s *firewallerSuite) TestGetMachineActiveSubnets(c *gc.C) { s.openPorts(c) subnetTag := names.NewSubnetTag("10.20.30.0/24").String() args := addFakeEntities(params.Entities{Entities: []params.Entity{ {Tag: s.machines[0].Tag().String()}, {Tag: s.machines[1].Tag().String()}, {Tag: s.machines[2].Tag().String()}, {Tag: s.service.Tag().String()}, {Tag: s.units[0].Tag().String()}, }}) expectResultsMachine0 := []string{subnetTag, ""} expectResultsMachine2 := []string{""} result, err := s.firewaller.GetMachineActiveSubnets(args) c.Assert(err, jc.ErrorIsNil) c.Assert(result, jc.DeepEquals, params.StringsResults{ Results: []params.StringsResult{ {Result: expectResultsMachine0}, {Result: nil, Error: nil}, {Result: expectResultsMachine2}, {Error: apiservertesting.ServerError(`"application-wordpress" is not a valid machine tag`)}, {Error: apiservertesting.ServerError(`"unit-wordpress-0" is not a valid machine tag`)}, {Error: apiservertesting.NotFoundError("machine 42")}, {Error: apiservertesting.ServerError(`"unit-foo-0" is not a valid machine tag`)}, {Error: apiservertesting.ServerError(`"application-bar" is not a valid machine tag`)}, {Error: apiservertesting.ServerError(`"user-foo" is not a valid machine tag`)}, {Error: apiservertesting.ServerError(`"foo-bar" is not a valid tag`)}, {Error: apiservertesting.ServerError(`"" is not a valid tag`)}, }, }) }
func (s *InstancePollerSuite) TestAreManuallyProvisionedSuccess(c *gc.C) { s.st.SetMachineInfo(c, machineInfo{id: "1", isManual: true}) s.st.SetMachineInfo(c, machineInfo{id: "2", isManual: false}) result, err := s.api.AreManuallyProvisioned(s.mixedEntities) c.Assert(err, jc.ErrorIsNil) c.Assert(result, jc.DeepEquals, params.BoolResults{ Results: []params.BoolResult{ {Result: true}, {Result: false}, {Error: apiservertesting.NotFoundError("machine 42")}, {Error: apiservertesting.ServerError(`"application-unknown" is not a valid machine tag`)}, {Error: apiservertesting.ServerError(`"invalid-tag" is not a valid tag`)}, {Error: apiservertesting.ServerError(`"unit-missing-1" is not a valid machine tag`)}, {Error: apiservertesting.ServerError(`"" is not a valid tag`)}, {Error: apiservertesting.ServerError(`"42" is not a valid tag`)}, }}, ) s.st.CheckFindEntityCall(c, 0, "1") s.st.CheckCall(c, 1, "IsManual") s.st.CheckFindEntityCall(c, 2, "2") s.st.CheckCall(c, 3, "IsManual") s.st.CheckFindEntityCall(c, 4, "42") }
func (s *InstancePollerSuite) TestLifeSuccess(c *gc.C) { s.st.SetMachineInfo(c, machineInfo{id: "1", life: state.Alive}) s.st.SetMachineInfo(c, machineInfo{id: "2", life: state.Dying}) result, err := s.api.Life(s.mixedEntities) c.Assert(err, jc.ErrorIsNil) c.Assert(result, jc.DeepEquals, params.LifeResults{ Results: []params.LifeResult{ {Life: params.Alive}, {Life: params.Dying}, {Error: apiservertesting.NotFoundError("machine 42")}, {Error: apiservertesting.ErrUnauthorized}, {Error: apiservertesting.ErrUnauthorized}, {Error: apiservertesting.ErrUnauthorized}, {Error: apiservertesting.ErrUnauthorized}, {Error: apiservertesting.ErrUnauthorized}, }}, ) s.st.CheckFindEntityCall(c, 0, "1") s.st.CheckCall(c, 1, "Life") s.st.CheckFindEntityCall(c, 2, "2") s.st.CheckCall(c, 3, "Life") s.st.CheckFindEntityCall(c, 4, "42") }
func (s *InstancePollerSuite) TestInstanceStatusSuccess(c *gc.C) { s.st.SetMachineInfo(c, machineInfo{id: "1", instanceStatus: statusInfo("foo")}) s.st.SetMachineInfo(c, machineInfo{id: "2", instanceStatus: statusInfo("")}) result, err := s.api.InstanceStatus(s.mixedEntities) c.Assert(err, jc.ErrorIsNil) c.Assert(result, jc.DeepEquals, params.StatusResults{ Results: []params.StatusResult{ {Status: "foo"}, {Status: ""}, {Error: apiservertesting.NotFoundError("machine 42")}, {Error: apiservertesting.ServerError(`"application-unknown" is not a valid machine tag`)}, {Error: apiservertesting.ServerError(`"invalid-tag" is not a valid tag`)}, {Error: apiservertesting.ServerError(`"unit-missing-1" is not a valid machine tag`)}, {Error: apiservertesting.ServerError(`"" is not a valid tag`)}, {Error: apiservertesting.ServerError(`"42" is not a valid tag`)}, }, }, ) s.st.CheckFindEntityCall(c, 0, "1") s.st.CheckCall(c, 1, "InstanceStatus") s.st.CheckFindEntityCall(c, 2, "2") s.st.CheckCall(c, 3, "InstanceStatus") s.st.CheckFindEntityCall(c, 4, "42") }
func (s *InstancePollerSuite) TestProviderAddressesSuccess(c *gc.C) { addrs := network.NewAddresses("0.1.2.3", "127.0.0.1", "8.8.8.8") expectedAddresses := params.FromNetworkAddresses(addrs...) s.st.SetMachineInfo(c, machineInfo{id: "1", providerAddresses: addrs}) s.st.SetMachineInfo(c, machineInfo{id: "2", providerAddresses: nil}) result, err := s.api.ProviderAddresses(s.mixedEntities) c.Assert(err, jc.ErrorIsNil) c.Assert(result, jc.DeepEquals, params.MachineAddressesResults{ Results: []params.MachineAddressesResult{ {Addresses: expectedAddresses}, {Addresses: nil}, {Error: apiservertesting.NotFoundError("machine 42")}, {Error: apiservertesting.ServerError(`"application-unknown" is not a valid machine tag`)}, {Error: apiservertesting.ServerError(`"invalid-tag" is not a valid tag`)}, {Error: apiservertesting.ServerError(`"unit-missing-1" is not a valid machine tag`)}, {Error: apiservertesting.ServerError(`"" is not a valid tag`)}, {Error: apiservertesting.ServerError(`"42" is not a valid tag`)}, }}, ) s.st.CheckFindEntityCall(c, 0, "1") s.st.CheckCall(c, 1, "ProviderAddresses") s.st.CheckFindEntityCall(c, 2, "2") s.st.CheckCall(c, 3, "ProviderAddresses") s.st.CheckFindEntityCall(c, 4, "42") }
func (s *InstancePollerSuite) TestInstanceIdSuccess(c *gc.C) { s.st.SetMachineInfo(c, machineInfo{id: "1", instanceId: "i-foo"}) s.st.SetMachineInfo(c, machineInfo{id: "2", instanceId: ""}) result, err := s.api.InstanceId(s.mixedEntities) c.Assert(err, jc.ErrorIsNil) c.Assert(result, jc.DeepEquals, params.StringResults{ Results: []params.StringResult{ {Result: "i-foo"}, {Result: ""}, {Error: apiservertesting.NotFoundError("machine 42")}, {Error: apiservertesting.ErrUnauthorized}, {Error: apiservertesting.ErrUnauthorized}, {Error: apiservertesting.ErrUnauthorized}, {Error: apiservertesting.ErrUnauthorized}, {Error: apiservertesting.ErrUnauthorized}, }}, ) s.st.CheckFindEntityCall(c, 0, "1") s.st.CheckCall(c, 1, "InstanceId") s.st.CheckFindEntityCall(c, 2, "2") s.st.CheckCall(c, 3, "InstanceId") s.st.CheckFindEntityCall(c, 4, "42") }
func (s *withoutControllerSuite) TestStatus(c *gc.C) { now := time.Now() sInfo := status.StatusInfo{ Status: status.Started, Message: "blah", Since: &now, } err := s.machines[0].SetStatus(sInfo) c.Assert(err, jc.ErrorIsNil) sInfo = status.StatusInfo{ Status: status.Stopped, Message: "foo", Since: &now, } err = s.machines[1].SetStatus(sInfo) c.Assert(err, jc.ErrorIsNil) sInfo = status.StatusInfo{ Status: status.Error, Message: "not really", Data: map[string]interface{}{"foo": "bar"}, Since: &now, } err = s.machines[2].SetStatus(sInfo) c.Assert(err, jc.ErrorIsNil) args := params.Entities{Entities: []params.Entity{ {Tag: s.machines[0].Tag().String()}, {Tag: s.machines[1].Tag().String()}, {Tag: s.machines[2].Tag().String()}, {Tag: "machine-42"}, {Tag: "unit-foo-0"}, {Tag: "application-bar"}, }} result, err := s.provisioner.Status(args) c.Assert(err, jc.ErrorIsNil) // Zero out the updated timestamps so we can easily check the results. for i, statusResult := range result.Results { r := statusResult if r.Status != "" { c.Assert(r.Since, gc.NotNil) } r.Since = nil result.Results[i] = r } c.Assert(result, gc.DeepEquals, params.StatusResults{ Results: []params.StatusResult{ {Status: status.Started.String(), Info: "blah", Data: map[string]interface{}{}}, {Status: status.Stopped.String(), Info: "foo", Data: map[string]interface{}{}}, {Status: status.Error.String(), Info: "not really", Data: map[string]interface{}{"foo": "bar"}}, {Error: apiservertesting.NotFoundError("machine 42")}, {Error: apiservertesting.ErrUnauthorized}, {Error: apiservertesting.ErrUnauthorized}, }, }) }
func (s *InstancePollerSuite) SetUpTest(c *gc.C) { s.BaseSuite.SetUpTest(c) s.authoriser = apiservertesting.FakeAuthorizer{ EnvironManager: true, } s.resources = common.NewResources() s.AddCleanup(func(*gc.C) { s.resources.StopAll() }) s.st = NewMockState() instancepoller.PatchState(s, s.st) var err error s.clock = jujutesting.NewClock(time.Now()) s.api, err = instancepoller.NewInstancePollerAPI(nil, s.resources, s.authoriser, s.clock) c.Assert(err, jc.ErrorIsNil) s.machineEntities = params.Entities{ Entities: []params.Entity{ {Tag: "machine-1"}, {Tag: "machine-2"}, {Tag: "machine-3"}, }} s.machineErrorResults = params.ErrorResults{ Results: []params.ErrorResult{ {Error: apiservertesting.ServerError("pow!")}, {Error: apiservertesting.ServerError("FAIL")}, {Error: apiservertesting.NotProvisionedError("42")}, }} s.mixedEntities = params.Entities{ Entities: []params.Entity{ {Tag: "machine-1"}, {Tag: "machine-2"}, {Tag: "machine-42"}, {Tag: "application-unknown"}, {Tag: "invalid-tag"}, {Tag: "unit-missing-1"}, {Tag: ""}, {Tag: "42"}, }} s.mixedErrorResults = params.ErrorResults{ Results: []params.ErrorResult{ {Error: nil}, {Error: nil}, {Error: apiservertesting.NotFoundError("machine 42")}, {Error: apiservertesting.ServerError(`"application-unknown" is not a valid machine tag`)}, {Error: apiservertesting.ServerError(`"invalid-tag" is not a valid tag`)}, {Error: apiservertesting.ServerError(`"unit-missing-1" is not a valid machine tag`)}, {Error: apiservertesting.ServerError(`"" is not a valid tag`)}, {Error: apiservertesting.ServerError(`"42" is not a valid tag`)}, }} }
func (s *withoutControllerSuite) TestSetStatus(c *gc.C) { now := time.Now() sInfo := status.StatusInfo{ Status: status.Started, Message: "blah", Since: &now, } err := s.machines[0].SetStatus(sInfo) c.Assert(err, jc.ErrorIsNil) sInfo = status.StatusInfo{ Status: status.Stopped, Message: "foo", Since: &now, } err = s.machines[1].SetStatus(sInfo) c.Assert(err, jc.ErrorIsNil) sInfo = status.StatusInfo{ Status: status.Error, Message: "not really", Since: &now, } err = s.machines[2].SetStatus(sInfo) c.Assert(err, jc.ErrorIsNil) args := params.SetStatus{ Entities: []params.EntityStatusArgs{ {Tag: s.machines[0].Tag().String(), Status: status.Error.String(), Info: "not really", Data: map[string]interface{}{"foo": "bar"}}, {Tag: s.machines[1].Tag().String(), Status: status.Stopped.String(), Info: "foobar"}, {Tag: s.machines[2].Tag().String(), Status: status.Started.String(), Info: "again"}, {Tag: "machine-42", Status: status.Started.String(), Info: "blah"}, {Tag: "unit-foo-0", Status: status.Stopped.String(), Info: "foobar"}, {Tag: "application-bar", Status: status.Stopped.String(), Info: "foobar"}, }} result, err := s.provisioner.SetStatus(args) c.Assert(err, jc.ErrorIsNil) c.Assert(result, gc.DeepEquals, params.ErrorResults{ Results: []params.ErrorResult{ {nil}, {nil}, {nil}, {apiservertesting.NotFoundError("machine 42")}, {apiservertesting.ErrUnauthorized}, {apiservertesting.ErrUnauthorized}, }, }) // Verify the changes. s.assertStatus(c, 0, status.Error, "not really", map[string]interface{}{"foo": "bar"}) s.assertStatus(c, 1, status.Stopped, "foobar", map[string]interface{}{}) s.assertStatus(c, 2, status.Started, "again", map[string]interface{}{}) }
func (s *firewallerSuite) TestGetMachinePorts(c *gc.C) { s.openPorts(c) subnetTag := names.NewSubnetTag("10.20.30.0/24").String() args := params.MachinePortsParams{ Params: []params.MachinePorts{ {MachineTag: s.machines[0].Tag().String(), SubnetTag: ""}, {MachineTag: s.machines[0].Tag().String(), SubnetTag: subnetTag}, {MachineTag: s.machines[1].Tag().String(), SubnetTag: ""}, {MachineTag: s.machines[2].Tag().String(), SubnetTag: ""}, {MachineTag: s.machines[0].Tag().String(), SubnetTag: "invalid"}, {MachineTag: "machine-42", SubnetTag: ""}, {MachineTag: s.machines[0].Tag().String(), SubnetTag: "subnet-bad"}, }, } unit0Tag := s.units[0].Tag().String() expectPortsMachine0NoSubnet := []params.MachinePortRange{ {UnitTag: unit0Tag, PortRange: params.PortRange{ FromPort: 4321, ToPort: 4321, Protocol: "tcp", }}, } expectPortsMachine0WithSubnet := []params.MachinePortRange{ {UnitTag: unit0Tag, PortRange: params.PortRange{ FromPort: 1234, ToPort: 1400, Protocol: "tcp", }}, } unit2Tag := s.units[2].Tag().String() expectPortsMachine2 := []params.MachinePortRange{ {UnitTag: unit2Tag, PortRange: params.PortRange{ FromPort: 1111, ToPort: 2222, Protocol: "udp", }}, } result, err := s.firewaller.GetMachinePorts(args) c.Assert(err, jc.ErrorIsNil) c.Assert(result, jc.DeepEquals, params.MachinePortsResults{ Results: []params.MachinePortsResult{ {Ports: expectPortsMachine0NoSubnet}, {Ports: expectPortsMachine0WithSubnet}, {Error: nil, Ports: nil}, {Ports: expectPortsMachine2}, {Error: apiservertesting.ServerError(`"invalid" is not a valid tag`)}, {Error: apiservertesting.NotFoundError("machine 42")}, {Error: apiservertesting.ServerError(`"subnet-bad" is not a valid subnet tag`)}, }, }) }
func (s *firewallerBaseSuite) testGetAssignedMachine( c *gc.C, facade interface { GetAssignedMachine(args params.Entities) (params.StringResults, error) }, ) { // Unassign a unit first. err := s.units[2].UnassignFromMachine() c.Assert(err, jc.ErrorIsNil) args := addFakeEntities(params.Entities{Entities: []params.Entity{ {Tag: s.units[0].Tag().String()}, {Tag: s.units[1].Tag().String()}, {Tag: s.units[2].Tag().String()}, }}) result, err := facade.GetAssignedMachine(args) c.Assert(err, jc.ErrorIsNil) c.Assert(result, jc.DeepEquals, params.StringResults{ Results: []params.StringResult{ {Result: s.machines[0].Tag().String()}, {Result: s.machines[1].Tag().String()}, {Error: apiservertesting.NotAssignedError("wordpress/2")}, {Error: apiservertesting.ErrUnauthorized}, {Error: apiservertesting.NotFoundError(`unit "foo/0"`)}, {Error: apiservertesting.ErrUnauthorized}, {Error: apiservertesting.ErrUnauthorized}, {Error: apiservertesting.ErrUnauthorized}, {Error: apiservertesting.ErrUnauthorized}, }, }) // Now reset assign unit 2 again and check. err = s.units[2].AssignToMachine(s.machines[0]) c.Assert(err, jc.ErrorIsNil) args = params.Entities{Entities: []params.Entity{ {Tag: s.units[2].Tag().String()}, }} result, err = facade.GetAssignedMachine(args) c.Assert(err, jc.ErrorIsNil) c.Assert(result, jc.DeepEquals, params.StringResults{ Results: []params.StringResult{ {Result: s.machines[0].Tag().String()}, }, }) }
func (s *withoutStateServerSuite) TestSetProvisioned(c *gc.C) { // Provision machine 0 first. hwChars := instance.MustParseHardware("arch=i386", "mem=4G") err := s.machines[0].SetProvisioned("i-am", "fake_nonce", &hwChars) c.Assert(err, gc.IsNil) args := params.SetProvisioned{Machines: []params.MachineSetProvisioned{ {Tag: s.machines[0].Tag().String(), InstanceId: "i-was", Nonce: "fake_nonce", Characteristics: nil}, {Tag: s.machines[1].Tag().String(), InstanceId: "i-will", Nonce: "fake_nonce", Characteristics: &hwChars}, {Tag: s.machines[2].Tag().String(), InstanceId: "i-am-too", Nonce: "fake", Characteristics: nil}, {Tag: "machine-42", InstanceId: "", Nonce: "", Characteristics: nil}, {Tag: "unit-foo-0", InstanceId: "", Nonce: "", Characteristics: nil}, {Tag: "service-bar", InstanceId: "", Nonce: "", Characteristics: nil}, }} result, err := s.provisioner.SetProvisioned(args) c.Assert(err, gc.IsNil) c.Assert(result, gc.DeepEquals, params.ErrorResults{ Results: []params.ErrorResult{ {¶ms.Error{ Message: `cannot set instance data for machine "0": already set`, }}, {nil}, {nil}, {apiservertesting.NotFoundError("machine 42")}, {apiservertesting.ErrUnauthorized}, {apiservertesting.ErrUnauthorized}, }, }) // Verify machine 1 and 2 were provisioned. c.Assert(s.machines[1].Refresh(), gc.IsNil) c.Assert(s.machines[2].Refresh(), gc.IsNil) instanceId, err := s.machines[1].InstanceId() c.Assert(err, gc.IsNil) c.Check(instanceId, gc.Equals, instance.Id("i-will")) instanceId, err = s.machines[2].InstanceId() c.Assert(err, gc.IsNil) c.Check(instanceId, gc.Equals, instance.Id("i-am-too")) c.Check(s.machines[1].CheckProvisioned("fake_nonce"), jc.IsTrue) c.Check(s.machines[2].CheckProvisioned("fake"), jc.IsTrue) gotHardware, err := s.machines[1].HardwareCharacteristics() c.Assert(err, gc.IsNil) c.Check(gotHardware, gc.DeepEquals, &hwChars) }
func (s *firewallerSuite) TestGetMachinePorts(c *gc.C) { s.openPorts(c) networkTag := names.NewNetworkTag(network.DefaultPublic).String() args := params.MachinePortsParams{ Params: []params.MachinePorts{ {MachineTag: s.machines[0].Tag().String(), NetworkTag: networkTag}, {MachineTag: s.machines[1].Tag().String(), NetworkTag: networkTag}, {MachineTag: s.machines[2].Tag().String(), NetworkTag: networkTag}, {MachineTag: s.machines[0].Tag().String(), NetworkTag: "invalid"}, {MachineTag: "machine-42", NetworkTag: networkTag}, {MachineTag: s.machines[0].Tag().String(), NetworkTag: "network-missing"}, }, } unit0Tag := s.units[0].Tag().String() expectPortsMachine0 := []params.MachinePortRange{ {UnitTag: unit0Tag, PortRange: params.PortRange{ FromPort: 1234, ToPort: 1400, Protocol: "tcp", }}, {UnitTag: unit0Tag, PortRange: params.PortRange{ FromPort: 4321, ToPort: 4321, Protocol: "tcp", }}, } unit2Tag := s.units[2].Tag().String() expectPortsMachine2 := []params.MachinePortRange{ {UnitTag: unit2Tag, PortRange: params.PortRange{ FromPort: 1111, ToPort: 2222, Protocol: "udp", }}, } result, err := s.firewaller.GetMachinePorts(args) c.Assert(err, jc.ErrorIsNil) c.Assert(result, jc.DeepEquals, params.MachinePortsResults{ Results: []params.MachinePortsResult{ {Ports: expectPortsMachine0}, {Error: nil, Ports: nil}, {Ports: expectPortsMachine2}, {Error: apiservertesting.ErrUnauthorized}, {Error: apiservertesting.NotFoundError("machine 42")}, {Error: nil, Ports: nil}, }, }) }
func (s *InstancePollerSuite) TestStatusSuccess(c *gc.C) { now := time.Now() s1 := status.StatusInfo{ Status: status.Error, Message: "not really", Data: map[string]interface{}{ "price": 4.2, "bool": false, "bar": []string{"a", "b"}, }, Since: &now, } s2 := status.StatusInfo{} s.st.SetMachineInfo(c, machineInfo{id: "1", status: s1}) s.st.SetMachineInfo(c, machineInfo{id: "2", status: s2}) result, err := s.api.Status(s.mixedEntities) c.Assert(err, jc.ErrorIsNil) c.Assert(result, jc.DeepEquals, params.StatusResults{ Results: []params.StatusResult{ { Status: status.Error.String(), Info: s1.Message, Data: s1.Data, Since: s1.Since, }, {Status: "", Info: "", Data: nil, Since: nil}, {Error: apiservertesting.NotFoundError("machine 42")}, {Error: apiservertesting.ErrUnauthorized}, {Error: apiservertesting.ServerError(`"invalid-tag" is not a valid tag`)}, {Error: apiservertesting.ErrUnauthorized}, {Error: apiservertesting.ServerError(`"" is not a valid tag`)}, {Error: apiservertesting.ServerError(`"42" is not a valid tag`)}, }}, ) s.st.CheckFindEntityCall(c, 0, "1") s.st.CheckCall(c, 1, "Status") s.st.CheckFindEntityCall(c, 2, "2") s.st.CheckCall(c, 3, "Status") s.st.CheckFindEntityCall(c, 4, "42") }
func (s *firewallerBaseSuite) testGetExposed( c *gc.C, facade interface { GetExposed(args params.Entities) (params.BoolResults, error) }, ) { // Set the service to exposed first. err := s.service.SetExposed() c.Assert(err, jc.ErrorIsNil) args := addFakeEntities(params.Entities{Entities: []params.Entity{ {Tag: s.service.Tag().String()}, }}) result, err := facade.GetExposed(args) c.Assert(err, jc.ErrorIsNil) c.Assert(result, jc.DeepEquals, params.BoolResults{ Results: []params.BoolResult{ {Result: true}, {Error: apiservertesting.ErrUnauthorized}, {Error: apiservertesting.ErrUnauthorized}, {Error: apiservertesting.NotFoundError(`service "bar"`)}, {Error: apiservertesting.ErrUnauthorized}, {Error: apiservertesting.ErrUnauthorized}, {Error: apiservertesting.ErrUnauthorized}, }, }) // Now reset the exposed flag for the service and check again. err = s.service.ClearExposed() c.Assert(err, jc.ErrorIsNil) args = params.Entities{Entities: []params.Entity{ {Tag: s.service.Tag().String()}, }} result, err = facade.GetExposed(args) c.Assert(err, jc.ErrorIsNil) c.Assert(result, jc.DeepEquals, params.BoolResults{ Results: []params.BoolResult{ {Result: false}, }, }) }
func (s *facadeSuite) TestPrivateAddress(c *gc.C) { args := params.Entities{ Entities: []params.Entity{{s.uOther}, {s.m0}, {s.uFoo}}, } results, err := s.facade.PrivateAddress(args) c.Assert(err, jc.ErrorIsNil) c.Check(results, gc.DeepEquals, params.SSHAddressResults{ Results: []params.SSHAddressResult{ {Error: apiservertesting.NotFoundError("entity")}, {Address: "2.2.2.2"}, {Address: "4.4.4.4"}, }, }) s.backend.stub.CheckCalls(c, []jujutesting.StubCall{ {"GetMachineForEntity", []interface{}{s.uOther}}, {"GetMachineForEntity", []interface{}{s.m0}}, {"GetMachineForEntity", []interface{}{s.uFoo}}, }) }
func (s *withoutControllerSuite) TestProvisioningInfoPermissions(c *gc.C) { // Login as a machine agent for machine 0. anAuthorizer := s.authorizer anAuthorizer.EnvironManager = false anAuthorizer.Tag = s.machines[0].Tag() aProvisioner, err := provisioner.NewProvisionerAPI(s.State, s.resources, anAuthorizer) c.Assert(err, jc.ErrorIsNil) c.Assert(aProvisioner, gc.NotNil) args := params.Entities{Entities: []params.Entity{ {Tag: s.machines[0].Tag().String()}, {Tag: s.machines[0].Tag().String() + "-lxd-0"}, {Tag: "machine-42"}, {Tag: s.machines[1].Tag().String()}, {Tag: "application-bar"}, }} // Only machine 0 and containers therein can be accessed. results, err := aProvisioner.ProvisioningInfo(args) controllerCfg := coretesting.FakeControllerConfig() // Dummy provider uses a random port, which is added to cfg used to create environment. apiPort := dummy.ApiPort(s.Environ.Provider()) controllerCfg["api-port"] = apiPort c.Assert(results, jc.DeepEquals, params.ProvisioningInfoResults{ Results: []params.ProvisioningInfoResult{ {Result: ¶ms.ProvisioningInfo{ ControllerConfig: controllerCfg, Series: "quantal", Jobs: []multiwatcher.MachineJob{multiwatcher.JobHostUnits}, Tags: map[string]string{ tags.JujuController: coretesting.ControllerTag.Id(), tags.JujuModel: coretesting.ModelTag.Id(), }, }}, {Error: apiservertesting.NotFoundError("machine 0/lxd/0")}, {Error: apiservertesting.ErrUnauthorized}, {Error: apiservertesting.ErrUnauthorized}, {Error: apiservertesting.ErrUnauthorized}, }, }) }