github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/cmd/juju/storage/import_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  	"errors"
     8  
     9  	"github.com/juju/cmd"
    10  	"github.com/juju/cmd/cmdtesting"
    11  	"github.com/juju/testing"
    12  	jc "github.com/juju/testing/checkers"
    13  	gc "gopkg.in/check.v1"
    14  	"gopkg.in/juju/names.v2"
    15  
    16  	"github.com/juju/juju/cmd/juju/storage"
    17  	_ "github.com/juju/juju/provider/dummy"
    18  	jujustorage "github.com/juju/juju/storage"
    19  )
    20  
    21  type ImportFilesystemSuite struct {
    22  	SubStorageSuite
    23  	importer mockStorageImporter
    24  }
    25  
    26  var _ = gc.Suite(&ImportFilesystemSuite{})
    27  
    28  func (s *ImportFilesystemSuite) SetUpTest(c *gc.C) {
    29  	s.SubStorageSuite.SetUpTest(c)
    30  	s.importer = mockStorageImporter{}
    31  }
    32  
    33  var initErrorTests = []struct {
    34  	args        []string
    35  	expectedErr string
    36  }{{
    37  	args:        []string{"foo", "bar"},
    38  	expectedErr: "import-filesystem requires a storage provider, provider ID, and storage name",
    39  }, {
    40  	args:        []string{"123", "foo", "bar"},
    41  	expectedErr: `pool name "123" not valid`,
    42  }, {
    43  	args:        []string{"foo", "abc123", "123"},
    44  	expectedErr: `"123" is not a valid storage name`,
    45  }}
    46  
    47  func (s *ImportFilesystemSuite) TestInitErrors(c *gc.C) {
    48  	for i, t := range initErrorTests {
    49  		c.Logf("test %d for %q", i, t.args)
    50  		_, err := s.run(c, t.args...)
    51  		c.Assert(err, gc.ErrorMatches, t.expectedErr)
    52  	}
    53  }
    54  
    55  func (s *ImportFilesystemSuite) TestImportSuccess(c *gc.C) {
    56  	ctx, err := s.run(c, "foo", "bar", "baz")
    57  	c.Assert(err, jc.ErrorIsNil)
    58  
    59  	c.Assert(cmdtesting.Stdout(ctx), gc.Equals, "")
    60  	c.Assert(cmdtesting.Stderr(ctx), gc.Equals, `
    61  importing "bar" from storage pool "foo" as storage "baz"
    62  imported storage baz/0
    63  `[1:])
    64  
    65  	s.importer.CheckCalls(c, []testing.StubCall{
    66  		{"ImportStorage", []interface{}{
    67  			jujustorage.StorageKindFilesystem,
    68  			"foo", "bar", "baz",
    69  		}},
    70  		{"Close", nil},
    71  	})
    72  }
    73  
    74  func (s *ImportFilesystemSuite) TestImportError(c *gc.C) {
    75  	s.importer.SetErrors(errors.New("nope"))
    76  
    77  	ctx, err := s.run(c, "foo", "bar", "baz")
    78  	c.Assert(err, gc.ErrorMatches, "nope")
    79  
    80  	c.Assert(cmdtesting.Stdout(ctx), gc.Equals, "")
    81  	c.Assert(cmdtesting.Stderr(ctx), gc.Equals, `importing "bar" from storage pool "foo" as storage "baz"`+"\n")
    82  }
    83  
    84  func (s *ImportFilesystemSuite) run(c *gc.C, args ...string) (*cmd.Context, error) {
    85  	return cmdtesting.RunCommand(c, storage.NewImportFilesystemCommand(
    86  		func(*storage.StorageCommandBase) (storage.StorageImporter, error) {
    87  			return &s.importer, nil
    88  		},
    89  		s.store,
    90  	), args...)
    91  }
    92  
    93  type mockStorageImporter struct {
    94  	testing.Stub
    95  }
    96  
    97  func (m *mockStorageImporter) Close() error {
    98  	m.MethodCall(m, "Close")
    99  	return m.NextErr()
   100  }
   101  
   102  func (m *mockStorageImporter) ImportStorage(
   103  	k jujustorage.StorageKind,
   104  	pool, providerId, storageName string,
   105  ) (names.StorageTag, error) {
   106  	m.MethodCall(m, "ImportStorage", k, pool, providerId, storageName)
   107  	return names.NewStorageTag(storageName + "/0"), m.NextErr()
   108  }