github.com/axw/juju@v0.0.0-20161005053422-4bd6544d08d4/api/common/cloudspec/cloudspec_test.go (about) 1 // Copyright 2016 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package cloudspec_test 5 6 import ( 7 "errors" 8 9 "github.com/juju/testing" 10 jc "github.com/juju/testing/checkers" 11 gc "gopkg.in/check.v1" 12 13 apitesting "github.com/juju/juju/api/base/testing" 14 "github.com/juju/juju/api/common/cloudspec" 15 "github.com/juju/juju/apiserver/params" 16 "github.com/juju/juju/cloud" 17 "github.com/juju/juju/environs" 18 coretesting "github.com/juju/juju/testing" 19 ) 20 21 var _ = gc.Suite(&CloudSpecSuite{}) 22 23 type CloudSpecSuite struct { 24 testing.IsolationSuite 25 } 26 27 func (s *CloudSpecSuite) TestNewCloudSpecAPI(c *gc.C) { 28 api := cloudspec.NewCloudSpecAPI(nil) 29 c.Check(api, gc.NotNil) 30 } 31 32 func (s *CloudSpecSuite) TestCloudSpec(c *gc.C) { 33 facadeCaller := apitesting.StubFacadeCaller{Stub: &testing.Stub{}} 34 facadeCaller.FacadeCallFn = func(name string, args, response interface{}) error { 35 c.Assert(name, gc.Equals, "CloudSpec") 36 c.Assert(args, jc.DeepEquals, params.Entities{[]params.Entity{ 37 {coretesting.ModelTag.String()}, 38 }}) 39 *(response.(*params.CloudSpecResults)) = params.CloudSpecResults{ 40 []params.CloudSpecResult{{ 41 Result: ¶ms.CloudSpec{ 42 Type: "type", 43 Name: "name", 44 Region: "region", 45 Endpoint: "endpoint", 46 IdentityEndpoint: "identity-endpoint", 47 StorageEndpoint: "storage-endpoint", 48 Credential: ¶ms.CloudCredential{ 49 AuthType: "auth-type", 50 Attributes: map[string]string{"k": "v"}, 51 }, 52 }, 53 }}, 54 } 55 return nil 56 } 57 api := cloudspec.NewCloudSpecAPI(&facadeCaller) 58 cloudSpec, err := api.CloudSpec(coretesting.ModelTag) 59 c.Assert(err, jc.ErrorIsNil) 60 61 credential := cloud.NewCredential( 62 "auth-type", 63 map[string]string{"k": "v"}, 64 ) 65 c.Assert(cloudSpec, jc.DeepEquals, environs.CloudSpec{ 66 Type: "type", 67 Name: "name", 68 Region: "region", 69 Endpoint: "endpoint", 70 IdentityEndpoint: "identity-endpoint", 71 StorageEndpoint: "storage-endpoint", 72 Credential: &credential, 73 }) 74 } 75 76 func (s *CloudSpecSuite) TestCloudSpecOverallError(c *gc.C) { 77 expect := errors.New("bewm") 78 facadeCaller := apitesting.StubFacadeCaller{Stub: &testing.Stub{}} 79 facadeCaller.FacadeCallFn = func(name string, args, response interface{}) error { 80 return expect 81 } 82 api := cloudspec.NewCloudSpecAPI(&facadeCaller) 83 _, err := api.CloudSpec(coretesting.ModelTag) 84 c.Assert(err, gc.Equals, expect) 85 } 86 87 func (s *CloudSpecSuite) TestCloudSpecResultCountMismatch(c *gc.C) { 88 facadeCaller := apitesting.StubFacadeCaller{Stub: &testing.Stub{}} 89 facadeCaller.FacadeCallFn = func(name string, args, response interface{}) error { 90 return nil 91 } 92 api := cloudspec.NewCloudSpecAPI(&facadeCaller) 93 _, err := api.CloudSpec(coretesting.ModelTag) 94 c.Assert(err, gc.ErrorMatches, "expected 1 result, got 0") 95 } 96 97 func (s *CloudSpecSuite) TestCloudSpecResultError(c *gc.C) { 98 facadeCaller := apitesting.StubFacadeCaller{Stub: &testing.Stub{}} 99 facadeCaller.FacadeCallFn = func(name string, args, response interface{}) error { 100 *(response.(*params.CloudSpecResults)) = params.CloudSpecResults{ 101 []params.CloudSpecResult{{ 102 Error: ¶ms.Error{ 103 Code: params.CodeUnauthorized, 104 Message: "dang", 105 }, 106 }}, 107 } 108 return nil 109 } 110 api := cloudspec.NewCloudSpecAPI(&facadeCaller) 111 _, err := api.CloudSpec(coretesting.ModelTag) 112 c.Assert(err, jc.Satisfies, params.IsCodeUnauthorized) 113 c.Assert(err, gc.ErrorMatches, "API request failed: dang") 114 } 115 116 func (s *CloudSpecSuite) TestCloudSpecInvalidCloudSpec(c *gc.C) { 117 facadeCaller := apitesting.StubFacadeCaller{Stub: &testing.Stub{}} 118 facadeCaller.FacadeCallFn = func(name string, args, response interface{}) error { 119 *(response.(*params.CloudSpecResults)) = params.CloudSpecResults{[]params.CloudSpecResult{{ 120 Result: ¶ms.CloudSpec{ 121 Type: "", 122 }, 123 }}} 124 return nil 125 } 126 api := cloudspec.NewCloudSpecAPI(&facadeCaller) 127 _, err := api.CloudSpec(coretesting.ModelTag) 128 c.Assert(err, gc.ErrorMatches, "validating CloudSpec: empty Type not valid") 129 }