forked from gophercloud/gophercloud
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil_test.go
More file actions
175 lines (145 loc) · 4.88 KB
/
util_test.go
File metadata and controls
175 lines (145 loc) · 4.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package testing
import (
"context"
"errors"
"os"
"path/filepath"
"reflect"
"strings"
"testing"
"time"
"github.com/gophercloud/gophercloud/v2"
th "github.com/gophercloud/gophercloud/v2/testhelper"
)
func TestWaitFor(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
defer cancel()
err := gophercloud.WaitFor(ctx, func(context.Context) (bool, error) {
return true, nil
})
th.CheckNoErr(t, err)
}
func TestWaitForTimeout(t *testing.T) {
if testing.Short() {
t.Skip("skipping test in short mode.")
}
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
defer cancel()
err := gophercloud.WaitFor(ctx, func(context.Context) (bool, error) {
return false, nil
})
th.AssertErrIs(t, err, context.DeadlineExceeded)
}
func TestWaitForError(t *testing.T) {
if testing.Short() {
t.Skip("skipping test in short mode.")
}
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
defer cancel()
err := gophercloud.WaitFor(ctx, func(context.Context) (bool, error) {
return false, errors.New("Error has occurred")
})
th.AssertEquals(t, "Error has occurred", err.Error())
}
func TestWaitForPredicateExceed(t *testing.T) {
if testing.Short() {
t.Skip("skipping test in short mode.")
}
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
defer cancel()
err := gophercloud.WaitFor(ctx, func(ctx context.Context) (bool, error) {
// NOTE: predicate should obey context cancellation
select {
case <-ctx.Done():
return true, ctx.Err()
case <-time.After(4 * time.Second):
return false, errors.New("Just wasting time")
}
})
th.AssertErrIs(t, err, context.DeadlineExceeded)
}
func TestNormalizeURL(t *testing.T) {
urls := []string{
"NoSlashAtEnd",
"SlashAtEnd/",
}
expected := []string{
"NoSlashAtEnd/",
"SlashAtEnd/",
}
for i := 0; i < len(expected); i++ {
th.CheckEquals(t, expected[i], gophercloud.NormalizeURL(urls[i]))
}
}
func TestNormalizePathURL(t *testing.T) {
baseDir, _ := os.Getwd()
rawPath := "template.yaml"
basePath, _ := filepath.Abs(".")
result, _ := gophercloud.NormalizePathURL(basePath, rawPath)
expected := strings.Join([]string{"file:/", filepath.ToSlash(baseDir), "template.yaml"}, "/")
th.CheckEquals(t, expected, result)
rawPath = "http://www.google.com"
basePath, _ = filepath.Abs(".")
result, _ = gophercloud.NormalizePathURL(basePath, rawPath)
expected = "http://www.google.com"
th.CheckEquals(t, expected, result)
rawPath = "very/nested/file.yaml"
basePath, _ = filepath.Abs(".")
result, _ = gophercloud.NormalizePathURL(basePath, rawPath)
expected = strings.Join([]string{"file:/", filepath.ToSlash(baseDir), "very/nested/file.yaml"}, "/")
th.CheckEquals(t, expected, result)
rawPath = "very/nested/file.yaml"
basePath = "http://www.google.com"
result, _ = gophercloud.NormalizePathURL(basePath, rawPath)
expected = "http://www.google.com/very/nested/file.yaml"
th.CheckEquals(t, expected, result)
rawPath = "very/nested/file.yaml/"
basePath = "http://www.google.com/"
result, _ = gophercloud.NormalizePathURL(basePath, rawPath)
expected = "http://www.google.com/very/nested/file.yaml"
th.CheckEquals(t, expected, result)
rawPath = "very/nested/file.yaml"
basePath = "http://www.google.com/even/more"
result, _ = gophercloud.NormalizePathURL(basePath, rawPath)
expected = "http://www.google.com/even/more/very/nested/file.yaml"
th.CheckEquals(t, expected, result)
rawPath = "very/nested/file.yaml"
basePath = strings.Join([]string{"file:/", filepath.ToSlash(baseDir), "only/file/even/more"}, "/")
result, _ = gophercloud.NormalizePathURL(basePath, rawPath)
expected = strings.Join([]string{"file:/", filepath.ToSlash(baseDir), "only/file/even/more/very/nested/file.yaml"}, "/")
th.CheckEquals(t, expected, result)
rawPath = "very/nested/file.yaml/"
basePath = strings.Join([]string{"file:/", filepath.ToSlash(baseDir), "only/file/even/more"}, "/")
result, _ = gophercloud.NormalizePathURL(basePath, rawPath)
expected = strings.Join([]string{"file:/", filepath.ToSlash(baseDir), "only/file/even/more/very/nested/file.yaml"}, "/")
th.CheckEquals(t, expected, result)
}
func TestRemainingKeys(t *testing.T) {
type User struct {
UserID string `json:"user_id"`
Username string `json:"username"`
Location string `json:"-"`
CreatedAt string `json:"-"`
Status string
IsAdmin bool
}
userResponse := map[string]any{
"user_id": "abcd1234",
"username": "jdoe",
"location": "Hawaii",
"created_at": "2017-06-08T02:49:03.000000",
"status": "active",
"is_admin": "true",
"custom_field": "foo",
}
expected := map[string]any{
"created_at": "2017-06-08T02:49:03.000000",
"is_admin": "true",
"custom_field": "foo",
}
actual := gophercloud.RemainingKeys(User{}, userResponse)
isEqual := reflect.DeepEqual(expected, actual)
if !isEqual {
t.Fatalf("expected %s but got %s", expected, actual)
}
}