github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/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, coretesting.ModelTag)
    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{Entities: []params.Entity{
    37  			{Tag: coretesting.ModelTag.String()},
    38  		}})
    39  		*(response.(*params.CloudSpecResults)) = params.CloudSpecResults{
    40  			Results: []params.CloudSpecResult{{
    41  				Result: &params.CloudSpec{
    42  					Type:             "type",
    43  					Name:             "name",
    44  					Region:           "region",
    45  					Endpoint:         "endpoint",
    46  					IdentityEndpoint: "identity-endpoint",
    47  					StorageEndpoint:  "storage-endpoint",
    48  					Credential: &params.CloudCredential{
    49  						AuthType:   "auth-type",
    50  						Attributes: map[string]string{"k": "v"},
    51  					},
    52  					CACertificates: []string{coretesting.CACert},
    53  				},
    54  			}},
    55  		}
    56  		return nil
    57  	}
    58  	api := cloudspec.NewCloudSpecAPI(&facadeCaller, coretesting.ModelTag)
    59  	cloudSpec, err := api.CloudSpec()
    60  	c.Assert(err, jc.ErrorIsNil)
    61  
    62  	credential := cloud.NewCredential(
    63  		"auth-type",
    64  		map[string]string{"k": "v"},
    65  	)
    66  	c.Assert(cloudSpec, jc.DeepEquals, environs.CloudSpec{
    67  		Type:             "type",
    68  		Name:             "name",
    69  		Region:           "region",
    70  		Endpoint:         "endpoint",
    71  		IdentityEndpoint: "identity-endpoint",
    72  		StorageEndpoint:  "storage-endpoint",
    73  		Credential:       &credential,
    74  		CACertificates:   []string{coretesting.CACert},
    75  	})
    76  }
    77  
    78  func (s *CloudSpecSuite) TestCloudSpecOverallError(c *gc.C) {
    79  	expect := errors.New("bewm")
    80  	facadeCaller := apitesting.StubFacadeCaller{Stub: &testing.Stub{}}
    81  	facadeCaller.FacadeCallFn = func(name string, args, response interface{}) error {
    82  		return expect
    83  	}
    84  	api := cloudspec.NewCloudSpecAPI(&facadeCaller, coretesting.ModelTag)
    85  	_, err := api.CloudSpec()
    86  	c.Assert(err, gc.Equals, expect)
    87  }
    88  
    89  func (s *CloudSpecSuite) TestCloudSpecResultCountMismatch(c *gc.C) {
    90  	facadeCaller := apitesting.StubFacadeCaller{Stub: &testing.Stub{}}
    91  	facadeCaller.FacadeCallFn = func(name string, args, response interface{}) error {
    92  		return nil
    93  	}
    94  	api := cloudspec.NewCloudSpecAPI(&facadeCaller, coretesting.ModelTag)
    95  	_, err := api.CloudSpec()
    96  	c.Assert(err, gc.ErrorMatches, "expected 1 result, got 0")
    97  }
    98  
    99  func (s *CloudSpecSuite) TestCloudSpecResultError(c *gc.C) {
   100  	facadeCaller := apitesting.StubFacadeCaller{Stub: &testing.Stub{}}
   101  	facadeCaller.FacadeCallFn = func(name string, args, response interface{}) error {
   102  		*(response.(*params.CloudSpecResults)) = params.CloudSpecResults{
   103  			Results: []params.CloudSpecResult{{
   104  				Error: &params.Error{
   105  					Code:    params.CodeUnauthorized,
   106  					Message: "dang",
   107  				},
   108  			}},
   109  		}
   110  		return nil
   111  	}
   112  	api := cloudspec.NewCloudSpecAPI(&facadeCaller, coretesting.ModelTag)
   113  	_, err := api.CloudSpec()
   114  	c.Assert(err, jc.Satisfies, params.IsCodeUnauthorized)
   115  	c.Assert(err, gc.ErrorMatches, "API request failed: dang")
   116  }
   117  
   118  func (s *CloudSpecSuite) TestCloudSpecInvalidCloudSpec(c *gc.C) {
   119  	facadeCaller := apitesting.StubFacadeCaller{Stub: &testing.Stub{}}
   120  	facadeCaller.FacadeCallFn = func(name string, args, response interface{}) error {
   121  		*(response.(*params.CloudSpecResults)) = params.CloudSpecResults{Results: []params.CloudSpecResult{{
   122  			Result: &params.CloudSpec{
   123  				Type: "",
   124  			},
   125  		}}}
   126  		return nil
   127  	}
   128  	api := cloudspec.NewCloudSpecAPI(&facadeCaller, coretesting.ModelTag)
   129  	_, err := api.CloudSpec()
   130  	c.Assert(err, gc.ErrorMatches, "validating CloudSpec: empty Type not valid")
   131  }