jrperritt | 3d96616 | 2016-06-06 14:08:54 -0500 | [diff] [blame] | 1 | package testing |
Jon Perritt | 59c68fc | 2014-10-06 17:32:15 -0500 | [diff] [blame] | 2 | |
| 3 | import ( |
Joe Topjian | 48584fb | 2017-02-13 21:18:26 -0700 | [diff] [blame] | 4 | "errors" |
Pratik Mallya | 5fddb2a | 2015-09-14 14:04:49 -0500 | [diff] [blame] | 5 | "os" |
| 6 | "path/filepath" |
| 7 | "strings" |
Jon Perritt | 59c68fc | 2014-10-06 17:32:15 -0500 | [diff] [blame] | 8 | "testing" |
Joe Topjian | 48584fb | 2017-02-13 21:18:26 -0700 | [diff] [blame] | 9 | "time" |
Jon Perritt | 59c68fc | 2014-10-06 17:32:15 -0500 | [diff] [blame] | 10 | |
jrperritt | 3d96616 | 2016-06-06 14:08:54 -0500 | [diff] [blame] | 11 | "github.com/gophercloud/gophercloud" |
Jon Perritt | 27249f4 | 2016-02-18 10:35:59 -0600 | [diff] [blame] | 12 | th "github.com/gophercloud/gophercloud/testhelper" |
Jon Perritt | 59c68fc | 2014-10-06 17:32:15 -0500 | [diff] [blame] | 13 | ) |
| 14 | |
| 15 | func TestWaitFor(t *testing.T) { |
Joe Topjian | 48584fb | 2017-02-13 21:18:26 -0700 | [diff] [blame] | 16 | err := gophercloud.WaitFor(2, func() (bool, error) { |
Jon Perritt | 59c68fc | 2014-10-06 17:32:15 -0500 | [diff] [blame] | 17 | return true, nil |
| 18 | }) |
| 19 | th.CheckNoErr(t, err) |
| 20 | } |
Pratik Mallya | 5fddb2a | 2015-09-14 14:04:49 -0500 | [diff] [blame] | 21 | |
Joe Topjian | 48584fb | 2017-02-13 21:18:26 -0700 | [diff] [blame] | 22 | func TestWaitForTimeout(t *testing.T) { |
| 23 | if testing.Short() { |
| 24 | t.Skip("skipping test in short mode.") |
| 25 | } |
| 26 | |
| 27 | err := gophercloud.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 := gophercloud.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 := gophercloud.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 | |
Pratik Mallya | 5fddb2a | 2015-09-14 14:04:49 -0500 | [diff] [blame] | 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++ { |
jrperritt | 3d96616 | 2016-06-06 14:08:54 -0500 | [diff] [blame] | 66 | th.CheckEquals(t, expected[i], gophercloud.NormalizeURL(urls[i])) |
Pratik Mallya | 5fddb2a | 2015-09-14 14:04:49 -0500 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | } |
| 70 | |
| 71 | func TestNormalizePathURL(t *testing.T) { |
| 72 | baseDir, _ := os.Getwd() |
| 73 | |
| 74 | rawPath := "template.yaml" |
| 75 | basePath, _ := filepath.Abs(".") |
jrperritt | 3d96616 | 2016-06-06 14:08:54 -0500 | [diff] [blame] | 76 | result, _ := gophercloud.NormalizePathURL(basePath, rawPath) |
Pratik Mallya | 5fddb2a | 2015-09-14 14:04:49 -0500 | [diff] [blame] | 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(".") |
jrperritt | 3d96616 | 2016-06-06 14:08:54 -0500 | [diff] [blame] | 82 | result, _ = gophercloud.NormalizePathURL(basePath, rawPath) |
Pratik Mallya | 5fddb2a | 2015-09-14 14:04:49 -0500 | [diff] [blame] | 83 | expected = "http://www.google.com" |
| 84 | th.CheckEquals(t, expected, result) |
| 85 | |
| 86 | rawPath = "very/nested/file.yaml" |
| 87 | basePath, _ = filepath.Abs(".") |
jrperritt | 3d96616 | 2016-06-06 14:08:54 -0500 | [diff] [blame] | 88 | result, _ = gophercloud.NormalizePathURL(basePath, rawPath) |
Pratik Mallya | 5fddb2a | 2015-09-14 14:04:49 -0500 | [diff] [blame] | 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" |
jrperritt | 3d96616 | 2016-06-06 14:08:54 -0500 | [diff] [blame] | 94 | result, _ = gophercloud.NormalizePathURL(basePath, rawPath) |
Pratik Mallya | 5fddb2a | 2015-09-14 14:04:49 -0500 | [diff] [blame] | 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/" |
jrperritt | 3d96616 | 2016-06-06 14:08:54 -0500 | [diff] [blame] | 100 | result, _ = gophercloud.NormalizePathURL(basePath, rawPath) |
Pratik Mallya | 5fddb2a | 2015-09-14 14:04:49 -0500 | [diff] [blame] | 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" |
jrperritt | 3d96616 | 2016-06-06 14:08:54 -0500 | [diff] [blame] | 106 | result, _ = gophercloud.NormalizePathURL(basePath, rawPath) |
Pratik Mallya | 5fddb2a | 2015-09-14 14:04:49 -0500 | [diff] [blame] | 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"}, "/") |
jrperritt | 3d96616 | 2016-06-06 14:08:54 -0500 | [diff] [blame] | 112 | result, _ = gophercloud.NormalizePathURL(basePath, rawPath) |
Pratik Mallya | 5fddb2a | 2015-09-14 14:04:49 -0500 | [diff] [blame] | 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"}, "/") |
jrperritt | 3d96616 | 2016-06-06 14:08:54 -0500 | [diff] [blame] | 118 | result, _ = gophercloud.NormalizePathURL(basePath, rawPath) |
Pratik Mallya | 5fddb2a | 2015-09-14 14:04:49 -0500 | [diff] [blame] | 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 | } |