github.com/kaisenlinux/docker.io@v0.0.0-20230510090727-ea55db55fac7/swarmkit/manager/orchestrator/jobs/fakes_test.go (about)

     1  package jobs
     2  
     3  import (
     4  	"context"
     5  	"sync"
     6  	"time"
     7  
     8  	"github.com/docker/swarmkit/api"
     9  	"github.com/docker/swarmkit/manager/orchestrator"
    10  	"github.com/docker/swarmkit/manager/orchestrator/restart"
    11  	"github.com/docker/swarmkit/manager/orchestrator/taskinit"
    12  	"github.com/docker/swarmkit/manager/state/store"
    13  )
    14  
    15  // fakes_test.go is just a file to hold all of the test fakes used with the
    16  // orchestrator, so that they don't pollute the test files
    17  
    18  // fakeReconciler implements the reconciler interface for testing the
    19  // orchestrator.
    20  type fakeReconciler struct {
    21  	sync.Mutex
    22  
    23  	// serviceErrors contains a mapping of ids to errors that should be
    24  	// returned if that ID is passed to reconcileService
    25  	serviceErrors map[string]error
    26  
    27  	// servicesReconciled is a list, in order, of all values this function has
    28  	// been called with, including those that would return errors.
    29  	servicesReconciled []string
    30  
    31  	// servicesRelated is a set of all service IDs of services passed to
    32  	// IsRelatedService
    33  	servicesRelated []string
    34  }
    35  
    36  // ReconcileService implements the reconciler's ReconcileService method, but
    37  // just records what arguments it has been passed, and maybe also returns an
    38  // error if desired.
    39  func (f *fakeReconciler) ReconcileService(id string) error {
    40  	f.Lock()
    41  	defer f.Unlock()
    42  	f.servicesReconciled = append(f.servicesReconciled, id)
    43  	if err, ok := f.serviceErrors[id]; ok {
    44  		return err
    45  	}
    46  	return nil
    47  }
    48  
    49  func (f *fakeReconciler) getServicesReconciled() []string {
    50  	f.Lock()
    51  	defer f.Unlock()
    52  	// we can't just return the slice, because then we'd be accessing it
    53  	// outside of the protection of the mutex anyway. instead, we'll copy its
    54  	// contents. this is fine because this is only the tests, and the slice is
    55  	// almost certainly rather short.
    56  	returnSet := make([]string, len(f.servicesReconciled))
    57  	copy(returnSet, f.servicesReconciled)
    58  	return returnSet
    59  }
    60  
    61  func (f *fakeReconciler) getRelatedServices() []string {
    62  	f.Lock()
    63  	defer f.Unlock()
    64  
    65  	returnSet := make([]string, len(f.servicesRelated))
    66  	copy(returnSet, f.servicesRelated)
    67  	return returnSet
    68  }
    69  
    70  // finally, a few stubs to implement the InitHandler interface, just so types
    71  // work out
    72  
    73  func (f *fakeReconciler) IsRelatedService(s *api.Service) bool {
    74  	f.Lock()
    75  	defer f.Unlock()
    76  	if s != nil {
    77  		f.servicesRelated = append(f.servicesRelated, s.ID)
    78  	}
    79  	return true
    80  }
    81  
    82  func (f *fakeReconciler) FixTask(_ context.Context, _ *store.Batch, _ *api.Task) {}
    83  
    84  func (f *fakeReconciler) SlotTuple(_ *api.Task) orchestrator.SlotTuple {
    85  	return orchestrator.SlotTuple{}
    86  }
    87  
    88  // fakeRestartSupervisor implements the restart.SupervisorInterface interface.
    89  // All of its methods are currently stubs, as it exists mostly to ensure that
    90  // a real restart.Supervisor is not instantiated in the unit tests.
    91  type fakeRestartSupervisor struct{}
    92  
    93  func (f *fakeRestartSupervisor) Restart(_ context.Context, _ store.Tx, _ *api.Cluster, _ *api.Service, _ api.Task) error {
    94  	return nil
    95  }
    96  
    97  func (f *fakeRestartSupervisor) UpdatableTasksInSlot(_ context.Context, _ orchestrator.Slot, _ *api.Service) orchestrator.Slot {
    98  	return orchestrator.Slot{}
    99  }
   100  
   101  func (f *fakeRestartSupervisor) RecordRestartHistory(_ orchestrator.SlotTuple, _ *api.Task) {}
   102  
   103  func (f *fakeRestartSupervisor) DelayStart(_ context.Context, _ store.Tx, _ *api.Task, _ string, _ time.Duration, _ bool) <-chan struct{} {
   104  	return make(chan struct{})
   105  }
   106  
   107  func (f *fakeRestartSupervisor) StartNow(_ store.Tx, _ string) error {
   108  	return nil
   109  }
   110  
   111  func (f *fakeRestartSupervisor) Cancel(_ string) {}
   112  
   113  func (f *fakeRestartSupervisor) CancelAll() {}
   114  
   115  func (f *fakeRestartSupervisor) ClearServiceHistory(_ string) {}
   116  
   117  // fakeCheckTasksFunc is a function to use as checkTasksFunc when unit testing
   118  // the orchestrator. it will create a service with ID fakeCheckTasksFuncCalled
   119  // and call ih.IsRelatedService with that service, allowing a roundabout way
   120  // to ensure it's been called.
   121  func fakeCheckTasksFunc(_ context.Context, _ *store.MemoryStore, _ store.ReadTx, ih taskinit.InitHandler, _ restart.SupervisorInterface) error {
   122  	ih.IsRelatedService(&api.Service{ID: "fakeCheckTasksFuncCalled"})
   123  	return nil
   124  }