github.com/juju/juju@v0.0.0-20240327075706-a90865de2538/worker/uniter/operation/remoteinit_test.go (about)

     1  // Copyright 2020 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package operation_test
     5  
     6  import (
     7  	"github.com/juju/charm/v12/hooks"
     8  	"github.com/juju/errors"
     9  	"github.com/juju/testing"
    10  	jc "github.com/juju/testing/checkers"
    11  	gc "gopkg.in/check.v1"
    12  
    13  	"github.com/juju/juju/worker/uniter/hook"
    14  	"github.com/juju/juju/worker/uniter/operation"
    15  	"github.com/juju/juju/worker/uniter/remotestate"
    16  )
    17  
    18  type RemoteInitSuite struct {
    19  	testing.IsolationSuite
    20  }
    21  
    22  var _ = gc.Suite(&RemoteInitSuite{})
    23  
    24  func (s *RemoteInitSuite) TestRemoteInit(c *gc.C) {
    25  	callbacks := &RemoteInitCallbacks{
    26  		MockRemoteInit: &MockRemoteInit{
    27  			err: nil,
    28  		},
    29  	}
    30  	var abort <-chan struct{} = make(chan struct{})
    31  	factory := operation.NewFactory(operation.FactoryParams{
    32  		Callbacks: callbacks,
    33  		Abort:     abort,
    34  	})
    35  	runningStatus := remotestate.ContainerRunningStatus{
    36  		PodName: "test",
    37  	}
    38  	op, err := factory.NewRemoteInit(runningStatus)
    39  	c.Assert(err, jc.ErrorIsNil)
    40  
    41  	newState, err := op.Prepare(operation.State{})
    42  	c.Assert(err, jc.ErrorIsNil)
    43  	c.Assert(newState, gc.DeepEquals, &operation.State{
    44  		Kind: operation.RemoteInit,
    45  		Step: operation.Pending,
    46  	})
    47  	c.Assert(callbacks.MockRemoteInit.gotRunningStatus, gc.IsNil)
    48  	c.Assert(callbacks.MockRemoteInit.gotAbort, gc.IsNil)
    49  
    50  	newState, err = op.Execute(*newState)
    51  	c.Assert(err, jc.ErrorIsNil)
    52  	c.Assert(newState, gc.DeepEquals, &operation.State{
    53  		Kind: operation.RemoteInit,
    54  		Step: operation.Done,
    55  	})
    56  	c.Assert(callbacks.MockRemoteInit.gotRunningStatus, gc.DeepEquals, &runningStatus)
    57  	c.Assert(callbacks.MockRemoteInit.gotAbort, gc.Equals, abort)
    58  
    59  	newState, err = op.Commit(*newState)
    60  	c.Assert(err, jc.ErrorIsNil)
    61  	c.Assert(newState, gc.DeepEquals, &operation.State{
    62  		Kind: operation.Continue,
    63  		Step: operation.Pending,
    64  	})
    65  }
    66  
    67  func (s *RemoteInitSuite) TestRemoteInitWithHook(c *gc.C) {
    68  	callbacks := &RemoteInitCallbacks{
    69  		MockRemoteInit: &MockRemoteInit{
    70  			err: nil,
    71  		},
    72  	}
    73  	var abort <-chan struct{} = make(chan struct{})
    74  	factory := operation.NewFactory(operation.FactoryParams{
    75  		Callbacks: callbacks,
    76  		Abort:     abort,
    77  	})
    78  	runningStatus := remotestate.ContainerRunningStatus{
    79  		PodName: "test",
    80  	}
    81  	op, err := factory.NewRemoteInit(runningStatus)
    82  	c.Assert(err, jc.ErrorIsNil)
    83  
    84  	newState, err := op.Prepare(operation.State{
    85  		Kind: operation.RunHook,
    86  		Step: operation.Pending,
    87  		Hook: &hook.Info{
    88  			Kind: hooks.LeaderElected,
    89  		},
    90  	})
    91  	c.Assert(err, jc.ErrorIsNil)
    92  	c.Assert(newState, gc.DeepEquals, &operation.State{
    93  		Kind: operation.RemoteInit,
    94  		Step: operation.Pending,
    95  		Hook: &hook.Info{
    96  			Kind: hooks.LeaderElected,
    97  		},
    98  	})
    99  	c.Assert(callbacks.MockRemoteInit.gotRunningStatus, gc.IsNil)
   100  	c.Assert(callbacks.MockRemoteInit.gotAbort, gc.IsNil)
   101  
   102  	newState, err = op.Execute(*newState)
   103  	c.Assert(err, jc.ErrorIsNil)
   104  	c.Assert(newState, gc.DeepEquals, &operation.State{
   105  		Kind: operation.RemoteInit,
   106  		Step: operation.Done,
   107  		Hook: &hook.Info{
   108  			Kind: hooks.LeaderElected,
   109  		},
   110  	})
   111  	c.Assert(callbacks.MockRemoteInit.gotRunningStatus, gc.DeepEquals, &runningStatus)
   112  	c.Assert(callbacks.MockRemoteInit.gotAbort, gc.Equals, abort)
   113  
   114  	newState, err = op.Commit(*newState)
   115  	c.Assert(err, jc.ErrorIsNil)
   116  	c.Assert(newState, gc.DeepEquals, &operation.State{
   117  		Kind: operation.RunHook,
   118  		Step: operation.Pending,
   119  		Hook: &hook.Info{
   120  			Kind: hooks.LeaderElected,
   121  		},
   122  	})
   123  }
   124  
   125  func (s *RemoteInitSuite) TestRemoteInitFail(c *gc.C) {
   126  	callbacks := &RemoteInitCallbacks{
   127  		MockRemoteInit: &MockRemoteInit{
   128  			err: errors.New("ooops"),
   129  		},
   130  	}
   131  	var abort <-chan struct{} = make(chan struct{})
   132  	factory := operation.NewFactory(operation.FactoryParams{
   133  		Callbacks: callbacks,
   134  		Abort:     abort,
   135  	})
   136  	runningStatus := remotestate.ContainerRunningStatus{
   137  		PodName: "test",
   138  	}
   139  	op, err := factory.NewRemoteInit(runningStatus)
   140  	c.Assert(err, jc.ErrorIsNil)
   141  
   142  	newState, err := op.Prepare(operation.State{})
   143  	c.Assert(err, jc.ErrorIsNil)
   144  	c.Assert(newState, gc.DeepEquals, &operation.State{
   145  		Kind: operation.RemoteInit,
   146  		Step: operation.Pending,
   147  	})
   148  	c.Assert(callbacks.MockRemoteInit.gotRunningStatus, gc.IsNil)
   149  	c.Assert(callbacks.MockRemoteInit.gotAbort, gc.IsNil)
   150  
   151  	newState, err = op.Execute(*newState)
   152  	c.Assert(err, gc.ErrorMatches, "ooops")
   153  	c.Assert(newState, gc.IsNil)
   154  	c.Assert(callbacks.MockRemoteInit.gotRunningStatus, gc.DeepEquals, &runningStatus)
   155  	c.Assert(callbacks.MockRemoteInit.gotAbort, gc.Equals, abort)
   156  }
   157  
   158  func (s *RemoteInitSuite) TestSkipRemoteInit(c *gc.C) {
   159  	callbacks := &RemoteInitCallbacks{
   160  		MockRemoteInit: &MockRemoteInit{
   161  			err: nil,
   162  		},
   163  	}
   164  	var abort <-chan struct{} = make(chan struct{})
   165  	factory := operation.NewFactory(operation.FactoryParams{
   166  		Callbacks: callbacks,
   167  		Abort:     abort,
   168  	})
   169  	op, err := factory.NewSkipRemoteInit(false)
   170  	c.Assert(err, jc.ErrorIsNil)
   171  
   172  	newState, err := op.Prepare(operation.State{})
   173  	c.Assert(err, gc.Equals, operation.ErrSkipExecute)
   174  	c.Assert(newState, gc.IsNil)
   175  	c.Assert(callbacks.MockRemoteInit.gotRunningStatus, gc.IsNil)
   176  	c.Assert(callbacks.MockRemoteInit.gotAbort, gc.IsNil)
   177  
   178  	newState, err = op.Execute(operation.State{})
   179  	c.Assert(err, gc.Equals, operation.ErrSkipExecute)
   180  	c.Assert(newState, gc.IsNil)
   181  	c.Assert(callbacks.MockRemoteInit.gotRunningStatus, gc.IsNil)
   182  	c.Assert(callbacks.MockRemoteInit.gotAbort, gc.IsNil)
   183  
   184  	newState, err = op.Commit(operation.State{})
   185  	c.Assert(err, jc.ErrorIsNil)
   186  	c.Assert(newState, gc.DeepEquals, &operation.State{
   187  		Kind: operation.Continue,
   188  		Step: operation.Pending,
   189  	})
   190  }
   191  
   192  func (s *RemoteInitSuite) TestSkipRemoteInitWithHook(c *gc.C) {
   193  	callbacks := &RemoteInitCallbacks{
   194  		MockRemoteInit: &MockRemoteInit{
   195  			err: nil,
   196  		},
   197  	}
   198  	var abort <-chan struct{} = make(chan struct{})
   199  	factory := operation.NewFactory(operation.FactoryParams{
   200  		Callbacks: callbacks,
   201  		Abort:     abort,
   202  	})
   203  	op, err := factory.NewSkipRemoteInit(false)
   204  	c.Assert(err, jc.ErrorIsNil)
   205  
   206  	newState, err := op.Prepare(operation.State{
   207  		Kind: operation.RemoteInit,
   208  		Step: operation.Pending,
   209  		Hook: &hook.Info{
   210  			Kind: hooks.LeaderElected,
   211  		},
   212  	})
   213  	c.Assert(err, gc.Equals, operation.ErrSkipExecute)
   214  	c.Assert(newState, gc.IsNil)
   215  	c.Assert(callbacks.MockRemoteInit.gotRunningStatus, gc.IsNil)
   216  	c.Assert(callbacks.MockRemoteInit.gotAbort, gc.IsNil)
   217  
   218  	newState, err = op.Execute(operation.State{
   219  		Kind: operation.RemoteInit,
   220  		Step: operation.Pending,
   221  		Hook: &hook.Info{
   222  			Kind: hooks.LeaderElected,
   223  		},
   224  	})
   225  	c.Assert(err, gc.Equals, operation.ErrSkipExecute)
   226  	c.Assert(newState, gc.IsNil)
   227  	c.Assert(callbacks.MockRemoteInit.gotRunningStatus, gc.IsNil)
   228  	c.Assert(callbacks.MockRemoteInit.gotAbort, gc.IsNil)
   229  
   230  	newState, err = op.Commit(operation.State{
   231  		Kind: operation.RemoteInit,
   232  		Step: operation.Pending,
   233  		Hook: &hook.Info{
   234  			Kind: hooks.LeaderElected,
   235  		},
   236  	})
   237  	c.Assert(err, jc.ErrorIsNil)
   238  	c.Assert(newState, gc.DeepEquals, &operation.State{
   239  		Kind: operation.RunHook,
   240  		Step: operation.Pending,
   241  		Hook: &hook.Info{
   242  			Kind: hooks.LeaderElected,
   243  		},
   244  	})
   245  }
   246  
   247  func (s *RemoteInitSuite) TestSkipRemoteInitRetry(c *gc.C) {
   248  	callbacks := &RemoteInitCallbacks{
   249  		MockRemoteInit: &MockRemoteInit{
   250  			err: nil,
   251  		},
   252  	}
   253  	var abort <-chan struct{} = make(chan struct{})
   254  	factory := operation.NewFactory(operation.FactoryParams{
   255  		Callbacks: callbacks,
   256  		Abort:     abort,
   257  	})
   258  	op, err := factory.NewSkipRemoteInit(true)
   259  	c.Assert(err, jc.ErrorIsNil)
   260  
   261  	newState, err := op.Prepare(operation.State{})
   262  	c.Assert(err, gc.Equals, operation.ErrSkipExecute)
   263  	c.Assert(newState, gc.IsNil)
   264  	c.Assert(callbacks.MockRemoteInit.gotRunningStatus, gc.IsNil)
   265  	c.Assert(callbacks.MockRemoteInit.gotAbort, gc.IsNil)
   266  
   267  	newState, err = op.Execute(operation.State{})
   268  	c.Assert(err, gc.Equals, operation.ErrSkipExecute)
   269  	c.Assert(newState, gc.IsNil)
   270  	c.Assert(callbacks.MockRemoteInit.gotRunningStatus, gc.IsNil)
   271  	c.Assert(callbacks.MockRemoteInit.gotAbort, gc.IsNil)
   272  
   273  	newState, err = op.Commit(operation.State{})
   274  	c.Assert(err, jc.ErrorIsNil)
   275  	c.Assert(newState, gc.DeepEquals, &operation.State{
   276  		Kind: operation.RemoteInit,
   277  		Step: operation.Pending,
   278  	})
   279  }
   280  
   281  func (s *RemoteInitSuite) TestSkipRemoteInitRetryWithHook(c *gc.C) {
   282  	callbacks := &RemoteInitCallbacks{
   283  		MockRemoteInit: &MockRemoteInit{
   284  			err: nil,
   285  		},
   286  	}
   287  	var abort <-chan struct{} = make(chan struct{})
   288  	factory := operation.NewFactory(operation.FactoryParams{
   289  		Callbacks: callbacks,
   290  		Abort:     abort,
   291  	})
   292  	op, err := factory.NewSkipRemoteInit(true)
   293  	c.Assert(err, jc.ErrorIsNil)
   294  
   295  	newState, err := op.Prepare(operation.State{
   296  		Kind: operation.RemoteInit,
   297  		Step: operation.Done,
   298  		Hook: &hook.Info{
   299  			Kind: hooks.LeaderElected,
   300  		},
   301  	})
   302  	c.Assert(err, gc.Equals, operation.ErrSkipExecute)
   303  	c.Assert(newState, gc.IsNil)
   304  	c.Assert(callbacks.MockRemoteInit.gotRunningStatus, gc.IsNil)
   305  	c.Assert(callbacks.MockRemoteInit.gotAbort, gc.IsNil)
   306  
   307  	newState, err = op.Execute(operation.State{
   308  		Kind: operation.RemoteInit,
   309  		Step: operation.Done,
   310  		Hook: &hook.Info{
   311  			Kind: hooks.LeaderElected,
   312  		},
   313  	})
   314  	c.Assert(err, gc.Equals, operation.ErrSkipExecute)
   315  	c.Assert(newState, gc.IsNil)
   316  	c.Assert(callbacks.MockRemoteInit.gotRunningStatus, gc.IsNil)
   317  	c.Assert(callbacks.MockRemoteInit.gotAbort, gc.IsNil)
   318  
   319  	newState, err = op.Commit(operation.State{
   320  		Kind: operation.RemoteInit,
   321  		Step: operation.Done,
   322  		Hook: &hook.Info{
   323  			Kind: hooks.LeaderElected,
   324  		},
   325  	})
   326  	c.Assert(err, jc.ErrorIsNil)
   327  	c.Assert(newState, gc.DeepEquals, &operation.State{
   328  		Kind: operation.RemoteInit,
   329  		Step: operation.Pending,
   330  		Hook: &hook.Info{
   331  			Kind: hooks.LeaderElected,
   332  		},
   333  	})
   334  }