Skip to content

Commit f32eec2

Browse files
crmejiaVille Aikas
authored andcommitted
unit test for ./pkg/rest/core/fake rest client, addresses #860 Test ready to be reviewed (#1113)
* test ready to be reviewed * removed commented out loc
1 parent e388aee commit f32eec2

File tree

1 file changed

+287
-0
lines changed

1 file changed

+287
-0
lines changed
Lines changed: 287 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,287 @@
1+
/*
2+
Copyright 2017 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package fake
18+
19+
import (
20+
"bytes"
21+
"fmt"
22+
"io/ioutil"
23+
"net/http"
24+
"testing"
25+
26+
"github.com/kubernetes-incubator/service-catalog/pkg/apis/servicecatalog"
27+
_ "github.com/kubernetes-incubator/service-catalog/pkg/apis/servicecatalog/install"
28+
"github.com/kubernetes-incubator/service-catalog/pkg/apis/servicecatalog/testapi"
29+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
30+
"k8s.io/apimachinery/pkg/runtime"
31+
)
32+
33+
const (
34+
ns1 = "ns1"
35+
ns2 = "ns2"
36+
tipe1 = "tipe1"
37+
name1 = "name1"
38+
name2 = "name2"
39+
name3 = "name3"
40+
)
41+
42+
//Helpers
43+
func createSingleItemStorage() NamespacedStorage {
44+
storage := make(NamespacedStorage)
45+
storage.Set(ns1, tipe1, name1, &servicecatalog.Broker{})
46+
return storage
47+
}
48+
49+
func createMultipleItemStorage() NamespacedStorage {
50+
storage := make(NamespacedStorage)
51+
storage.Set(ns1, tipe1, name1, &servicecatalog.Broker{})
52+
storage.Set(ns1, tipe1, name2, &servicecatalog.Broker{})
53+
storage.Set(ns1, tipe1, name3, &servicecatalog.Broker{})
54+
55+
storage.Set(ns2, tipe1, name1, &servicecatalog.Broker{})
56+
storage.Set(ns2, tipe1, name2, &servicecatalog.Broker{})
57+
58+
return storage
59+
60+
}
61+
62+
func TestNamespacedStorageSetDelete(t *testing.T) {
63+
storage := make(NamespacedStorage)
64+
65+
if nil != storage.Get(ns1, tipe1, name1) {
66+
t.Fatal("Expected no results from an empty storage")
67+
}
68+
69+
storage = createSingleItemStorage()
70+
71+
if storage.Get(ns1, tipe1, name1) == nil {
72+
t.Fatal("Expected a object to be stored")
73+
}
74+
75+
storage.Delete(ns1, tipe1, name1)
76+
if nil != storage.Get(ns1, tipe1, name1) {
77+
t.Fatal("Expected the object to be deleted")
78+
}
79+
}
80+
81+
func TestNamespacedStorageGetList(t *testing.T) {
82+
storage := createMultipleItemStorage()
83+
84+
objects := storage.GetList(ns1, tipe1)
85+
if count := len(objects); count != 3 {
86+
t.Fatal("Expected ", 3, "got", count)
87+
}
88+
89+
objects = storage.GetList(ns2, tipe1)
90+
if count := len(objects); count != 2 {
91+
t.Fatal("Expected ", 2, "got", count)
92+
}
93+
}
94+
95+
func TestResponseWriter(t *testing.T) {
96+
rw := newResponseWriter()
97+
length, err := rw.Write([]byte{0, 0, 0})
98+
if err != nil {
99+
t.Fatal("Error writing response", err)
100+
}
101+
102+
if length != 3 {
103+
t.Fatal("Expected length", 3, "got", length)
104+
}
105+
}
106+
107+
func TestResponseWriterGetResponsePanic(t *testing.T) {
108+
rw := newResponseWriter()
109+
// no hearder status set should panic
110+
defer func() {
111+
if r := recover(); r == nil {
112+
t.Fatal("Expected a panic")
113+
}
114+
}()
115+
rw.getResponse()
116+
}
117+
118+
func TestResponseWriterGetResponse(t *testing.T) {
119+
rw := newResponseWriter()
120+
rw.WriteHeader(http.StatusFound)
121+
122+
if response := rw.getResponse(); response.StatusCode != http.StatusFound {
123+
t.Fatal("Expected", 0, "got", response.StatusCode)
124+
}
125+
}
126+
127+
func TestGetItem(t *testing.T) {
128+
testCases := []struct {
129+
name string
130+
storage NamespacedStorage
131+
watcher *Watcher
132+
rw *responseWriter
133+
url string
134+
expectedStatus int
135+
}{
136+
{"Empty Storage", make(NamespacedStorage), NewWatcher(), newResponseWriter(), fmt.Sprintf("/apis/servicecatalog.k8s.io/v1alpha1/namespaces/%s/%s/%s", ns1, tipe1, name1), http.StatusNotFound},
137+
{"One Item in storage", createSingleItemStorage(), NewWatcher(), newResponseWriter(), fmt.Sprintf("/apis/servicecatalog.k8s.io/v1alpha1/namespaces/%s/%s/%s", ns1, tipe1, name1), http.StatusOK},
138+
}
139+
140+
for _, tc := range testCases {
141+
t.Run(tc.name, func(t *testing.T) {
142+
request, err := http.NewRequest("GET", tc.url, nil)
143+
if err != nil {
144+
t.Fatal(err)
145+
}
146+
147+
router := getRouter(tc.storage, tc.watcher, func() runtime.Object {
148+
return &servicecatalog.Instance{}
149+
})
150+
151+
router.ServeHTTP(tc.rw, request)
152+
153+
body, err := ioutil.ReadAll(tc.rw.getResponse().Body)
154+
if err != nil {
155+
t.Error("Could not read response.", err)
156+
}
157+
158+
if tc.rw.getResponse().StatusCode != tc.expectedStatus && tc.rw.headerSet {
159+
t.Error("Expected Status", tc.expectedStatus, "got", tc.rw.getResponse().StatusCode)
160+
t.Error("Http error:", string(body))
161+
}
162+
if err != nil {
163+
t.Fatal(err)
164+
}
165+
})
166+
}
167+
}
168+
169+
func TestGetItems(t *testing.T) {
170+
testCases := []struct {
171+
name string
172+
storage NamespacedStorage
173+
watcher *Watcher
174+
rw *responseWriter
175+
url string
176+
expectedStatus int
177+
}{
178+
{"Empty Storage", make(NamespacedStorage), NewWatcher(), newResponseWriter(), fmt.Sprintf("/apis/servicecatalog.k8s.io/v1alpha1/namespaces/%v/brokers", ns1), http.StatusOK},
179+
{"Multiple Items", createMultipleItemStorage(), NewWatcher(), newResponseWriter(), fmt.Sprintf("/apis/servicecatalog.k8s.io/v1alpha1/namespaces/%v/brokers", ns1), http.StatusOK},
180+
}
181+
182+
for _, tc := range testCases {
183+
t.Run(tc.name, func(t *testing.T) {
184+
request, err := http.NewRequest("GET", tc.url, nil)
185+
if err != nil {
186+
t.Fatal(err)
187+
}
188+
189+
router := getRouter(tc.storage, tc.watcher, func() runtime.Object {
190+
return &servicecatalog.Instance{}
191+
})
192+
193+
router.ServeHTTP(tc.rw, request)
194+
195+
body, err := ioutil.ReadAll(tc.rw.getResponse().Body)
196+
if err != nil {
197+
t.Error("Could not read response.", err)
198+
}
199+
200+
if tc.rw.getResponse().StatusCode != tc.expectedStatus && tc.rw.headerSet {
201+
t.Error("Expected Status", tc.expectedStatus, "got", tc.rw.getResponse().StatusCode)
202+
t.Error("Http error:", string(body))
203+
}
204+
if err != nil {
205+
t.Fatal(err)
206+
}
207+
})
208+
}
209+
210+
}
211+
212+
func TestCreateItem(t *testing.T) {
213+
testCases := []struct {
214+
name string
215+
storage NamespacedStorage
216+
watcher *Watcher
217+
rw *responseWriter
218+
url string
219+
item runtime.Object
220+
expectedStatus int
221+
expectedStorageLength int
222+
}{
223+
{
224+
"Create Item (empty storage)",
225+
make(NamespacedStorage), NewWatcher(),
226+
newResponseWriter(),
227+
fmt.Sprintf("/apis/servicecatalog.k8s.io/v1alpha1/namespaces/%s/%s", ns1, tipe1),
228+
&servicecatalog.Broker{ObjectMeta: metav1.ObjectMeta{Name: name1}, TypeMeta: metav1.TypeMeta{Kind: "Broker", APIVersion: "servicecatalog.k8s.io/v1alpha1"}},
229+
http.StatusCreated,
230+
1,
231+
},
232+
{
233+
"Create misformed item(no Kind)",
234+
make(NamespacedStorage),
235+
NewWatcher(),
236+
newResponseWriter(),
237+
fmt.Sprintf("/apis/servicecatalog.k8s.io/v1alpha1/namespaces/%s/%s", ns1, tipe1),
238+
&servicecatalog.Broker{}, http.StatusInternalServerError,
239+
0,
240+
},
241+
{
242+
"Create Item(non-empty storage)",
243+
createMultipleItemStorage(),
244+
NewWatcher(),
245+
newResponseWriter(),
246+
fmt.Sprintf("/apis/servicecatalog.k8s.io/v1alpha1/namespaces/%s/%s", ns1, tipe1), &servicecatalog.Broker{TypeMeta: metav1.TypeMeta{Kind: "Broker", APIVersion: "servicecatalog.k8s.io/v1alpha1"}},
247+
http.StatusCreated,
248+
2,
249+
},
250+
}
251+
252+
for _, tc := range testCases {
253+
t.Run(tc.name, func(t *testing.T) {
254+
codec, err := testapi.GetCodecForObject(tc.item)
255+
if err != nil {
256+
t.Fatalf("error getting a codec for %#v (%s)", tc.item, err)
257+
}
258+
bodyBytes, err := runtime.Encode(codec, tc.item)
259+
if err != nil {
260+
t.Fatalf("error getting a encoding %#v (%s)", tc.item, err)
261+
}
262+
request, err := http.NewRequest("POST", tc.url, bytes.NewReader(bodyBytes))
263+
if err != nil {
264+
t.Fatal(err)
265+
}
266+
267+
router := getRouter(tc.storage, tc.watcher, func() runtime.Object {
268+
return tc.item
269+
})
270+
271+
router.ServeHTTP(tc.rw, request)
272+
273+
body, err := ioutil.ReadAll(tc.rw.getResponse().Body)
274+
if err != nil {
275+
t.Error("Could not read response.", err)
276+
}
277+
278+
if tc.rw.getResponse().StatusCode != tc.expectedStatus && tc.rw.headerSet {
279+
t.Error("Expected Status", tc.expectedStatus, "got", tc.rw.getResponse().StatusCode)
280+
t.Error("Http error:", string(body))
281+
}
282+
if len(tc.storage) != tc.expectedStorageLength {
283+
t.Error("Expected the length of the storage to be", tc.expectedStorageLength, "got", len(tc.storage))
284+
}
285+
})
286+
}
287+
}

0 commit comments

Comments
 (0)