github.com/wallyworld/juju@v0.0.0-20161013125918-6cf1bc9d917a/jujuclient/controllersfile_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 ControllersFileSuite struct { 18 testing.FakeJujuXDGDataHomeSuite 19 } 20 21 var _ = gc.Suite(&ControllersFileSuite{}) 22 23 const testControllersYAML = ` 24 controllers: 25 aws-test: 26 unresolved-api-endpoints: [instance-1-2-4.useast.aws.com] 27 uuid: this-is-the-aws-test-uuid 28 api-endpoints: [this-is-aws-test-of-many-api-endpoints] 29 ca-cert: this-is-aws-test-ca-cert 30 cloud: aws 31 region: us-east-1 32 controller-machine-count: 0 33 active-controller-machine-count: 0 34 mallards: 35 unresolved-api-endpoints: [maas-1-05.cluster.mallards] 36 uuid: this-is-another-uuid 37 api-endpoints: [this-is-another-of-many-api-endpoints, this-is-one-more-of-many-api-endpoints] 38 ca-cert: this-is-another-ca-cert 39 cloud: mallards 40 controller-machine-count: 0 41 active-controller-machine-count: 0 42 mark-test-prodstack: 43 unresolved-api-endpoints: [vm-23532.prodstack.canonical.com, great.test.server.hostname.co.nz] 44 uuid: this-is-a-uuid 45 api-endpoints: [this-is-one-of-many-api-endpoints] 46 ca-cert: this-is-a-ca-cert 47 cloud: prodstack 48 controller-machine-count: 0 49 active-controller-machine-count: 0 50 current-controller: mallards 51 ` 52 53 func (s *ControllersFileSuite) TestWriteFile(c *gc.C) { 54 writeTestControllersFile(c) 55 data, err := ioutil.ReadFile(osenv.JujuXDGDataHomePath("controllers.yaml")) 56 c.Assert(err, jc.ErrorIsNil) 57 c.Assert(string(data), gc.Equals, testControllersYAML[1:]) 58 } 59 60 func (s *ControllersFileSuite) TestReadNoFile(c *gc.C) { 61 controllers, err := jujuclient.ReadControllersFile("nohere.yaml") 62 c.Assert(err, jc.ErrorIsNil) 63 c.Assert(controllers, gc.NotNil) 64 c.Assert(controllers.Controllers, gc.HasLen, 0) 65 c.Assert(controllers.CurrentController, gc.Equals, "") 66 } 67 68 func (s *ControllersFileSuite) TestReadEmptyFile(c *gc.C) { 69 err := ioutil.WriteFile(osenv.JujuXDGDataHomePath("controllers.yaml"), []byte(""), 0600) 70 c.Assert(err, jc.ErrorIsNil) 71 72 controllerStore := jujuclient.NewFileClientStore() 73 controllers, err := controllerStore.AllControllers() 74 c.Assert(err, jc.ErrorIsNil) 75 c.Assert(controllers, gc.IsNil) 76 } 77 78 func parseControllers(c *gc.C) *jujuclient.Controllers { 79 controllers, err := jujuclient.ParseControllers([]byte(testControllersYAML)) 80 c.Assert(err, jc.ErrorIsNil) 81 82 // ensure that multiple server hostnames and eapi endpoints are parsed correctly 83 c.Assert(controllers.Controllers["mark-test-prodstack"].UnresolvedAPIEndpoints, gc.HasLen, 2) 84 c.Assert(controllers.Controllers["mallards"].APIEndpoints, gc.HasLen, 2) 85 return controllers 86 } 87 88 func writeTestControllersFile(c *gc.C) *jujuclient.Controllers { 89 controllers := parseControllers(c) 90 err := jujuclient.WriteControllersFile(controllers) 91 c.Assert(err, jc.ErrorIsNil) 92 return controllers 93 } 94 95 func (s *ControllersFileSuite) TestParseControllerMetadata(c *gc.C) { 96 controllers := parseControllers(c) 97 var names []string 98 for name, _ := range controllers.Controllers { 99 names = append(names, name) 100 } 101 c.Assert(names, jc.SameContents, 102 []string{"mark-test-prodstack", "mallards", "aws-test"}, 103 ) 104 c.Assert(controllers.CurrentController, gc.Equals, "mallards") 105 } 106 107 func (s *ControllersFileSuite) TestParseControllerMetadataError(c *gc.C) { 108 controllers, err := jujuclient.ParseControllers([]byte("fail me now")) 109 c.Assert(err, gc.ErrorMatches, "cannot unmarshal yaml controllers metadata: yaml: unmarshal errors:\n line 1: cannot unmarshal !!str `fail me...` into jujuclient.Controllers") 110 c.Assert(controllers, gc.IsNil) 111 }