Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
K
k3s
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Jacklull
k3s
Commits
be2e53cb
Commit
be2e53cb
authored
Apr 28, 2018
by
andyzhangx
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add formatAndMount unit test on Windows
fix comments fix comments fix comments fix comments
parent
50b9c459
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
77 additions
and
4 deletions
+77
-4
mount_windows.go
pkg/util/mount/mount_windows.go
+4
-4
mount_windows_test.go
pkg/util/mount/mount_windows_test.go
+73
-0
No files found.
pkg/util/mount/mount_windows.go
View file @
be2e53cb
...
@@ -358,7 +358,7 @@ func (mounter *SafeFormatAndMount) formatAndMount(source string, target string,
...
@@ -358,7 +358,7 @@ func (mounter *SafeFormatAndMount) formatAndMount(source string, target string,
glog
.
V
(
4
)
.
Infof
(
"Attempting to formatAndMount disk: %s %s %s"
,
fstype
,
source
,
target
)
glog
.
V
(
4
)
.
Infof
(
"Attempting to formatAndMount disk: %s %s %s"
,
fstype
,
source
,
target
)
if
err
:=
ValidateDiskNumber
(
source
);
err
!=
nil
{
if
err
:=
ValidateDiskNumber
(
source
);
err
!=
nil
{
glog
.
Errorf
(
"diskMount: formatAndMount failed, err: %v
\n
"
,
err
)
glog
.
Errorf
(
"diskMount: formatAndMount failed, err: %v"
,
err
)
return
err
return
err
}
}
...
@@ -368,12 +368,12 @@ func (mounter *SafeFormatAndMount) formatAndMount(source string, target string,
...
@@ -368,12 +368,12 @@ func (mounter *SafeFormatAndMount) formatAndMount(source string, target string,
}
}
// format disk if it is unformatted(raw)
// format disk if it is unformatted(raw)
cmd
:=
fmt
.
Sprintf
(
"Get-Disk -Number %s | Where partitionstyle -eq 'raw' | Initialize-Disk -PartitionStyle MBR -PassThru"
,
source
)
cmd
:=
fmt
.
Sprintf
(
"Get-Disk -Number %s | Where partitionstyle -eq 'raw' | Initialize-Disk -PartitionStyle MBR -PassThru"
+
cmd
+=
fmt
.
Sprintf
(
" | New-Partition -AssignDriveLetter -UseMaximumSize | Format-Volume -FileSystem %s -Confirm:$false"
,
fstype
)
" | New-Partition -AssignDriveLetter -UseMaximumSize | Format-Volume -FileSystem %s -Confirm:$false"
,
source
,
fstype
)
if
output
,
err
:=
mounter
.
Exec
.
Run
(
"powershell"
,
"/c"
,
cmd
);
err
!=
nil
{
if
output
,
err
:=
mounter
.
Exec
.
Run
(
"powershell"
,
"/c"
,
cmd
);
err
!=
nil
{
return
fmt
.
Errorf
(
"diskMount: format disk failed, error: %v, output: %q"
,
err
,
string
(
output
))
return
fmt
.
Errorf
(
"diskMount: format disk failed, error: %v, output: %q"
,
err
,
string
(
output
))
}
}
glog
.
V
(
4
)
.
Infof
(
"diskMount: Disk successfully formatted, disk: %q, fstype: %q
\n
"
,
source
,
fstype
)
glog
.
V
(
4
)
.
Infof
(
"diskMount: Disk successfully formatted, disk: %q, fstype: %q"
,
source
,
fstype
)
driveLetter
,
err
:=
getDriveLetterByDiskNumber
(
source
,
mounter
.
Exec
)
driveLetter
,
err
:=
getDriveLetterByDiskNumber
(
source
,
mounter
.
Exec
)
if
err
!=
nil
{
if
err
!=
nil
{
...
...
pkg/util/mount/mount_windows_test.go
View file @
be2e53cb
...
@@ -24,6 +24,7 @@ import (
...
@@ -24,6 +24,7 @@ import (
"os"
"os"
"os/exec"
"os/exec"
"path/filepath"
"path/filepath"
"strings"
"testing"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/assert"
...
@@ -601,3 +602,75 @@ func TestGetFileType(t *testing.T) {
...
@@ -601,3 +602,75 @@ func TestGetFileType(t *testing.T) {
}
}
}
}
}
}
func
TestFormatAndMount
(
t
*
testing
.
T
)
{
fakeMounter
:=
ErrorMounter
{
&
FakeMounter
{},
0
,
nil
}
execCallback
:=
func
(
cmd
string
,
args
...
string
)
([]
byte
,
error
)
{
for
j
:=
range
args
{
if
strings
.
Contains
(
args
[
j
],
"Get-Disk -Number"
)
{
return
[]
byte
(
"0"
),
nil
}
if
strings
.
Contains
(
args
[
j
],
"Get-Partition -DiskNumber"
)
{
return
[]
byte
(
"0"
),
nil
}
if
strings
.
Contains
(
args
[
j
],
"mklink"
)
{
return
nil
,
nil
}
}
return
nil
,
fmt
.
Errorf
(
"Unexpected cmd %s, args %v"
,
cmd
,
args
)
}
fakeExec
:=
NewFakeExec
(
execCallback
)
mounter
:=
SafeFormatAndMount
{
Interface
:
&
fakeMounter
,
Exec
:
fakeExec
,
}
tests
:=
[]
struct
{
device
string
target
string
fstype
string
mountOptions
[]
string
expectError
bool
}{
{
"0"
,
"disk"
,
"NTFS"
,
[]
string
{},
false
,
},
{
"0"
,
"disk"
,
""
,
[]
string
{},
false
,
},
{
"invalidDevice"
,
"disk"
,
"NTFS"
,
[]
string
{},
true
,
},
}
for
_
,
test
:=
range
tests
{
base
,
err
:=
ioutil
.
TempDir
(
""
,
test
.
device
)
if
err
!=
nil
{
t
.
Fatalf
(
err
.
Error
())
}
defer
os
.
RemoveAll
(
base
)
target
:=
filepath
.
Join
(
base
,
test
.
target
)
err
=
mounter
.
FormatAndMount
(
test
.
device
,
target
,
test
.
fstype
,
test
.
mountOptions
)
if
test
.
expectError
{
assert
.
NotNil
(
t
,
err
,
"Expect error during FormatAndMount(%s, %s, %s, %v)"
,
test
.
device
,
test
.
target
,
test
.
fstype
,
test
.
mountOptions
)
}
else
{
assert
.
Nil
(
t
,
err
,
"Expect error is nil during FormatAndMount(%s, %s, %s, %v)"
,
test
.
device
,
test
.
target
,
test
.
fstype
,
test
.
mountOptions
)
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment