path_windows_test.go 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. // +build windows
  2. package system // import "github.com/docker/docker/pkg/system"
  3. import (
  4. "testing"
  5. "github.com/containerd/continuity/pathdriver"
  6. )
  7. // TestCheckSystemDriveAndRemoveDriveLetter tests CheckSystemDriveAndRemoveDriveLetter
  8. func TestCheckSystemDriveAndRemoveDriveLetter(t *testing.T) {
  9. // Fails if not C drive.
  10. _, err := CheckSystemDriveAndRemoveDriveLetter(`d:\`, pathdriver.LocalPathDriver)
  11. if err == nil || err.Error() != "The specified path is not on the system drive (C:)" {
  12. t.Fatalf("Expected error for d:")
  13. }
  14. // Single character is unchanged
  15. var path string
  16. if path, err = CheckSystemDriveAndRemoveDriveLetter("z", pathdriver.LocalPathDriver); err != nil {
  17. t.Fatalf("Single character should pass")
  18. }
  19. if path != "z" {
  20. t.Fatalf("Single character should be unchanged")
  21. }
  22. // Two characters without colon is unchanged
  23. if path, err = CheckSystemDriveAndRemoveDriveLetter("AB", pathdriver.LocalPathDriver); err != nil {
  24. t.Fatalf("2 characters without colon should pass")
  25. }
  26. if path != "AB" {
  27. t.Fatalf("2 characters without colon should be unchanged")
  28. }
  29. // Abs path without drive letter
  30. if path, err = CheckSystemDriveAndRemoveDriveLetter(`\l`, pathdriver.LocalPathDriver); err != nil {
  31. t.Fatalf("abs path no drive letter should pass")
  32. }
  33. if path != `\l` {
  34. t.Fatalf("abs path without drive letter should be unchanged")
  35. }
  36. // Abs path without drive letter, linux style
  37. if path, err = CheckSystemDriveAndRemoveDriveLetter(`/l`, pathdriver.LocalPathDriver); err != nil {
  38. t.Fatalf("abs path no drive letter linux style should pass")
  39. }
  40. if path != `\l` {
  41. t.Fatalf("abs path without drive letter linux failed %s", path)
  42. }
  43. // Drive-colon should be stripped
  44. if path, err = CheckSystemDriveAndRemoveDriveLetter(`c:\`, pathdriver.LocalPathDriver); err != nil {
  45. t.Fatalf("An absolute path should pass")
  46. }
  47. if path != `\` {
  48. t.Fatalf(`An absolute path should have been shortened to \ %s`, path)
  49. }
  50. // Verify with a linux-style path
  51. if path, err = CheckSystemDriveAndRemoveDriveLetter(`c:/`, pathdriver.LocalPathDriver); err != nil {
  52. t.Fatalf("An absolute path should pass")
  53. }
  54. if path != `\` {
  55. t.Fatalf(`A linux style absolute path should have been shortened to \ %s`, path)
  56. }
  57. // Failure on c:
  58. if path, err = CheckSystemDriveAndRemoveDriveLetter(`c:`, pathdriver.LocalPathDriver); err == nil {
  59. t.Fatalf("c: should fail")
  60. }
  61. if err.Error() != `No relative path specified in "c:"` {
  62. t.Fatalf(path, err)
  63. }
  64. // Failure on d:
  65. if path, err = CheckSystemDriveAndRemoveDriveLetter(`d:`, pathdriver.LocalPathDriver); err == nil {
  66. t.Fatalf("c: should fail")
  67. }
  68. if err.Error() != `No relative path specified in "d:"` {
  69. t.Fatalf(path, err)
  70. }
  71. }