2023-03-29 08:45:13 -04:00
|
|
|
/*
|
|
|
|
Copyright (c) Edgeless Systems GmbH
|
|
|
|
|
|
|
|
SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
|
|
|
package journald
|
|
|
|
|
|
|
|
import (
|
2023-04-03 07:58:34 -04:00
|
|
|
"bytes"
|
2023-03-29 08:45:13 -04:00
|
|
|
"errors"
|
2023-04-03 07:58:34 -04:00
|
|
|
"io"
|
2023-03-29 08:45:13 -04:00
|
|
|
"os/exec"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2023-04-03 07:58:34 -04:00
|
|
|
"github.com/stretchr/testify/require"
|
2023-03-29 08:45:13 -04:00
|
|
|
)
|
|
|
|
|
2023-04-03 07:58:34 -04:00
|
|
|
func TestPipe(t *testing.T) {
|
2023-03-29 08:45:13 -04:00
|
|
|
someError := errors.New("failed")
|
|
|
|
|
|
|
|
testCases := map[string]struct {
|
2023-04-03 07:58:34 -04:00
|
|
|
command *stubCommand
|
|
|
|
stdoutPipe io.ReadCloser
|
2023-03-29 08:45:13 -04:00
|
|
|
wantedOutput []byte
|
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
"success": {
|
2023-04-03 07:58:34 -04:00
|
|
|
command: &stubCommand{},
|
|
|
|
wantedOutput: []byte("asdf"),
|
|
|
|
stdoutPipe: &stubReadCloser{reader: bytes.NewReader([]byte("asdf"))},
|
2023-03-29 08:45:13 -04:00
|
|
|
},
|
|
|
|
"execution failed": {
|
2023-04-03 07:58:34 -04:00
|
|
|
command: &stubCommand{startErr: someError},
|
2023-03-29 08:45:13 -04:00
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
"exit error": {
|
2023-04-03 07:58:34 -04:00
|
|
|
command: &stubCommand{startErr: &exec.ExitError{}},
|
2023-03-29 08:45:13 -04:00
|
|
|
wantErr: true,
|
|
|
|
},
|
2023-04-03 07:58:34 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
for name, tc := range testCases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
|
|
|
collector := Collector{cmd: tc.command, stdoutPipe: tc.stdoutPipe}
|
|
|
|
|
|
|
|
pipe, err := collector.Start()
|
|
|
|
if tc.wantErr {
|
|
|
|
assert.Error(err)
|
|
|
|
} else {
|
|
|
|
stdout := make([]byte, 4)
|
|
|
|
_, err = io.ReadFull(pipe, stdout)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(tc.wantedOutput, stdout)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestError(t *testing.T) {
|
|
|
|
someError := errors.New("failed")
|
|
|
|
|
|
|
|
testCases := map[string]struct {
|
|
|
|
stderrPipe io.ReadCloser
|
|
|
|
exitCode error
|
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
"success": {
|
|
|
|
stderrPipe: &stubReadCloser{readErr: io.EOF},
|
|
|
|
},
|
|
|
|
"reading error": {
|
|
|
|
stderrPipe: &stubReadCloser{readErr: someError},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
"close error": {
|
|
|
|
stderrPipe: &stubReadCloser{closeErr: someError, readErr: io.EOF},
|
|
|
|
},
|
|
|
|
"command exit": {
|
|
|
|
stderrPipe: &stubReadCloser{readErr: io.EOF},
|
|
|
|
exitCode: someError,
|
|
|
|
wantErr: true,
|
2023-03-29 08:45:13 -04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, tc := range testCases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
2023-04-03 07:58:34 -04:00
|
|
|
collector := Collector{
|
|
|
|
stderrPipe: tc.stderrPipe,
|
|
|
|
cmd: &stubCommand{waitErr: tc.exitCode},
|
|
|
|
}
|
2023-03-29 08:45:13 -04:00
|
|
|
|
2023-04-03 07:58:34 -04:00
|
|
|
stderrOut, err := collector.Error()
|
2023-03-29 08:45:13 -04:00
|
|
|
if tc.wantErr {
|
|
|
|
assert.Error(err)
|
2023-04-03 07:58:34 -04:00
|
|
|
} else {
|
|
|
|
assert.Equal(stderrOut, []byte{})
|
2023-03-29 08:45:13 -04:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2023-04-03 07:58:34 -04:00
|
|
|
|
|
|
|
type stubCommand struct {
|
|
|
|
startCalled bool
|
|
|
|
startErr error
|
|
|
|
waitErr error
|
|
|
|
}
|
|
|
|
|
|
|
|
func (j *stubCommand) Start() error {
|
|
|
|
j.startCalled = true
|
|
|
|
return j.startErr
|
|
|
|
}
|
|
|
|
|
|
|
|
func (j *stubCommand) Wait() error {
|
|
|
|
return j.waitErr
|
|
|
|
}
|
|
|
|
|
|
|
|
type stubReadCloser struct {
|
|
|
|
reader io.Reader
|
|
|
|
readErr error
|
|
|
|
closeErr error
|
|
|
|
}
|
2023-05-30 07:47:36 -04:00
|
|
|
|
|
|
|
func (s *stubReadCloser) Read(p []byte) (n int, err error) {
|
|
|
|
if s.readErr != nil {
|
|
|
|
return 0, s.readErr
|
|
|
|
}
|
|
|
|
return s.reader.Read(p)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *stubReadCloser) Close() error {
|
|
|
|
return s.closeErr
|
|
|
|
}
|