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 28, 2023
1 parent fb51e50 commit 21d7953
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 69 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
}
153 changes: 103 additions & 50 deletions pkg/workceptor/stdio_utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,30 +11,58 @@ import (
"github.com/golang/mock/gomock"
)

const errorMsgFmt = "Expected error: %s, got: %v"

// checkErrorAndNum checks common return types against expected values.
func checkErrorAndNum(err error, expectedErr string, num int, expectedNum int, t *testing.T) {
if expectedErr == "" && err != nil {
t.Errorf("Expected no error, got: %v", err)
} else if expectedErr != "" && (err == nil || err.Error() != expectedErr) {
t.Errorf("Expected error: %s, got: %v", expectedErr, err)
t.Errorf(errorMsgFmt, expectedErr, err)
}
if num != expectedNum {
t.Errorf("Expected num to be %d, got: %d", expectedNum, num)
}
}

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 +87,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 +149,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,43 +175,89 @@ func TestRead(t *testing.T) {
}

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

mockfilesystemer := mock_workceptor.NewMockFileSystemer(ctrl)
statObj := NewInfo("test", 1, 0, time.Now())
channel := wc.Done()
if channel == nil {
t.Errorf("Done chan is set to nil")
}
}

mockfilesystemer.EXPECT().Stat(gomock.Any()).Return(statObj, nil)
mockfilesystemer.EXPECT().Open(gomock.Any()).Return(&os.File{}, nil)
func TestError(t *testing.T) {
_, wc := setupReader(t)

wc, err := workceptor.NewStdinReader(mockfilesystemer, "")
err := wc.Error()
if err != nil {
t.Errorf(stdinError)
t.Errorf("Unexpected error returned from stdreader")
}
}

func TestNewStdoutWriter(t *testing.T) {
_, mockfilesystemer := setup(t)

newWriterTestCases := []struct {
name string
returnErr error
expectedErr string
}{
{"Create Writer OK", nil, ""},
{"Create Writer Error", errors.New("Create Write error"), "Create Write error"},
}

channel := wc.Done()
if channel == nil {
t.Errorf("Done chan is set to nil")
for _, testCase := range newWriterTestCases {
t.Run(testCase.name, func(t *testing.T) {
mockfilesystemer.EXPECT().OpenFile(gomock.Any(), gomock.Any(), gomock.Any()).Return(&os.File{}, testCase.returnErr)
_, err := workceptor.NewStdoutWriter(mockfilesystemer, "")
checkErrorAndNum(err, testCase.expectedErr, 0, 0, t)
})
}
}

func TestError(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
func checkErrorsReader(err error, expectedStatErr string, expectedOpenErr string, expectedStatSize int, t *testing.T) {
switch {
case expectedStatErr == "" && expectedOpenErr == "" && expectedStatSize > 0 && err != nil:
t.Errorf("Expected no error, got: %v", err)

mockfilesystemer := mock_workceptor.NewMockFileSystemer(ctrl)
statObj := NewInfo("test", 1, 0, time.Now())
case expectedStatErr != "" && (err == nil || err.Error() != expectedStatErr):
t.Errorf(errorMsgFmt, expectedStatErr, err)

mockfilesystemer.EXPECT().Stat(gomock.Any()).Return(statObj, nil)
mockfilesystemer.EXPECT().Open(gomock.Any()).Return(&os.File{}, nil)
case expectedOpenErr != "" && (err == nil || err.Error() != expectedOpenErr):
t.Errorf(errorMsgFmt, expectedOpenErr, err)

wc, err := workceptor.NewStdinReader(mockfilesystemer, "")
if err != nil {
t.Errorf(stdinError)
case expectedStatSize < 1 && (err == nil || err.Error() != "file is empty"):
t.Errorf(errorMsgFmt, "file is empty", err)
}
}

func TestNewStdinReader(t *testing.T) {
_, mockfilesystemer := setup(t)

readTestCases := []struct {
name string
returnStatSize int
returnStatErr error
expectedStatSize int
expectedStatErr string
returnOpenErr error
expectedOpenErr string
mockOpen bool
}{
{"Create Read ok", 1, nil, 1, "", nil, "", true},
{"Create Read Stat Error", 1, errors.New("Create Read Stat error"), 1, "Create Read Stat error", nil, "", false},
{"Create Read Size Error", 0, nil, 0, "", nil, "", false},
{"Create Read Open Error", 1, nil, 1, "", errors.New("Create Read Open error"), "Create Read Open error", true},
}

err = wc.Error()
if err != nil {
t.Errorf("Unexpected error returned from stdreader")
for _, testCase := range readTestCases {
t.Run(testCase.name, func(t *testing.T) {
statObj := NewInfo("test", int64(testCase.returnStatSize), 0, time.Now())
mockfilesystemer.EXPECT().Stat(gomock.Any()).Return(statObj, testCase.returnStatErr)
if testCase.mockOpen {
mockfilesystemer.EXPECT().Open(gomock.Any()).Return(&os.File{}, testCase.returnOpenErr)
}

_, err := workceptor.NewStdinReader(mockfilesystemer, "")
checkErrorsReader(err, testCase.expectedStatErr, testCase.expectedOpenErr, testCase.expectedStatSize, t)
})
}
}

0 comments on commit 21d7953

Please sign in to comment.