github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/featuretests/api_undertaker_test.go (about)

     1  // Copyright 2015 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package featuretests
     5  
     6  import (
     7  	"github.com/juju/errors"
     8  	jc "github.com/juju/testing/checkers"
     9  	"github.com/juju/utils"
    10  	gc "gopkg.in/check.v1"
    11  
    12  	"github.com/juju/juju/api"
    13  	"github.com/juju/juju/api/undertaker"
    14  	apiwatcher "github.com/juju/juju/api/watcher"
    15  	"github.com/juju/juju/apiserver/params"
    16  	"github.com/juju/juju/core/watcher/watchertest"
    17  	jujutesting "github.com/juju/juju/juju/testing"
    18  	"github.com/juju/juju/rpc"
    19  	"github.com/juju/juju/state"
    20  	coretesting "github.com/juju/juju/testing"
    21  	"github.com/juju/juju/testing/factory"
    22  	"gopkg.in/juju/names.v2"
    23  )
    24  
    25  // TODO(fwereade) 2016-03-17 lp:1558668
    26  // this is not a feature test; much of it is redundant, and other
    27  // bits should be tested elsewhere.
    28  type undertakerSuite struct {
    29  	jujutesting.JujuConnSuite
    30  }
    31  
    32  func (s *undertakerSuite) TestPermDenied(c *gc.C) {
    33  	nonManagerMachine, _ := s.OpenAPIAsNewMachine(c, state.JobHostUnits)
    34  	for _, conn := range []api.Connection{
    35  		nonManagerMachine,
    36  		s.APIState,
    37  	} {
    38  		undertakerClient, err := undertaker.NewClient(conn, apiwatcher.NewNotifyWatcher)
    39  		c.Assert(err, jc.ErrorIsNil)
    40  		c.Assert(undertakerClient, gc.NotNil)
    41  
    42  		_, err = undertakerClient.ModelInfo()
    43  		c.Assert(errors.Cause(err), gc.DeepEquals, &rpc.RequestError{
    44  			Message: "permission denied",
    45  			Code:    "unauthorized access",
    46  		})
    47  	}
    48  }
    49  
    50  func (s *undertakerSuite) TestStateEnvironInfo(c *gc.C) {
    51  	st, _ := s.OpenAPIAsNewMachine(c, state.JobManageModel)
    52  	undertakerClient, err := undertaker.NewClient(st, apiwatcher.NewNotifyWatcher)
    53  	c.Assert(err, jc.ErrorIsNil)
    54  	c.Assert(undertakerClient, gc.NotNil)
    55  
    56  	result, err := undertakerClient.ModelInfo()
    57  	c.Assert(err, jc.ErrorIsNil)
    58  	c.Assert(result, gc.NotNil)
    59  	c.Assert(result.Error, gc.IsNil)
    60  	info := result.Result
    61  	c.Assert(info.UUID, gc.Equals, coretesting.ModelTag.Id())
    62  	c.Assert(info.Name, gc.Equals, "controller")
    63  	c.Assert(info.GlobalName, gc.Equals, "user-admin/controller")
    64  	c.Assert(info.IsSystem, jc.IsTrue)
    65  	c.Assert(info.Life, gc.Equals, params.Alive)
    66  }
    67  
    68  func (s *undertakerSuite) TestStateProcessDyingEnviron(c *gc.C) {
    69  	st, _ := s.OpenAPIAsNewMachine(c, state.JobManageModel)
    70  	undertakerClient, err := undertaker.NewClient(st, apiwatcher.NewNotifyWatcher)
    71  	c.Assert(err, jc.ErrorIsNil)
    72  	c.Assert(undertakerClient, gc.NotNil)
    73  
    74  	err = undertakerClient.ProcessDyingModel()
    75  	c.Assert(err, gc.ErrorMatches, "model is not dying")
    76  
    77  	model, err := s.State.Model()
    78  	c.Assert(err, jc.ErrorIsNil)
    79  	c.Assert(model.Destroy(state.DestroyModelParams{}), jc.ErrorIsNil)
    80  	c.Assert(model.Refresh(), jc.ErrorIsNil)
    81  	c.Assert(model.Life(), gc.Equals, state.Dying)
    82  
    83  	err = undertakerClient.ProcessDyingModel()
    84  	c.Assert(err, gc.ErrorMatches, `model not empty, found 1 machine \(model not empty\)`)
    85  }
    86  
    87  func (s *undertakerSuite) TestStateRemoveEnvironFails(c *gc.C) {
    88  	st, _ := s.OpenAPIAsNewMachine(c, state.JobManageModel)
    89  	undertakerClient, err := undertaker.NewClient(st, apiwatcher.NewNotifyWatcher)
    90  	c.Assert(err, jc.ErrorIsNil)
    91  	c.Assert(undertakerClient, gc.NotNil)
    92  	c.Assert(undertakerClient.RemoveModel(), gc.ErrorMatches, "can't remove model: model still alive")
    93  }
    94  
    95  func (s *undertakerSuite) TestHostedEnvironInfo(c *gc.C) {
    96  	undertakerClient, otherSt := s.hostedAPI(c)
    97  	defer otherSt.Close()
    98  
    99  	result, err := undertakerClient.ModelInfo()
   100  	c.Assert(err, jc.ErrorIsNil)
   101  	c.Assert(result, gc.NotNil)
   102  	c.Assert(result.Error, gc.IsNil)
   103  	envInfo := result.Result
   104  	c.Assert(envInfo.UUID, gc.Equals, otherSt.ModelUUID())
   105  	c.Assert(envInfo.Name, gc.Equals, "hosted-env")
   106  	c.Assert(envInfo.GlobalName, gc.Equals, "user-admin/hosted-env")
   107  	c.Assert(envInfo.IsSystem, jc.IsFalse)
   108  	c.Assert(envInfo.Life, gc.Equals, params.Alive)
   109  }
   110  
   111  func (s *undertakerSuite) TestHostedProcessDyingEnviron(c *gc.C) {
   112  	undertakerClient, otherSt := s.hostedAPI(c)
   113  	defer otherSt.Close()
   114  
   115  	err := undertakerClient.ProcessDyingModel()
   116  	c.Assert(err, gc.ErrorMatches, "model is not dying")
   117  
   118  	factory.NewFactory(otherSt, s.StatePool).MakeApplication(c, nil)
   119  	model, err := otherSt.Model()
   120  	c.Assert(err, jc.ErrorIsNil)
   121  	c.Assert(model.Destroy(state.DestroyModelParams{}), jc.ErrorIsNil)
   122  	c.Assert(model.Refresh(), jc.ErrorIsNil)
   123  	c.Assert(model.Life(), gc.Equals, state.Dying)
   124  
   125  	err = otherSt.Cleanup()
   126  	c.Assert(err, jc.ErrorIsNil)
   127  	c.Assert(undertakerClient.ProcessDyingModel(), jc.ErrorIsNil)
   128  
   129  	c.Assert(model.Refresh(), jc.ErrorIsNil)
   130  	c.Assert(model.Life(), gc.Equals, state.Dying)
   131  }
   132  
   133  func (s *undertakerSuite) TestWatchModelResources(c *gc.C) {
   134  	undertakerClient, otherSt := s.hostedAPI(c)
   135  	defer otherSt.Close()
   136  
   137  	w, err := undertakerClient.WatchModelResources()
   138  	c.Assert(err, jc.ErrorIsNil)
   139  	defer w.Kill()
   140  	wc := watchertest.NewNotifyWatcherC(c, w, nil)
   141  	wc.AssertOneChange()
   142  	wc.AssertStops()
   143  }
   144  
   145  func (s *undertakerSuite) TestHostedRemoveEnviron(c *gc.C) {
   146  	undertakerClient, otherSt := s.hostedAPI(c)
   147  	defer otherSt.Close()
   148  
   149  	// Aborts on alive environ.
   150  	err := undertakerClient.RemoveModel()
   151  	c.Assert(err, gc.ErrorMatches, "can't remove model: model still alive")
   152  
   153  	factory.NewFactory(otherSt, s.StatePool).MakeApplication(c, nil)
   154  	model, err := otherSt.Model()
   155  	c.Assert(err, jc.ErrorIsNil)
   156  	c.Assert(model.Destroy(state.DestroyModelParams{}), jc.ErrorIsNil)
   157  
   158  	err = otherSt.Cleanup()
   159  	c.Assert(err, jc.ErrorIsNil)
   160  	c.Assert(undertakerClient.ProcessDyingModel(), jc.ErrorIsNil)
   161  
   162  	c.Assert(undertakerClient.RemoveModel(), jc.ErrorIsNil)
   163  	c.Assert(otherSt.EnsureModelRemoved(), jc.ErrorIsNil)
   164  }
   165  
   166  func (s *undertakerSuite) hostedAPI(c *gc.C) (*undertaker.Client, *state.State) {
   167  	otherState := s.Factory.MakeModel(c, &factory.ModelParams{Name: "hosted-env"})
   168  
   169  	password, err := utils.RandomPassword()
   170  	c.Assert(err, jc.ErrorIsNil)
   171  
   172  	machine := s.Factory.MakeMachine(c, &factory.MachineParams{
   173  		Jobs:     []state.MachineJob{state.JobManageModel},
   174  		Password: password,
   175  		Nonce:    "fake_nonce",
   176  	})
   177  
   178  	// Connect to hosted environ from controller.
   179  	info := s.APIInfo(c)
   180  	info.Tag = machine.Tag()
   181  	info.Password = password
   182  	info.Nonce = "fake_nonce"
   183  	info.ModelTag = names.NewModelTag(otherState.ModelUUID())
   184  
   185  	otherAPIState, err := api.Open(info, api.DefaultDialOpts())
   186  	c.Assert(err, jc.ErrorIsNil)
   187  
   188  	undertakerClient, err := undertaker.NewClient(otherAPIState, apiwatcher.NewNotifyWatcher)
   189  	c.Assert(err, jc.ErrorIsNil)
   190  	c.Assert(undertakerClient, gc.NotNil)
   191  
   192  	return undertakerClient, otherState
   193  }