github.com/kaisenlinux/docker.io@v0.0.0-20230510090727-ea55db55fac7/swarmkit/agent/exec/controller_stub.go (about)

     1  package exec
     2  
     3  import (
     4  	"context"
     5  	"runtime"
     6  	"strings"
     7  
     8  	"github.com/docker/swarmkit/api"
     9  )
    10  
    11  // StubController implements the Controller interface,
    12  // but allows you to specify behaviors for each of its methods.
    13  type StubController struct {
    14  	Controller
    15  	UpdateFn    func(ctx context.Context, t *api.Task) error
    16  	PrepareFn   func(ctx context.Context) error
    17  	StartFn     func(ctx context.Context) error
    18  	WaitFn      func(ctx context.Context) error
    19  	ShutdownFn  func(ctx context.Context) error
    20  	TerminateFn func(ctx context.Context) error
    21  	RemoveFn    func(ctx context.Context) error
    22  	CloseFn     func() error
    23  	calls       map[string]int
    24  	cstatus     *api.ContainerStatus
    25  }
    26  
    27  // NewStubController returns an initialized StubController
    28  func NewStubController() *StubController {
    29  	return &StubController{
    30  		calls: make(map[string]int),
    31  	}
    32  }
    33  
    34  // If function A calls updateCountsForSelf,
    35  // The callCount[A] value will be incremented
    36  func (sc *StubController) called() {
    37  	pc, _, _, ok := runtime.Caller(1)
    38  	if !ok {
    39  		panic("Failed to find caller of function")
    40  	}
    41  	// longName looks like 'github.com/docker/swarmkit/agent/exec.(*StubController).Prepare:1'
    42  	longName := runtime.FuncForPC(pc).Name()
    43  	parts := strings.Split(longName, ".")
    44  	tail := strings.Split(parts[len(parts)-1], ":")
    45  	sc.calls[tail[0]]++
    46  }
    47  
    48  // Update is part of the Controller interface
    49  func (sc *StubController) Update(ctx context.Context, t *api.Task) error {
    50  	sc.called()
    51  	return sc.UpdateFn(ctx, t)
    52  }
    53  
    54  // Prepare is part of the Controller interface
    55  func (sc *StubController) Prepare(ctx context.Context) error { sc.called(); return sc.PrepareFn(ctx) }
    56  
    57  // Start is part of the Controller interface
    58  func (sc *StubController) Start(ctx context.Context) error { sc.called(); return sc.StartFn(ctx) }
    59  
    60  // Wait is part of the Controller interface
    61  func (sc *StubController) Wait(ctx context.Context) error { sc.called(); return sc.WaitFn(ctx) }
    62  
    63  // Shutdown is part of the Controller interface
    64  func (sc *StubController) Shutdown(ctx context.Context) error { sc.called(); return sc.ShutdownFn(ctx) }
    65  
    66  // Terminate is part of the Controller interface
    67  func (sc *StubController) Terminate(ctx context.Context) error {
    68  	sc.called()
    69  	return sc.TerminateFn(ctx)
    70  }
    71  
    72  // Remove is part of the Controller interface
    73  func (sc *StubController) Remove(ctx context.Context) error { sc.called(); return sc.RemoveFn(ctx) }
    74  
    75  // Close is part of the Controller interface
    76  func (sc *StubController) Close() error { sc.called(); return sc.CloseFn() }