Skip to content

Commit

Permalink
refactor: small improvements to tests
Browse files Browse the repository at this point in the history
- More usage of testify
- Expand a test case to attempt to reproduce an issue

Signed-off-by: Flavio Castelli <[email protected]>
  • Loading branch information
flavio committed Aug 30, 2024
1 parent 137d1c0 commit 14e829f
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 38 deletions.
53 changes: 17 additions & 36 deletions internal/finder/kubectl_finder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import (
"fmt"
"os"
"testing"

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

type localCacheTestData struct {
Expand Down Expand Up @@ -46,9 +49,7 @@ func teardownFilesystemTest(td localCacheTestData) error {

func TestAllKubectlBinaries(t *testing.T) {
td, err := setupFilesystemTest()
if err != nil {
t.Errorf("Unexpeted failure: %v", err)
}
require.NoError(t, err)
defer func() {
if err = teardownFilesystemTest(td); err != nil {
panic(fmt.Sprintf("Error while tearing down test filesystem: %v\n", err))
Expand All @@ -59,72 +60,52 @@ func TestAllKubectlBinaries(t *testing.T) {
td.FakeHome,
[]string{"1.4.2"},
&localKubectlNamer{})
if err = createFakeKubectlBinaries(localBins); err != nil {
t.Error(err)
}
require.NoError(t, createFakeKubectlBinaries(localBins))

systemBins := fakeKubectlBinaries(
td.FakeSysBinPath,
[]string{"2.1.3"},
&systemKubectlNamer{})
if err = createFakeKubectlBinaries(systemBins); err != nil {
t.Error(err)
}
require.NoError(t, createFakeKubectlBinaries(systemBins))

//nolint: gocritic // append returns a different array on purpose, we're joining two arrays
expected := append(systemBins, localBins...)
actual := td.Finder.AllKubectlBinaries(true)

if len(expected) != len(actual) {
t.Errorf("Expected %+v, got %+v instead", expected, actual)
}
assert.Equal(t, expected, actual, "Expected %+v, got %+v instead", expected, actual)

for i, expectedBin := range expected {
if !actual[i].Version.Equals(expectedBin.Version) {
t.Errorf("Got %+v instead of %+v", actual[i].Version, expectedBin.Version)
}
if actual[i].Path != expectedBin.Path {
t.Errorf("Got %+v instead of %+v", actual[i].Path, expectedBin.Path)
}
assert.Condition(t, func() bool {
return actual[i].Version.Equals(expectedBin.Version)
}, "Expected %+v, got %+v instead", expectedBin.Version, actual[i].Version)
assert.Equal(t, expectedBin.Path, actual[i].Path, "Expected %s, got %s instead", expectedBin.Path, actual[i].Path)
}
}

func TestLocalKubectlVersionsEmptyCache(t *testing.T) {
td, err := setupFilesystemTest()
if err != nil {
t.Errorf("Unexpeted failure: %v", err)
}
require.NoError(t, err)
defer func() {
if err = teardownFilesystemTest(td); err != nil {
panic(fmt.Sprintf("Error while tearing down test filesystem: %v\n", err))
}
}()

bins, err := td.Finder.LocalKubectlBinaries()
if err != nil {
t.Errorf("Got unexpected error %v", err)
}
if len(bins) != 0 {
t.Errorf("Expected empty list")
}
require.NoError(t, err)
assert.Empty(t, bins)
}

func TestLocalKubectlVersionsDownloadDirNotCreated(t *testing.T) {
td, err := setupFilesystemTest()
if err != nil {
t.Errorf("Unexpeted failure: %v", err)
}
require.NoError(t, err)
defer func() {
if err = teardownFilesystemTest(td); err != nil {
panic(fmt.Sprintf("Error while tearing down test filesystem: %v\n", err))
}
}()

bins, err := td.Finder.LocalKubectlBinaries()
if err != nil {
t.Errorf("Got unexpected error %v", err)
}
if len(bins) != 0 {
t.Errorf("Expected empty list")
}
require.NoError(t, err)
assert.Empty(t, bins)
}
4 changes: 2 additions & 2 deletions internal/finder/versioner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ func TestEnsureCompatibleKubectlAvailableDownloadsKubectlBinaryWhenNeeded(t *tes
}{
{
name: "requested version can be satisfied by already downloaded kubectl binary",
kubectlAvailableVersions: []string{"1.9.8"},
requestedVersion: semver.MustParse("1.9.0"),
kubectlAvailableVersions: []string{"1.29.0", "1.26.0"},
requestedVersion: semver.MustParse("1.30.2"),
expectedToMakeDownloads: false,
downloadAllowed: true,
expectsError: false,
Expand Down

0 comments on commit 14e829f

Please sign in to comment.