github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/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/clock"
     8  	"github.com/juju/errors"
     9  	"github.com/juju/testing"
    10  	jc "github.com/juju/testing/checkers"
    11  	gc "gopkg.in/check.v1"
    12  	"gopkg.in/juju/names.v2"
    13  
    14  	"github.com/juju/juju/environs/context"
    15  	"github.com/juju/juju/storage"
    16  	coretesting "github.com/juju/juju/testing"
    17  	"github.com/juju/juju/worker/storageprovisioner"
    18  )
    19  
    20  type ConfigSuite struct {
    21  	testing.IsolationSuite
    22  
    23  	// This is a bit unexpected: these tests should mutate the stored
    24  	// config, and then call the checkNotValid method.
    25  	config storageprovisioner.Config
    26  }
    27  
    28  var _ = gc.Suite(&ConfigSuite{})
    29  
    30  func (s *ConfigSuite) SetUpTest(c *gc.C) {
    31  	s.IsolationSuite.SetUpTest(c)
    32  	s.config = validEnvironConfig()
    33  }
    34  
    35  func (s *ConfigSuite) TestNilScope(c *gc.C) {
    36  	s.config.Scope = nil
    37  	s.checkNotValid(c, "nil Scope not valid")
    38  }
    39  
    40  func (s *ConfigSuite) TestInvalidScope(c *gc.C) {
    41  	s.config.Scope = names.NewUnitTag("boo/0")
    42  	s.checkNotValid(c, ".* Scope not valid")
    43  }
    44  
    45  func (s *ConfigSuite) TestEnvironScopeStorageDir(c *gc.C) {
    46  	s.config.StorageDir = "surprise!"
    47  	s.checkNotValid(c, "environ Scope with non-empty StorageDir not valid")
    48  }
    49  
    50  func (s *ConfigSuite) TestMachineScopeStorageDir(c *gc.C) {
    51  	s.config = validMachineConfig()
    52  	s.config.StorageDir = ""
    53  	s.checkNotValid(c, "machine Scope with empty StorageDir not valid")
    54  }
    55  
    56  func (s *ConfigSuite) TestApplicationScopeStorageDir(c *gc.C) {
    57  	s.config = validApplicationConfig()
    58  	s.config.StorageDir = "surprise!"
    59  	s.checkNotValid(c, "application Scope with StorageDir not valid")
    60  }
    61  
    62  func (s *ConfigSuite) TestNilApplications(c *gc.C) {
    63  	s.config.Scope = names.NewApplicationTag("mariadb")
    64  	s.config.Applications = nil
    65  	s.checkNotValid(c, "nil Applications not valid")
    66  }
    67  
    68  func (s *ConfigSuite) TestNilVolumes(c *gc.C) {
    69  	s.config.Volumes = nil
    70  	s.checkNotValid(c, "nil Volumes not valid")
    71  }
    72  
    73  func (s *ConfigSuite) TestNilFilesystems(c *gc.C) {
    74  	s.config.Filesystems = nil
    75  	s.checkNotValid(c, "nil Filesystems not valid")
    76  }
    77  
    78  func (s *ConfigSuite) TestNilLife(c *gc.C) {
    79  	s.config.Life = nil
    80  	s.checkNotValid(c, "nil Life not valid")
    81  }
    82  
    83  func (s *ConfigSuite) TestNilRegistry(c *gc.C) {
    84  	s.config.Registry = nil
    85  	s.checkNotValid(c, "nil Registry not valid")
    86  }
    87  
    88  func (s *ConfigSuite) TestNilMachines(c *gc.C) {
    89  	s.config.Scope = names.NewMachineTag("123")
    90  	s.config.Machines = nil
    91  	s.config.StorageDir = "surprise!"
    92  	s.checkNotValid(c, "nil Machines not valid")
    93  }
    94  
    95  func (s *ConfigSuite) TestNilStatus(c *gc.C) {
    96  	s.config.Status = nil
    97  	s.checkNotValid(c, "nil Status not valid")
    98  }
    99  
   100  func (s *ConfigSuite) TestNilClock(c *gc.C) {
   101  	s.config.Clock = nil
   102  	s.checkNotValid(c, "nil Clock not valid")
   103  }
   104  
   105  func (s *ConfigSuite) checkNotValid(c *gc.C, match string) {
   106  	err := s.config.Validate()
   107  	c.Check(err, jc.Satisfies, errors.IsNotValid)
   108  	c.Check(err, gc.ErrorMatches, match)
   109  }
   110  
   111  func validEnvironConfig() storageprovisioner.Config {
   112  	cfg := almostValidConfig()
   113  	cfg.Scope = coretesting.ModelTag
   114  	return cfg
   115  }
   116  
   117  func validMachineConfig() storageprovisioner.Config {
   118  	config := almostValidConfig()
   119  	config.Scope = names.NewMachineTag("123/lxd/7")
   120  	config.StorageDir = "storage-dir"
   121  	return config
   122  }
   123  
   124  func validApplicationConfig() storageprovisioner.Config {
   125  	config := almostValidConfig()
   126  	config.Scope = names.NewApplicationTag("mariadb")
   127  	return config
   128  }
   129  
   130  func almostValidConfig() storageprovisioner.Config {
   131  	// gofmt doesn't seem to want to let me one-line any of these
   132  	// except the last one, so I'm standardising on multi-line.
   133  	return storageprovisioner.Config{
   134  		CloudCallContext: context.NewCloudCallContext(),
   135  		Volumes: struct {
   136  			storageprovisioner.VolumeAccessor
   137  		}{},
   138  		Filesystems: struct {
   139  			storageprovisioner.FilesystemAccessor
   140  		}{},
   141  		Life: struct {
   142  			storageprovisioner.LifecycleManager
   143  		}{},
   144  		Registry: struct {
   145  			storage.ProviderRegistry
   146  		}{},
   147  		Machines: struct {
   148  			storageprovisioner.MachineAccessor
   149  		}{},
   150  		Status: struct {
   151  			storageprovisioner.StatusSetter
   152  		}{},
   153  		Clock: struct {
   154  			clock.Clock
   155  		}{},
   156  	}
   157  }