Skip to content

Commit

Permalink
Fix linting issues
Browse files Browse the repository at this point in the history
  • Loading branch information
AaronH88 committed Aug 8, 2023
1 parent 862d568 commit f2f0b1b
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 71 deletions.
8 changes: 4 additions & 4 deletions pkg/workceptor/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ func (kw *kubeUnit) kubeLoggingConnectionHandler(timestamps bool) (io.ReadCloser
return logStream, nil
}

func (kw *kubeUnit) kubeLoggingNoReconnect(streamWait *sync.WaitGroup, stdout *stdoutWriter, stdoutErr *error) {
func (kw *kubeUnit) kubeLoggingNoReconnect(streamWait *sync.WaitGroup, stdout *STDoutWriter, stdoutErr *error) {
// Legacy method, for use on k8s < v1.23.14
// uses io.Copy to stream data from pod to stdout file
// known issues around this, as logstream can terminate due to log rotation
Expand All @@ -190,7 +190,7 @@ func (kw *kubeUnit) kubeLoggingNoReconnect(streamWait *sync.WaitGroup, stdout *s
}
}

func (kw *kubeUnit) kubeLoggingWithReconnect(streamWait *sync.WaitGroup, stdout *stdoutWriter, stdinErr *error, stdoutErr *error) {
func (kw *kubeUnit) kubeLoggingWithReconnect(streamWait *sync.WaitGroup, stdout *STDoutWriter, stdinErr *error, stdoutErr *error) {
// preferred method for k8s >= 1.23.14
defer streamWait.Done()
var sinceTime time.Time
Expand Down Expand Up @@ -606,7 +606,7 @@ func (kw *kubeUnit) runWorkUsingLogger() {
stdinErrChan := make(chan struct{}) // signal that stdin goroutine have errored and stop stdout goroutine

// open stdin reader that reads from the work unit's data directory
var stdin *stdinReader
var stdin *STDinReader
if !skipStdin {
var err error
stdin, err = NewStdinReader(FileSystem{}, kw.UnitDir())
Expand Down Expand Up @@ -950,7 +950,7 @@ func (kw *kubeUnit) runWorkUsingTCP() {
}

// Open stdin reader
var stdin *stdinReader
var stdin *STDinReader
stdin, err = NewStdinReader(FileSystem{}, kw.UnitDir())
if err != nil {
errMsg := fmt.Sprintf("Error opening stdin file: %s", err)
Expand Down
30 changes: 15 additions & 15 deletions pkg/workceptor/stdio_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,29 +56,29 @@ func saveStdoutSize(unitdir string, stdoutSize int64) error {
})
}

// StdoutWriter writes to a stdout file while also updating the status file.
type stdoutWriter struct {
// STDoutWriter writes to a stdout file while also updating the status file.
type STDoutWriter struct {
unitdir string
writer FileWriteCloser
bytesWritten int64
}

// NewStdoutWriter allocates a new stdoutWriter, which writes to both the stdout and status files.
func NewStdoutWriter(fs FileSystemer, unitdir string) (*stdoutWriter, error) {
func NewStdoutWriter(fs FileSystemer, unitdir string) (*STDoutWriter, error) {
writer, err := fs.OpenFile(path.Join(unitdir, "stdout"), os.O_CREATE+os.O_WRONLY+os.O_SYNC, 0o600)
if err != nil {
return nil, err
}

return &stdoutWriter{
return &STDoutWriter{
unitdir: unitdir,
writer: writer,
bytesWritten: 0,
}, nil
}

// Write writes data to the stdout file and status file, implementing io.Writer.
func (sw *stdoutWriter) Write(p []byte) (n int, err error) {
func (sw *STDoutWriter) Write(p []byte) (n int, err error) {
wn, werr := sw.writer.Write(p)
var serr error
if wn > 0 {
Expand All @@ -93,17 +93,17 @@ func (sw *stdoutWriter) Write(p []byte) (n int, err error) {
}

// Size returns the current size of the stdout file.
func (sw *stdoutWriter) Size() int64 {
func (sw *STDoutWriter) Size() int64 {
return sw.bytesWritten
}

// SetWriter sets the writer var.
func (sw *stdoutWriter) SetWriter(writer FileWriteCloser) {
func (sw *STDoutWriter) SetWriter(writer FileWriteCloser) {
sw.writer = writer
}

// stdinReader reads from a stdin file and provides a Done function.
type stdinReader struct {
// STDinReader reads from a stdin file and provides a Done function.
type STDinReader struct {
reader FileReadCloser
lasterr error
doneChan chan struct{}
Expand All @@ -113,7 +113,7 @@ type stdinReader struct {
var errFileSizeZero = errors.New("file is empty")

// NewStdinReader allocates a new stdinReader, which reads from a stdin file and provides a Done function.
func NewStdinReader(fs FileSystemer, unitdir string) (*stdinReader, error) {
func NewStdinReader(fs FileSystemer, unitdir string) (*STDinReader, error) {
stdinpath := path.Join(unitdir, "stdin")
stat, err := fs.Stat(stdinpath)
if err != nil {
Expand All @@ -127,7 +127,7 @@ func NewStdinReader(fs FileSystemer, unitdir string) (*stdinReader, error) {
return nil, err
}

return &stdinReader{
return &STDinReader{
reader: reader,
lasterr: nil,
doneChan: make(chan struct{}),
Expand All @@ -136,7 +136,7 @@ func NewStdinReader(fs FileSystemer, unitdir string) (*stdinReader, error) {
}

// Read reads data from the stdout file, implementing io.Reader.
func (sr *stdinReader) Read(p []byte) (n int, err error) {
func (sr *STDinReader) Read(p []byte) (n int, err error) {
n, err = sr.reader.Read(p)
if err != nil {
sr.lasterr = err
Expand All @@ -149,16 +149,16 @@ func (sr *stdinReader) Read(p []byte) (n int, err error) {
}

// Done returns a channel that will be closed on error (including EOF) in the reader.
func (sr *stdinReader) Done() <-chan struct{} {
func (sr *STDinReader) Done() <-chan struct{} {
return sr.doneChan
}

// Error returns the most recent error encountered in the reader.
func (sr *stdinReader) Error() error {
func (sr *STDinReader) Error() error {
return sr.lasterr
}

// SetReader sets the reader var.
func (sr *stdinReader) SetReader(reader FileReadCloser) {
func (sr *STDinReader) SetReader(reader FileReadCloser) {
sr.reader = reader
}
85 changes: 33 additions & 52 deletions pkg/workceptor/stdio_utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,44 @@ func checkErrorAndNum(err error, expectedErr string, num int, expectedNum int, t
}
}

func TestWrite(t *testing.T) {
func setup(t *testing.T) (*gomock.Controller, *mock_workceptor.MockFileSystemer) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

mockfilesystemer := mock_workceptor.NewMockFileSystemer(ctrl)
mockfilewc := mock_workceptor.NewMockFileWriteCloser(ctrl)

return ctrl, mockfilesystemer
}

func setupWriter(t *testing.T) (*gomock.Controller, *workceptor.STDoutWriter) {
ctrl, mockfilesystemer := setup(t)
mockfilesystemer.EXPECT().OpenFile(gomock.Any(), gomock.Any(), gomock.Any()).Return(&os.File{}, nil)
wc, err := workceptor.NewStdoutWriter(mockfilesystemer, "")
if err != nil {
t.Errorf("Error while creating std writer: %v", err)
}

return ctrl, wc
}

func setupReader(t *testing.T) (*gomock.Controller, *workceptor.STDinReader) {
ctrl, mockfilesystemer := setup(t)
statObj := NewInfo("test", 1, 0, time.Now())

mockfilesystemer.EXPECT().Stat(gomock.Any()).Return(statObj, nil)
mockfilesystemer.EXPECT().Open(gomock.Any()).Return(&os.File{}, nil)

wc, err := workceptor.NewStdinReader(mockfilesystemer, "")
if err != nil {
t.Errorf(stdinError)
}

return ctrl, wc
}

func TestWrite(t *testing.T) {
ctrl, wc := setupWriter(t)
mockfilewc := mock_workceptor.NewMockFileWriteCloser(ctrl)
wc.SetWriter(mockfilewc)

writeTestCases := []struct {
Expand All @@ -59,16 +85,7 @@ func TestWrite(t *testing.T) {
}

func TestWriteSize(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

mockfilesystemer := mock_workceptor.NewMockFileSystemer(ctrl)

mockfilesystemer.EXPECT().OpenFile(gomock.Any(), gomock.Any(), gomock.Any()).Return(&os.File{}, nil)
wc, err := workceptor.NewStdoutWriter(mockfilesystemer, "")
if err != nil {
t.Errorf("Error while creating std writer: %v", err)
}
_, wc := setupWriter(t)

sizeTestCases := []struct {
name string
Expand Down Expand Up @@ -130,19 +147,7 @@ func (i *Info) Sys() interface{} {
const stdinError = "Error creating stdinReader"

func TestRead(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

mockfilesystemer := mock_workceptor.NewMockFileSystemer(ctrl)
statObj := NewInfo("test", 1, 0, time.Now())

mockfilesystemer.EXPECT().Stat(gomock.Any()).Return(statObj, nil)
mockfilesystemer.EXPECT().Open(gomock.Any()).Return(&os.File{}, nil)

wc, err := workceptor.NewStdinReader(mockfilesystemer, "")
if err != nil {
t.Errorf(stdinError)
}
ctrl, wc := setupReader(t)

mockReadClose := mock_workceptor.NewMockFileReadCloser(ctrl)
wc.SetReader(mockReadClose)
Expand All @@ -168,19 +173,7 @@ func TestRead(t *testing.T) {
}

func TestDone(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

mockfilesystemer := mock_workceptor.NewMockFileSystemer(ctrl)
statObj := NewInfo("test", 1, 0, time.Now())

mockfilesystemer.EXPECT().Stat(gomock.Any()).Return(statObj, nil)
mockfilesystemer.EXPECT().Open(gomock.Any()).Return(&os.File{}, nil)

wc, err := workceptor.NewStdinReader(mockfilesystemer, "")
if err != nil {
t.Errorf(stdinError)
}
_, wc := setupReader(t)

channel := wc.Done()
if channel == nil {
Expand All @@ -189,21 +182,9 @@ func TestDone(t *testing.T) {
}

func TestError(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

mockfilesystemer := mock_workceptor.NewMockFileSystemer(ctrl)
statObj := NewInfo("test", 1, 0, time.Now())

mockfilesystemer.EXPECT().Stat(gomock.Any()).Return(statObj, nil)
mockfilesystemer.EXPECT().Open(gomock.Any()).Return(&os.File{}, nil)

wc, err := workceptor.NewStdinReader(mockfilesystemer, "")
if err != nil {
t.Errorf(stdinError)
}
_, wc := setupReader(t)

err = wc.Error()
err := wc.Error()
if err != nil {
t.Errorf("Unexpected error returned from stdreader")
}
Expand Down

0 comments on commit f2f0b1b

Please sign in to comment.