github.com/Pankov404/juju@v0.0.0-20150703034450-be266991dceb/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  	jc "github.com/juju/testing/checkers"
     9  	gc "gopkg.in/check.v1"
    10  
    11  	"github.com/juju/juju/testing"
    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) newHookContext() jujuc.Context {
    22  	hctx, _ := s.NewHookContext()
    23  	return hctx
    24  }
    25  
    26  func (s *storageAddSuite) getStorageUnitAddCommand(c *gc.C) cmd.Command {
    27  	hctx := s.newHookContext()
    28  	com, err := jujuc.NewCommand(hctx, cmdString("storage-add"))
    29  	c.Assert(err, jc.ErrorIsNil)
    30  	return com
    31  }
    32  
    33  func (s *storageAddSuite) TestHelp(c *gc.C) {
    34  	com := s.getStorageUnitAddCommand(c)
    35  	ctx := testing.Context(c)
    36  	code := cmd.Main(com, ctx, []string{"--help"})
    37  	c.Assert(code, gc.Equals, 0)
    38  	help := `
    39  usage: storage-add <charm storage name>[=count] ...
    40  purpose: add storage instances
    41  `[1:] +
    42  		jujuc.StorageAddDoc
    43  	s.assertOutput(c, ctx, help, "")
    44  }
    45  
    46  func (s *storageAddSuite) assertOutput(c *gc.C, ctx *cmd.Context, o, e string) {
    47  	c.Assert(bufferString(ctx.Stdout), gc.Equals, o)
    48  	c.Assert(bufferString(ctx.Stderr), gc.Equals, e)
    49  }
    50  
    51  type tstData struct {
    52  	args []string
    53  	code int
    54  	err  string
    55  }
    56  
    57  func (s *storageAddSuite) TestStorageAddInit(c *gc.C) {
    58  	tests := []tstData{
    59  		{[]string{}, 1, "storage add requires a storage directive"},
    60  		{[]string{"data=-676"}, 1, `.*cannot parse count: count must be gre.*`},
    61  		{[]string{"data="}, 1, ".*storage constraints require at least one.*"},
    62  		{[]string{"data=pool"}, 1, `.*only count can be specified for "data".*`},
    63  		{[]string{"data=pool,1M"}, 1, `.*only count can be specified for "data".*`},
    64  		{[]string{"data=1M"}, 1, `.*only count can be specified for "data".*`},
    65  		{[]string{"data=2,1M"}, 1, `.*only count can be specified for "data".*`},
    66  		{[]string{"cache", "data=2,1M"}, 1, `.*only count can be specified for "data".*`},
    67  	}
    68  	for i, t := range tests {
    69  		c.Logf("test %d: %#v", i, t.args)
    70  		com := s.getStorageUnitAddCommand(c)
    71  		testing.TestInit(c, com, t.args, t.err)
    72  	}
    73  }
    74  
    75  func (s *storageAddSuite) TestAddUnitStorage(c *gc.C) {
    76  	tests := []tstData{
    77  		{[]string{"data=676"}, 0, ""},
    78  		{[]string{"data"}, 0, ``},
    79  	}
    80  
    81  	for i, t := range tests {
    82  		c.Logf("test %d: %#v", i, t.args)
    83  		com := s.getStorageUnitAddCommand(c)
    84  		ctx := testing.Context(c)
    85  		code := cmd.Main(com, ctx, t.args)
    86  		c.Assert(code, gc.Equals, t.code)
    87  		s.assertOutput(c, ctx, "", t.err)
    88  	}
    89  }