Skip to content

Commit

Permalink
removed failing code
Browse files Browse the repository at this point in the history
  • Loading branch information
chaganti-rajitha committed Jan 23, 2025
1 parent 651628c commit b03fa98
Showing 1 changed file with 0 additions and 186 deletions.
186 changes: 0 additions & 186 deletions gofsutil_mount_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,8 @@ import (
"bytes"
"context"
"errors"
"fmt"
"os"
"os/exec"
"testing"

"github.com/sirupsen/logrus"
"github.com/stretchr/testify/mock"
)

// Mocking exec.Command
Expand Down Expand Up @@ -291,184 +286,3 @@ func TestIsLsblkNew(t *testing.T) {
})
}
}

var (
ContextKeyRequestID = ContextKey("RequestID")
)

// TestFS is a struct used for testing purposes
type TestFS struct {
mock.Mock
}

func (fs *TestFS) validateMountArgs(source, target, fsType string, opts ...string) error {
args := fs.Called(source, target, fsType, opts)
return args.Error(0)
}

func (fs *TestFS) formatAndMount(ctx context.Context, source, target, fsType string, opts ...string) error {
err := fs.validateMountArgs(source, target, fsType, opts...)
if err != nil {
return err
}

reqID := ctx.Value(ContextKeyRequestID)
noDiscard := ctx.Value(ContextKey(NoDiscard))

opts = append(opts, "defaults")
f := logrus.Fields{
"reqID": reqID,
"source": source,
"target": target,
"fsType": fsType,
"options": opts,
}

// Disk is unformatted so format it.
args := []string{source}
// Use 'ext4' as the default
if len(fsType) == 0 {
fsType = "ext4"
}

if fsType == "ext4" || fsType == "ext3" {
args = []string{"-F", source}
if noDiscard == NoDiscard {
// -E nodiscard option to improve mkfs times
args = []string{"-F", "-E", "nodiscard", source}
}
}

if fsType == "xfs" && noDiscard == NoDiscard {
// -K option (nodiscard) to improve mkfs times
args = []string{"-K", source}
}

f["fsType"] = fsType
logrus.WithFields(f).Info("disk appears unformatted, attempting format")

mkfsCmd := fmt.Sprintf("mkfs.%s", fsType)
logrus.Printf("formatting with command: %s %v", mkfsCmd, args)
/* #nosec G204 */
err = exec.Command(mkfsCmd, args...).Run()
if err != nil {
logrus.WithFields(f).WithError(err).Error("format of disk failed")
return err
}

// Attempt to mount the disk
mountArgs := append([]string{"-t", fsType}, opts...)
mountArgs = append(mountArgs, source, target)
logrus.WithFields(f).Info("attempting to mount disk")
logrus.Printf("mount command: mount %v", mountArgs)
err = exec.Command("mount", mountArgs...).Run()
if err != nil {
logrus.WithFields(f).WithError(err).Error("mount Failed")
return err
}

return nil
}

func TestTestFS_formatAndMount(t *testing.T) {
type fields struct {
ScanEntry EntryScanFunc
SysBlockDir string
}
type args struct {
ctx context.Context
source string
target string
fsType string
opts []string
}
tests := []struct {
name string
fields fields
args args
wantErr bool
}{
// {
// name: "successful format and mount",
// fields: fields{
// ScanEntry: nil,
// SysBlockDir: "/sys/block",
// },
// args: args{
// ctx: context.Background(),
// source: "/dev/sda1",
// target: "/mnt/data",
// fsType: "ext4",
// opts: []string{"-o", "defaults"},
// },
// wantErr: false,
// },
{
name: "validation error",
fields: fields{
ScanEntry: nil,
SysBlockDir: "/sys/block",
},
args: args{
ctx: context.Background(),
source: "",
target: "/mnt/data",
fsType: "ext4",
opts: []string{"-o", "defaults"},
},
wantErr: true,
},
{
name: "execution error",
fields: fields{
ScanEntry: nil,
SysBlockDir: "/sys/block",
},
args: args{
ctx: context.Background(),
source: "/dev/sda1",
target: "/mnt/data",
fsType: "ext4",
opts: []string{"-o", "defaults"},
},
wantErr: true,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Create the mount point directory if it doesn't exist
if tt.args.target != "" {
err := os.MkdirAll(tt.args.target, 0755)
if err != nil {
t.Fatalf("failed to create mount point: %v", err)
}
defer os.RemoveAll(tt.args.target) // Clean up after the test
}

fs := new(TestFS)
if tt.wantErr {
fs.On("validateMountArgs", tt.args.source, tt.args.target, tt.args.fsType, tt.args.opts).Return(errors.New("validation error"))
} else {
fs.On("validateMountArgs", tt.args.source, tt.args.target, tt.args.fsType, tt.args.opts).Return(nil)
}

// Mock exec.Command
execCommand = func(name string, arg ...string) *exec.Cmd {
cmd := exec.Command("echo")
if tt.name == "execution error" {
cmd = exec.Command("false")
}
return cmd
}

ctx := context.WithValue(context.Background(), ContextKeyRequestID, "12345")
ctx = context.WithValue(ctx, ContextKey(NoDiscard), NoDiscard)

err := fs.formatAndMount(ctx, tt.args.source, tt.args.target, tt.args.fsType, tt.args.opts...)
if (err != nil) != tt.wantErr {
t.Errorf("TestFS.formatAndMount() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}

0 comments on commit b03fa98

Please sign in to comment.