diff --git a/.golangci.yaml b/.golangci.yaml index 45e2760742..ef82bee794 100644 --- a/.golangci.yaml +++ b/.golangci.yaml @@ -4,26 +4,37 @@ run: linters: enable: + - bodyclose - canonicalheader - containedctx - contextcheck - - exhaustruct - copyloopvar + - errname + - exhaustruct + - exptostd - fatcontext - gci - gocritic - gocyclo - gofmt - gomodguard + - iface + - makezero - misspell + - nilerr + - nilnesserr - nolintlint - prealloc + - predeclared - reassign + - recvcheck - revive - testifylint - unconvert - unparam - unused + - usetesting + - wastedassign - whitespace issues: diff --git a/bin/.golangci-lint-1.62.2.pkg b/bin/.golangci-lint-1.63.3.pkg similarity index 100% rename from bin/.golangci-lint-1.62.2.pkg rename to bin/.golangci-lint-1.63.3.pkg diff --git a/bin/golangci-lint b/bin/golangci-lint index 0ba8ac4932..f48cc7fcf6 120000 --- a/bin/golangci-lint +++ b/bin/golangci-lint @@ -1 +1 @@ -.golangci-lint-1.62.2.pkg \ No newline at end of file +.golangci-lint-1.63.3.pkg \ No newline at end of file diff --git a/internal/inventory/azurefetcher/fetcher_storage.go b/internal/inventory/azurefetcher/fetcher_storage.go index 5a2987f853..8676f8c662 100644 --- a/internal/inventory/azurefetcher/fetcher_storage.go +++ b/internal/inventory/azurefetcher/fetcher_storage.go @@ -78,7 +78,7 @@ func (f *storageFetcher) listStorageAccounts(ctx context.Context) ([]azurelib.Az return nil, fmt.Errorf("error listing subscriptions: %v", err) } - subscriptionIds := make([]string, len(subscriptions)) + subscriptionIds := make([]string, 0, len(subscriptions)) for _, subscription := range subscriptions { subscriptionIds = append(subscriptionIds, subscription.Name) } diff --git a/internal/launcher/launcher.go b/internal/launcher/launcher.go index 89aadd5bed..a177ef970d 100644 --- a/internal/launcher/launcher.go +++ b/internal/launcher/launcher.go @@ -40,14 +40,14 @@ const ( shutdownGracePeriod = 20 * time.Second ) -// ErrorStopSignal is used to indicate we got a stop signal -var ErrorStopSignal = errors.New("stop beater") +// ErrStopSignal is used to indicate we got a stop signal +var ErrStopSignal = errors.New("stop beater") -// ErrorGracefulExit is used when the launcher stops before shutdownGracePeriod, after waiting for the beater to stop -var ErrorGracefulExit = beat.GracefulExit +// ErrGracefulExit is used when the launcher stops before shutdownGracePeriod, after waiting for the beater to stop +var ErrGracefulExit = beat.GracefulExit -// ErrorTimeoutExit is used when the launcher stops after shutdownGracePeriod, without waiting for the beater to stop -var ErrorTimeoutExit = errors.New("exit after timeout") +// ErrTimeoutExit is used when the launcher stops after shutdownGracePeriod, without waiting for the beater to stop +var ErrTimeoutExit = errors.New("exit after timeout") type launcher struct { wg sync.WaitGroup // WaitGroup used to wait for active beaters @@ -115,9 +115,9 @@ func (l *launcher) run() error { err := l.runLoop() switch { - case errors.Is(err, ErrorGracefulExit): + case errors.Is(err, ErrGracefulExit): l.log.Info("Launcher stopped successfully") - case errors.Is(err, ErrorTimeoutExit): + case errors.Is(err, ErrTimeoutExit): l.log.Info("Launcher stopped after timeout") case err == nil: // unexpected default: @@ -140,7 +140,7 @@ func (l *launcher) runLoop() error { // Wait for something to happen: // config update (val, nil) - // stop signal (nil, ErrorStopSignal) + // stop signal (nil, ErrStopSignal) // beater error (nil, err) cfg, err := l.waitForUpdates() @@ -224,16 +224,16 @@ func (l *launcher) stopBeaterWithTimeout(duration time.Duration) error { select { case <-time.After(duration): l.log.Infof("Grace period for %s ended", l.name) - return ErrorTimeoutExit + return ErrTimeoutExit case <-wgCh: l.log.Infof("Launcher shut %s down gracefully", l.name) - return ErrorGracefulExit + return ErrGracefulExit } } // waitForUpdates is the function that keeps Launcher runLoop busy. // It will finish for one of following reasons: -// 1. The Stop function got called (nil, ErrorStopSignal) +// 1. The Stop function got called (nil, ErrStopSignal) // 2. The beater run has returned (nil, err) // 3. A config update received (val, nil) func (l *launcher) waitForUpdates() (*config.C, error) { @@ -241,7 +241,7 @@ func (l *launcher) waitForUpdates() (*config.C, error) { case err, ok := <-l.beaterErr: if !ok { l.log.Infof("Launcher received a stop signal") - return nil, ErrorStopSignal + return nil, ErrStopSignal } return nil, err @@ -260,7 +260,7 @@ func isConfigUpdate(cfg *config.C, err error) bool { } func isStopSignal(cfg *config.C, err error) bool { - return cfg == nil && errors.Is(err, ErrorStopSignal) + return cfg == nil && errors.Is(err, ErrStopSignal) } func isBeaterError(cfg *config.C, err error) bool { diff --git a/internal/launcher/launcher_test.go b/internal/launcher/launcher_test.go index c72f1d0379..9b19a2de57 100644 --- a/internal/launcher/launcher_test.go +++ b/internal/launcher/launcher_test.go @@ -304,7 +304,7 @@ func (s *LauncherTestSuite) TestWaitForUpdates() { }(tt.configs) err := sut.run() - s.Require().ErrorIs(err, ErrorGracefulExit) + s.Require().ErrorIs(err, ErrGracefulExit) beater, ok := sut.beater.(*beaterMock) s.Require().True(ok) s.Equal(tt.expected, beater.cfg) @@ -450,7 +450,7 @@ func (s *LauncherTestSuite) TestLauncherUpdateAndStop() { sut.Stop() }() err := sut.run() - s.Require().ErrorIs(err, ErrorGracefulExit) + s.Require().ErrorIs(err, ErrGracefulExit) } func (s *LauncherTestSuite) TestLauncherStopTwicePanics() { @@ -461,7 +461,7 @@ func (s *LauncherTestSuite) TestLauncherStopTwicePanics() { sut.Stop() }() err := sut.run() - s.Require().ErrorIs(err, ErrorGracefulExit) + s.Require().ErrorIs(err, ErrGracefulExit) s.Panics(func() { sut.Stop() @@ -481,7 +481,7 @@ func (s *LauncherTestSuite) TestLauncherStop() { }() err := sut.run() - s.Require().ErrorIs(err, ErrorGracefulExit) + s.Require().ErrorIs(err, ErrGracefulExit) } func (s *LauncherTestSuite) TestLauncherStopTimeout() { @@ -496,7 +496,7 @@ func (s *LauncherTestSuite) TestLauncherStopTimeout() { }() err := sut.run() - s.Require().ErrorIs(err, ErrorTimeoutExit) + s.Require().ErrorIs(err, ErrTimeoutExit) } func (s *LauncherTestSuite) initMocks() *launcherMocks { diff --git a/internal/processor/add_cluster_id/errors.go b/internal/processor/add_cluster_id/errors.go index 60ed61319f..a68ff3f5fe 100644 --- a/internal/processor/add_cluster_id/errors.go +++ b/internal/processor/add_cluster_id/errors.go @@ -22,26 +22,26 @@ import ( ) type ( - errConfigUnpack struct{ cause error } - errComputeID struct{ cause error } + configUnpackError struct{ cause error } + computeIDError struct{ cause error } ) -func makeErrConfigUnpack(cause error) errConfigUnpack { - return errConfigUnpack{cause} +func makeErrConfigUnpack(cause error) configUnpackError { + return configUnpackError{cause} } -func (e errConfigUnpack) Error() string { +func (e configUnpackError) Error() string { return fmt.Sprintf("failed to unpack %v processor configuration: %v", processorName, e.cause) } -func (e errConfigUnpack) Unwrap() error { +func (e configUnpackError) Unwrap() error { return e.cause } -func makeErrComputeID(cause error) errComputeID { - return errComputeID{cause} +func makeErrComputeID(cause error) computeIDError { + return computeIDError{cause} } -func (e errComputeID) Error() string { +func (e computeIDError) Error() string { return fmt.Sprintf("failed to compute ID: %v", e.cause) } -func (e errComputeID) Unwrap() error { +func (e computeIDError) Unwrap() error { return e.cause } diff --git a/internal/resources/providers/awslib/current_region_selecot_test.go b/internal/resources/providers/awslib/current_region_selecot_test.go index df08c57e14..b6084da440 100644 --- a/internal/resources/providers/awslib/current_region_selecot_test.go +++ b/internal/resources/providers/awslib/current_region_selecot_test.go @@ -35,7 +35,7 @@ var successfulCurrentCloudRegionOutput = &ec2imds.InstanceIdentityDocument{ type CurrentRegionSelectorTestSuite struct { suite.Suite selector *currentRegionSelector - mock *mockCurrentCloudRegion + mock *MockMetadataProvider } func TestCurrentRegionSelectorTestSuite(t *testing.T) { @@ -46,7 +46,7 @@ func TestCurrentRegionSelectorTestSuite(t *testing.T) { func (s *CurrentRegionSelectorTestSuite) SetupTest() { s.selector = ¤tRegionSelector{} - s.mock = &mockCurrentCloudRegion{} + s.mock = &MockMetadataProvider{} s.selector.client = s.mock } diff --git a/internal/resources/providers/awslib/current_region_selector.go b/internal/resources/providers/awslib/current_region_selector.go index 4f12439d2c..8c16e17eb9 100644 --- a/internal/resources/providers/awslib/current_region_selector.go +++ b/internal/resources/providers/awslib/current_region_selector.go @@ -21,16 +21,11 @@ import ( "context" "github.com/aws/aws-sdk-go-v2/aws" - ec2imds "github.com/aws/aws-sdk-go-v2/feature/ec2/imds" "github.com/elastic/elastic-agent-libs/logp" ) type currentRegionSelector struct { - client currentCloudRegion -} - -type currentCloudRegion interface { - GetMetadata(ctx context.Context, cfg aws.Config) (*ec2imds.InstanceIdentityDocument, error) + client MetadataProvider } func (s *currentRegionSelector) Regions(ctx context.Context, cfg aws.Config) ([]string, error) { diff --git a/internal/resources/providers/awslib/mock_current_cloud_region.go b/internal/resources/providers/awslib/mock_current_cloud_region.go deleted file mode 100644 index c04bb9a6d4..0000000000 --- a/internal/resources/providers/awslib/mock_current_cloud_region.go +++ /dev/null @@ -1,112 +0,0 @@ -// Licensed to Elasticsearch B.V. under one or more contributor -// license agreements. See the NOTICE file distributed with -// this work for additional information regarding copyright -// ownership. Elasticsearch B.V. licenses this file to you under -// the Apache License, Version 2.0 (the "License"); you may -// not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, -// software distributed under the License is distributed on an -// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -// KIND, either express or implied. See the License for the -// specific language governing permissions and limitations -// under the License. - -// Code generated by mockery v2.37.1. DO NOT EDIT. - -package awslib - -import ( - context "context" - - aws "github.com/aws/aws-sdk-go-v2/aws" - - imds "github.com/aws/aws-sdk-go-v2/feature/ec2/imds" - - mock "github.com/stretchr/testify/mock" -) - -// mockCurrentCloudRegion is an autogenerated mock type for the currentCloudRegion type -type mockCurrentCloudRegion struct { - mock.Mock -} - -type mockCurrentCloudRegion_Expecter struct { - mock *mock.Mock -} - -func (_m *mockCurrentCloudRegion) EXPECT() *mockCurrentCloudRegion_Expecter { - return &mockCurrentCloudRegion_Expecter{mock: &_m.Mock} -} - -// GetMetadata provides a mock function with given fields: ctx, cfg -func (_m *mockCurrentCloudRegion) GetMetadata(ctx context.Context, cfg aws.Config) (*imds.InstanceIdentityDocument, error) { - ret := _m.Called(ctx, cfg) - - var r0 *imds.InstanceIdentityDocument - var r1 error - if rf, ok := ret.Get(0).(func(context.Context, aws.Config) (*imds.InstanceIdentityDocument, error)); ok { - return rf(ctx, cfg) - } - if rf, ok := ret.Get(0).(func(context.Context, aws.Config) *imds.InstanceIdentityDocument); ok { - r0 = rf(ctx, cfg) - } else { - if ret.Get(0) != nil { - r0 = ret.Get(0).(*imds.InstanceIdentityDocument) - } - } - - if rf, ok := ret.Get(1).(func(context.Context, aws.Config) error); ok { - r1 = rf(ctx, cfg) - } else { - r1 = ret.Error(1) - } - - return r0, r1 -} - -// mockCurrentCloudRegion_GetMetadata_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'GetMetadata' -type mockCurrentCloudRegion_GetMetadata_Call struct { - *mock.Call -} - -// GetMetadata is a helper method to define mock.On call -// - ctx context.Context -// - cfg aws.Config -func (_e *mockCurrentCloudRegion_Expecter) GetMetadata(ctx interface{}, cfg interface{}) *mockCurrentCloudRegion_GetMetadata_Call { - return &mockCurrentCloudRegion_GetMetadata_Call{Call: _e.mock.On("GetMetadata", ctx, cfg)} -} - -func (_c *mockCurrentCloudRegion_GetMetadata_Call) Run(run func(ctx context.Context, cfg aws.Config)) *mockCurrentCloudRegion_GetMetadata_Call { - _c.Call.Run(func(args mock.Arguments) { - run(args[0].(context.Context), args[1].(aws.Config)) - }) - return _c -} - -func (_c *mockCurrentCloudRegion_GetMetadata_Call) Return(_a0 *imds.InstanceIdentityDocument, _a1 error) *mockCurrentCloudRegion_GetMetadata_Call { - _c.Call.Return(_a0, _a1) - return _c -} - -func (_c *mockCurrentCloudRegion_GetMetadata_Call) RunAndReturn(run func(context.Context, aws.Config) (*imds.InstanceIdentityDocument, error)) *mockCurrentCloudRegion_GetMetadata_Call { - _c.Call.Return(run) - return _c -} - -// newMockCurrentCloudRegion creates a new instance of mockCurrentCloudRegion. It also registers a testing interface on the mock and a cleanup function to assert the mocks expectations. -// The first argument is typically a *testing.T value. -func newMockCurrentCloudRegion(t interface { - mock.TestingT - Cleanup(func()) -}) *mockCurrentCloudRegion { - mock := &mockCurrentCloudRegion{} - mock.Mock.Test(t) - - t.Cleanup(func() { mock.AssertExpectations(t) }) - - return mock -} diff --git a/internal/resources/utils/user/user.go b/internal/resources/utils/user/user.go index 82e3ac3c0e..ab742f9352 100644 --- a/internal/resources/utils/user/user.go +++ b/internal/resources/utils/user/user.go @@ -206,6 +206,7 @@ func matchGroupIndexValue(value string, idx int) lineFunc { return } if _, err := strconv.Atoi(parts[2]); err != nil { + //nolint:nilerr return nil, nil } return &Group{Name: parts[0], Gid: parts[2]}, nil @@ -240,9 +241,11 @@ func matchUserIndexValue(value string, idx int) lineFunc { return } if _, err := strconv.Atoi(parts[2]); err != nil { + //nolint:nilerr return nil, nil } if _, err := strconv.Atoi(parts[3]); err != nil { + //nolint:nilerr return nil, nil } u := &User{