github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/worker/storageprovisioner/config_test.go (about)

     1  // Copyright 2015 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package storageprovisioner_test
     5  
     6  import (
     7  	"github.com/juju/errors"
     8  	"github.com/juju/names"
     9  	"github.com/juju/testing"
    10  	jc "github.com/juju/testing/checkers"
    11  	"github.com/juju/utils/clock"
    12  	gc "gopkg.in/check.v1"
    13  
    14  	coretesting "github.com/juju/juju/testing"
    15  	"github.com/juju/juju/worker/storageprovisioner"
    16  )
    17  
    18  type ConfigSuite struct {
    19  	testing.IsolationSuite
    20  
    21  	// This is a bit unexpected: these tests should mutate the stored
    22  	// config, and then call the checkNotValid method.
    23  	config storageprovisioner.Config
    24  }
    25  
    26  var _ = gc.Suite(&ConfigSuite{})
    27  
    28  func (s *ConfigSuite) SetUpTest(c *gc.C) {
    29  	s.IsolationSuite.SetUpTest(c)
    30  	s.config = validEnvironConfig()
    31  }
    32  
    33  func (s *ConfigSuite) TestNilScope(c *gc.C) {
    34  	s.config.Scope = nil
    35  	s.checkNotValid(c, "nil Scope not valid")
    36  }
    37  
    38  func (s *ConfigSuite) TestInvalidScope(c *gc.C) {
    39  	s.config.Scope = names.NewServiceTag("boo")
    40  	s.checkNotValid(c, ".* Scope not valid")
    41  }
    42  
    43  func (s *ConfigSuite) TestEnvironScopeStorageDir(c *gc.C) {
    44  	s.config.StorageDir = "surprise!"
    45  	s.checkNotValid(c, "environ Scope with non-empty StorageDir not valid")
    46  }
    47  
    48  func (s *ConfigSuite) TestMachineScopeStorageDir(c *gc.C) {
    49  	s.config = validMachineConfig()
    50  	s.config.StorageDir = ""
    51  	s.checkNotValid(c, "machine Scope with empty StorageDir not valid")
    52  }
    53  
    54  func (s *ConfigSuite) TestNilVolumes(c *gc.C) {
    55  	s.config.Volumes = nil
    56  	s.checkNotValid(c, "nil Volumes not valid")
    57  }
    58  
    59  func (s *ConfigSuite) TestNilFilesystems(c *gc.C) {
    60  	s.config.Filesystems = nil
    61  	s.checkNotValid(c, "nil Filesystems not valid")
    62  }
    63  
    64  func (s *ConfigSuite) TestNilLife(c *gc.C) {
    65  	s.config.Life = nil
    66  	s.checkNotValid(c, "nil Life not valid")
    67  }
    68  
    69  func (s *ConfigSuite) TestNilEnviron(c *gc.C) {
    70  	s.config.Environ = nil
    71  	s.checkNotValid(c, "nil Environ not valid")
    72  }
    73  
    74  func (s *ConfigSuite) TestNilMachines(c *gc.C) {
    75  	s.config.Machines = nil
    76  	s.checkNotValid(c, "nil Machines not valid")
    77  }
    78  
    79  func (s *ConfigSuite) TestNilStatus(c *gc.C) {
    80  	s.config.Status = nil
    81  	s.checkNotValid(c, "nil Status not valid")
    82  }
    83  
    84  func (s *ConfigSuite) TestNilClock(c *gc.C) {
    85  	s.config.Clock = nil
    86  	s.checkNotValid(c, "nil Clock not valid")
    87  }
    88  
    89  func (s *ConfigSuite) checkNotValid(c *gc.C, match string) {
    90  	err := s.config.Validate()
    91  	c.Check(err, jc.Satisfies, errors.IsNotValid)
    92  	c.Check(err, gc.ErrorMatches, match)
    93  }
    94  
    95  func validEnvironConfig() storageprovisioner.Config {
    96  	config := almostValidConfig()
    97  	config.Scope = coretesting.ModelTag
    98  	return config
    99  }
   100  
   101  func validMachineConfig() storageprovisioner.Config {
   102  	config := almostValidConfig()
   103  	config.Scope = names.NewMachineTag("123/lxc/7")
   104  	config.StorageDir = "storage-dir"
   105  	return config
   106  }
   107  
   108  func almostValidConfig() storageprovisioner.Config {
   109  	// gofmt doesn't seem to want to let me one-line any of these
   110  	// except the last one, so I'm standardising on multi-line.
   111  	return storageprovisioner.Config{
   112  		Volumes: struct {
   113  			storageprovisioner.VolumeAccessor
   114  		}{},
   115  		Filesystems: struct {
   116  			storageprovisioner.FilesystemAccessor
   117  		}{},
   118  		Life: struct {
   119  			storageprovisioner.LifecycleManager
   120  		}{},
   121  		Environ: struct {
   122  			storageprovisioner.ModelAccessor
   123  		}{},
   124  		Machines: struct {
   125  			storageprovisioner.MachineAccessor
   126  		}{},
   127  		Status: struct {
   128  			storageprovisioner.StatusSetter
   129  		}{},
   130  		Clock: struct {
   131  			clock.Clock
   132  		}{},
   133  	}
   134  }