github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/apiserver/facades/agent/provisioner/container_test.go (about) 1 // Copyright 2016 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package provisioner_test 5 6 import ( 7 jc "github.com/juju/testing/checkers" 8 gc "gopkg.in/check.v1" 9 10 "github.com/juju/juju/apiserver/facades/agent/provisioner" 11 "github.com/juju/juju/apiserver/params" 12 apiservertesting "github.com/juju/juju/apiserver/testing" 13 "github.com/juju/juju/core/instance" 14 "github.com/juju/juju/state" 15 ) 16 17 type containerProvisionerSuite struct { 18 provisionerSuite 19 } 20 21 var _ = gc.Suite(&containerProvisionerSuite{}) 22 23 func (s *containerProvisionerSuite) SetUpTest(c *gc.C) { 24 // We have a Controller machine, and 5 other machines to provision in 25 s.setUpTest(c, true) 26 } 27 28 func addContainerToMachine(c *gc.C, st *state.State, machine *state.Machine) *state.Machine { 29 // Add a container machine with machine as its host. 30 containerTemplate := state.MachineTemplate{ 31 Series: "quantal", 32 Jobs: []state.MachineJob{state.JobHostUnits}, 33 } 34 container, err := st.AddMachineInsideMachine(containerTemplate, machine.Id(), instance.LXD) 35 c.Assert(err, jc.ErrorIsNil) 36 return container 37 } 38 39 func (s *containerProvisionerSuite) TestPrepareContainerInterfaceInfoPermission(c *gc.C) { 40 // Login as a machine agent for machine 1, which has a container put on it 41 addContainerToMachine(c, s.State, s.machines[1]) 42 addContainerToMachine(c, s.State, s.machines[1]) 43 addContainerToMachine(c, s.State, s.machines[2]) 44 45 anAuthorizer := s.authorizer 46 anAuthorizer.Controller = false 47 anAuthorizer.Tag = s.machines[1].Tag() 48 aProvisioner, err := provisioner.NewProvisionerAPI(s.State, s.resources, anAuthorizer) 49 c.Assert(err, jc.ErrorIsNil) 50 c.Assert(aProvisioner, gc.NotNil) 51 52 args := params.Entities{ 53 Entities: []params.Entity{{ 54 Tag: "machine-1/lxd/0", // valid 55 }, { 56 Tag: "machine-1/lxd/1", // valid 57 }, { 58 Tag: "machine-2/lxd/0", // wrong host machine 59 }, { 60 Tag: "machine-2", // host machine 61 }, { 62 Tag: "unit-mysql-0", // not a valid machine tag 63 }}} 64 // Only machine 0 can have it's containers updated. 65 results, err := aProvisioner.PrepareContainerInterfaceInfo(args) 66 c.Assert(err, gc.ErrorMatches, "dummy provider network config not supported") 67 c.Skip("dummy provider needs networking https://pad.lv/1651974") 68 // Overall request is ok 69 c.Assert(err, jc.ErrorIsNil) 70 71 errors := make([]*params.Error, 0) 72 c.Check(results.Results, gc.HasLen, 4) 73 for _, configResult := range results.Results { 74 errors = append(errors, configResult.Error) 75 } 76 c.Check(errors, gc.DeepEquals, []*params.Error{ 77 nil, // can touch 1/lxd/0 78 nil, // can touch 1/lxd/1 79 apiservertesting.ErrUnauthorized, // not 2/lxd/0 80 apiservertesting.ErrUnauthorized, // nor 2 81 }) 82 } 83 84 // TODO(jam): Add a test for requesting PrepareContainerInterfaceInfo with a 85 // machine that is not yet provisioned. 86 87 func (s *containerProvisionerSuite) TestHostChangesForContainersPermission(c *gc.C) { 88 // Login as a machine agent for machine 1, which has a container put on it 89 addContainerToMachine(c, s.State, s.machines[1]) 90 addContainerToMachine(c, s.State, s.machines[1]) 91 addContainerToMachine(c, s.State, s.machines[2]) 92 93 anAuthorizer := s.authorizer 94 anAuthorizer.Controller = false 95 anAuthorizer.Tag = s.machines[1].Tag() 96 aProvisioner, err := provisioner.NewProvisionerAPI(s.State, s.resources, anAuthorizer) 97 c.Assert(err, jc.ErrorIsNil) 98 c.Assert(aProvisioner, gc.NotNil) 99 100 args := params.Entities{ 101 Entities: []params.Entity{{ 102 Tag: "machine-1/lxd/0", // valid 103 }, { 104 Tag: "machine-1/lxd/1", // valid 105 }, { 106 Tag: "machine-2/lxd/0", // wrong host machine 107 }, { 108 Tag: "machine-2", // host machine 109 }, { 110 Tag: "unit-mysql-0", // not a valid machine tag 111 }}} 112 // Only machine 0 can have it's containers updated. 113 results, err := aProvisioner.HostChangesForContainers(args) 114 c.Assert(err, gc.ErrorMatches, "dummy provider network config not supported") 115 c.Skip("dummy provider needs networking https://pad.lv/1651974") 116 // Overall request is ok 117 c.Assert(err, jc.ErrorIsNil) 118 119 errors := make([]*params.Error, 0) 120 c.Check(results.Results, gc.HasLen, 4) 121 for _, configResult := range results.Results { 122 errors = append(errors, configResult.Error) 123 } 124 c.Check(errors, gc.DeepEquals, []*params.Error{ 125 nil, // can touch 1/lxd/0 126 nil, // can touch 1/lxd/1 127 apiservertesting.ErrUnauthorized, // not 2/lxd/0 128 apiservertesting.ErrUnauthorized, // nor 2 129 }) 130 } 131 132 // TODO(jam): Add a test for requesting HostChangesForContainers with a 133 // machine that is not yet provisioned.