2016-04-26 22:26:12 -04:00
|
|
|
// +build windows
|
|
|
|
|
2018-02-05 16:05:59 -05:00
|
|
|
package system // import "github.com/docker/docker/pkg/system"
|
2016-04-26 22:26:12 -04:00
|
|
|
|
2017-08-03 20:22:00 -04:00
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/containerd/continuity/pathdriver"
|
|
|
|
)
|
2016-04-26 22:26:12 -04:00
|
|
|
|
|
|
|
// TestCheckSystemDriveAndRemoveDriveLetter tests CheckSystemDriveAndRemoveDriveLetter
|
|
|
|
func TestCheckSystemDriveAndRemoveDriveLetter(t *testing.T) {
|
|
|
|
// Fails if not C drive.
|
2017-08-03 20:22:00 -04:00
|
|
|
_, err := CheckSystemDriveAndRemoveDriveLetter(`d:\`, pathdriver.LocalPathDriver)
|
2016-04-26 22:26:12 -04:00
|
|
|
if err == nil || (err != nil && err.Error() != "The specified path is not on the system drive (C:)") {
|
|
|
|
t.Fatalf("Expected error for d:")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Single character is unchanged
|
2017-09-08 18:00:14 -04:00
|
|
|
var path string
|
2017-08-03 20:22:00 -04:00
|
|
|
if path, err = CheckSystemDriveAndRemoveDriveLetter("z", pathdriver.LocalPathDriver); err != nil {
|
2016-04-26 22:26:12 -04:00
|
|
|
t.Fatalf("Single character should pass")
|
|
|
|
}
|
|
|
|
if path != "z" {
|
|
|
|
t.Fatalf("Single character should be unchanged")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Two characters without colon is unchanged
|
2017-08-03 20:22:00 -04:00
|
|
|
if path, err = CheckSystemDriveAndRemoveDriveLetter("AB", pathdriver.LocalPathDriver); err != nil {
|
2016-04-26 22:26:12 -04:00
|
|
|
t.Fatalf("2 characters without colon should pass")
|
|
|
|
}
|
|
|
|
if path != "AB" {
|
|
|
|
t.Fatalf("2 characters without colon should be unchanged")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Abs path without drive letter
|
2017-08-03 20:22:00 -04:00
|
|
|
if path, err = CheckSystemDriveAndRemoveDriveLetter(`\l`, pathdriver.LocalPathDriver); err != nil {
|
2016-04-26 22:26:12 -04:00
|
|
|
t.Fatalf("abs path no drive letter should pass")
|
|
|
|
}
|
|
|
|
if path != `\l` {
|
|
|
|
t.Fatalf("abs path without drive letter should be unchanged")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Abs path without drive letter, linux style
|
2017-08-03 20:22:00 -04:00
|
|
|
if path, err = CheckSystemDriveAndRemoveDriveLetter(`/l`, pathdriver.LocalPathDriver); err != nil {
|
2016-04-26 22:26:12 -04:00
|
|
|
t.Fatalf("abs path no drive letter linux style should pass")
|
|
|
|
}
|
|
|
|
if path != `\l` {
|
|
|
|
t.Fatalf("abs path without drive letter linux failed %s", path)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Drive-colon should be stripped
|
2017-08-03 20:22:00 -04:00
|
|
|
if path, err = CheckSystemDriveAndRemoveDriveLetter(`c:\`, pathdriver.LocalPathDriver); err != nil {
|
2016-04-26 22:26:12 -04:00
|
|
|
t.Fatalf("An absolute path should pass")
|
|
|
|
}
|
|
|
|
if path != `\` {
|
|
|
|
t.Fatalf(`An absolute path should have been shortened to \ %s`, path)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify with a linux-style path
|
2017-08-03 20:22:00 -04:00
|
|
|
if path, err = CheckSystemDriveAndRemoveDriveLetter(`c:/`, pathdriver.LocalPathDriver); err != nil {
|
2016-04-26 22:26:12 -04:00
|
|
|
t.Fatalf("An absolute path should pass")
|
|
|
|
}
|
|
|
|
if path != `\` {
|
|
|
|
t.Fatalf(`A linux style absolute path should have been shortened to \ %s`, path)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Failure on c:
|
2017-08-03 20:22:00 -04:00
|
|
|
if path, err = CheckSystemDriveAndRemoveDriveLetter(`c:`, pathdriver.LocalPathDriver); err == nil {
|
2016-04-26 22:26:12 -04:00
|
|
|
t.Fatalf("c: should fail")
|
|
|
|
}
|
|
|
|
if err.Error() != `No relative path specified in "c:"` {
|
|
|
|
t.Fatalf(path, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Failure on d:
|
2017-08-03 20:22:00 -04:00
|
|
|
if path, err = CheckSystemDriveAndRemoveDriveLetter(`d:`, pathdriver.LocalPathDriver); err == nil {
|
2016-04-26 22:26:12 -04:00
|
|
|
t.Fatalf("c: should fail")
|
|
|
|
}
|
|
|
|
if err.Error() != `No relative path specified in "d:"` {
|
|
|
|
t.Fatalf(path, err)
|
|
|
|
}
|
|
|
|
}
|