github.com/cloud-green/juju@v0.0.0-20151002100041-a00291338d3d/worker/localstorage/config_test.go (about) 1 // Copyright 2013 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package localstorage_test 5 6 import ( 7 stdtesting "testing" 8 9 jc "github.com/juju/testing/checkers" 10 gc "gopkg.in/check.v1" 11 goyaml "gopkg.in/yaml.v1" 12 13 "github.com/juju/juju/worker/localstorage" 14 ) 15 16 type configSuite struct{} 17 18 var _ = gc.Suite(&configSuite{}) 19 20 func TestPackage(t *stdtesting.T) { 21 gc.TestingT(t) 22 } 23 24 type localStorageConfig struct { 25 storageDir string 26 storageAddr string 27 } 28 29 func (c *localStorageConfig) StorageDir() string { 30 return c.storageDir 31 } 32 33 func (c *localStorageConfig) StorageAddr() string { 34 return c.storageAddr 35 } 36 37 type localTLSStorageConfig struct { 38 localStorageConfig 39 caCertPEM string 40 caKeyPEM string 41 hostnames []string 42 authkey string 43 } 44 45 func (c *localTLSStorageConfig) StorageCACert() string { 46 return c.caCertPEM 47 } 48 49 func (c *localTLSStorageConfig) StorageCAKey() string { 50 return c.caKeyPEM 51 } 52 53 func (c *localTLSStorageConfig) StorageHostnames() []string { 54 return c.hostnames 55 } 56 57 func (c *localTLSStorageConfig) StorageAuthKey() string { 58 return c.authkey 59 } 60 61 func (*configSuite) TestStoreConfig(c *gc.C) { 62 var config localStorageConfig 63 m, err := localstorage.StoreConfig(&config) 64 c.Assert(err, jc.ErrorIsNil) 65 c.Assert(m, gc.DeepEquals, map[string]string{ 66 localstorage.StorageDir: "", 67 localstorage.StorageAddr: "", 68 }) 69 70 config.storageDir = "a" 71 config.storageAddr = "b" 72 m, err = localstorage.StoreConfig(&config) 73 c.Assert(err, jc.ErrorIsNil) 74 c.Assert(m, gc.DeepEquals, map[string]string{ 75 localstorage.StorageDir: config.storageDir, 76 localstorage.StorageAddr: config.storageAddr, 77 }) 78 } 79 80 func (*configSuite) TestStoreConfigTLS(c *gc.C) { 81 var config localTLSStorageConfig 82 m, err := localstorage.StoreConfig(&config) 83 c.Assert(err, jc.ErrorIsNil) 84 c.Assert(m, gc.DeepEquals, map[string]string{ 85 localstorage.StorageDir: "", 86 localstorage.StorageAddr: "", 87 }) 88 89 config.storageDir = "a" 90 config.storageAddr = "b" 91 config.caCertPEM = "heyhey" 92 config.caKeyPEM = "hoho" 93 config.hostnames = []string{"easy", "as", "1.2.3"} 94 config.authkey = "password" 95 m, err = localstorage.StoreConfig(&config) 96 c.Assert(err, jc.ErrorIsNil) 97 c.Assert(m, gc.DeepEquals, map[string]string{ 98 localstorage.StorageDir: config.storageDir, 99 localstorage.StorageAddr: config.storageAddr, 100 localstorage.StorageCACert: string(config.caCertPEM), 101 localstorage.StorageCAKey: string(config.caKeyPEM), 102 localstorage.StorageHostnames: mustMarshalYAML(c, config.hostnames), 103 localstorage.StorageAuthKey: config.authkey, 104 }) 105 } 106 107 func mustMarshalYAML(c *gc.C, v interface{}) string { 108 data, err := goyaml.Marshal(v) 109 c.Assert(err, jc.ErrorIsNil) 110 return string(data) 111 }