github.com/axw/juju@v0.0.0-20161005053422-4bd6544d08d4/state/cloud_test.go (about) 1 // Copyright 2016 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package state_test 5 6 import ( 7 "github.com/juju/errors" 8 jc "github.com/juju/testing/checkers" 9 gc "gopkg.in/check.v1" 10 "gopkg.in/juju/names.v2" 11 12 "github.com/juju/juju/cloud" 13 ) 14 15 type CloudSuite struct { 16 ConnSuite 17 } 18 19 var _ = gc.Suite(&CloudSuite{}) 20 21 var lowCloud = cloud.Cloud{ 22 Type: "low", 23 AuthTypes: cloud.AuthTypes{cloud.AccessKeyAuthType, cloud.UserPassAuthType}, 24 Endpoint: "global-endpoint", 25 IdentityEndpoint: "identity-endpoint", 26 StorageEndpoint: "storage-endpoint", 27 Regions: []cloud.Region{{ 28 Name: "region1", 29 Endpoint: "region1-endpoint", 30 IdentityEndpoint: "region1-identity", 31 StorageEndpoint: "region1-storage", 32 }, { 33 Name: "region2", 34 Endpoint: "region2-endpoint", 35 IdentityEndpoint: "region2-identity", 36 StorageEndpoint: "region2-storage", 37 }}, 38 } 39 40 func (s *CloudSuite) TestCloudNotFound(c *gc.C) { 41 cld, err := s.State.Cloud("unknown") 42 c.Assert(err, gc.ErrorMatches, `cloud "unknown" not found`) 43 c.Assert(cld, jc.DeepEquals, cloud.Cloud{}) 44 c.Assert(err, jc.Satisfies, errors.IsNotFound) 45 } 46 47 func (s *CloudSuite) TestClouds(c *gc.C) { 48 dummyCloud, err := s.State.Cloud("dummy") 49 c.Assert(err, jc.ErrorIsNil) 50 err = s.State.AddCloud("stratus", lowCloud) 51 c.Assert(err, jc.ErrorIsNil) 52 53 clouds, err := s.State.Clouds() 54 c.Assert(err, jc.ErrorIsNil) 55 c.Assert(clouds, jc.DeepEquals, map[names.CloudTag]cloud.Cloud{ 56 names.NewCloudTag("dummy"): dummyCloud, 57 names.NewCloudTag("stratus"): lowCloud, 58 }) 59 } 60 61 func (s *CloudSuite) TestAddCloud(c *gc.C) { 62 err := s.State.AddCloud("stratus", lowCloud) 63 c.Assert(err, jc.ErrorIsNil) 64 cloud, err := s.State.Cloud("stratus") 65 c.Assert(err, jc.ErrorIsNil) 66 c.Assert(cloud, jc.DeepEquals, lowCloud) 67 } 68 69 func (s *CloudSuite) TestAddCloudDuplicate(c *gc.C) { 70 err := s.State.AddCloud("stratus", cloud.Cloud{ 71 Type: "low", 72 AuthTypes: cloud.AuthTypes{cloud.AccessKeyAuthType, cloud.UserPassAuthType}, 73 }) 74 c.Assert(err, jc.ErrorIsNil) 75 err = s.State.AddCloud("stratus", cloud.Cloud{ 76 Type: "low", 77 AuthTypes: cloud.AuthTypes{cloud.AccessKeyAuthType, cloud.UserPassAuthType}, 78 }) 79 c.Assert(err, gc.ErrorMatches, `cloud "stratus" already exists`) 80 c.Assert(err, jc.Satisfies, errors.IsAlreadyExists) 81 } 82 83 func (s *CloudSuite) TestAddCloudNoType(c *gc.C) { 84 err := s.State.AddCloud("stratus", cloud.Cloud{ 85 AuthTypes: cloud.AuthTypes{cloud.AccessKeyAuthType, cloud.UserPassAuthType}, 86 }) 87 c.Assert(err, gc.ErrorMatches, `invalid cloud: empty Type not valid`) 88 } 89 90 func (s *CloudSuite) TestAddCloudNoAuthTypes(c *gc.C) { 91 err := s.State.AddCloud("stratus", cloud.Cloud{ 92 Type: "foo", 93 }) 94 c.Assert(err, gc.ErrorMatches, `invalid cloud: empty auth-types not valid`) 95 }