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