Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✅ Pkg: test fixes & cleanup #845

Merged
merged 2 commits into from
Jan 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ on:
- "go.*"
- ".github/workflows/build.yml"
- "!tests/**"
schedule:
- cron: '0 0 * * *'
workflow_dispatch:
jobs:
format:
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ require (
github.com/onsi/ginkgo/v2 v2.20.1
github.com/onsi/gomega v1.34.1
github.com/outscale/osc-sdk-go/v2 v2.21.0
github.com/stretchr/testify v1.9.0
github.com/stretchr/testify v1.10.0
golang.org/x/sys v0.23.0
google.golang.org/grpc v1.66.2
google.golang.org/protobuf v1.34.2
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -194,8 +194,8 @@ github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/tmc/grpc-websocket-proxy v0.0.0-20220101234140-673ab2c3ae75 h1:6fotK7otjonDflCTK0BCfls4SPy3NcCVb5dqqmbRknE=
github.com/tmc/grpc-websocket-proxy v0.0.0-20220101234140-673ab2c3ae75/go.mod h1:KO6IkyS8Y3j8OdNO85qEYBsRPuteD+YciPomcXdrMnk=
github.com/xiang90/probing v0.0.0-20190116061207-43a291ad63a2 h1:eY9dn8+vbi4tKz5Qo6v2eYzo7kUS51QINcR5jNpbZS8=
Expand Down
57 changes: 26 additions & 31 deletions pkg/driver/luks_util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/outscale-dev/osc-bsu-csi-driver/pkg/driver/luks"
"github.com/outscale-dev/osc-bsu-csi-driver/pkg/driver/mocks"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

var (
Expand All @@ -26,23 +27,21 @@ var (
)

func TestIsLuks(t *testing.T) {

mockCtl := gomock.NewController(t)
devicePath := "/dev/fake"
// Check Isluks when device is not luks
mockCommand := mocks.NewMockInterface(mockCtl)
mockRun := mocks.NewMockCmd(mockCtl)
mockRun.EXPECT().Run().Return(fmt.Errorf("error"))
mockCommand.EXPECT().Command(gomock.Eq("cryptsetup"), gomock.Eq("isLuks"), gomock.Eq(devicePath)).Return(mockRun)
assert.Equal(t, false, IsLuks(mockCommand, devicePath))
assert.False(t, IsLuks(mockCommand, devicePath))

// Check when it is luks device
mockCommand = mocks.NewMockInterface(mockCtl)
mockRun = mocks.NewMockCmd(mockCtl)
mockCommand.EXPECT().Command(gomock.Eq("cryptsetup"), gomock.Eq("isLuks"), gomock.Eq(devicePath)).Return(mockRun)
mockRun.EXPECT().Run().Return(nil)
assert.Equal(t, true, IsLuks(mockCommand, devicePath))

assert.True(t, IsLuks(mockCommand, devicePath))
}

func TestLuksFormat(t *testing.T) {
Expand All @@ -68,7 +67,7 @@ func TestLuksFormat(t *testing.T) {
).Return(mockRun)
mockRun.EXPECT().CombinedOutput().Return([]byte{}, nil)
mockRun.EXPECT().SetStdin(gomock.Any()).Return()
assert.Equal(t, nil, LuksFormat(mockCommand, devicePath, passphrase, context))
require.NoError(t, LuksFormat(mockCommand, devicePath, passphrase, context))

// Check luksformat with Cipher
context.Cipher = "OneContext"
Expand All @@ -85,7 +84,7 @@ func TestLuksFormat(t *testing.T) {
).Return(mockRun)
mockRun.EXPECT().CombinedOutput().Return([]byte{}, nil)
mockRun.EXPECT().SetStdin(gomock.Any()).Return()
assert.Equal(t, nil, LuksFormat(mockCommand, devicePath, passphrase, context))
require.NoError(t, LuksFormat(mockCommand, devicePath, passphrase, context))

// Check luksformat with Cipher and Hash
context.Cipher = "OneContext"
Expand All @@ -104,7 +103,7 @@ func TestLuksFormat(t *testing.T) {
).Return(mockRun)
mockRun.EXPECT().CombinedOutput().Return([]byte{}, nil)
mockRun.EXPECT().SetStdin(gomock.Any()).Return()
assert.Equal(t, nil, LuksFormat(mockCommand, devicePath, passphrase, context))
require.NoError(t, LuksFormat(mockCommand, devicePath, passphrase, context))

// Check luksformat with Cipher, Hash and KeySize
context.Cipher = "OneContext"
Expand All @@ -125,8 +124,7 @@ func TestLuksFormat(t *testing.T) {
).Return(mockRun)
mockRun.EXPECT().CombinedOutput().Return([]byte{}, nil)
mockRun.EXPECT().SetStdin(gomock.Any()).Return()
assert.Equal(t, nil, LuksFormat(mockCommand, devicePath, passphrase, context))

require.NoError(t, LuksFormat(mockCommand, devicePath, passphrase, context))
}

func TestCheckLuksPassphrase(t *testing.T) {
Expand All @@ -148,7 +146,7 @@ func TestCheckLuksPassphrase(t *testing.T) {
gomock.Eq(devicePath),
).Return(mockRun)

assert.Equal(t, true, CheckLuksPassphrase(mockCommand, devicePath, passphrase))
assert.True(t, CheckLuksPassphrase(mockCommand, devicePath, passphrase))

// Check when it is luks device
mockCommand = mocks.NewMockInterface(mockCtl)
Expand All @@ -165,8 +163,7 @@ func TestCheckLuksPassphrase(t *testing.T) {
gomock.Eq(devicePath),
).Return(mockRun)

assert.Equal(t, false, CheckLuksPassphrase(mockCommand, devicePath, passphrase))

assert.False(t, CheckLuksPassphrase(mockCommand, devicePath, passphrase))
}

func TestLuksOpen(t *testing.T) {
Expand All @@ -190,15 +187,15 @@ func TestLuksOpen(t *testing.T) {
mockRun.EXPECT().SetStdin(gomock.Any()).Return()
mockRun.EXPECT().CombinedOutput().Return([]byte{}, nil)
ok, err := LuksOpen(mockStat, devicePath, "fake_crypt", passphrase)
assert.Equal(t, true, ok)
assert.Equal(t, nil, err)
require.NoError(t, err)
assert.True(t, ok)

// Check when already opened (idempotency)
mockStat = mocks.NewMockMounter(mockCtl)
mockStat.EXPECT().ExistsPath("/dev/mapper/fake_crypt").Return(true, nil)
ok, err = LuksOpen(mockStat, devicePath, "fake_crypt", passphrase)
assert.Equal(t, true, ok)
assert.Equal(t, nil, err)
require.NoError(t, err)
assert.True(t, ok)

// Check when open failed
mockStat = mocks.NewMockMounter(mockCtl)
Expand All @@ -216,9 +213,8 @@ func TestLuksOpen(t *testing.T) {
mockRun.EXPECT().SetStdin(gomock.Any()).Return()
mockRun.EXPECT().CombinedOutput().Return([]byte{}, fmt.Errorf("error"))
ok, err = LuksOpen(mockStat, devicePath, "fake_crypt", passphrase)
assert.Equal(t, false, ok)
assert.NotEqual(t, nil, err)

require.Error(t, err)
assert.False(t, ok)
}

func TestIsLuksMapping(t *testing.T) {
Expand All @@ -237,9 +233,9 @@ func TestIsLuksMapping(t *testing.T) {

mockRun.EXPECT().CombinedOutput().Return([]byte(ValidStatus), nil)
ok, mappingName, err := IsLuksMapping(mockCommand, devicePath)
assert.Equal(t, true, ok)
require.NoError(t, err)
assert.True(t, ok)
assert.Equal(t, "fake_crypt", mappingName)
assert.Equal(t, nil, err)

// Check when it is not a luks mapping
mockCommand = mocks.NewMockInterface(mockCtl)
Expand All @@ -254,18 +250,18 @@ func TestIsLuksMapping(t *testing.T) {

mockRun.EXPECT().CombinedOutput().Return([]byte("/dev/mapper/fake_crypt is inactive"), nil)
ok, mappingName, err = IsLuksMapping(mockCommand, devicePath)
assert.Equal(t, false, ok)
require.NoError(t, err)
assert.False(t, ok)
assert.Equal(t, "fake_crypt", mappingName)
assert.Equal(t, nil, err)

// Check when it is not a luks mapping because the device is not a mapping at all
mockCommand = mocks.NewMockInterface(mockCtl)
devicePath = "/dev/notmapper/fake_crypt"

ok, mappingName, err = IsLuksMapping(mockCommand, devicePath)
assert.Equal(t, false, ok)
require.NoError(t, err)
assert.False(t, ok)
assert.Equal(t, "", mappingName)
assert.Equal(t, nil, err)
}

func TestLuksResize(t *testing.T) {
Expand Down Expand Up @@ -299,7 +295,7 @@ func TestLuksResize(t *testing.T) {
mockRun.EXPECT().CombinedOutput().Return([]byte(""), nil)

// Call LuksResize with the mock command and passphrase
assert.Equal(t, nil, LuksResize(mockCommand, devicePath, passphrase))
require.NoError(t, LuksResize(mockCommand, devicePath, passphrase))

// Check failure case
mockCommand = mocks.NewMockInterface(mockCtl)
Expand All @@ -321,7 +317,7 @@ func TestLuksResize(t *testing.T) {
// Expect CombinedOutput to return an error
mockRun.EXPECT().CombinedOutput().Return([]byte(""), fmt.Errorf("Error"))

assert.NotEqual(t, nil, LuksResize(mockCommand, devicePath, passphrase))
require.Error(t, LuksResize(mockCommand, devicePath, passphrase))
}

func TestLuksClose(t *testing.T) {
Expand All @@ -339,13 +335,13 @@ func TestLuksClose(t *testing.T) {
).Return(mockRun)
mockRun.EXPECT().Run().Return(nil)
err := LuksClose(mockStat, "fake_crypt")
assert.Equal(t, nil, err)
require.NoError(t, err)

// Check when not opened (idempotency)
mockStat = mocks.NewMockMounter(mockCtl)
mockStat.EXPECT().ExistsPath("/dev/mapper/fake_crypt").Return(false, nil)
err = LuksClose(mockStat, "fake_crypt")
assert.Equal(t, nil, err)
require.NoError(t, err)

// Check when open failed
mockStat = mocks.NewMockMounter(mockCtl)
Expand All @@ -359,6 +355,5 @@ func TestLuksClose(t *testing.T) {
).Return(mockRun)
mockRun.EXPECT().Run().Return(fmt.Errorf("error"))
err = LuksClose(mockStat, "fake_crypt")
assert.NotEqual(t, nil, err)

require.Error(t, err)
}
98 changes: 33 additions & 65 deletions pkg/driver/mount_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,109 +17,77 @@ limitations under the License.
package driver

import (
"io/ioutil"
"os"
"path/filepath"
"testing"

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

func TestMakeDir(t *testing.T) {
// Setup the full driver and its environment
dir, err :=os.MkdirTemp("", "mount-bsu-csi")
if err != nil {
t.Fatalf("error creating directory %v", err)
}
defer os.RemoveAll(dir)
dir, err := os.MkdirTemp(t.TempDir(), "mount-bsu-csi")
require.NoError(t, err)

targetPath := filepath.Join(dir, "targetdir")

var (
mountObj = newNodeMounter()
)
mountObj := newNodeMounter()

if mountObj.MakeDir(targetPath) != nil {
t.Fatalf("Expect no error but got: %v", err)
}
err = mountObj.MakeDir(targetPath)
require.NoError(t, err)

if mountObj.MakeDir(targetPath) != nil {
t.Fatalf("Expect no error but got: %v", err)
}
err = mountObj.MakeDir(targetPath)
require.NoError(t, err)

if exists, err := mountObj.ExistsPath(targetPath); !exists {
t.Fatalf("Expect no error but got: %v", err)
}
exists, err := mountObj.ExistsPath(targetPath)
require.NoError(t, err)
assert.True(t, exists, "The directory must have been created")
}

func TestMakeFile(t *testing.T) {
// Setup the full driver and its environment
dir, err :=os.MkdirTemp("", "mount-bsu-csi")
if err != nil {
t.Fatalf("error creating directory %v", err)
}
defer os.RemoveAll(dir)
dir, err := os.MkdirTemp(t.TempDir(), "mount-bsu-csi")
require.NoError(t, err)

targetPath := filepath.Join(dir, "targetfile")

var (
mountObj = newNodeMounter()
)
mountObj := newNodeMounter()

if mountObj.MakeFile(targetPath) != nil {
t.Fatalf("Expect no error but got: %v", err)
}
err = mountObj.MakeFile(targetPath)
require.NoError(t, err)

if mountObj.MakeFile(targetPath) != nil {
t.Fatalf("Expect no error but got: %v", err)
}

if exists, err := mountObj.ExistsPath(targetPath); !exists {
t.Fatalf("Expect no error but got: %v", err)
}
err = mountObj.MakeFile(targetPath)
require.NoError(t, err)

exists, err := mountObj.ExistsPath(targetPath)
require.NoError(t, err)
assert.True(t, exists, "The file must have been created")
}

func TestExistsPath(t *testing.T) {
// Setup the full driver and its environment
dir, err :=os.MkdirTemp("", "mount-bsu-csi")
if err != nil {
t.Fatalf("error creating directory %v", err)
}
defer os.RemoveAll(dir)
dir, err := os.MkdirTemp(t.TempDir(), "mount-bsu-csi")
require.NoError(t, err)

targetPath := filepath.Join(dir, "notafile")

var (
mountObj = newNodeMounter()
)
mountObj := newNodeMounter()

exists, err := mountObj.ExistsPath(targetPath)

if err != nil {
t.Fatalf("Expect no error but got: %v", err)
}

if exists {
t.Fatalf("Expected file %s to not exist", targetPath)
}

require.NoError(t, err)
assert.False(t, exists, "The path must not exist")
}

func TestGetDeviceName(t *testing.T) {
// Setup the full driver and its environment
dir, err :=os.MkdirTemp("", "mount-bsu-csi")
if err != nil {
t.Fatalf("error creating directory %v", err)
}
defer os.RemoveAll(dir)
dir, err := os.MkdirTemp(t.TempDir(), "mount-bsu-csi")
require.NoError(t, err)

targetPath := filepath.Join(dir, "notafile")

var (
mountObj = newNodeMounter()
)

if _, _, err := mountObj.GetDeviceName(targetPath); err != nil {
t.Fatalf("Expect no error but got: %v", err)
}
mountObj := newNodeMounter()

_, _, err = mountObj.GetDeviceName(targetPath)
require.NoError(t, err)
}
Loading
Loading