Repository navigation
Expand file tree
/
Copy pathutils_test.go
More file actions
164 lines (141 loc) · 4.23 KB
/
Copy pathutils_test.go
File metadata and controls
164 lines (141 loc) · 4.23 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
/*
Copyright 2016 The Kubernetes Authors All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package util
import (
"io"
"net/http"
"net/http/httptest"
"testing"
"github.com/pkg/errors"
"k8s.io/minikube/pkg/minikube/constants"
)
// Returns a function that will return n errors, then return successfully forever.
func errorGenerator(n int, retryable bool) func() error {
errorCount := 0
return func() (err error) {
if errorCount < n {
errorCount += 1
e := errors.New("Error!")
if retryable {
return &RetriableError{Err: e}
} else {
return e
}
}
return nil
}
}
func TestErrorGenerator(t *testing.T) {
errors := 3
f := errorGenerator(errors, false)
for i := 0; i < errors-1; i++ {
if err := f(); err == nil {
t.Fatalf("Error should have been thrown at iteration %v", i)
}
}
if err := f(); err == nil {
t.Fatalf("Error should not have been thrown this call!")
}
}
func TestRetry(t *testing.T) {
f := errorGenerator(4, true)
if err := Retry(5, f); err != nil {
t.Fatalf("Error should not have been raised by retry.")
}
f = errorGenerator(5, true)
if err := Retry(4, f); err == nil {
t.Fatalf("Error should have been raised by retry.")
}
}
func TestRetryNotRetriableError(t *testing.T) {
f := errorGenerator(4, false)
if err := Retry(5, f); err == nil {
t.Fatalf("Error should have been raised by retry.")
}
f = errorGenerator(5, false)
if err := Retry(4, f); err == nil {
t.Fatalf("Error should have been raised by retry.")
}
f = errorGenerator(0, false)
if err := Retry(5, f); err != nil {
t.Fatalf("Error should not have been raised by retry.")
}
}
type getTestArgs struct {
input string
expected string
expectedError bool
}
func TestGetLocalkubeDownloadURL(t *testing.T) {
argsList := [...]getTestArgs{
{"v1.3.0",
"https://storage.googleapis.com/minikube/k8sReleases/v1.3.0/localkube-linux-amd64", false},
{"v1.3.3",
"https://storage.googleapis.com/minikube/k8sReleases/v1.3.3/localkube-linux-amd64", false},
{"http://www.example.com/my-localkube", "http://www.example.com/my-localkube", false},
{"abc", "", true},
{"1.2.3.4", "", true},
}
for _, args := range argsList {
url, err := GetLocalkubeDownloadURL(args.input, constants.LocalkubeLinuxFilename)
wasError := err != nil
if wasError != args.expectedError {
t.Errorf("GetLocalkubeDownloadURL Expected error was: %t, Actual Error was: %s",
args.expectedError, err)
}
if url != args.expected {
t.Errorf("GetLocalkubeDownloadURL: Expected %s, Actual: %s", args.expected, url)
}
}
}
var testSHAString = "test"
func TestParseSHAFromURL(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
io.WriteString(w, testSHAString)
}))
serverBadResponse := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte("500 HTTP status code returned!"))
}))
argsList := [...]getTestArgs{
{server.URL, testSHAString, false},
{serverBadResponse.URL, "", true},
{"abc", "", true},
}
for _, args := range argsList {
url, err := ParseSHAFromURL(args.input)
wasError := err != nil
if wasError != args.expectedError {
t.Errorf("ParseSHAFromURL Expected error was: %t, Actual Error was: %s",
args.expectedError, err)
}
if url != args.expected {
t.Errorf("ParseSHAFromURL: Expected %s, Actual: %s", args.expected, url)
}
}
}
func TestMultiError(t *testing.T) {
m := MultiError{}
m.Collect(errors.New("Error 1"))
m.Collect(errors.New("Error 2"))
err := m.ToError()
expected := `Error 1
Error 2`
if err.Error() != expected {
t.Fatalf("%s != %s", err, expected)
}
m = MultiError{}
if err := m.ToError(); err != nil {
t.Fatalf("Unexpected error: %s", err)
}
}