Skip to content

Commit 6544a19

Browse files
committed
Add migParted interface for testing
Signed-off-by: Evan Lezar <[email protected]>
1 parent f793f1a commit 6544a19

File tree

4 files changed

+281
-22
lines changed

4 files changed

+281
-22
lines changed

pkg/mig/reconfigure/api.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,14 @@ type Reconfigurer interface {
1919
type commandRunner interface {
2020
Run(*exec.Cmd) error
2121
}
22+
23+
// migParted defines an interface for interacting with mig-parted.
24+
//
25+
//go:generate moq -rm -fmt=goimports -out mig-parted_mock.go . migParted
26+
type migParted interface {
27+
assertValidMIGConfig() error
28+
assertMIGConfig() error
29+
assertMIGModeOnly() error
30+
applyMIGModeOnly() error
31+
applyMIGConfig() error
32+
}

pkg/mig/reconfigure/mig-parted_mock.go

Lines changed: 215 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/mig/reconfigure/reconfigure.go

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ const (
4141

4242
type reconfigurer struct {
4343
*reconfigureMIGOptions
44+
migParted migParted
4445
commandRunner
4546
}
4647

@@ -62,9 +63,17 @@ func New(opts ...Option) (Reconfigurer, error) {
6263
return nil, err
6364
}
6465

66+
c := &commandWithOutput{}
67+
6568
r := &reconfigurer{
6669
reconfigureMIGOptions: o,
67-
commandRunner: &commandWithOutput{},
70+
commandRunner: c,
71+
migParted: &migPartedCLI{
72+
MIGPartedConfigFile: o.MIGPartedConfigFile,
73+
SelectedMIGConfig: o.SelectedMIGConfig,
74+
DriverLibraryPath: o.DriverLibraryPath,
75+
commandRunner: c,
76+
},
6877
}
6978

7079
return r, nil
@@ -77,7 +86,7 @@ func New(opts ...Option) (Reconfigurer, error) {
7786
// applied safely with proper service lifecycle management.
7887
func (opts *reconfigurer) Reconfigure() error {
7988
log.Info("Asserting that the requested configuration is present in the configuration file")
80-
if err := opts.assertValidMIGConfig(); err != nil {
89+
if err := opts.migParted.assertValidMIGConfig(); err != nil {
8190
return fmt.Errorf("error validating the selected MIG configuration: %w", err)
8291
}
8392

@@ -89,7 +98,7 @@ func (opts *reconfigurer) Reconfigure() error {
8998
log.Infof("Current value of '%s=%s'", opts.ConfigStateLabel, state)
9099

91100
log.Info("Checking if the selected MIG config is currently applied or not")
92-
if err := opts.assertMIGConfig(); err == nil {
101+
if err := opts.migParted.assertMIGConfig(); err == nil {
93102
log.Info("MIG configuration already applied")
94103
return nil
95104
}
@@ -107,7 +116,7 @@ func (opts *reconfigurer) Reconfigure() error {
107116
log.Info("Checking if the MIG mode setting in the selected config is currently applied or not")
108117
log.Infof("If the state is '%s', we expect this to always return true", configStateRebooting)
109118
migModeChangeRequired := false
110-
if err := opts.assertMIGModeOnly(); err != nil {
119+
if err := opts.migParted.assertMIGModeOnly(); err != nil {
111120
if state == configStateRebooting {
112121
return fmt.Errorf("MIG mode change failed after reboot: %w", err)
113122
}
@@ -130,7 +139,7 @@ func (opts *reconfigurer) Reconfigure() error {
130139

131140
log.Info("Applying the MIG mode change from the selected config to the node (and double checking it took effect)")
132141
log.Info("If the -r option was passed, the node will be automatically rebooted if this is not successful")
133-
if err := opts.applyMIGModeOnly(); err != nil || opts.assertMIGModeOnly() != nil {
142+
if err := opts.migParted.applyMIGModeOnly(); err != nil || opts.migParted.assertMIGModeOnly() != nil {
134143
if opts.WithReboot {
135144
log.Infof("Changing the '%s' node label to '%s'", opts.ConfigStateLabel, configStateRebooting)
136145
if err := opts.setNodeLabelValue(opts.ConfigStateLabel, configStateRebooting); err != nil {
@@ -143,7 +152,7 @@ func (opts *reconfigurer) Reconfigure() error {
143152
}
144153

145154
log.Info("Applying the selected MIG config to the node")
146-
if err := opts.applyMIGConfig(); err != nil {
155+
if err := opts.migParted.applyMIGConfig(); err != nil {
147156
return err
148157
}
149158

@@ -157,7 +166,16 @@ func (opts *reconfigurer) Reconfigure() error {
157166
return nil
158167
}
159168

160-
func (opts *reconfigurer) assertValidMIGConfig() error {
169+
type migPartedCLI struct {
170+
MIGPartedConfigFile string
171+
SelectedMIGConfig string
172+
DriverLibraryPath string
173+
commandRunner
174+
}
175+
176+
var _ migParted = (*migPartedCLI)(nil)
177+
178+
func (opts *migPartedCLI) assertValidMIGConfig() error {
161179
args := []string{
162180
"--debug",
163181
"assert",
@@ -168,7 +186,7 @@ func (opts *reconfigurer) assertValidMIGConfig() error {
168186
return opts.runMigParted(args...)
169187
}
170188

171-
func (opts *reconfigurer) assertMIGConfig() error {
189+
func (opts *migPartedCLI) assertMIGConfig() error {
172190
args := []string{
173191
"--debug",
174192
"assert",
@@ -178,7 +196,7 @@ func (opts *reconfigurer) assertMIGConfig() error {
178196
return opts.runMigParted(args...)
179197
}
180198

181-
func (opts *reconfigurer) assertMIGModeOnly() error {
199+
func (opts *migPartedCLI) assertMIGModeOnly() error {
182200
args := []string{
183201
"--debug",
184202
"assert",
@@ -189,7 +207,7 @@ func (opts *reconfigurer) assertMIGModeOnly() error {
189207
return opts.runMigParted(args...)
190208
}
191209

192-
func (opts *reconfigurer) applyMIGModeOnly() error {
210+
func (opts *migPartedCLI) applyMIGModeOnly() error {
193211
args := []string{
194212
"--debug",
195213
"apply",
@@ -200,7 +218,7 @@ func (opts *reconfigurer) applyMIGModeOnly() error {
200218
return opts.runMigParted(args...)
201219
}
202220

203-
func (opts *reconfigurer) applyMIGConfig() error {
221+
func (opts *migPartedCLI) applyMIGConfig() error {
204222
args := []string{
205223
"--debug",
206224
"apply",
@@ -354,12 +372,12 @@ func (c *commandWithOutput) Run(cmd *exec.Cmd) error {
354372
return cmd.Run()
355373
}
356374

357-
func (opts *reconfigurer) runMigParted(args ...string) error {
375+
func (opts *migPartedCLI) runMigParted(args ...string) error {
358376
cmd := opts.migPartedCmd(args...)
359377
return opts.Run(cmd)
360378
}
361379

362-
func (opts *reconfigureMIGOptions) migPartedCmd(args ...string) *exec.Cmd {
380+
func (opts *migPartedCLI) migPartedCmd(args ...string) *exec.Cmd {
363381
cmd := exec.Command(migPartedCliName, args...)
364382
cmd.Env = append(os.Environ(), fmt.Sprintf("%s=%s", ldPreloadEnvVar, opts.DriverLibraryPath))
365383
return cmd

0 commit comments

Comments
 (0)