Skip to content

Commit

Permalink
chore: enable require-error from testifylint on cmpserver and reposer…
Browse files Browse the repository at this point in the history
…ver folders (#18702)

* chore: enable require-error from testifylint on cmpserver and reposerver folders

Signed-off-by: Matthieu MOREL <matthieu.morel35@gmail.com>

* Update app_k8s_events_test.go

Signed-off-by: Matthieu MOREL <matthieu.morel35@gmail.com>

---------

Signed-off-by: Matthieu MOREL <matthieu.morel35@gmail.com>
  • Loading branch information
mmorel-35 authored Jun 18, 2024
1 parent 4c6ad9d commit 8f28199
Show file tree
Hide file tree
Showing 8 changed files with 270 additions and 283 deletions.
5 changes: 0 additions & 5 deletions .golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,6 @@ issues:
exclude:
- SA1019
- SA5011
exclude-rules:
- path: "(cmpserver|reposerver)/"
text: "require-error:"
linters:
- testifylint
max-issues-per-linter: 0
max-same-issues: 0
linters:
Expand Down
4 changes: 2 additions & 2 deletions cmpserver/plugin/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,9 +159,9 @@ spec:
require.NoError(t, err)
config, err := ReadPluginConfig(tempDir)
if tcc.expectedErr != "" {
assert.EqualError(t, err, tcc.expectedErr)
require.EqualError(t, err, tcc.expectedErr)
} else {
assert.NoError(t, err)
require.NoError(t, err)
}
assert.Equal(t, tcc.expected, config)
})
Expand Down
46 changes: 23 additions & 23 deletions cmpserver/plugin/plugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func TestMatchRepository(t *testing.T) {
match, discovery, err := f.service.matchRepository(context.Background(), f.path, f.env, ".")

// then
assert.NoError(t, err)
require.NoError(t, err)
assert.True(t, match)
assert.True(t, discovery)
})
Expand All @@ -118,7 +118,7 @@ func TestMatchRepository(t *testing.T) {
match, discovery, err := f.service.matchRepository(context.Background(), f.path, f.env, ".")

// then
assert.NoError(t, err)
require.NoError(t, err)
assert.False(t, match)
assert.True(t, discovery)
})
Expand All @@ -133,7 +133,7 @@ func TestMatchRepository(t *testing.T) {
_, _, err := f.service.matchRepository(context.Background(), f.path, f.env, ".")

// then
assert.ErrorContains(t, err, "syntax error")
require.ErrorContains(t, err, "syntax error")
})
t.Run("will match plugin by glob", func(t *testing.T) {
// given
Expand All @@ -148,7 +148,7 @@ func TestMatchRepository(t *testing.T) {
match, discovery, err := f.service.matchRepository(context.Background(), f.path, f.env, ".")

// then
assert.NoError(t, err)
require.NoError(t, err)
assert.True(t, match)
assert.True(t, discovery)
})
Expand All @@ -165,7 +165,7 @@ func TestMatchRepository(t *testing.T) {
match, discovery, err := f.service.matchRepository(context.Background(), f.path, f.env, ".")

// then
assert.NoError(t, err)
require.NoError(t, err)
assert.False(t, match)
assert.True(t, discovery)
})
Expand All @@ -182,7 +182,7 @@ func TestMatchRepository(t *testing.T) {
_, _, err := f.service.matchRepository(context.Background(), f.path, f.env, ".")

// then
assert.ErrorContains(t, err, "error finding glob match for pattern")
require.ErrorContains(t, err, "error finding glob match for pattern")
})
t.Run("will match plugin by command when returns any output", func(t *testing.T) {
// given
Expand All @@ -199,7 +199,7 @@ func TestMatchRepository(t *testing.T) {
match, discovery, err := f.service.matchRepository(context.Background(), f.path, f.env, ".")

// then
assert.NoError(t, err)
require.NoError(t, err)
assert.True(t, match)
assert.True(t, discovery)
})
Expand All @@ -217,7 +217,7 @@ func TestMatchRepository(t *testing.T) {
// when
match, discovery, err := f.service.matchRepository(context.Background(), f.path, f.env, ".")
// then
assert.NoError(t, err)
require.NoError(t, err)
assert.False(t, match)
assert.True(t, discovery)
})
Expand All @@ -236,7 +236,7 @@ func TestMatchRepository(t *testing.T) {
match, discovery, err := f.service.matchRepository(context.Background(), f.path, f.env, ".")

// then
assert.NoError(t, err)
require.NoError(t, err)
assert.True(t, match)
assert.True(t, discovery)
})
Expand All @@ -256,7 +256,7 @@ func TestMatchRepository(t *testing.T) {
match, discovery, err := f.service.matchRepository(context.Background(), f.path, f.env, ".")

// then
assert.NoError(t, err)
require.NoError(t, err)
assert.False(t, match)
assert.True(t, discovery)
})
Expand All @@ -275,7 +275,7 @@ func TestMatchRepository(t *testing.T) {
match, discovery, err := f.service.matchRepository(context.Background(), f.path, f.env, ".")

// then
assert.Error(t, err)
require.Error(t, err)
assert.False(t, match)
assert.True(t, discovery)
})
Expand All @@ -288,7 +288,7 @@ func TestMatchRepository(t *testing.T) {
match, discovery, err := f.service.matchRepository(context.Background(), f.path, f.env, ".")

// then
assert.NoError(t, err)
require.NoError(t, err)
assert.False(t, match)
assert.False(t, discovery)
})
Expand Down Expand Up @@ -323,7 +323,7 @@ func TestGenerateManifest(t *testing.T) {
service.WithGenerateCommand(Command{Command: []string{"bad-command"}})

res, err := service.generateManifest(context.Background(), "testdata/kustomize", nil)
assert.ErrorContains(t, err, "executable file not found")
require.ErrorContains(t, err, "executable file not found")
assert.Nil(t, res.Manifests)
})
t.Run("bad yaml output", func(t *testing.T) {
Expand All @@ -332,7 +332,7 @@ func TestGenerateManifest(t *testing.T) {
service.WithGenerateCommand(Command{Command: []string{"echo", "invalid yaml: }"}})

res, err := service.generateManifest(context.Background(), "testdata/kustomize", nil)
assert.ErrorContains(t, err, "failed to unmarshal manifest")
require.ErrorContains(t, err, "failed to unmarshal manifest")
assert.Nil(t, res.Manifests)
})
}
Expand All @@ -345,7 +345,7 @@ func TestGenerateManifest_deadline_exceeded(t *testing.T) {
expiredCtx, cancel := context.WithTimeout(context.Background(), time.Second*0)
defer cancel()
_, err = service.generateManifest(expiredCtx, "", nil)
assert.ErrorContains(t, err, "context deadline exceeded")
require.ErrorContains(t, err, "context deadline exceeded")
}

// TestRunCommandContextTimeout makes sure the command dies at timeout rather than sleeping past the timeout.
Expand All @@ -360,13 +360,13 @@ func TestRunCommandContextTimeout(t *testing.T) {
before := time.Now()
_, err := runCommand(ctx, command, "", []string{})
after := time.Now()
assert.Error(t, err) // The command should time out, causing an error.
require.Error(t, err) // The command should time out, causing an error.
assert.Less(t, after.Sub(before), 1*time.Second)
}

func TestRunCommandEmptyCommand(t *testing.T) {
_, err := runCommand(context.Background(), Command{}, "", nil)
assert.ErrorContains(t, err, "Command is empty")
require.ErrorContains(t, err, "Command is empty")
}

// TestRunCommandContextTimeoutWithCleanup makes sure that the process is given enough time to cleanup before sending SIGKILL.
Expand All @@ -385,7 +385,7 @@ func TestRunCommandContextTimeoutWithCleanup(t *testing.T) {
output, err := runCommand(ctx, command, "", []string{})
after := time.Now()

assert.Error(t, err) // The command should time out, causing an error.
require.Error(t, err) // The command should time out, causing an error.
assert.Less(t, after.Sub(before), 1*time.Second)
// The command should still have completed the cleanup after termination.
assert.Contains(t, output, "cleanup completed")
Expand Down Expand Up @@ -451,7 +451,7 @@ func Test_getParametersAnnouncement_invalid_json(t *testing.T) {
Args: []string{`[`},
}
_, err := getParametersAnnouncement(context.Background(), "", []*repoclient.ParameterAnnouncement{}, command, []*apiclient.EnvEntry{})
assert.Error(t, err)
require.Error(t, err)
assert.Contains(t, err.Error(), "unexpected end of JSON input")
}

Expand All @@ -461,7 +461,7 @@ func Test_getParametersAnnouncement_bad_command(t *testing.T) {
Args: []string{"1"},
}
_, err := getParametersAnnouncement(context.Background(), "", []*repoclient.ParameterAnnouncement{}, command, []*apiclient.EnvEntry{})
assert.Error(t, err)
require.Error(t, err)
assert.Contains(t, err.Error(), "error executing dynamic parameter output command")
}

Expand All @@ -472,7 +472,7 @@ func Test_getTempDirMustCleanup(t *testing.T) {
err := os.Chmod(tempDir, 0o000)
require.NoError(t, err)
_, _, err = getTempDirMustCleanup(path.Join(tempDir, "test"))
assert.ErrorContains(t, err, "error creating temp dir")
require.ErrorContains(t, err, "error creating temp dir")

err = os.Chmod(tempDir, 0o700)
require.NoError(t, err)
Expand Down Expand Up @@ -500,13 +500,13 @@ func TestService_Init(t *testing.T) {
require.NoError(t, err)
s := NewService(CMPServerInitConstants{PluginConfig: PluginConfig{}})
err = s.Init(workDir)
assert.ErrorContains(t, err, "error removing workdir", "Init must throw an error if it can't remove the work directory")
require.ErrorContains(t, err, "error removing workdir", "Init must throw an error if it can't remove the work directory")

// Make the base directory writable so Init's cleanup succeeds.
err = os.Chmod(tempDir, 0o700)
require.NoError(t, err)
err = s.Init(workDir)
assert.NoError(t, err)
require.NoError(t, err)
assert.DirExists(t, workDir)
assert.NoFileExists(t, testfile)
}
Expand Down
9 changes: 5 additions & 4 deletions reposerver/apiclient/clientset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/argoproj/argo-cd/v2/reposerver/apiclient"
"github.com/argoproj/argo-cd/v2/reposerver/apiclient/mocks"
Expand All @@ -16,7 +17,7 @@ func TestNewRepoServerClient_CorrectClientReturned(t *testing.T) {

closer, client, err := mockClientset.NewRepoServerClient()

assert.NoError(t, err)
require.NoError(t, err)
assert.NotNil(t, closer)
assert.NotNil(t, client)
assert.Equal(t, mockClientset.RepoServerServiceClient, client)
Expand Down Expand Up @@ -59,7 +60,7 @@ func TestNewConnection_TLSWithStrictValidation(t *testing.T) {

conn, err := apiclient.NewConnection("example.com:443", 10, &tlsConfig)

assert.NoError(t, err)
require.NoError(t, err)
assert.NotNil(t, conn)
}

Expand All @@ -72,7 +73,7 @@ func TestNewConnection_TLSWithStrictValidationAndCertificates(t *testing.T) {

conn, err := apiclient.NewConnection("example.com:443", 10, &tlsConfig)

assert.NoError(t, err)
require.NoError(t, err)
assert.NotNil(t, conn)
}

Expand All @@ -86,6 +87,6 @@ func TestNewConnection_InsecureConnection(t *testing.T) {

conn, err := apiclient.NewConnection("example.com:80", 10, &tlsConfig)

assert.NoError(t, err)
require.NoError(t, err)
assert.NotNil(t, conn)
}
Loading

0 comments on commit 8f28199

Please sign in to comment.