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