github.com/wallyworld/juju@v0.0.0-20161013125918-6cf1bc9d917a/apiserver/migrationminion/migrationminion_test.go (about)

     1  // Copyright 2016 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package migrationminion_test
     5  
     6  import (
     7  	"github.com/juju/errors"
     8  	"github.com/juju/testing"
     9  	jc "github.com/juju/testing/checkers"
    10  	gc "gopkg.in/check.v1"
    11  	"gopkg.in/juju/names.v2"
    12  
    13  	"github.com/juju/juju/apiserver/common"
    14  	"github.com/juju/juju/apiserver/migrationminion"
    15  	"github.com/juju/juju/apiserver/params"
    16  	apiservertesting "github.com/juju/juju/apiserver/testing"
    17  	"github.com/juju/juju/core/migration"
    18  	"github.com/juju/juju/state"
    19  	coretesting "github.com/juju/juju/testing"
    20  )
    21  
    22  // Ensure that Backend remains compatible with *state.State
    23  var _ migrationminion.Backend = (*state.State)(nil)
    24  
    25  type Suite struct {
    26  	coretesting.BaseSuite
    27  
    28  	stub       *testing.Stub
    29  	backend    *stubBackend
    30  	resources  *common.Resources
    31  	authorizer apiservertesting.FakeAuthorizer
    32  }
    33  
    34  var _ = gc.Suite(&Suite{})
    35  
    36  func (s *Suite) SetUpTest(c *gc.C) {
    37  	s.BaseSuite.SetUpTest(c)
    38  
    39  	s.stub = &testing.Stub{}
    40  	s.backend = &stubBackend{stub: s.stub}
    41  
    42  	s.resources = common.NewResources()
    43  	s.AddCleanup(func(*gc.C) { s.resources.StopAll() })
    44  
    45  	s.authorizer = apiservertesting.FakeAuthorizer{
    46  		Tag: names.NewMachineTag("99"),
    47  	}
    48  }
    49  
    50  func (s *Suite) TestAuthMachineAgent(c *gc.C) {
    51  	s.authorizer.Tag = names.NewMachineTag("42")
    52  	s.mustMakeAPI(c)
    53  }
    54  
    55  func (s *Suite) TestAuthUnitAgent(c *gc.C) {
    56  	s.authorizer.Tag = names.NewUnitTag("foo/0")
    57  	s.mustMakeAPI(c)
    58  }
    59  
    60  func (s *Suite) TestAuthNotAgent(c *gc.C) {
    61  	s.authorizer.Tag = names.NewUserTag("dorothy")
    62  	_, err := s.makeAPI()
    63  	c.Assert(err, gc.Equals, common.ErrPerm)
    64  }
    65  
    66  func (s *Suite) TestWatch(c *gc.C) {
    67  	api := s.mustMakeAPI(c)
    68  	result, err := api.Watch()
    69  	c.Assert(err, jc.ErrorIsNil)
    70  	c.Assert(s.resources.Get(result.NotifyWatcherId), gc.NotNil)
    71  }
    72  
    73  func (s *Suite) TestReport(c *gc.C) {
    74  	api := s.mustMakeAPI(c)
    75  	err := api.Report(params.MinionReport{
    76  		MigrationId: "id",
    77  		Phase:       "IMPORT",
    78  		Success:     true,
    79  	})
    80  	c.Assert(err, jc.ErrorIsNil)
    81  	s.stub.CheckCalls(c, []testing.StubCall{
    82  		{"Migration", []interface{}{"id"}},
    83  		{"Report", []interface{}{s.authorizer.Tag, migration.IMPORT, true}},
    84  	})
    85  }
    86  
    87  func (s *Suite) TestReportInvalidPhase(c *gc.C) {
    88  	api := s.mustMakeAPI(c)
    89  	err := api.Report(params.MinionReport{
    90  		MigrationId: "id",
    91  		Phase:       "WTF",
    92  		Success:     true,
    93  	})
    94  	c.Assert(err, gc.ErrorMatches, "unable to parse phase")
    95  }
    96  
    97  func (s *Suite) TestReportNoSuchMigration(c *gc.C) {
    98  	failure := errors.NotFoundf("model")
    99  	s.backend.modelLookupErr = failure
   100  	api := s.mustMakeAPI(c)
   101  	err := api.Report(params.MinionReport{
   102  		MigrationId: "id",
   103  		Phase:       "QUIESCE",
   104  		Success:     false,
   105  	})
   106  	c.Assert(errors.Cause(err), gc.Equals, failure)
   107  }
   108  
   109  func (s *Suite) makeAPI() (*migrationminion.API, error) {
   110  	return migrationminion.NewAPI(s.backend, s.resources, s.authorizer)
   111  }
   112  
   113  func (s *Suite) mustMakeAPI(c *gc.C) *migrationminion.API {
   114  	api, err := s.makeAPI()
   115  	c.Assert(err, jc.ErrorIsNil)
   116  	return api
   117  }
   118  
   119  type stubBackend struct {
   120  	migrationminion.Backend
   121  	stub           *testing.Stub
   122  	modelLookupErr error
   123  }
   124  
   125  func (b *stubBackend) WatchMigrationStatus() state.NotifyWatcher {
   126  	b.stub.AddCall("WatchMigrationStatus")
   127  	return apiservertesting.NewFakeNotifyWatcher()
   128  }
   129  
   130  func (b *stubBackend) Migration(id string) (state.ModelMigration, error) {
   131  	b.stub.AddCall("Migration", id)
   132  	if b.modelLookupErr != nil {
   133  		return nil, b.modelLookupErr
   134  	}
   135  	return &stubModelMigration{stub: b.stub}, nil
   136  }
   137  
   138  type stubModelMigration struct {
   139  	state.ModelMigration
   140  	stub *testing.Stub
   141  }
   142  
   143  func (m *stubModelMigration) SubmitMinionReport(tag names.Tag, phase migration.Phase, success bool) error {
   144  	m.stub.AddCall("Report", tag, phase, success)
   145  	return nil
   146  }