Skip to content

Commit

Permalink
Wrap mount.Interface to our own interface
Browse files Browse the repository at this point in the history
Signed-off-by: David Cassany <[email protected]>
  • Loading branch information
davidcassany committed Dec 12, 2023
1 parent 9676ec9 commit 99367c0
Show file tree
Hide file tree
Showing 24 changed files with 95 additions and 78 deletions.
3 changes: 1 addition & 2 deletions cmd/build-disk.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down
3 changes: 1 addition & 2 deletions cmd/build-iso.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 {
Expand Down
5 changes: 2 additions & 3 deletions cmd/cloud-init.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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)
}
Expand Down
5 changes: 2 additions & 3 deletions cmd/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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),
Expand Down
12 changes: 6 additions & 6 deletions cmd/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
Expand Down
3 changes: 1 addition & 2 deletions cmd/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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 {
Expand Down
3 changes: 1 addition & 2 deletions cmd/pull-image.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions cmd/reset.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions cmd/run-stage.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions cmd/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions pkg/action/build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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{}
Expand Down
4 changes: 2 additions & 2 deletions pkg/action/install_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions pkg/action/reset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions pkg/action/upgrade_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down
5 changes: 2 additions & 3 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
7 changes: 3 additions & 4 deletions pkg/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand All @@ -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{}
Expand Down Expand Up @@ -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() {
Expand Down
Loading

0 comments on commit 99367c0

Please sign in to comment.