github.com/mattyw/juju@v0.0.0-20140610034352-732aecd63861/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  	gc "launchpad.net/gocheck"
    10  	"launchpad.net/goyaml"
    11  
    12  	"github.com/juju/juju/worker/localstorage"
    13  )
    14  
    15  type configSuite struct{}
    16  
    17  var _ = gc.Suite(&configSuite{})
    18  
    19  func TestPackage(t *stdtesting.T) {
    20  	gc.TestingT(t)
    21  }
    22  
    23  type localStorageConfig struct {
    24  	storageDir  string
    25  	storageAddr string
    26  }
    27  
    28  func (c *localStorageConfig) StorageDir() string {
    29  	return c.storageDir
    30  }
    31  
    32  func (c *localStorageConfig) StorageAddr() string {
    33  	return c.storageAddr
    34  }
    35  
    36  type localTLSStorageConfig struct {
    37  	localStorageConfig
    38  	caCertPEM string
    39  	caKeyPEM  string
    40  	hostnames []string
    41  	authkey   string
    42  }
    43  
    44  func (c *localTLSStorageConfig) StorageCACert() string {
    45  	return c.caCertPEM
    46  }
    47  
    48  func (c *localTLSStorageConfig) StorageCAKey() string {
    49  	return c.caKeyPEM
    50  }
    51  
    52  func (c *localTLSStorageConfig) StorageHostnames() []string {
    53  	return c.hostnames
    54  }
    55  
    56  func (c *localTLSStorageConfig) StorageAuthKey() string {
    57  	return c.authkey
    58  }
    59  
    60  func (*configSuite) TestStoreConfig(c *gc.C) {
    61  	var config localStorageConfig
    62  	m, err := localstorage.StoreConfig(&config)
    63  	c.Assert(err, gc.IsNil)
    64  	c.Assert(m, gc.DeepEquals, map[string]string{
    65  		localstorage.StorageDir:  "",
    66  		localstorage.StorageAddr: "",
    67  	})
    68  
    69  	config.storageDir = "a"
    70  	config.storageAddr = "b"
    71  	m, err = localstorage.StoreConfig(&config)
    72  	c.Assert(err, gc.IsNil)
    73  	c.Assert(m, gc.DeepEquals, map[string]string{
    74  		localstorage.StorageDir:  config.storageDir,
    75  		localstorage.StorageAddr: config.storageAddr,
    76  	})
    77  }
    78  
    79  func (*configSuite) TestStoreConfigTLS(c *gc.C) {
    80  	var config localTLSStorageConfig
    81  	m, err := localstorage.StoreConfig(&config)
    82  	c.Assert(err, gc.IsNil)
    83  	c.Assert(m, gc.DeepEquals, map[string]string{
    84  		localstorage.StorageDir:  "",
    85  		localstorage.StorageAddr: "",
    86  	})
    87  
    88  	config.storageDir = "a"
    89  	config.storageAddr = "b"
    90  	config.caCertPEM = "heyhey"
    91  	config.caKeyPEM = "hoho"
    92  	config.hostnames = []string{"easy", "as", "1.2.3"}
    93  	config.authkey = "password"
    94  	m, err = localstorage.StoreConfig(&config)
    95  	c.Assert(err, gc.IsNil)
    96  	c.Assert(m, gc.DeepEquals, map[string]string{
    97  		localstorage.StorageDir:       config.storageDir,
    98  		localstorage.StorageAddr:      config.storageAddr,
    99  		localstorage.StorageCACert:    string(config.caCertPEM),
   100  		localstorage.StorageCAKey:     string(config.caKeyPEM),
   101  		localstorage.StorageHostnames: mustMarshalYAML(c, config.hostnames),
   102  		localstorage.StorageAuthKey:   config.authkey,
   103  	})
   104  }
   105  
   106  func mustMarshalYAML(c *gc.C, v interface{}) string {
   107  	data, err := goyaml.Marshal(v)
   108  	c.Assert(err, gc.IsNil)
   109  	return string(data)
   110  }