github.com/juju/juju@v0.0.0-20240430160146-1752b71fcf00/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/cloud"
    16  	environscloudspec "github.com/juju/juju/environs/cloudspec"
    17  	"github.com/juju/juju/rpc/params"
    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  					SkipTLSVerify:  true,
    54  				},
    55  			}},
    56  		}
    57  		return nil
    58  	}
    59  	api := cloudspec.NewCloudSpecAPI(&facadeCaller, coretesting.ModelTag)
    60  	cloudSpec, err := api.CloudSpec()
    61  	c.Assert(err, jc.ErrorIsNil)
    62  
    63  	credential := cloud.NewCredential(
    64  		"auth-type",
    65  		map[string]string{"k": "v"},
    66  	)
    67  	c.Assert(cloudSpec, jc.DeepEquals, environscloudspec.CloudSpec{
    68  		Type:             "type",
    69  		Name:             "name",
    70  		Region:           "region",
    71  		Endpoint:         "endpoint",
    72  		IdentityEndpoint: "identity-endpoint",
    73  		StorageEndpoint:  "storage-endpoint",
    74  		Credential:       &credential,
    75  		CACertificates:   []string{coretesting.CACert},
    76  		SkipTLSVerify:    true,
    77  	})
    78  }
    79  
    80  func (s *CloudSpecSuite) TestWatchCloudSpecChanges(c *gc.C) {
    81  	called := false
    82  	facadeCaller := apitesting.StubFacadeCaller{Stub: &testing.Stub{}}
    83  	facadeCaller.ReturnRawAPICaller = apitesting.BestVersionCaller{
    84  		APICallerFunc: apitesting.APICallerFunc(
    85  			func(objType string, version int, id, request string, a, response interface{}) error {
    86  				c.Assert(request, gc.Equals, "Next")
    87  				return nil
    88  			}),
    89  		BestVersion: 1}
    90  	facadeCaller.FacadeCallFn = func(name string, args, response interface{}) error {
    91  		c.Assert(name, gc.Equals, "WatchCloudSpecsChanges")
    92  		c.Assert(args, jc.DeepEquals, params.Entities{Entities: []params.Entity{
    93  			{Tag: coretesting.ModelTag.String()},
    94  		}})
    95  		*(response.(*params.NotifyWatchResults)) = params.NotifyWatchResults{
    96  			Results: []params.NotifyWatchResult{{
    97  				NotifyWatcherId: "1",
    98  			}},
    99  		}
   100  		called = true
   101  		return nil
   102  	}
   103  	api := cloudspec.NewCloudSpecAPI(&facadeCaller, coretesting.ModelTag)
   104  	w, err := api.WatchCloudSpecChanges()
   105  	c.Assert(err, jc.ErrorIsNil)
   106  	c.Assert(w, gc.NotNil)
   107  	c.Assert(called, jc.IsTrue)
   108  }
   109  
   110  func (s *CloudSpecSuite) TestCloudSpecOverallError(c *gc.C) {
   111  	expect := errors.New("bewm")
   112  	facadeCaller := apitesting.StubFacadeCaller{Stub: &testing.Stub{}}
   113  	facadeCaller.FacadeCallFn = func(name string, args, response interface{}) error {
   114  		return expect
   115  	}
   116  	api := cloudspec.NewCloudSpecAPI(&facadeCaller, coretesting.ModelTag)
   117  	_, err := api.CloudSpec()
   118  	c.Assert(err, gc.Equals, expect)
   119  }
   120  
   121  func (s *CloudSpecSuite) TestCloudSpecResultCountMismatch(c *gc.C) {
   122  	facadeCaller := apitesting.StubFacadeCaller{Stub: &testing.Stub{}}
   123  	facadeCaller.FacadeCallFn = func(name string, args, response interface{}) error {
   124  		return nil
   125  	}
   126  	api := cloudspec.NewCloudSpecAPI(&facadeCaller, coretesting.ModelTag)
   127  	_, err := api.CloudSpec()
   128  	c.Assert(err, gc.ErrorMatches, "expected 1 result, got 0")
   129  }
   130  
   131  func (s *CloudSpecSuite) TestCloudSpecResultError(c *gc.C) {
   132  	facadeCaller := apitesting.StubFacadeCaller{Stub: &testing.Stub{}}
   133  	facadeCaller.FacadeCallFn = func(name string, args, response interface{}) error {
   134  		*(response.(*params.CloudSpecResults)) = params.CloudSpecResults{
   135  			Results: []params.CloudSpecResult{{
   136  				Error: &params.Error{
   137  					Code:    params.CodeUnauthorized,
   138  					Message: "dang",
   139  				},
   140  			}},
   141  		}
   142  		return nil
   143  	}
   144  	api := cloudspec.NewCloudSpecAPI(&facadeCaller, coretesting.ModelTag)
   145  	_, err := api.CloudSpec()
   146  	c.Assert(err, jc.Satisfies, params.IsCodeUnauthorized)
   147  	c.Assert(err, gc.ErrorMatches, "API request failed: dang")
   148  }
   149  
   150  func (s *CloudSpecSuite) TestCloudSpecInvalidCloudSpec(c *gc.C) {
   151  	facadeCaller := apitesting.StubFacadeCaller{Stub: &testing.Stub{}}
   152  	facadeCaller.FacadeCallFn = func(name string, args, response interface{}) error {
   153  		*(response.(*params.CloudSpecResults)) = params.CloudSpecResults{Results: []params.CloudSpecResult{{
   154  			Result: &params.CloudSpec{
   155  				Type: "",
   156  			},
   157  		}}}
   158  		return nil
   159  	}
   160  	api := cloudspec.NewCloudSpecAPI(&facadeCaller, coretesting.ModelTag)
   161  	_, err := api.CloudSpec()
   162  	c.Assert(err, gc.ErrorMatches, "validating CloudSpec: empty Type not valid")
   163  }