github.com/juju/juju@v0.0.0-20240430160146-1752b71fcf00/api/agent/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/v3"
    10  	"github.com/juju/version/v2"
    11  	gc "gopkg.in/check.v1"
    12  
    13  	"github.com/juju/juju/api"
    14  	"github.com/juju/juju/api/agent/upgrader"
    15  	"github.com/juju/juju/core/arch"
    16  	"github.com/juju/juju/core/instance"
    17  	"github.com/juju/juju/core/watcher/watchertest"
    18  	jujutesting "github.com/juju/juju/juju/testing"
    19  	"github.com/juju/juju/rpc/params"
    20  	"github.com/juju/juju/state"
    21  	"github.com/juju/juju/testing"
    22  	"github.com/juju/juju/tools"
    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  func (s *unitUpgraderSuite) SetUpTest(c *gc.C) {
    41  	s.JujuConnSuite.SetUpTest(c)
    42  
    43  	s.rawMachine, _, _, s.rawUnit = s.addMachineApplicationCharmAndUnit(c, "wordpress")
    44  	password, err := utils.RandomPassword()
    45  	c.Assert(err, jc.ErrorIsNil)
    46  	err = s.rawUnit.SetPassword(password)
    47  	c.Assert(err, jc.ErrorIsNil)
    48  	s.stateAPI = s.OpenAPIAs(c, s.rawUnit.Tag(), password)
    49  
    50  	// Create the upgrader facade.
    51  	s.st = upgrader.NewState(s.stateAPI)
    52  	c.Assert(s.st, gc.NotNil)
    53  
    54  	s.WaitForModelWatchersIdle(c, s.State.ModelUUID())
    55  }
    56  
    57  func (s *unitUpgraderSuite) addMachineApplicationCharmAndUnit(c *gc.C, appName string) (*state.Machine, *state.Application, *state.Charm, *state.Unit) {
    58  	machine, err := s.State.AddMachine(state.UbuntuBase("12.10"), state.JobHostUnits)
    59  	c.Assert(err, jc.ErrorIsNil)
    60  
    61  	arch := arch.DefaultArchitecture
    62  	hwChar := &instance.HardwareCharacteristics{
    63  		Arch: &arch,
    64  	}
    65  	instId := instance.Id("i-host-machine")
    66  	err = machine.SetProvisioned(instId, "", "fake-nonce", hwChar)
    67  	c.Assert(err, jc.ErrorIsNil)
    68  
    69  	charm := s.AddTestingCharm(c, appName)
    70  	app := s.AddTestingApplication(c, appName, charm)
    71  	unit, err := app.AddUnit(state.AddUnitParams{})
    72  	c.Assert(err, jc.ErrorIsNil)
    73  	err = unit.AssignToMachine(machine)
    74  	c.Assert(err, jc.ErrorIsNil)
    75  	return machine, app, charm, unit
    76  }
    77  
    78  func (s *unitUpgraderSuite) TestSetVersionWrongUnit(c *gc.C) {
    79  	err := s.st.SetVersion("unit-wordpress-42", testing.CurrentVersion())
    80  	c.Assert(err, gc.ErrorMatches, "permission denied")
    81  	c.Assert(err, jc.Satisfies, params.IsCodeUnauthorized)
    82  }
    83  
    84  func (s *unitUpgraderSuite) TestSetVersionNotUnit(c *gc.C) {
    85  	err := s.st.SetVersion("foo-42", testing.CurrentVersion())
    86  	c.Assert(err, gc.ErrorMatches, "permission denied")
    87  	c.Assert(err, jc.Satisfies, params.IsCodeUnauthorized)
    88  }
    89  
    90  func (s *unitUpgraderSuite) TestSetVersion(c *gc.C) {
    91  	current := testing.CurrentVersion()
    92  	agentTools, err := s.rawUnit.AgentTools()
    93  	c.Assert(err, jc.Satisfies, errors.IsNotFound)
    94  	c.Assert(agentTools, gc.IsNil)
    95  	err = s.st.SetVersion(s.rawUnit.Tag().String(), current)
    96  	c.Assert(err, jc.ErrorIsNil)
    97  	s.rawUnit.Refresh()
    98  	agentTools, err = s.rawUnit.AgentTools()
    99  	c.Assert(err, jc.ErrorIsNil)
   100  	c.Check(agentTools.Version, gc.Equals, current)
   101  }
   102  
   103  func (s *unitUpgraderSuite) TestToolsWrongUnit(c *gc.C) {
   104  	tools, err := s.st.Tools("unit-wordpress-42")
   105  	c.Assert(err, gc.ErrorMatches, "permission denied")
   106  	c.Assert(err, jc.Satisfies, params.IsCodeUnauthorized)
   107  	c.Assert(tools, gc.IsNil)
   108  }
   109  
   110  func (s *unitUpgraderSuite) TestToolsNotUnit(c *gc.C) {
   111  	tools, err := s.st.Tools("foo-42")
   112  	c.Assert(err, gc.ErrorMatches, "permission denied")
   113  	c.Assert(err, jc.Satisfies, params.IsCodeUnauthorized)
   114  	c.Assert(tools, gc.IsNil)
   115  }
   116  
   117  func (s *unitUpgraderSuite) TestTools(c *gc.C) {
   118  	current := testing.CurrentVersion()
   119  	curTools := &tools.Tools{Version: current, URL: ""}
   120  	curTools.Version.Minor++
   121  	s.rawMachine.SetAgentVersion(current)
   122  	// UnitUpgrader.Tools returns the *desired* set of tools, not the currently
   123  	// running set. We want to be upgraded to cur.Version
   124  	stateToolsList, err := s.st.Tools(s.rawUnit.Tag().String())
   125  	c.Assert(err, jc.ErrorIsNil)
   126  	c.Assert(stateToolsList, gc.HasLen, 1)
   127  	stateTools := stateToolsList[0]
   128  	c.Check(stateTools.Version.Number, gc.DeepEquals, current.Number)
   129  	c.Assert(stateTools.URL, gc.NotNil)
   130  }
   131  
   132  func (s *unitUpgraderSuite) TestWatchAPIVersion(c *gc.C) {
   133  	w, err := s.st.WatchAPIVersion(s.rawUnit.Tag().String())
   134  	c.Assert(err, jc.ErrorIsNil)
   135  	wc := watchertest.NewNotifyWatcherC(c, w)
   136  	defer wc.AssertStops()
   137  
   138  	// Initial event
   139  	wc.AssertOneChange()
   140  	vers := version.MustParseBinary("10.20.34-ubuntu-amd64")
   141  	err = s.rawMachine.SetAgentVersion(vers)
   142  	c.Assert(err, jc.ErrorIsNil)
   143  
   144  	// One change noticing the new version
   145  	wc.AssertOneChange()
   146  	vers = version.MustParseBinary("10.20.35-ubuntu-amd64")
   147  	err = s.rawMachine.SetAgentVersion(vers)
   148  	c.Assert(err, jc.ErrorIsNil)
   149  	wc.AssertOneChange()
   150  }
   151  
   152  func (s *unitUpgraderSuite) TestWatchAPIVersionWrongUnit(c *gc.C) {
   153  	_, err := s.st.WatchAPIVersion("unit-wordpress-42")
   154  	c.Assert(err, gc.ErrorMatches, "permission denied")
   155  	c.Assert(err, jc.Satisfies, params.IsCodeUnauthorized)
   156  }
   157  
   158  func (s *unitUpgraderSuite) TestWatchAPIVersionNotUnit(c *gc.C) {
   159  	_, err := s.st.WatchAPIVersion("foo-42")
   160  	c.Assert(err, gc.ErrorMatches, "permission denied")
   161  	c.Assert(err, jc.Satisfies, params.IsCodeUnauthorized)
   162  }
   163  
   164  func (s *unitUpgraderSuite) TestDesiredVersion(c *gc.C) {
   165  	current := testing.CurrentVersion()
   166  	curTools := &tools.Tools{Version: current, URL: ""}
   167  	curTools.Version.Minor++
   168  	s.rawMachine.SetAgentVersion(current)
   169  	// UnitUpgrader.DesiredVersion returns the *desired* set of tools, not the
   170  	// currently running set. We want to be upgraded to cur.Version
   171  	stateVersion, err := s.st.DesiredVersion(s.rawUnit.Tag().String())
   172  	c.Assert(err, jc.ErrorIsNil)
   173  	c.Assert(stateVersion, gc.Equals, current.Number)
   174  }
   175  
   176  func (s *unitUpgraderSuite) TestDesiredVersionWrongUnit(c *gc.C) {
   177  	_, err := s.st.DesiredVersion("unit-wordpress-42")
   178  	c.Assert(err, gc.ErrorMatches, "permission denied")
   179  	c.Assert(err, jc.Satisfies, params.IsCodeUnauthorized)
   180  }
   181  
   182  func (s *unitUpgraderSuite) TestDesiredVersionNotUnit(c *gc.C) {
   183  	_, err := s.st.DesiredVersion("foo-42")
   184  	c.Assert(err, gc.ErrorMatches, "permission denied")
   185  	c.Assert(err, jc.Satisfies, params.IsCodeUnauthorized)
   186  }