Skip to content

Commit

Permalink
test(controller): add unit tests for package entry encoding and decoding
Browse files Browse the repository at this point in the history
  • Loading branch information
NikitaCOEUR committed Jan 11, 2025
1 parent 1b71a57 commit d2000a8
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 0 deletions.
61 changes: 61 additions & 0 deletions pkg/controller/vacuum/vacuum_internal_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package vacuum

import (
"encoding/json"
"testing"
"time"

"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
)

func TestHandleAsyncStorePackage_NilPackage(t *testing.T) {
logE := logrus.NewEntry(logrus.New())

vacuumCtrl := New(nil, nil)

// Test
err := vacuumCtrl.handleAsyncStorePackage(logE, nil)

// Assert
assert.Error(t, err)
assert.Equal(t, "vacuumPkg is nil", err.Error())
}
func TestEncodePackageEntry(t *testing.T) {
pkgEntry := &PackageEntry{
LastUsageTime: time.Now(),
Package: &Package{Name: "test-package"},
}

data, err := encodePackageEntry(pkgEntry)
assert.NoError(t, err)
assert.NotNil(t, data)

var decodedEntry PackageEntry
err = json.Unmarshal(data, &decodedEntry)
assert.NoError(t, err)
assert.Equal(t, pkgEntry.LastUsageTime.Unix(), decodedEntry.LastUsageTime.Unix())
assert.Equal(t, pkgEntry.Package.Name, decodedEntry.Package.Name)
}

func TestDecodePackageEntry(t *testing.T) {
pkgEntry := &PackageEntry{
LastUsageTime: time.Now(),
Package: &Package{Name: "test-package"},
}

data, err := json.Marshal(pkgEntry)
assert.NoError(t, err)

decodedEntry, err := decodePackageEntry(data)
assert.NoError(t, err)
assert.NotNil(t, decodedEntry)
assert.Equal(t, pkgEntry.LastUsageTime.Unix(), decodedEntry.LastUsageTime.Unix())
assert.Equal(t, pkgEntry.Package.Name, decodedEntry.Package.Name)
}

func TestDecodePackageEntry_Error(t *testing.T) {
_, err := decodePackageEntry([]byte("invalid json"))
assert.Error(t, err)
assert.Contains(t, err.Error(), "unmarshal package entry")
}
6 changes: 6 additions & 0 deletions pkg/controller/vacuum/vacuum_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ func TestVacuum(t *testing.T) { //nolint:funlen,maintidx,cyclop

err = controller.ListPackages(logE, false, "test")
require.NoError(t, err, "Should return nil when vacuum is disabled")

err = controller.Vacuum(logE)
require.NoError(t, err, "Should return nil when vacuum is disabled")

err = controller.Close(logE)
require.NoError(t, err, "Should return nil when vacuum is disabled")
})

t.Run("vacuum bad configuration", func(t *testing.T) {
Expand Down

0 comments on commit d2000a8

Please sign in to comment.