github.com/opentelekomcloud/gophertelekomcloud@v0.9.3/testing/util_test.go (about) 1 package testing 2 3 import ( 4 "errors" 5 "os" 6 "path/filepath" 7 "strings" 8 "testing" 9 "time" 10 11 golangsdk "github.com/opentelekomcloud/gophertelekomcloud" 12 th "github.com/opentelekomcloud/gophertelekomcloud/testhelper" 13 ) 14 15 func TestWaitFor(t *testing.T) { 16 err := golangsdk.WaitFor(2, func() (bool, error) { 17 return true, nil 18 }) 19 th.CheckNoErr(t, err) 20 } 21 22 func TestWaitForTimeout(t *testing.T) { 23 if testing.Short() { 24 t.Skip("skipping test in short mode.") 25 } 26 27 err := golangsdk.WaitFor(1, func() (bool, error) { 28 return false, nil 29 }) 30 if err == nil { 31 t.Fatalf("Expected to receive error") 32 } 33 th.AssertEquals(t, "A timeout occurred", err.Error()) 34 } 35 36 func TestWaitForError(t *testing.T) { 37 if testing.Short() { 38 t.Skip("skipping test in short mode.") 39 } 40 41 err := golangsdk.WaitFor(2, func() (bool, error) { 42 return false, errors.New("Error has occurred") 43 }) 44 if err == nil { 45 t.Fatalf("Expected to receive error") 46 } 47 th.AssertEquals(t, "Error has occurred", err.Error()) 48 } 49 50 func TestWaitForPredicateExceed(t *testing.T) { 51 if testing.Short() { 52 t.Skip("skipping test in short mode.") 53 } 54 55 err := golangsdk.WaitFor(1, func() (bool, error) { 56 time.Sleep(4 * time.Second) 57 return false, errors.New("Just wasting time") 58 }) 59 if err == nil { 60 t.Fatalf("Expected to receive error") 61 } 62 th.AssertEquals(t, "A timeout occurred", err.Error()) 63 } 64 65 func TestNormalizeURL(t *testing.T) { 66 urls := []string{ 67 "NoSlashAtEnd", 68 "SlashAtEnd/", 69 } 70 expected := []string{ 71 "NoSlashAtEnd/", 72 "SlashAtEnd/", 73 } 74 for i := 0; i < len(expected); i++ { 75 th.CheckEquals(t, expected[i], golangsdk.NormalizeURL(urls[i])) 76 } 77 78 } 79 80 func TestNormalizePathURL(t *testing.T) { 81 baseDir, _ := os.Getwd() 82 83 rawPath := "template.yaml" 84 basePath, _ := filepath.Abs(".") 85 result, _ := golangsdk.NormalizePathURL(basePath, rawPath) 86 expected := strings.Join([]string{"file:/", filepath.ToSlash(baseDir), "template.yaml"}, "/") 87 th.CheckEquals(t, expected, result) 88 89 rawPath = "http://www.google.com" 90 basePath, _ = filepath.Abs(".") 91 result, _ = golangsdk.NormalizePathURL(basePath, rawPath) 92 expected = "http://www.google.com" 93 th.CheckEquals(t, expected, result) 94 95 rawPath = "very/nested/file.yaml" 96 basePath, _ = filepath.Abs(".") 97 result, _ = golangsdk.NormalizePathURL(basePath, rawPath) 98 expected = strings.Join([]string{"file:/", filepath.ToSlash(baseDir), "very/nested/file.yaml"}, "/") 99 th.CheckEquals(t, expected, result) 100 101 rawPath = "very/nested/file.yaml" 102 basePath = "http://www.google.com" 103 result, _ = golangsdk.NormalizePathURL(basePath, rawPath) 104 expected = "http://www.google.com/very/nested/file.yaml" 105 th.CheckEquals(t, expected, result) 106 107 rawPath = "very/nested/file.yaml/" 108 basePath = "http://www.google.com/" 109 result, _ = golangsdk.NormalizePathURL(basePath, rawPath) 110 expected = "http://www.google.com/very/nested/file.yaml" 111 th.CheckEquals(t, expected, result) 112 113 rawPath = "very/nested/file.yaml" 114 basePath = "http://www.google.com/even/more" 115 result, _ = golangsdk.NormalizePathURL(basePath, rawPath) 116 expected = "http://www.google.com/even/more/very/nested/file.yaml" 117 th.CheckEquals(t, expected, result) 118 119 rawPath = "very/nested/file.yaml" 120 basePath = strings.Join([]string{"file:/", filepath.ToSlash(baseDir), "only/file/even/more"}, "/") 121 result, _ = golangsdk.NormalizePathURL(basePath, rawPath) 122 expected = strings.Join([]string{"file:/", filepath.ToSlash(baseDir), "only/file/even/more/very/nested/file.yaml"}, "/") 123 th.CheckEquals(t, expected, result) 124 125 rawPath = "very/nested/file.yaml/" 126 basePath = strings.Join([]string{"file:/", filepath.ToSlash(baseDir), "only/file/even/more"}, "/") 127 result, _ = golangsdk.NormalizePathURL(basePath, rawPath) 128 expected = strings.Join([]string{"file:/", filepath.ToSlash(baseDir), "only/file/even/more/very/nested/file.yaml"}, "/") 129 th.CheckEquals(t, expected, result) 130 131 }