blob: dbe804298cf26b1b18ff2ac97968f3209e510ed9 [file] [log] [blame]
jrperritt3d966162016-06-06 14:08:54 -05001package testing
Jon Perritt59c68fc2014-10-06 17:32:15 -05002
3import (
Joe Topjian48584fb2017-02-13 21:18:26 -07004 "errors"
Pratik Mallya5fddb2a2015-09-14 14:04:49 -05005 "os"
6 "path/filepath"
7 "strings"
Jon Perritt59c68fc2014-10-06 17:32:15 -05008 "testing"
Joe Topjian48584fb2017-02-13 21:18:26 -07009 "time"
Jon Perritt59c68fc2014-10-06 17:32:15 -050010
Krzysztof Szukiełojć3f41d082017-05-07 14:43:06 +020011 "gerrit.mcp.mirantis.net/debian/gophercloud.git"
Krzysztof Szukiełojć24a29ce2017-05-07 14:24:02 +020012 th "gerrit.mcp.mirantis.net/debian/gophercloud.git/testhelper"
Jon Perritt59c68fc2014-10-06 17:32:15 -050013)
14
15func TestWaitFor(t *testing.T) {
Joe Topjian48584fb2017-02-13 21:18:26 -070016 err := gophercloud.WaitFor(2, func() (bool, error) {
Jon Perritt59c68fc2014-10-06 17:32:15 -050017 return true, nil
18 })
19 th.CheckNoErr(t, err)
20}
Pratik Mallya5fddb2a2015-09-14 14:04:49 -050021
Joe Topjian48584fb2017-02-13 21:18:26 -070022func 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
33func 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
44func 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 Mallya5fddb2a2015-09-14 14:04:49 -050056func 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++ {
jrperritt3d966162016-06-06 14:08:54 -050066 th.CheckEquals(t, expected[i], gophercloud.NormalizeURL(urls[i]))
Pratik Mallya5fddb2a2015-09-14 14:04:49 -050067 }
68
69}
70
71func TestNormalizePathURL(t *testing.T) {
72 baseDir, _ := os.Getwd()
73
74 rawPath := "template.yaml"
75 basePath, _ := filepath.Abs(".")
jrperritt3d966162016-06-06 14:08:54 -050076 result, _ := gophercloud.NormalizePathURL(basePath, rawPath)
Pratik Mallya5fddb2a2015-09-14 14:04:49 -050077 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(".")
jrperritt3d966162016-06-06 14:08:54 -050082 result, _ = gophercloud.NormalizePathURL(basePath, rawPath)
Pratik Mallya5fddb2a2015-09-14 14:04:49 -050083 expected = "http://www.google.com"
84 th.CheckEquals(t, expected, result)
85
86 rawPath = "very/nested/file.yaml"
87 basePath, _ = filepath.Abs(".")
jrperritt3d966162016-06-06 14:08:54 -050088 result, _ = gophercloud.NormalizePathURL(basePath, rawPath)
Pratik Mallya5fddb2a2015-09-14 14:04:49 -050089 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"
jrperritt3d966162016-06-06 14:08:54 -050094 result, _ = gophercloud.NormalizePathURL(basePath, rawPath)
Pratik Mallya5fddb2a2015-09-14 14:04:49 -050095 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/"
jrperritt3d966162016-06-06 14:08:54 -0500100 result, _ = gophercloud.NormalizePathURL(basePath, rawPath)
Pratik Mallya5fddb2a2015-09-14 14:04:49 -0500101 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"
jrperritt3d966162016-06-06 14:08:54 -0500106 result, _ = gophercloud.NormalizePathURL(basePath, rawPath)
Pratik Mallya5fddb2a2015-09-14 14:04:49 -0500107 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"}, "/")
jrperritt3d966162016-06-06 14:08:54 -0500112 result, _ = gophercloud.NormalizePathURL(basePath, rawPath)
Pratik Mallya5fddb2a2015-09-14 14:04:49 -0500113 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"}, "/")
jrperritt3d966162016-06-06 14:08:54 -0500118 result, _ = gophercloud.NormalizePathURL(basePath, rawPath)
Pratik Mallya5fddb2a2015-09-14 14:04:49 -0500119 expected = strings.Join([]string{"file:/", filepath.ToSlash(baseDir), "only/file/even/more/very/nested/file.yaml"}, "/")
120 th.CheckEquals(t, expected, result)
121
122}