From dbc590db0074e7c967ecfe585689b1c653fb41ee Mon Sep 17 00:00:00 2001 From: David Cassany Viladomat Date: Tue, 12 Dec 2023 17:14:39 +0100 Subject: [PATCH] Wrap mount.Interface to our own interface (#1875) Signed-off-by: David Cassany --- cmd/build-disk.go | 3 +-- cmd/build-iso.go | 3 +-- cmd/cloud-init.go | 5 ++--- cmd/config/config.go | 5 ++--- cmd/config/config_test.go | 12 +++++------ cmd/init.go | 4 ++-- cmd/install.go | 3 +-- cmd/pull-image.go | 3 +-- cmd/reset.go | 4 ++-- cmd/run-stage.go | 4 ++-- cmd/upgrade.go | 4 ++-- pkg/action/build_test.go | 4 ++-- pkg/action/install_test.go | 4 ++-- pkg/action/reset_test.go | 4 ++-- pkg/action/upgrade_test.go | 4 ++-- pkg/config/config.go | 5 ++--- pkg/config/config_test.go | 7 +++---- pkg/elemental/elemental_test.go | 7 +++---- pkg/mocks/mounter_mock.go | 37 +++++++++++++-------------------- pkg/types/v1/config.go | 3 +-- pkg/types/v1/config_test.go | 6 +++--- pkg/types/v1/mounter.go | 36 ++++++++++++++++++++++++++++++++ pkg/utils/runstage_test.go | 2 +- pkg/utils/utils_test.go | 4 ++-- 24 files changed, 95 insertions(+), 78 deletions(-) create mode 100644 pkg/types/v1/mounter.go diff --git a/cmd/build-disk.go b/cmd/build-disk.go index b9d52d26478..bd79e967858 100644 --- a/cmd/build-disk.go +++ b/cmd/build-disk.go @@ -26,7 +26,6 @@ import ( "github.com/spf13/cobra" "github.com/spf13/viper" - "k8s.io/mount-utils" "github.com/rancher/elemental-toolkit/cmd/config" "github.com/rancher/elemental-toolkit/pkg/action" @@ -61,7 +60,7 @@ func NewBuildDisk(root *cobra.Command, addCheckRoot bool) *cobra.Command { if err != nil { return err } - mounter := mount.New(path) + mounter := v1.NewMounter(path) flags := cmd.Flags() cfg, err = config.ReadConfigBuild(viper.GetString("config-dir"), flags, mounter) diff --git a/cmd/build-iso.go b/cmd/build-iso.go index 95a4bc6e46c..2b5af2265ee 100644 --- a/cmd/build-iso.go +++ b/cmd/build-iso.go @@ -22,7 +22,6 @@ import ( "github.com/spf13/cobra" "github.com/spf13/viper" - "k8s.io/mount-utils" "github.com/rancher/elemental-toolkit/cmd/config" "github.com/rancher/elemental-toolkit/pkg/action" @@ -54,7 +53,7 @@ func NewBuildISO(root *cobra.Command, addCheckRoot bool) *cobra.Command { if err != nil { return elementalError.NewFromError(err, elementalError.StatFile) } - mounter := mount.New(path) + mounter := v1.NewMounter(path) cfg, err := config.ReadConfigBuild(viper.GetString("config-dir"), cmd.Flags(), mounter) if err != nil { diff --git a/cmd/cloud-init.go b/cmd/cloud-init.go index e9c137022eb..dd853b2f5c1 100644 --- a/cmd/cloud-init.go +++ b/cmd/cloud-init.go @@ -20,10 +20,9 @@ import ( "io" "os" - "k8s.io/mount-utils" - "github.com/rancher/elemental-toolkit/cmd/config" elementalError "github.com/rancher/elemental-toolkit/pkg/error" + v1 "github.com/rancher/elemental-toolkit/pkg/types/v1" "github.com/mudler/yip/pkg/schema" "github.com/spf13/cobra" @@ -39,7 +38,7 @@ func NewCloudInitCmd(root *cobra.Command) *cobra.Command { _ = viper.BindPFlags(cmd.Flags()) }, RunE: func(cmd *cobra.Command, args []string) error { - cfg, err := config.ReadConfigRun(viper.GetString("config-dir"), cmd.Flags(), &mount.FakeMounter{}) + cfg, err := config.ReadConfigRun(viper.GetString("config-dir"), cmd.Flags(), v1.NewDummyMounter()) if err != nil { return elementalError.NewFromError(err, elementalError.ReadingRunConfig) } diff --git a/cmd/config/config.go b/cmd/config/config.go index 1e54bce3e8c..c8b43c3cedf 100644 --- a/cmd/config/config.go +++ b/cmd/config/config.go @@ -31,7 +31,6 @@ import ( "github.com/sirupsen/logrus" "github.com/spf13/pflag" "github.com/spf13/viper" - "k8s.io/mount-utils" "github.com/rancher/elemental-toolkit/internal/version" "github.com/rancher/elemental-toolkit/pkg/config" @@ -98,7 +97,7 @@ func bindGivenFlags(vp *viper.Viper, flagSet *pflag.FlagSet) { } } -func ReadConfigBuild(configDir string, flags *pflag.FlagSet, mounter mount.Interface) (*v1.BuildConfig, error) { +func ReadConfigBuild(configDir string, flags *pflag.FlagSet, mounter v1.Mounter) (*v1.BuildConfig, error) { logger := v1.NewLogger() cfg := config.NewBuildConfig( @@ -144,7 +143,7 @@ func ReadConfigBuild(configDir string, flags *pflag.FlagSet, mounter mount.Inter return cfg, err } -func ReadConfigRun(configDir string, flags *pflag.FlagSet, mounter mount.Interface) (*v1.RunConfig, error) { +func ReadConfigRun(configDir string, flags *pflag.FlagSet, mounter v1.Mounter) (*v1.RunConfig, error) { cfg := config.NewRunConfig( config.WithLogger(v1.NewLogger()), config.WithMounter(mounter), diff --git a/cmd/config/config_test.go b/cmd/config/config_test.go index 89f40e496ce..5e1ce68f0df 100644 --- a/cmd/config/config_test.go +++ b/cmd/config/config_test.go @@ -42,10 +42,10 @@ import ( ) var _ = Describe("Config", Label("config"), func() { - var mounter *v1mock.ErrorMounter + var mounter *v1mock.FakeMounter BeforeEach(func() { - mounter = &v1mock.ErrorMounter{} + mounter = v1mock.NewFakeMounter() }) AfterEach(func() { viper.Reset() @@ -133,7 +133,7 @@ var _ = Describe("Config", Label("config"), func() { var runner *v1mock.FakeRunner var fs vfs.FS var logger v1.Logger - var mounter *v1mock.ErrorMounter + var mounter *v1mock.FakeMounter var syscall *v1mock.FakeSyscall var client *v1mock.FakeHTTPClient var cloudInit *v1mock.FakeCloudInitRunner @@ -144,7 +144,7 @@ var _ = Describe("Config", Label("config"), func() { BeforeEach(func() { runner = v1mock.NewFakeRunner() syscall = &v1mock.FakeSyscall{} - mounter = v1mock.NewErrorMounter() + mounter = v1mock.NewFakeMounter() client = &v1mock.FakeHTTPClient{} memLog = &bytes.Buffer{} logger = v1.NewBufferLogger(memLog) @@ -251,7 +251,7 @@ var _ = Describe("Config", Label("config"), func() { var runner *v1mock.FakeRunner var fs vfs.FS var logger v1.Logger - var mounter *v1mock.ErrorMounter + var mounter *v1mock.FakeMounter var syscall *v1mock.FakeSyscall var client *v1mock.FakeHTTPClient var cloudInit *v1mock.FakeCloudInitRunner @@ -262,7 +262,7 @@ var _ = Describe("Config", Label("config"), func() { BeforeEach(func() { runner = v1mock.NewFakeRunner() syscall = &v1mock.FakeSyscall{} - mounter = v1mock.NewErrorMounter() + mounter = v1mock.NewFakeMounter() client = &v1mock.FakeHTTPClient{} memLog = &bytes.Buffer{} logger = v1.NewBufferLogger(memLog) diff --git a/cmd/init.go b/cmd/init.go index 0b84a9f93de..b8f9a3f45b4 100644 --- a/cmd/init.go +++ b/cmd/init.go @@ -21,12 +21,12 @@ import ( "github.com/spf13/cobra" "github.com/spf13/viper" - "k8s.io/mount-utils" "github.com/rancher/elemental-toolkit/cmd/config" "github.com/rancher/elemental-toolkit/pkg/action" elementalError "github.com/rancher/elemental-toolkit/pkg/error" "github.com/rancher/elemental-toolkit/pkg/features" + v1 "github.com/rancher/elemental-toolkit/pkg/types/v1" ) func InitCmd(root *cobra.Command) *cobra.Command { @@ -39,7 +39,7 @@ func InitCmd(root *cobra.Command) *cobra.Command { " Defaults to all", Args: cobra.MaximumNArgs(1), RunE: func(cmd *cobra.Command, args []string) error { - cfg, err := config.ReadConfigRun(viper.GetString("config-dir"), cmd.Flags(), &mount.FakeMounter{}) + cfg, err := config.ReadConfigRun(viper.GetString("config-dir"), cmd.Flags(), v1.NewDummyMounter()) if err != nil { cfg.Logger.Errorf("Error reading config: %s\n", err) return elementalError.NewFromError(err, elementalError.ReadingRunConfig) diff --git a/cmd/install.go b/cmd/install.go index 6ec2cda3da0..d7e8ac63fff 100644 --- a/cmd/install.go +++ b/cmd/install.go @@ -21,7 +21,6 @@ import ( "github.com/spf13/cobra" "github.com/spf13/viper" - "k8s.io/mount-utils" "github.com/rancher/elemental-toolkit/cmd/config" "github.com/rancher/elemental-toolkit/pkg/action" @@ -48,7 +47,7 @@ func NewInstallCmd(root *cobra.Command, addCheckRoot bool) *cobra.Command { if err != nil { return err } - mounter := mount.New(path) + mounter := v1.NewMounter(path) cfg, err := config.ReadConfigRun(viper.GetString("config-dir"), cmd.Flags(), mounter) if err != nil { diff --git a/cmd/pull-image.go b/cmd/pull-image.go index db35be9f1f5..38d666087fa 100644 --- a/cmd/pull-image.go +++ b/cmd/pull-image.go @@ -21,7 +21,6 @@ import ( "github.com/spf13/cobra" "github.com/spf13/viper" - "k8s.io/mount-utils" "github.com/rancher/elemental-toolkit/cmd/config" elementalError "github.com/rancher/elemental-toolkit/pkg/error" @@ -40,7 +39,7 @@ func NewPullImageCmd(root *cobra.Command, addCheckRoot bool) *cobra.Command { return nil }, RunE: func(cmd *cobra.Command, args []string) error { - cfg, err := config.ReadConfigRun(viper.GetString("config-dir"), cmd.Flags(), &mount.FakeMounter{}) + cfg, err := config.ReadConfigRun(viper.GetString("config-dir"), cmd.Flags(), v1.NewDummyMounter()) if err != nil { cfg.Logger.Errorf("Error reading config: %s\n", err) return elementalError.NewFromError(err, elementalError.ReadingRunConfig) diff --git a/cmd/reset.go b/cmd/reset.go index dc78010780a..db0e74c2365 100644 --- a/cmd/reset.go +++ b/cmd/reset.go @@ -21,11 +21,11 @@ import ( "github.com/spf13/cobra" "github.com/spf13/viper" - "k8s.io/mount-utils" "github.com/rancher/elemental-toolkit/cmd/config" "github.com/rancher/elemental-toolkit/pkg/action" elementalError "github.com/rancher/elemental-toolkit/pkg/error" + v1 "github.com/rancher/elemental-toolkit/pkg/types/v1" ) func NewResetCmd(root *cobra.Command, addCheckRoot bool) *cobra.Command { @@ -44,7 +44,7 @@ func NewResetCmd(root *cobra.Command, addCheckRoot bool) *cobra.Command { if err != nil { return err } - mounter := mount.New(path) + mounter := v1.NewMounter(path) cfg, err := config.ReadConfigRun(viper.GetString("config-dir"), cmd.Flags(), mounter) if err != nil { diff --git a/cmd/run-stage.go b/cmd/run-stage.go index ec42a099198..c5d1e13afae 100644 --- a/cmd/run-stage.go +++ b/cmd/run-stage.go @@ -19,10 +19,10 @@ package cmd import ( "github.com/spf13/cobra" "github.com/spf13/viper" - "k8s.io/mount-utils" "github.com/rancher/elemental-toolkit/cmd/config" elementalError "github.com/rancher/elemental-toolkit/pkg/error" + v1 "github.com/rancher/elemental-toolkit/pkg/types/v1" "github.com/rancher/elemental-toolkit/pkg/utils" ) @@ -35,7 +35,7 @@ func NewRunStage(root *cobra.Command) *cobra.Command { }, RunE: func(cmd *cobra.Command, args []string) error { - cfg, err := config.ReadConfigRun(viper.GetString("config-dir"), cmd.Flags(), &mount.FakeMounter{}) + cfg, err := config.ReadConfigRun(viper.GetString("config-dir"), cmd.Flags(), v1.NewDummyMounter()) if err != nil { cfg.Logger.Errorf("Error reading config: %s\n", err) return elementalError.NewFromError(err, elementalError.ReadingRunConfig) diff --git a/cmd/upgrade.go b/cmd/upgrade.go index 453330e757d..1bee6319a4d 100644 --- a/cmd/upgrade.go +++ b/cmd/upgrade.go @@ -21,11 +21,11 @@ import ( "github.com/spf13/cobra" "github.com/spf13/viper" - "k8s.io/mount-utils" "github.com/rancher/elemental-toolkit/cmd/config" "github.com/rancher/elemental-toolkit/pkg/action" elementalError "github.com/rancher/elemental-toolkit/pkg/error" + v1 "github.com/rancher/elemental-toolkit/pkg/types/v1" ) // NewUpgradeCmd returns a new instance of the upgrade subcommand and appends it to @@ -48,7 +48,7 @@ func NewUpgradeCmd(root *cobra.Command, addCheckRoot bool) *cobra.Command { if err != nil { return err } - mounter := mount.New(path) + mounter := v1.NewMounter(path) cfg, err := config.ReadConfigRun(viper.GetString("config-dir"), cmd.Flags(), mounter) if err != nil { diff --git a/pkg/action/build_test.go b/pkg/action/build_test.go index ed200af7655..5026be274b0 100644 --- a/pkg/action/build_test.go +++ b/pkg/action/build_test.go @@ -43,7 +43,7 @@ var _ = Describe("Build Actions", func() { var runner *v1mock.FakeRunner var fs vfs.FS var logger v1.Logger - var mounter *v1mock.ErrorMounter + var mounter *v1mock.FakeMounter var syscall *v1mock.FakeSyscall var client *v1mock.FakeHTTPClient var cloudInit *v1mock.FakeCloudInitRunner @@ -55,7 +55,7 @@ var _ = Describe("Build Actions", func() { BeforeEach(func() { runner = v1mock.NewFakeRunner() syscall = &v1mock.FakeSyscall{} - mounter = v1mock.NewErrorMounter() + mounter = v1mock.NewFakeMounter() client = &v1mock.FakeHTTPClient{} memLog = &bytes.Buffer{} bootloader = &v1mock.FakeBootloader{} diff --git a/pkg/action/install_test.go b/pkg/action/install_test.go index de03f5bf402..41ba56bfa56 100644 --- a/pkg/action/install_test.go +++ b/pkg/action/install_test.go @@ -47,7 +47,7 @@ var _ = Describe("Install action tests", func() { var runner *v1mock.FakeRunner var fs vfs.FS var logger v1.Logger - var mounter *v1mock.ErrorMounter + var mounter *v1mock.FakeMounter var syscall *v1mock.FakeSyscall var client *v1mock.FakeHTTPClient var cloudInit *v1mock.FakeCloudInitRunner @@ -60,7 +60,7 @@ var _ = Describe("Install action tests", func() { BeforeEach(func() { runner = v1mock.NewFakeRunner() syscall = &v1mock.FakeSyscall{} - mounter = v1mock.NewErrorMounter() + mounter = v1mock.NewFakeMounter() client = &v1mock.FakeHTTPClient{} memLog = &bytes.Buffer{} logger = v1.NewBufferLogger(memLog) diff --git a/pkg/action/reset_test.go b/pkg/action/reset_test.go index a2decbc4cfa..537f7eefb43 100644 --- a/pkg/action/reset_test.go +++ b/pkg/action/reset_test.go @@ -40,7 +40,7 @@ var _ = Describe("Reset action tests", func() { var runner *v1mock.FakeRunner var fs vfs.FS var logger v1.Logger - var mounter *v1mock.ErrorMounter + var mounter *v1mock.FakeMounter var syscall *v1mock.FakeSyscall var client *v1mock.FakeHTTPClient var cloudInit *v1mock.FakeCloudInitRunner @@ -53,7 +53,7 @@ var _ = Describe("Reset action tests", func() { BeforeEach(func() { runner = v1mock.NewFakeRunner() syscall = &v1mock.FakeSyscall{} - mounter = v1mock.NewErrorMounter() + mounter = v1mock.NewFakeMounter() client = &v1mock.FakeHTTPClient{} memLog = &bytes.Buffer{} logger = v1.NewBufferLogger(memLog) diff --git a/pkg/action/upgrade_test.go b/pkg/action/upgrade_test.go index 7ce965d1ede..aaa74680196 100644 --- a/pkg/action/upgrade_test.go +++ b/pkg/action/upgrade_test.go @@ -43,7 +43,7 @@ var _ = Describe("Runtime Actions", func() { var runner *v1mock.FakeRunner var fs vfs.FS var logger v1.Logger - var mounter *v1mock.ErrorMounter + var mounter *v1mock.FakeMounter var syscall *v1mock.FakeSyscall var client *v1mock.FakeHTTPClient var cloudInit *v1mock.FakeCloudInitRunner @@ -56,7 +56,7 @@ var _ = Describe("Runtime Actions", func() { BeforeEach(func() { runner = v1mock.NewFakeRunner() syscall = &v1mock.FakeSyscall{} - mounter = v1mock.NewErrorMounter() + mounter = v1mock.NewFakeMounter() client = &v1mock.FakeHTTPClient{} memLog = &bytes.Buffer{} logger = v1.NewBufferLogger(memLog) diff --git a/pkg/config/config.go b/pkg/config/config.go index 3aa4395d92e..cb55d455238 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -23,7 +23,6 @@ import ( "strings" "github.com/twpayne/go-vfs" - "k8s.io/mount-utils" "github.com/rancher/elemental-toolkit/pkg/cloudinit" "github.com/rancher/elemental-toolkit/pkg/constants" @@ -61,7 +60,7 @@ func WithSyscall(syscall v1.SyscallInterface) func(r *v1.Config) error { } } -func WithMounter(mounter mount.Interface) func(r *v1.Config) error { +func WithMounter(mounter v1.Mounter) func(r *v1.Config) error { return func(r *v1.Config) error { r.Mounter = mounter return nil @@ -155,7 +154,7 @@ func NewConfig(opts ...GenericOptions) *v1.Config { } if c.Mounter == nil { - c.Mounter = mount.New(constants.MountBinary) + c.Mounter = v1.NewMounter(constants.MountBinary) } return c diff --git a/pkg/config/config_test.go b/pkg/config/config_test.go index 19b72c0e301..8bfb6e7ce3b 100644 --- a/pkg/config/config_test.go +++ b/pkg/config/config_test.go @@ -23,7 +23,6 @@ import ( . "github.com/onsi/ginkgo/v2" . "github.com/onsi/gomega" "github.com/twpayne/go-vfs/vfst" - "k8s.io/mount-utils" "github.com/rancher/elemental-toolkit/pkg/config" "github.com/rancher/elemental-toolkit/pkg/constants" @@ -37,7 +36,7 @@ var _ = Describe("Types", Label("types", "config"), func() { var err error var cleanup func() var fs *vfst.TestFS - var mounter *v1mock.ErrorMounter + var mounter *v1mock.FakeMounter var runner *v1mock.FakeRunner var client *v1mock.FakeHTTPClient var sysc *v1mock.FakeSyscall @@ -47,7 +46,7 @@ var _ = Describe("Types", Label("types", "config"), func() { BeforeEach(func() { fs, cleanup, err = vfst.NewTestFS(nil) Expect(err).ToNot(HaveOccurred()) - mounter = v1mock.NewErrorMounter() + mounter = v1mock.NewFakeMounter() runner = v1mock.NewFakeRunner() client = &v1mock.FakeHTTPClient{} sysc = &v1mock.FakeSyscall{} @@ -99,7 +98,7 @@ var _ = Describe("Types", Label("types", "config"), func() { config.WithSyscall(sysc), config.WithLogger(logger), ) - Expect(c.Mounter).To(Equal(mount.New(constants.MountBinary))) + Expect(c.Mounter).To(Equal(v1.NewMounter(constants.MountBinary))) }) }) Describe("RunConfig", func() { diff --git a/pkg/elemental/elemental_test.go b/pkg/elemental/elemental_test.go index a4d860a24a4..95c55aaae64 100644 --- a/pkg/elemental/elemental_test.go +++ b/pkg/elemental/elemental_test.go @@ -29,7 +29,6 @@ import ( . "github.com/onsi/gomega" "github.com/twpayne/go-vfs" "github.com/twpayne/go-vfs/vfst" - "k8s.io/mount-utils" conf "github.com/rancher/elemental-toolkit/pkg/config" "github.com/rancher/elemental-toolkit/pkg/constants" @@ -55,14 +54,14 @@ var _ = Describe("Elemental", Label("elemental"), func() { var logger v1.Logger var syscall v1.SyscallInterface var client *v1mock.FakeHTTPClient - var mounter *v1mock.ErrorMounter + var mounter *v1mock.FakeMounter var extractor *v1mock.FakeImageExtractor var fs *vfst.TestFS var cleanup func() BeforeEach(func() { runner = v1mock.NewFakeRunner() syscall = &v1mock.FakeSyscall{} - mounter = v1mock.NewErrorMounter() + mounter = v1mock.NewFakeMounter() client = &v1mock.FakeHTTPClient{} logger = v1.NewNullLogger() extractor = v1mock.NewFakeImageExtractor(logger) @@ -977,7 +976,7 @@ var _ = Describe("Elemental", Label("elemental"), func() { }) // PathInMountPoints will check if the given path is in the mountPoints list -func pathInMountPoints(mounter mount.Interface, path string) bool { +func pathInMountPoints(mounter *v1mock.FakeMounter, path string) bool { mountPoints, _ := mounter.List() for _, m := range mountPoints { if path == m.Path { diff --git a/pkg/mocks/mounter_mock.go b/pkg/mocks/mounter_mock.go index 51f827305fd..bbd3bb1dd17 100644 --- a/pkg/mocks/mounter_mock.go +++ b/pkg/mocks/mounter_mock.go @@ -19,25 +19,28 @@ package mocks import ( "errors" + v1 "github.com/rancher/elemental-toolkit/pkg/types/v1" "k8s.io/mount-utils" ) -// ErrorMounter is a fake mounter for tests that can error out. -type ErrorMounter struct { +var _ v1.Mounter = (*FakeMounter)(nil) + +// FakeMounter is a fake mounter for tests that can error out. +type FakeMounter struct { ErrorOnMount bool ErrorOnUnmount bool FakeMounter mount.Interface } -// NewErrorMounter returns an ErrorMounter with an instance of FakeMounter inside so we can use its functions -func NewErrorMounter() *ErrorMounter { - return &ErrorMounter{ +// NewFakeMounter returns an FakeMounter with an instance of FakeMounter inside so we can use its functions +func NewFakeMounter() *FakeMounter { + return &FakeMounter{ FakeMounter: &mount.FakeMounter{}, } } // Mount will return an error if ErrorOnMount is true -func (e ErrorMounter) Mount(source string, target string, fstype string, options []string) error { +func (e FakeMounter) Mount(source string, target string, fstype string, options []string) error { if e.ErrorOnMount { return errors.New("mount error") } @@ -45,18 +48,14 @@ func (e ErrorMounter) Mount(source string, target string, fstype string, options } // Unmount will return an error if ErrorOnUnmount is true -func (e ErrorMounter) Unmount(target string) error { +func (e FakeMounter) Unmount(target string) error { if e.ErrorOnUnmount { return errors.New("unmount error") } return e.FakeMounter.Unmount(target) } -func (e ErrorMounter) List() ([]mount.MountPoint, error) { - return e.FakeMounter.List() -} - -func (e ErrorMounter) IsLikelyNotMountPoint(file string) (bool, error) { +func (e FakeMounter) IsLikelyNotMountPoint(file string) (bool, error) { mnts, _ := e.List() for _, mnt := range mnts { if file == mnt.Path { @@ -66,15 +65,7 @@ func (e ErrorMounter) IsLikelyNotMountPoint(file string) (bool, error) { return true, nil } -// We need to have this below to fulfill the interface for mount.Interface - -func (e ErrorMounter) MountSensitive(_, _, _ string, _, _ []string) error { - return nil -} -func (e ErrorMounter) MountSensitiveWithoutSystemd(_, _, _ string, _, _ []string) error { - return nil -} -func (e ErrorMounter) MountSensitiveWithoutSystemdWithMountFlags(_, _, _ string, _, _, _ []string) error { - return nil +// This is not part of the interface, just a helper method for tests +func (e FakeMounter) List() ([]mount.MountPoint, error) { + return e.FakeMounter.List() } -func (e ErrorMounter) GetMountRefs(_ string) ([]string, error) { return []string{}, nil } diff --git a/pkg/types/v1/config.go b/pkg/types/v1/config.go index 969dbc3b8b6..29dc117c6b4 100644 --- a/pkg/types/v1/config.go +++ b/pkg/types/v1/config.go @@ -25,7 +25,6 @@ import ( "strings" "gopkg.in/yaml.v3" - "k8s.io/mount-utils" "github.com/rancher/elemental-toolkit/pkg/constants" ) @@ -45,7 +44,7 @@ const ( type Config struct { Logger Logger Fs FS - Mounter mount.Interface + Mounter Mounter Runner Runner Syscall SyscallInterface CloudInitRunner CloudInitRunner diff --git a/pkg/types/v1/config_test.go b/pkg/types/v1/config_test.go index 447f713e668..889a893fc89 100644 --- a/pkg/types/v1/config_test.go +++ b/pkg/types/v1/config_test.go @@ -37,7 +37,7 @@ var _ = Describe("Types", Label("types", "config"), func() { var config *v1.RunConfig var runner *v1mocks.FakeRunner var fs vfs.FS - var mounter *v1mocks.ErrorMounter + var mounter *v1mocks.FakeMounter var cleanup func() var err error var dockerState, channelState *v1.ImageState @@ -46,7 +46,7 @@ var _ = Describe("Types", Label("types", "config"), func() { BeforeEach(func() { runner = v1mocks.NewFakeRunner() - mounter = v1mocks.NewErrorMounter() + mounter = v1mocks.NewFakeMounter() fs, cleanup, err = vfst.NewTestFS(map[string]interface{}{}) Expect(err).Should(BeNil()) @@ -375,7 +375,7 @@ var _ = Describe("Types", Label("types", "config"), func() { var spec *v1.InstallSpec BeforeEach(func() { - cfg := config.NewConfig(config.WithMounter(v1mocks.NewErrorMounter())) + cfg := config.NewConfig(config.WithMounter(v1mocks.NewFakeMounter())) spec = config.NewInstallSpec(*cfg) }) Describe("sanitize", func() { diff --git a/pkg/types/v1/mounter.go b/pkg/types/v1/mounter.go new file mode 100644 index 00000000000..51454128326 --- /dev/null +++ b/pkg/types/v1/mounter.go @@ -0,0 +1,36 @@ +/* +Copyright © 2022 - 2023 SUSE LLC + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package v1 + +import ( + "k8s.io/mount-utils" +) + +// This is is just a redefinition of mount.Interface to v1.Mounter types +type Mounter interface { + Mount(source string, target string, fstype string, options []string) error + Unmount(target string) error + IsLikelyNotMountPoint(file string) (bool, error) +} + +func NewMounter(binary string) Mounter { + return mount.New(binary) +} + +func NewDummyMounter() Mounter { + return mount.NewFakeMounter([]mount.MountPoint{}) +} diff --git a/pkg/utils/runstage_test.go b/pkg/utils/runstage_test.go index d2ef705ea69..184963656ba 100644 --- a/pkg/utils/runstage_test.go +++ b/pkg/utils/runstage_test.go @@ -55,7 +55,7 @@ var _ = Describe("run stage", Label("RunStage"), func() { var logger v1.Logger var syscall *v1mock.FakeSyscall var client *v1mock.FakeHTTPClient - var mounter *v1mock.ErrorMounter + var mounter *v1mock.FakeMounter var fs vfs.FS var memLog *bytes.Buffer diff --git a/pkg/utils/utils_test.go b/pkg/utils/utils_test.go index 208e0e4ab56..eb58eb27f93 100644 --- a/pkg/utils/utils_test.go +++ b/pkg/utils/utils_test.go @@ -55,7 +55,7 @@ var _ = Describe("Utils", Label("utils"), func() { var logger v1.Logger var syscall *v1mock.FakeSyscall var client *v1mock.FakeHTTPClient - var mounter *v1mock.ErrorMounter + var mounter *v1mock.FakeMounter var extractor *v1mock.FakeImageExtractor var fs vfs.FS var cleanup func() @@ -63,7 +63,7 @@ var _ = Describe("Utils", Label("utils"), func() { BeforeEach(func() { runner = v1mock.NewFakeRunner() syscall = &v1mock.FakeSyscall{} - mounter = v1mock.NewErrorMounter() + mounter = v1mock.NewFakeMounter() client = &v1mock.FakeHTTPClient{} logger = v1.NewNullLogger() realRunner = &v1.RealRunner{Logger: logger}