Skip to content

Commit

Permalink
final refactoring
Browse files Browse the repository at this point in the history
Signed-off-by: Viktor Kramarenko <viktor.kramarenko@flant.com>
  • Loading branch information
ViktorKram committed Aug 9, 2024
1 parent b652481 commit 13ef205
Show file tree
Hide file tree
Showing 4 changed files with 210 additions and 0 deletions.
2 changes: 2 additions & 0 deletions images/sds-local-volume-csi/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ require (
github.com/deckhouse/sds-node-configurator/api v0.0.0-20240805103635-969dc811217b
github.com/go-logr/logr v1.4.1
github.com/golang/protobuf v1.5.4
github.com/stretchr/testify v1.9.0
golang.org/x/sync v0.6.0
google.golang.org/grpc v1.62.1
gopkg.in/yaml.v2 v2.4.0
Expand Down Expand Up @@ -41,6 +42,7 @@ require (
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
golang.org/x/net v0.23.0 // indirect
golang.org/x/oauth2 v0.18.0 // indirect
Expand Down
95 changes: 95 additions & 0 deletions images/sds-local-volume-csi/pkg/utils/node_store_maganer_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package utils

import (
"testing"

"github.com/stretchr/testify/assert"
mountutils "k8s.io/mount-utils"
"sds-local-volume-csi/pkg/logger"
)

func TestNodeStoreManager(t *testing.T) {
t.Run("toMapperPath", func(t *testing.T) {
t.Run("does_not_have_prefix_returns_empty", func(t *testing.T) {
assert.Equal(t, "", toMapperPath("not-dev-path"))
})

t.Run("have_prefix_returns_path", func(t *testing.T) {
path := "/dev/some-good/path"
expected := "/dev/mapper/some--good-path"

assert.Equal(t, expected, toMapperPath(path))
})
})

t.Run("checkMount", func(t *testing.T) {
t.Run("all_good", func(t *testing.T) {
const (
devPath = "/dev/some-good/path"
target = "some-target"
)
f := &mountutils.FakeMounter{}
f.MountPoints = []mountutils.MountPoint{
{
Device: devPath,
Path: target,
},
}
store := &Store{
Log: &logger.Logger{},
NodeStorage: mountutils.SafeFormatAndMount{
Interface: f,
},
}

err := checkMount(store, devPath, target, []string{})
assert.NoError(t, err)
})

t.Run("device_is_not_devPath_nor_mapperDevPath_returns_error", func(t *testing.T) {
const (
devPath = "weird-path"
target = "some-target"
)
f := &mountutils.FakeMounter{}
f.MountPoints = []mountutils.MountPoint{
{
Device: "other-name",
Path: target,
},
}
store := &Store{
Log: &logger.Logger{},
NodeStorage: mountutils.SafeFormatAndMount{
Interface: f,
},
}

err := checkMount(store, devPath, target, []string{})
assert.ErrorContains(t, err, "[checkMount] device from mount point \"other-name\" does not match expected source device path weird-path or mapper device path ")
})

t.Run("path_is_not_target_returns_error", func(t *testing.T) {
const (
devPath = "weird-path"
target = "some-target"
)
f := &mountutils.FakeMounter{}
f.MountPoints = []mountutils.MountPoint{
{
Device: devPath,
Path: "other-path",
},
}
store := &Store{
Log: &logger.Logger{},
NodeStorage: mountutils.SafeFormatAndMount{
Interface: f,
},
}

err := checkMount(store, devPath, target, []string{})
assert.ErrorContains(t, err, "[checkMount] mount point \"some-target\" not found in mount info")
})
})
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
package controller

import (
snc "github.com/deckhouse/sds-node-configurator/api/v1alpha1"
"github.com/stretchr/testify/assert"
"k8s.io/apimachinery/pkg/api/resource"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"testing"
)

func TestLVGWatcherCache(t *testing.T) {
t.Run("shouldReconcileLVG", func(t *testing.T) {
t.Run("deletion_timestamp_not_nil_returns_false", func(t *testing.T) {
lvg := &snc.LvmVolumeGroup{}
lvg.DeletionTimestamp = &v1.Time{}

assert.False(t, shouldReconcileLVG(&snc.LvmVolumeGroup{}, lvg))
})

t.Run("allocated_size_and_status_thin_pools_equal_returns_false", func(t *testing.T) {
size := resource.MustParse("1G")
thinPools := []snc.LvmVolumeGroupThinPoolStatus{
{
Name: "thin",
ActualSize: resource.MustParse("1G"),
},
}
oldLvg := &snc.LvmVolumeGroup{
ObjectMeta: v1.ObjectMeta{
Name: "first",
},
Status: snc.LvmVolumeGroupStatus{
AllocatedSize: size,
ThinPools: thinPools,
},
}
newLvg := &snc.LvmVolumeGroup{
ObjectMeta: v1.ObjectMeta{
Name: "first",
},
Status: snc.LvmVolumeGroupStatus{
AllocatedSize: size,
ThinPools: thinPools,
},
}

assert.False(t, shouldReconcileLVG(oldLvg, newLvg))
})

t.Run("allocated_size_not_equal_returns_true", func(t *testing.T) {
thinPools := []snc.LvmVolumeGroupThinPoolStatus{
{
Name: "thin",
ActualSize: resource.MustParse("1G"),
},
}
oldLvg := &snc.LvmVolumeGroup{
ObjectMeta: v1.ObjectMeta{
Name: "first",
},
Status: snc.LvmVolumeGroupStatus{
AllocatedSize: resource.MustParse("1G"),
ThinPools: thinPools,
},
}
newLvg := &snc.LvmVolumeGroup{
ObjectMeta: v1.ObjectMeta{
Name: "first",
},
Status: snc.LvmVolumeGroupStatus{
AllocatedSize: resource.MustParse("2G"),
ThinPools: thinPools,
},
}

assert.True(t, shouldReconcileLVG(oldLvg, newLvg))
})

t.Run("status_thin_pools_not_equal_returns_false", func(t *testing.T) {
size := resource.MustParse("1G")
oldLvg := &snc.LvmVolumeGroup{
ObjectMeta: v1.ObjectMeta{
Name: "first",
},
Status: snc.LvmVolumeGroupStatus{
AllocatedSize: size,
ThinPools: []snc.LvmVolumeGroupThinPoolStatus{
{
Name: "thin",
ActualSize: resource.MustParse("1G"),
},
},
},
}
newLvg := &snc.LvmVolumeGroup{
ObjectMeta: v1.ObjectMeta{
Name: "first",
},
Status: snc.LvmVolumeGroupStatus{
AllocatedSize: size,
ThinPools: []snc.LvmVolumeGroupThinPoolStatus{
{
Name: "thin",
ActualSize: resource.MustParse("2G"),
},
},
},
}

assert.True(t, shouldReconcileLVG(oldLvg, newLvg))
})
})
}

0 comments on commit 13ef205

Please sign in to comment.