util_test.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. package testing
  2. import (
  3. "errors"
  4. "os"
  5. "path/filepath"
  6. "strings"
  7. "testing"
  8. "time"
  9. "devel.mephi.ru/iacherepanov/openstack-gophercloud"
  10. th "devel.mephi.ru/iacherepanov/openstack-gophercloud/testhelper"
  11. )
  12. func TestWaitFor(t *testing.T) {
  13. err := gophercloud.WaitFor(2, func() (bool, error) {
  14. return true, nil
  15. })
  16. th.CheckNoErr(t, err)
  17. }
  18. func TestWaitForTimeout(t *testing.T) {
  19. if testing.Short() {
  20. t.Skip("skipping test in short mode.")
  21. }
  22. err := gophercloud.WaitFor(1, func() (bool, error) {
  23. return false, nil
  24. })
  25. th.AssertEquals(t, "A timeout occurred", err.Error())
  26. }
  27. func TestWaitForError(t *testing.T) {
  28. if testing.Short() {
  29. t.Skip("skipping test in short mode.")
  30. }
  31. err := gophercloud.WaitFor(2, func() (bool, error) {
  32. return false, errors.New("Error has occurred")
  33. })
  34. th.AssertEquals(t, "Error has occurred", err.Error())
  35. }
  36. func TestWaitForPredicateExceed(t *testing.T) {
  37. if testing.Short() {
  38. t.Skip("skipping test in short mode.")
  39. }
  40. err := gophercloud.WaitFor(1, func() (bool, error) {
  41. time.Sleep(4 * time.Second)
  42. return false, errors.New("Just wasting time")
  43. })
  44. th.AssertEquals(t, "A timeout occurred", err.Error())
  45. }
  46. func TestNormalizeURL(t *testing.T) {
  47. urls := []string{
  48. "NoSlashAtEnd",
  49. "SlashAtEnd/",
  50. }
  51. expected := []string{
  52. "NoSlashAtEnd/",
  53. "SlashAtEnd/",
  54. }
  55. for i := 0; i < len(expected); i++ {
  56. th.CheckEquals(t, expected[i], gophercloud.NormalizeURL(urls[i]))
  57. }
  58. }
  59. func TestNormalizePathURL(t *testing.T) {
  60. baseDir, _ := os.Getwd()
  61. rawPath := "template.yaml"
  62. basePath, _ := filepath.Abs(".")
  63. result, _ := gophercloud.NormalizePathURL(basePath, rawPath)
  64. expected := strings.Join([]string{"file:/", filepath.ToSlash(baseDir), "template.yaml"}, "/")
  65. th.CheckEquals(t, expected, result)
  66. rawPath = "http://www.google.com"
  67. basePath, _ = filepath.Abs(".")
  68. result, _ = gophercloud.NormalizePathURL(basePath, rawPath)
  69. expected = "http://www.google.com"
  70. th.CheckEquals(t, expected, result)
  71. rawPath = "very/nested/file.yaml"
  72. basePath, _ = filepath.Abs(".")
  73. result, _ = gophercloud.NormalizePathURL(basePath, rawPath)
  74. expected = strings.Join([]string{"file:/", filepath.ToSlash(baseDir), "very/nested/file.yaml"}, "/")
  75. th.CheckEquals(t, expected, result)
  76. rawPath = "very/nested/file.yaml"
  77. basePath = "http://www.google.com"
  78. result, _ = gophercloud.NormalizePathURL(basePath, rawPath)
  79. expected = "http://www.google.com/very/nested/file.yaml"
  80. th.CheckEquals(t, expected, result)
  81. rawPath = "very/nested/file.yaml/"
  82. basePath = "http://www.google.com/"
  83. result, _ = gophercloud.NormalizePathURL(basePath, rawPath)
  84. expected = "http://www.google.com/very/nested/file.yaml"
  85. th.CheckEquals(t, expected, result)
  86. rawPath = "very/nested/file.yaml"
  87. basePath = "http://www.google.com/even/more"
  88. result, _ = gophercloud.NormalizePathURL(basePath, rawPath)
  89. expected = "http://www.google.com/even/more/very/nested/file.yaml"
  90. th.CheckEquals(t, expected, result)
  91. rawPath = "very/nested/file.yaml"
  92. basePath = strings.Join([]string{"file:/", filepath.ToSlash(baseDir), "only/file/even/more"}, "/")
  93. result, _ = gophercloud.NormalizePathURL(basePath, rawPath)
  94. expected = strings.Join([]string{"file:/", filepath.ToSlash(baseDir), "only/file/even/more/very/nested/file.yaml"}, "/")
  95. th.CheckEquals(t, expected, result)
  96. rawPath = "very/nested/file.yaml/"
  97. basePath = strings.Join([]string{"file:/", filepath.ToSlash(baseDir), "only/file/even/more"}, "/")
  98. result, _ = gophercloud.NormalizePathURL(basePath, rawPath)
  99. expected = strings.Join([]string{"file:/", filepath.ToSlash(baseDir), "only/file/even/more/very/nested/file.yaml"}, "/")
  100. th.CheckEquals(t, expected, result)
  101. }