github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/apiserver/facades/agent/uniter/lxdprofile_test.go (about) 1 // Copyright 2018 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package uniter_test 5 6 import ( 7 "github.com/golang/mock/gomock" 8 "github.com/juju/loggo" 9 jc "github.com/juju/testing/checkers" 10 gc "gopkg.in/check.v1" 11 names "gopkg.in/juju/names.v2" 12 13 "github.com/juju/juju/apiserver/common" 14 "github.com/juju/juju/apiserver/facades/agent/uniter" 15 "github.com/juju/juju/apiserver/facades/agent/uniter/mocks" 16 "github.com/juju/juju/apiserver/params" 17 apiservertesting "github.com/juju/juju/apiserver/testing" 18 "github.com/juju/juju/core/lxdprofile" 19 "github.com/juju/juju/testing" 20 ) 21 22 type lxdProfileSuite struct { 23 testing.BaseSuite 24 25 machineTag1 names.MachineTag 26 unitTag1 names.UnitTag 27 unitTag2 names.UnitTag 28 } 29 30 var _ = gc.Suite(&lxdProfileSuite{}) 31 32 func (s *lxdProfileSuite) SetUpTest(c *gc.C) { 33 s.machineTag1 = names.NewMachineTag("1") 34 s.unitTag1 = names.NewUnitTag("mysql/1") 35 s.unitTag2 = names.NewUnitTag("redis/1") 36 } 37 38 func (s *lxdProfileSuite) assertBackendAPI(c *gc.C, tag names.Tag) (*uniter.LXDProfileAPI, *gomock.Controller, *mocks.MockLXDProfileBackend) { 39 resources := common.NewResources() 40 authorizer := apiservertesting.FakeAuthorizer{ 41 Tag: tag, 42 } 43 44 ctrl := gomock.NewController(c) 45 mockBackend := mocks.NewMockLXDProfileBackend(ctrl) 46 47 unitAuthFunc := func() (common.AuthFunc, error) { 48 return func(tag names.Tag) bool { 49 if tag.Id() == s.unitTag1.Id() { 50 return true 51 } 52 return false 53 }, nil 54 } 55 56 machineAuthFunc := func() (common.AuthFunc, error) { 57 return func(tag names.Tag) bool { 58 if tag.Id() == s.machineTag1.Id() { 59 return true 60 } 61 return false 62 }, nil 63 } 64 65 api := uniter.NewLXDProfileAPI( 66 mockBackend, resources, authorizer, machineAuthFunc, unitAuthFunc, loggo.GetLogger("juju.apiserver.facades.agent.uniter")) 67 return api, ctrl, mockBackend 68 } 69 70 func (s *lxdProfileSuite) TestWatchLXDProfileUpgradeNotificationsUnitTag(c *gc.C) { 71 api, ctrl, mockBackend := s.assertBackendAPI(c, s.unitTag1) 72 defer ctrl.Finish() 73 74 lxdProfileWatcher := &mockStringsWatcher{ 75 changes: make(chan []string, 1), 76 } 77 lxdProfileWatcher.changes <- []string{lxdprofile.EmptyStatus} 78 79 mockMachine1 := mocks.NewMockLXDProfileMachine(ctrl) 80 mockUnit1 := mocks.NewMockLXDProfileUnit(ctrl) 81 82 mockBackend.EXPECT().Machine(s.machineTag1.Id()).Return(mockMachine1, nil) 83 mockBackend.EXPECT().Unit(s.unitTag1.Id()).Return(mockUnit1, nil) 84 mockMachine1.EXPECT().WatchLXDProfileUpgradeNotifications("foo-bar").Return(lxdProfileWatcher, nil) 85 mockUnit1.EXPECT().AssignedMachineId().Return(s.machineTag1.Id(), nil) 86 87 args := params.LXDProfileUpgrade{ 88 Entities: []params.Entity{ 89 {Tag: names.NewUnitTag("mysql/2").String()}, 90 {Tag: s.unitTag1.String()}, 91 }, 92 ApplicationName: "foo-bar", 93 } 94 watches, err := api.WatchLXDProfileUpgradeNotifications(args) 95 c.Assert(err, jc.ErrorIsNil) 96 c.Assert(watches, gc.DeepEquals, params.StringsWatchResults{ 97 Results: []params.StringsWatchResult{ 98 {StringsWatcherId: "", Error: ¶ms.Error{Message: "permission denied", Code: "unauthorized access"}}, 99 {StringsWatcherId: "1", Changes: []string{""}, Error: nil}, 100 }, 101 }) 102 } 103 104 func (s *lxdProfileSuite) TestWatchLXDProfileUpgradeNotificationsMachineTag(c *gc.C) { 105 api, ctrl, mockBackend := s.assertBackendAPI(c, s.machineTag1) 106 defer ctrl.Finish() 107 108 mockMachine := mocks.NewMockLXDProfileMachine(ctrl) 109 110 lxdProfileWatcher := &mockStringsWatcher{ 111 changes: make(chan []string, 1), 112 } 113 lxdProfileWatcher.changes <- []string{lxdprofile.EmptyStatus} 114 115 mockBackend.EXPECT().Machine(s.machineTag1.Id()).Return(mockMachine, nil) 116 mockMachine.EXPECT().WatchLXDProfileUpgradeNotifications("foo-bar").Return(lxdProfileWatcher, nil) 117 118 watches, err := api.WatchLXDProfileUpgradeNotifications( 119 params.LXDProfileUpgrade{ 120 Entities: []params.Entity{ 121 {Tag: s.machineTag1.String()}, 122 {Tag: names.NewMachineTag("7").String()}, 123 }, 124 ApplicationName: "foo-bar", 125 }, 126 ) 127 c.Assert(err, jc.ErrorIsNil) 128 c.Assert(watches, gc.DeepEquals, params.StringsWatchResults{ 129 Results: []params.StringsWatchResult{ 130 {StringsWatcherId: "1", Changes: []string{""}}, 131 {StringsWatcherId: "", Error: ¶ms.Error{Message: "permission denied", Code: "unauthorized access"}}, 132 }, 133 }) 134 }