github.com/mhilton/juju-juju@v0.0.0-20150901100907-a94dd2c73455/upgrades/steps125_test.go (about)

     1  // Copyright 2015 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package upgrades_test
     5  
     6  import (
     7  	"errors"
     8  	"strings"
     9  
    10  	gc "gopkg.in/check.v1"
    11  
    12  	jc "github.com/juju/testing/checkers"
    13  	"github.com/juju/utils/exec"
    14  
    15  	"github.com/juju/juju/cloudconfig"
    16  	"github.com/juju/juju/testing"
    17  	"github.com/juju/juju/upgrades"
    18  	"github.com/juju/juju/version"
    19  )
    20  
    21  type steps125Suite struct {
    22  	testing.BaseSuite
    23  }
    24  
    25  var _ = gc.Suite(&steps125Suite{})
    26  
    27  func (s *steps125Suite) TestStateStepsFor125(c *gc.C) {
    28  	expected := []string{
    29  		"set hosted environment count to number of hosted environments",
    30  		"tag machine instances",
    31  		"add missing env-uuid to statuses",
    32  		"add attachmentCount to volume",
    33  		"add attachmentCount to filesystem",
    34  		"add binding to volume",
    35  		"add binding to filesystem",
    36  		"add status to volume",
    37  		"move lastlogin and last connection to their own collections",
    38  	}
    39  	assertStateSteps(c, version.MustParse("1.25.0"), expected)
    40  }
    41  
    42  func (s *steps125Suite) TestStepsFor125(c *gc.C) {
    43  	expected := []string{
    44  		"remove Jujud.pass file on windows",
    45  		"add juju registry key",
    46  	}
    47  	assertSteps(c, version.MustParse("1.25.0"), expected)
    48  }
    49  
    50  type mockOSRemove struct {
    51  	called     bool
    52  	path       string
    53  	shouldFail bool
    54  }
    55  
    56  func (m *mockOSRemove) osRemove(path string) error {
    57  	m.called = true
    58  	m.path = path
    59  	if m.shouldFail {
    60  		return errors.New("i done error'd")
    61  	}
    62  	return nil
    63  }
    64  
    65  var removeFileTests = []struct {
    66  	os           version.OSType
    67  	callExpected bool
    68  	shouldFail   bool
    69  }{
    70  	{
    71  		os:           version.Ubuntu,
    72  		callExpected: false,
    73  		shouldFail:   false,
    74  	},
    75  	{
    76  		os:           version.Windows,
    77  		callExpected: true,
    78  		shouldFail:   false,
    79  	},
    80  	{
    81  		os:           version.Windows,
    82  		callExpected: true,
    83  		shouldFail:   true,
    84  	},
    85  }
    86  
    87  func (s *steps125Suite) TestRemoveJujudPass(c *gc.C) {
    88  	for _, t := range removeFileTests {
    89  		mock := &mockOSRemove{shouldFail: t.shouldFail}
    90  		s.PatchValue(upgrades.OsRemove, mock.osRemove)
    91  		s.PatchValue(&version.Current.OS, t.os)
    92  		err := upgrades.RemoveJujudpass(nil)
    93  		c.Assert(err, jc.ErrorIsNil)
    94  		c.Assert(mock.called, gc.Equals, t.callExpected)
    95  	}
    96  }
    97  
    98  type mockRunCmds struct {
    99  	c          *gc.C
   100  	commands   string
   101  	called     bool
   102  	shouldFail bool
   103  }
   104  
   105  func (m *mockRunCmds) runCommands(params exec.RunParams) (*exec.ExecResponse, error) {
   106  	m.called = true
   107  	m.c.Assert(params.Commands, gc.Equals, strings.Join(cloudconfig.CreateJujuRegistryKeyCmds(), "\n"))
   108  	if m.shouldFail {
   109  		return nil, errors.New("derp")
   110  	}
   111  	return nil, nil
   112  }
   113  
   114  var addRegKeyTests = []struct {
   115  	os           version.OSType
   116  	callExpected bool
   117  	shouldFail   bool
   118  	errMessage   string
   119  }{
   120  	{
   121  		os:           version.Ubuntu,
   122  		callExpected: false,
   123  		shouldFail:   false,
   124  	},
   125  	{
   126  		os:           version.Windows,
   127  		callExpected: true,
   128  		shouldFail:   false,
   129  	},
   130  	{
   131  		os:           version.Windows,
   132  		callExpected: true,
   133  		shouldFail:   true,
   134  		errMessage:   "could not create juju registry key: derp",
   135  	},
   136  }
   137  
   138  func (s *steps125Suite) TestAddJujuRegKey(c *gc.C) {
   139  	for _, t := range addRegKeyTests {
   140  		mock := &mockRunCmds{shouldFail: t.shouldFail, c: c}
   141  		s.PatchValue(upgrades.ExecRunCommands, mock.runCommands)
   142  		s.PatchValue(&version.Current.OS, t.os)
   143  		err := upgrades.AddJujuRegKey(nil)
   144  		if t.shouldFail {
   145  			c.Assert(err, gc.ErrorMatches, t.errMessage)
   146  		} else {
   147  			c.Assert(err, jc.ErrorIsNil)
   148  		}
   149  		c.Assert(mock.called, gc.Equals, t.callExpected)
   150  	}
   151  }