github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/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 "github.com/huaweicloud/golangsdk" 12 th "github.com/huaweicloud/golangsdk/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 th.AssertEquals(t, "A timeout occurred", err.Error()) 31 } 32 33 func TestWaitForError(t *testing.T) { 34 if testing.Short() { 35 t.Skip("skipping test in short mode.") 36 } 37 38 err := golangsdk.WaitFor(2, func() (bool, error) { 39 return false, errors.New("Error has occurred") 40 }) 41 th.AssertEquals(t, "Error has occurred", err.Error()) 42 } 43 44 func TestWaitForPredicateExceed(t *testing.T) { 45 if testing.Short() { 46 t.Skip("skipping test in short mode.") 47 } 48 49 err := golangsdk.WaitFor(1, func() (bool, error) { 50 time.Sleep(4 * time.Second) 51 return false, errors.New("Just wasting time") 52 }) 53 th.AssertEquals(t, "A timeout occurred", err.Error()) 54 } 55 56 func TestNormalizeURL(t *testing.T) { 57 urls := []string{ 58 "NoSlashAtEnd", 59 "SlashAtEnd/", 60 } 61 expected := []string{ 62 "NoSlashAtEnd/", 63 "SlashAtEnd/", 64 } 65 for i := 0; i < len(expected); i++ { 66 th.CheckEquals(t, expected[i], golangsdk.NormalizeURL(urls[i])) 67 } 68 69 } 70 71 func TestNormalizePathURL(t *testing.T) { 72 baseDir, _ := os.Getwd() 73 74 rawPath := "template.yaml" 75 basePath, _ := filepath.Abs(".") 76 result, _ := golangsdk.NormalizePathURL(basePath, rawPath) 77 expected := strings.Join([]string{"file:/", filepath.ToSlash(baseDir), "template.yaml"}, "/") 78 th.CheckEquals(t, expected, result) 79 80 rawPath = "http://www.google.com" 81 basePath, _ = filepath.Abs(".") 82 result, _ = golangsdk.NormalizePathURL(basePath, rawPath) 83 expected = "http://www.google.com" 84 th.CheckEquals(t, expected, result) 85 86 rawPath = "very/nested/file.yaml" 87 basePath, _ = filepath.Abs(".") 88 result, _ = golangsdk.NormalizePathURL(basePath, rawPath) 89 expected = strings.Join([]string{"file:/", filepath.ToSlash(baseDir), "very/nested/file.yaml"}, "/") 90 th.CheckEquals(t, expected, result) 91 92 rawPath = "very/nested/file.yaml" 93 basePath = "http://www.google.com" 94 result, _ = golangsdk.NormalizePathURL(basePath, rawPath) 95 expected = "http://www.google.com/very/nested/file.yaml" 96 th.CheckEquals(t, expected, result) 97 98 rawPath = "very/nested/file.yaml/" 99 basePath = "http://www.google.com/" 100 result, _ = golangsdk.NormalizePathURL(basePath, rawPath) 101 expected = "http://www.google.com/very/nested/file.yaml" 102 th.CheckEquals(t, expected, result) 103 104 rawPath = "very/nested/file.yaml" 105 basePath = "http://www.google.com/even/more" 106 result, _ = golangsdk.NormalizePathURL(basePath, rawPath) 107 expected = "http://www.google.com/even/more/very/nested/file.yaml" 108 th.CheckEquals(t, expected, result) 109 110 rawPath = "very/nested/file.yaml" 111 basePath = strings.Join([]string{"file:/", filepath.ToSlash(baseDir), "only/file/even/more"}, "/") 112 result, _ = golangsdk.NormalizePathURL(basePath, rawPath) 113 expected = strings.Join([]string{"file:/", filepath.ToSlash(baseDir), "only/file/even/more/very/nested/file.yaml"}, "/") 114 th.CheckEquals(t, expected, result) 115 116 rawPath = "very/nested/file.yaml/" 117 basePath = strings.Join([]string{"file:/", filepath.ToSlash(baseDir), "only/file/even/more"}, "/") 118 result, _ = golangsdk.NormalizePathURL(basePath, rawPath) 119 expected = strings.Join([]string{"file:/", filepath.ToSlash(baseDir), "only/file/even/more/very/nested/file.yaml"}, "/") 120 th.CheckEquals(t, expected, result) 121 122 }