github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/cmd/juju/application/scaleapplication_test.go (about)

     1  // Copyright 2018 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package application
     5  
     6  import (
     7  	"strings"
     8  
     9  	"github.com/juju/cmd"
    10  	"github.com/juju/cmd/cmdtesting"
    11  	"github.com/juju/juju/core/model"
    12  	"github.com/juju/juju/jujuclient"
    13  	"github.com/juju/juju/jujuclient/jujuclienttesting"
    14  	"github.com/juju/testing"
    15  	jc "github.com/juju/testing/checkers"
    16  	gc "gopkg.in/check.v1"
    17  
    18  	"github.com/juju/juju/api/application"
    19  	"github.com/juju/juju/apiserver/params"
    20  )
    21  
    22  type ScaleApplicationSuite struct {
    23  	testing.IsolationSuite
    24  
    25  	mockAPI *mockScaleApplicationAPI
    26  }
    27  
    28  var _ = gc.Suite(&ScaleApplicationSuite{})
    29  
    30  type mockScaleApplicationAPI struct {
    31  	*testing.Stub
    32  	version int
    33  	err     error
    34  }
    35  
    36  func (s mockScaleApplicationAPI) Close() error {
    37  	s.MethodCall(s, "Close")
    38  	return s.NextErr()
    39  }
    40  
    41  func (s mockScaleApplicationAPI) ScaleApplication(args application.ScaleApplicationParams) (params.ScaleApplicationResult, error) {
    42  	s.MethodCall(s, "ScaleApplication", args)
    43  	return params.ScaleApplicationResult{Info: &params.ScaleApplicationInfo{Scale: args.Scale}}, s.NextErr()
    44  }
    45  
    46  func (s mockScaleApplicationAPI) BestAPIVersion() int {
    47  	return s.version
    48  }
    49  
    50  func (s *ScaleApplicationSuite) SetUpTest(c *gc.C) {
    51  	s.IsolationSuite.SetUpTest(c)
    52  	s.mockAPI = &mockScaleApplicationAPI{Stub: &testing.Stub{}, version: 8}
    53  }
    54  
    55  func (s *ScaleApplicationSuite) runScaleApplication(c *gc.C, args ...string) (*cmd.Context, error) {
    56  	store := jujuclienttesting.MinimalStore()
    57  	store.Models["arthur"] = &jujuclient.ControllerModels{
    58  		CurrentModel: "king/sword",
    59  		Models: map[string]jujuclient.ModelDetails{"king/sword": {
    60  			ModelType: model.CAAS,
    61  		}},
    62  	}
    63  	return cmdtesting.RunCommand(c, NewScaleCommandForTest(s.mockAPI, store), args...)
    64  }
    65  
    66  func (s *ScaleApplicationSuite) TestScaleApplication(c *gc.C) {
    67  	ctx, err := s.runScaleApplication(c, "foo", "2")
    68  	c.Assert(err, jc.ErrorIsNil)
    69  
    70  	stderr := cmdtesting.Stderr(ctx)
    71  	out := strings.Replace(stderr, "\n", "", -1)
    72  	c.Assert(out, gc.Equals, `foo scaled to 2 units`)
    73  }
    74  
    75  func (s *ScaleApplicationSuite) TestScaleApplicationWrongModel(c *gc.C) {
    76  	store := jujuclienttesting.MinimalStore()
    77  	_, err := cmdtesting.RunCommand(c, NewScaleCommandForTest(s.mockAPI, store), "foo", "2")
    78  	c.Assert(err, gc.ErrorMatches, `Juju command "scale-application" not supported on non-container models`)
    79  }
    80  
    81  func (s *ScaleApplicationSuite) TestInvalidArgs(c *gc.C) {
    82  	_, err := s.runScaleApplication(c)
    83  	c.Assert(err, gc.ErrorMatches, `no application specified`)
    84  	_, err = s.runScaleApplication(c, "invalid:name")
    85  	c.Assert(err, gc.ErrorMatches, `invalid application name "invalid:name"`)
    86  	_, err = s.runScaleApplication(c, "name")
    87  	c.Assert(err, gc.ErrorMatches, `no scale specified`)
    88  	_, err = s.runScaleApplication(c, "name", "scale")
    89  	c.Assert(err, gc.ErrorMatches, `invalid scale "scale": strconv.Atoi: parsing "scale": invalid syntax`)
    90  }
    91  
    92  func (s *ScaleApplicationSuite) TestOldServer(c *gc.C) {
    93  	s.mockAPI.version = 4
    94  	_, err := s.runScaleApplication(c, "foo", "2")
    95  	c.Assert(err, gc.ErrorMatches, "scaling applications is not supported by this controller")
    96  	s.mockAPI.CheckCall(c, 0, "Close")
    97  }