Skip to content

Commit

Permalink
ci: add lint for e2e and unit
Browse files Browse the repository at this point in the history
Signed-off-by: Ziwen Ning <[email protected]>
  • Loading branch information
ningziwen committed Sep 30, 2023
1 parent d03a148 commit 78951c3
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 29 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ jobs:
# There may not be an automatic way (e.g., dependabot) to update a specific parameter of a GitHub Action,
# so we will just update it manually whenever it makes sense (e.g., a feature that we want is added).
version: v1.54.0
args: --fix=false --timeout=5m --out-format=colored-line-number
args: --build-tags e2e,unit --fix=false --timeout=5m --out-format=colored-line-number
unit-tests:
strategy:
fail-fast: false
Expand Down
4 changes: 4 additions & 0 deletions .golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ linters-settings:
stylecheck:
# ST1003 is left out because it is a bit opinionated.
checks: ["all", "-ST1003"]
paralleltest:
# Ignore missing calls to `t.Parallel()` and only report incorrect uses of it.
# Default: false
ignore-missing: true
linters:
enable:
- errname
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ coverage:

.PHONY: lint
lint: $(SOURCES)
$(DEPSPATH)/golangci-lint run
$(DEPSPATH)/golangci-lint run --build-tags e2e,unit

.PHONY: mdlint
# Install it locally: https://github.com/igorshubovych/markdownlint-cli#installation
Expand Down
20 changes: 10 additions & 10 deletions args_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func TestGetGlobalArgs(t *testing.T) {
}

// testGetGlobalArgsNoError is a sub-test of TestGetGlobalArgs. It tests
// getGlobalArgs function with no returned errors
// getGlobalArgs function with no returned errors.
func testGetGlobalArgsNoError(t *testing.T) {
// Unset all keys used for this test
defer viper.Reset()
Expand All @@ -67,7 +67,7 @@ func testGetGlobalArgsNoError(t *testing.T) {
assert.Equal(t, args.LogDriver, testLogDriver)
assert.Equal(t, args.Mode, blockingMode)
assert.Equal(t, args.MaxBufferSize, 0)
assert.Equal(t, *args.CleanupTime, time.Duration(5*time.Second))
assert.Equal(t, *args.CleanupTime, 5*time.Second)
}

// testGetGlobalArgsWithError is a sub-test of TestGetGlobalArgs. It tests
Expand Down Expand Up @@ -197,7 +197,7 @@ func testGetMaxBufferSizeWithError(t *testing.T) {
}
}

// TestGetCleanupTime tests getCleanupTime with/without valid setting cleanup time options
// TestGetCleanupTime tests getCleanupTime with/without valid setting cleanup time options.
func TestGetCleanupTime(t *testing.T) {
t.Run("NoError", testGetCleanupTimeNoError)
t.Run("WithError", testGetCleanupTimeWithError)
Expand All @@ -213,9 +213,9 @@ func testGetCleanupTimeNoError(t *testing.T) {
cleanupTime string
expectedCleanupTime time.Duration
}{
{"3s", time.Duration(3 * time.Second)},
{"10s", time.Duration(10 * time.Second)},
{"12s", time.Duration(12 * time.Second)},
{"3s", 3 * time.Second},
{"10s", 10 * time.Second},
{"12s", 12 * time.Second},
}

for _, tc := range testCasesNoError {
Expand Down Expand Up @@ -246,14 +246,14 @@ func testGetCleanupTimeWithError(t *testing.T) {
}
}

// TestGetDockerConfigs tests that we can correctly get the docker config input parameters
// TestGetDockerConfigs tests that we can correctly get the docker config input parameters.
func TestGetDockerConfigs(t *testing.T) {
t.Run("NoError", testGetDockerConfigsNoError)
t.Run("Empty", testGetDockerConfigsEmpty)
t.Run("WithError", testGetDockerConfigsWithError)
}

// testGetDockerConfigsNoError tests that the correctly formatted input can be parsed without error
// testGetDockerConfigsNoError tests that the correctly formatted input can be parsed without error.
func testGetDockerConfigsNoError(t *testing.T) {
defer viper.Reset()

Expand Down Expand Up @@ -283,15 +283,15 @@ func testGetDockerConfigsNoError(t *testing.T) {
assert.Equal(t, true, reflect.DeepEqual(testContainerEnvMap, argsContainerEnvMap))
}

// testGetDockerConfigsEmpty tests that empty docker config input parameter generates no error
// testGetDockerConfigsEmpty tests that empty docker config input parameter generates no error.
func testGetDockerConfigsEmpty(t *testing.T) {
defer viper.Reset()

_, err := getDockerConfigs()
require.NoError(t, err)
}

// testGetDockerConfigsWithError tests that malformat docker config results in an error
// testGetDockerConfigsWithError tests that malformat docker config results in an error.
func testGetDockerConfigsWithError(t *testing.T) {
defer viper.Reset()
testCaseWithError := "{invalidJsonMap"
Expand Down
2 changes: 1 addition & 1 deletion logger/awslogs/logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const (
testGroup = "test-group"
testRegion = "test-region"
testStream = "test-stream"
testCredentialsEndpoint = "test-credential-endpoints"
testCredentialsEndpoint = "test-credential-endpoints" //nolint:gosec // not credentials
testCreateGroup = "true"
testCreateStream = "true"
testMultilinePattern = "test-multiline-pattern"
Expand Down
5 changes: 2 additions & 3 deletions logger/buffered_logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,7 @@ func testEnqueue(t *testing.T) *ringBuffer {
return lb
}

// TestLogBufferEnqueueDequeue tests dequeue operations from
// buffer
// TestLogBufferEnqueueDequeue tests dequeue operations from buffer.
func TestLogBufferEnqueueDequeue(t *testing.T) {
lb := testEnqueue(t)
queueLen := len(lb.queue)
Expand All @@ -63,7 +62,7 @@ func TestLogBufferEnqueueDequeue(t *testing.T) {
require.Len(t, lb.queue, 0)
}

// TestLogBufferEnqueueFlush tests flush messages from buffer
// TestLogBufferEnqueueFlush tests flush messages from buffer.
func TestLogBufferEnqueueFlush(t *testing.T) {
lb := testEnqueue(t)
flushedMsg := lb.Flush()
Expand Down
22 changes: 11 additions & 11 deletions logger/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,9 @@ const (
)

var (
dummyLogMsg = []byte("test log message")
dummySource = "stdout"
dummyTime = time.Date(2020, time.January, 14, 01, 59, 0, 0, time.UTC)
dummyCleanupTime = time.Duration(2 * time.Second)
dummyCleanupTime = 2 * time.Second
logDestinationFileName string
)

Expand All @@ -54,15 +53,15 @@ func (d *dummyClient) Log(msg *dockerlogger.Message) error {
if err != nil {
return err
}
f, err := os.OpenFile(logDestinationFileName, os.O_APPEND|os.O_RDWR, 0644)
f, err := os.OpenFile(logDestinationFileName, os.O_APPEND|os.O_RDWR, 0644) //nolint:gosec // testing only
if err != nil {
return fmt.Errorf("unable to open file %s to record log message: %w", logDestinationFileName, err)
}
defer f.Close()
defer f.Close() //nolint:errcheck // testing only
b, err = json.Marshal(msg)
require.NoError(d.t, err)
f.Write(b)
f.Write([]byte{'\n'})
f.Write(b) //nolint:errcheck,gosec // testing only
f.Write([]byte{'\n'}) //nolint:errcheck,gosec // testing only

return nil
}
Expand All @@ -75,9 +74,9 @@ func checkLogFile(t *testing.T, fileName string, expectedNumLines int,
lastPartialID string
lastPartialOrdinal int
)
file, err := os.Open(fileName)
file, err := os.Open(fileName) //nolint:gosec // testing only
require.NoError(t, err)
defer file.Close()
defer file.Close() //nolint:errcheck // testing only

scanner := bufio.NewScanner(file)
lines := 0
Expand Down Expand Up @@ -111,7 +110,6 @@ func checkLogFile(t *testing.T, fileName string, expectedNumLines int,
// to mock destination. In this test case, the source and destination are both tmp files that
// read from and write to inside the customized Log function.
func TestSendLogs(t *testing.T) {

for _, tc := range []struct {
testName string
bufferSizeInBytes int
Expand Down Expand Up @@ -153,7 +151,9 @@ func TestSendLogs(t *testing.T) {
expectedPartialOrdinalSequence: []int{1, 2, 3, 1, 2, 3},
},
} {
tc := tc
t.Run(tc.testName, func(t *testing.T) {
t.Parallel()
l := &Logger{
Info: &dockerlogger.Info{},
Stream: &dummyClient{t},
Expand All @@ -164,7 +164,7 @@ func TestSendLogs(t *testing.T) {
// messages from.
tmpIOSource, err := os.CreateTemp("", "")
require.NoError(t, err)
defer os.Remove(tmpIOSource.Name())
defer os.Remove(tmpIOSource.Name()) //nolint:errcheck // testing only
var (
testPipe bytes.Buffer
)
Expand All @@ -177,7 +177,7 @@ func TestSendLogs(t *testing.T) {
// logger sends log messages to.
tmpDest, err := os.CreateTemp("", "")
require.NoError(t, err)
defer os.Remove(tmpDest.Name())
defer os.Remove(tmpDest.Name()) //nolint:errcheck // testing only
logDestinationFileName = tmpDest.Name()

var errGroup errgroup.Group
Expand Down
3 changes: 1 addition & 2 deletions logger/splunk/logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,7 @@ var (
}
)

// TestGetSplunkConfig tests if all arguments are converted in correctly
// for splunk driver configuration
// TestGetSplunkConfig tests if all arguments are converted in correctly for splunk driver configuration.
func TestGetSplunkConfig(t *testing.T) {
expectedConfig := map[string]string{
TokenKey: testToken,
Expand Down

0 comments on commit 78951c3

Please sign in to comment.