github.com/cloud-green/juju@v0.0.0-20151002100041-a00291338d3d/upgrades/steps124_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  	"io/ioutil"
     8  	"os"
     9  	"path/filepath"
    10  
    11  	jc "github.com/juju/testing/checkers"
    12  	gc "gopkg.in/check.v1"
    13  
    14  	"github.com/juju/juju/agent"
    15  	"github.com/juju/juju/testing"
    16  	"github.com/juju/juju/upgrades"
    17  	"github.com/juju/juju/version"
    18  )
    19  
    20  type steps124Suite struct {
    21  	testing.BaseSuite
    22  }
    23  
    24  var _ = gc.Suite(&steps124Suite{})
    25  
    26  func (s *steps124Suite) TestStateStepsFor124(c *gc.C) {
    27  	expected := []string{
    28  		"add the version field to all settings docs",
    29  		"add block device documents for existing machines",
    30  		"move service.UnitSeq to sequence collection",
    31  		"add instance id field to IP addresses",
    32  		"add UUID field to IP addresses",
    33  		"migrate charm archives into environment storage",
    34  		"change entityid field on status history to globalkey",
    35  		"change updated field on statushistory from time to int",
    36  		"change updated field on status from time to int",
    37  		"add preferred addresses to machines",
    38  	}
    39  	assertStateSteps(c, version.MustParse("1.24.0"), expected)
    40  }
    41  
    42  func (s *steps124Suite) TestStateStepsFor1244(c *gc.C) {
    43  	expected := []string{
    44  		"add missing service statuses",
    45  	}
    46  	assertStateSteps(c, version.MustParse("1.24.4"), expected)
    47  }
    48  
    49  func (s *steps124Suite) TestStepsFor124(c *gc.C) {
    50  	expected := []string{
    51  		"move syslog config from LogDir to DataDir",
    52  	}
    53  	assertSteps(c, version.MustParse("1.24.0"), expected)
    54  }
    55  
    56  func (s *steps124Suite) TestCopyFileNew(c *gc.C) {
    57  	src := c.MkDir()
    58  	dest := c.MkDir()
    59  	srcdata := []byte("new data!")
    60  
    61  	// test that a file in src dir and not in dest dir gets copied.
    62  
    63  	newSrc := filepath.Join(src, "new")
    64  	err := ioutil.WriteFile(newSrc, srcdata, 0644)
    65  	c.Assert(err, jc.ErrorIsNil)
    66  
    67  	newDest := filepath.Join(dest, "new")
    68  
    69  	err = upgrades.CopyFile(newDest, newSrc)
    70  	c.Assert(err, jc.ErrorIsNil)
    71  
    72  	srcb, err := ioutil.ReadFile(newSrc)
    73  	c.Assert(err, jc.ErrorIsNil)
    74  	destb, err := ioutil.ReadFile(newDest)
    75  	c.Assert(err, jc.ErrorIsNil)
    76  	// convert to string and use Equals because we'll get a better failure message
    77  	c.Assert(string(destb), gc.Equals, string(srcb))
    78  }
    79  
    80  func (s *steps124Suite) TestCopyFileExisting(c *gc.C) {
    81  	src := c.MkDir()
    82  	dest := c.MkDir()
    83  	srcdata := []byte("new data!")
    84  	destdata := []byte("old data!")
    85  
    86  	exSrc := filepath.Join(src, "existing")
    87  	exDest := filepath.Join(dest, "existing")
    88  
    89  	err := ioutil.WriteFile(exSrc, srcdata, 0644)
    90  	c.Assert(err, jc.ErrorIsNil)
    91  	err = ioutil.WriteFile(exDest, destdata, 0644)
    92  	c.Assert(err, jc.ErrorIsNil)
    93  
    94  	err = upgrades.CopyFile(exDest, exSrc)
    95  	c.Assert(err, jc.ErrorIsNil)
    96  
    97  	// assert we haven't changed the destination
    98  	b, err := ioutil.ReadFile(exDest)
    99  
   100  	c.Assert(err, jc.ErrorIsNil)
   101  	// convert to string because we'll get a better failure message
   102  	c.Assert(string(b), gc.Equals, string(destdata))
   103  }
   104  
   105  func (s *steps124Suite) TestMoveSyslogConfigDefault(c *gc.C) {
   106  	logdir := c.MkDir()
   107  	datadir := c.MkDir()
   108  	data := []byte("data!")
   109  	files := []string{
   110  		"ca-cert.pem",
   111  		"rsyslog-cert.pem",
   112  		"rsyslog-key.pem",
   113  		"logrotate.conf",
   114  		"logrotate.run",
   115  	}
   116  	for _, f := range files {
   117  		err := ioutil.WriteFile(filepath.Join(logdir, f), data, 0644)
   118  		c.Assert(err, jc.ErrorIsNil)
   119  	}
   120  
   121  	ctx := fakeContext{cfg: fakeConfig{logdir: logdir, datadir: datadir}}
   122  	err := upgrades.MoveSyslogConfig(ctx)
   123  	c.Assert(err, jc.ErrorIsNil)
   124  
   125  	for _, f := range files {
   126  		_, err := os.Stat(filepath.Join(datadir, f))
   127  		c.Assert(err, jc.ErrorIsNil)
   128  		_, err = os.Stat(filepath.Join(logdir, f))
   129  		c.Assert(err, jc.Satisfies, os.IsNotExist)
   130  	}
   131  }
   132  
   133  func (s *steps124Suite) TestMoveSyslogConfig(c *gc.C) {
   134  	logdir := c.MkDir()
   135  	datadir := c.MkDir()
   136  	data := []byte("data!")
   137  	files := []string{
   138  		"logrotate.conf",
   139  		"logrotate.run",
   140  	}
   141  
   142  	// ensure that we don't overwrite an existing file in datadir, and don't
   143  	// error out if one of the files exists in datadir but not logdir.
   144  
   145  	err := ioutil.WriteFile(filepath.Join(logdir, "logrotate.conf"), data, 0644)
   146  	c.Assert(err, jc.ErrorIsNil)
   147  
   148  	err = ioutil.WriteFile(filepath.Join(datadir, "logrotate.run"), data, 0644)
   149  	c.Assert(err, jc.ErrorIsNil)
   150  
   151  	differentData := []byte("different")
   152  	existing := filepath.Join(datadir, "logrotate.conf")
   153  	err = ioutil.WriteFile(existing, differentData, 0644)
   154  	c.Assert(err, jc.ErrorIsNil)
   155  
   156  	ctx := fakeContext{cfg: fakeConfig{logdir: logdir, datadir: datadir}}
   157  	err = upgrades.MoveSyslogConfig(ctx)
   158  	c.Assert(err, jc.ErrorIsNil)
   159  
   160  	for _, f := range files {
   161  		_, err := os.Stat(filepath.Join(datadir, f))
   162  		c.Assert(err, jc.ErrorIsNil)
   163  		_, err = os.Stat(filepath.Join(logdir, f))
   164  		c.Assert(err, jc.Satisfies, os.IsNotExist)
   165  	}
   166  
   167  	b, err := ioutil.ReadFile(existing)
   168  	c.Assert(err, jc.ErrorIsNil)
   169  	// convert to string because we'll get a better failure message
   170  	c.Assert(string(b), gc.Not(gc.Equals), string(existing))
   171  
   172  }
   173  
   174  func (s *steps124Suite) TestMoveSyslogConfigCantDeleteOld(c *gc.C) {
   175  	logdir := c.MkDir()
   176  	datadir := c.MkDir()
   177  	file := filepath.Join(logdir, "logrotate.conf")
   178  
   179  	// ensure that we don't error out if we can't remove the old file.
   180  	// error out if one of the files exists in datadir but not logdir.
   181  	s.PatchValue(upgrades.OsRemove, func(string) error { return os.ErrPermission })
   182  
   183  	err := ioutil.WriteFile(file, []byte("data!"), 0644)
   184  	c.Assert(err, jc.ErrorIsNil)
   185  
   186  	ctx := fakeContext{cfg: fakeConfig{logdir: logdir, datadir: datadir}}
   187  	err = upgrades.MoveSyslogConfig(ctx)
   188  	c.Assert(err, jc.ErrorIsNil)
   189  
   190  	// should still exist in both places (i.e. check we didn't screw up the test)
   191  	_, err = os.Stat(file)
   192  	c.Assert(err, jc.ErrorIsNil)
   193  	_, err = os.Stat(filepath.Join(datadir, "logrotate.conf"))
   194  	c.Assert(err, jc.ErrorIsNil)
   195  }
   196  
   197  type fakeContext struct {
   198  	upgrades.Context
   199  	cfg fakeConfig
   200  }
   201  
   202  func (f fakeContext) AgentConfig() agent.ConfigSetter {
   203  	return f.cfg
   204  }
   205  
   206  type fakeConfig struct {
   207  	agent.ConfigSetter
   208  	logdir  string
   209  	datadir string
   210  }
   211  
   212  func (f fakeConfig) LogDir() string {
   213  	return f.logdir
   214  }
   215  
   216  func (f fakeConfig) DataDir() string {
   217  	return f.datadir
   218  }