github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/apiserver/machinemanager/machinemanager_test.go (about) 1 // Copyright 2015 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package machinemanager_test 5 6 import ( 7 "errors" 8 9 "github.com/juju/names" 10 jc "github.com/juju/testing/checkers" 11 gc "gopkg.in/check.v1" 12 13 "github.com/juju/juju/apiserver/common" 14 "github.com/juju/juju/apiserver/machinemanager" 15 "github.com/juju/juju/apiserver/params" 16 apiservertesting "github.com/juju/juju/apiserver/testing" 17 "github.com/juju/juju/environs/config" 18 "github.com/juju/juju/instance" 19 "github.com/juju/juju/state" 20 "github.com/juju/juju/state/multiwatcher" 21 "github.com/juju/juju/storage" 22 coretesting "github.com/juju/juju/testing" 23 ) 24 25 var _ = gc.Suite(&MachineManagerSuite{}) 26 27 type MachineManagerSuite struct { 28 coretesting.BaseSuite 29 resources *common.Resources 30 authorizer *apiservertesting.FakeAuthorizer 31 st *mockState 32 api *machinemanager.MachineManagerAPI 33 } 34 35 func (s *MachineManagerSuite) SetUpTest(c *gc.C) { 36 s.BaseSuite.SetUpTest(c) 37 s.resources = common.NewResources() 38 tag := names.NewUserTag("admin") 39 s.authorizer = &apiservertesting.FakeAuthorizer{Tag: tag} 40 s.st = &mockState{} 41 machinemanager.PatchState(s, s.st) 42 43 var err error 44 s.api, err = machinemanager.NewMachineManagerAPI(nil, nil, s.authorizer) 45 c.Assert(err, jc.ErrorIsNil) 46 } 47 48 func (s *MachineManagerSuite) TestAddMachines(c *gc.C) { 49 apiParams := make([]params.AddMachineParams, 2) 50 for i := range apiParams { 51 apiParams[i] = params.AddMachineParams{ 52 Series: "trusty", 53 Jobs: []multiwatcher.MachineJob{multiwatcher.JobHostUnits}, 54 } 55 } 56 apiParams[0].Disks = []storage.Constraints{{Size: 1, Count: 2}, {Size: 2, Count: 1}} 57 apiParams[1].Disks = []storage.Constraints{{Size: 1, Count: 2, Pool: "three"}} 58 machines, err := s.api.AddMachines(params.AddMachines{MachineParams: apiParams}) 59 c.Assert(err, jc.ErrorIsNil) 60 c.Assert(machines.Machines, gc.HasLen, 2) 61 c.Assert(s.st.calls, gc.Equals, 2) 62 c.Assert(s.st.machines, jc.DeepEquals, []state.MachineTemplate{ 63 { 64 Series: "trusty", 65 Jobs: []state.MachineJob{state.JobHostUnits}, 66 Volumes: []state.MachineVolumeParams{ 67 { 68 Volume: state.VolumeParams{Pool: "", Size: 1}, 69 Attachment: state.VolumeAttachmentParams{ReadOnly: false}, 70 }, 71 { 72 Volume: state.VolumeParams{Pool: "", Size: 1}, 73 Attachment: state.VolumeAttachmentParams{ReadOnly: false}, 74 }, 75 { 76 Volume: state.VolumeParams{Pool: "", Size: 2}, 77 Attachment: state.VolumeAttachmentParams{ReadOnly: false}, 78 }, 79 }, 80 }, 81 { 82 Series: "trusty", 83 Jobs: []state.MachineJob{state.JobHostUnits}, 84 Volumes: []state.MachineVolumeParams{ 85 { 86 Volume: state.VolumeParams{Pool: "three", Size: 1}, 87 Attachment: state.VolumeAttachmentParams{ReadOnly: false}, 88 }, 89 { 90 Volume: state.VolumeParams{Pool: "three", Size: 1}, 91 Attachment: state.VolumeAttachmentParams{ReadOnly: false}, 92 }, 93 }, 94 }, 95 }) 96 } 97 98 func (s *MachineManagerSuite) TestNewMachineManagerAPINonClient(c *gc.C) { 99 tag := names.NewUnitTag("mysql/0") 100 s.authorizer = &apiservertesting.FakeAuthorizer{Tag: tag} 101 _, err := machinemanager.NewMachineManagerAPI(nil, nil, s.authorizer) 102 c.Assert(err, gc.ErrorMatches, "permission denied") 103 } 104 105 func (s *MachineManagerSuite) TestAddMachinesStateError(c *gc.C) { 106 s.st.err = errors.New("boom") 107 results, err := s.api.AddMachines(params.AddMachines{ 108 MachineParams: []params.AddMachineParams{{ 109 Series: "trusty", 110 }}, 111 }) 112 c.Assert(err, jc.ErrorIsNil) 113 c.Assert(results, gc.DeepEquals, params.AddMachinesResults{ 114 Machines: []params.AddMachinesResult{{ 115 Error: ¶ms.Error{Message: "boom", Code: ""}, 116 }}, 117 }) 118 c.Assert(s.st.calls, gc.Equals, 1) 119 } 120 121 type mockState struct { 122 calls int 123 machines []state.MachineTemplate 124 err error 125 } 126 127 func (st *mockState) AddOneMachine(template state.MachineTemplate) (*state.Machine, error) { 128 st.calls++ 129 st.machines = append(st.machines, template) 130 m := state.Machine{} 131 return &m, st.err 132 } 133 134 func (st *mockState) GetBlockForType(t state.BlockType) (state.Block, bool, error) { 135 return &mockBlock{}, false, nil 136 } 137 138 func (st *mockState) ModelConfig() (*config.Config, error) { 139 panic("not implemented") 140 } 141 142 func (st *mockState) Model() (*state.Model, error) { 143 panic("not implemented") 144 } 145 146 func (st *mockState) AddMachineInsideNewMachine(template, parentTemplate state.MachineTemplate, containerType instance.ContainerType) (*state.Machine, error) { 147 panic("not implemented") 148 } 149 150 func (st *mockState) AddMachineInsideMachine(template state.MachineTemplate, parentId string, containerType instance.ContainerType) (*state.Machine, error) { 151 panic("not implemented") 152 } 153 154 type mockBlock struct { 155 state.Block 156 } 157 158 func (st *mockBlock) Id() string { 159 return "id" 160 } 161 162 func (st *mockBlock) Tag() (names.Tag, error) { 163 return names.ParseTag("machine-1") 164 } 165 166 func (st *mockBlock) Type() state.BlockType { 167 return state.ChangeBlock 168 } 169 170 func (st *mockBlock) Message() string { 171 return "not allowed" 172 } 173 174 func (st *mockBlock) ModelUUID() string { 175 return "uuid" 176 }