github.com/axw/juju@v0.0.0-20161005053422-4bd6544d08d4/api/upgrader/unitupgrader_test.go (about) 1 // Copyright 2012, 2013 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package upgrader_test 5 6 import ( 7 "github.com/juju/errors" 8 jc "github.com/juju/testing/checkers" 9 "github.com/juju/utils" 10 "github.com/juju/utils/arch" 11 "github.com/juju/utils/series" 12 "github.com/juju/version" 13 gc "gopkg.in/check.v1" 14 15 "github.com/juju/juju/api" 16 "github.com/juju/juju/api/upgrader" 17 "github.com/juju/juju/apiserver/params" 18 jujutesting "github.com/juju/juju/juju/testing" 19 "github.com/juju/juju/state" 20 "github.com/juju/juju/tools" 21 jujuversion "github.com/juju/juju/version" 22 "github.com/juju/juju/watcher/watchertest" 23 ) 24 25 type unitUpgraderSuite struct { 26 jujutesting.JujuConnSuite 27 28 stateAPI api.Connection 29 30 // These are raw State objects. Use them for setup and assertions, but 31 // should never be touched by the API calls themselves 32 rawMachine *state.Machine 33 rawUnit *state.Unit 34 35 st *upgrader.State 36 } 37 38 var _ = gc.Suite(&unitUpgraderSuite{}) 39 40 var current = version.Binary{ 41 Number: jujuversion.Current, 42 Arch: arch.HostArch(), 43 Series: series.HostSeries(), 44 } 45 46 func (s *unitUpgraderSuite) SetUpTest(c *gc.C) { 47 s.JujuConnSuite.SetUpTest(c) 48 49 s.rawMachine, _, _, s.rawUnit = s.addMachineServiceCharmAndUnit(c, "wordpress") 50 password, err := utils.RandomPassword() 51 c.Assert(err, jc.ErrorIsNil) 52 err = s.rawUnit.SetPassword(password) 53 c.Assert(err, jc.ErrorIsNil) 54 s.stateAPI = s.OpenAPIAs(c, s.rawUnit.Tag(), password) 55 56 // Create the upgrader facade. 57 s.st = s.stateAPI.Upgrader() 58 c.Assert(s.st, gc.NotNil) 59 } 60 61 func (s *unitUpgraderSuite) addMachineServiceCharmAndUnit(c *gc.C, serviceName string) (*state.Machine, *state.Application, *state.Charm, *state.Unit) { 62 machine, err := s.State.AddMachine("quantal", state.JobHostUnits) 63 c.Assert(err, jc.ErrorIsNil) 64 charm := s.AddTestingCharm(c, serviceName) 65 service := s.AddTestingService(c, serviceName, charm) 66 unit, err := service.AddUnit() 67 c.Assert(err, jc.ErrorIsNil) 68 err = unit.AssignToMachine(machine) 69 c.Assert(err, jc.ErrorIsNil) 70 return machine, service, charm, unit 71 } 72 73 func (s *unitUpgraderSuite) TestSetVersionWrongUnit(c *gc.C) { 74 err := s.st.SetVersion("unit-wordpress-42", current) 75 c.Assert(err, gc.ErrorMatches, "permission denied") 76 c.Assert(err, jc.Satisfies, params.IsCodeUnauthorized) 77 } 78 79 func (s *unitUpgraderSuite) TestSetVersionNotUnit(c *gc.C) { 80 err := s.st.SetVersion("foo-42", current) 81 c.Assert(err, gc.ErrorMatches, "permission denied") 82 c.Assert(err, jc.Satisfies, params.IsCodeUnauthorized) 83 } 84 85 func (s *unitUpgraderSuite) TestSetVersion(c *gc.C) { 86 agentTools, err := s.rawUnit.AgentTools() 87 c.Assert(err, jc.Satisfies, errors.IsNotFound) 88 c.Assert(agentTools, gc.IsNil) 89 err = s.st.SetVersion(s.rawUnit.Tag().String(), current) 90 c.Assert(err, jc.ErrorIsNil) 91 s.rawUnit.Refresh() 92 agentTools, err = s.rawUnit.AgentTools() 93 c.Assert(err, jc.ErrorIsNil) 94 c.Check(agentTools.Version, gc.Equals, current) 95 } 96 97 func (s *unitUpgraderSuite) TestToolsWrongUnit(c *gc.C) { 98 tools, err := s.st.Tools("unit-wordpress-42") 99 c.Assert(err, gc.ErrorMatches, "permission denied") 100 c.Assert(err, jc.Satisfies, params.IsCodeUnauthorized) 101 c.Assert(tools, gc.IsNil) 102 } 103 104 func (s *unitUpgraderSuite) TestToolsNotUnit(c *gc.C) { 105 tools, err := s.st.Tools("foo-42") 106 c.Assert(err, gc.ErrorMatches, "permission denied") 107 c.Assert(err, jc.Satisfies, params.IsCodeUnauthorized) 108 c.Assert(tools, gc.IsNil) 109 } 110 111 func (s *unitUpgraderSuite) TestTools(c *gc.C) { 112 curTools := &tools.Tools{Version: current, URL: ""} 113 curTools.Version.Minor++ 114 s.rawMachine.SetAgentVersion(current) 115 // UnitUpgrader.Tools returns the *desired* set of tools, not the currently 116 // running set. We want to be upgraded to cur.Version 117 stateToolsList, err := s.st.Tools(s.rawUnit.Tag().String()) 118 c.Assert(err, jc.ErrorIsNil) 119 c.Assert(stateToolsList, gc.HasLen, 1) 120 stateTools := stateToolsList[0] 121 c.Check(stateTools.Version.Number, gc.DeepEquals, current.Number) 122 c.Assert(stateTools.URL, gc.NotNil) 123 } 124 125 func (s *unitUpgraderSuite) TestWatchAPIVersion(c *gc.C) { 126 w, err := s.st.WatchAPIVersion(s.rawUnit.Tag().String()) 127 c.Assert(err, jc.ErrorIsNil) 128 wc := watchertest.NewNotifyWatcherC(c, w, s.BackingState.StartSync) 129 defer wc.AssertStops() 130 131 // Initial event 132 wc.AssertOneChange() 133 vers := version.MustParseBinary("10.20.34-quantal-amd64") 134 err = s.rawMachine.SetAgentVersion(vers) 135 c.Assert(err, jc.ErrorIsNil) 136 137 // One change noticing the new version 138 wc.AssertOneChange() 139 vers = version.MustParseBinary("10.20.35-quantal-amd64") 140 err = s.rawMachine.SetAgentVersion(vers) 141 c.Assert(err, jc.ErrorIsNil) 142 wc.AssertOneChange() 143 } 144 145 func (s *unitUpgraderSuite) TestWatchAPIVersionWrongUnit(c *gc.C) { 146 _, err := s.st.WatchAPIVersion("unit-wordpress-42") 147 c.Assert(err, gc.ErrorMatches, "permission denied") 148 c.Assert(err, jc.Satisfies, params.IsCodeUnauthorized) 149 } 150 151 func (s *unitUpgraderSuite) TestWatchAPIVersionNotUnit(c *gc.C) { 152 _, err := s.st.WatchAPIVersion("foo-42") 153 c.Assert(err, gc.ErrorMatches, "permission denied") 154 c.Assert(err, jc.Satisfies, params.IsCodeUnauthorized) 155 } 156 157 func (s *unitUpgraderSuite) TestDesiredVersion(c *gc.C) { 158 curTools := &tools.Tools{Version: current, URL: ""} 159 curTools.Version.Minor++ 160 s.rawMachine.SetAgentVersion(current) 161 // UnitUpgrader.DesiredVersion returns the *desired* set of tools, not the 162 // currently running set. We want to be upgraded to cur.Version 163 stateVersion, err := s.st.DesiredVersion(s.rawUnit.Tag().String()) 164 c.Assert(err, jc.ErrorIsNil) 165 c.Assert(stateVersion, gc.Equals, current.Number) 166 } 167 168 func (s *unitUpgraderSuite) TestDesiredVersionWrongUnit(c *gc.C) { 169 _, err := s.st.DesiredVersion("unit-wordpress-42") 170 c.Assert(err, gc.ErrorMatches, "permission denied") 171 c.Assert(err, jc.Satisfies, params.IsCodeUnauthorized) 172 } 173 174 func (s *unitUpgraderSuite) TestDesiredVersionNotUnit(c *gc.C) { 175 _, err := s.st.DesiredVersion("foo-42") 176 c.Assert(err, gc.ErrorMatches, "permission denied") 177 c.Assert(err, jc.Satisfies, params.IsCodeUnauthorized) 178 }