github.com/axw/juju@v0.0.0-20161005053422-4bd6544d08d4/apiserver/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/params"
    12  	apiservertesting "github.com/juju/juju/apiserver/testing"
    13  	"github.com/juju/juju/apiserver/undertaker"
    14  	"github.com/juju/juju/state"
    15  	"github.com/juju/juju/status"
    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, envName 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  		EnvironManager: true,
    34  	}
    35  
    36  	st := newMockState(names.NewUserTag("admin"), envName, 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  		apiservertesting.FakeAuthorizer{
    45  			Tag: names.NewMachineTag("0"),
    46  		},
    47  		apiservertesting.FakeAuthorizer{
    48  			Tag:            names.NewUserTag("bob"),
    49  			EnvironManager: true,
    50  		},
    51  	} {
    52  		st := newMockState(names.NewUserTag("admin"), "admin", true)
    53  		_, err := undertaker.NewUndertaker(
    54  			st,
    55  			nil,
    56  			authorizer,
    57  		)
    58  		c.Assert(err, gc.ErrorMatches, "permission denied")
    59  	}
    60  }
    61  
    62  func (s *undertakerSuite) TestEnvironInfo(c *gc.C) {
    63  	otherSt, hostedAPI := s.setupStateAndAPI(c, false, "hostedenv")
    64  	st, api := s.setupStateAndAPI(c, true, "admin")
    65  	for _, test := range []struct {
    66  		st       *mockState
    67  		api      *undertaker.UndertakerAPI
    68  		isSystem bool
    69  		envName  string
    70  	}{
    71  		{otherSt, hostedAPI, false, "hostedenv"},
    72  		{st, api, true, "admin"},
    73  	} {
    74  		env, err := test.st.Model()
    75  		c.Assert(err, jc.ErrorIsNil)
    76  		c.Assert(env.Destroy(), jc.ErrorIsNil)
    77  
    78  		result, err := test.api.ModelInfo()
    79  		c.Assert(err, jc.ErrorIsNil)
    80  
    81  		info := result.Result
    82  		c.Assert(err, jc.ErrorIsNil)
    83  		c.Assert(result.Error, gc.IsNil)
    84  
    85  		c.Assert(info.UUID, gc.Equals, env.UUID())
    86  		c.Assert(info.GlobalName, gc.Equals, "user-admin/"+test.envName)
    87  		c.Assert(info.Name, gc.Equals, test.envName)
    88  		c.Assert(info.IsSystem, gc.Equals, test.isSystem)
    89  		c.Assert(info.Life, gc.Equals, params.Dying)
    90  	}
    91  }
    92  
    93  func (s *undertakerSuite) TestProcessDyingEnviron(c *gc.C) {
    94  	otherSt, hostedAPI := s.setupStateAndAPI(c, false, "hostedenv")
    95  	env, err := otherSt.Model()
    96  	c.Assert(err, jc.ErrorIsNil)
    97  
    98  	err = hostedAPI.ProcessDyingModel()
    99  	c.Assert(err, gc.ErrorMatches, "model is not dying")
   100  	c.Assert(env.Life(), gc.Equals, state.Alive)
   101  
   102  	err = env.Destroy()
   103  	c.Assert(err, jc.ErrorIsNil)
   104  
   105  	c.Assert(env.Life(), gc.Equals, state.Dying)
   106  
   107  	err = hostedAPI.ProcessDyingModel()
   108  	c.Assert(err, gc.IsNil)
   109  	c.Assert(env.Life(), gc.Equals, state.Dead)
   110  }
   111  
   112  func (s *undertakerSuite) TestRemoveAliveEnviron(c *gc.C) {
   113  	otherSt, hostedAPI := s.setupStateAndAPI(c, false, "hostedenv")
   114  	_, err := otherSt.Model()
   115  	c.Assert(err, jc.ErrorIsNil)
   116  
   117  	err = hostedAPI.RemoveModel()
   118  	c.Assert(err, gc.ErrorMatches, "model not dead")
   119  }
   120  
   121  func (s *undertakerSuite) TestRemoveDyingEnviron(c *gc.C) {
   122  	otherSt, hostedAPI := s.setupStateAndAPI(c, false, "hostedenv")
   123  	env, err := otherSt.Model()
   124  	c.Assert(err, jc.ErrorIsNil)
   125  
   126  	// Set env to dying
   127  	err = env.Destroy()
   128  	c.Assert(err, jc.ErrorIsNil)
   129  
   130  	err = hostedAPI.RemoveModel()
   131  	c.Assert(err, gc.ErrorMatches, "model not dead")
   132  }
   133  
   134  func (s *undertakerSuite) TestDeadRemoveEnviron(c *gc.C) {
   135  	otherSt, hostedAPI := s.setupStateAndAPI(c, false, "hostedenv")
   136  	env, err := otherSt.Model()
   137  	c.Assert(err, jc.ErrorIsNil)
   138  
   139  	// Set env to dead
   140  	err = env.Destroy()
   141  	c.Assert(err, jc.ErrorIsNil)
   142  	err = hostedAPI.ProcessDyingModel()
   143  	c.Assert(err, gc.IsNil)
   144  
   145  	err = hostedAPI.RemoveModel()
   146  	c.Assert(err, jc.ErrorIsNil)
   147  
   148  	c.Assert(otherSt.removed, jc.IsTrue)
   149  }
   150  
   151  func (s *undertakerSuite) TestModelConfig(c *gc.C) {
   152  	_, hostedAPI := s.setupStateAndAPI(c, false, "hostedenv")
   153  
   154  	cfg, err := hostedAPI.ModelConfig()
   155  	c.Assert(err, jc.ErrorIsNil)
   156  	c.Assert(cfg, gc.NotNil)
   157  }
   158  
   159  func (s *undertakerSuite) TestSetStatus(c *gc.C) {
   160  	mock, hostedAPI := s.setupStateAndAPI(c, false, "hostedenv")
   161  
   162  	results, err := hostedAPI.SetStatus(params.SetStatus{
   163  		Entities: []params.EntityStatusArgs{{
   164  			mock.env.Tag().String(), status.Destroying.String(),
   165  			"woop", map[string]interface{}{"da": "ta"},
   166  		}},
   167  	})
   168  	c.Assert(err, jc.ErrorIsNil)
   169  	c.Assert(results.Results, gc.HasLen, 1)
   170  	c.Assert(results.Results[0].Error, gc.IsNil)
   171  	c.Assert(mock.env.status, gc.Equals, status.Destroying)
   172  	c.Assert(mock.env.statusInfo, gc.Equals, "woop")
   173  	c.Assert(mock.env.statusData, jc.DeepEquals, map[string]interface{}{"da": "ta"})
   174  }
   175  
   176  func (s *undertakerSuite) TestSetStatusControllerPermissions(c *gc.C) {
   177  	_, hostedAPI := s.setupStateAndAPI(c, true, "hostedenv")
   178  	results, err := hostedAPI.SetStatus(params.SetStatus{
   179  		Entities: []params.EntityStatusArgs{{
   180  			"model-6ada782f-bcd4-454b-a6da-d1793fbcb35e", status.Destroying.String(),
   181  			"woop", map[string]interface{}{"da": "ta"},
   182  		}},
   183  	})
   184  	c.Assert(err, jc.ErrorIsNil)
   185  	c.Assert(results.Results, gc.HasLen, 1)
   186  	c.Assert(results.Results[0].Error, gc.ErrorMatches, ".*not found")
   187  }
   188  
   189  func (s *undertakerSuite) TestSetStatusNonControllerPermissions(c *gc.C) {
   190  	_, hostedAPI := s.setupStateAndAPI(c, false, "hostedenv")
   191  	results, err := hostedAPI.SetStatus(params.SetStatus{
   192  		Entities: []params.EntityStatusArgs{{
   193  			"model-6ada782f-bcd4-454b-a6da-d1793fbcb35e", status.Destroying.String(),
   194  			"woop", map[string]interface{}{"da": "ta"},
   195  		}},
   196  	})
   197  	c.Assert(err, jc.ErrorIsNil)
   198  	c.Assert(results.Results[0].Error, gc.ErrorMatches, "permission denied")
   199  }