github.com/mwhudson/juju@v0.0.0-20160512215208-90ff01f3497f/jujuclient/bootstrapconfigfile_test.go (about)

     1  // Copyright 2016 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package jujuclient_test
     5  
     6  import (
     7  	"io/ioutil"
     8  
     9  	jc "github.com/juju/testing/checkers"
    10  	gc "gopkg.in/check.v1"
    11  
    12  	"github.com/juju/juju/juju/osenv"
    13  	"github.com/juju/juju/jujuclient"
    14  	"github.com/juju/juju/testing"
    15  )
    16  
    17  type BootstrapConfigFileSuite struct {
    18  	testing.FakeJujuXDGDataHomeSuite
    19  }
    20  
    21  var _ = gc.Suite(&BootstrapConfigFileSuite{})
    22  
    23  const testBootstrapConfigYAML = `
    24  controllers:
    25    local.aws-test:
    26      config:
    27        name: admin
    28        type: ec2
    29      credential: default
    30      cloud: aws
    31      region: us-east-1
    32      endpoint: https://us-east-1.amazonaws.com
    33    local.mallards:
    34      config:
    35        name: admin
    36        type: maas
    37      cloud: maas
    38      region: 127.0.0.1
    39  `
    40  
    41  var testBootstrapConfig = map[string]jujuclient.BootstrapConfig{
    42  	"local.aws-test": {
    43  		Config: map[string]interface{}{
    44  			"type": "ec2",
    45  			"name": "admin",
    46  		},
    47  		Credential:    "default",
    48  		Cloud:         "aws",
    49  		CloudRegion:   "us-east-1",
    50  		CloudEndpoint: "https://us-east-1.amazonaws.com",
    51  	},
    52  	"local.mallards": {
    53  		Config: map[string]interface{}{
    54  			"type": "maas",
    55  			"name": "admin",
    56  		},
    57  		Cloud:       "maas",
    58  		CloudRegion: "127.0.0.1",
    59  	},
    60  }
    61  
    62  func (s *BootstrapConfigFileSuite) TestWriteFile(c *gc.C) {
    63  	writeTestBootstrapConfigFile(c)
    64  	data, err := ioutil.ReadFile(osenv.JujuXDGDataHomePath("bootstrap-config.yaml"))
    65  	c.Assert(err, jc.ErrorIsNil)
    66  	c.Assert(string(data), gc.Equals, testBootstrapConfigYAML[1:])
    67  }
    68  
    69  func (s *BootstrapConfigFileSuite) TestReadNoFile(c *gc.C) {
    70  	controllers, err := jujuclient.ReadBootstrapConfigFile("nohere.yaml")
    71  	c.Assert(err, jc.ErrorIsNil)
    72  	c.Assert(controllers, gc.IsNil)
    73  }
    74  
    75  func (s *BootstrapConfigFileSuite) TestReadEmptyFile(c *gc.C) {
    76  	path := osenv.JujuXDGDataHomePath("bootstrap-config.yaml")
    77  	err := ioutil.WriteFile(path, []byte(""), 0600)
    78  	c.Assert(err, jc.ErrorIsNil)
    79  
    80  	configs, err := jujuclient.ReadBootstrapConfigFile(path)
    81  	c.Assert(err, jc.ErrorIsNil)
    82  	c.Assert(configs, gc.HasLen, 0)
    83  }
    84  
    85  func parseBootstrapConfig(c *gc.C) map[string]jujuclient.BootstrapConfig {
    86  	configs, err := jujuclient.ParseBootstrapConfig([]byte(testBootstrapConfigYAML))
    87  	c.Assert(err, jc.ErrorIsNil)
    88  	return configs
    89  }
    90  
    91  func writeTestBootstrapConfigFile(c *gc.C) map[string]jujuclient.BootstrapConfig {
    92  	configs := parseBootstrapConfig(c)
    93  	err := jujuclient.WriteBootstrapConfigFile(configs)
    94  	c.Assert(err, jc.ErrorIsNil)
    95  	return configs
    96  }
    97  
    98  func (s *BootstrapConfigFileSuite) TestParseControllerMetadata(c *gc.C) {
    99  	controllers := parseBootstrapConfig(c)
   100  	var names []string
   101  	for name, _ := range controllers {
   102  		names = append(names, name)
   103  	}
   104  	c.Assert(names, jc.SameContents, []string{"local.mallards", "local.aws-test"})
   105  }
   106  
   107  func (s *BootstrapConfigFileSuite) TestParseControllerMetadataError(c *gc.C) {
   108  	controllers, err := jujuclient.ParseBootstrapConfig([]byte("fail me now"))
   109  	c.Assert(err, gc.ErrorMatches, "cannot unmarshal bootstrap config: yaml: unmarshal errors:\n  line 1: cannot unmarshal !!str `fail me...` into jujuclient.bootstrapConfigCollection")
   110  	c.Assert(controllers, gc.IsNil)
   111  }