-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #423 from suleymanakbas91/wipefs
OCPVE-600: Implement the ability to wipe the devices
- Loading branch information
Showing
22 changed files
with
768 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package dmsetup | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/openshift/lvm-operator/pkg/internal/exec" | ||
) | ||
|
||
var ( | ||
DefaultDMSetup = "/usr/sbin/dmsetup" | ||
ErrReferenceNotFound = errors.New("device-mapper reference not found") | ||
) | ||
|
||
type Dmsetup interface { | ||
Remove(deviceName string) error | ||
} | ||
|
||
type HostDmsetup struct { | ||
exec.Executor | ||
dmsetup string | ||
} | ||
|
||
func NewDefaultHostDmsetup() *HostDmsetup { | ||
return NewHostDmsetup(&exec.CommandExecutor{}, DefaultDMSetup) | ||
} | ||
|
||
func NewHostDmsetup(executor exec.Executor, dmsetup string) *HostDmsetup { | ||
return &HostDmsetup{ | ||
Executor: executor, | ||
dmsetup: dmsetup, | ||
} | ||
} | ||
|
||
// Remove removes the device's reference from the device-mapper | ||
func (dmsetup *HostDmsetup) Remove(deviceName string) error { | ||
if len(deviceName) == 0 { | ||
return errors.New("failed to remove device-mapper reference. Device name is empty") | ||
} | ||
|
||
args := []string{"remove"} | ||
args = append(args, deviceName) | ||
output, err := dmsetup.ExecuteCommandWithOutputAsHost(dmsetup.dmsetup, args...) | ||
if err != nil { | ||
if strings.Contains(output, "not found") { | ||
return ErrReferenceNotFound | ||
} | ||
return fmt.Errorf("failed to remove the reference from device-mapper %q: %v", deviceName, err) | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package dmsetup | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"testing" | ||
|
||
mockExec "github.com/openshift/lvm-operator/pkg/internal/exec/test" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestRemove(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
deviceName string | ||
wantErr bool | ||
}{ | ||
{"Empty device name", "", true}, | ||
{"Existing device name", "/dev/loop1", false}, | ||
{"Non-existing device name", "/dev/loop2", true}, | ||
} | ||
|
||
executor := &mockExec.MockExecutor{ | ||
MockExecuteCommandWithOutputAsHost: func(command string, args ...string) (string, error) { | ||
if args[0] != "remove" { | ||
return "", fmt.Errorf("invalid args %q", args[0]) | ||
} | ||
if args[1] == "/dev/loop1" { | ||
return "", nil | ||
} else if args[1] == "/dev/loop2" { | ||
return "device loop2 not found", errors.New("device loop2 not found") | ||
} | ||
return "", fmt.Errorf("invalid args %q", args[1]) | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
err := NewHostDmsetup(executor, DefaultDMSetup).Remove(tt.deviceName) | ||
if tt.wantErr { | ||
assert.Error(t, err) | ||
} else { | ||
assert.NoError(t, err) | ||
} | ||
}) | ||
} | ||
} |
Oops, something went wrong.