Skip to content

Commit 0bbed4f

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

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
@@ -24,6 +24,7 @@ const (
2424

2525
type reconfigurer struct {
2626
*reconfigureMIGOptions
27+
migParted migParted
2728
commandRunner
2829
}
2930

@@ -45,9 +46,17 @@ func New(opts ...Option) (Reconfigurer, error) {
4546
return nil, err
4647
}
4748

49+
c := &commandWithOutput{}
50+
4851
r := &reconfigurer{
4952
reconfigureMIGOptions: o,
50-
commandRunner: &commandWithOutput{},
53+
commandRunner: c,
54+
migParted: &migPartedCLI{
55+
MIGPartedConfigFile: o.MIGPartedConfigFile,
56+
SelectedMIGConfig: o.SelectedMIGConfig,
57+
DriverLibraryPath: o.DriverLibraryPath,
58+
commandRunner: c,
59+
},
5160
}
5261

5362
return r, nil
@@ -60,7 +69,7 @@ func New(opts ...Option) (Reconfigurer, error) {
6069
// applied safely with proper service lifecycle management.
6170
func (opts *reconfigurer) Reconfigure() error {
6271
log.Info("Asserting that the requested configuration is present in the configuration file")
63-
if err := opts.assertValidMIGConfig(); err != nil {
72+
if err := opts.migParted.assertValidMIGConfig(); err != nil {
6473
return fmt.Errorf("error validating the selected MIG configuration: %w", err)
6574
}
6675

@@ -72,7 +81,7 @@ func (opts *reconfigurer) Reconfigure() error {
7281
log.Infof("Current value of '%s=%s'", opts.configStateLabel, state)
7382

7483
log.Info("Checking if the selected MIG config is currently applied or not")
75-
if err := opts.assertMIGConfig(); err == nil {
84+
if err := opts.migParted.assertMIGConfig(); err == nil {
7685
log.Info("MIG configuration already applied")
7786
return nil
7887
}
@@ -90,7 +99,7 @@ func (opts *reconfigurer) Reconfigure() error {
9099
log.Info("Checking if the MIG mode setting in the selected config is currently applied or not")
91100
log.Infof("If the state is '%s', we expect this to always return true", configStateRebooting)
92101
migModeChangeRequired := false
93-
if err := opts.assertMIGModeOnly(); err != nil {
102+
if err := opts.migParted.assertMIGModeOnly(); err != nil {
94103
if state == configStateRebooting {
95104
return fmt.Errorf("MIG mode change failed after reboot: %w", err)
96105
}
@@ -113,7 +122,7 @@ func (opts *reconfigurer) Reconfigure() error {
113122

114123
log.Info("Applying the MIG mode change from the selected config to the node (and double checking it took effect)")
115124
log.Info("If the -r option was passed, the node will be automatically rebooted if this is not successful")
116-
if err := opts.applyMIGModeOnly(); err != nil || opts.assertMIGModeOnly() != nil {
125+
if err := opts.migParted.applyMIGModeOnly(); err != nil || opts.migParted.assertMIGModeOnly() != nil {
117126
if opts.WithReboot {
118127
log.Infof("Changing the '%s' node label to '%s'", opts.configStateLabel, configStateRebooting)
119128
if err := opts.setNodeLabelValue(opts.configStateLabel, configStateRebooting); err != nil {
@@ -126,7 +135,7 @@ func (opts *reconfigurer) Reconfigure() error {
126135
}
127136

128137
log.Info("Applying the selected MIG config to the node")
129-
if err := opts.applyMIGConfig(); err != nil {
138+
if err := opts.migParted.applyMIGConfig(); err != nil {
130139
return err
131140
}
132141

@@ -140,7 +149,16 @@ func (opts *reconfigurer) Reconfigure() error {
140149
return nil
141150
}
142151

143-
func (opts *reconfigurer) assertValidMIGConfig() error {
152+
type migPartedCLI struct {
153+
MIGPartedConfigFile string
154+
SelectedMIGConfig string
155+
DriverLibraryPath string
156+
commandRunner
157+
}
158+
159+
var _ migParted = (*migPartedCLI)(nil)
160+
161+
func (opts *migPartedCLI) assertValidMIGConfig() error {
144162
args := []string{
145163
"--debug",
146164
"assert",
@@ -151,7 +169,7 @@ func (opts *reconfigurer) assertValidMIGConfig() error {
151169
return opts.runMigParted(args...)
152170
}
153171

154-
func (opts *reconfigurer) assertMIGConfig() error {
172+
func (opts *migPartedCLI) assertMIGConfig() error {
155173
args := []string{
156174
"--debug",
157175
"assert",
@@ -161,7 +179,7 @@ func (opts *reconfigurer) assertMIGConfig() error {
161179
return opts.runMigParted(args...)
162180
}
163181

164-
func (opts *reconfigurer) assertMIGModeOnly() error {
182+
func (opts *migPartedCLI) assertMIGModeOnly() error {
165183
args := []string{
166184
"--debug",
167185
"assert",
@@ -172,7 +190,7 @@ func (opts *reconfigurer) assertMIGModeOnly() error {
172190
return opts.runMigParted(args...)
173191
}
174192

175-
func (opts *reconfigurer) applyMIGModeOnly() error {
193+
func (opts *migPartedCLI) applyMIGModeOnly() error {
176194
args := []string{
177195
"--debug",
178196
"apply",
@@ -183,7 +201,7 @@ func (opts *reconfigurer) applyMIGModeOnly() error {
183201
return opts.runMigParted(args...)
184202
}
185203

186-
func (opts *reconfigurer) applyMIGConfig() error {
204+
func (opts *migPartedCLI) applyMIGConfig() error {
187205
args := []string{
188206
"--debug",
189207
"apply",
@@ -337,12 +355,12 @@ func (c *commandWithOutput) Run(cmd *exec.Cmd) error {
337355
return cmd.Run()
338356
}
339357

340-
func (opts *reconfigurer) runMigParted(args ...string) error {
358+
func (opts *migPartedCLI) runMigParted(args ...string) error {
341359
cmd := opts.migPartedCmd(args...)
342360
return opts.commandRunner.Run(cmd)
343361
}
344362

345-
func (opts *reconfigureMIGOptions) migPartedCmd(args ...string) *exec.Cmd {
363+
func (opts *migPartedCLI) migPartedCmd(args ...string) *exec.Cmd {
346364
cmd := exec.Command(migPartedCliName, args...)
347365
cmd.Env = append(os.Environ(), fmt.Sprintf("%s=%s", ldPreloadEnvVar, opts.DriverLibraryPath))
348366
return cmd

0 commit comments

Comments
 (0)