github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/container/lxd/server_test.go (about) 1 // Copyright 2018 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package lxd_test 5 6 import ( 7 "github.com/golang/mock/gomock" 8 jc "github.com/juju/testing/checkers" 9 "github.com/lxc/lxd/shared/api" 10 gc "gopkg.in/check.v1" 11 12 "github.com/juju/juju/container/lxd" 13 lxdtesting "github.com/juju/juju/container/lxd/testing" 14 ) 15 16 type serverSuite struct { 17 lxdtesting.BaseSuite 18 } 19 20 var _ = gc.Suite(&serverSuite{}) 21 22 func (s *serverSuite) TestUpdateServerConfig(c *gc.C) { 23 ctrl := gomock.NewController(c) 24 defer ctrl.Finish() 25 cSvr := lxdtesting.NewMockContainerServer(ctrl) 26 27 updateReq := api.ServerPut{Config: map[string]interface{}{"key1": "val1"}} 28 gomock.InOrder( 29 cSvr.EXPECT().GetServer().Return(&api.Server{}, lxdtesting.ETag, nil).Times(2), 30 cSvr.EXPECT().UpdateServer(updateReq, lxdtesting.ETag).Return(nil), 31 ) 32 33 jujuSvr, err := lxd.NewServer(cSvr) 34 c.Assert(err, jc.ErrorIsNil) 35 36 err = jujuSvr.UpdateServerConfig(map[string]string{"key1": "val1"}) 37 c.Assert(err, jc.ErrorIsNil) 38 } 39 40 func (s *serverSuite) TestUpdateContainerConfig(c *gc.C) { 41 ctrl := gomock.NewController(c) 42 defer ctrl.Finish() 43 cSvr := lxdtesting.NewMockContainerServer(ctrl) 44 45 cName := "juju-lxd-1" 46 newConfig := map[string]string{"key1": "val1"} 47 updateReq := api.ContainerPut{Config: newConfig} 48 op := lxdtesting.NewMockOperation(ctrl) 49 gomock.InOrder( 50 cSvr.EXPECT().GetServer().Return(&api.Server{}, lxdtesting.ETag, nil), 51 cSvr.EXPECT().GetContainer(cName).Return(&api.Container{}, lxdtesting.ETag, nil), 52 cSvr.EXPECT().UpdateContainer(cName, updateReq, lxdtesting.ETag).Return(op, nil), 53 op.EXPECT().Wait().Return(nil), 54 ) 55 jujuSvr, err := lxd.NewServer(cSvr) 56 c.Assert(err, jc.ErrorIsNil) 57 58 err = jujuSvr.UpdateContainerConfig("juju-lxd-1", newConfig) 59 c.Assert(err, jc.ErrorIsNil) 60 } 61 62 func (s *serverSuite) TestHasProfile(c *gc.C) { 63 ctrl := gomock.NewController(c) 64 defer ctrl.Finish() 65 cSvr := s.NewMockServer(ctrl) 66 67 cSvr.EXPECT().GetProfileNames().Return([]string{"default", "custom"}, nil).Times(2) 68 69 jujuSvr, err := lxd.NewServer(cSvr) 70 c.Assert(err, jc.ErrorIsNil) 71 72 has, err := jujuSvr.HasProfile("default") 73 c.Assert(err, jc.ErrorIsNil) 74 c.Check(has, jc.IsTrue) 75 76 has, err = jujuSvr.HasProfile("unknown") 77 c.Assert(err, jc.ErrorIsNil) 78 c.Check(has, jc.IsFalse) 79 } 80 81 func (s *serverSuite) TestCreateProfileWithConfig(c *gc.C) { 82 ctrl := gomock.NewController(c) 83 defer ctrl.Finish() 84 cSvr := s.NewMockServer(ctrl) 85 86 req := api.ProfilesPost{ 87 Name: "custom", 88 ProfilePut: api.ProfilePut{ 89 Config: map[string]string{ 90 "boot.autostart": "false", 91 }, 92 }, 93 } 94 cSvr.EXPECT().CreateProfile(req).Return(nil) 95 96 jujuSvr, err := lxd.NewServer(cSvr) 97 c.Assert(err, jc.ErrorIsNil) 98 err = jujuSvr.CreateProfileWithConfig("custom", map[string]string{"boot.autostart": "false"}) 99 c.Assert(err, jc.ErrorIsNil) 100 } 101 102 func (s *serverSuite) TestGetServerName(c *gc.C) { 103 ctrl := gomock.NewController(c) 104 defer ctrl.Finish() 105 106 serverName := "nuc8" 107 mutate := func(s *api.Server) { 108 s.Environment.ServerClustered = false 109 s.Environment.ServerName = serverName 110 } 111 112 cSvr := s.NewMockServer(ctrl, mutate) 113 jujuSvr, err := lxd.NewServer(cSvr) 114 c.Assert(err, jc.ErrorIsNil) 115 c.Assert(jujuSvr.Name(), gc.Equals, serverName) 116 } 117 118 func (s *serverSuite) TestGetServerNameReturnsNoneIfServerNameIsEmpty(c *gc.C) { 119 ctrl := gomock.NewController(c) 120 defer ctrl.Finish() 121 122 mutate := func(s *api.Server) { 123 s.Environment.ServerClustered = false 124 s.Environment.ServerName = "" 125 } 126 127 cSvr := s.NewMockServer(ctrl, mutate) 128 jujuSvr, err := lxd.NewServer(cSvr) 129 c.Assert(err, jc.ErrorIsNil) 130 c.Assert(jujuSvr.Name(), gc.Equals, "none") 131 } 132 133 func (s *serverSuite) TestGetServerNameReturnsEmptyIfServerNameIsEmptyAndClustered(c *gc.C) { 134 ctrl := gomock.NewController(c) 135 defer ctrl.Finish() 136 137 mutate := func(s *api.Server) { 138 s.Environment.ServerClustered = true 139 s.Environment.ServerName = "" 140 } 141 142 cSvr := s.NewMockServer(ctrl, mutate) 143 jujuSvr, err := lxd.NewServer(cSvr) 144 c.Assert(err, jc.ErrorIsNil) 145 c.Assert(jujuSvr.Name(), gc.Equals, "") 146 } 147 148 func (s *serverSuite) TestReplaceOrAddContainerProfile(c *gc.C) { 149 ctrl := gomock.NewController(c) 150 defer ctrl.Finish() 151 cSvr := s.NewMockServer(ctrl) 152 153 updateOp := lxdtesting.NewMockOperation(ctrl) 154 updateOp.EXPECT().Wait().Return(nil) 155 updateOp.EXPECT().Get().Return(api.Operation{Description: "Updating ontainer"}) 156 157 instId := "testme" 158 old := "old-profile" 159 oldProfiles := []string{"default", "juju-default", old} 160 new := "new-profile" 161 cSvr.EXPECT().GetContainer(instId).Return( 162 &api.Container{ 163 ContainerPut: api.ContainerPut{ 164 Profiles: oldProfiles, 165 }, 166 }, "", nil) 167 cSvr.EXPECT().UpdateContainer(instId, gomock.Any(), gomock.Any()).Return(updateOp, nil) 168 169 jujuSvr, err := lxd.NewServer(cSvr) 170 c.Assert(err, jc.ErrorIsNil) 171 err = jujuSvr.ReplaceOrAddContainerProfile(instId, old, new) 172 c.Assert(err, jc.ErrorIsNil) 173 }