hostutil_windows_test.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. // +build windows
  2. /*
  3. Copyright 2017 The Kubernetes Authors.
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. http://www.apache.org/licenses/LICENSE-2.0
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. */
  14. package hostutil
  15. import (
  16. "io/ioutil"
  17. "os"
  18. "testing"
  19. )
  20. func TestGetFileType(t *testing.T) {
  21. hu := NewHostUtil()
  22. testCase := []struct {
  23. name string
  24. expectedType FileType
  25. setUp func() (string, string, error)
  26. }{
  27. {
  28. "Directory Test",
  29. FileTypeDirectory,
  30. func() (string, string, error) {
  31. tempDir, err := ioutil.TempDir("", "test-get-filetype-")
  32. return tempDir, tempDir, err
  33. },
  34. },
  35. {
  36. "File Test",
  37. FileTypeFile,
  38. func() (string, string, error) {
  39. tempFile, err := ioutil.TempFile("", "test-get-filetype")
  40. if err != nil {
  41. return "", "", err
  42. }
  43. tempFile.Close()
  44. return tempFile.Name(), tempFile.Name(), nil
  45. },
  46. },
  47. }
  48. for idx, tc := range testCase {
  49. path, cleanUpPath, err := tc.setUp()
  50. if err != nil {
  51. t.Fatalf("[%d-%s] unexpected error : %v", idx, tc.name, err)
  52. }
  53. if len(cleanUpPath) > 0 {
  54. defer os.RemoveAll(cleanUpPath)
  55. }
  56. fileType, err := hu.GetFileType(path)
  57. if err != nil {
  58. t.Fatalf("[%d-%s] unexpected error : %v", idx, tc.name, err)
  59. }
  60. if fileType != tc.expectedType {
  61. t.Fatalf("[%d-%s] expected %s, but got %s", idx, tc.name, tc.expectedType, fileType)
  62. }
  63. }
  64. }