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

     1  // Copyright 2017 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package storage_test
     5  
     6  import (
     7  	"github.com/juju/cmd"
     8  	"github.com/juju/cmd/cmdtesting"
     9  	"github.com/juju/testing"
    10  	jc "github.com/juju/testing/checkers"
    11  	gc "gopkg.in/check.v1"
    12  
    13  	"github.com/juju/juju/apiserver/params"
    14  	"github.com/juju/juju/cmd/juju/storage"
    15  	"github.com/juju/juju/jujuclient/jujuclienttesting"
    16  )
    17  
    18  type AttachStorageSuite struct {
    19  	testing.IsolationSuite
    20  }
    21  
    22  var _ = gc.Suite(&AttachStorageSuite{})
    23  
    24  func (s *AttachStorageSuite) TestAttach(c *gc.C) {
    25  	fake := fakeEntityAttacher{results: []params.ErrorResult{
    26  		{},
    27  		{},
    28  	}}
    29  	cmd := storage.NewAttachStorageCommandForTest(fake.new, jujuclienttesting.MinimalStore())
    30  	ctx, err := cmdtesting.RunCommand(c, cmd, "foo/0", "bar/1", "baz/2")
    31  	c.Assert(err, jc.ErrorIsNil)
    32  	fake.CheckCallNames(c, "NewEntityAttacherCloser", "Attach", "Close")
    33  	fake.CheckCall(c, 1, "Attach", "foo/0", []string{"bar/1", "baz/2"})
    34  	c.Assert(cmdtesting.Stderr(ctx), gc.Equals, `
    35  attaching bar/1 to foo/0
    36  attaching baz/2 to foo/0
    37  `[1:])
    38  }
    39  
    40  func (s *AttachStorageSuite) TestAttachError(c *gc.C) {
    41  	fake := fakeEntityAttacher{results: []params.ErrorResult{
    42  		{Error: &params.Error{Message: "foo"}},
    43  		{Error: &params.Error{Message: "bar"}},
    44  	}}
    45  	attachCmd := storage.NewAttachStorageCommandForTest(fake.new, jujuclienttesting.MinimalStore())
    46  	ctx, err := cmdtesting.RunCommand(c, attachCmd, "baz/0", "qux/1", "quux/2")
    47  	stderr := cmdtesting.Stderr(ctx)
    48  	c.Assert(stderr, gc.Equals, `failed to attach qux/1 to baz/0: foo
    49  failed to attach quux/2 to baz/0: bar
    50  `)
    51  	c.Assert(err, gc.Equals, cmd.ErrSilent)
    52  }
    53  
    54  func (s *AttachStorageSuite) TestAttachUnauthorizedError(c *gc.C) {
    55  	var fake fakeEntityAttacher
    56  	fake.SetErrors(nil, &params.Error{Code: params.CodeUnauthorized, Message: "nope"})
    57  	cmd := storage.NewAttachStorageCommandForTest(fake.new, jujuclienttesting.MinimalStore())
    58  	ctx, err := cmdtesting.RunCommand(c, cmd, "foo/0", "bar/1")
    59  	c.Assert(err, gc.ErrorMatches, "nope")
    60  	c.Assert(cmdtesting.Stderr(ctx), gc.Equals, `
    61  You do not have permission to attach storage.
    62  You may ask an administrator to grant you access with "juju grant".
    63  
    64  `)
    65  }
    66  
    67  func (s *AttachStorageSuite) TestAttachInitErrors(c *gc.C) {
    68  	s.testAttachInitError(c, []string{}, "attach-storage requires a unit ID and at least one storage ID")
    69  	s.testAttachInitError(c, []string{"unit/0"}, "attach-storage requires a unit ID and at least one storage ID")
    70  }
    71  
    72  func (s *AttachStorageSuite) testAttachInitError(c *gc.C, args []string, expect string) {
    73  	cmd := storage.NewAttachStorageCommandForTest(nil, jujuclienttesting.MinimalStore())
    74  	_, err := cmdtesting.RunCommand(c, cmd, args...)
    75  	c.Assert(err, gc.ErrorMatches, expect)
    76  }
    77  
    78  type fakeEntityAttacher struct {
    79  	testing.Stub
    80  	results []params.ErrorResult
    81  }
    82  
    83  func (f *fakeEntityAttacher) new() (storage.EntityAttacherCloser, error) {
    84  	f.MethodCall(f, "NewEntityAttacherCloser")
    85  	return f, f.NextErr()
    86  }
    87  
    88  func (f *fakeEntityAttacher) Close() error {
    89  	f.MethodCall(f, "Close")
    90  	return f.NextErr()
    91  }
    92  
    93  func (f *fakeEntityAttacher) Attach(unitId string, storageIds []string) ([]params.ErrorResult, error) {
    94  	f.MethodCall(f, "Attach", unitId, storageIds)
    95  	return f.results, f.NextErr()
    96  }