launchpad.net/~rogpeppe/juju-core/500-errgo-fix@v0.0.0-20140213181702-000000002356/environs/bootstrap/state_test.go (about)

     1  // Copyright 2013 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package bootstrap_test
     5  
     6  import (
     7  	"bytes"
     8  	"io/ioutil"
     9  	"os"
    10  	"path/filepath"
    11  
    12  	gc "launchpad.net/gocheck"
    13  	"launchpad.net/goyaml"
    14  
    15  	"launchpad.net/juju-core/environs"
    16  	"launchpad.net/juju-core/environs/bootstrap"
    17  	"launchpad.net/juju-core/environs/storage"
    18  	envtesting "launchpad.net/juju-core/environs/testing"
    19  	"launchpad.net/juju-core/instance"
    20  	jc "launchpad.net/juju-core/testing/checkers"
    21  	"launchpad.net/juju-core/testing/testbase"
    22  )
    23  
    24  type StateSuite struct {
    25  	testbase.LoggingSuite
    26  }
    27  
    28  var _ = gc.Suite(&StateSuite{})
    29  
    30  func (suite *StateSuite) newStorage(c *gc.C) storage.Storage {
    31  	closer, stor, _ := envtesting.CreateLocalTestStorage(c)
    32  	suite.AddCleanup(func(*gc.C) { closer.Close() })
    33  	envtesting.UploadFakeTools(c, stor)
    34  	return stor
    35  }
    36  
    37  func (suite *StateSuite) TestCreateStateFileWritesEmptyStateFile(c *gc.C) {
    38  	stor := suite.newStorage(c)
    39  
    40  	url, err := bootstrap.CreateStateFile(stor)
    41  	c.Assert(err, gc.IsNil)
    42  
    43  	reader, err := storage.Get(stor, bootstrap.StateFile)
    44  	c.Assert(err, gc.IsNil)
    45  	data, err := ioutil.ReadAll(reader)
    46  	c.Assert(err, gc.IsNil)
    47  	c.Check(string(data), gc.Equals, "")
    48  	c.Assert(url, gc.NotNil)
    49  	expectedURL, err := stor.URL(bootstrap.StateFile)
    50  	c.Assert(err, gc.IsNil)
    51  	c.Check(url, gc.Equals, expectedURL)
    52  }
    53  
    54  func (suite *StateSuite) TestDeleteStateFile(c *gc.C) {
    55  	closer, stor, dataDir := envtesting.CreateLocalTestStorage(c)
    56  	defer closer.Close()
    57  
    58  	err := bootstrap.DeleteStateFile(stor)
    59  	c.Assert(err, gc.IsNil) // doesn't exist, juju don't care
    60  
    61  	_, err = bootstrap.CreateStateFile(stor)
    62  	c.Assert(err, gc.IsNil)
    63  	_, err = os.Stat(filepath.Join(dataDir, bootstrap.StateFile))
    64  	c.Assert(err, gc.IsNil)
    65  
    66  	err = bootstrap.DeleteStateFile(stor)
    67  	c.Assert(err, gc.IsNil)
    68  	_, err = os.Stat(filepath.Join(dataDir, bootstrap.StateFile))
    69  	c.Assert(err, jc.Satisfies, os.IsNotExist)
    70  }
    71  
    72  func (suite *StateSuite) TestSaveStateWritesStateFile(c *gc.C) {
    73  	stor := suite.newStorage(c)
    74  	arch := "amd64"
    75  	state := bootstrap.BootstrapState{
    76  		StateInstances:  []instance.Id{instance.Id("an-instance-id")},
    77  		Characteristics: []instance.HardwareCharacteristics{{Arch: &arch}}}
    78  	marshaledState, err := goyaml.Marshal(state)
    79  	c.Assert(err, gc.IsNil)
    80  
    81  	err = bootstrap.SaveState(stor, &state)
    82  	c.Assert(err, gc.IsNil)
    83  
    84  	loadedState, err := storage.Get(stor, bootstrap.StateFile)
    85  	c.Assert(err, gc.IsNil)
    86  	content, err := ioutil.ReadAll(loadedState)
    87  	c.Assert(err, gc.IsNil)
    88  	c.Check(content, gc.DeepEquals, marshaledState)
    89  }
    90  
    91  func (suite *StateSuite) setUpSavedState(c *gc.C, stor storage.Storage) bootstrap.BootstrapState {
    92  	arch := "amd64"
    93  	state := bootstrap.BootstrapState{
    94  		StateInstances:  []instance.Id{instance.Id("an-instance-id")},
    95  		Characteristics: []instance.HardwareCharacteristics{{Arch: &arch}}}
    96  	content, err := goyaml.Marshal(state)
    97  	c.Assert(err, gc.IsNil)
    98  	err = stor.Put(bootstrap.StateFile, ioutil.NopCloser(bytes.NewReader(content)), int64(len(content)))
    99  	c.Assert(err, gc.IsNil)
   100  	return state
   101  }
   102  
   103  func (suite *StateSuite) TestLoadStateReadsStateFile(c *gc.C) {
   104  	storage := suite.newStorage(c)
   105  	state := suite.setUpSavedState(c, storage)
   106  	storedState, err := bootstrap.LoadState(storage)
   107  	c.Assert(err, gc.IsNil)
   108  	c.Check(*storedState, gc.DeepEquals, state)
   109  }
   110  
   111  func (suite *StateSuite) TestLoadStateFromURLReadsStateFile(c *gc.C) {
   112  	stor := suite.newStorage(c)
   113  	state := suite.setUpSavedState(c, stor)
   114  	url, err := stor.URL(bootstrap.StateFile)
   115  	c.Assert(err, gc.IsNil)
   116  	storedState, err := bootstrap.LoadStateFromURL(url)
   117  	c.Assert(err, gc.IsNil)
   118  	c.Check(*storedState, gc.DeepEquals, state)
   119  }
   120  
   121  func (suite *StateSuite) TestLoadStateMissingFile(c *gc.C) {
   122  	stor := suite.newStorage(c)
   123  	_, err := bootstrap.LoadState(stor)
   124  	c.Check(err, gc.Equals, environs.ErrNotBootstrapped)
   125  }
   126  
   127  func (suite *StateSuite) TestLoadStateIntegratesWithSaveState(c *gc.C) {
   128  	storage := suite.newStorage(c)
   129  	arch := "amd64"
   130  	state := bootstrap.BootstrapState{
   131  		StateInstances:  []instance.Id{instance.Id("an-instance-id")},
   132  		Characteristics: []instance.HardwareCharacteristics{{Arch: &arch}}}
   133  	err := bootstrap.SaveState(storage, &state)
   134  	c.Assert(err, gc.IsNil)
   135  	storedState, err := bootstrap.LoadState(storage)
   136  	c.Assert(err, gc.IsNil)
   137  
   138  	c.Check(*storedState, gc.DeepEquals, state)
   139  }