github.com/wallyworld/juju@v0.0.0-20161013125918-6cf1bc9d917a/api/cloud/cloud_test.go (about)

     1  // Copyright 2016 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package cloud_test
     5  
     6  import (
     7  	gitjujutesting "github.com/juju/testing"
     8  	jc "github.com/juju/testing/checkers"
     9  	gc "gopkg.in/check.v1"
    10  	"gopkg.in/juju/names.v2"
    11  
    12  	basetesting "github.com/juju/juju/api/base/testing"
    13  	cloudapi "github.com/juju/juju/api/cloud"
    14  	"github.com/juju/juju/apiserver/params"
    15  	"github.com/juju/juju/cloud"
    16  )
    17  
    18  type cloudSuite struct {
    19  	gitjujutesting.IsolationSuite
    20  }
    21  
    22  var _ = gc.Suite(&cloudSuite{})
    23  
    24  func (s *cloudSuite) TestCloud(c *gc.C) {
    25  	apiCaller := basetesting.APICallerFunc(
    26  		func(objType string,
    27  			version int,
    28  			id, request string,
    29  			a, result interface{},
    30  		) error {
    31  			c.Check(objType, gc.Equals, "Cloud")
    32  			c.Check(id, gc.Equals, "")
    33  			c.Check(request, gc.Equals, "Cloud")
    34  			c.Check(a, jc.DeepEquals, params.Entities{
    35  				Entities: []params.Entity{{Tag: "cloud-foo"}},
    36  			})
    37  			c.Assert(result, gc.FitsTypeOf, &params.CloudResults{})
    38  			results := result.(*params.CloudResults)
    39  			results.Results = append(results.Results, params.CloudResult{
    40  				Cloud: &params.Cloud{
    41  					Type:      "dummy",
    42  					AuthTypes: []string{"empty", "userpass"},
    43  					Regions:   []params.CloudRegion{{Name: "nether", Endpoint: "endpoint"}},
    44  				},
    45  			})
    46  			return nil
    47  		},
    48  	)
    49  
    50  	client := cloudapi.NewClient(apiCaller)
    51  	result, err := client.Cloud(names.NewCloudTag("foo"))
    52  	c.Assert(err, jc.ErrorIsNil)
    53  	c.Assert(result, jc.DeepEquals, cloud.Cloud{
    54  		Type:      "dummy",
    55  		AuthTypes: []cloud.AuthType{cloud.EmptyAuthType, cloud.UserPassAuthType},
    56  		Regions:   []cloud.Region{{Name: "nether", Endpoint: "endpoint"}},
    57  	})
    58  }
    59  
    60  func (s *cloudSuite) TestClouds(c *gc.C) {
    61  	apiCaller := basetesting.APICallerFunc(
    62  		func(objType string,
    63  			version int,
    64  			id, request string,
    65  			a, result_ interface{},
    66  		) error {
    67  			c.Check(objType, gc.Equals, "Cloud")
    68  			c.Check(id, gc.Equals, "")
    69  			c.Check(request, gc.Equals, "Clouds")
    70  			c.Check(a, gc.IsNil)
    71  			c.Assert(result_, gc.FitsTypeOf, &params.CloudsResult{})
    72  			result := result_.(*params.CloudsResult)
    73  			result.Clouds = map[string]params.Cloud{
    74  				"cloud-foo": {
    75  					Type: "bar",
    76  				},
    77  				"cloud-baz": {
    78  					Type:      "qux",
    79  					AuthTypes: []string{"empty", "userpass"},
    80  					Regions:   []params.CloudRegion{{Name: "nether", Endpoint: "endpoint"}},
    81  				},
    82  			}
    83  			return nil
    84  		},
    85  	)
    86  
    87  	client := cloudapi.NewClient(apiCaller)
    88  	clouds, err := client.Clouds()
    89  	c.Assert(err, jc.ErrorIsNil)
    90  	c.Assert(clouds, jc.DeepEquals, map[names.CloudTag]cloud.Cloud{
    91  		names.NewCloudTag("foo"): {
    92  			Type: "bar",
    93  		},
    94  		names.NewCloudTag("baz"): {
    95  			Type:      "qux",
    96  			AuthTypes: []cloud.AuthType{cloud.EmptyAuthType, cloud.UserPassAuthType},
    97  			Regions:   []cloud.Region{{Name: "nether", Endpoint: "endpoint"}},
    98  		},
    99  	})
   100  }
   101  
   102  func (s *cloudSuite) TestDefaultCloud(c *gc.C) {
   103  	apiCaller := basetesting.APICallerFunc(
   104  		func(objType string,
   105  			version int,
   106  			id, request string,
   107  			a, result interface{},
   108  		) error {
   109  			c.Check(objType, gc.Equals, "Cloud")
   110  			c.Check(id, gc.Equals, "")
   111  			c.Check(request, gc.Equals, "DefaultCloud")
   112  			c.Assert(result, gc.FitsTypeOf, &params.StringResult{})
   113  			results := result.(*params.StringResult)
   114  			results.Result = "cloud-foo"
   115  			return nil
   116  		},
   117  	)
   118  
   119  	client := cloudapi.NewClient(apiCaller)
   120  	result, err := client.DefaultCloud()
   121  	c.Assert(err, jc.ErrorIsNil)
   122  	c.Assert(result, jc.DeepEquals, names.NewCloudTag("foo"))
   123  }
   124  
   125  func (s *cloudSuite) TestUserCredentials(c *gc.C) {
   126  	apiCaller := basetesting.APICallerFunc(
   127  		func(objType string,
   128  			version int,
   129  			id, request string,
   130  			a, result interface{},
   131  		) error {
   132  			c.Check(objType, gc.Equals, "Cloud")
   133  			c.Check(id, gc.Equals, "")
   134  			c.Check(request, gc.Equals, "UserCredentials")
   135  			c.Assert(result, gc.FitsTypeOf, &params.StringsResults{})
   136  			c.Assert(a, jc.DeepEquals, params.UserClouds{UserClouds: []params.UserCloud{{
   137  				UserTag:  "user-bob",
   138  				CloudTag: "cloud-foo",
   139  			}}})
   140  			*result.(*params.StringsResults) = params.StringsResults{
   141  				Results: []params.StringsResult{{
   142  					Result: []string{
   143  						"cloudcred-foo_bob_one",
   144  						"cloudcred-foo_bob_two",
   145  					},
   146  				}},
   147  			}
   148  			return nil
   149  		},
   150  	)
   151  
   152  	client := cloudapi.NewClient(apiCaller)
   153  	result, err := client.UserCredentials(names.NewUserTag("bob"), names.NewCloudTag("foo"))
   154  	c.Assert(err, jc.ErrorIsNil)
   155  	c.Assert(result, jc.SameContents, []names.CloudCredentialTag{
   156  		names.NewCloudCredentialTag("foo/bob/one"),
   157  		names.NewCloudCredentialTag("foo/bob/two"),
   158  	})
   159  }
   160  
   161  func (s *cloudSuite) TestUpdateCredentials(c *gc.C) {
   162  	var called bool
   163  	apiCaller := basetesting.APICallerFunc(
   164  		func(objType string,
   165  			version int,
   166  			id, request string,
   167  			a, result interface{},
   168  		) error {
   169  			c.Check(objType, gc.Equals, "Cloud")
   170  			c.Check(id, gc.Equals, "")
   171  			c.Check(request, gc.Equals, "UpdateCredentials")
   172  			c.Assert(result, gc.FitsTypeOf, &params.ErrorResults{})
   173  			c.Assert(a, jc.DeepEquals, params.UpdateCloudCredentials{Credentials: []params.UpdateCloudCredential{{
   174  				Tag: "cloudcred-foo_bob_bar",
   175  				Credential: params.CloudCredential{
   176  					AuthType: "userpass",
   177  					Attributes: map[string]string{
   178  						"username": "admin",
   179  						"password": "adm1n",
   180  					},
   181  				},
   182  			}}})
   183  			*result.(*params.ErrorResults) = params.ErrorResults{
   184  				Results: []params.ErrorResult{{}},
   185  			}
   186  			called = true
   187  			return nil
   188  		},
   189  	)
   190  
   191  	client := cloudapi.NewClient(apiCaller)
   192  	tag := names.NewCloudCredentialTag("foo/bob/bar")
   193  	err := client.UpdateCredential(tag, cloud.NewCredential(cloud.UserPassAuthType, map[string]string{
   194  		"username": "admin",
   195  		"password": "adm1n",
   196  	}))
   197  	c.Assert(err, jc.ErrorIsNil)
   198  	c.Assert(called, jc.IsTrue)
   199  }
   200  
   201  func (s *cloudSuite) TestRevokeCredential(c *gc.C) {
   202  	var called bool
   203  	apiCaller := basetesting.APICallerFunc(
   204  		func(objType string,
   205  			version int,
   206  			id, request string,
   207  			a, result interface{},
   208  		) error {
   209  			c.Check(objType, gc.Equals, "Cloud")
   210  			c.Check(id, gc.Equals, "")
   211  			c.Check(request, gc.Equals, "RevokeCredentials")
   212  			c.Assert(result, gc.FitsTypeOf, &params.ErrorResults{})
   213  			c.Assert(a, jc.DeepEquals, params.Entities{Entities: []params.Entity{{
   214  				Tag: "cloudcred-foo_bob_bar",
   215  			}}})
   216  			*result.(*params.ErrorResults) = params.ErrorResults{
   217  				Results: []params.ErrorResult{{}},
   218  			}
   219  			called = true
   220  			return nil
   221  		},
   222  	)
   223  
   224  	client := cloudapi.NewClient(apiCaller)
   225  	tag := names.NewCloudCredentialTag("foo/bob/bar")
   226  	err := client.RevokeCredential(tag)
   227  	c.Assert(err, jc.ErrorIsNil)
   228  	c.Assert(called, jc.IsTrue)
   229  }
   230  
   231  func (s *cloudSuite) TestCredentials(c *gc.C) {
   232  	apiCaller := basetesting.APICallerFunc(
   233  		func(objType string,
   234  			version int,
   235  			id, request string,
   236  			a, result interface{},
   237  		) error {
   238  			c.Check(objType, gc.Equals, "Cloud")
   239  			c.Check(id, gc.Equals, "")
   240  			c.Check(request, gc.Equals, "Credential")
   241  			c.Assert(result, gc.FitsTypeOf, &params.CloudCredentialResults{})
   242  			c.Assert(a, jc.DeepEquals, params.Entities{Entities: []params.Entity{{
   243  				Tag: "cloudcred-foo_bob_bar",
   244  			}}})
   245  			*result.(*params.CloudCredentialResults) = params.CloudCredentialResults{
   246  				Results: []params.CloudCredentialResult{
   247  					{
   248  						Result: &params.CloudCredential{
   249  							AuthType:   "userpass",
   250  							Attributes: map[string]string{"username": "fred"},
   251  							Redacted:   []string{"password"},
   252  						},
   253  					}, {
   254  						Result: &params.CloudCredential{
   255  							AuthType:   "userpass",
   256  							Attributes: map[string]string{"username": "mary"},
   257  							Redacted:   []string{"password"},
   258  						},
   259  					},
   260  				},
   261  			}
   262  			return nil
   263  		},
   264  	)
   265  
   266  	client := cloudapi.NewClient(apiCaller)
   267  	tag := names.NewCloudCredentialTag("foo/bob/bar")
   268  	result, err := client.Credentials(tag)
   269  	c.Assert(err, jc.ErrorIsNil)
   270  	c.Assert(result, jc.DeepEquals, []params.CloudCredentialResult{
   271  		{
   272  			Result: &params.CloudCredential{
   273  				AuthType:   "userpass",
   274  				Attributes: map[string]string{"username": "fred"},
   275  				Redacted:   []string{"password"},
   276  			},
   277  		}, {
   278  			Result: &params.CloudCredential{
   279  				AuthType:   "userpass",
   280  				Attributes: map[string]string{"username": "mary"},
   281  				Redacted:   []string{"password"},
   282  			},
   283  		},
   284  	})
   285  }