github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/worker/uniter/runner/jujuc/storage-add_test.go (about)

     1  // Copyright 2015 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package jujuc_test
     5  
     6  import (
     7  	"github.com/juju/cmd"
     8  	"github.com/juju/cmd/cmdtesting"
     9  	jc "github.com/juju/testing/checkers"
    10  	gc "gopkg.in/check.v1"
    11  
    12  	"github.com/juju/juju/worker/uniter/runner/jujuc"
    13  )
    14  
    15  type storageAddSuite struct {
    16  	storageSuite
    17  }
    18  
    19  var _ = gc.Suite(&storageAddSuite{})
    20  
    21  func (s *storageAddSuite) getStorageUnitAddCommand(c *gc.C) cmd.Command {
    22  	hctx, _ := s.ContextSuite.NewHookContext()
    23  	com, err := jujuc.NewCommand(hctx, cmdString("storage-add"))
    24  	c.Assert(err, jc.ErrorIsNil)
    25  	return jujuc.NewJujucCommandWrappedForTest(com)
    26  }
    27  
    28  func (s *storageAddSuite) TestHelp(c *gc.C) {
    29  	com := s.getStorageUnitAddCommand(c)
    30  	ctx := cmdtesting.Context(c)
    31  	code := cmd.Main(com, ctx, []string{"--help"})
    32  	c.Assert(code, gc.Equals, 0)
    33  	help := `
    34  Usage: storage-add <charm storage name>[=count] ...
    35  
    36  Summary:
    37  add storage instances
    38  
    39  Details:
    40  `[1:] +
    41  		jujuc.StorageAddDoc
    42  	s.assertOutput(c, ctx, help, "")
    43  }
    44  
    45  func (s *storageAddSuite) assertOutput(c *gc.C, ctx *cmd.Context, o, e string) {
    46  	c.Assert(bufferString(ctx.Stdout), gc.Equals, o)
    47  	c.Assert(bufferString(ctx.Stderr), gc.Equals, e)
    48  }
    49  
    50  type tstData struct {
    51  	args []string
    52  	code int
    53  	err  string
    54  }
    55  
    56  func (s *storageAddSuite) TestStorageAddInit(c *gc.C) {
    57  	tests := []tstData{
    58  		{[]string{}, 1, "storage add requires a storage directive"},
    59  		{[]string{"data=-676"}, 1, `.*cannot parse count: count must be gre.*`},
    60  		{[]string{"data="}, 1, ".*storage constraints require at least one.*"},
    61  		{[]string{"data=pool"}, 1, `.*only count can be specified for "data".*`},
    62  		{[]string{"data=pool,1M"}, 1, `.*only count can be specified for "data".*`},
    63  		{[]string{"data=1M"}, 1, `.*only count can be specified for "data".*`},
    64  		{[]string{"data=2,1M"}, 1, `.*only count can be specified for "data".*`},
    65  		{[]string{"cache", "data=2,1M"}, 1, `.*only count can be specified for "data".*`},
    66  	}
    67  	for i, t := range tests {
    68  		c.Logf("test %d: %#v", i, t.args)
    69  		com := s.getStorageUnitAddCommand(c)
    70  		cmdtesting.TestInit(c, com, t.args, t.err)
    71  	}
    72  }
    73  
    74  func (s *storageAddSuite) TestAddUnitStorage(c *gc.C) {
    75  	tests := []tstData{
    76  		{[]string{"data=676"}, 0, ""},
    77  		{[]string{"data"}, 0, ``},
    78  	}
    79  
    80  	for i, t := range tests {
    81  		c.Logf("test %d: %#v", i, t.args)
    82  		com := s.getStorageUnitAddCommand(c)
    83  		ctx := cmdtesting.Context(c)
    84  		code := cmd.Main(com, ctx, t.args)
    85  		c.Assert(code, gc.Equals, t.code)
    86  		s.assertOutput(c, ctx, "", t.err)
    87  	}
    88  }