github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/cmd/juju/commands/migrate_test.go (about)

     1  // Copyright 2016 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package commands
     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/api/controller"
    12  	"github.com/juju/juju/cmd/modelcmd"
    13  	"github.com/juju/juju/feature"
    14  	"github.com/juju/juju/jujuclient"
    15  	"github.com/juju/juju/jujuclient/jujuclienttesting"
    16  	"github.com/juju/juju/testing"
    17  )
    18  
    19  type MigrateSuite struct {
    20  	testing.FakeJujuXDGDataHomeSuite
    21  	api   *fakeMigrateAPI
    22  	store *jujuclienttesting.MemStore
    23  }
    24  
    25  var _ = gc.Suite(&MigrateSuite{})
    26  
    27  const modelUUID = "deadbeef-0bad-400d-8000-4b1d0d06f00d"
    28  const targetControllerUUID = "beefdead-0bad-400d-8000-4b1d0d06f00d"
    29  
    30  func (s *MigrateSuite) SetUpTest(c *gc.C) {
    31  	s.SetInitialFeatureFlags(feature.Migration)
    32  	s.FakeJujuXDGDataHomeSuite.SetUpTest(c)
    33  
    34  	s.store = jujuclienttesting.NewMemStore()
    35  
    36  	// Define the source controller in the config and set it as the default.
    37  	err := s.store.UpdateController("source", jujuclient.ControllerDetails{
    38  		ControllerUUID: "eeeeeeee-0bad-400d-8000-4b1d0d06f00d",
    39  		CACert:         "somecert",
    40  	})
    41  	c.Assert(err, jc.ErrorIsNil)
    42  	err = modelcmd.WriteCurrentController("source")
    43  	c.Assert(err, jc.ErrorIsNil)
    44  
    45  	// Define an account for the model in the source controller in the config.
    46  	err = s.store.UpdateAccount("source", "source@local", jujuclient.AccountDetails{
    47  		User: "whatever@local",
    48  	})
    49  	c.Assert(err, jc.ErrorIsNil)
    50  	err = s.store.SetCurrentAccount("source", "source@local")
    51  	c.Assert(err, jc.ErrorIsNil)
    52  
    53  	// Define the model to migrate in the config.
    54  	err = s.store.UpdateModel("source", "source@local", "model", jujuclient.ModelDetails{
    55  		ModelUUID: modelUUID,
    56  	})
    57  	c.Assert(err, jc.ErrorIsNil)
    58  
    59  	// Define the account for the target controller.
    60  	err = s.store.UpdateAccount("target", "target@local", jujuclient.AccountDetails{
    61  		User:     "admin@local",
    62  		Password: "secret",
    63  	})
    64  	c.Assert(err, jc.ErrorIsNil)
    65  	err = s.store.SetCurrentAccount("target", "target@local")
    66  	c.Assert(err, jc.ErrorIsNil)
    67  
    68  	// Define the target controller in the config.
    69  	err = s.store.UpdateController("target", jujuclient.ControllerDetails{
    70  		ControllerUUID: targetControllerUUID,
    71  		APIEndpoints:   []string{"1.2.3.4:5"},
    72  		CACert:         "cert",
    73  	})
    74  	c.Assert(err, jc.ErrorIsNil)
    75  
    76  	s.api = &fakeMigrateAPI{}
    77  }
    78  
    79  func (s *MigrateSuite) TestMissingModel(c *gc.C) {
    80  	_, err := s.runCommand(c)
    81  	c.Assert(err, gc.ErrorMatches, "model not specified")
    82  }
    83  
    84  func (s *MigrateSuite) TestMissingTargetController(c *gc.C) {
    85  	_, err := s.runCommand(c, "mymodel")
    86  	c.Assert(err, gc.ErrorMatches, "target controller not specified")
    87  }
    88  
    89  func (s *MigrateSuite) TestTooManyArgs(c *gc.C) {
    90  	_, err := s.runCommand(c, "one", "too", "many")
    91  	c.Assert(err, gc.ErrorMatches, "too many arguments specified")
    92  }
    93  
    94  func (s *MigrateSuite) TestSuccess(c *gc.C) {
    95  	ctx, err := s.runCommand(c, "model", "target")
    96  	c.Assert(err, jc.ErrorIsNil)
    97  
    98  	c.Check(testing.Stderr(ctx), gc.Matches, "Migration started with ID \"uuid:0\"\n")
    99  	c.Check(s.api.specSeen, jc.DeepEquals, &controller.ModelMigrationSpec{
   100  		ModelUUID:            modelUUID,
   101  		TargetControllerUUID: targetControllerUUID,
   102  		TargetAddrs:          []string{"1.2.3.4:5"},
   103  		TargetCACert:         "cert",
   104  		TargetUser:           "admin@local",
   105  		TargetPassword:       "secret",
   106  	})
   107  }
   108  
   109  func (s *MigrateSuite) TestModelDoesntExist(c *gc.C) {
   110  	_, err := s.runCommand(c, "wat", "target")
   111  	c.Check(err, gc.ErrorMatches, "model .+ not found")
   112  	c.Check(s.api.specSeen, gc.IsNil) // API shouldn't have been called
   113  }
   114  
   115  func (s *MigrateSuite) TestControllerDoesntExist(c *gc.C) {
   116  	_, err := s.runCommand(c, "model", "wat")
   117  	c.Check(err, gc.ErrorMatches, "controller wat not found")
   118  	c.Check(s.api.specSeen, gc.IsNil) // API shouldn't have been called
   119  }
   120  
   121  func (s *MigrateSuite) runCommand(c *gc.C, args ...string) (*cmd.Context, error) {
   122  	cmd := &migrateCommand{
   123  		api: s.api,
   124  	}
   125  	cmd.SetClientStore(s.store)
   126  	return testing.RunCommand(c, modelcmd.WrapController(cmd), args...)
   127  }
   128  
   129  type fakeMigrateAPI struct {
   130  	specSeen *controller.ModelMigrationSpec
   131  }
   132  
   133  func (a *fakeMigrateAPI) InitiateModelMigration(spec controller.ModelMigrationSpec) (string, error) {
   134  	a.specSeen = &spec
   135  	return "uuid:0", nil
   136  }