github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/apiserver/facades/controller/undertaker/undertaker_test.go (about)

     1  // Copyright 2015 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package undertaker_test
     5  
     6  import (
     7  	jc "github.com/juju/testing/checkers"
     8  	gc "gopkg.in/check.v1"
     9  	"gopkg.in/juju/names.v2"
    10  
    11  	"github.com/juju/juju/apiserver/facades/controller/undertaker"
    12  	"github.com/juju/juju/apiserver/params"
    13  	apiservertesting "github.com/juju/juju/apiserver/testing"
    14  	"github.com/juju/juju/core/status"
    15  	"github.com/juju/juju/state"
    16  	coretesting "github.com/juju/juju/testing"
    17  )
    18  
    19  type undertakerSuite struct {
    20  	coretesting.BaseSuite
    21  }
    22  
    23  var _ = gc.Suite(&undertakerSuite{})
    24  
    25  func (s *undertakerSuite) setupStateAndAPI(c *gc.C, isSystem bool, modelName string) (*mockState, *undertaker.UndertakerAPI) {
    26  	machineNo := "1"
    27  	if isSystem {
    28  		machineNo = "0"
    29  	}
    30  
    31  	authorizer := apiservertesting.FakeAuthorizer{
    32  		Tag:        names.NewMachineTag(machineNo),
    33  		Controller: true,
    34  	}
    35  
    36  	st := newMockState(names.NewUserTag("admin"), modelName, isSystem)
    37  	api, err := undertaker.NewUndertaker(st, nil, authorizer)
    38  	c.Assert(err, jc.ErrorIsNil)
    39  	return st, api
    40  }
    41  
    42  func (s *undertakerSuite) TestNoPerms(c *gc.C) {
    43  	for _, authorizer := range []apiservertesting.FakeAuthorizer{{
    44  		Tag: names.NewMachineTag("0"),
    45  	}, {
    46  		Tag: names.NewUserTag("bob"),
    47  	}} {
    48  		st := newMockState(names.NewUserTag("admin"), "admin", true)
    49  		_, err := undertaker.NewUndertaker(
    50  			st,
    51  			nil,
    52  			authorizer,
    53  		)
    54  		c.Assert(err, gc.ErrorMatches, "permission denied")
    55  	}
    56  }
    57  
    58  func (s *undertakerSuite) TestModelInfo(c *gc.C) {
    59  	otherSt, hostedAPI := s.setupStateAndAPI(c, false, "hostedmodel")
    60  	st, api := s.setupStateAndAPI(c, true, "admin")
    61  	for _, test := range []struct {
    62  		st        *mockState
    63  		api       *undertaker.UndertakerAPI
    64  		isSystem  bool
    65  		modelName string
    66  	}{
    67  		{otherSt, hostedAPI, false, "hostedmodel"},
    68  		{st, api, true, "admin"},
    69  	} {
    70  		test.st.model.life = state.Dying
    71  
    72  		result, err := test.api.ModelInfo()
    73  		c.Assert(err, jc.ErrorIsNil)
    74  
    75  		info := result.Result
    76  		c.Assert(err, jc.ErrorIsNil)
    77  		c.Assert(result.Error, gc.IsNil)
    78  
    79  		c.Assert(info.UUID, gc.Equals, test.st.model.UUID())
    80  		c.Assert(info.GlobalName, gc.Equals, "user-admin/"+test.modelName)
    81  		c.Assert(info.Name, gc.Equals, test.modelName)
    82  		c.Assert(info.IsSystem, gc.Equals, test.isSystem)
    83  		c.Assert(info.Life, gc.Equals, params.Dying)
    84  	}
    85  }
    86  
    87  func (s *undertakerSuite) TestProcessDyingModel(c *gc.C) {
    88  	otherSt, hostedAPI := s.setupStateAndAPI(c, false, "hostedmodel")
    89  	model, err := otherSt.Model()
    90  	c.Assert(err, jc.ErrorIsNil)
    91  
    92  	err = hostedAPI.ProcessDyingModel()
    93  	c.Assert(err, gc.ErrorMatches, "model is not dying")
    94  	c.Assert(model.Life(), gc.Equals, state.Alive)
    95  
    96  	otherSt.model.life = state.Dying
    97  	err = hostedAPI.ProcessDyingModel()
    98  	c.Assert(err, gc.IsNil)
    99  	c.Assert(model.Life(), gc.Equals, state.Dead)
   100  }
   101  
   102  func (s *undertakerSuite) TestRemoveAliveModel(c *gc.C) {
   103  	otherSt, hostedAPI := s.setupStateAndAPI(c, false, "hostedmodel")
   104  	_, err := otherSt.Model()
   105  	c.Assert(err, jc.ErrorIsNil)
   106  
   107  	err = hostedAPI.RemoveModel()
   108  	c.Assert(err, gc.ErrorMatches, "model not dying or dead")
   109  }
   110  
   111  func (s *undertakerSuite) TestRemoveDyingModel(c *gc.C) {
   112  	otherSt, hostedAPI := s.setupStateAndAPI(c, false, "hostedmodel")
   113  
   114  	// Set model to dying
   115  	otherSt.model.life = state.Dying
   116  
   117  	c.Assert(hostedAPI.RemoveModel(), jc.ErrorIsNil)
   118  }
   119  
   120  func (s *undertakerSuite) TestDeadRemoveModel(c *gc.C) {
   121  	otherSt, hostedAPI := s.setupStateAndAPI(c, false, "hostedmodel")
   122  
   123  	// Set model to dead
   124  	otherSt.model.life = state.Dying
   125  	err := hostedAPI.ProcessDyingModel()
   126  	c.Assert(err, gc.IsNil)
   127  
   128  	err = hostedAPI.RemoveModel()
   129  	c.Assert(err, jc.ErrorIsNil)
   130  
   131  	c.Assert(otherSt.removed, jc.IsTrue)
   132  }
   133  
   134  func (s *undertakerSuite) TestModelConfig(c *gc.C) {
   135  	_, hostedAPI := s.setupStateAndAPI(c, false, "hostedmodel")
   136  
   137  	cfg, err := hostedAPI.ModelConfig()
   138  	c.Assert(err, jc.ErrorIsNil)
   139  	c.Assert(cfg, gc.NotNil)
   140  }
   141  
   142  func (s *undertakerSuite) TestSetStatus(c *gc.C) {
   143  	mock, hostedAPI := s.setupStateAndAPI(c, false, "hostedmodel")
   144  
   145  	results, err := hostedAPI.SetStatus(params.SetStatus{
   146  		Entities: []params.EntityStatusArgs{{
   147  			mock.model.Tag().String(), status.Destroying.String(),
   148  			"woop", map[string]interface{}{"da": "ta"},
   149  		}},
   150  	})
   151  	c.Assert(err, jc.ErrorIsNil)
   152  	c.Assert(results.Results, gc.HasLen, 1)
   153  	c.Assert(results.Results[0].Error, gc.IsNil)
   154  	c.Assert(mock.model.status, gc.Equals, status.Destroying)
   155  	c.Assert(mock.model.statusInfo, gc.Equals, "woop")
   156  	c.Assert(mock.model.statusData, jc.DeepEquals, map[string]interface{}{"da": "ta"})
   157  }
   158  
   159  func (s *undertakerSuite) TestSetStatusControllerPermissions(c *gc.C) {
   160  	_, hostedAPI := s.setupStateAndAPI(c, true, "hostedmodel")
   161  	results, err := hostedAPI.SetStatus(params.SetStatus{
   162  		Entities: []params.EntityStatusArgs{{
   163  			"model-6ada782f-bcd4-454b-a6da-d1793fbcb35e", status.Destroying.String(),
   164  			"woop", map[string]interface{}{"da": "ta"},
   165  		}},
   166  	})
   167  	c.Assert(err, jc.ErrorIsNil)
   168  	c.Assert(results.Results, gc.HasLen, 1)
   169  	c.Assert(results.Results[0].Error, gc.ErrorMatches, ".*not found")
   170  }
   171  
   172  func (s *undertakerSuite) TestSetStatusNonControllerPermissions(c *gc.C) {
   173  	_, hostedAPI := s.setupStateAndAPI(c, false, "hostedmodel")
   174  	results, err := hostedAPI.SetStatus(params.SetStatus{
   175  		Entities: []params.EntityStatusArgs{{
   176  			"model-6ada782f-bcd4-454b-a6da-d1793fbcb35e", status.Destroying.String(),
   177  			"woop", map[string]interface{}{"da": "ta"},
   178  		}},
   179  	})
   180  	c.Assert(err, jc.ErrorIsNil)
   181  	c.Assert(results.Results[0].Error, gc.ErrorMatches, "permission denied")
   182  }