github.com/mwhudson/juju@v0.0.0-20160512215208-90ff01f3497f/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 local.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 local.mallards: 31 unresolved-api-endpoints: [maas-1-05.cluster.mallards] 32 uuid: this-is-another-uuid 33 api-endpoints: [this-is-another-of-many-api-endpoints, this-is-one-more-of-many-api-endpoints] 34 ca-cert: this-is-another-ca-cert 35 local.mark-test-prodstack: 36 unresolved-api-endpoints: [vm-23532.prodstack.canonical.com, great.test.server.hostname.co.nz] 37 uuid: this-is-a-uuid 38 api-endpoints: [this-is-one-of-many-api-endpoints] 39 ca-cert: this-is-a-ca-cert 40 ` 41 42 func (s *ControllersFileSuite) TestWriteFile(c *gc.C) { 43 writeTestControllersFile(c) 44 data, err := ioutil.ReadFile(osenv.JujuXDGDataHomePath("controllers.yaml")) 45 c.Assert(err, jc.ErrorIsNil) 46 c.Assert(string(data), gc.Equals, testControllersYAML[1:]) 47 } 48 49 func (s *ControllersFileSuite) TestReadNoFile(c *gc.C) { 50 controllers, err := jujuclient.ReadControllersFile("nohere.yaml") 51 c.Assert(err, jc.ErrorIsNil) 52 c.Assert(controllers, gc.IsNil) 53 } 54 55 func (s *ControllersFileSuite) TestReadEmptyFile(c *gc.C) { 56 err := ioutil.WriteFile(osenv.JujuXDGDataHomePath("controllers.yaml"), []byte(""), 0600) 57 c.Assert(err, jc.ErrorIsNil) 58 59 controllerStore := jujuclient.NewFileClientStore() 60 controllers, err := controllerStore.AllControllers() 61 c.Assert(err, jc.ErrorIsNil) 62 c.Assert(controllers, gc.IsNil) 63 } 64 65 func parseControllers(c *gc.C) map[string]jujuclient.ControllerDetails { 66 controllers, err := jujuclient.ParseControllers([]byte(testControllersYAML)) 67 c.Assert(err, jc.ErrorIsNil) 68 69 // ensure that multiple server hostnames and eapi endpoints are parsed correctly 70 c.Assert(controllers["local.mark-test-prodstack"].UnresolvedAPIEndpoints, gc.HasLen, 2) 71 c.Assert(controllers["local.mallards"].APIEndpoints, gc.HasLen, 2) 72 return controllers 73 } 74 75 func writeTestControllersFile(c *gc.C) map[string]jujuclient.ControllerDetails { 76 controllers := parseControllers(c) 77 err := jujuclient.WriteControllersFile(controllers) 78 c.Assert(err, jc.ErrorIsNil) 79 return controllers 80 } 81 82 func (s *ControllersFileSuite) TestParseControllerMetadata(c *gc.C) { 83 controllers := parseControllers(c) 84 var names []string 85 for name, _ := range controllers { 86 names = append(names, name) 87 } 88 c.Assert(names, jc.SameContents, 89 []string{"local.mark-test-prodstack", "local.mallards", "local.aws-test"}) 90 } 91 92 func (s *ControllersFileSuite) TestParseControllerMetadataError(c *gc.C) { 93 controllers, err := jujuclient.ParseControllers([]byte("fail me now")) 94 c.Assert(err, gc.ErrorMatches, "cannot unmarshal yaml controllers metadata: yaml: unmarshal errors:\n line 1: cannot unmarshal !!str `fail me...` into jujuclient.controllersCollection") 95 c.Assert(controllers, gc.IsNil) 96 }