github.com/cloud-green/juju@v0.0.0-20151002100041-a00291338d3d/worker/uniter/operation/leader_test.go (about)

     1  // Copyright 2015 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package operation_test
     5  
     6  import (
     7  	"github.com/juju/testing"
     8  	jc "github.com/juju/testing/checkers"
     9  	gc "gopkg.in/check.v1"
    10  	"gopkg.in/juju/charm.v6-unstable/hooks"
    11  
    12  	"github.com/juju/juju/worker/uniter/hook"
    13  	"github.com/juju/juju/worker/uniter/operation"
    14  )
    15  
    16  type LeaderSuite struct {
    17  	testing.IsolationSuite
    18  }
    19  
    20  var _ = gc.Suite(&LeaderSuite{})
    21  
    22  func (s *LeaderSuite) TestAcceptLeadership_Prepare_BadState(c *gc.C) {
    23  	factory := operation.NewFactory(operation.FactoryParams{})
    24  	op, err := factory.NewAcceptLeadership()
    25  	c.Assert(err, jc.ErrorIsNil)
    26  
    27  	newState, err := op.Prepare(operation.State{})
    28  	c.Check(newState, gc.IsNil)
    29  	// accept is only valid in Continue mode, when we're sure nothing is queued
    30  	// or in progress.
    31  	c.Check(err, gc.Equals, operation.ErrCannotAcceptLeadership)
    32  }
    33  
    34  func (s *LeaderSuite) TestAcceptLeadership_Prepare_NotLeader(c *gc.C) {
    35  	factory := operation.NewFactory(operation.FactoryParams{})
    36  	op, err := factory.NewAcceptLeadership()
    37  	c.Assert(err, jc.ErrorIsNil)
    38  
    39  	newState, err := op.Prepare(operation.State{Kind: operation.Continue})
    40  	c.Check(newState, gc.IsNil)
    41  	// *execute* is currently just a no-op -- all the meat happens in commit.
    42  	c.Check(err, gc.Equals, operation.ErrSkipExecute)
    43  }
    44  
    45  func (s *LeaderSuite) TestAcceptLeadership_Prepare_AlreadyLeader(c *gc.C) {
    46  	factory := operation.NewFactory(operation.FactoryParams{})
    47  	op, err := factory.NewAcceptLeadership()
    48  	c.Assert(err, jc.ErrorIsNil)
    49  
    50  	newState, err := op.Prepare(operation.State{
    51  		Kind:   operation.Continue,
    52  		Leader: true,
    53  	})
    54  	c.Check(newState, gc.IsNil)
    55  	// *execute* is currently just a no-op -- all the meat happens in commit.
    56  	c.Check(err, gc.Equals, operation.ErrSkipExecute)
    57  }
    58  
    59  func (s *LeaderSuite) TestAcceptLeadership_Commit_NotLeader_BlankSlate(c *gc.C) {
    60  	factory := operation.NewFactory(operation.FactoryParams{})
    61  	op, err := factory.NewAcceptLeadership()
    62  	c.Assert(err, jc.ErrorIsNil)
    63  	_, err = op.Prepare(operation.State{Kind: operation.Continue})
    64  	c.Check(err, gc.Equals, operation.ErrSkipExecute)
    65  
    66  	newState, err := op.Commit(operation.State{
    67  		Kind: operation.Continue,
    68  	})
    69  	c.Check(err, jc.ErrorIsNil)
    70  	c.Check(newState, gc.DeepEquals, &operation.State{
    71  		Kind:   operation.RunHook,
    72  		Step:   operation.Queued,
    73  		Hook:   &hook.Info{Kind: hook.LeaderElected},
    74  		Leader: true,
    75  	})
    76  }
    77  
    78  func (s *LeaderSuite) TestAcceptLeadership_Commit_NotLeader_Preserve(c *gc.C) {
    79  	factory := operation.NewFactory(operation.FactoryParams{})
    80  	op, err := factory.NewAcceptLeadership()
    81  	c.Assert(err, jc.ErrorIsNil)
    82  	_, err = op.Prepare(operation.State{Kind: operation.Continue})
    83  	c.Check(err, gc.Equals, operation.ErrSkipExecute)
    84  
    85  	newState, err := op.Commit(operation.State{
    86  		Kind:    operation.Continue,
    87  		Started: true,
    88  		Hook:    &hook.Info{Kind: hooks.Install},
    89  	})
    90  	c.Check(err, jc.ErrorIsNil)
    91  	c.Check(newState, gc.DeepEquals, &operation.State{
    92  		Kind:    operation.RunHook,
    93  		Step:    operation.Queued,
    94  		Hook:    &hook.Info{Kind: hook.LeaderElected},
    95  		Leader:  true,
    96  		Started: true,
    97  	})
    98  }
    99  
   100  func (s *LeaderSuite) TestAcceptLeadership_Commit_AlreadyLeader(c *gc.C) {
   101  	factory := operation.NewFactory(operation.FactoryParams{})
   102  	op, err := factory.NewAcceptLeadership()
   103  	c.Assert(err, jc.ErrorIsNil)
   104  	_, err = op.Prepare(operation.State{Kind: operation.Continue})
   105  	c.Check(err, gc.Equals, operation.ErrSkipExecute)
   106  
   107  	newState, err := op.Commit(operation.State{
   108  		Kind:   operation.Continue,
   109  		Leader: true,
   110  	})
   111  	c.Check(newState, gc.IsNil)
   112  	c.Check(err, jc.ErrorIsNil)
   113  }
   114  
   115  func (s *LeaderSuite) TestAcceptLeadership_DoesNotNeedGlobalMachineLock(c *gc.C) {
   116  	factory := operation.NewFactory(operation.FactoryParams{})
   117  	op, err := factory.NewAcceptLeadership()
   118  	c.Assert(err, jc.ErrorIsNil)
   119  	c.Assert(op.NeedsGlobalMachineLock(), jc.IsFalse)
   120  }
   121  
   122  func (s *LeaderSuite) TestResignLeadership_Prepare_Leader(c *gc.C) {
   123  	factory := operation.NewFactory(operation.FactoryParams{})
   124  	op, err := factory.NewResignLeadership()
   125  	c.Assert(err, jc.ErrorIsNil)
   126  
   127  	newState, err := op.Prepare(operation.State{Leader: true})
   128  	c.Check(newState, gc.IsNil)
   129  	c.Check(err, jc.ErrorIsNil)
   130  }
   131  
   132  func (s *LeaderSuite) TestResignLeadership_Prepare_NotLeader(c *gc.C) {
   133  	factory := operation.NewFactory(operation.FactoryParams{})
   134  	op, err := factory.NewResignLeadership()
   135  	c.Assert(err, jc.ErrorIsNil)
   136  
   137  	newState, err := op.Prepare(operation.State{})
   138  	c.Check(newState, gc.IsNil)
   139  	c.Check(err, gc.Equals, operation.ErrSkipExecute)
   140  }
   141  
   142  func (s *LeaderSuite) TestResignLeadership_Execute(c *gc.C) {
   143  	factory := operation.NewFactory(operation.FactoryParams{})
   144  	op, err := factory.NewResignLeadership()
   145  	c.Assert(err, jc.ErrorIsNil)
   146  
   147  	_, err = op.Prepare(operation.State{Leader: true})
   148  	c.Check(err, jc.ErrorIsNil)
   149  
   150  	// Execute is a no-op (which logs that we should run leader-deposed)
   151  	newState, err := op.Execute(operation.State{})
   152  	c.Check(newState, gc.IsNil)
   153  	c.Check(err, jc.ErrorIsNil)
   154  }
   155  
   156  func (s *LeaderSuite) TestResignLeadership_Commit_ClearLeader(c *gc.C) {
   157  	factory := operation.NewFactory(operation.FactoryParams{})
   158  	op, err := factory.NewResignLeadership()
   159  	c.Assert(err, jc.ErrorIsNil)
   160  
   161  	newState, err := op.Commit(operation.State{Leader: true})
   162  	c.Check(newState, gc.DeepEquals, &operation.State{})
   163  	c.Check(err, jc.ErrorIsNil)
   164  }
   165  
   166  func (s *LeaderSuite) TestResignLeadership_Commit_PreserveOthers(c *gc.C) {
   167  	factory := operation.NewFactory(operation.FactoryParams{})
   168  	op, err := factory.NewResignLeadership()
   169  	c.Assert(err, jc.ErrorIsNil)
   170  
   171  	newState, err := op.Commit(overwriteState)
   172  	c.Check(newState, gc.DeepEquals, &overwriteState)
   173  	c.Check(err, jc.ErrorIsNil)
   174  }
   175  
   176  func (s *LeaderSuite) TestResignLeadership_Commit_All(c *gc.C) {
   177  	factory := operation.NewFactory(operation.FactoryParams{})
   178  	op, err := factory.NewResignLeadership()
   179  	c.Assert(err, jc.ErrorIsNil)
   180  
   181  	leaderState := overwriteState
   182  	leaderState.Leader = true
   183  	newState, err := op.Commit(leaderState)
   184  	c.Check(newState, gc.DeepEquals, &overwriteState)
   185  	c.Check(err, jc.ErrorIsNil)
   186  }
   187  
   188  func (s *LeaderSuite) TestResignLeadership_DoesNotNeedGlobalMachineLock(c *gc.C) {
   189  	factory := operation.NewFactory(operation.FactoryParams{})
   190  	op, err := factory.NewResignLeadership()
   191  	c.Assert(err, jc.ErrorIsNil)
   192  	c.Assert(op.NeedsGlobalMachineLock(), jc.IsFalse)
   193  }