github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/apiserver/migrationtarget/migrationtarget_test.go (about)

     1  // Copyright 2016 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package migrationtarget_test
     5  
     6  import (
     7  	"github.com/juju/errors"
     8  	"github.com/juju/names"
     9  	jc "github.com/juju/testing/checkers"
    10  	"github.com/juju/utils"
    11  	gc "gopkg.in/check.v1"
    12  
    13  	"github.com/juju/juju/apiserver/common"
    14  	"github.com/juju/juju/apiserver/migrationtarget"
    15  	"github.com/juju/juju/apiserver/params"
    16  	apiservertesting "github.com/juju/juju/apiserver/testing"
    17  	"github.com/juju/juju/cmd/modelcmd"
    18  	"github.com/juju/juju/core/description"
    19  	"github.com/juju/juju/environs"
    20  	"github.com/juju/juju/jujuclient/jujuclienttesting"
    21  	"github.com/juju/juju/provider/dummy"
    22  	"github.com/juju/juju/state"
    23  	statetesting "github.com/juju/juju/state/testing"
    24  	"github.com/juju/juju/testing"
    25  )
    26  
    27  type Suite struct {
    28  	statetesting.StateSuite
    29  	resources  *common.Resources
    30  	authorizer apiservertesting.FakeAuthorizer
    31  }
    32  
    33  var _ = gc.Suite(&Suite{})
    34  
    35  func (s *Suite) SetUpTest(c *gc.C) {
    36  	// Set up InitialConfig with a dummy provider configuration. This
    37  	// is required to allow model import test to work.
    38  	env, err := environs.Prepare(
    39  		modelcmd.BootstrapContext(testing.Context(c)),
    40  		jujuclienttesting.NewMemStore(),
    41  		environs.PrepareParams{
    42  			ControllerName: "dummycontroller",
    43  			BaseConfig:     dummy.SampleConfig(),
    44  			CloudName:      "dummy",
    45  		},
    46  	)
    47  	c.Assert(err, jc.ErrorIsNil)
    48  	s.InitialConfig = testing.CustomModelConfig(c, env.Config().AllAttrs())
    49  
    50  	// The call up to StateSuite's SetUpTest uses s.InitialConfig so
    51  	// it has to happen here.
    52  	s.StateSuite.SetUpTest(c)
    53  
    54  	s.resources = common.NewResources()
    55  	s.AddCleanup(func(*gc.C) { s.resources.StopAll() })
    56  
    57  	s.authorizer = apiservertesting.FakeAuthorizer{
    58  		Tag: s.Owner,
    59  	}
    60  }
    61  
    62  func (s *Suite) TestFacadeRegistered(c *gc.C) {
    63  	factory, err := common.Facades.GetFactory("MigrationTarget", 1)
    64  	c.Assert(err, jc.ErrorIsNil)
    65  
    66  	api, err := factory(s.State, s.resources, s.authorizer, "")
    67  	c.Assert(err, jc.ErrorIsNil)
    68  	c.Assert(api, gc.FitsTypeOf, new(migrationtarget.API))
    69  }
    70  
    71  func (s *Suite) TestNotUser(c *gc.C) {
    72  	s.authorizer.Tag = names.NewMachineTag("0")
    73  	_, err := s.newAPI()
    74  	c.Assert(errors.Cause(err), gc.Equals, common.ErrPerm)
    75  }
    76  
    77  func (s *Suite) TestNotControllerAdmin(c *gc.C) {
    78  	s.authorizer.Tag = names.NewUserTag("jrandomuser")
    79  	_, err := s.newAPI()
    80  	c.Assert(errors.Cause(err), gc.Equals, common.ErrPerm)
    81  }
    82  
    83  func (s *Suite) importModel(c *gc.C, api *migrationtarget.API) names.ModelTag {
    84  	uuid, bytes := s.makeExportedModel(c)
    85  	err := api.Import(params.SerializedModel{Bytes: bytes})
    86  	c.Assert(err, jc.ErrorIsNil)
    87  	return names.NewModelTag(uuid)
    88  }
    89  
    90  func (s *Suite) TestImport(c *gc.C) {
    91  	api := s.mustNewAPI(c)
    92  	tag := s.importModel(c, api)
    93  	// Check the model was imported.
    94  	model, err := s.State.GetModel(tag)
    95  	c.Assert(err, jc.ErrorIsNil)
    96  	c.Assert(model.Name(), gc.Equals, "some-model")
    97  	c.Assert(model.MigrationMode(), gc.Equals, state.MigrationModeImporting)
    98  }
    99  
   100  func (s *Suite) TestAbort(c *gc.C) {
   101  	api := s.mustNewAPI(c)
   102  	tag := s.importModel(c, api)
   103  
   104  	err := api.Abort(params.ModelArgs{ModelTag: tag.String()})
   105  	c.Assert(err, jc.ErrorIsNil)
   106  
   107  	// The model should no longer exist.
   108  	_, err = s.State.GetModel(tag)
   109  	c.Assert(err, gc.ErrorMatches, `model not found`)
   110  }
   111  
   112  func (s *Suite) TestAbortNotATag(c *gc.C) {
   113  	api := s.mustNewAPI(c)
   114  	err := api.Abort(params.ModelArgs{ModelTag: "not-a-tag"})
   115  	c.Assert(err, gc.ErrorMatches, `"not-a-tag" is not a valid tag`)
   116  }
   117  
   118  func (s *Suite) TestAbortMissingEnv(c *gc.C) {
   119  	api := s.mustNewAPI(c)
   120  	newUUID := utils.MustNewUUID().String()
   121  	err := api.Abort(params.ModelArgs{ModelTag: names.NewModelTag(newUUID).String()})
   122  	c.Assert(err, gc.ErrorMatches, `model not found`)
   123  }
   124  
   125  func (s *Suite) TestAbortNotImportingEnv(c *gc.C) {
   126  	st := s.Factory.MakeModel(c, nil)
   127  	defer st.Close()
   128  	model, err := st.Model()
   129  	c.Assert(err, jc.ErrorIsNil)
   130  
   131  	api := s.mustNewAPI(c)
   132  	err = api.Abort(params.ModelArgs{ModelTag: model.ModelTag().String()})
   133  	c.Assert(err, gc.ErrorMatches, `migration mode for the model is not importing`)
   134  }
   135  
   136  func (s *Suite) TestActivate(c *gc.C) {
   137  	api := s.mustNewAPI(c)
   138  	tag := s.importModel(c, api)
   139  
   140  	err := api.Activate(params.ModelArgs{ModelTag: tag.String()})
   141  	c.Assert(err, jc.ErrorIsNil)
   142  
   143  	// The model should no longer exist.
   144  	model, err := s.State.GetModel(tag)
   145  	c.Assert(err, jc.ErrorIsNil)
   146  	c.Assert(model.MigrationMode(), gc.Equals, state.MigrationModeActive)
   147  }
   148  
   149  func (s *Suite) TestActivateNotATag(c *gc.C) {
   150  	api := s.mustNewAPI(c)
   151  	err := api.Activate(params.ModelArgs{ModelTag: "not-a-tag"})
   152  	c.Assert(err, gc.ErrorMatches, `"not-a-tag" is not a valid tag`)
   153  }
   154  
   155  func (s *Suite) TestActivateMissingEnv(c *gc.C) {
   156  	api := s.mustNewAPI(c)
   157  	newUUID := utils.MustNewUUID().String()
   158  	err := api.Activate(params.ModelArgs{ModelTag: names.NewModelTag(newUUID).String()})
   159  	c.Assert(err, gc.ErrorMatches, `model not found`)
   160  }
   161  
   162  func (s *Suite) TestActivateNotImportingEnv(c *gc.C) {
   163  	st := s.Factory.MakeModel(c, nil)
   164  	defer st.Close()
   165  	model, err := st.Model()
   166  	c.Assert(err, jc.ErrorIsNil)
   167  
   168  	api := s.mustNewAPI(c)
   169  	err = api.Activate(params.ModelArgs{ModelTag: model.ModelTag().String()})
   170  	c.Assert(err, gc.ErrorMatches, `migration mode for the model is not importing`)
   171  }
   172  
   173  func (s *Suite) newAPI() (*migrationtarget.API, error) {
   174  	return migrationtarget.NewAPI(s.State, s.resources, s.authorizer)
   175  }
   176  
   177  func (s *Suite) mustNewAPI(c *gc.C) *migrationtarget.API {
   178  	api, err := s.newAPI()
   179  	c.Assert(err, jc.ErrorIsNil)
   180  	return api
   181  }
   182  
   183  func (s *Suite) makeExportedModel(c *gc.C) (string, []byte) {
   184  	model, err := s.State.Export()
   185  	c.Assert(err, jc.ErrorIsNil)
   186  
   187  	newUUID := utils.MustNewUUID().String()
   188  	model.UpdateConfig(map[string]interface{}{
   189  		"name":    "some-model",
   190  		"uuid":    newUUID,
   191  		"ca-cert": "not really a cert",
   192  	})
   193  
   194  	bytes, err := description.Serialize(model)
   195  	c.Assert(err, jc.ErrorIsNil)
   196  	return newUUID, bytes
   197  }