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