github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/api/caasoperatorprovisioner/client_test.go (about)

     1  // Copyright 2017 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package caasoperatorprovisioner_test
     5  
     6  import (
     7  	"github.com/juju/errors"
     8  	"github.com/juju/testing"
     9  	jc "github.com/juju/testing/checkers"
    10  	"github.com/juju/version"
    11  	gc "gopkg.in/check.v1"
    12  	"gopkg.in/juju/names.v2"
    13  
    14  	basetesting "github.com/juju/juju/api/base/testing"
    15  	"github.com/juju/juju/api/caasoperatorprovisioner"
    16  	"github.com/juju/juju/apiserver/params"
    17  	"github.com/juju/juju/core/life"
    18  	"github.com/juju/juju/storage"
    19  )
    20  
    21  type provisionerSuite struct {
    22  	testing.IsolationSuite
    23  }
    24  
    25  var _ = gc.Suite(&provisionerSuite{})
    26  
    27  func newClient(f basetesting.APICallerFunc) *caasoperatorprovisioner.Client {
    28  	return caasoperatorprovisioner.NewClient(basetesting.BestVersionCaller{f, 5})
    29  }
    30  
    31  func (s *provisionerSuite) TestWatchApplications(c *gc.C) {
    32  	var called bool
    33  	client := newClient(func(objType string, version int, id, request string, a, result interface{}) error {
    34  		called = true
    35  		c.Check(objType, gc.Equals, "CAASOperatorProvisioner")
    36  		c.Check(id, gc.Equals, "")
    37  		c.Assert(request, gc.Equals, "WatchApplications")
    38  		c.Assert(a, gc.IsNil)
    39  		c.Assert(result, gc.FitsTypeOf, &params.StringsWatchResult{})
    40  		*(result.(*params.StringsWatchResult)) = params.StringsWatchResult{
    41  			Error: &params.Error{Message: "FAIL"},
    42  		}
    43  		return nil
    44  	})
    45  	_, err := client.WatchApplications()
    46  	c.Check(err, gc.ErrorMatches, "FAIL")
    47  	c.Check(called, jc.IsTrue)
    48  }
    49  
    50  func (s *provisionerSuite) TestSetPasswords(c *gc.C) {
    51  	passwords := []caasoperatorprovisioner.ApplicationPassword{
    52  		{Name: "app", Password: "secret"},
    53  	}
    54  	var called bool
    55  	client := newClient(func(objType string, version int, id, request string, a, result interface{}) error {
    56  		called = true
    57  		c.Check(objType, gc.Equals, "CAASOperatorProvisioner")
    58  		c.Check(id, gc.Equals, "")
    59  		c.Assert(request, gc.Equals, "SetPasswords")
    60  		c.Assert(a, jc.DeepEquals, params.EntityPasswords{
    61  			Changes: []params.EntityPassword{{Tag: "application-app", Password: "secret"}},
    62  		})
    63  		c.Assert(result, gc.FitsTypeOf, &params.ErrorResults{})
    64  		*(result.(*params.ErrorResults)) = params.ErrorResults{
    65  			Results: []params.ErrorResult{{}},
    66  		}
    67  		return nil
    68  	})
    69  	result, err := client.SetPasswords(passwords)
    70  	c.Check(err, jc.ErrorIsNil)
    71  	c.Check(result.Combine(), jc.ErrorIsNil)
    72  	c.Check(called, jc.IsTrue)
    73  }
    74  
    75  func (s *provisionerSuite) TestSetPasswordsCount(c *gc.C) {
    76  	client := newClient(func(objType string, version int, id, request string, a, result interface{}) error {
    77  		*(result.(*params.ErrorResults)) = params.ErrorResults{
    78  			Results: []params.ErrorResult{
    79  				{Error: &params.Error{Message: "FAIL"}},
    80  				{Error: &params.Error{Message: "FAIL"}},
    81  			},
    82  		}
    83  		return nil
    84  	})
    85  	passwords := []caasoperatorprovisioner.ApplicationPassword{
    86  		{Name: "app", Password: "secret"},
    87  	}
    88  	_, err := client.SetPasswords(passwords)
    89  	c.Check(err, gc.ErrorMatches, `expected 1 result\(s\), got 2`)
    90  }
    91  
    92  func (s *provisionerSuite) TestLife(c *gc.C) {
    93  	tag := names.NewApplicationTag("app")
    94  	apiCaller := basetesting.APICallerFunc(func(objType string, version int, id, request string, arg, result interface{}) error {
    95  		c.Check(objType, gc.Equals, "CAASOperatorProvisioner")
    96  		c.Check(version, gc.Equals, 0)
    97  		c.Check(id, gc.Equals, "")
    98  		c.Check(request, gc.Equals, "Life")
    99  		c.Check(arg, jc.DeepEquals, params.Entities{
   100  			Entities: []params.Entity{{
   101  				Tag: tag.String(),
   102  			}},
   103  		})
   104  		c.Assert(result, gc.FitsTypeOf, &params.LifeResults{})
   105  		*(result.(*params.LifeResults)) = params.LifeResults{
   106  			Results: []params.LifeResult{{
   107  				Life: params.Alive,
   108  			}},
   109  		}
   110  		return nil
   111  	})
   112  
   113  	client := caasoperatorprovisioner.NewClient(apiCaller)
   114  	lifeValue, err := client.Life(tag.Id())
   115  	c.Assert(err, jc.ErrorIsNil)
   116  	c.Assert(lifeValue, gc.Equals, life.Alive)
   117  }
   118  
   119  func (s *provisionerSuite) TestLifeError(c *gc.C) {
   120  	apiCaller := basetesting.APICallerFunc(func(objType string, version int, id, request string, arg, result interface{}) error {
   121  		*(result.(*params.LifeResults)) = params.LifeResults{
   122  			Results: []params.LifeResult{{Error: &params.Error{
   123  				Code:    params.CodeNotFound,
   124  				Message: "bletch",
   125  			}}},
   126  		}
   127  		return nil
   128  	})
   129  
   130  	client := caasoperatorprovisioner.NewClient(apiCaller)
   131  	_, err := client.Life("gitlab")
   132  	c.Assert(err, gc.ErrorMatches, "bletch")
   133  	c.Assert(err, jc.Satisfies, errors.IsNotFound)
   134  }
   135  
   136  func (s *provisionerSuite) TestLifeInvalidApplicationName(c *gc.C) {
   137  	client := caasoperatorprovisioner.NewClient(basetesting.APICallerFunc(func(_ string, _ int, _, _ string, _, _ interface{}) error {
   138  		return errors.New("should not be called")
   139  	}))
   140  	_, err := client.Life("")
   141  	c.Assert(err, gc.ErrorMatches, `application name "" not valid`)
   142  }
   143  
   144  func (s *provisionerSuite) TestLifeCount(c *gc.C) {
   145  	client := newClient(func(objType string, version int, id, request string, a, result interface{}) error {
   146  		*(result.(*params.LifeResults)) = params.LifeResults{
   147  			Results: []params.LifeResult{
   148  				{Error: &params.Error{Message: "FAIL"}},
   149  				{Error: &params.Error{Message: "FAIL"}},
   150  			},
   151  		}
   152  		return nil
   153  	})
   154  	_, err := client.Life("gitlab")
   155  	c.Check(err, gc.ErrorMatches, `expected 1 result, got 2`)
   156  }
   157  
   158  func (s *provisionerSuite) OperatorProvisioningInfo(c *gc.C) {
   159  	vers := version.MustParse("2.99.0")
   160  	client := newClient(func(objType string, version int, id, request string, a, result interface{}) error {
   161  		c.Check(objType, gc.Equals, "CAASOperatorProvisioner")
   162  		c.Check(id, gc.Equals, "")
   163  		c.Assert(request, gc.Equals, "OperatorProvisioningInfo")
   164  		c.Assert(a, gc.IsNil)
   165  		c.Assert(result, gc.FitsTypeOf, &params.OperatorProvisioningInfo{})
   166  		*(result.(*params.OperatorProvisioningInfo)) = params.OperatorProvisioningInfo{
   167  			ImagePath:    "juju-operator-image",
   168  			Version:      vers,
   169  			APIAddresses: []string{"10.0.0.1:1"},
   170  			Tags:         map[string]string{"foo": "bar"},
   171  			CharmStorage: params.KubernetesFilesystemParams{
   172  				Size:        10,
   173  				Provider:    "kubernetes",
   174  				StorageName: "stor",
   175  				Tags:        map[string]string{"model": "model-tag"},
   176  				Attributes:  map[string]interface{}{"key": "value"},
   177  			},
   178  		}
   179  		return nil
   180  	})
   181  	info, err := client.OperatorProvisioningInfo()
   182  	c.Assert(err, jc.ErrorIsNil)
   183  	c.Assert(info, jc.DeepEquals, caasoperatorprovisioner.OperatorProvisioningInfo{
   184  		ImagePath:    "juju-operator-image",
   185  		Version:      vers,
   186  		APIAddresses: []string{"10.0.0.1:1"},
   187  		Tags:         map[string]string{"foo": "bar"},
   188  		CharmStorage: storage.KubernetesFilesystemParams{
   189  			Size:         10,
   190  			Provider:     "kubernetes",
   191  			StorageName:  "stor",
   192  			ResourceTags: map[string]string{"model": "model-tag"},
   193  			Attributes:   map[string]interface{}{"key": "value"},
   194  		},
   195  	})
   196  }