From a1527fe8c841d796e28096135b7a21207f8853e4 Mon Sep 17 00:00:00 2001 From: resoluteCoder Date: Wed, 29 Nov 2023 15:04:47 -0600 Subject: [PATCH 01/21] blarg --- pkg/workceptor/command.go | 62 ++++++++++++++++++++++++++++------ pkg/workceptor/json_test.go | 4 +-- pkg/workceptor/python.go | 4 +-- pkg/workceptor/workunitbase.go | 16 +++++++++ 4 files changed, 71 insertions(+), 15 deletions(-) diff --git a/pkg/workceptor/command.go b/pkg/workceptor/command.go index 664a87004..099470802 100644 --- a/pkg/workceptor/command.go +++ b/pkg/workceptor/command.go @@ -11,6 +11,7 @@ import ( "os/signal" "path" "strings" + "sync" "syscall" "time" @@ -18,9 +19,48 @@ import ( "github.com/google/shlex" ) +type BaseWorkUnitForWorkUnit interface { + CancelContext() + Debug(format string, v ...interface{}) + Error(format string, v ...interface{}) + GetBaseStatus() *StatusFileData + GetStatus() StatusFileData + GetStatusLock() *sync.RWMutex + GetWorkceptor() *Workceptor + ID() string + Info(format string, v ...interface{}) + Init(w *Workceptor, unitID string, workType string, fs FileSystemer, watcher WatcherWrapper) + LastUpdateError() error + Load() error + MonitorLocalStatus() + Release(force bool) error + Save() error + SetFromParams(_ map[string]string) error + Status() *StatusFileData + StatusFileName() string + StdoutFileName() string + UnitDir() string + UnredactedStatus() *StatusFileData + UpdateBasicStatus(state int, detail string, stdoutSize int64) + UpdateFullStatus(statusFunc func(*StatusFileData)) + Warning(format string, v ...interface{}) + getStatus() *StatusFileData +} + +// type BaseWorkUnitForCommand interface { +// WorkUnit +// GetStatus() StatusFileData +// MonitorLocalStatus() +// GetStatusLock() *sync.RWMutex +// GetBaseStatus() *StatusFileData +// GetWorkceptor() *Workceptor +// CancelCancel() +// Init(w *Workceptor, unitID string, workType string, fs FileSystemer, watcher WatcherWrapper) +// } + // commandUnit implements the WorkUnit interface for the Receptor command worker plugin. type commandUnit struct { - BaseWorkUnit + BaseWorkUnitForWorkUnit command string baseParams string allowRuntimeParams bool @@ -169,7 +209,7 @@ func (cw *commandUnit) SetFromParams(params map[string]string) error { if cmdParams != "" && !cw.allowRuntimeParams { return fmt.Errorf("extra params provided but not allowed") } - cw.status.ExtraData.(*commandExtraData).Params = combineParams(cw.baseParams, cmdParams) + cw.GetStatus().ExtraData.(*commandExtraData).Params = combineParams(cw.baseParams, cmdParams) return nil } @@ -181,10 +221,10 @@ func (cw *commandUnit) Status() *StatusFileData { // UnredactedStatus returns a copy of the status currently loaded in memory, including secrets. func (cw *commandUnit) UnredactedStatus() *StatusFileData { - cw.statusLock.RLock() - defer cw.statusLock.RUnlock() + cw.GetStatusLock().RLock() + defer cw.GetStatusLock().RUnlock() status := cw.getStatus() - ed, ok := cw.status.ExtraData.(*commandExtraData) + ed, ok := cw.getStatus().ExtraData.(*commandExtraData) if ok { edCopy := *ed status.ExtraData = &edCopy @@ -226,8 +266,8 @@ func (cw *commandUnit) runCommand(cmd *exec.Cmd) error { // Start launches a job with given parameters. func (cw *commandUnit) Start() error { - level := cw.w.nc.GetLogger().GetLogLevel() - levelName, _ := cw.w.nc.GetLogger().LogLevelToName(level) + level := cw.GetWorkceptor().nc.GetLogger().GetLogLevel() + levelName, _ := cw.GetWorkceptor().nc.GetLogger().LogLevelToName(level) cw.UpdateBasicStatus(WorkStatePending, "Launching command runner", 0) // TODO: This is another place where we rely on a pre-built binary for testing. @@ -270,7 +310,7 @@ func (cw *commandUnit) Restart() error { // Cancel stops a running job. func (cw *commandUnit) Cancel() error { - cw.cancel() + cw.CancelContext() status := cw.Status() ced, ok := status.ExtraData.(*commandExtraData) if !ok || ced.Pid <= 0 { @@ -304,7 +344,7 @@ func (cw *commandUnit) Release(force bool) error { return err } - return cw.BaseWorkUnit.Release(force) + return cw.BaseWorkUnitForWorkUnit.Release(force) } // ************************************************************************** @@ -322,7 +362,7 @@ type CommandWorkerCfg struct { func (cfg CommandWorkerCfg) NewWorker(w *Workceptor, unitID string, workType string) WorkUnit { cw := &commandUnit{ - BaseWorkUnit: BaseWorkUnit{ + BaseWorkUnitForWorkUnit: &BaseWorkUnit{ status: StatusFileData{ ExtraData: &commandExtraData{}, }, @@ -331,7 +371,7 @@ func (cfg CommandWorkerCfg) NewWorker(w *Workceptor, unitID string, workType str baseParams: cfg.Params, allowRuntimeParams: cfg.AllowRuntimeParams, } - cw.BaseWorkUnit.Init(w, unitID, workType, FileSystem{}, nil) + cw.BaseWorkUnitForWorkUnit.Init(w, unitID, workType, FileSystem{}, nil) return cw } diff --git a/pkg/workceptor/json_test.go b/pkg/workceptor/json_test.go index de4d1f4da..29a34be28 100644 --- a/pkg/workceptor/json_test.go +++ b/pkg/workceptor/json_test.go @@ -13,7 +13,7 @@ import ( func newCommandWorker(w *Workceptor, unitID string, workType string) WorkUnit { cw := &commandUnit{ - BaseWorkUnit: BaseWorkUnit{ + BaseWorkUnitForWorkUnit: &BaseWorkUnit{ status: StatusFileData{ ExtraData: &commandExtraData{}, }, @@ -22,7 +22,7 @@ func newCommandWorker(w *Workceptor, unitID string, workType string) WorkUnit { baseParams: "foo", allowRuntimeParams: true, } - cw.BaseWorkUnit.Init(w, unitID, workType, FileSystem{}, nil) + cw.BaseWorkUnitForWorkUnit.Init(w, unitID, workType, FileSystem{}, nil) return cw } diff --git a/pkg/workceptor/python.go b/pkg/workceptor/python.go index 98a363fdf..1fb6a1222 100644 --- a/pkg/workceptor/python.go +++ b/pkg/workceptor/python.go @@ -53,7 +53,7 @@ type workPythonCfg struct { func (cfg workPythonCfg) NewWorker(w *Workceptor, unitID string, workType string) WorkUnit { cw := &pythonUnit{ commandUnit: commandUnit{ - BaseWorkUnit: BaseWorkUnit{ + BaseWorkUnitForWorkUnit: &BaseWorkUnit{ status: StatusFileData{ ExtraData: &commandExtraData{}, }, @@ -63,7 +63,7 @@ func (cfg workPythonCfg) NewWorker(w *Workceptor, unitID string, workType string function: cfg.Function, config: cfg.Config, } - cw.BaseWorkUnit.Init(w, unitID, workType, FileSystem{}, nil) + cw.BaseWorkUnitForWorkUnit.Init(w, unitID, workType, FileSystem{}, nil) return cw } diff --git a/pkg/workceptor/workunitbase.go b/pkg/workceptor/workunitbase.go index 291e95978..672a0035e 100644 --- a/pkg/workceptor/workunitbase.go +++ b/pkg/workceptor/workunitbase.go @@ -485,6 +485,22 @@ func (bwu *BaseWorkUnit) CancelContext() { bwu.cancel() } +func (bwu *BaseWorkUnit) GetStatus() StatusFileData { + return bwu.status +} + +func (bwu *BaseWorkUnit) GetStatusLock() *sync.RWMutex { + return bwu.statusLock +} + +func (bwu *BaseWorkUnit) GetBaseStatus() *StatusFileData { + return bwu.getStatus() +} + +func (bwu *BaseWorkUnit) GetWorkceptor() *Workceptor { + return bwu.w +} + // =============================================================================================== // func newUnknownWorker(w *Workceptor, unitID string, workType string) WorkUnit { From ef7b9e94d550a236b3bdad0a5a543017426185d3 Mon Sep 17 00:00:00 2001 From: resoluteCoder Date: Thu, 30 Nov 2023 08:38:40 -0600 Subject: [PATCH 02/21] blarg --- pkg/workceptor/command.go | 32 +++++------ pkg/workceptor/interfaces.go | 2 +- pkg/workceptor/json_test.go | 4 +- pkg/workceptor/kubernetes.go | 3 +- pkg/workceptor/python.go | 2 +- pkg/workceptor/remote_work.go | 91 +++++++++++++++++-------------- pkg/workceptor/workceptor.go | 4 +- pkg/workceptor/workceptor_test.go | 2 +- pkg/workceptor/workunitbase.go | 4 ++ 9 files changed, 75 insertions(+), 69 deletions(-) diff --git a/pkg/workceptor/command.go b/pkg/workceptor/command.go index 099470802..0c6adb0ef 100644 --- a/pkg/workceptor/command.go +++ b/pkg/workceptor/command.go @@ -25,6 +25,7 @@ type BaseWorkUnitForWorkUnit interface { Error(format string, v ...interface{}) GetBaseStatus() *StatusFileData GetStatus() StatusFileData + SetStatusExtraData(*remoteExtraData) GetStatusLock() *sync.RWMutex GetWorkceptor() *Workceptor ID() string @@ -47,17 +48,6 @@ type BaseWorkUnitForWorkUnit interface { getStatus() *StatusFileData } -// type BaseWorkUnitForCommand interface { -// WorkUnit -// GetStatus() StatusFileData -// MonitorLocalStatus() -// GetStatusLock() *sync.RWMutex -// GetBaseStatus() *StatusFileData -// GetWorkceptor() *Workceptor -// CancelCancel() -// Init(w *Workceptor, unitID string, workType string, fs FileSystemer, watcher WatcherWrapper) -// } - // commandUnit implements the WorkUnit interface for the Receptor command worker plugin. type commandUnit struct { BaseWorkUnitForWorkUnit @@ -224,7 +214,7 @@ func (cw *commandUnit) UnredactedStatus() *StatusFileData { cw.GetStatusLock().RLock() defer cw.GetStatusLock().RUnlock() status := cw.getStatus() - ed, ok := cw.getStatus().ExtraData.(*commandExtraData) + ed, ok := cw.GetStatus().ExtraData.(*commandExtraData) if ok { edCopy := *ed status.ExtraData = &edCopy @@ -360,16 +350,20 @@ type CommandWorkerCfg struct { VerifySignature bool `description:"Verify a signed work submission" default:"false"` } -func (cfg CommandWorkerCfg) NewWorker(w *Workceptor, unitID string, workType string) WorkUnit { - cw := &commandUnit{ - BaseWorkUnitForWorkUnit: &BaseWorkUnit{ +func (cfg CommandWorkerCfg) NewWorker(bwu BaseWorkUnitForWorkUnit, w *Workceptor, unitID string, workType string) WorkUnit { + if bwu == nil { + bwu = &BaseWorkUnit{ status: StatusFileData{ ExtraData: &commandExtraData{}, }, - }, - command: cfg.Command, - baseParams: cfg.Params, - allowRuntimeParams: cfg.AllowRuntimeParams, + } + } + + cw := &commandUnit{ + BaseWorkUnitForWorkUnit: bwu, + command: cfg.Command, + baseParams: cfg.Params, + allowRuntimeParams: cfg.AllowRuntimeParams, } cw.BaseWorkUnitForWorkUnit.Init(w, unitID, workType, FileSystem{}, nil) diff --git a/pkg/workceptor/interfaces.go b/pkg/workceptor/interfaces.go index ca7e14ad6..72b738a74 100644 --- a/pkg/workceptor/interfaces.go +++ b/pkg/workceptor/interfaces.go @@ -27,7 +27,7 @@ type WorkerConfig interface { } // NewWorkerFunc represents a factory of WorkUnit instances. -type NewWorkerFunc func(w *Workceptor, unitID string, workType string) WorkUnit +type NewWorkerFunc func(bwu BaseWorkUnitForWorkUnit, w *Workceptor, unitID string, workType string) WorkUnit // StatusFileData is the structure of the JSON data saved to a status file. // This struct should only contain value types, except for ExtraData. diff --git a/pkg/workceptor/json_test.go b/pkg/workceptor/json_test.go index 29a34be28..31f465ea6 100644 --- a/pkg/workceptor/json_test.go +++ b/pkg/workceptor/json_test.go @@ -11,7 +11,7 @@ import ( "github.com/ansible/receptor/pkg/netceptor" ) -func newCommandWorker(w *Workceptor, unitID string, workType string) WorkUnit { +func newCommandWorker(bwu BaseWorkUnitForWorkUnit, w *Workceptor, unitID string, workType string) WorkUnit { cw := &commandUnit{ BaseWorkUnitForWorkUnit: &BaseWorkUnit{ status: StatusFileData{ @@ -57,7 +57,7 @@ func TestWorkceptorJson(t *testing.T) { if err != nil { t.Fatal(err) } - cw2 := newCommandWorker(w, cw.ID(), "command") + cw2 := newCommandWorker(nil, w, cw.ID(), "command") err = cw2.Load() if err != nil { t.Fatal(err) diff --git a/pkg/workceptor/kubernetes.go b/pkg/workceptor/kubernetes.go index 14c279139..66d5ff356 100644 --- a/pkg/workceptor/kubernetes.go +++ b/pkg/workceptor/kubernetes.go @@ -38,7 +38,8 @@ import ( // kubeUnit implements the WorkUnit interface. type kubeUnit struct { - BaseWorkUnit + // BaseWorkUnit + BaseWorkUnitForWorkUnit authMethod string streamMethod string baseParams string diff --git a/pkg/workceptor/python.go b/pkg/workceptor/python.go index 1fb6a1222..b450ac0b5 100644 --- a/pkg/workceptor/python.go +++ b/pkg/workceptor/python.go @@ -50,7 +50,7 @@ type workPythonCfg struct { } // NewWorker is a factory to produce worker instances. -func (cfg workPythonCfg) NewWorker(w *Workceptor, unitID string, workType string) WorkUnit { +func (cfg workPythonCfg) NewWorker(bwu BaseWorkUnitForWorkUnit, w *Workceptor, unitID string, workType string) WorkUnit { cw := &pythonUnit{ commandUnit: commandUnit{ BaseWorkUnitForWorkUnit: &BaseWorkUnit{ diff --git a/pkg/workceptor/remote_work.go b/pkg/workceptor/remote_work.go index 69acfd3a1..d26dd7e4f 100644 --- a/pkg/workceptor/remote_work.go +++ b/pkg/workceptor/remote_work.go @@ -23,7 +23,8 @@ import ( // remoteUnit implements the WorkUnit interface for the Receptor remote worker plugin. type remoteUnit struct { - BaseWorkUnit + // BaseWorkUnit + BaseWorkUnitForWorkUnit topJC *utils.JobContext logger *logger.ReceptorLogger } @@ -51,11 +52,11 @@ func (rw *remoteUnit) connectToRemote(ctx context.Context) (net.Conn, *bufio.Rea if !ok { return nil, nil, fmt.Errorf("remote ExtraData missing") } - tlsConfig, err := rw.w.nc.GetClientTLSConfig(red.TLSClient, red.RemoteNode, netceptor.ExpectedHostnameTypeReceptor) + tlsConfig, err := rw.GetWorkceptor().nc.GetClientTLSConfig(red.TLSClient, red.RemoteNode, netceptor.ExpectedHostnameTypeReceptor) if err != nil { return nil, nil, err } - conn, err := rw.w.nc.DialContext(ctx, red.RemoteNode, "control", tlsConfig) + conn, err := rw.GetWorkceptor().nc.DialContext(ctx, red.RemoteNode, "control", tlsConfig) if err != nil { return nil, nil, err } @@ -85,7 +86,7 @@ func (rw *remoteUnit) getConnection(ctx context.Context) (net.Conn, *bufio.Reade if err == nil { return conn, reader } - rw.w.nc.GetLogger().Debug("Connection to %s failed with error: %s", + rw.GetWorkceptor().nc.GetLogger().Debug("Connection to %s failed with error: %s", rw.Status().ExtraData.(*remoteExtraData).RemoteNode, err) errStr := err.Error() if strings.Contains(errStr, "CRYPTO_ERROR") { @@ -157,7 +158,7 @@ func (rw *remoteUnit) startRemoteUnit(ctx context.Context, conn net.Conn, reader workSubmitCmd["worktype"] = red.RemoteWorkType workSubmitCmd["tlsclient"] = red.TLSClient if red.SignWork { - signature, err := rw.w.createSignature(red.RemoteNode) + signature, err := rw.GetWorkceptor().createSignature(red.RemoteNode) if err != nil { return err } @@ -234,7 +235,7 @@ func (rw *remoteUnit) cancelOrReleaseRemoteUnit(ctx context.Context, conn net.Co workSubmitCmd["subcommand"] = workCmd workSubmitCmd["unitid"] = red.RemoteUnitID if red.SignWork { - signature, err := rw.w.createSignature(red.RemoteNode) + signature, err := rw.GetWorkceptor().createSignature(red.RemoteNode) if err != nil { return err } @@ -269,7 +270,7 @@ func (rw *remoteUnit) monitorRemoteStatus(mw *utils.JobContext, forRelease bool) status := rw.Status() red, ok := status.ExtraData.(*remoteExtraData) if !ok { - rw.w.nc.GetLogger().Error("remote ExtraData missing") + rw.GetWorkceptor().nc.GetLogger().Error("remote ExtraData missing") return } @@ -294,7 +295,7 @@ func (rw *remoteUnit) monitorRemoteStatus(mw *utils.JobContext, forRelease bool) } _, err := conn.Write([]byte(fmt.Sprintf("work status %s\n", remoteUnitID))) if err != nil { - rw.w.nc.GetLogger().Debug("Write error sending to %s: %s\n", remoteUnitID, err) + rw.GetWorkceptor().nc.GetLogger().Debug("Write error sending to %s: %s\n", remoteUnitID, err) _ = conn.(interface{ CloseConnection() error }).CloseConnection() conn = nil @@ -302,7 +303,7 @@ func (rw *remoteUnit) monitorRemoteStatus(mw *utils.JobContext, forRelease bool) } status, err := utils.ReadStringContext(mw, reader, '\n') if err != nil { - rw.w.nc.GetLogger().Debug("Read error reading from %s: %s\n", remoteNode, err) + rw.GetWorkceptor().nc.GetLogger().Debug("Read error reading from %s: %s\n", remoteNode, err) _ = conn.(interface{ CloseConnection() error }).CloseConnection() conn = nil @@ -311,7 +312,7 @@ func (rw *remoteUnit) monitorRemoteStatus(mw *utils.JobContext, forRelease bool) if status[:5] == "ERROR" { if strings.Contains(status, "unknown work unit") { if !forRelease { - rw.w.nc.GetLogger().Debug("Work unit %s on node %s is gone.\n", remoteUnitID, remoteNode) + rw.GetWorkceptor().nc.GetLogger().Debug("Work unit %s on node %s is gone.\n", remoteUnitID, remoteNode) rw.UpdateFullStatus(func(status *StatusFileData) { status.State = WorkStateFailed status.Detail = "Remote work unit is gone" @@ -320,14 +321,14 @@ func (rw *remoteUnit) monitorRemoteStatus(mw *utils.JobContext, forRelease bool) return } - rw.w.nc.GetLogger().Error("Remote error: %s\n", strings.TrimRight(status[6:], "\n")) + rw.GetWorkceptor().nc.GetLogger().Error("Remote error: %s\n", strings.TrimRight(status[6:], "\n")) return } si := StatusFileData{} err = json.Unmarshal([]byte(status), &si) if err != nil { - rw.w.nc.GetLogger().Error("Error unmarshalling JSON: %s\n", status) + rw.GetWorkceptor().nc.GetLogger().Error("Error unmarshalling JSON: %s\n", status) return } @@ -335,7 +336,7 @@ func (rw *remoteUnit) monitorRemoteStatus(mw *utils.JobContext, forRelease bool) if rw.LastUpdateError() != nil { writeStatusFailures++ if writeStatusFailures > 3 { - rw.w.nc.GetLogger().Error("Exceeded retries for updating status file for work unit %s", rw.unitID) + rw.GetWorkceptor().nc.GetLogger().Error("Exceeded retries for updating status file for work unit %s", rw.ID()) return } @@ -343,7 +344,7 @@ func (rw *remoteUnit) monitorRemoteStatus(mw *utils.JobContext, forRelease bool) writeStatusFailures = 0 } if err != nil { - rw.w.nc.GetLogger().Error("Error saving local status file: %s\n", err) + rw.GetWorkceptor().nc.GetLogger().Error("Error saving local status file: %s\n", err) return } @@ -363,18 +364,18 @@ func (rw *remoteUnit) monitorRemoteStdout(mw *utils.JobContext) { status := rw.Status() red, ok := status.ExtraData.(*remoteExtraData) if !ok { - rw.w.nc.GetLogger().Error("remote ExtraData missing") + rw.GetWorkceptor().nc.GetLogger().Error("remote ExtraData missing") return } remoteNode := red.RemoteNode remoteUnitID := red.RemoteUnitID - stdout, err := os.OpenFile(rw.stdoutFileName, os.O_CREATE+os.O_APPEND+os.O_WRONLY, 0o600) + stdout, err := os.OpenFile(rw.StdoutFileName(), os.O_CREATE+os.O_APPEND+os.O_WRONLY, 0o600) if err == nil { err = stdout.Close() } if err != nil { - rw.w.nc.GetLogger().Error("Could not open stdout file %s: %s\n", rw.stdoutFileName, err) + rw.GetWorkceptor().nc.GetLogger().Error("Could not open stdout file %s: %s\n", rw.StdoutFileName(), err) return } @@ -389,7 +390,7 @@ func (rw *remoteUnit) monitorRemoteStdout(mw *utils.JobContext) { } err := rw.Load() if err != nil { - rw.w.nc.GetLogger().Error("Could not read status file %s: %s\n", rw.statusFileName, err) + rw.GetWorkceptor().nc.GetLogger().Error("Could not read status file %s: %s\n", rw.StatusFileName(), err) return } @@ -414,9 +415,9 @@ func (rw *remoteUnit) monitorRemoteStdout(mw *utils.JobContext) { workSubmitCmd["unitid"] = remoteUnitID workSubmitCmd["startpos"] = diskStdoutSize if red.SignWork { - signature, err := rw.w.createSignature(red.RemoteNode) + signature, err := rw.GetWorkceptor().createSignature(red.RemoteNode) if err != nil { - rw.w.nc.GetLogger().Error("could not create signature to get results") + rw.GetWorkceptor().nc.GetLogger().Error("could not create signature to get results") return } @@ -424,31 +425,31 @@ func (rw *remoteUnit) monitorRemoteStdout(mw *utils.JobContext) { } wscBytes, err := json.Marshal(workSubmitCmd) if err != nil { - rw.w.nc.GetLogger().Error("error constructing work results command: %s", err) + rw.GetWorkceptor().nc.GetLogger().Error("error constructing work results command: %s", err) return } wscBytes = append(wscBytes, '\n') _, err = conn.Write(wscBytes) if err != nil { - rw.w.nc.GetLogger().Warning("Write error sending to %s: %s\n", remoteNode, err) + rw.GetWorkceptor().nc.GetLogger().Warning("Write error sending to %s: %s\n", remoteNode, err) continue } status, err := utils.ReadStringContext(mw, reader, '\n') if err != nil { - rw.w.nc.GetLogger().Warning("Read error reading from %s: %s\n", remoteNode, err) + rw.GetWorkceptor().nc.GetLogger().Warning("Read error reading from %s: %s\n", remoteNode, err) continue } if !strings.Contains(status, "Streaming results") { - rw.w.nc.GetLogger().Warning("Remote node %s did not stream results\n", remoteNode) + rw.GetWorkceptor().nc.GetLogger().Warning("Remote node %s did not stream results\n", remoteNode) continue } - stdout, err := os.OpenFile(rw.stdoutFileName, os.O_CREATE+os.O_APPEND+os.O_WRONLY, 0o600) + stdout, err := os.OpenFile(rw.StdoutFileName(), os.O_CREATE+os.O_APPEND+os.O_WRONLY, 0o600) if err != nil { - rw.w.nc.GetLogger().Error("Could not open stdout file %s: %s\n", rw.stdoutFileName, err) + rw.GetWorkceptor().nc.GetLogger().Error("Could not open stdout file %s: %s\n", rw.StdoutFileName(), err) return } @@ -476,7 +477,7 @@ func (rw *remoteUnit) monitorRemoteStdout(mw *utils.JobContext) { } else { errmsg = err.Error() } - rw.w.nc.GetLogger().Warning("Could not copy to stdout file %s: %s\n", rw.stdoutFileName, errmsg) + rw.GetWorkceptor().nc.GetLogger().Warning("Could not copy to stdout file %s: %s\n", rw.StdoutFileName(), errmsg) continue } @@ -501,7 +502,7 @@ func (rw *remoteUnit) monitorRemoteUnit(ctx context.Context, forRelease bool) { // SetFromParams sets the in-memory state from parameters. func (rw *remoteUnit) SetFromParams(params map[string]string) error { for k, v := range params { - rw.status.ExtraData.(*remoteExtraData).RemoteParams[k] = v + rw.GetStatus().ExtraData.(*remoteExtraData).RemoteParams[k] = v } return nil @@ -528,10 +529,10 @@ func (rw *remoteUnit) Status() *StatusFileData { // UnredactedStatus returns a copy of the status currently loaded in memory, including secrets. func (rw *remoteUnit) UnredactedStatus() *StatusFileData { - rw.statusLock.RLock() - defer rw.statusLock.RUnlock() + rw.GetStatusLock().RLock() + defer rw.GetStatusLock().RUnlock() status := rw.getStatus() - ed, ok := rw.status.ExtraData.(*remoteExtraData) + ed, ok := rw.GetStatus().ExtraData.(*remoteExtraData) if ok { edCopy := *ed edCopy.RemoteParams = make(map[string]string) @@ -556,9 +557,9 @@ func (rw *remoteUnit) runAndMonitor(mw *utils.JobContext, forRelease bool, actio go func() { rw.monitorRemoteUnit(ctx, forRelease) if forRelease { - err := rw.BaseWorkUnit.Release(false) + err := rw.BaseWorkUnitForWorkUnit.Release(false) if err != nil { - rw.w.nc.GetLogger().Error("Error releasing unit %s: %s", rw.UnitDir(), err) + rw.GetWorkceptor().nc.GetLogger().Error("Error releasing unit %s: %s", rw.UnitDir(), err) } } mw.WorkerDone() @@ -593,7 +594,7 @@ func (rw *remoteUnit) startOrRestart(start bool) error { if start && red.RemoteStarted { return fmt.Errorf("unit was already started") } - newJobStarted := rw.topJC.NewJob(rw.w.ctx, 1, true) + newJobStarted := rw.topJC.NewJob(rw.GetWorkceptor().ctx, 1, true) if !newJobStarted { return fmt.Errorf("start or monitor process already running") } @@ -648,20 +649,20 @@ func (rw *remoteUnit) cancelOrRelease(release bool, force bool) error { rw.topJC.Cancel() rw.topJC.Wait() if release { - return rw.BaseWorkUnit.Release(true) + return rw.BaseWorkUnitForWorkUnit.Release(true) } rw.UpdateBasicStatus(WorkStateFailed, "Locally Cancelled", 0) return nil } if release && force { - _ = rw.connectAndRun(rw.w.ctx, func(ctx context.Context, conn net.Conn, reader *bufio.Reader) error { + _ = rw.connectAndRun(rw.GetWorkceptor().ctx, func(ctx context.Context, conn net.Conn, reader *bufio.Reader) error { return rw.cancelOrReleaseRemoteUnit(ctx, conn, reader, true) }) - return rw.BaseWorkUnit.Release(true) + return rw.BaseWorkUnitForWorkUnit.Release(true) } - rw.topJC.NewJob(rw.w.ctx, 1, false) + rw.topJC.NewJob(rw.GetWorkceptor().ctx, 1, false) return rw.runAndMonitor(rw.topJC, release, func(ctx context.Context, conn net.Conn, reader *bufio.Reader) error { return rw.cancelOrReleaseRemoteUnit(ctx, conn, reader, release) @@ -678,12 +679,18 @@ func (rw *remoteUnit) Release(force bool) error { return rw.cancelOrRelease(true, force) } -func newRemoteWorker(w *Workceptor, unitID, workType string) WorkUnit { - rw := &remoteUnit{logger: w.nc.GetLogger()} - rw.BaseWorkUnit.Init(w, unitID, workType, FileSystem{}, nil) +func newRemoteWorker(bwu BaseWorkUnitForWorkUnit, w *Workceptor, unitID, workType string) WorkUnit { + if bwu == nil { + bwu = &BaseWorkUnit{} + } + rw := &remoteUnit{ + BaseWorkUnitForWorkUnit: bwu, + logger: w.nc.GetLogger(), + } + rw.BaseWorkUnitForWorkUnit.Init(w, unitID, workType, FileSystem{}, nil) red := &remoteExtraData{} red.RemoteParams = make(map[string]string) - rw.status.ExtraData = red + rw.SetStatusExtraData(red) rw.topJC = &utils.JobContext{} return rw diff --git a/pkg/workceptor/workceptor.go b/pkg/workceptor/workceptor.go index 0057f9a77..6c2708bb7 100644 --- a/pkg/workceptor/workceptor.go +++ b/pkg/workceptor/workceptor.go @@ -243,7 +243,7 @@ func (w *Workceptor) AllocateUnit(workTypeName string, params map[string]string) if err != nil { return nil, err } - worker := wt.newWorkerFunc(w, ident, workTypeName) + worker := wt.newWorkerFunc(nil, w, ident, workTypeName) err = worker.SetFromParams(params) if err == nil { err = worker.Save() @@ -330,7 +330,7 @@ func (w *Workceptor) scanForUnit(unitID string) { w.workTypesLock.RUnlock() var worker WorkUnit if ok { - worker = wt.newWorkerFunc(w, ident, sfd.WorkType) + worker = wt.newWorkerFunc(nil, w, ident, sfd.WorkType) } else { worker = newUnknownWorker(w, ident, sfd.WorkType) } diff --git a/pkg/workceptor/workceptor_test.go b/pkg/workceptor/workceptor_test.go index 44b1d7a24..f74e3a36c 100644 --- a/pkg/workceptor/workceptor_test.go +++ b/pkg/workceptor/workceptor_test.go @@ -23,7 +23,7 @@ func TestAllocateUnit(t *testing.T) { logger := logger.NewReceptorLogger("") mockNetceptor.EXPECT().GetLogger().AnyTimes().Return(logger) - workFunc := func(w *workceptor.Workceptor, unitID string, workType string) workceptor.WorkUnit { + workFunc := func(bwu workceptor.BaseWorkUnitForWorkUnit, w *workceptor.Workceptor, unitID string, workType string) workceptor.WorkUnit { return mockWorkUnit } diff --git a/pkg/workceptor/workunitbase.go b/pkg/workceptor/workunitbase.go index 672a0035e..e2d36c71f 100644 --- a/pkg/workceptor/workunitbase.go +++ b/pkg/workceptor/workunitbase.go @@ -489,6 +489,10 @@ func (bwu *BaseWorkUnit) GetStatus() StatusFileData { return bwu.status } +func (bwu *BaseWorkUnit) SetStatusExtraData(red *remoteExtraData) { + bwu.status.ExtraData = red +} + func (bwu *BaseWorkUnit) GetStatusLock() *sync.RWMutex { return bwu.statusLock } From 5e0a4d4a1c6d2ff754304f812504cd9e862e5eb0 Mon Sep 17 00:00:00 2001 From: resoluteCoder Date: Thu, 30 Nov 2023 12:46:47 -0600 Subject: [PATCH 03/21] another one --- pkg/workceptor/command.go | 29 +- pkg/workceptor/command_test.go | 93 +++++++ pkg/workceptor/controlsvc.go | 2 +- pkg/workceptor/kubernetes.go | 6 +- pkg/workceptor/mock_workceptor/idk.go | 386 ++++++++++++++++++++++++++ pkg/workceptor/remote_work.go | 46 +-- pkg/workceptor/workceptor.go | 2 +- pkg/workceptor/workunitbase.go | 12 +- 8 files changed, 527 insertions(+), 49 deletions(-) create mode 100644 pkg/workceptor/command_test.go create mode 100644 pkg/workceptor/mock_workceptor/idk.go diff --git a/pkg/workceptor/command.go b/pkg/workceptor/command.go index 0c6adb0ef..bba47a4fa 100644 --- a/pkg/workceptor/command.go +++ b/pkg/workceptor/command.go @@ -23,9 +23,9 @@ type BaseWorkUnitForWorkUnit interface { CancelContext() Debug(format string, v ...interface{}) Error(format string, v ...interface{}) - GetBaseStatus() *StatusFileData - GetStatus() StatusFileData - SetStatusExtraData(*remoteExtraData) + GetStatusCopy() StatusFileData + GetStatusWithoutExtraData() *StatusFileData + SetStatusExtraData(*RemoteExtraData) GetStatusLock() *sync.RWMutex GetWorkceptor() *Workceptor ID() string @@ -45,7 +45,6 @@ type BaseWorkUnitForWorkUnit interface { UpdateBasicStatus(state int, detail string, stdoutSize int64) UpdateFullStatus(statusFunc func(*StatusFileData)) Warning(format string, v ...interface{}) - getStatus() *StatusFileData } // commandUnit implements the WorkUnit interface for the Receptor command worker plugin. @@ -57,8 +56,8 @@ type commandUnit struct { done bool } -// commandExtraData is the content of the ExtraData JSON field for a command worker. -type commandExtraData struct { +// CommandExtraData is the content of the ExtraData JSON field for a command worker. +type CommandExtraData struct { Pid int Params string } @@ -90,7 +89,7 @@ func cmdWaiter(cmd *exec.Cmd, doneChan chan bool) { // commandRunner is run in a separate process, to monitor the subprocess and report back metadata. func commandRunner(command string, params string, unitdir string) error { status := StatusFileData{} - status.ExtraData = &commandExtraData{} + status.ExtraData = &CommandExtraData{} statusFilename := path.Join(unitdir, "status") err := status.UpdateBasicStatus(statusFilename, WorkStatePending, "Not started yet", 0) if err != nil { @@ -199,7 +198,7 @@ func (cw *commandUnit) SetFromParams(params map[string]string) error { if cmdParams != "" && !cw.allowRuntimeParams { return fmt.Errorf("extra params provided but not allowed") } - cw.GetStatus().ExtraData.(*commandExtraData).Params = combineParams(cw.baseParams, cmdParams) + cw.GetStatusCopy().ExtraData.(*CommandExtraData).Params = combineParams(cw.baseParams, cmdParams) return nil } @@ -213,8 +212,8 @@ func (cw *commandUnit) Status() *StatusFileData { func (cw *commandUnit) UnredactedStatus() *StatusFileData { cw.GetStatusLock().RLock() defer cw.GetStatusLock().RUnlock() - status := cw.getStatus() - ed, ok := cw.GetStatus().ExtraData.(*commandExtraData) + status := cw.GetStatusWithoutExtraData() + ed, ok := cw.GetStatusCopy().ExtraData.(*CommandExtraData) if ok { edCopy := *ed status.ExtraData = &edCopy @@ -236,9 +235,9 @@ func (cw *commandUnit) runCommand(cmd *exec.Cmd) error { } cw.UpdateFullStatus(func(status *StatusFileData) { if status.ExtraData == nil { - status.ExtraData = &commandExtraData{} + status.ExtraData = &CommandExtraData{} } - status.ExtraData.(*commandExtraData).Pid = cmd.Process.Pid + status.ExtraData.(*CommandExtraData).Pid = cmd.Process.Pid }) doneChan := make(chan bool) go func() { @@ -273,7 +272,7 @@ func (cw *commandUnit) Start() error { "--log-level", levelName, "--command-runner", fmt.Sprintf("command=%s", cw.command), - fmt.Sprintf("params=%s", cw.Status().ExtraData.(*commandExtraData).Params), + fmt.Sprintf("params=%s", cw.Status().ExtraData.(*CommandExtraData).Params), fmt.Sprintf("unitdir=%s", cw.UnitDir())) return cw.runCommand(cmd) @@ -302,7 +301,7 @@ func (cw *commandUnit) Restart() error { func (cw *commandUnit) Cancel() error { cw.CancelContext() status := cw.Status() - ced, ok := status.ExtraData.(*commandExtraData) + ced, ok := status.ExtraData.(*CommandExtraData) if !ok || ced.Pid <= 0 { return nil } @@ -354,7 +353,7 @@ func (cfg CommandWorkerCfg) NewWorker(bwu BaseWorkUnitForWorkUnit, w *Workceptor if bwu == nil { bwu = &BaseWorkUnit{ status: StatusFileData{ - ExtraData: &commandExtraData{}, + ExtraData: &CommandExtraData{}, }, } } diff --git a/pkg/workceptor/command_test.go b/pkg/workceptor/command_test.go new file mode 100644 index 000000000..af64e94b2 --- /dev/null +++ b/pkg/workceptor/command_test.go @@ -0,0 +1,93 @@ +package workceptor_test + +import ( + "context" + "sync" + "testing" + + "github.com/ansible/receptor/pkg/workceptor" + "github.com/ansible/receptor/pkg/workceptor/mock_workceptor" + "github.com/golang/mock/gomock" +) + +func createTestSetup(t *testing.T) (workceptor.WorkUnit, *mock_workceptor.MockBaseWorkUnitForWorkUnit, *mock_workceptor.MockNetceptorForWorkceptor, *gomock.Controller) { + ctrl := gomock.NewController(t) + ctx := context.Background() + + mockBaseWorkUnit := mock_workceptor.NewMockBaseWorkUnitForWorkUnit(ctrl) + mockNetceptor := mock_workceptor.NewMockNetceptorForWorkceptor(ctrl) + mockNetceptor.EXPECT().NodeID().Return("NodeID") + + w, err := workceptor.New(ctx, mockNetceptor, "/tmp") + if err != nil { + t.Errorf("Error while creating Workceptor: %v", err) + } + + // mockBaseWorkUnit.SetStatusExtraData(&workceptor.CommandExtraData{}) + cwc := &workceptor.CommandWorkerCfg{} + mockBaseWorkUnit.EXPECT().Init(w, "", "", workceptor.FileSystem{}, nil) + workUnit := cwc.NewWorker(mockBaseWorkUnit, w, "", "") + return workUnit, mockBaseWorkUnit, mockNetceptor, ctrl +} + +func TestSetFromParams2(t *testing.T) { + wu, _, _, _ := createTestSetup(t) + + paramsTestCases := []struct { + name string + params map[string]string + errorCatch func(error, *testing.T) + }{ + { + name: "one", + params: map[string]string{"": ""}, + errorCatch: func(err error, t *testing.T) { + if err != nil { + t.Error(err) + } + }, + }, + { + name: "two", + params: map[string]string{"params": "param"}, + errorCatch: func(err error, t *testing.T) { + if err == nil { + t.Error(err) + } + }, + }, + } + + for _, testCase := range paramsTestCases { + t.Run(testCase.name, func(t *testing.T) { + err := wu.SetFromParams(testCase.params) + testCase.errorCatch(err, t) + }) + } + +} + +func TestCancel(t *testing.T) { + wu, mockBaseWorkUnit, _, _ := createTestSetup(t) + + mockBaseWorkUnit.EXPECT().CancelContext() + mockBaseWorkUnit.EXPECT().Status().Return(&workceptor.StatusFileData{}) + mockBaseWorkUnit.EXPECT().GetStatusLock().Return(&sync.RWMutex{}).Times(2) + mockBaseWorkUnit.EXPECT().GetStatusWithoutExtraData().Return(&workceptor.StatusFileData{}) + mockBaseWorkUnit.EXPECT().GetStatusCopy().Return(workceptor.StatusFileData{}) + + wu.Cancel() +} + +// func TestUnredactedStatus(t *testing.T) { +// wu := createTestWorkUnit(t) +// wu.UnredactedStatus() +// } + +// func TestStart(t *testing.T) { +// wu := createTestWorkUnit(t) +// err := wu.Start() +// if err != nil { +// t.Error(err) +// } +// } diff --git a/pkg/workceptor/controlsvc.go b/pkg/workceptor/controlsvc.go index 452d4804f..0d032bcd0 100644 --- a/pkg/workceptor/controlsvc.go +++ b/pkg/workceptor/controlsvc.go @@ -211,7 +211,7 @@ func (c *workceptorCommand) processSignature(workType, signature string, connIsU } func getSignWorkFromStatus(status *StatusFileData) bool { - red, ok := status.ExtraData.(*remoteExtraData) + red, ok := status.ExtraData.(*RemoteExtraData) if ok { return red.SignWork } diff --git a/pkg/workceptor/kubernetes.go b/pkg/workceptor/kubernetes.go index 66d5ff356..49bc1e35a 100644 --- a/pkg/workceptor/kubernetes.go +++ b/pkg/workceptor/kubernetes.go @@ -38,8 +38,8 @@ import ( // kubeUnit implements the WorkUnit interface. type kubeUnit struct { - // BaseWorkUnit - BaseWorkUnitForWorkUnit + BaseWorkUnit + // BaseWorkUnitForWorkUnit authMethod string streamMethod string baseParams string @@ -1374,7 +1374,7 @@ type KubeWorkerCfg struct { } // NewWorker is a factory to produce worker instances. -func (cfg KubeWorkerCfg) NewWorker(w *Workceptor, unitID string, workType string) WorkUnit { +func (cfg KubeWorkerCfg) NewWorker(bwu BaseWorkUnitForWorkUnit, w *Workceptor, unitID string, workType string) WorkUnit { ku := &kubeUnit{ BaseWorkUnit: BaseWorkUnit{ status: StatusFileData{ diff --git a/pkg/workceptor/mock_workceptor/idk.go b/pkg/workceptor/mock_workceptor/idk.go new file mode 100644 index 000000000..112510a76 --- /dev/null +++ b/pkg/workceptor/mock_workceptor/idk.go @@ -0,0 +1,386 @@ +// Code generated by MockGen. DO NOT EDIT. +// Source: github.com/ansible/receptor/pkg/workceptor (interfaces: BaseWorkUnitForWorkUnit) + +// Package mock_workceptor is a generated GoMock package. +package mock_workceptor + +import ( + reflect "reflect" + sync "sync" + + workceptor "github.com/ansible/receptor/pkg/workceptor" + gomock "github.com/golang/mock/gomock" +) + +// MockBaseWorkUnitForWorkUnit is a mock of BaseWorkUnitForWorkUnit interface. +type MockBaseWorkUnitForWorkUnit struct { + ctrl *gomock.Controller + recorder *MockBaseWorkUnitForWorkUnitMockRecorder +} + +// MockBaseWorkUnitForWorkUnitMockRecorder is the mock recorder for MockBaseWorkUnitForWorkUnit. +type MockBaseWorkUnitForWorkUnitMockRecorder struct { + mock *MockBaseWorkUnitForWorkUnit +} + +// NewMockBaseWorkUnitForWorkUnit creates a new mock instance. +func NewMockBaseWorkUnitForWorkUnit(ctrl *gomock.Controller) *MockBaseWorkUnitForWorkUnit { + mock := &MockBaseWorkUnitForWorkUnit{ctrl: ctrl} + mock.recorder = &MockBaseWorkUnitForWorkUnitMockRecorder{mock} + return mock +} + +// EXPECT returns an object that allows the caller to indicate expected use. +func (m *MockBaseWorkUnitForWorkUnit) EXPECT() *MockBaseWorkUnitForWorkUnitMockRecorder { + return m.recorder +} + +// CancelContext mocks base method. +func (m *MockBaseWorkUnitForWorkUnit) CancelContext() { + m.ctrl.T.Helper() + m.ctrl.Call(m, "CancelContext") +} + +// CancelContext indicates an expected call of CancelContext. +func (mr *MockBaseWorkUnitForWorkUnitMockRecorder) CancelContext() *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "CancelContext", reflect.TypeOf((*MockBaseWorkUnitForWorkUnit)(nil).CancelContext)) +} + +// Debug mocks base method. +func (m *MockBaseWorkUnitForWorkUnit) Debug(arg0 string, arg1 ...interface{}) { + m.ctrl.T.Helper() + varargs := []interface{}{arg0} + for _, a := range arg1 { + varargs = append(varargs, a) + } + m.ctrl.Call(m, "Debug", varargs...) +} + +// Debug indicates an expected call of Debug. +func (mr *MockBaseWorkUnitForWorkUnitMockRecorder) Debug(arg0 interface{}, arg1 ...interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + varargs := append([]interface{}{arg0}, arg1...) + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Debug", reflect.TypeOf((*MockBaseWorkUnitForWorkUnit)(nil).Debug), varargs...) +} + +// Error mocks base method. +func (m *MockBaseWorkUnitForWorkUnit) Error(arg0 string, arg1 ...interface{}) { + m.ctrl.T.Helper() + varargs := []interface{}{arg0} + for _, a := range arg1 { + varargs = append(varargs, a) + } + m.ctrl.Call(m, "Error", varargs...) +} + +// Error indicates an expected call of Error. +func (mr *MockBaseWorkUnitForWorkUnitMockRecorder) Error(arg0 interface{}, arg1 ...interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + varargs := append([]interface{}{arg0}, arg1...) + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Error", reflect.TypeOf((*MockBaseWorkUnitForWorkUnit)(nil).Error), varargs...) +} + +// GetStatusCopy mocks base method. +func (m *MockBaseWorkUnitForWorkUnit) GetStatusCopy() workceptor.StatusFileData { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "GetStatusCopy") + ret0, _ := ret[0].(workceptor.StatusFileData) + return ret0 +} + +// GetStatusCopy indicates an expected call of GetStatusCopy. +func (mr *MockBaseWorkUnitForWorkUnitMockRecorder) GetStatusCopy() *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetStatusCopy", reflect.TypeOf((*MockBaseWorkUnitForWorkUnit)(nil).GetStatusCopy)) +} + +// GetStatusLock mocks base method. +func (m *MockBaseWorkUnitForWorkUnit) GetStatusLock() *sync.RWMutex { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "GetStatusLock") + ret0, _ := ret[0].(*sync.RWMutex) + return ret0 +} + +// GetStatusLock indicates an expected call of GetStatusLock. +func (mr *MockBaseWorkUnitForWorkUnitMockRecorder) GetStatusLock() *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetStatusLock", reflect.TypeOf((*MockBaseWorkUnitForWorkUnit)(nil).GetStatusLock)) +} + +// GetStatusWithoutExtraData mocks base method. +func (m *MockBaseWorkUnitForWorkUnit) GetStatusWithoutExtraData() *workceptor.StatusFileData { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "GetStatusWithoutExtraData") + ret0, _ := ret[0].(*workceptor.StatusFileData) + return ret0 +} + +// GetStatusWithoutExtraData indicates an expected call of GetStatusWithoutExtraData. +func (mr *MockBaseWorkUnitForWorkUnitMockRecorder) GetStatusWithoutExtraData() *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetStatusWithoutExtraData", reflect.TypeOf((*MockBaseWorkUnitForWorkUnit)(nil).GetStatusWithoutExtraData)) +} + +// GetWorkceptor mocks base method. +func (m *MockBaseWorkUnitForWorkUnit) GetWorkceptor() *workceptor.Workceptor { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "GetWorkceptor") + ret0, _ := ret[0].(*workceptor.Workceptor) + return ret0 +} + +// GetWorkceptor indicates an expected call of GetWorkceptor. +func (mr *MockBaseWorkUnitForWorkUnitMockRecorder) GetWorkceptor() *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetWorkceptor", reflect.TypeOf((*MockBaseWorkUnitForWorkUnit)(nil).GetWorkceptor)) +} + +// ID mocks base method. +func (m *MockBaseWorkUnitForWorkUnit) ID() string { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "ID") + ret0, _ := ret[0].(string) + return ret0 +} + +// ID indicates an expected call of ID. +func (mr *MockBaseWorkUnitForWorkUnitMockRecorder) ID() *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ID", reflect.TypeOf((*MockBaseWorkUnitForWorkUnit)(nil).ID)) +} + +// Info mocks base method. +func (m *MockBaseWorkUnitForWorkUnit) Info(arg0 string, arg1 ...interface{}) { + m.ctrl.T.Helper() + varargs := []interface{}{arg0} + for _, a := range arg1 { + varargs = append(varargs, a) + } + m.ctrl.Call(m, "Info", varargs...) +} + +// Info indicates an expected call of Info. +func (mr *MockBaseWorkUnitForWorkUnitMockRecorder) Info(arg0 interface{}, arg1 ...interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + varargs := append([]interface{}{arg0}, arg1...) + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Info", reflect.TypeOf((*MockBaseWorkUnitForWorkUnit)(nil).Info), varargs...) +} + +// Init mocks base method. +func (m *MockBaseWorkUnitForWorkUnit) Init(arg0 *workceptor.Workceptor, arg1, arg2 string, arg3 workceptor.FileSystemer, arg4 workceptor.WatcherWrapper) { + m.ctrl.T.Helper() + m.ctrl.Call(m, "Init", arg0, arg1, arg2, arg3, arg4) +} + +// Init indicates an expected call of Init. +func (mr *MockBaseWorkUnitForWorkUnitMockRecorder) Init(arg0, arg1, arg2, arg3, arg4 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Init", reflect.TypeOf((*MockBaseWorkUnitForWorkUnit)(nil).Init), arg0, arg1, arg2, arg3, arg4) +} + +// LastUpdateError mocks base method. +func (m *MockBaseWorkUnitForWorkUnit) LastUpdateError() error { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "LastUpdateError") + ret0, _ := ret[0].(error) + return ret0 +} + +// LastUpdateError indicates an expected call of LastUpdateError. +func (mr *MockBaseWorkUnitForWorkUnitMockRecorder) LastUpdateError() *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "LastUpdateError", reflect.TypeOf((*MockBaseWorkUnitForWorkUnit)(nil).LastUpdateError)) +} + +// Load mocks base method. +func (m *MockBaseWorkUnitForWorkUnit) Load() error { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "Load") + ret0, _ := ret[0].(error) + return ret0 +} + +// Load indicates an expected call of Load. +func (mr *MockBaseWorkUnitForWorkUnitMockRecorder) Load() *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Load", reflect.TypeOf((*MockBaseWorkUnitForWorkUnit)(nil).Load)) +} + +// MonitorLocalStatus mocks base method. +func (m *MockBaseWorkUnitForWorkUnit) MonitorLocalStatus() { + m.ctrl.T.Helper() + m.ctrl.Call(m, "MonitorLocalStatus") +} + +// MonitorLocalStatus indicates an expected call of MonitorLocalStatus. +func (mr *MockBaseWorkUnitForWorkUnitMockRecorder) MonitorLocalStatus() *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "MonitorLocalStatus", reflect.TypeOf((*MockBaseWorkUnitForWorkUnit)(nil).MonitorLocalStatus)) +} + +// Release mocks base method. +func (m *MockBaseWorkUnitForWorkUnit) Release(arg0 bool) error { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "Release", arg0) + ret0, _ := ret[0].(error) + return ret0 +} + +// Release indicates an expected call of Release. +func (mr *MockBaseWorkUnitForWorkUnitMockRecorder) Release(arg0 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Release", reflect.TypeOf((*MockBaseWorkUnitForWorkUnit)(nil).Release), arg0) +} + +// Save mocks base method. +func (m *MockBaseWorkUnitForWorkUnit) Save() error { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "Save") + ret0, _ := ret[0].(error) + return ret0 +} + +// Save indicates an expected call of Save. +func (mr *MockBaseWorkUnitForWorkUnitMockRecorder) Save() *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Save", reflect.TypeOf((*MockBaseWorkUnitForWorkUnit)(nil).Save)) +} + +// SetFromParams mocks base method. +func (m *MockBaseWorkUnitForWorkUnit) SetFromParams(arg0 map[string]string) error { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "SetFromParams", arg0) + ret0, _ := ret[0].(error) + return ret0 +} + +// SetFromParams indicates an expected call of SetFromParams. +func (mr *MockBaseWorkUnitForWorkUnitMockRecorder) SetFromParams(arg0 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "SetFromParams", reflect.TypeOf((*MockBaseWorkUnitForWorkUnit)(nil).SetFromParams), arg0) +} + +// SetStatusExtraData mocks base method. +func (m *MockBaseWorkUnitForWorkUnit) SetStatusExtraData(arg0 *workceptor.RemoteExtraData) { + m.ctrl.T.Helper() + m.ctrl.Call(m, "SetStatusExtraData", arg0) +} + +// SetStatusExtraData indicates an expected call of SetStatusExtraData. +func (mr *MockBaseWorkUnitForWorkUnitMockRecorder) SetStatusExtraData(arg0 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "SetStatusExtraData", reflect.TypeOf((*MockBaseWorkUnitForWorkUnit)(nil).SetStatusExtraData), arg0) +} + +// Status mocks base method. +func (m *MockBaseWorkUnitForWorkUnit) Status() *workceptor.StatusFileData { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "Status") + ret0, _ := ret[0].(*workceptor.StatusFileData) + return ret0 +} + +// Status indicates an expected call of Status. +func (mr *MockBaseWorkUnitForWorkUnitMockRecorder) Status() *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Status", reflect.TypeOf((*MockBaseWorkUnitForWorkUnit)(nil).Status)) +} + +// StatusFileName mocks base method. +func (m *MockBaseWorkUnitForWorkUnit) StatusFileName() string { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "StatusFileName") + ret0, _ := ret[0].(string) + return ret0 +} + +// StatusFileName indicates an expected call of StatusFileName. +func (mr *MockBaseWorkUnitForWorkUnitMockRecorder) StatusFileName() *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "StatusFileName", reflect.TypeOf((*MockBaseWorkUnitForWorkUnit)(nil).StatusFileName)) +} + +// StdoutFileName mocks base method. +func (m *MockBaseWorkUnitForWorkUnit) StdoutFileName() string { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "StdoutFileName") + ret0, _ := ret[0].(string) + return ret0 +} + +// StdoutFileName indicates an expected call of StdoutFileName. +func (mr *MockBaseWorkUnitForWorkUnitMockRecorder) StdoutFileName() *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "StdoutFileName", reflect.TypeOf((*MockBaseWorkUnitForWorkUnit)(nil).StdoutFileName)) +} + +// UnitDir mocks base method. +func (m *MockBaseWorkUnitForWorkUnit) UnitDir() string { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "UnitDir") + ret0, _ := ret[0].(string) + return ret0 +} + +// UnitDir indicates an expected call of UnitDir. +func (mr *MockBaseWorkUnitForWorkUnitMockRecorder) UnitDir() *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "UnitDir", reflect.TypeOf((*MockBaseWorkUnitForWorkUnit)(nil).UnitDir)) +} + +// UnredactedStatus mocks base method. +func (m *MockBaseWorkUnitForWorkUnit) UnredactedStatus() *workceptor.StatusFileData { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "UnredactedStatus") + ret0, _ := ret[0].(*workceptor.StatusFileData) + return ret0 +} + +// UnredactedStatus indicates an expected call of UnredactedStatus. +func (mr *MockBaseWorkUnitForWorkUnitMockRecorder) UnredactedStatus() *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "UnredactedStatus", reflect.TypeOf((*MockBaseWorkUnitForWorkUnit)(nil).UnredactedStatus)) +} + +// UpdateBasicStatus mocks base method. +func (m *MockBaseWorkUnitForWorkUnit) UpdateBasicStatus(arg0 int, arg1 string, arg2 int64) { + m.ctrl.T.Helper() + m.ctrl.Call(m, "UpdateBasicStatus", arg0, arg1, arg2) +} + +// UpdateBasicStatus indicates an expected call of UpdateBasicStatus. +func (mr *MockBaseWorkUnitForWorkUnitMockRecorder) UpdateBasicStatus(arg0, arg1, arg2 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "UpdateBasicStatus", reflect.TypeOf((*MockBaseWorkUnitForWorkUnit)(nil).UpdateBasicStatus), arg0, arg1, arg2) +} + +// UpdateFullStatus mocks base method. +func (m *MockBaseWorkUnitForWorkUnit) UpdateFullStatus(arg0 func(*workceptor.StatusFileData)) { + m.ctrl.T.Helper() + m.ctrl.Call(m, "UpdateFullStatus", arg0) +} + +// UpdateFullStatus indicates an expected call of UpdateFullStatus. +func (mr *MockBaseWorkUnitForWorkUnitMockRecorder) UpdateFullStatus(arg0 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "UpdateFullStatus", reflect.TypeOf((*MockBaseWorkUnitForWorkUnit)(nil).UpdateFullStatus), arg0) +} + +// Warning mocks base method. +func (m *MockBaseWorkUnitForWorkUnit) Warning(arg0 string, arg1 ...interface{}) { + m.ctrl.T.Helper() + varargs := []interface{}{arg0} + for _, a := range arg1 { + varargs = append(varargs, a) + } + m.ctrl.Call(m, "Warning", varargs...) +} + +// Warning indicates an expected call of Warning. +func (mr *MockBaseWorkUnitForWorkUnitMockRecorder) Warning(arg0 interface{}, arg1 ...interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + varargs := append([]interface{}{arg0}, arg1...) + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Warning", reflect.TypeOf((*MockBaseWorkUnitForWorkUnit)(nil).Warning), varargs...) +} diff --git a/pkg/workceptor/remote_work.go b/pkg/workceptor/remote_work.go index d26dd7e4f..74b82d0a0 100644 --- a/pkg/workceptor/remote_work.go +++ b/pkg/workceptor/remote_work.go @@ -29,8 +29,8 @@ type remoteUnit struct { logger *logger.ReceptorLogger } -// remoteExtraData is the content of the ExtraData JSON field for a remote work unit. -type remoteExtraData struct { +// RemoteExtraData is the content of the ExtraData JSON field for a remote work unit. +type RemoteExtraData struct { RemoteNode string RemoteWorkType string RemoteParams map[string]string @@ -48,7 +48,7 @@ type actionFunc func(context.Context, net.Conn, *bufio.Reader) error // connectToRemote establishes a control socket connection to a remote node. func (rw *remoteUnit) connectToRemote(ctx context.Context) (net.Conn, *bufio.Reader, error) { status := rw.Status() - red, ok := status.ExtraData.(*remoteExtraData) + red, ok := status.ExtraData.(*RemoteExtraData) if !ok { return nil, nil, fmt.Errorf("remote ExtraData missing") } @@ -87,13 +87,13 @@ func (rw *remoteUnit) getConnection(ctx context.Context) (net.Conn, *bufio.Reade return conn, reader } rw.GetWorkceptor().nc.GetLogger().Debug("Connection to %s failed with error: %s", - rw.Status().ExtraData.(*remoteExtraData).RemoteNode, err) + rw.Status().ExtraData.(*RemoteExtraData).RemoteNode, err) errStr := err.Error() if strings.Contains(errStr, "CRYPTO_ERROR") { shouldExit := false rw.UpdateFullStatus(func(status *StatusFileData) { status.Detail = fmt.Sprintf("TLS error connecting to remote service: %s", errStr) - if !status.ExtraData.(*remoteExtraData).RemoteStarted { + if !status.ExtraData.(*RemoteExtraData).RemoteStarted { shouldExit = true status.State = WorkStateFailed } @@ -147,7 +147,7 @@ func (rw *remoteUnit) getConnectionAndRun(ctx context.Context, firstTimeSync boo // startRemoteUnit makes a single attempt to start a remote unit. func (rw *remoteUnit) startRemoteUnit(ctx context.Context, conn net.Conn, reader *bufio.Reader) error { defer conn.(interface{ CloseConnection() error }).CloseConnection() - red := rw.UnredactedStatus().ExtraData.(*remoteExtraData) + red := rw.UnredactedStatus().ExtraData.(*RemoteExtraData) workSubmitCmd := make(map[string]interface{}) for k, v := range red.RemoteParams { workSubmitCmd[k] = v @@ -186,7 +186,7 @@ func (rw *remoteUnit) startRemoteUnit(ctx context.Context, conn net.Conn, reader } red.RemoteUnitID = string(match[1]) rw.UpdateFullStatus(func(status *StatusFileData) { - ed := status.ExtraData.(*remoteExtraData) + ed := status.ExtraData.(*RemoteExtraData) ed.RemoteUnitID = red.RemoteUnitID }) stdin, err := os.Open(path.Join(rw.UnitDir(), "stdin")) @@ -211,7 +211,7 @@ func (rw *remoteUnit) startRemoteUnit(ctx context.Context, conn net.Conn, reader return fmt.Errorf("error from remote: %s", match[1]) } rw.UpdateFullStatus(func(status *StatusFileData) { - ed := status.ExtraData.(*remoteExtraData) + ed := status.ExtraData.(*RemoteExtraData) ed.RemoteStarted = true }) @@ -223,7 +223,7 @@ func (rw *remoteUnit) cancelOrReleaseRemoteUnit(ctx context.Context, conn net.Co release bool, ) error { defer conn.(interface{ CloseConnection() error }).CloseConnection() - red := rw.Status().ExtraData.(*remoteExtraData) + red := rw.Status().ExtraData.(*RemoteExtraData) var workCmd string if release { workCmd = "release" @@ -268,7 +268,7 @@ func (rw *remoteUnit) monitorRemoteStatus(mw *utils.JobContext, forRelease bool) mw.WorkerDone() }() status := rw.Status() - red, ok := status.ExtraData.(*remoteExtraData) + red, ok := status.ExtraData.(*RemoteExtraData) if !ok { rw.GetWorkceptor().nc.GetLogger().Error("remote ExtraData missing") @@ -362,7 +362,7 @@ func (rw *remoteUnit) monitorRemoteStdout(mw *utils.JobContext) { }() firstTime := true status := rw.Status() - red, ok := status.ExtraData.(*remoteExtraData) + red, ok := status.ExtraData.(*RemoteExtraData) if !ok { rw.GetWorkceptor().nc.GetLogger().Error("remote ExtraData missing") @@ -502,7 +502,7 @@ func (rw *remoteUnit) monitorRemoteUnit(ctx context.Context, forRelease bool) { // SetFromParams sets the in-memory state from parameters. func (rw *remoteUnit) SetFromParams(params map[string]string) error { for k, v := range params { - rw.GetStatus().ExtraData.(*remoteExtraData).RemoteParams[k] = v + rw.GetStatusCopy().ExtraData.(*RemoteExtraData).RemoteParams[k] = v } return nil @@ -511,7 +511,7 @@ func (rw *remoteUnit) SetFromParams(params map[string]string) error { // Status returns a copy of the status currently loaded in memory. func (rw *remoteUnit) Status() *StatusFileData { status := rw.UnredactedStatus() - ed, ok := status.ExtraData.(*remoteExtraData) + ed, ok := status.ExtraData.(*RemoteExtraData) if ok { keysToDelete := make([]string, 0) for k := range ed.RemoteParams { @@ -531,8 +531,8 @@ func (rw *remoteUnit) Status() *StatusFileData { func (rw *remoteUnit) UnredactedStatus() *StatusFileData { rw.GetStatusLock().RLock() defer rw.GetStatusLock().RUnlock() - status := rw.getStatus() - ed, ok := rw.GetStatus().ExtraData.(*remoteExtraData) + status := rw.GetStatusWithoutExtraData() + ed, ok := rw.GetStatusCopy().ExtraData.(*RemoteExtraData) if ok { edCopy := *ed edCopy.RemoteParams = make(map[string]string) @@ -572,12 +572,12 @@ func (rw *remoteUnit) runAndMonitor(mw *utils.JobContext, forRelease bool, actio } func (rw *remoteUnit) setExpiration(mw *utils.JobContext) { - red := rw.Status().ExtraData.(*remoteExtraData) + red := rw.Status().ExtraData.(*RemoteExtraData) dur := time.Until(red.Expiration) select { case <-mw.Done(): case <-time.After(dur): - red := rw.Status().ExtraData.(*remoteExtraData) + red := rw.Status().ExtraData.(*RemoteExtraData) if !red.RemoteStarted { rw.UpdateFullStatus(func(status *StatusFileData) { status.Detail = fmt.Sprintf("Work unit expired on %s", red.Expiration.Format("Mon Jan 2 15:04:05")) @@ -590,7 +590,7 @@ func (rw *remoteUnit) setExpiration(mw *utils.JobContext) { // startOrRestart is a shared implementation of Start() and Restart(). func (rw *remoteUnit) startOrRestart(start bool) error { - red := rw.Status().ExtraData.(*remoteExtraData) + red := rw.Status().ExtraData.(*RemoteExtraData) if start && red.RemoteStarted { return fmt.Errorf("unit was already started") } @@ -625,7 +625,7 @@ func (rw *remoteUnit) Start() error { // Restart resumes monitoring a job after a Receptor restart. func (rw *remoteUnit) Restart() error { - red := rw.Status().ExtraData.(*remoteExtraData) + red := rw.Status().ExtraData.(*RemoteExtraData) if red.RemoteStarted { return rw.startOrRestart(false) } @@ -638,11 +638,11 @@ func (rw *remoteUnit) cancelOrRelease(release bool, force bool) error { // Update the status file that the unit is locally cancelled/released var remoteStarted bool rw.UpdateFullStatus(func(status *StatusFileData) { - status.ExtraData.(*remoteExtraData).LocalCancelled = true + status.ExtraData.(*RemoteExtraData).LocalCancelled = true if release { - status.ExtraData.(*remoteExtraData).LocalReleased = true + status.ExtraData.(*RemoteExtraData).LocalReleased = true } - remoteStarted = status.ExtraData.(*remoteExtraData).RemoteStarted + remoteStarted = status.ExtraData.(*RemoteExtraData).RemoteStarted }) // if remote work has not started, don't attempt to connect to remote if !remoteStarted { @@ -688,7 +688,7 @@ func newRemoteWorker(bwu BaseWorkUnitForWorkUnit, w *Workceptor, unitID, workTyp logger: w.nc.GetLogger(), } rw.BaseWorkUnitForWorkUnit.Init(w, unitID, workType, FileSystem{}, nil) - red := &remoteExtraData{} + red := &RemoteExtraData{} red.RemoteParams = make(map[string]string) rw.SetStatusExtraData(red) rw.topJC = &utils.JobContext{} diff --git a/pkg/workceptor/workceptor.go b/pkg/workceptor/workceptor.go index 6c2708bb7..2292822a9 100644 --- a/pkg/workceptor/workceptor.go +++ b/pkg/workceptor/workceptor.go @@ -295,7 +295,7 @@ func (w *Workceptor) AllocateRemoteUnit(remoteNode, remoteWorkType, tlsClient, t expiration = time.Time{} } rw.UpdateFullStatus(func(status *StatusFileData) { - ed := status.ExtraData.(*remoteExtraData) + ed := status.ExtraData.(*RemoteExtraData) ed.RemoteNode = remoteNode ed.RemoteWorkType = remoteWorkType ed.TLSClient = tlsClient diff --git a/pkg/workceptor/workunitbase.go b/pkg/workceptor/workunitbase.go index e2d36c71f..59c1e0ea2 100644 --- a/pkg/workceptor/workunitbase.go +++ b/pkg/workceptor/workunitbase.go @@ -485,11 +485,15 @@ func (bwu *BaseWorkUnit) CancelContext() { bwu.cancel() } -func (bwu *BaseWorkUnit) GetStatus() StatusFileData { +func (bwu *BaseWorkUnit) GetStatusCopy() StatusFileData { return bwu.status } -func (bwu *BaseWorkUnit) SetStatusExtraData(red *remoteExtraData) { +func (bwu *BaseWorkUnit) GetStatusWithoutExtraData() *StatusFileData { + return bwu.getStatus() +} + +func (bwu *BaseWorkUnit) SetStatusExtraData(red *RemoteExtraData) { bwu.status.ExtraData = red } @@ -497,10 +501,6 @@ func (bwu *BaseWorkUnit) GetStatusLock() *sync.RWMutex { return bwu.statusLock } -func (bwu *BaseWorkUnit) GetBaseStatus() *StatusFileData { - return bwu.getStatus() -} - func (bwu *BaseWorkUnit) GetWorkceptor() *Workceptor { return bwu.w } From fda0b595782cbe827a7159d886132e4d1dbbada5 Mon Sep 17 00:00:00 2001 From: resoluteCoder Date: Thu, 30 Nov 2023 18:53:28 -0600 Subject: [PATCH 04/21] yup --- pkg/workceptor/command.go | 2 +- pkg/workceptor/command_test.go | 114 ++++++++++++++++++++++++-- pkg/workceptor/json_test.go | 6 +- pkg/workceptor/mock_workceptor/idk.go | 2 +- pkg/workceptor/python.go | 4 +- pkg/workceptor/workunitbase.go | 4 +- 6 files changed, 116 insertions(+), 16 deletions(-) diff --git a/pkg/workceptor/command.go b/pkg/workceptor/command.go index bba47a4fa..7ad619a3b 100644 --- a/pkg/workceptor/command.go +++ b/pkg/workceptor/command.go @@ -25,7 +25,7 @@ type BaseWorkUnitForWorkUnit interface { Error(format string, v ...interface{}) GetStatusCopy() StatusFileData GetStatusWithoutExtraData() *StatusFileData - SetStatusExtraData(*RemoteExtraData) + SetStatusExtraData(interface{}) GetStatusLock() *sync.RWMutex GetWorkceptor() *Workceptor ID() string diff --git a/pkg/workceptor/command_test.go b/pkg/workceptor/command_test.go index af64e94b2..56a5578c9 100644 --- a/pkg/workceptor/command_test.go +++ b/pkg/workceptor/command_test.go @@ -2,8 +2,10 @@ package workceptor_test import ( "context" + "os/exec" "sync" "testing" + "time" "github.com/ansible/receptor/pkg/workceptor" "github.com/ansible/receptor/pkg/workceptor/mock_workceptor" @@ -23,7 +25,6 @@ func createTestSetup(t *testing.T) (workceptor.WorkUnit, *mock_workceptor.MockBa t.Errorf("Error while creating Workceptor: %v", err) } - // mockBaseWorkUnit.SetStatusExtraData(&workceptor.CommandExtraData{}) cwc := &workceptor.CommandWorkerCfg{} mockBaseWorkUnit.EXPECT().Init(w, "", "", workceptor.FileSystem{}, nil) workUnit := cwc.NewWorker(mockBaseWorkUnit, w, "", "") @@ -70,13 +71,106 @@ func TestSetFromParams2(t *testing.T) { func TestCancel(t *testing.T) { wu, mockBaseWorkUnit, _, _ := createTestSetup(t) - mockBaseWorkUnit.EXPECT().CancelContext() - mockBaseWorkUnit.EXPECT().Status().Return(&workceptor.StatusFileData{}) - mockBaseWorkUnit.EXPECT().GetStatusLock().Return(&sync.RWMutex{}).Times(2) - mockBaseWorkUnit.EXPECT().GetStatusWithoutExtraData().Return(&workceptor.StatusFileData{}) - mockBaseWorkUnit.EXPECT().GetStatusCopy().Return(workceptor.StatusFileData{}) + paramsTestCases := []struct { + name string + expectedCalls func() + errorCatch func(error, *testing.T) + }{ + { + name: "return no error 1", + expectedCalls: func() { + mockBaseWorkUnit.EXPECT().CancelContext() + mockBaseWorkUnit.EXPECT().GetStatusLock().Return(&sync.RWMutex{}).Times(2) + mockBaseWorkUnit.EXPECT().GetStatusWithoutExtraData().Return(&workceptor.StatusFileData{}) + mockBaseWorkUnit.EXPECT().GetStatusCopy().Return(workceptor.StatusFileData{ + ExtraData: &workceptor.CommandExtraData{}, + }) + + }, + errorCatch: func(err error, t *testing.T) { + if err != nil { + t.Error(err) + } + }, + }, + { + name: "return err 2", + expectedCalls: func() { + mockBaseWorkUnit.EXPECT().CancelContext() + mockBaseWorkUnit.EXPECT().GetStatusLock().Return(&sync.RWMutex{}).Times(2) + mockBaseWorkUnit.EXPECT().GetStatusWithoutExtraData().Return(&workceptor.StatusFileData{}) + mockBaseWorkUnit.EXPECT().GetStatusCopy().Return(workceptor.StatusFileData{ + ExtraData: &workceptor.CommandExtraData{ + Pid: 1, + }, + }) + }, + errorCatch: func(err error, t *testing.T) { + if err == nil { + t.Error(err) + } + }, + }, + { + name: "return nil already finished", + expectedCalls: func() { + mockBaseWorkUnit.EXPECT().CancelContext() + mockBaseWorkUnit.EXPECT().GetStatusLock().Return(&sync.RWMutex{}).Times(2) + mockBaseWorkUnit.EXPECT().GetStatusWithoutExtraData().Return(&workceptor.StatusFileData{}) + + c := exec.Command("ls", "/tmp") + go func() { + c.Run() + }() + time.Sleep(200 * time.Millisecond) + + mockBaseWorkUnit.EXPECT().GetStatusCopy().Return(workceptor.StatusFileData{ + ExtraData: &workceptor.CommandExtraData{ + Pid: c.Process.Pid, + }, + }) + }, + errorCatch: func(err error, t *testing.T) { + if err == nil { + t.Error(err) + } + }, + }, + // { + // name: "happy day", + // expectedCalls: func() { + // mockBaseWorkUnit.EXPECT().CancelContext() + // mockBaseWorkUnit.EXPECT().GetStatusLock().Return(&sync.RWMutex{}).Times(2) + // mockBaseWorkUnit.EXPECT().GetStatusWithoutExtraData().Return(&workceptor.StatusFileData{}) + + // c := exec.Command("sleep", "30") + // go func() { + // c.Run() + // }() + // time.Sleep(200 * time.Millisecond) + + // mockBaseWorkUnit.EXPECT().GetStatusCopy().Return(workceptor.StatusFileData{ + // ExtraData: &workceptor.CommandExtraData{ + // Pid: c.Process.Pid, + // }, + // }) + // }, + // errorCatch: func(err error, t *testing.T) { + // if err == nil { + // t.Error(err) + // } + // }, + // }, + } + + for _, testCase := range paramsTestCases[len(paramsTestCases)-1:] { + t.Run(testCase.name, func(t *testing.T) { + testCase.expectedCalls() + err := wu.Cancel() + testCase.errorCatch(err, t) + }) + } - wu.Cancel() } // func TestUnredactedStatus(t *testing.T) { @@ -91,3 +185,9 @@ func TestCancel(t *testing.T) { // t.Error(err) // } // } + +// mockBaseWorkUnit.EXPECT().GetStatusLock().Return(&sync.RWMutex{}).Times(2) +// mockBaseWorkUnit.EXPECT().GetStatusWithoutExtraData().Return(&workceptor.StatusFileData{}) +// mockBaseWorkUnit.EXPECT().GetStatusCopy().Return(workceptor.StatusFileData{ +// ExtraData: &workceptor.CommandExtraData{}, +// }) diff --git a/pkg/workceptor/json_test.go b/pkg/workceptor/json_test.go index 31f465ea6..e1d15d067 100644 --- a/pkg/workceptor/json_test.go +++ b/pkg/workceptor/json_test.go @@ -15,7 +15,7 @@ func newCommandWorker(bwu BaseWorkUnitForWorkUnit, w *Workceptor, unitID string, cw := &commandUnit{ BaseWorkUnitForWorkUnit: &BaseWorkUnit{ status: StatusFileData{ - ExtraData: &commandExtraData{}, + ExtraData: &CommandExtraData{}, }, }, command: "echo", @@ -47,7 +47,7 @@ func TestWorkceptorJson(t *testing.T) { t.Fatal(err) } cw.UpdateFullStatus(func(status *StatusFileData) { - ed, ok := status.ExtraData.(*commandExtraData) + ed, ok := status.ExtraData.(*CommandExtraData) if !ok { t.Fatal("ExtraData type assertion failed") } @@ -62,7 +62,7 @@ func TestWorkceptorJson(t *testing.T) { if err != nil { t.Fatal(err) } - ed2, ok := cw2.Status().ExtraData.(*commandExtraData) + ed2, ok := cw2.Status().ExtraData.(*CommandExtraData) if !ok { t.Fatal("ExtraData type assertion failed") } diff --git a/pkg/workceptor/mock_workceptor/idk.go b/pkg/workceptor/mock_workceptor/idk.go index 112510a76..024775b3c 100644 --- a/pkg/workceptor/mock_workceptor/idk.go +++ b/pkg/workceptor/mock_workceptor/idk.go @@ -263,7 +263,7 @@ func (mr *MockBaseWorkUnitForWorkUnitMockRecorder) SetFromParams(arg0 interface{ } // SetStatusExtraData mocks base method. -func (m *MockBaseWorkUnitForWorkUnit) SetStatusExtraData(arg0 *workceptor.RemoteExtraData) { +func (m *MockBaseWorkUnitForWorkUnit) SetStatusExtraData(arg0 interface{}) { m.ctrl.T.Helper() m.ctrl.Call(m, "SetStatusExtraData", arg0) } diff --git a/pkg/workceptor/python.go b/pkg/workceptor/python.go index b450ac0b5..520d740ef 100644 --- a/pkg/workceptor/python.go +++ b/pkg/workceptor/python.go @@ -26,7 +26,7 @@ func (pw *pythonUnit) Start() error { for k, v := range pw.config { config[k] = v } - config["params"] = pw.Status().ExtraData.(*commandExtraData).Params + config["params"] = pw.Status().ExtraData.(*CommandExtraData).Params configJSON, err := json.Marshal(config) if err != nil { return err @@ -55,7 +55,7 @@ func (cfg workPythonCfg) NewWorker(bwu BaseWorkUnitForWorkUnit, w *Workceptor, u commandUnit: commandUnit{ BaseWorkUnitForWorkUnit: &BaseWorkUnit{ status: StatusFileData{ - ExtraData: &commandExtraData{}, + ExtraData: &CommandExtraData{}, }, }, }, diff --git a/pkg/workceptor/workunitbase.go b/pkg/workceptor/workunitbase.go index 59c1e0ea2..f7707f5d6 100644 --- a/pkg/workceptor/workunitbase.go +++ b/pkg/workceptor/workunitbase.go @@ -493,8 +493,8 @@ func (bwu *BaseWorkUnit) GetStatusWithoutExtraData() *StatusFileData { return bwu.getStatus() } -func (bwu *BaseWorkUnit) SetStatusExtraData(red *RemoteExtraData) { - bwu.status.ExtraData = red +func (bwu *BaseWorkUnit) SetStatusExtraData(ed interface{}) { + bwu.status.ExtraData = ed } func (bwu *BaseWorkUnit) GetStatusLock() *sync.RWMutex { From 4b9699afd3a72f6f2687336dce121e45ed898760 Mon Sep 17 00:00:00 2001 From: resoluteCoder Date: Fri, 1 Dec 2023 09:28:59 -0600 Subject: [PATCH 05/21] idk --- pkg/workceptor/command_test.go | 74 +++++++++++++--------------------- 1 file changed, 28 insertions(+), 46 deletions(-) diff --git a/pkg/workceptor/command_test.go b/pkg/workceptor/command_test.go index 56a5578c9..de12246ff 100644 --- a/pkg/workceptor/command_test.go +++ b/pkg/workceptor/command_test.go @@ -129,41 +129,42 @@ func TestCancel(t *testing.T) { Pid: c.Process.Pid, }, }) + mockBaseWorkUnit.EXPECT().UpdateBasicStatus(gomock.Any(), gomock.Any(), gomock.Any()) }, errorCatch: func(err error, t *testing.T) { - if err == nil { + if err != nil { + t.Error(err) + } + }, + }, + { + name: "happy day", + expectedCalls: func() { + mockBaseWorkUnit.EXPECT().CancelContext() + mockBaseWorkUnit.EXPECT().GetStatusLock().Return(&sync.RWMutex{}).Times(2) + mockBaseWorkUnit.EXPECT().GetStatusWithoutExtraData().Return(&workceptor.StatusFileData{}) + + c := exec.Command("sleep", "30") + go func() { + c.Run() + }() + time.Sleep(200 * time.Millisecond) + + mockBaseWorkUnit.EXPECT().GetStatusCopy().Return(workceptor.StatusFileData{ + ExtraData: &workceptor.CommandExtraData{ + Pid: c.Process.Pid, + }, + }) + }, + errorCatch: func(err error, t *testing.T) { + if err != nil { t.Error(err) } }, }, - // { - // name: "happy day", - // expectedCalls: func() { - // mockBaseWorkUnit.EXPECT().CancelContext() - // mockBaseWorkUnit.EXPECT().GetStatusLock().Return(&sync.RWMutex{}).Times(2) - // mockBaseWorkUnit.EXPECT().GetStatusWithoutExtraData().Return(&workceptor.StatusFileData{}) - - // c := exec.Command("sleep", "30") - // go func() { - // c.Run() - // }() - // time.Sleep(200 * time.Millisecond) - - // mockBaseWorkUnit.EXPECT().GetStatusCopy().Return(workceptor.StatusFileData{ - // ExtraData: &workceptor.CommandExtraData{ - // Pid: c.Process.Pid, - // }, - // }) - // }, - // errorCatch: func(err error, t *testing.T) { - // if err == nil { - // t.Error(err) - // } - // }, - // }, } - for _, testCase := range paramsTestCases[len(paramsTestCases)-1:] { + for _, testCase := range paramsTestCases { t.Run(testCase.name, func(t *testing.T) { testCase.expectedCalls() err := wu.Cancel() @@ -172,22 +173,3 @@ func TestCancel(t *testing.T) { } } - -// func TestUnredactedStatus(t *testing.T) { -// wu := createTestWorkUnit(t) -// wu.UnredactedStatus() -// } - -// func TestStart(t *testing.T) { -// wu := createTestWorkUnit(t) -// err := wu.Start() -// if err != nil { -// t.Error(err) -// } -// } - -// mockBaseWorkUnit.EXPECT().GetStatusLock().Return(&sync.RWMutex{}).Times(2) -// mockBaseWorkUnit.EXPECT().GetStatusWithoutExtraData().Return(&workceptor.StatusFileData{}) -// mockBaseWorkUnit.EXPECT().GetStatusCopy().Return(workceptor.StatusFileData{ -// ExtraData: &workceptor.CommandExtraData{}, -// }) From 60490881285181df36e53657b682de4138c1db7e Mon Sep 17 00:00:00 2001 From: resoluteCoder Date: Mon, 4 Dec 2023 11:11:55 -0600 Subject: [PATCH 06/21] yar --- Makefile | 2 +- pkg/workceptor/command_test.go | 45 +++++++++++++++++++++++++--------- 2 files changed, 34 insertions(+), 13 deletions(-) diff --git a/Makefile b/Makefile index 4bdd35028..5cf18447b 100644 --- a/Makefile +++ b/Makefile @@ -102,7 +102,7 @@ build-all: GOOS=windows go build -o receptor.exe ./cmd/receptor-cl && \ GOOS=darwin go build -o receptor.app ./cmd/receptor-cl && \ go build example/*.go && \ - go build -o receptor --tags no_backends,no_services,no_tls_config,no_workceptor,no_cert_auth ./cmd/receptor-cl && \ + go build -o receptor --tags no_backends,no_services,no_tls_config ./cmd/receptor-cl && \ go build -o receptor ./cmd/receptor-cl DIST := receptor_$(shell echo '$(VERSION)' | sed 's/^v//')_$(GOOS)_$(GOARCH) diff --git a/pkg/workceptor/command_test.go b/pkg/workceptor/command_test.go index de12246ff..dadf59350 100644 --- a/pkg/workceptor/command_test.go +++ b/pkg/workceptor/command_test.go @@ -2,6 +2,7 @@ package workceptor_test import ( "context" + "fmt" "os/exec" "sync" "testing" @@ -32,16 +33,22 @@ func createTestSetup(t *testing.T) (workceptor.WorkUnit, *mock_workceptor.MockBa } func TestSetFromParams2(t *testing.T) { - wu, _, _, _ := createTestSetup(t) + wu, mockBaseWorkUnit, _, _ := createTestSetup(t) paramsTestCases := []struct { - name string - params map[string]string - errorCatch func(error, *testing.T) + name string + params map[string]string + expectedCalls func() + errorCatch func(error, *testing.T) }{ { name: "one", params: map[string]string{"": ""}, + expectedCalls: func() { + mockBaseWorkUnit.EXPECT().GetStatusCopy().Return(workceptor.StatusFileData{ + ExtraData: &workceptor.CommandExtraData{}, + }) + }, errorCatch: func(err error, t *testing.T) { if err != nil { t.Error(err) @@ -51,6 +58,9 @@ func TestSetFromParams2(t *testing.T) { { name: "two", params: map[string]string{"params": "param"}, + expectedCalls: func() { + + }, errorCatch: func(err error, t *testing.T) { if err == nil { t.Error(err) @@ -61,6 +71,7 @@ func TestSetFromParams2(t *testing.T) { for _, testCase := range paramsTestCases { t.Run(testCase.name, func(t *testing.T) { + testCase.expectedCalls() err := wu.SetFromParams(testCase.params) testCase.errorCatch(err, t) }) @@ -119,17 +130,20 @@ func TestCancel(t *testing.T) { mockBaseWorkUnit.EXPECT().GetStatusWithoutExtraData().Return(&workceptor.StatusFileData{}) c := exec.Command("ls", "/tmp") - go func() { + processPid := make(chan int) + + go func(c *exec.Cmd, processPid chan int) { c.Run() - }() + processPid <- c.Process.Pid + }(c, processPid) + time.Sleep(200 * time.Millisecond) mockBaseWorkUnit.EXPECT().GetStatusCopy().Return(workceptor.StatusFileData{ ExtraData: &workceptor.CommandExtraData{ - Pid: c.Process.Pid, + Pid: <-processPid, }, }) - mockBaseWorkUnit.EXPECT().UpdateBasicStatus(gomock.Any(), gomock.Any(), gomock.Any()) }, errorCatch: func(err error, t *testing.T) { if err != nil { @@ -143,16 +157,23 @@ func TestCancel(t *testing.T) { mockBaseWorkUnit.EXPECT().CancelContext() mockBaseWorkUnit.EXPECT().GetStatusLock().Return(&sync.RWMutex{}).Times(2) mockBaseWorkUnit.EXPECT().GetStatusWithoutExtraData().Return(&workceptor.StatusFileData{}) + mockBaseWorkUnit.EXPECT().UpdateBasicStatus(gomock.Any(), gomock.Any(), gomock.Any()) c := exec.Command("sleep", "30") - go func() { - c.Run() - }() + processPid := make(chan int) + + go func(c *exec.Cmd, processPid chan int) { + err := c.Start() + if err != nil { + fmt.Println(err) + } + processPid <- c.Process.Pid + }(c, processPid) time.Sleep(200 * time.Millisecond) mockBaseWorkUnit.EXPECT().GetStatusCopy().Return(workceptor.StatusFileData{ ExtraData: &workceptor.CommandExtraData{ - Pid: c.Process.Pid, + Pid: <-processPid, }, }) }, From 11a3d4484523256dc4bec943a146f43e9039736e Mon Sep 17 00:00:00 2001 From: resoluteCoder Date: Mon, 4 Dec 2023 13:03:32 -0600 Subject: [PATCH 07/21] another one --- pkg/workceptor/interfaces.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/workceptor/interfaces.go b/pkg/workceptor/interfaces.go index 72b738a74..3b16bc251 100644 --- a/pkg/workceptor/interfaces.go +++ b/pkg/workceptor/interfaces.go @@ -23,7 +23,7 @@ type WorkUnit interface { type WorkerConfig interface { GetWorkType() string GetVerifySignature() bool - NewWorker(w *Workceptor, unitID string, workType string) WorkUnit + NewWorker(bwu BaseWorkUnitForWorkUnit, w *Workceptor, unitID string, workType string) WorkUnit } // NewWorkerFunc represents a factory of WorkUnit instances. From a8d290d56cd96db3de6aefc86f32dbc9bd548cc4 Mon Sep 17 00:00:00 2001 From: resoluteCoder Date: Thu, 7 Dec 2023 13:59:48 -0600 Subject: [PATCH 08/21] who let the dogs out? --- pkg/workceptor/command_test.go | 145 +++++++++++++++++++++++++++++++++ 1 file changed, 145 insertions(+) diff --git a/pkg/workceptor/command_test.go b/pkg/workceptor/command_test.go index dadf59350..e3c2b331d 100644 --- a/pkg/workceptor/command_test.go +++ b/pkg/workceptor/command_test.go @@ -2,6 +2,7 @@ package workceptor_test import ( "context" + "errors" "fmt" "os/exec" "sync" @@ -79,6 +80,100 @@ func TestSetFromParams2(t *testing.T) { } +func TestUnredactedStatus(t *testing.T) { + wu, mockBaseWorkUnit, _, _ := createTestSetup(t) + statusLock := &sync.RWMutex{} + mockBaseWorkUnit.EXPECT().GetStatusLock().Return(statusLock).Times(2) + mockBaseWorkUnit.EXPECT().GetStatusWithoutExtraData().Return(&workceptor.StatusFileData{}) + mockBaseWorkUnit.EXPECT().GetStatusCopy().Return(workceptor.StatusFileData{ + ExtraData: &workceptor.CommandExtraData{}, + }) + + wu.UnredactedStatus() +} + +// func TestStart(t *testing.T) { +// wu, mockBaseWorkUnit, _, _ := createTestSetup(t) +// // w, err := workceptor.New(context.Background(), mockNetceptor, "") +// // if err != nil { +// // fmt.Println(err) +// // } + +// stuff := mockBaseWorkUnit.EXPECT().GetWorkceptor().Return() +// // mockNetceptor.EXPECT().GetLogger() + +// wu.Start() + +// } + +func TestRestart(t *testing.T) { + wu, mockBaseWorkUnit, _, _ := createTestSetup(t) + + restartTestCases := []struct { + name string + expectedCalls func() + errorCatch func(error, *testing.T) + }{ + { + name: "return error 1", + expectedCalls: func() { + mockBaseWorkUnit.EXPECT().Load().Return(errors.New("terminated")) + }, + errorCatch: func(err error, t *testing.T) { + if err.Error() != "terminated" { + t.Error(err) + } + }, + }, + { + name: "return nil 1", + expectedCalls: func() { + statusFile := &workceptor.StatusFileData{State: 2} + mockBaseWorkUnit.EXPECT().Load().Return(nil) + statusLock := &sync.RWMutex{} + mockBaseWorkUnit.EXPECT().GetStatusLock().Return(statusLock).Times(2) + mockBaseWorkUnit.EXPECT().GetStatusWithoutExtraData().Return(statusFile) + mockBaseWorkUnit.EXPECT().GetStatusCopy().Return(workceptor.StatusFileData{ + ExtraData: &workceptor.CommandExtraData{}, + }) + }, + errorCatch: func(err error, t *testing.T) { + if err != nil { + t.Error(err) + } + }, + }, + { + name: "return nil 2", + expectedCalls: func() { + statusFile := &workceptor.StatusFileData{State: 0} + mockBaseWorkUnit.EXPECT().Load().Return(nil) + statusLock := &sync.RWMutex{} + mockBaseWorkUnit.EXPECT().GetStatusLock().Return(statusLock).Times(2) + mockBaseWorkUnit.EXPECT().GetStatusWithoutExtraData().Return(statusFile) + mockBaseWorkUnit.EXPECT().GetStatusCopy().Return(workceptor.StatusFileData{ + ExtraData: &workceptor.CommandExtraData{}, + }) + mockBaseWorkUnit.EXPECT().UpdateBasicStatus(gomock.Any(), gomock.Any(), gomock.Any()) + mockBaseWorkUnit.EXPECT().UnitDir() + }, + errorCatch: func(err error, t *testing.T) { + if err != nil { + t.Error(err) + } + }, + }, + } + + for _, testCase := range restartTestCases { + t.Run(testCase.name, func(t *testing.T) { + testCase.expectedCalls() + err := wu.Restart() + testCase.errorCatch(err, t) + }) + } +} + func TestCancel(t *testing.T) { wu, mockBaseWorkUnit, _, _ := createTestSetup(t) @@ -194,3 +289,53 @@ func TestCancel(t *testing.T) { } } + +func TestRelease(t *testing.T) { + wu, mockBaseWorkUnit, _, _ := createTestSetup(t) + + releaseTestCases := []struct { + name string + expectedCalls func() + errorCatch func(error, *testing.T) + force bool + }{ + { + name: "error", + expectedCalls: func() {}, + errorCatch: func(err error, t *testing.T) { + if err == nil { + t.Error(err) + } + }, + force: false, + }, + { + name: "happy day", + expectedCalls: func() { + mockBaseWorkUnit.EXPECT().Release(gomock.Any()) + }, + errorCatch: func(err error, t *testing.T) { + if err != nil { + t.Error(err) + } + }, + force: true, + }, + } + for _, testCase := range releaseTestCases { + t.Run(testCase.name, func(t *testing.T) { + mockBaseWorkUnit.EXPECT().CancelContext() + mockBaseWorkUnit.EXPECT().GetStatusLock().Return(&sync.RWMutex{}).Times(2) + mockBaseWorkUnit.EXPECT().GetStatusWithoutExtraData().Return(&workceptor.StatusFileData{}) + mockBaseWorkUnit.EXPECT().GetStatusCopy().Return(workceptor.StatusFileData{ + ExtraData: &workceptor.CommandExtraData{ + Pid: 1, + }, + }) + testCase.expectedCalls() + err := wu.Release(testCase.force) + testCase.errorCatch(err, t) + }) + } + +} From 2adde43a294b42f289324de68d00b2d1104b1e53 Mon Sep 17 00:00:00 2001 From: resoluteCoder Date: Fri, 8 Dec 2023 13:03:19 -0600 Subject: [PATCH 09/21] skdfjlsj --- pkg/workceptor/command_test.go | 76 +++++++++++++++++++++++----------- 1 file changed, 51 insertions(+), 25 deletions(-) diff --git a/pkg/workceptor/command_test.go b/pkg/workceptor/command_test.go index e3c2b331d..8f8bfcb51 100644 --- a/pkg/workceptor/command_test.go +++ b/pkg/workceptor/command_test.go @@ -14,7 +14,16 @@ import ( "github.com/golang/mock/gomock" ) -func createTestSetup(t *testing.T) (workceptor.WorkUnit, *mock_workceptor.MockBaseWorkUnitForWorkUnit, *mock_workceptor.MockNetceptorForWorkceptor, *gomock.Controller) { +func statusExpectCalls(mockBaseWorkUnit *mock_workceptor.MockBaseWorkUnitForWorkUnit) { + statusLock := &sync.RWMutex{} + mockBaseWorkUnit.EXPECT().GetStatusLock().Return(statusLock).Times(2) + mockBaseWorkUnit.EXPECT().GetStatusWithoutExtraData().Return(&workceptor.StatusFileData{}) + mockBaseWorkUnit.EXPECT().GetStatusCopy().Return(workceptor.StatusFileData{ + ExtraData: &workceptor.CommandExtraData{}, + }) +} + +func createTestSetup(t *testing.T) (workceptor.WorkUnit, *mock_workceptor.MockBaseWorkUnitForWorkUnit, *mock_workceptor.MockNetceptorForWorkceptor, *gomock.Controller, *workceptor.Workceptor) { ctrl := gomock.NewController(t) ctx := context.Background() @@ -30,11 +39,11 @@ func createTestSetup(t *testing.T) (workceptor.WorkUnit, *mock_workceptor.MockBa cwc := &workceptor.CommandWorkerCfg{} mockBaseWorkUnit.EXPECT().Init(w, "", "", workceptor.FileSystem{}, nil) workUnit := cwc.NewWorker(mockBaseWorkUnit, w, "", "") - return workUnit, mockBaseWorkUnit, mockNetceptor, ctrl + return workUnit, mockBaseWorkUnit, mockNetceptor, ctrl, w } func TestSetFromParams2(t *testing.T) { - wu, mockBaseWorkUnit, _, _ := createTestSetup(t) + wu, mockBaseWorkUnit, _, _, _ := createTestSetup(t) paramsTestCases := []struct { name string @@ -81,7 +90,7 @@ func TestSetFromParams2(t *testing.T) { } func TestUnredactedStatus(t *testing.T) { - wu, mockBaseWorkUnit, _, _ := createTestSetup(t) + wu, mockBaseWorkUnit, _, _, _ := createTestSetup(t) statusLock := &sync.RWMutex{} mockBaseWorkUnit.EXPECT().GetStatusLock().Return(statusLock).Times(2) mockBaseWorkUnit.EXPECT().GetStatusWithoutExtraData().Return(&workceptor.StatusFileData{}) @@ -92,22 +101,23 @@ func TestUnredactedStatus(t *testing.T) { wu.UnredactedStatus() } -// func TestStart(t *testing.T) { -// wu, mockBaseWorkUnit, _, _ := createTestSetup(t) -// // w, err := workceptor.New(context.Background(), mockNetceptor, "") -// // if err != nil { -// // fmt.Println(err) -// // } - -// stuff := mockBaseWorkUnit.EXPECT().GetWorkceptor().Return() -// // mockNetceptor.EXPECT().GetLogger() +func TestStart(t *testing.T) { + wu, mockBaseWorkUnit, mockNetceptor, _, w := createTestSetup(t) -// wu.Start() + mockBaseWorkUnit.EXPECT().GetWorkceptor().Return(w).Times(2) + mockNetceptor.EXPECT().GetLogger().Times(2) + mockBaseWorkUnit.EXPECT().UpdateBasicStatus(gomock.Any(), gomock.Any(), gomock.Any()) + statusExpectCalls(mockBaseWorkUnit) -// } + mockBaseWorkUnit.EXPECT().UnitDir() + mockBaseWorkUnit.EXPECT().UpdateFullStatus(gomock.Any()) + mockBaseWorkUnit.EXPECT().MonitorLocalStatus().AnyTimes() + mockBaseWorkUnit.EXPECT().UpdateFullStatus(gomock.Any()).AnyTimes() + wu.Start() +} func TestRestart(t *testing.T) { - wu, mockBaseWorkUnit, _, _ := createTestSetup(t) + wu, mockBaseWorkUnit, _, _, _ := createTestSetup(t) restartTestCases := []struct { name string @@ -168,6 +178,7 @@ func TestRestart(t *testing.T) { for _, testCase := range restartTestCases { t.Run(testCase.name, func(t *testing.T) { testCase.expectedCalls() + mockBaseWorkUnit.EXPECT().MonitorLocalStatus().AnyTimes() err := wu.Restart() testCase.errorCatch(err, t) }) @@ -175,7 +186,7 @@ func TestRestart(t *testing.T) { } func TestCancel(t *testing.T) { - wu, mockBaseWorkUnit, _, _ := createTestSetup(t) + wu, mockBaseWorkUnit, _, _, _ := createTestSetup(t) paramsTestCases := []struct { name string @@ -186,12 +197,7 @@ func TestCancel(t *testing.T) { name: "return no error 1", expectedCalls: func() { mockBaseWorkUnit.EXPECT().CancelContext() - mockBaseWorkUnit.EXPECT().GetStatusLock().Return(&sync.RWMutex{}).Times(2) - mockBaseWorkUnit.EXPECT().GetStatusWithoutExtraData().Return(&workceptor.StatusFileData{}) - mockBaseWorkUnit.EXPECT().GetStatusCopy().Return(workceptor.StatusFileData{ - ExtraData: &workceptor.CommandExtraData{}, - }) - + statusExpectCalls(mockBaseWorkUnit) }, errorCatch: func(err error, t *testing.T) { if err != nil { @@ -291,7 +297,7 @@ func TestCancel(t *testing.T) { } func TestRelease(t *testing.T) { - wu, mockBaseWorkUnit, _, _ := createTestSetup(t) + wu, mockBaseWorkUnit, _, _, _ := createTestSetup(t) releaseTestCases := []struct { name string @@ -337,5 +343,25 @@ func TestRelease(t *testing.T) { testCase.errorCatch(err, t) }) } - } + +// func TestCommandWorkerCfgRun(t *testing.T) { +// cfgTestCases := []struct { +// name string +// verifySignature bool +// }{ +// { +// name: "error 1", +// verifySignature: true, +// }, +// } +// for _, testCase := range cfgTestCases { +// t.Run(testCase.name, func(t *testing.T) { +// commandWorkerCfg := workceptor.CommandWorkerCfg{ +// VerifySignature: testCase.verifySignature, +// } +// commandWorkerCfg.Run() +// }) +// } + +// } From b43aabc00cbc48e44d6eea993ed17e0d0492ea24 Mon Sep 17 00:00:00 2001 From: resoluteCoder Date: Mon, 11 Dec 2023 10:27:00 -0600 Subject: [PATCH 10/21] whitebox > blackbox --- pkg/workceptor/command_test.go | 159 ++++++++++++++++++++++++++------- 1 file changed, 127 insertions(+), 32 deletions(-) diff --git a/pkg/workceptor/command_test.go b/pkg/workceptor/command_test.go index 8f8bfcb51..09e5dec0a 100644 --- a/pkg/workceptor/command_test.go +++ b/pkg/workceptor/command_test.go @@ -42,7 +42,7 @@ func createTestSetup(t *testing.T) (workceptor.WorkUnit, *mock_workceptor.MockBa return workUnit, mockBaseWorkUnit, mockNetceptor, ctrl, w } -func TestSetFromParams2(t *testing.T) { +func TestCommandSetFromParams(t *testing.T) { wu, mockBaseWorkUnit, _, _, _ := createTestSetup(t) paramsTestCases := []struct { @@ -52,7 +52,7 @@ func TestSetFromParams2(t *testing.T) { errorCatch func(error, *testing.T) }{ { - name: "one", + name: "no params with no error", params: map[string]string{"": ""}, expectedCalls: func() { mockBaseWorkUnit.EXPECT().GetStatusCopy().Return(workceptor.StatusFileData{ @@ -66,7 +66,7 @@ func TestSetFromParams2(t *testing.T) { }, }, { - name: "two", + name: "params with error", params: map[string]string{"params": "param"}, expectedCalls: func() { @@ -125,7 +125,7 @@ func TestRestart(t *testing.T) { errorCatch func(error, *testing.T) }{ { - name: "return error 1", + name: "load error", expectedCalls: func() { mockBaseWorkUnit.EXPECT().Load().Return(errors.New("terminated")) }, @@ -136,7 +136,7 @@ func TestRestart(t *testing.T) { }, }, { - name: "return nil 1", + name: "job complete with no error", expectedCalls: func() { statusFile := &workceptor.StatusFileData{State: 2} mockBaseWorkUnit.EXPECT().Load().Return(nil) @@ -154,7 +154,7 @@ func TestRestart(t *testing.T) { }, }, { - name: "return nil 2", + name: "restart successful", expectedCalls: func() { statusFile := &workceptor.StatusFileData{State: 0} mockBaseWorkUnit.EXPECT().Load().Return(nil) @@ -194,7 +194,7 @@ func TestCancel(t *testing.T) { errorCatch func(error, *testing.T) }{ { - name: "return no error 1", + name: "not a valid pid no error", expectedCalls: func() { mockBaseWorkUnit.EXPECT().CancelContext() statusExpectCalls(mockBaseWorkUnit) @@ -206,7 +206,7 @@ func TestCancel(t *testing.T) { }, }, { - name: "return err 2", + name: "process interrupt error", expectedCalls: func() { mockBaseWorkUnit.EXPECT().CancelContext() mockBaseWorkUnit.EXPECT().GetStatusLock().Return(&sync.RWMutex{}).Times(2) @@ -224,7 +224,7 @@ func TestCancel(t *testing.T) { }, }, { - name: "return nil already finished", + name: "process already finished", expectedCalls: func() { mockBaseWorkUnit.EXPECT().CancelContext() mockBaseWorkUnit.EXPECT().GetStatusLock().Return(&sync.RWMutex{}).Times(2) @@ -253,7 +253,7 @@ func TestCancel(t *testing.T) { }, }, { - name: "happy day", + name: "cancelled process successfully", expectedCalls: func() { mockBaseWorkUnit.EXPECT().CancelContext() mockBaseWorkUnit.EXPECT().GetStatusLock().Return(&sync.RWMutex{}).Times(2) @@ -306,7 +306,7 @@ func TestRelease(t *testing.T) { force bool }{ { - name: "error", + name: "cancel error", expectedCalls: func() {}, errorCatch: func(err error, t *testing.T) { if err == nil { @@ -316,7 +316,7 @@ func TestRelease(t *testing.T) { force: false, }, { - name: "happy day", + name: "released successfully", expectedCalls: func() { mockBaseWorkUnit.EXPECT().Release(gomock.Any()) }, @@ -345,23 +345,118 @@ func TestRelease(t *testing.T) { } } -// func TestCommandWorkerCfgRun(t *testing.T) { -// cfgTestCases := []struct { -// name string -// verifySignature bool -// }{ -// { -// name: "error 1", -// verifySignature: true, -// }, -// } -// for _, testCase := range cfgTestCases { -// t.Run(testCase.name, func(t *testing.T) { -// commandWorkerCfg := workceptor.CommandWorkerCfg{ -// VerifySignature: testCase.verifySignature, -// } -// commandWorkerCfg.Run() -// }) -// } - -// } +func TestSigningKeyPrepare(t *testing.T) { + privateKey := workceptor.SigningKeyPrivateCfg{} + err := privateKey.Prepare() + + if err == nil { + t.Error(err) + } +} + +func TestPrepareSigningKeyPrivateCfg(t *testing.T) { + signingKeyTestCases := []struct { + name string + errorCatch func(error, *testing.T) + privateKey string + tokenExpiration string + }{ + { + name: "file does not exist error", + privateKey: "does_not_exist.txt", + tokenExpiration: "", + errorCatch: func(err error, t *testing.T) { + if err == nil { + t.Error(err) + } + }, + }, + { + name: "failed to parse token expiration", + privateKey: "/etc/hosts", + tokenExpiration: "random_input", + errorCatch: func(err error, t *testing.T) { + if err == nil { + t.Error(err) + } + }, + }, + { + name: "duration no error", + privateKey: "/etc/hosts", + tokenExpiration: "3h", + errorCatch: func(err error, t *testing.T) { + if err != nil { + t.Error(err) + } + }, + }, + { + name: "no duration no error", + privateKey: "/etc/hosts", + tokenExpiration: "", + errorCatch: func(err error, t *testing.T) { + if err != nil { + t.Error(err) + } + }, + }, + } + + for _, testCase := range signingKeyTestCases { + t.Run(testCase.name, func(t *testing.T) { + privateKey := workceptor.SigningKeyPrivateCfg{ + PrivateKey: testCase.privateKey, + TokenExpiration: testCase.tokenExpiration, + } + _, err := privateKey.PrepareSigningKeyPrivateCfg() + testCase.errorCatch(err, t) + }) + } +} + +func TestVerifyingKeyPrepare(t *testing.T) { + publicKey := workceptor.VerifyingKeyPublicCfg{} + err := publicKey.Prepare() + + if err == nil { + t.Error(err) + } +} + +func TestPrepareVerifyingKeyPrivateCfg(t *testing.T) { + verifyingKeyTestCases := []struct { + name string + errorCatch func(error, *testing.T) + publicKey string + }{ + { + name: "file does not exist", + publicKey: "does_not_exist.txt", + errorCatch: func(err error, t *testing.T) { + if err == nil { + t.Error(err) + } + }, + }, + { + name: "prepared successfully", + publicKey: "/etc/hosts", + errorCatch: func(err error, t *testing.T) { + if err != nil { + t.Error(err) + } + }, + }, + } + + for _, testCase := range verifyingKeyTestCases { + t.Run(testCase.name, func(t *testing.T) { + publicKey := workceptor.VerifyingKeyPublicCfg{ + PublicKey: testCase.publicKey, + } + err := publicKey.PrepareVerifyingKeyPublicCfg() + testCase.errorCatch(err, t) + }) + } +} From aff5cc26f88f07cc52cadada3c756bbe04756fd1 Mon Sep 17 00:00:00 2001 From: resoluteCoder Date: Mon, 11 Dec 2023 12:15:27 -0600 Subject: [PATCH 11/21] blarg --- pkg/workceptor/command_test.go | 3 --- pkg/workceptor/json_test.go | 2 +- pkg/workceptor/kubernetes.go | 2 +- pkg/workceptor/python.go | 2 +- 4 files changed, 3 insertions(+), 6 deletions(-) diff --git a/pkg/workceptor/command_test.go b/pkg/workceptor/command_test.go index 09e5dec0a..1367ba4d4 100644 --- a/pkg/workceptor/command_test.go +++ b/pkg/workceptor/command_test.go @@ -69,7 +69,6 @@ func TestCommandSetFromParams(t *testing.T) { name: "params with error", params: map[string]string{"params": "param"}, expectedCalls: func() { - }, errorCatch: func(err error, t *testing.T) { if err == nil { @@ -86,7 +85,6 @@ func TestCommandSetFromParams(t *testing.T) { testCase.errorCatch(err, t) }) } - } func TestUnredactedStatus(t *testing.T) { @@ -293,7 +291,6 @@ func TestCancel(t *testing.T) { testCase.errorCatch(err, t) }) } - } func TestRelease(t *testing.T) { diff --git a/pkg/workceptor/json_test.go b/pkg/workceptor/json_test.go index e1d15d067..bf6a720de 100644 --- a/pkg/workceptor/json_test.go +++ b/pkg/workceptor/json_test.go @@ -11,7 +11,7 @@ import ( "github.com/ansible/receptor/pkg/netceptor" ) -func newCommandWorker(bwu BaseWorkUnitForWorkUnit, w *Workceptor, unitID string, workType string) WorkUnit { +func newCommandWorker(_ BaseWorkUnitForWorkUnit, w *Workceptor, unitID string, workType string) WorkUnit { cw := &commandUnit{ BaseWorkUnitForWorkUnit: &BaseWorkUnit{ status: StatusFileData{ diff --git a/pkg/workceptor/kubernetes.go b/pkg/workceptor/kubernetes.go index 49bc1e35a..8daae73f1 100644 --- a/pkg/workceptor/kubernetes.go +++ b/pkg/workceptor/kubernetes.go @@ -1374,7 +1374,7 @@ type KubeWorkerCfg struct { } // NewWorker is a factory to produce worker instances. -func (cfg KubeWorkerCfg) NewWorker(bwu BaseWorkUnitForWorkUnit, w *Workceptor, unitID string, workType string) WorkUnit { +func (cfg KubeWorkerCfg) NewWorker(_ BaseWorkUnitForWorkUnit, w *Workceptor, unitID string, workType string) WorkUnit { ku := &kubeUnit{ BaseWorkUnit: BaseWorkUnit{ status: StatusFileData{ diff --git a/pkg/workceptor/python.go b/pkg/workceptor/python.go index 520d740ef..bc4124a2d 100644 --- a/pkg/workceptor/python.go +++ b/pkg/workceptor/python.go @@ -50,7 +50,7 @@ type workPythonCfg struct { } // NewWorker is a factory to produce worker instances. -func (cfg workPythonCfg) NewWorker(bwu BaseWorkUnitForWorkUnit, w *Workceptor, unitID string, workType string) WorkUnit { +func (cfg workPythonCfg) NewWorker(_ BaseWorkUnitForWorkUnit, w *Workceptor, unitID string, workType string) WorkUnit { cw := &pythonUnit{ commandUnit: commandUnit{ BaseWorkUnitForWorkUnit: &BaseWorkUnit{ From 24d43f3a63838868460ce8d2686703fa751a795d Mon Sep 17 00:00:00 2001 From: resoluteCoder Date: Mon, 11 Dec 2023 12:25:38 -0600 Subject: [PATCH 12/21] blarg --- pkg/workceptor/command_test.go | 10 +++++----- .../mock_workceptor/{idk.go => baseworkunit.go} | 0 2 files changed, 5 insertions(+), 5 deletions(-) rename pkg/workceptor/mock_workceptor/{idk.go => baseworkunit.go} (100%) diff --git a/pkg/workceptor/command_test.go b/pkg/workceptor/command_test.go index 1367ba4d4..9748e2a61 100644 --- a/pkg/workceptor/command_test.go +++ b/pkg/workceptor/command_test.go @@ -43,7 +43,7 @@ func createTestSetup(t *testing.T) (workceptor.WorkUnit, *mock_workceptor.MockBa } func TestCommandSetFromParams(t *testing.T) { - wu, mockBaseWorkUnit, _, _, _ := createTestSetup(t) + wu, mockBaseWorkUnit, _, _, _ := createTestSetup(t) // nolint:dogsled paramsTestCases := []struct { name string @@ -88,7 +88,7 @@ func TestCommandSetFromParams(t *testing.T) { } func TestUnredactedStatus(t *testing.T) { - wu, mockBaseWorkUnit, _, _, _ := createTestSetup(t) + wu, mockBaseWorkUnit, _, _, _ := createTestSetup(t) // nolint:dogsled statusLock := &sync.RWMutex{} mockBaseWorkUnit.EXPECT().GetStatusLock().Return(statusLock).Times(2) mockBaseWorkUnit.EXPECT().GetStatusWithoutExtraData().Return(&workceptor.StatusFileData{}) @@ -115,7 +115,7 @@ func TestStart(t *testing.T) { } func TestRestart(t *testing.T) { - wu, mockBaseWorkUnit, _, _, _ := createTestSetup(t) + wu, mockBaseWorkUnit, _, _, _ := createTestSetup(t) // nolint:dogsled restartTestCases := []struct { name string @@ -184,7 +184,7 @@ func TestRestart(t *testing.T) { } func TestCancel(t *testing.T) { - wu, mockBaseWorkUnit, _, _, _ := createTestSetup(t) + wu, mockBaseWorkUnit, _, _, _ := createTestSetup(t) // nolint:dogsled paramsTestCases := []struct { name string @@ -294,7 +294,7 @@ func TestCancel(t *testing.T) { } func TestRelease(t *testing.T) { - wu, mockBaseWorkUnit, _, _, _ := createTestSetup(t) + wu, mockBaseWorkUnit, _, _, _ := createTestSetup(t) // nolint:dogsled releaseTestCases := []struct { name string diff --git a/pkg/workceptor/mock_workceptor/idk.go b/pkg/workceptor/mock_workceptor/baseworkunit.go similarity index 100% rename from pkg/workceptor/mock_workceptor/idk.go rename to pkg/workceptor/mock_workceptor/baseworkunit.go From a6d91da0f9cd3d2244dbf2168b8c295920bca4ba Mon Sep 17 00:00:00 2001 From: resoluteCoder Date: Mon, 11 Dec 2023 12:30:26 -0600 Subject: [PATCH 13/21] lint --- pkg/workceptor/command_test.go | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/pkg/workceptor/command_test.go b/pkg/workceptor/command_test.go index 9748e2a61..9a0a568ec 100644 --- a/pkg/workceptor/command_test.go +++ b/pkg/workceptor/command_test.go @@ -39,11 +39,12 @@ func createTestSetup(t *testing.T) (workceptor.WorkUnit, *mock_workceptor.MockBa cwc := &workceptor.CommandWorkerCfg{} mockBaseWorkUnit.EXPECT().Init(w, "", "", workceptor.FileSystem{}, nil) workUnit := cwc.NewWorker(mockBaseWorkUnit, w, "", "") + return workUnit, mockBaseWorkUnit, mockNetceptor, ctrl, w } func TestCommandSetFromParams(t *testing.T) { - wu, mockBaseWorkUnit, _, _, _ := createTestSetup(t) // nolint:dogsled + wu, mockBaseWorkUnit, _, _, _ := createTestSetup(t) //nolint:dogsled paramsTestCases := []struct { name string @@ -88,7 +89,7 @@ func TestCommandSetFromParams(t *testing.T) { } func TestUnredactedStatus(t *testing.T) { - wu, mockBaseWorkUnit, _, _, _ := createTestSetup(t) // nolint:dogsled + wu, mockBaseWorkUnit, _, _, _ := createTestSetup(t) //nolint:dogsled statusLock := &sync.RWMutex{} mockBaseWorkUnit.EXPECT().GetStatusLock().Return(statusLock).Times(2) mockBaseWorkUnit.EXPECT().GetStatusWithoutExtraData().Return(&workceptor.StatusFileData{}) @@ -115,7 +116,7 @@ func TestStart(t *testing.T) { } func TestRestart(t *testing.T) { - wu, mockBaseWorkUnit, _, _, _ := createTestSetup(t) // nolint:dogsled + wu, mockBaseWorkUnit, _, _, _ := createTestSetup(t) //nolint:dogsled restartTestCases := []struct { name string @@ -184,7 +185,7 @@ func TestRestart(t *testing.T) { } func TestCancel(t *testing.T) { - wu, mockBaseWorkUnit, _, _, _ := createTestSetup(t) // nolint:dogsled + wu, mockBaseWorkUnit, _, _, _ := createTestSetup(t) //nolint:dogsled paramsTestCases := []struct { name string @@ -294,7 +295,7 @@ func TestCancel(t *testing.T) { } func TestRelease(t *testing.T) { - wu, mockBaseWorkUnit, _, _, _ := createTestSetup(t) // nolint:dogsled + wu, mockBaseWorkUnit, _, _, _ := createTestSetup(t) //nolint:dogsled releaseTestCases := []struct { name string From 482da058eb0baaa6f22ca9ecb7de92d1797da0cd Mon Sep 17 00:00:00 2001 From: resoluteCoder Date: Wed, 13 Dec 2023 14:31:19 -0600 Subject: [PATCH 14/21] another one --- pkg/workceptor/command_test.go | 16 ++++++++-------- pkg/workceptor/kubernetes.go | 1 - pkg/workceptor/remote_work.go | 1 - 3 files changed, 8 insertions(+), 10 deletions(-) diff --git a/pkg/workceptor/command_test.go b/pkg/workceptor/command_test.go index 9a0a568ec..a84925ca4 100644 --- a/pkg/workceptor/command_test.go +++ b/pkg/workceptor/command_test.go @@ -23,7 +23,7 @@ func statusExpectCalls(mockBaseWorkUnit *mock_workceptor.MockBaseWorkUnitForWork }) } -func createTestSetup(t *testing.T) (workceptor.WorkUnit, *mock_workceptor.MockBaseWorkUnitForWorkUnit, *mock_workceptor.MockNetceptorForWorkceptor, *gomock.Controller, *workceptor.Workceptor) { +func createTestSetup(t *testing.T) (workceptor.WorkUnit, *mock_workceptor.MockBaseWorkUnitForWorkUnit, *mock_workceptor.MockNetceptorForWorkceptor, *workceptor.Workceptor) { ctrl := gomock.NewController(t) ctx := context.Background() @@ -40,11 +40,11 @@ func createTestSetup(t *testing.T) (workceptor.WorkUnit, *mock_workceptor.MockBa mockBaseWorkUnit.EXPECT().Init(w, "", "", workceptor.FileSystem{}, nil) workUnit := cwc.NewWorker(mockBaseWorkUnit, w, "", "") - return workUnit, mockBaseWorkUnit, mockNetceptor, ctrl, w + return workUnit, mockBaseWorkUnit, mockNetceptor, w } func TestCommandSetFromParams(t *testing.T) { - wu, mockBaseWorkUnit, _, _, _ := createTestSetup(t) //nolint:dogsled + wu, mockBaseWorkUnit, _, _ := createTestSetup(t) paramsTestCases := []struct { name string @@ -89,7 +89,7 @@ func TestCommandSetFromParams(t *testing.T) { } func TestUnredactedStatus(t *testing.T) { - wu, mockBaseWorkUnit, _, _, _ := createTestSetup(t) //nolint:dogsled + wu, mockBaseWorkUnit, _, _ := createTestSetup(t) statusLock := &sync.RWMutex{} mockBaseWorkUnit.EXPECT().GetStatusLock().Return(statusLock).Times(2) mockBaseWorkUnit.EXPECT().GetStatusWithoutExtraData().Return(&workceptor.StatusFileData{}) @@ -101,7 +101,7 @@ func TestUnredactedStatus(t *testing.T) { } func TestStart(t *testing.T) { - wu, mockBaseWorkUnit, mockNetceptor, _, w := createTestSetup(t) + wu, mockBaseWorkUnit, mockNetceptor, w := createTestSetup(t) mockBaseWorkUnit.EXPECT().GetWorkceptor().Return(w).Times(2) mockNetceptor.EXPECT().GetLogger().Times(2) @@ -116,7 +116,7 @@ func TestStart(t *testing.T) { } func TestRestart(t *testing.T) { - wu, mockBaseWorkUnit, _, _, _ := createTestSetup(t) //nolint:dogsled + wu, mockBaseWorkUnit, _, _ := createTestSetup(t) restartTestCases := []struct { name string @@ -185,7 +185,7 @@ func TestRestart(t *testing.T) { } func TestCancel(t *testing.T) { - wu, mockBaseWorkUnit, _, _, _ := createTestSetup(t) //nolint:dogsled + wu, mockBaseWorkUnit, _, _ := createTestSetup(t) paramsTestCases := []struct { name string @@ -295,7 +295,7 @@ func TestCancel(t *testing.T) { } func TestRelease(t *testing.T) { - wu, mockBaseWorkUnit, _, _, _ := createTestSetup(t) //nolint:dogsled + wu, mockBaseWorkUnit, _, _ := createTestSetup(t) releaseTestCases := []struct { name string diff --git a/pkg/workceptor/kubernetes.go b/pkg/workceptor/kubernetes.go index 8daae73f1..94a5d2a9d 100644 --- a/pkg/workceptor/kubernetes.go +++ b/pkg/workceptor/kubernetes.go @@ -39,7 +39,6 @@ import ( // kubeUnit implements the WorkUnit interface. type kubeUnit struct { BaseWorkUnit - // BaseWorkUnitForWorkUnit authMethod string streamMethod string baseParams string diff --git a/pkg/workceptor/remote_work.go b/pkg/workceptor/remote_work.go index 74b82d0a0..d1336b39f 100644 --- a/pkg/workceptor/remote_work.go +++ b/pkg/workceptor/remote_work.go @@ -23,7 +23,6 @@ import ( // remoteUnit implements the WorkUnit interface for the Receptor remote worker plugin. type remoteUnit struct { - // BaseWorkUnit BaseWorkUnitForWorkUnit topJC *utils.JobContext logger *logger.ReceptorLogger From b0ac72ccbb9a5ffb93bcbc1564f6a3400d36fabc Mon Sep 17 00:00:00 2001 From: resoluteCoder Date: Fri, 15 Dec 2023 11:45:50 -0600 Subject: [PATCH 15/21] reworked kubernetes.go to include base work unit mock --- pkg/workceptor/command.go | 19 +++--- pkg/workceptor/kubernetes.go | 121 +++++++++++++++++---------------- pkg/workceptor/workunitbase.go | 8 +++ 3 files changed, 82 insertions(+), 66 deletions(-) diff --git a/pkg/workceptor/command.go b/pkg/workceptor/command.go index 7ad619a3b..f675c1c8f 100644 --- a/pkg/workceptor/command.go +++ b/pkg/workceptor/command.go @@ -4,6 +4,7 @@ package workceptor import ( + "context" "flag" "fmt" "os" @@ -21,15 +22,7 @@ import ( type BaseWorkUnitForWorkUnit interface { CancelContext() - Debug(format string, v ...interface{}) - Error(format string, v ...interface{}) - GetStatusCopy() StatusFileData - GetStatusWithoutExtraData() *StatusFileData - SetStatusExtraData(interface{}) - GetStatusLock() *sync.RWMutex - GetWorkceptor() *Workceptor ID() string - Info(format string, v ...interface{}) Init(w *Workceptor, unitID string, workType string, fs FileSystemer, watcher WatcherWrapper) LastUpdateError() error Load() error @@ -44,6 +37,16 @@ type BaseWorkUnitForWorkUnit interface { UnredactedStatus() *StatusFileData UpdateBasicStatus(state int, detail string, stdoutSize int64) UpdateFullStatus(statusFunc func(*StatusFileData)) + GetStatusCopy() StatusFileData + GetStatusWithoutExtraData() *StatusFileData + SetStatusExtraData(interface{}) + GetStatusLock() *sync.RWMutex + GetWorkceptor() *Workceptor + GetContext() context.Context + GetCancel() context.CancelFunc + Debug(format string, v ...interface{}) + Error(format string, v ...interface{}) + Info(format string, v ...interface{}) Warning(format string, v ...interface{}) } diff --git a/pkg/workceptor/kubernetes.go b/pkg/workceptor/kubernetes.go index 94a5d2a9d..35617728e 100644 --- a/pkg/workceptor/kubernetes.go +++ b/pkg/workceptor/kubernetes.go @@ -38,7 +38,8 @@ import ( // kubeUnit implements the WorkUnit interface. type kubeUnit struct { - BaseWorkUnit + BaseWorkUnitForWorkUnit + // BaseWorkUnit authMethod string streamMethod string baseParams string @@ -141,7 +142,7 @@ func (kw *kubeUnit) kubeLoggingConnectionHandler(timestamps bool) (io.ReadCloser ) // get logstream, with retry for retries := 5; retries > 0; retries-- { - logStream, err = logReq.Stream(kw.ctx) + logStream, err = logReq.Stream(kw.GetContext()) if err == nil { break } @@ -208,7 +209,7 @@ func (kw *kubeUnit) kubeLoggingWithReconnect(streamWait *sync.WaitGroup, stdout // get pod, with retry for retries := 5; retries > 0; retries-- { - kw.pod, err = kw.clientset.CoreV1().Pods(podNamespace).Get(kw.ctx, podName, metav1.GetOptions{}) + kw.pod, err = kw.clientset.CoreV1().Pods(podNamespace).Get(kw.GetContext(), podName, metav1.GetOptions{}) if err == nil { break } @@ -239,7 +240,7 @@ func (kw *kubeUnit) kubeLoggingWithReconnect(streamWait *sync.WaitGroup, stdout for *stdinErr == nil { // check between every line read to see if we need to stop reading line, err := streamReader.ReadString('\n') if err != nil { - if kw.ctx.Err() == context.Canceled { + if kw.GetContext().Err() == context.Canceled { kw.Info( "Context was canceled while reading logs for pod %s/%s. Assuming pod has finished", podNamespace, @@ -374,13 +375,13 @@ func (kw *kubeUnit) createPod(env map[string]string) error { } // get pod and store to kw.pod - kw.pod, err = kw.clientset.CoreV1().Pods(ked.KubeNamespace).Create(kw.ctx, pod, metav1.CreateOptions{}) + kw.pod, err = kw.clientset.CoreV1().Pods(ked.KubeNamespace).Create(kw.GetContext(), pod, metav1.CreateOptions{}) if err != nil { return err } select { - case <-kw.ctx.Done(): + case <-kw.GetContext().Done(): return fmt.Errorf("cancelled") default: } @@ -398,18 +399,18 @@ func (kw *kubeUnit) createPod(env map[string]string) error { ListFunc: func(options metav1.ListOptions) (runtime.Object, error) { options.FieldSelector = fieldSelector - return kw.clientset.CoreV1().Pods(ked.KubeNamespace).List(kw.ctx, options) + return kw.clientset.CoreV1().Pods(ked.KubeNamespace).List(kw.GetContext(), options) }, WatchFunc: func(options metav1.ListOptions) (watch.Interface, error) { options.FieldSelector = fieldSelector - return kw.clientset.CoreV1().Pods(ked.KubeNamespace).Watch(kw.ctx, options) + return kw.clientset.CoreV1().Pods(ked.KubeNamespace).Watch(kw.GetContext(), options) }, } - ctxPodReady := kw.ctx + ctxPodReady := kw.GetContext() if kw.podPendingTimeout != time.Duration(0) { - ctxPodReady, _ = context.WithTimeout(kw.ctx, kw.podPendingTimeout) + ctxPodReady, _ = context.WithTimeout(kw.GetContext(), kw.podPendingTimeout) } time.Sleep(2 * time.Second) @@ -520,15 +521,15 @@ func (kw *kubeUnit) runWorkUsingLogger() { for retries := 5; retries > 0; retries-- { // check if the kw.ctx is already cancel select { - case <-kw.ctx.Done(): - errMsg := fmt.Sprintf("Context Done while getting pod %s/%s. Error: %s", podNamespace, podName, kw.ctx.Err()) + case <-kw.GetContext().Done(): + errMsg := fmt.Sprintf("Context Done while getting pod %s/%s. Error: %s", podNamespace, podName, kw.GetContext().Err()) kw.Warning(errMsg) return default: } - kw.pod, err = kw.clientset.CoreV1().Pods(podNamespace).Get(kw.ctx, podName, metav1.GetOptions{}) + kw.pod, err = kw.clientset.CoreV1().Pods(podNamespace).Get(kw.GetContext(), podName, metav1.GetOptions{}) if err == nil { break } @@ -610,7 +611,7 @@ func (kw *kubeUnit) runWorkUsingLogger() { // goroutine to cancel stdin reader go func() { select { - case <-kw.ctx.Done(): + case <-kw.GetContext().Done(): stdin.reader.Close() return @@ -635,7 +636,7 @@ func (kw *kubeUnit) runWorkUsingLogger() { // goroutine to cancel stdout stream go func() { select { - case <-kw.ctx.Done(): + case <-kw.GetContext().Done(): stdout.writer.Close() return @@ -665,7 +666,7 @@ func (kw *kubeUnit) runWorkUsingLogger() { var err error for retries := 5; retries > 0; retries-- { - err = exec.StreamWithContext(kw.ctx, remotecommand.StreamOptions{ + err = exec.StreamWithContext(kw.GetContext(), remotecommand.StreamOptions{ Stdin: stdin, Tty: false, }) @@ -734,14 +735,14 @@ func (kw *kubeUnit) runWorkUsingLogger() { errDetail = fmt.Sprintf("Error running pod. stdin: %s, stdout: %s", stdinErr, stdoutErr) } - if kw.ctx.Err() != context.Canceled { + if kw.GetContext().Err() != context.Canceled { kw.UpdateBasicStatus(WorkStateFailed, errDetail, stdout.Size()) } return } - if kw.ctx.Err() != context.Canceled { + if kw.GetContext().Err() != context.Canceled { kw.UpdateBasicStatus(WorkStateSucceeded, "Finished", stdout.Size()) } } @@ -749,7 +750,7 @@ func (kw *kubeUnit) runWorkUsingLogger() { func isCompatibleK8S(kw *kubeUnit, versionStr string) bool { semver, err := version.ParseSemantic(versionStr) if err != nil { - kw.w.nc.GetLogger().Warning("could parse Kubernetes server version %s, will not use reconnect support", versionStr) + kw.GetWorkceptor().nc.GetLogger().Warning("could parse Kubernetes server version %s, will not use reconnect support", versionStr) return false } @@ -773,11 +774,11 @@ func isCompatibleK8S(kw *kubeUnit, versionStr string) bool { } if semver.AtLeast(version.MustParseSemantic(compatibleVer)) { - kw.w.nc.GetLogger().Debug("Kubernetes version %s is at least %s, using reconnect support", semver, compatibleVer) + kw.GetWorkceptor().nc.GetLogger().Debug("Kubernetes version %s is at least %s, using reconnect support", semver, compatibleVer) return true } - kw.w.nc.GetLogger().Debug("Kubernetes version %s not at least %s, not using reconnect support", semver, compatibleVer) + kw.GetWorkceptor().nc.GetLogger().Debug("Kubernetes version %s not at least %s, not using reconnect support", semver, compatibleVer) return false } @@ -814,7 +815,7 @@ func shouldUseReconnect(kw *kubeUnit) bool { serverVerInfo, err := kw.clientset.ServerVersion() if err != nil { - kw.w.nc.GetLogger().Warning("could not detect Kubernetes server version, will not use reconnect support") + kw.GetWorkceptor().nc.GetLogger().Warning("could not detect Kubernetes server version, will not use reconnect support") return false } @@ -864,7 +865,7 @@ func getDefaultInterface() (string, error) { func (kw *kubeUnit) runWorkUsingTCP() { // Create local cancellable context - ctx, cancel := kw.ctx, kw.cancel + ctx, cancel := kw.GetContext(), kw.GetCancel() defer cancel() // Create the TCP listener @@ -884,7 +885,7 @@ func (kw *kubeUnit) runWorkUsingTCP() { if err != nil { errMsg := fmt.Sprintf("Error listening: %s", err) kw.UpdateBasicStatus(WorkStateFailed, errMsg, 0) - kw.w.nc.GetLogger().Error(errMsg) + kw.GetWorkceptor().nc.GetLogger().Error(errMsg) return } @@ -908,7 +909,7 @@ func (kw *kubeUnit) runWorkUsingTCP() { if err != nil { errMsg := fmt.Sprintf("Error accepting: %s", err) kw.UpdateBasicStatus(WorkStateFailed, errMsg, 0) - kw.w.nc.GetLogger().Error(errMsg) + kw.GetWorkceptor().nc.GetLogger().Error(errMsg) cancel() return @@ -921,7 +922,7 @@ func (kw *kubeUnit) runWorkUsingTCP() { if err != nil { errMsg := fmt.Sprintf("Error creating pod: %s", err) kw.UpdateBasicStatus(WorkStateFailed, errMsg, 0) - kw.w.nc.GetLogger().Error(errMsg) + kw.GetWorkceptor().nc.GetLogger().Error(errMsg) cancel() return @@ -940,7 +941,7 @@ func (kw *kubeUnit) runWorkUsingTCP() { stdin, err = NewStdinReader(FileSystem{}, kw.UnitDir()) if err != nil { errMsg := fmt.Sprintf("Error opening stdin file: %s", err) - kw.w.nc.GetLogger().Error(errMsg) + kw.GetWorkceptor().nc.GetLogger().Error(errMsg) kw.UpdateBasicStatus(WorkStateFailed, errMsg, 0) cancel() @@ -951,7 +952,7 @@ func (kw *kubeUnit) runWorkUsingTCP() { stdout, err := NewStdoutWriter(FileSystem{}, kw.UnitDir()) if err != nil { errMsg := fmt.Sprintf("Error opening stdout file: %s", err) - kw.w.nc.GetLogger().Error(errMsg) + kw.GetWorkceptor().nc.GetLogger().Error(errMsg) kw.UpdateBasicStatus(WorkStateFailed, errMsg, 0) cancel() @@ -969,7 +970,7 @@ func (kw *kubeUnit) runWorkUsingTCP() { _ = conn.CloseWrite() if err != nil { errMsg := fmt.Sprintf("Error sending stdin to pod: %s", err) - kw.w.nc.GetLogger().Error(errMsg) + kw.GetWorkceptor().nc.GetLogger().Error(errMsg) kw.UpdateBasicStatus(WorkStateFailed, errMsg, 0) cancel() @@ -1001,7 +1002,7 @@ func (kw *kubeUnit) runWorkUsingTCP() { } if err != nil { errMsg := fmt.Sprintf("Error reading stdout from pod: %s", err) - kw.w.nc.GetLogger().Error(errMsg) + kw.GetWorkceptor().nc.GetLogger().Error(errMsg) kw.UpdateBasicStatus(WorkStateFailed, errMsg, 0) cancel() @@ -1156,7 +1157,7 @@ func readFileToString(filename string) (string, error) { // SetFromParams sets the in-memory state from parameters. func (kw *kubeUnit) SetFromParams(params map[string]string) error { - ked := kw.status.ExtraData.(*kubeExtraData) + ked := kw.GetStatusCopy().ExtraData.(*kubeExtraData) type value struct { name string permission bool @@ -1217,7 +1218,7 @@ func (kw *kubeUnit) SetFromParams(params map[string]string) error { if podPendingTimeoutString != "" { podPendingTimeout, err := time.ParseDuration(podPendingTimeoutString) if err != nil { - kw.w.nc.GetLogger().Error("Failed to parse pod_pending_timeout -- valid examples include '1.5h', '30m', '30m10s'") + kw.GetWorkceptor().nc.GetLogger().Error("Failed to parse pod_pending_timeout -- valid examples include '1.5h', '30m', '30m10s'") return err } @@ -1256,10 +1257,10 @@ func (kw *kubeUnit) Status() *StatusFileData { // Status returns a copy of the status currently loaded in memory. func (kw *kubeUnit) UnredactedStatus() *StatusFileData { - kw.statusLock.RLock() - defer kw.statusLock.RUnlock() - status := kw.getStatus() - ked, ok := kw.status.ExtraData.(*kubeExtraData) + kw.GetStatusLock().RLock() + defer kw.GetStatusLock().RUnlock() + status := kw.GetStatusWithoutExtraData() + ked, ok := kw.GetStatusCopy().ExtraData.(*kubeExtraData) if ok { kedCopy := *ked status.ExtraData = &kedCopy @@ -1300,11 +1301,11 @@ func (kw *kubeUnit) Restart() error { if kw.deletePodOnRestart { err := kw.connectToKube() if err != nil { - kw.w.nc.GetLogger().Warning("Pod %s could not be deleted: %s", ked.PodName, err.Error()) + kw.GetWorkceptor().nc.GetLogger().Warning("Pod %s could not be deleted: %s", ked.PodName, err.Error()) } else { err := kw.clientset.CoreV1().Pods(ked.KubeNamespace).Delete(context.Background(), ked.PodName, metav1.DeleteOptions{}) if err != nil { - kw.w.nc.GetLogger().Warning("Pod %s could not be deleted: %s", ked.PodName, err.Error()) + kw.GetWorkceptor().nc.GetLogger().Warning("Pod %s could not be deleted: %s", ked.PodName, err.Error()) } } } @@ -1324,16 +1325,16 @@ func (kw *kubeUnit) Start() error { // Cancel releases resources associated with a job, including cancelling it if running. func (kw *kubeUnit) Cancel() error { - kw.cancel() + kw.CancelContext() kw.UpdateBasicStatus(WorkStateCanceled, "Canceled", -1) if kw.pod != nil { err := kw.clientset.CoreV1().Pods(kw.pod.Namespace).Delete(context.Background(), kw.pod.Name, metav1.DeleteOptions{}) if err != nil { - kw.w.nc.GetLogger().Error("Error deleting pod %s: %s", kw.pod.Name, err) + kw.GetWorkceptor().nc.GetLogger().Error("Error deleting pod %s: %s", kw.pod.Name, err) } } - if kw.cancel != nil { - kw.cancel() + if kw.GetCancel() != nil { + kw.CancelContext() } return nil @@ -1346,7 +1347,7 @@ func (kw *kubeUnit) Release(force bool) error { return err } - return kw.BaseWorkUnit.Release(force) + return kw.BaseWorkUnitForWorkUnit.Release(force) } // ************************************************************************** @@ -1373,9 +1374,9 @@ type KubeWorkerCfg struct { } // NewWorker is a factory to produce worker instances. -func (cfg KubeWorkerCfg) NewWorker(_ BaseWorkUnitForWorkUnit, w *Workceptor, unitID string, workType string) WorkUnit { - ku := &kubeUnit{ - BaseWorkUnit: BaseWorkUnit{ +func (cfg KubeWorkerCfg) NewWorker(bwu BaseWorkUnitForWorkUnit, w *Workceptor, unitID string, workType string) WorkUnit { + if bwu == nil { + bwu = &BaseWorkUnit{ status: StatusFileData{ ExtraData: &kubeExtraData{ Image: cfg.Image, @@ -1385,18 +1386,22 @@ func (cfg KubeWorkerCfg) NewWorker(_ BaseWorkUnitForWorkUnit, w *Workceptor, uni KubeConfig: cfg.KubeConfig, }, }, - }, - authMethod: strings.ToLower(cfg.AuthMethod), - streamMethod: strings.ToLower(cfg.StreamMethod), - baseParams: cfg.Params, - allowRuntimeAuth: cfg.AllowRuntimeAuth, - allowRuntimeCommand: cfg.AllowRuntimeCommand, - allowRuntimeParams: cfg.AllowRuntimeParams, - allowRuntimePod: cfg.AllowRuntimePod, - deletePodOnRestart: cfg.DeletePodOnRestart, - namePrefix: fmt.Sprintf("%s-", strings.ToLower(cfg.WorkType)), - } - ku.BaseWorkUnit.Init(w, unitID, workType, FileSystem{}, nil) + } + } + + ku := &kubeUnit{ + BaseWorkUnitForWorkUnit: bwu, + authMethod: strings.ToLower(cfg.AuthMethod), + streamMethod: strings.ToLower(cfg.StreamMethod), + baseParams: cfg.Params, + allowRuntimeAuth: cfg.AllowRuntimeAuth, + allowRuntimeCommand: cfg.AllowRuntimeCommand, + allowRuntimeParams: cfg.AllowRuntimeParams, + allowRuntimePod: cfg.AllowRuntimePod, + deletePodOnRestart: cfg.DeletePodOnRestart, + namePrefix: fmt.Sprintf("%s-", strings.ToLower(cfg.WorkType)), + } + ku.BaseWorkUnitForWorkUnit.Init(w, unitID, workType, FileSystem{}, nil) return ku } diff --git a/pkg/workceptor/workunitbase.go b/pkg/workceptor/workunitbase.go index f7707f5d6..8f2571798 100644 --- a/pkg/workceptor/workunitbase.go +++ b/pkg/workceptor/workunitbase.go @@ -505,6 +505,14 @@ func (bwu *BaseWorkUnit) GetWorkceptor() *Workceptor { return bwu.w } +func (bwu *BaseWorkUnit) GetContext() context.Context { + return bwu.ctx +} + +func (bwu *BaseWorkUnit) GetCancel() context.CancelFunc { + return bwu.cancel +} + // =============================================================================================== // func newUnknownWorker(w *Workceptor, unitID string, workType string) WorkUnit { From 76316009233ee5738fed1c10e1ed7cc7813a33ba Mon Sep 17 00:00:00 2001 From: resoluteCoder Date: Fri, 15 Dec 2023 12:05:29 -0600 Subject: [PATCH 16/21] --amend --- pkg/workceptor/command.go | 1 + pkg/workceptor/kubernetes_test.go | 2 +- .../mock_workceptor/baseworkunit.go | 41 +++++++++++++++++++ pkg/workceptor/workunitbase.go | 4 ++ 4 files changed, 47 insertions(+), 1 deletion(-) diff --git a/pkg/workceptor/command.go b/pkg/workceptor/command.go index f675c1c8f..d0b8a09ca 100644 --- a/pkg/workceptor/command.go +++ b/pkg/workceptor/command.go @@ -42,6 +42,7 @@ type BaseWorkUnitForWorkUnit interface { SetStatusExtraData(interface{}) GetStatusLock() *sync.RWMutex GetWorkceptor() *Workceptor + SetWorkceptor(*Workceptor) GetContext() context.Context GetCancel() context.CancelFunc Debug(format string, v ...interface{}) diff --git a/pkg/workceptor/kubernetes_test.go b/pkg/workceptor/kubernetes_test.go index 47a04ff77..bcd4f50ed 100644 --- a/pkg/workceptor/kubernetes_test.go +++ b/pkg/workceptor/kubernetes_test.go @@ -29,7 +29,7 @@ func Test_isCompatibleK8S(t *testing.T) { if err != nil { t.Fatal(err) } - kw.w = w + kw.SetWorkceptor(w) tests := []args{ // K8S compatible versions diff --git a/pkg/workceptor/mock_workceptor/baseworkunit.go b/pkg/workceptor/mock_workceptor/baseworkunit.go index 024775b3c..da19ba9ae 100644 --- a/pkg/workceptor/mock_workceptor/baseworkunit.go +++ b/pkg/workceptor/mock_workceptor/baseworkunit.go @@ -5,6 +5,7 @@ package mock_workceptor import ( + context "context" reflect "reflect" sync "sync" @@ -81,6 +82,34 @@ func (mr *MockBaseWorkUnitForWorkUnitMockRecorder) Error(arg0 interface{}, arg1 return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Error", reflect.TypeOf((*MockBaseWorkUnitForWorkUnit)(nil).Error), varargs...) } +// GetCancel mocks base method. +func (m *MockBaseWorkUnitForWorkUnit) GetCancel() context.CancelFunc { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "GetCancel") + ret0, _ := ret[0].(context.CancelFunc) + return ret0 +} + +// GetCancel indicates an expected call of GetCancel. +func (mr *MockBaseWorkUnitForWorkUnitMockRecorder) GetCancel() *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetCancel", reflect.TypeOf((*MockBaseWorkUnitForWorkUnit)(nil).GetCancel)) +} + +// GetContext mocks base method. +func (m *MockBaseWorkUnitForWorkUnit) GetContext() context.Context { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "GetContext") + ret0, _ := ret[0].(context.Context) + return ret0 +} + +// GetContext indicates an expected call of GetContext. +func (mr *MockBaseWorkUnitForWorkUnitMockRecorder) GetContext() *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetContext", reflect.TypeOf((*MockBaseWorkUnitForWorkUnit)(nil).GetContext)) +} + // GetStatusCopy mocks base method. func (m *MockBaseWorkUnitForWorkUnit) GetStatusCopy() workceptor.StatusFileData { m.ctrl.T.Helper() @@ -274,6 +303,18 @@ func (mr *MockBaseWorkUnitForWorkUnitMockRecorder) SetStatusExtraData(arg0 inter return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "SetStatusExtraData", reflect.TypeOf((*MockBaseWorkUnitForWorkUnit)(nil).SetStatusExtraData), arg0) } +// SetWorkceptor mocks base method. +func (m *MockBaseWorkUnitForWorkUnit) SetWorkceptor(arg0 *workceptor.Workceptor) { + m.ctrl.T.Helper() + m.ctrl.Call(m, "SetWorkceptor", arg0) +} + +// SetWorkceptor indicates an expected call of SetWorkceptor. +func (mr *MockBaseWorkUnitForWorkUnitMockRecorder) SetWorkceptor(arg0 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "SetWorkceptor", reflect.TypeOf((*MockBaseWorkUnitForWorkUnit)(nil).SetWorkceptor), arg0) +} + // Status mocks base method. func (m *MockBaseWorkUnitForWorkUnit) Status() *workceptor.StatusFileData { m.ctrl.T.Helper() diff --git a/pkg/workceptor/workunitbase.go b/pkg/workceptor/workunitbase.go index 8f2571798..97413548b 100644 --- a/pkg/workceptor/workunitbase.go +++ b/pkg/workceptor/workunitbase.go @@ -505,6 +505,10 @@ func (bwu *BaseWorkUnit) GetWorkceptor() *Workceptor { return bwu.w } +func (bwu *BaseWorkUnit) SetWorkceptor(w *Workceptor) { + bwu.w = w +} + func (bwu *BaseWorkUnit) GetContext() context.Context { return bwu.ctx } From ea520cf6f6ba49b4281bf6307adc497aceb130ce Mon Sep 17 00:00:00 2001 From: resoluteCoder Date: Fri, 15 Dec 2023 14:09:38 -0600 Subject: [PATCH 17/21] fixed kube test --- pkg/workceptor/kubernetes_test.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/pkg/workceptor/kubernetes_test.go b/pkg/workceptor/kubernetes_test.go index bcd4f50ed..f5818bdd1 100644 --- a/pkg/workceptor/kubernetes_test.go +++ b/pkg/workceptor/kubernetes_test.go @@ -13,7 +13,9 @@ func Test_isCompatibleK8S(t *testing.T) { isCompatible bool } - kw := &kubeUnit{} + kw := &kubeUnit{ + BaseWorkUnitForWorkUnit: &BaseWorkUnit{}, + } // Create Netceptor node using external backends n1 := netceptor.New(context.Background(), "node1") From 437d46ab3a3a12646db1d006d46b9fd4a68dabab Mon Sep 17 00:00:00 2001 From: resoluteCoder Date: Thu, 11 Jan 2024 16:39:02 -0600 Subject: [PATCH 18/21] add more tests --- pkg/workceptor/command.go | 4 --- pkg/workceptor/command_test.go | 40 +++++++++++++-------- pkg/workceptor/kubernetes.go | 58 +++++++++++++++--------------- pkg/workceptor/remote_work.go | 4 +++ pkg/workceptor/remote_work_test.go | 57 +++++++++++++++++++++++++++++ 5 files changed, 116 insertions(+), 47 deletions(-) create mode 100644 pkg/workceptor/remote_work_test.go diff --git a/pkg/workceptor/command.go b/pkg/workceptor/command.go index d0b8a09ca..fa9394490 100644 --- a/pkg/workceptor/command.go +++ b/pkg/workceptor/command.go @@ -45,10 +45,6 @@ type BaseWorkUnitForWorkUnit interface { SetWorkceptor(*Workceptor) GetContext() context.Context GetCancel() context.CancelFunc - Debug(format string, v ...interface{}) - Error(format string, v ...interface{}) - Info(format string, v ...interface{}) - Warning(format string, v ...interface{}) } // commandUnit implements the WorkUnit interface for the Receptor command worker plugin. diff --git a/pkg/workceptor/command_test.go b/pkg/workceptor/command_test.go index a84925ca4..3844e7099 100644 --- a/pkg/workceptor/command_test.go +++ b/pkg/workceptor/command_test.go @@ -23,7 +23,7 @@ func statusExpectCalls(mockBaseWorkUnit *mock_workceptor.MockBaseWorkUnitForWork }) } -func createTestSetup(t *testing.T) (workceptor.WorkUnit, *mock_workceptor.MockBaseWorkUnitForWorkUnit, *mock_workceptor.MockNetceptorForWorkceptor, *workceptor.Workceptor) { +func createCommandTestSetup(t *testing.T) (workceptor.WorkUnit, *mock_workceptor.MockBaseWorkUnitForWorkUnit, *mock_workceptor.MockNetceptorForWorkceptor, *workceptor.Workceptor) { ctrl := gomock.NewController(t) ctx := context.Background() @@ -44,7 +44,7 @@ func createTestSetup(t *testing.T) (workceptor.WorkUnit, *mock_workceptor.MockBa } func TestCommandSetFromParams(t *testing.T) { - wu, mockBaseWorkUnit, _, _ := createTestSetup(t) + wu, mockBaseWorkUnit, _, _ := createCommandTestSetup(t) paramsTestCases := []struct { name string @@ -89,19 +89,31 @@ func TestCommandSetFromParams(t *testing.T) { } func TestUnredactedStatus(t *testing.T) { - wu, mockBaseWorkUnit, _, _ := createTestSetup(t) - statusLock := &sync.RWMutex{} - mockBaseWorkUnit.EXPECT().GetStatusLock().Return(statusLock).Times(2) - mockBaseWorkUnit.EXPECT().GetStatusWithoutExtraData().Return(&workceptor.StatusFileData{}) - mockBaseWorkUnit.EXPECT().GetStatusCopy().Return(workceptor.StatusFileData{ - ExtraData: &workceptor.CommandExtraData{}, - }) + wu, mockBaseWorkUnit, _, _ := createCommandTestSetup(t) + restartTestCases := []struct { + name string + }{ + {name: "test1"}, + {name: "test2"}, + {name: "test3"}, + } - wu.UnredactedStatus() + for _, testCase := range restartTestCases { + t.Run(testCase.name, func(t *testing.T) { + t.Parallel() + statusLock := &sync.RWMutex{} + mockBaseWorkUnit.EXPECT().GetStatusLock().Return(statusLock).Times(2) + mockBaseWorkUnit.EXPECT().GetStatusWithoutExtraData().Return(&workceptor.StatusFileData{}) + mockBaseWorkUnit.EXPECT().GetStatusCopy().Return(workceptor.StatusFileData{ + ExtraData: &workceptor.CommandExtraData{}, + }) + wu.UnredactedStatus() + }) + } } func TestStart(t *testing.T) { - wu, mockBaseWorkUnit, mockNetceptor, w := createTestSetup(t) + wu, mockBaseWorkUnit, mockNetceptor, w := createCommandTestSetup(t) mockBaseWorkUnit.EXPECT().GetWorkceptor().Return(w).Times(2) mockNetceptor.EXPECT().GetLogger().Times(2) @@ -116,7 +128,7 @@ func TestStart(t *testing.T) { } func TestRestart(t *testing.T) { - wu, mockBaseWorkUnit, _, _ := createTestSetup(t) + wu, mockBaseWorkUnit, _, _ := createCommandTestSetup(t) restartTestCases := []struct { name string @@ -185,7 +197,7 @@ func TestRestart(t *testing.T) { } func TestCancel(t *testing.T) { - wu, mockBaseWorkUnit, _, _ := createTestSetup(t) + wu, mockBaseWorkUnit, _, _ := createCommandTestSetup(t) paramsTestCases := []struct { name string @@ -295,7 +307,7 @@ func TestCancel(t *testing.T) { } func TestRelease(t *testing.T) { - wu, mockBaseWorkUnit, _, _ := createTestSetup(t) + wu, mockBaseWorkUnit, _, _ := createCommandTestSetup(t) releaseTestCases := []struct { name string diff --git a/pkg/workceptor/kubernetes.go b/pkg/workceptor/kubernetes.go index 35617728e..2b7272318 100644 --- a/pkg/workceptor/kubernetes.go +++ b/pkg/workceptor/kubernetes.go @@ -146,7 +146,7 @@ func (kw *kubeUnit) kubeLoggingConnectionHandler(timestamps bool) (io.ReadCloser if err == nil { break } - kw.Warning( + kw.GetWorkceptor().nc.GetLogger().Warning( "Error opening log stream for pod %s/%s. Will retry %d more times. Error: %s", podNamespace, podName, @@ -157,7 +157,7 @@ func (kw *kubeUnit) kubeLoggingConnectionHandler(timestamps bool) (io.ReadCloser } if err != nil { errMsg := fmt.Sprintf("Error opening log stream for pod %s/%s. Error: %s", podNamespace, podName, err) - kw.Error(errMsg) + kw.GetWorkceptor().nc.GetLogger().Error(errMsg) kw.UpdateBasicStatus(WorkStateFailed, errMsg, 0) return nil, err @@ -181,7 +181,7 @@ func (kw *kubeUnit) kubeLoggingNoReconnect(streamWait *sync.WaitGroup, stdout *S _, *stdoutErr = io.Copy(stdout, logStream) if *stdoutErr != nil { - kw.Error( + kw.GetWorkceptor().nc.GetLogger().Error( "Error streaming pod logs to stdout for pod %s/%s. Error: %s", podNamespace, podName, @@ -213,7 +213,7 @@ func (kw *kubeUnit) kubeLoggingWithReconnect(streamWait *sync.WaitGroup, stdout if err == nil { break } - kw.Warning( + kw.GetWorkceptor().nc.GetLogger().Warning( "Error getting pod %s/%s. Will retry %d more times. Error: %s", podNamespace, podName, @@ -224,7 +224,7 @@ func (kw *kubeUnit) kubeLoggingWithReconnect(streamWait *sync.WaitGroup, stdout } if err != nil { errMsg := fmt.Sprintf("Error getting pod %s/%s. Error: %s", podNamespace, podName, err) - kw.Error(errMsg) + kw.GetWorkceptor().nc.GetLogger().Error(errMsg) kw.UpdateBasicStatus(WorkStateFailed, errMsg, 0) break @@ -241,14 +241,14 @@ func (kw *kubeUnit) kubeLoggingWithReconnect(streamWait *sync.WaitGroup, stdout line, err := streamReader.ReadString('\n') if err != nil { if kw.GetContext().Err() == context.Canceled { - kw.Info( + kw.GetWorkceptor().nc.GetLogger().Info( "Context was canceled while reading logs for pod %s/%s. Assuming pod has finished", podNamespace, podName) return } - kw.Info( + kw.GetWorkceptor().nc.GetLogger().Info( "Detected Error: %s for pod %s/%s. Will retry %d more times.", err, podNamespace, @@ -264,7 +264,7 @@ func (kw *kubeUnit) kubeLoggingWithReconnect(streamWait *sync.WaitGroup, stdout break } *stdoutErr = err - kw.Error("Error reading from pod %s/%s: %s", podNamespace, podName, err) + kw.GetWorkceptor().nc.GetLogger().Error("Error reading from pod %s/%s: %s", podNamespace, podName, err) return } @@ -279,7 +279,7 @@ func (kw *kubeUnit) kubeLoggingWithReconnect(streamWait *sync.WaitGroup, stdout _, err = stdout.Write([]byte(msg)) if err != nil { *stdoutErr = fmt.Errorf("writing to stdout: %s", err) - kw.Error("Error writing to stdout: %s", err) + kw.GetWorkceptor().nc.GetLogger().Error("Error writing to stdout: %s", err) return } @@ -442,7 +442,7 @@ func (kw *kubeUnit) createPod(env map[string]string) error { stdout, err2 := NewStdoutWriter(FileSystem{}, kw.UnitDir()) if err2 != nil { errMsg := fmt.Sprintf("Error opening stdout file: %s", err2) - kw.Error(errMsg) + kw.GetWorkceptor().nc.GetLogger().Error(errMsg) kw.UpdateBasicStatus(WorkStateFailed, errMsg, 0) return fmt.Errorf(errMsg) @@ -493,7 +493,7 @@ func (kw *kubeUnit) runWorkUsingLogger() { if err := kw.createPod(nil); err != nil { if err != ErrPodCompleted { errMsg := fmt.Sprintf("Error creating pod: %s", err) - kw.Error(errMsg) + kw.GetWorkceptor().nc.GetLogger().Error(errMsg) kw.UpdateBasicStatus(WorkStateFailed, errMsg, 0) return @@ -510,7 +510,7 @@ func (kw *kubeUnit) runWorkUsingLogger() { errMsg := fmt.Sprintf("Error creating pod: pod namespace is empty for pod %s", podName, ) - kw.Error(errMsg) + kw.GetWorkceptor().nc.GetLogger().Error(errMsg) kw.UpdateBasicStatus(WorkStateFailed, errMsg, 0) return @@ -523,7 +523,7 @@ func (kw *kubeUnit) runWorkUsingLogger() { select { case <-kw.GetContext().Done(): errMsg := fmt.Sprintf("Context Done while getting pod %s/%s. Error: %s", podNamespace, podName, kw.GetContext().Err()) - kw.Warning(errMsg) + kw.GetWorkceptor().nc.GetLogger().Warning(errMsg) return default: @@ -533,7 +533,7 @@ func (kw *kubeUnit) runWorkUsingLogger() { if err == nil { break } - kw.Warning( + kw.GetWorkceptor().nc.GetLogger().Warning( "Error getting pod %s/%s. Will retry %d more times. Retrying: %s", podNamespace, podName, @@ -544,7 +544,7 @@ func (kw *kubeUnit) runWorkUsingLogger() { } if err != nil { errMsg := fmt.Sprintf("Error getting pod %s/%s. Error: %s", podNamespace, podName, err) - kw.Error(errMsg) + kw.GetWorkceptor().nc.GetLogger().Error(errMsg) kw.UpdateBasicStatus(WorkStateFailed, errMsg, 0) return @@ -602,7 +602,7 @@ func (kw *kubeUnit) runWorkUsingLogger() { skipStdin = true } else { errMsg := fmt.Sprintf("Error opening stdin file: %s", err) - kw.Error(errMsg) + kw.GetWorkceptor().nc.GetLogger().Error(errMsg) kw.UpdateBasicStatus(WorkStateFailed, errMsg, 0) return @@ -627,7 +627,7 @@ func (kw *kubeUnit) runWorkUsingLogger() { stdout, err := NewStdoutWriter(FileSystem{}, kw.UnitDir()) if err != nil { errMsg := fmt.Sprintf("Error opening stdout file: %s", err) - kw.Error(errMsg) + kw.GetWorkceptor().nc.GetLogger().Error(errMsg) kw.UpdateBasicStatus(WorkStateFailed, errMsg, 0) return @@ -672,7 +672,7 @@ func (kw *kubeUnit) runWorkUsingLogger() { }) if err != nil { // NOTE: io.EOF for stdin is handled by remotecommand and will not trigger this - kw.Warning( + kw.GetWorkceptor().nc.GetLogger().Warning( "Error streaming stdin to pod %s/%s. Will retry %d more times. Error: %s", podNamespace, podName, @@ -693,7 +693,7 @@ func (kw *kubeUnit) runWorkUsingLogger() { podName, err, ) - kw.Error(errMsg) + kw.GetWorkceptor().nc.GetLogger().Error(errMsg) kw.UpdateBasicStatus(WorkStateFailed, errMsg, stdout.Size()) close(stdinErrChan) // signal STDOUT goroutine to stop @@ -703,7 +703,7 @@ func (kw *kubeUnit) runWorkUsingLogger() { } else { // this is probably not possible... errMsg := fmt.Sprintf("Error reading stdin: %s", stdin.Error()) - kw.Error(errMsg) + kw.GetWorkceptor().nc.GetLogger().Error(errMsg) kw.UpdateBasicStatus(WorkStateFailed, errMsg, stdout.Size()) close(stdinErrChan) // signal STDOUT goroutine to stop @@ -714,10 +714,10 @@ func (kw *kubeUnit) runWorkUsingLogger() { stdoutWithReconnect := shouldUseReconnect(kw) if stdoutWithReconnect && stdoutErr == nil { - kw.Debug("streaming stdout with reconnect support") + kw.GetWorkceptor().nc.GetLogger().Debug("streaming stdout with reconnect support") go kw.kubeLoggingWithReconnect(&streamWait, stdout, &stdinErr, &stdoutErr) } else { - kw.Debug("streaming stdout with no reconnect support") + kw.GetWorkceptor().nc.GetLogger().Debug("streaming stdout with no reconnect support") go kw.kubeLoggingNoReconnect(&streamWait, stdout, &stdoutErr) } @@ -1094,14 +1094,14 @@ func (kw *kubeUnit) connectToKube() error { qps, err := strconv.Atoi(envQPS) if err != nil { // ignore error, use default - kw.Warning("Invalid value for RECEPTOR_KUBE_CLIENTSET_QPS: %s. Ignoring", envQPS) + kw.GetWorkceptor().nc.GetLogger().Warning("Invalid value for RECEPTOR_KUBE_CLIENTSET_QPS: %s. Ignoring", envQPS) } else { kw.config.QPS = float32(qps) kw.config.Burst = qps * 10 } } - kw.Debug("RECEPTOR_KUBE_CLIENTSET_QPS: %s", envQPS) + kw.GetWorkceptor().nc.GetLogger().Debug("RECEPTOR_KUBE_CLIENTSET_QPS: %s", envQPS) // RECEPTOR_KUBE_CLIENTSET_BURST // default: 10 x QPS @@ -1109,15 +1109,15 @@ func (kw *kubeUnit) connectToKube() error { if ok { burst, err := strconv.Atoi(envBurst) if err != nil { - kw.Warning("Invalid value for RECEPTOR_KUBE_CLIENTSET_BURST: %s. Ignoring", envQPS) + kw.GetWorkceptor().nc.GetLogger().Warning("Invalid value for RECEPTOR_KUBE_CLIENTSET_BURST: %s. Ignoring", envQPS) } else { kw.config.Burst = burst } } - kw.Debug("RECEPTOR_KUBE_CLIENTSET_BURST: %s", envBurst) + kw.GetWorkceptor().nc.GetLogger().Debug("RECEPTOR_KUBE_CLIENTSET_BURST: %s", envBurst) - kw.Debug("Initializing Kubernetes clientset") + kw.GetWorkceptor().nc.GetLogger().Debug("Initializing Kubernetes clientset") // RECEPTOR_KUBE_CLIENTSET_RATE_LIMITER // default: tokenbucket // options: never, always, tokenbucket @@ -1130,10 +1130,10 @@ func (kw *kubeUnit) connectToKube() error { kw.config.RateLimiter = flowcontrol.NewFakeAlwaysRateLimiter() default: } - kw.Debug("RateLimiter: %s", envRateLimiter) + kw.GetWorkceptor().nc.GetLogger().Debug("RateLimiter: %s", envRateLimiter) } - kw.Debug("QPS: %f, Burst: %d", kw.config.QPS, kw.config.Burst) + kw.GetWorkceptor().nc.GetLogger().Debug("QPS: %f, Burst: %d", kw.config.QPS, kw.config.Burst) kw.clientset, err = kubernetes.NewForConfig(kw.config) if err != nil { return err diff --git a/pkg/workceptor/remote_work.go b/pkg/workceptor/remote_work.go index d1336b39f..a48c95c77 100644 --- a/pkg/workceptor/remote_work.go +++ b/pkg/workceptor/remote_work.go @@ -678,6 +678,10 @@ func (rw *remoteUnit) Release(force bool) error { return rw.cancelOrRelease(true, force) } +func NewRemoteWorker(bwu BaseWorkUnitForWorkUnit, w *Workceptor, unitID, workType string) WorkUnit { + return newRemoteWorker(bwu, w, unitID, workType) +} + func newRemoteWorker(bwu BaseWorkUnitForWorkUnit, w *Workceptor, unitID, workType string) WorkUnit { if bwu == nil { bwu = &BaseWorkUnit{} diff --git a/pkg/workceptor/remote_work_test.go b/pkg/workceptor/remote_work_test.go new file mode 100644 index 000000000..3e5dd7d30 --- /dev/null +++ b/pkg/workceptor/remote_work_test.go @@ -0,0 +1,57 @@ +package workceptor_test + +import ( + "context" + "sync" + "testing" + + "github.com/ansible/receptor/pkg/workceptor" + "github.com/ansible/receptor/pkg/workceptor/mock_workceptor" + "github.com/golang/mock/gomock" +) + +func createRemoteWorkTestSetup(t *testing.T) (workceptor.WorkUnit, *mock_workceptor.MockBaseWorkUnitForWorkUnit, *mock_workceptor.MockNetceptorForWorkceptor, *workceptor.Workceptor) { + ctrl := gomock.NewController(t) + ctx := context.Background() + + mockBaseWorkUnit := mock_workceptor.NewMockBaseWorkUnitForWorkUnit(ctrl) + mockNetceptor := mock_workceptor.NewMockNetceptorForWorkceptor(ctrl) + mockNetceptor.EXPECT().NodeID().Return("NodeID") + mockNetceptor.EXPECT().GetLogger() + + w, err := workceptor.New(ctx, mockNetceptor, "/tmp") + if err != nil { + t.Errorf("Error while creating Workceptor: %v", err) + } + + mockBaseWorkUnit.EXPECT().Init(w, "", "", workceptor.FileSystem{}, nil) + mockBaseWorkUnit.EXPECT().SetStatusExtraData(gomock.Any()) + workUnit := workceptor.NewRemoteWorker(mockBaseWorkUnit, w, "", "") + + return workUnit, mockBaseWorkUnit, mockNetceptor, w +} + +func TestRemoteWorkUnredactedStatus(t *testing.T) { + wu, mockBaseWorkUnit, _, _ := createRemoteWorkTestSetup(t) + restartTestCases := []struct { + name string + }{ + {name: "test1"}, + {name: "test2"}, + {name: "test3"}, + } + + for _, testCase := range restartTestCases { + t.Run(testCase.name, func(t *testing.T) { + t.Parallel() + statusLock := &sync.RWMutex{} + mockBaseWorkUnit.EXPECT().GetWorkceptor().AnyTimes() + mockBaseWorkUnit.EXPECT().GetStatusLock().Return(statusLock).Times(2) + mockBaseWorkUnit.EXPECT().GetStatusWithoutExtraData().Return(&workceptor.StatusFileData{}) + mockBaseWorkUnit.EXPECT().GetStatusCopy().Return(workceptor.StatusFileData{ + ExtraData: &workceptor.RemoteExtraData{}, + }) + wu.UnredactedStatus() + }) + } +} From c10962c2b2b7746cb89379efa2c4e2bff0bf97d8 Mon Sep 17 00:00:00 2001 From: resoluteCoder Date: Fri, 12 Jan 2024 10:13:17 -0600 Subject: [PATCH 19/21] fixed stuff --- pkg/workceptor/command_test.go | 1 + pkg/workceptor/remote_work_test.go | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/pkg/workceptor/command_test.go b/pkg/workceptor/command_test.go index 3844e7099..36a4c7c1e 100644 --- a/pkg/workceptor/command_test.go +++ b/pkg/workceptor/command_test.go @@ -89,6 +89,7 @@ func TestCommandSetFromParams(t *testing.T) { } func TestUnredactedStatus(t *testing.T) { + t.Parallel() wu, mockBaseWorkUnit, _, _ := createCommandTestSetup(t) restartTestCases := []struct { name string diff --git a/pkg/workceptor/remote_work_test.go b/pkg/workceptor/remote_work_test.go index 3e5dd7d30..249ae6dbe 100644 --- a/pkg/workceptor/remote_work_test.go +++ b/pkg/workceptor/remote_work_test.go @@ -32,6 +32,7 @@ func createRemoteWorkTestSetup(t *testing.T) (workceptor.WorkUnit, *mock_workcep } func TestRemoteWorkUnredactedStatus(t *testing.T) { + t.Parallel() wu, mockBaseWorkUnit, _, _ := createRemoteWorkTestSetup(t) restartTestCases := []struct { name string @@ -45,7 +46,6 @@ func TestRemoteWorkUnredactedStatus(t *testing.T) { t.Run(testCase.name, func(t *testing.T) { t.Parallel() statusLock := &sync.RWMutex{} - mockBaseWorkUnit.EXPECT().GetWorkceptor().AnyTimes() mockBaseWorkUnit.EXPECT().GetStatusLock().Return(statusLock).Times(2) mockBaseWorkUnit.EXPECT().GetStatusWithoutExtraData().Return(&workceptor.StatusFileData{}) mockBaseWorkUnit.EXPECT().GetStatusCopy().Return(workceptor.StatusFileData{ From 8d029b4713465ebf45289f53d876d5703b5cb8ed Mon Sep 17 00:00:00 2001 From: resoluteCoder Date: Fri, 12 Jan 2024 13:40:09 -0600 Subject: [PATCH 20/21] more stuff --- pkg/workceptor/command_test.go | 3 +-- pkg/workceptor/remote_work_test.go | 3 +-- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/pkg/workceptor/command_test.go b/pkg/workceptor/command_test.go index 36a4c7c1e..813dfaf27 100644 --- a/pkg/workceptor/command_test.go +++ b/pkg/workceptor/command_test.go @@ -96,13 +96,12 @@ func TestUnredactedStatus(t *testing.T) { }{ {name: "test1"}, {name: "test2"}, - {name: "test3"}, } + statusLock := &sync.RWMutex{} for _, testCase := range restartTestCases { t.Run(testCase.name, func(t *testing.T) { t.Parallel() - statusLock := &sync.RWMutex{} mockBaseWorkUnit.EXPECT().GetStatusLock().Return(statusLock).Times(2) mockBaseWorkUnit.EXPECT().GetStatusWithoutExtraData().Return(&workceptor.StatusFileData{}) mockBaseWorkUnit.EXPECT().GetStatusCopy().Return(workceptor.StatusFileData{ diff --git a/pkg/workceptor/remote_work_test.go b/pkg/workceptor/remote_work_test.go index 249ae6dbe..1f1e1fa01 100644 --- a/pkg/workceptor/remote_work_test.go +++ b/pkg/workceptor/remote_work_test.go @@ -39,13 +39,12 @@ func TestRemoteWorkUnredactedStatus(t *testing.T) { }{ {name: "test1"}, {name: "test2"}, - {name: "test3"}, } + statusLock := &sync.RWMutex{} for _, testCase := range restartTestCases { t.Run(testCase.name, func(t *testing.T) { t.Parallel() - statusLock := &sync.RWMutex{} mockBaseWorkUnit.EXPECT().GetStatusLock().Return(statusLock).Times(2) mockBaseWorkUnit.EXPECT().GetStatusWithoutExtraData().Return(&workceptor.StatusFileData{}) mockBaseWorkUnit.EXPECT().GetStatusCopy().Return(workceptor.StatusFileData{ From 7236810aa1f0185baed97c939e74115a9797d73f Mon Sep 17 00:00:00 2001 From: resoluteCoder Date: Mon, 15 Jan 2024 09:26:34 -0600 Subject: [PATCH 21/21] removed comment --- pkg/workceptor/kubernetes.go | 1 - 1 file changed, 1 deletion(-) diff --git a/pkg/workceptor/kubernetes.go b/pkg/workceptor/kubernetes.go index 2b7272318..d173f2870 100644 --- a/pkg/workceptor/kubernetes.go +++ b/pkg/workceptor/kubernetes.go @@ -39,7 +39,6 @@ import ( // kubeUnit implements the WorkUnit interface. type kubeUnit struct { BaseWorkUnitForWorkUnit - // BaseWorkUnit authMethod string streamMethod string baseParams string