github.com/Pankov404/juju@v0.0.0-20150703034450-be266991dceb/apiserver/leadership/leadership_test.go (about)

     1  // Copyright 2014 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package leadership
     5  
     6  /*
     7  Test that the service is translating incoming parameters to the
     8  manager layer correctly, and also translates the results back into
     9  network parameters.
    10  */
    11  
    12  import (
    13  	"time"
    14  
    15  	"github.com/juju/errors"
    16  	"github.com/juju/names"
    17  	"github.com/juju/testing"
    18  	jc "github.com/juju/testing/checkers"
    19  	gc "gopkg.in/check.v1"
    20  
    21  	"github.com/juju/juju/apiserver/params"
    22  	"github.com/juju/juju/leadership"
    23  )
    24  
    25  type leadershipSuite struct {
    26  	testing.IsolationSuite
    27  }
    28  
    29  var _ = gc.Suite(&leadershipSuite{})
    30  
    31  const (
    32  	StubServiceNm = "stub-service"
    33  	StubUnitNm    = "stub-unit/0"
    34  )
    35  
    36  type stubLeadershipManager struct {
    37  	ClaimLeadershipFn              func(sid, uid string, duration time.Duration) error
    38  	ReleaseLeadershipFn            func(sid, uid string) error
    39  	BlockUntilLeadershipReleasedFn func(serviceId string) error
    40  }
    41  
    42  func (m *stubLeadershipManager) ClaimLeadership(sid, uid string, duration time.Duration) error {
    43  	if m.ClaimLeadershipFn != nil {
    44  		return m.ClaimLeadershipFn(sid, uid, duration)
    45  	}
    46  	return nil
    47  }
    48  
    49  func (m *stubLeadershipManager) ReleaseLeadership(sid, uid string) error {
    50  	if m.ReleaseLeadershipFn != nil {
    51  		return m.ReleaseLeadershipFn(sid, uid)
    52  	}
    53  	return nil
    54  }
    55  
    56  func (m *stubLeadershipManager) BlockUntilLeadershipReleased(serviceId string) error {
    57  	if m.BlockUntilLeadershipReleasedFn != nil {
    58  		return m.BlockUntilLeadershipReleasedFn(serviceId)
    59  	}
    60  	return nil
    61  }
    62  
    63  type stubAuthorizer struct {
    64  	AuthOwnerFn     func(names.Tag) bool
    65  	AuthUnitAgentFn func() bool
    66  }
    67  
    68  func (m *stubAuthorizer) AuthMachineAgent() bool { return true }
    69  func (m *stubAuthorizer) AuthUnitAgent() bool {
    70  	if m.AuthUnitAgentFn != nil {
    71  		return m.AuthUnitAgentFn()
    72  	}
    73  	return true
    74  }
    75  func (m *stubAuthorizer) AuthOwner(tag names.Tag) bool {
    76  	if m.AuthOwnerFn != nil {
    77  		return m.AuthOwnerFn(tag)
    78  	}
    79  	return true
    80  }
    81  func (m *stubAuthorizer) AuthEnvironManager() bool { return true }
    82  func (m *stubAuthorizer) AuthClient() bool         { return true }
    83  func (m *stubAuthorizer) GetAuthTag() names.Tag    { return names.NewServiceTag(StubUnitNm) }
    84  
    85  func checkDurationEquals(c *gc.C, actual, expect time.Duration) {
    86  	delta := actual - expect
    87  	if delta < 0 {
    88  		delta = -delta
    89  	}
    90  	c.Check(delta, jc.LessThan, time.Microsecond)
    91  }
    92  
    93  func (s *leadershipSuite) TestClaimLeadershipTranslation(c *gc.C) {
    94  	var ldrMgr stubLeadershipManager
    95  	ldrMgr.ClaimLeadershipFn = func(sid, uid string, duration time.Duration) error {
    96  		c.Check(sid, gc.Equals, StubServiceNm)
    97  		c.Check(uid, gc.Equals, StubUnitNm)
    98  		expectDuration := time.Duration(299.9 * float64(time.Second))
    99  		checkDurationEquals(c, duration, expectDuration)
   100  		return nil
   101  	}
   102  
   103  	ldrSvc := &leadershipService{LeadershipManager: &ldrMgr, authorizer: &stubAuthorizer{}}
   104  	results, err := ldrSvc.ClaimLeadership(params.ClaimLeadershipBulkParams{
   105  		Params: []params.ClaimLeadershipParams{
   106  			{
   107  				ServiceTag:      names.NewServiceTag(StubServiceNm).String(),
   108  				UnitTag:         names.NewUnitTag(StubUnitNm).String(),
   109  				DurationSeconds: 299.9,
   110  			},
   111  		},
   112  	})
   113  
   114  	c.Check(err, jc.ErrorIsNil)
   115  	c.Assert(results.Results, gc.HasLen, 1)
   116  	c.Check(results.Results[0].Error, gc.IsNil)
   117  }
   118  
   119  func (s *leadershipSuite) TestClaimLeadershipDeniedError(c *gc.C) {
   120  	var ldrMgr stubLeadershipManager
   121  	ldrMgr.ClaimLeadershipFn = func(sid, uid string, duration time.Duration) error {
   122  		c.Check(sid, gc.Equals, StubServiceNm)
   123  		c.Check(uid, gc.Equals, StubUnitNm)
   124  		expectDuration := time.Duration(5.001 * float64(time.Second))
   125  		checkDurationEquals(c, duration, expectDuration)
   126  		return errors.Annotatef(leadership.ErrClaimDenied, "obfuscated")
   127  	}
   128  
   129  	ldrSvc := &leadershipService{LeadershipManager: &ldrMgr, authorizer: &stubAuthorizer{}}
   130  	results, err := ldrSvc.ClaimLeadership(params.ClaimLeadershipBulkParams{
   131  		Params: []params.ClaimLeadershipParams{
   132  			{
   133  				ServiceTag:      names.NewServiceTag(StubServiceNm).String(),
   134  				UnitTag:         names.NewUnitTag(StubUnitNm).String(),
   135  				DurationSeconds: 5.001,
   136  			},
   137  		},
   138  	})
   139  
   140  	c.Check(err, jc.ErrorIsNil)
   141  	c.Assert(results.Results, gc.HasLen, 1)
   142  	c.Check(results.Results[0].Error, jc.Satisfies, params.IsCodeLeadershipClaimDenied)
   143  }
   144  
   145  func (s *leadershipSuite) TestClaimLeadershipBadService(c *gc.C) {
   146  	ldrSvc := &leadershipService{authorizer: &stubAuthorizer{}}
   147  	results, err := ldrSvc.ClaimLeadership(params.ClaimLeadershipBulkParams{
   148  		Params: []params.ClaimLeadershipParams{
   149  			{
   150  				ServiceTag:      "service-bad/0",
   151  				UnitTag:         names.NewUnitTag(StubUnitNm).String(),
   152  				DurationSeconds: 123.45,
   153  			},
   154  		},
   155  	})
   156  	c.Check(err, jc.ErrorIsNil)
   157  	c.Assert(results.Results, gc.HasLen, 1)
   158  	c.Check(results.Results[0].Error, jc.Satisfies, params.IsCodeUnauthorized)
   159  }
   160  
   161  func (s *leadershipSuite) TestClaimLeadershipBadUnit(c *gc.C) {
   162  	ldrSvc := &leadershipService{authorizer: &stubAuthorizer{}}
   163  	results, err := ldrSvc.ClaimLeadership(params.ClaimLeadershipBulkParams{
   164  		Params: []params.ClaimLeadershipParams{
   165  			{
   166  				ServiceTag:      names.NewServiceTag(StubServiceNm).String(),
   167  				UnitTag:         "unit-bad",
   168  				DurationSeconds: 123.45,
   169  			},
   170  		},
   171  	})
   172  	c.Check(err, jc.ErrorIsNil)
   173  	c.Assert(results.Results, gc.HasLen, 1)
   174  	c.Check(results.Results[0].Error, jc.Satisfies, params.IsCodeUnauthorized)
   175  }
   176  
   177  func (s *leadershipSuite) TestClaimLeadershipDurationTooShort(c *gc.C) {
   178  	ldrSvc := &leadershipService{authorizer: &stubAuthorizer{}}
   179  	results, err := ldrSvc.ClaimLeadership(params.ClaimLeadershipBulkParams{
   180  		Params: []params.ClaimLeadershipParams{
   181  			{
   182  				ServiceTag:      names.NewServiceTag(StubServiceNm).String(),
   183  				UnitTag:         names.NewUnitTag(StubUnitNm).String(),
   184  				DurationSeconds: 4.99,
   185  			},
   186  		},
   187  	})
   188  	c.Check(err, jc.ErrorIsNil)
   189  	c.Assert(results.Results, gc.HasLen, 1)
   190  	c.Check(results.Results[0].Error, gc.ErrorMatches, "invalid duration")
   191  }
   192  
   193  func (s *leadershipSuite) TestClaimLeadershipDurationTooLong(c *gc.C) {
   194  	ldrSvc := &leadershipService{authorizer: &stubAuthorizer{}}
   195  	results, err := ldrSvc.ClaimLeadership(params.ClaimLeadershipBulkParams{
   196  		Params: []params.ClaimLeadershipParams{
   197  			{
   198  				ServiceTag:      names.NewServiceTag(StubServiceNm).String(),
   199  				UnitTag:         names.NewUnitTag(StubUnitNm).String(),
   200  				DurationSeconds: 300.1,
   201  			},
   202  		},
   203  	})
   204  	c.Check(err, jc.ErrorIsNil)
   205  	c.Assert(results.Results, gc.HasLen, 1)
   206  	c.Check(results.Results[0].Error, gc.ErrorMatches, "invalid duration")
   207  }
   208  
   209  func (s *leadershipSuite) TestReleaseLeadershipTranslation(c *gc.C) {
   210  
   211  	var ldrMgr stubLeadershipManager
   212  	ldrMgr.ReleaseLeadershipFn = func(sid, uid string) error {
   213  		c.Check(sid, gc.Equals, StubServiceNm)
   214  		c.Check(uid, gc.Equals, StubUnitNm)
   215  		return nil
   216  	}
   217  
   218  	ldrSvc := &leadershipService{LeadershipManager: &ldrMgr, authorizer: &stubAuthorizer{}}
   219  	results, err := ldrSvc.ReleaseLeadership(params.ReleaseLeadershipBulkParams{
   220  		Params: []params.ReleaseLeadershipParams{
   221  			{
   222  				ServiceTag: names.NewServiceTag(StubServiceNm).String(),
   223  				UnitTag:    names.NewUnitTag(StubUnitNm).String(),
   224  			},
   225  		},
   226  	})
   227  
   228  	c.Check(err, jc.ErrorIsNil)
   229  	c.Assert(results.Results, gc.HasLen, 1)
   230  	c.Check(results.Results[0].Error, gc.IsNil)
   231  }
   232  
   233  func (s *leadershipSuite) TestBlockUntilLeadershipReleasedTranslation(c *gc.C) {
   234  
   235  	var ldrMgr stubLeadershipManager
   236  	ldrMgr.BlockUntilLeadershipReleasedFn = func(sid string) error {
   237  		c.Check(sid, gc.Equals, StubServiceNm)
   238  		return nil
   239  	}
   240  
   241  	ldrSvc := &leadershipService{LeadershipManager: &ldrMgr, authorizer: &stubAuthorizer{}}
   242  	result, err := ldrSvc.BlockUntilLeadershipReleased(names.NewServiceTag(StubServiceNm))
   243  
   244  	c.Check(err, jc.ErrorIsNil)
   245  	c.Check(result.Error, gc.IsNil)
   246  }
   247  
   248  func (s *leadershipSuite) TestClaimLeadershipFailOnAuthorizerErrors(c *gc.C) {
   249  	authorizer := &stubAuthorizer{
   250  		AuthUnitAgentFn: func() bool { return false },
   251  	}
   252  
   253  	ldrSvc := &leadershipService{LeadershipManager: nil, authorizer: authorizer}
   254  	results, err := ldrSvc.ClaimLeadership(params.ClaimLeadershipBulkParams{
   255  		Params: []params.ClaimLeadershipParams{
   256  			{
   257  				ServiceTag:      names.NewServiceTag(StubServiceNm).String(),
   258  				UnitTag:         names.NewUnitTag(StubUnitNm).String(),
   259  				DurationSeconds: 123.45,
   260  			},
   261  		},
   262  	})
   263  
   264  	c.Check(err, jc.ErrorIsNil)
   265  	c.Assert(results.Results, gc.HasLen, 1)
   266  	c.Check(results.Results[0].Error, jc.Satisfies, params.IsCodeUnauthorized)
   267  }
   268  
   269  func (s *leadershipSuite) TestReleaseLeadershipFailOnAuthorizerErrors(c *gc.C) {
   270  	authorizer := &stubAuthorizer{
   271  		AuthUnitAgentFn: func() bool { return false },
   272  	}
   273  
   274  	ldrSvc := &leadershipService{LeadershipManager: nil, authorizer: authorizer}
   275  	results, err := ldrSvc.ReleaseLeadership(params.ReleaseLeadershipBulkParams{
   276  		Params: []params.ReleaseLeadershipParams{
   277  			{
   278  				ServiceTag: names.NewServiceTag(StubServiceNm).String(),
   279  				UnitTag:    names.NewUnitTag(StubUnitNm).String(),
   280  			},
   281  		},
   282  	})
   283  
   284  	c.Check(err, jc.ErrorIsNil)
   285  	c.Assert(results.Results, gc.HasLen, 1)
   286  	c.Check(results.Results[0].Error, jc.Satisfies, params.IsCodeUnauthorized)
   287  }
   288  
   289  func (s *leadershipSuite) TestBlockUntilLeadershipReleasedErrors(c *gc.C) {
   290  	authorizer := &stubAuthorizer{
   291  		AuthUnitAgentFn: func() bool { return false },
   292  	}
   293  
   294  	ldrSvc := &leadershipService{LeadershipManager: nil, authorizer: authorizer}
   295  	result, err := ldrSvc.BlockUntilLeadershipReleased(names.NewServiceTag(StubServiceNm))
   296  
   297  	// Overall function call should succeed, but operations should
   298  	// fail with a permissions issue.
   299  	c.Check(err, jc.ErrorIsNil)
   300  	c.Check(result.Error, jc.Satisfies, params.IsCodeUnauthorized)
   301  }