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

     1  // Copyright 2017 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package caasoperator_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/caasoperator"
    16  	"github.com/juju/juju/apiserver/params"
    17  	"github.com/juju/juju/core/life"
    18  	"github.com/juju/juju/core/model"
    19  )
    20  
    21  type operatorSuite struct {
    22  	testing.IsolationSuite
    23  }
    24  
    25  var _ = gc.Suite(&operatorSuite{})
    26  
    27  func (s *operatorSuite) TestSetStatus(c *gc.C) {
    28  	apiCaller := basetesting.APICallerFunc(func(objType string, version int, id, request string, arg, result interface{}) error {
    29  		c.Check(objType, gc.Equals, "CAASOperator")
    30  		c.Check(version, gc.Equals, 0)
    31  		c.Check(id, gc.Equals, "")
    32  		c.Check(request, gc.Equals, "SetStatus")
    33  		c.Check(arg, jc.DeepEquals, params.SetStatus{
    34  			Entities: []params.EntityStatusArgs{{
    35  				Tag:    "application-gitlab",
    36  				Status: "foo",
    37  				Info:   "bar",
    38  				Data: map[string]interface{}{
    39  					"baz": "qux",
    40  				},
    41  			}},
    42  		})
    43  		c.Assert(result, gc.FitsTypeOf, &params.ErrorResults{})
    44  		*(result.(*params.ErrorResults)) = params.ErrorResults{
    45  			Results: []params.ErrorResult{{Error: &params.Error{Message: "bletch"}}},
    46  		}
    47  		return nil
    48  	})
    49  
    50  	client := caasoperator.NewClient(apiCaller)
    51  	err := client.SetStatus("gitlab", "foo", "bar", map[string]interface{}{
    52  		"baz": "qux",
    53  	})
    54  	c.Assert(err, gc.ErrorMatches, "bletch")
    55  }
    56  
    57  func (s *operatorSuite) TestSetStatusInvalidApplicationName(c *gc.C) {
    58  	client := caasoperator.NewClient(basetesting.APICallerFunc(func(_ string, _ int, _, _ string, _, _ interface{}) error {
    59  		return errors.New("should not be called")
    60  	}))
    61  	err := client.SetStatus("", "foo", "bar", nil)
    62  	c.Assert(err, gc.ErrorMatches, `application name "" not valid`)
    63  }
    64  
    65  func (s *operatorSuite) TestCharm(c *gc.C) {
    66  	apiCaller := basetesting.APICallerFunc(func(objType string, version int, id, request string, arg, result interface{}) error {
    67  		c.Check(objType, gc.Equals, "CAASOperator")
    68  		c.Check(version, gc.Equals, 0)
    69  		c.Check(id, gc.Equals, "")
    70  		c.Check(request, gc.Equals, "Charm")
    71  		c.Check(arg, jc.DeepEquals, params.Entities{
    72  			Entities: []params.Entity{{
    73  				Tag: "application-gitlab",
    74  			}},
    75  		})
    76  		c.Assert(result, gc.FitsTypeOf, &params.ApplicationCharmResults{})
    77  		*(result.(*params.ApplicationCharmResults)) = params.ApplicationCharmResults{
    78  			Results: []params.ApplicationCharmResult{{
    79  				Result: &params.ApplicationCharm{
    80  					URL:                  "cs:foo/bar-1",
    81  					ForceUpgrade:         true,
    82  					SHA256:               "fake-sha256",
    83  					CharmModifiedVersion: 666,
    84  				},
    85  			}},
    86  		}
    87  		return nil
    88  	})
    89  
    90  	client := caasoperator.NewClient(apiCaller)
    91  	curl, force, sha256, vers, err := client.Charm("gitlab")
    92  	c.Assert(err, jc.ErrorIsNil)
    93  	c.Assert(curl, gc.NotNil)
    94  	c.Assert(curl.String(), gc.Equals, "cs:foo/bar-1")
    95  	c.Assert(sha256, gc.Equals, "fake-sha256")
    96  	c.Assert(force, jc.IsTrue)
    97  	c.Assert(vers, gc.Equals, 666)
    98  }
    99  
   100  func (s *operatorSuite) TestCharmError(c *gc.C) {
   101  	apiCaller := basetesting.APICallerFunc(func(objType string, version int, id, request string, arg, result interface{}) error {
   102  		*(result.(*params.ApplicationCharmResults)) = params.ApplicationCharmResults{
   103  			Results: []params.ApplicationCharmResult{{Error: &params.Error{Message: "bletch"}}},
   104  		}
   105  		return nil
   106  	})
   107  	client := caasoperator.NewClient(apiCaller)
   108  	_, _, _, _, err := client.Charm("gitlab")
   109  	c.Assert(err, gc.ErrorMatches, "bletch")
   110  }
   111  
   112  func (s *operatorSuite) TestCharmInvalidApplicationName(c *gc.C) {
   113  	client := caasoperator.NewClient(basetesting.APICallerFunc(func(_ string, _ int, _, _ string, _, _ interface{}) error {
   114  		return errors.New("should not be called")
   115  	}))
   116  	_, _, _, _, err := client.Charm("")
   117  	c.Assert(err, gc.ErrorMatches, `application name "" not valid`)
   118  }
   119  
   120  func (s *operatorSuite) TestSetPodSpec(c *gc.C) {
   121  	tag := names.NewApplicationTag("gitlab")
   122  	apiCaller := basetesting.APICallerFunc(func(objType string, version int, id, request string, arg, result interface{}) error {
   123  		c.Check(objType, gc.Equals, "CAASOperator")
   124  		c.Check(version, gc.Equals, 0)
   125  		c.Check(id, gc.Equals, "")
   126  		c.Check(request, gc.Equals, "SetPodSpec")
   127  		c.Check(arg, jc.DeepEquals, params.SetPodSpecParams{
   128  			Specs: []params.EntityString{{
   129  				Tag:   tag.String(),
   130  				Value: "spec",
   131  			}},
   132  		})
   133  		c.Assert(result, gc.FitsTypeOf, &params.ErrorResults{})
   134  		*(result.(*params.ErrorResults)) = params.ErrorResults{
   135  			Results: []params.ErrorResult{{Error: &params.Error{Message: "bletch"}}},
   136  		}
   137  		return nil
   138  	})
   139  
   140  	client := caasoperator.NewClient(apiCaller)
   141  	err := client.SetPodSpec(tag.Id(), "spec")
   142  	c.Assert(err, gc.ErrorMatches, "bletch")
   143  }
   144  
   145  func (s *operatorSuite) TestSetPodSpecInvalidEntityame(c *gc.C) {
   146  	client := caasoperator.NewClient(basetesting.APICallerFunc(func(_ string, _ int, _, _ string, _, _ interface{}) error {
   147  		return errors.New("should not be called")
   148  	}))
   149  	err := client.SetPodSpec("", "spec")
   150  	c.Assert(err, gc.ErrorMatches, `application name "" not valid`)
   151  }
   152  
   153  func (s *operatorSuite) TestModel(c *gc.C) {
   154  	apiCaller := basetesting.APICallerFunc(func(objType string, version int, id, request string, arg, result interface{}) error {
   155  		c.Check(objType, gc.Equals, "CAASOperator")
   156  		c.Check(version, gc.Equals, 0)
   157  		c.Check(id, gc.Equals, "")
   158  		c.Check(request, gc.Equals, "CurrentModel")
   159  		c.Check(arg, gc.IsNil)
   160  		c.Assert(result, gc.FitsTypeOf, &params.ModelResult{})
   161  		*(result.(*params.ModelResult)) = params.ModelResult{
   162  			Name: "some-model",
   163  			UUID: "deadbeef",
   164  			Type: "iaas",
   165  		}
   166  		return nil
   167  	})
   168  
   169  	client := caasoperator.NewClient(apiCaller)
   170  	m, err := client.Model()
   171  	c.Assert(err, jc.ErrorIsNil)
   172  	c.Assert(m, jc.DeepEquals, &model.Model{
   173  		Name:      "some-model",
   174  		UUID:      "deadbeef",
   175  		ModelType: model.IAAS,
   176  	})
   177  }
   178  
   179  func (s *operatorSuite) TestWatchUnits(c *gc.C) {
   180  	apiCaller := basetesting.APICallerFunc(func(objType string, version int, id, request string, arg, result interface{}) error {
   181  		c.Check(objType, gc.Equals, "CAASOperator")
   182  		c.Check(version, gc.Equals, 0)
   183  		c.Check(id, gc.Equals, "")
   184  		c.Check(request, gc.Equals, "WatchUnits")
   185  		c.Assert(arg, jc.DeepEquals, params.Entities{
   186  			Entities: []params.Entity{{
   187  				Tag: "application-gitlab",
   188  			}},
   189  		})
   190  		c.Assert(result, gc.FitsTypeOf, &params.StringsWatchResults{})
   191  		*(result.(*params.StringsWatchResults)) = params.StringsWatchResults{
   192  			Results: []params.StringsWatchResult{{
   193  				Error: &params.Error{Message: "FAIL"},
   194  			}},
   195  		}
   196  		return nil
   197  	})
   198  
   199  	client := caasoperator.NewClient(apiCaller)
   200  	watcher, err := client.WatchUnits("gitlab")
   201  	c.Assert(watcher, gc.IsNil)
   202  	c.Assert(err, gc.ErrorMatches, "FAIL")
   203  }
   204  
   205  func (s *operatorSuite) TestRemoveUnit(c *gc.C) {
   206  	called := false
   207  	apiCaller := basetesting.APICallerFunc(func(objType string, version int, id, request string, arg, result interface{}) error {
   208  		c.Check(objType, gc.Equals, "CAASOperator")
   209  		c.Check(version, gc.Equals, 0)
   210  		c.Check(id, gc.Equals, "")
   211  		c.Check(request, gc.Equals, "Remove")
   212  		c.Check(arg, jc.DeepEquals, params.Entities{
   213  			Entities: []params.Entity{{
   214  				Tag: "unit-gitlab-0",
   215  			}},
   216  		})
   217  		c.Assert(result, gc.FitsTypeOf, &params.ErrorResults{})
   218  		*(result.(*params.ErrorResults)) = params.ErrorResults{
   219  			Results: []params.ErrorResult{{
   220  				Error: &params.Error{Message: "FAIL"},
   221  			}},
   222  		}
   223  		called = true
   224  		return nil
   225  	})
   226  
   227  	client := caasoperator.NewClient(apiCaller)
   228  	err := client.RemoveUnit("gitlab/0")
   229  	c.Assert(err, gc.ErrorMatches, "FAIL")
   230  	c.Assert(called, jc.IsTrue)
   231  }
   232  
   233  func (s *operatorSuite) TestRemoveUnitInvalidUnitName(c *gc.C) {
   234  	client := caasoperator.NewClient(basetesting.APICallerFunc(func(_ string, _ int, _, _ string, _, _ interface{}) error {
   235  		return errors.New("should not be called")
   236  	}))
   237  	err := client.RemoveUnit("")
   238  	c.Assert(err, gc.ErrorMatches, `unit name "" not valid`)
   239  }
   240  
   241  func (s *operatorSuite) TestLife(c *gc.C) {
   242  	s.testLife(c, names.NewApplicationTag("gitlab"))
   243  	s.testLife(c, names.NewUnitTag("gitlab/0"))
   244  }
   245  
   246  func (s *operatorSuite) testLife(c *gc.C, tag names.Tag) {
   247  	apiCaller := basetesting.APICallerFunc(func(objType string, version int, id, request string, arg, result interface{}) error {
   248  		c.Check(objType, gc.Equals, "CAASOperator")
   249  		c.Check(version, gc.Equals, 0)
   250  		c.Check(id, gc.Equals, "")
   251  		c.Check(request, gc.Equals, "Life")
   252  		c.Check(arg, jc.DeepEquals, params.Entities{
   253  			Entities: []params.Entity{{
   254  				Tag: tag.String(),
   255  			}},
   256  		})
   257  		c.Assert(result, gc.FitsTypeOf, &params.LifeResults{})
   258  		*(result.(*params.LifeResults)) = params.LifeResults{
   259  			Results: []params.LifeResult{{
   260  				Life: params.Alive,
   261  			}},
   262  		}
   263  		return nil
   264  	})
   265  
   266  	client := caasoperator.NewClient(apiCaller)
   267  	lifeValue, err := client.Life(tag.Id())
   268  	c.Assert(err, jc.ErrorIsNil)
   269  	c.Assert(lifeValue, gc.Equals, life.Alive)
   270  }
   271  
   272  func (s *operatorSuite) TestLifeError(c *gc.C) {
   273  	apiCaller := basetesting.APICallerFunc(func(objType string, version int, id, request string, arg, result interface{}) error {
   274  		*(result.(*params.LifeResults)) = params.LifeResults{
   275  			Results: []params.LifeResult{{Error: &params.Error{
   276  				Code:    params.CodeNotFound,
   277  				Message: "bletch",
   278  			}}},
   279  		}
   280  		return nil
   281  	})
   282  
   283  	client := caasoperator.NewClient(apiCaller)
   284  	_, err := client.Life("gitlab/0")
   285  	c.Assert(err, gc.ErrorMatches, "bletch")
   286  	c.Assert(err, jc.Satisfies, errors.IsNotFound)
   287  }
   288  
   289  func (s *operatorSuite) TestLifeInvalidEntityName(c *gc.C) {
   290  	client := caasoperator.NewClient(basetesting.APICallerFunc(func(_ string, _ int, _, _ string, _, _ interface{}) error {
   291  		return errors.New("should not be called")
   292  	}))
   293  	_, err := client.Life("")
   294  	c.Assert(err, gc.ErrorMatches, `application or unit name "" not valid`)
   295  }
   296  
   297  func (s *operatorSuite) TestSetVersion(c *gc.C) {
   298  	called := false
   299  	vers := version.Binary{}
   300  	apiCaller := basetesting.APICallerFunc(func(objType string, version int, id, request string, arg, result interface{}) error {
   301  		c.Check(objType, gc.Equals, "CAASOperator")
   302  		c.Check(version, gc.Equals, 0)
   303  		c.Check(id, gc.Equals, "")
   304  		c.Check(request, gc.Equals, "SetTools")
   305  		c.Check(arg, jc.DeepEquals, params.EntitiesVersion{
   306  			AgentTools: []params.EntityVersion{{
   307  				Tag:   "application-gitlab",
   308  				Tools: &params.Version{Version: vers},
   309  			}},
   310  		})
   311  		c.Assert(result, gc.FitsTypeOf, &params.ErrorResults{})
   312  		*(result.(*params.ErrorResults)) = params.ErrorResults{
   313  			Results: []params.ErrorResult{{
   314  				Error: &params.Error{Message: "FAIL"},
   315  			}},
   316  		}
   317  		called = true
   318  		return nil
   319  	})
   320  
   321  	client := caasoperator.NewClient(apiCaller)
   322  	err := client.SetVersion("gitlab", vers)
   323  	c.Assert(err, gc.ErrorMatches, "FAIL")
   324  	c.Assert(called, jc.IsTrue)
   325  }
   326  
   327  func (s *operatorSuite) TestSetVersionInvalidApplicationName(c *gc.C) {
   328  	client := caasoperator.NewClient(basetesting.APICallerFunc(func(_ string, _ int, _, _ string, _, _ interface{}) error {
   329  		return errors.New("should not be called")
   330  	}))
   331  	err := client.SetVersion("", version.Binary{})
   332  	c.Assert(err, gc.ErrorMatches, `application name "" not valid`)
   333  }