forked from kubernetes/kubernetes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config_test.go
150 lines (117 loc) · 4.17 KB
/
config_test.go
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
/*
Copyright 2014 Google Inc. 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 config
import (
"fmt"
"net/http"
"net/http/httptest"
"net/url"
"strings"
"testing"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/latest"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/meta"
"github.com/GoogleCloudPlatform/kubernetes/pkg/client"
"github.com/GoogleCloudPlatform/kubernetes/pkg/runtime"
)
func getTyperAndMapper() (runtime.ObjectTyper, meta.RESTMapper) {
return api.Scheme, latest.RESTMapper
}
func getFakeClient(t *testing.T, validURLs []string) (ClientPosterFunc, *httptest.Server) {
handlerFunc := func(w http.ResponseWriter, r *http.Request) {
for _, u := range validURLs {
if u == r.RequestURI {
return
}
}
t.Errorf("Unexpected HTTP request: %s, expected %v", r.RequestURI, validURLs)
}
server := httptest.NewServer(http.HandlerFunc(handlerFunc))
return func(mapping *meta.RESTMapping) (RESTClientPoster, error) {
fakeCodec := runtime.CodecFor(api.Scheme, "v1beta1")
fakeUri, _ := url.Parse(server.URL + "/api/v1beta1")
return client.NewRESTClient(fakeUri, "v1beta1", fakeCodec, true), nil
}, server
}
func TestCreateObjects(t *testing.T) {
items := []runtime.Object{}
items = append(items, &api.Pod{
ObjectMeta: api.ObjectMeta{Name: "test-pod", Namespace: "default"},
})
items = append(items, &api.Service{
ObjectMeta: api.ObjectMeta{Name: "test-service", Namespace: "default"},
})
typer, mapper := getTyperAndMapper()
client, s := getFakeClient(t, []string{"/api/v1beta1/pods?namespace=default", "/api/v1beta1/services?namespace=default"})
errs := CreateObjects(typer, mapper, client, items)
s.Close()
if len(errs) != 0 {
t.Errorf("Unexpected errors during config.Create(): %v", errs)
}
}
func TestCreateNoNameItem(t *testing.T) {
items := []runtime.Object{}
items = append(items, &api.Service{
TypeMeta: api.TypeMeta{APIVersion: "v1beta1", Kind: "Service"},
})
typer, mapper := getTyperAndMapper()
client, s := getFakeClient(t, []string{"/api/v1beta1/services"})
errs := CreateObjects(typer, mapper, client, items)
s.Close()
if len(errs) == 0 {
t.Errorf("Expected required value error for missing name")
}
errStr := errs[0].Error()
if !strings.Contains(errStr, "Config.item[0]: name") {
t.Errorf("Expected 'Config.item[0]: name' in error string, got '%s'", errStr)
}
}
type InvalidItem struct{}
func (*InvalidItem) IsAnAPIObject() {}
func TestCreateInvalidItem(t *testing.T) {
items := []runtime.Object{
&InvalidItem{},
}
typer, mapper := getTyperAndMapper()
client, s := getFakeClient(t, []string{})
errs := CreateObjects(typer, mapper, client, items)
s.Close()
if len(errs) == 0 {
t.Errorf("Expected invalid value error for kind")
}
errStr := errs[0].Error()
if !strings.Contains(errStr, "Config.item[0] kind") {
t.Errorf("Expected 'Config.item[0] kind' in error string, got '%s'", errStr)
}
}
func TestCreateNoClientItems(t *testing.T) {
items := []runtime.Object{}
items = append(items, &api.Pod{
TypeMeta: api.TypeMeta{APIVersion: "v1beta1", Kind: "Pod"},
ObjectMeta: api.ObjectMeta{Name: "test-pod"},
})
typer, mapper := getTyperAndMapper()
_, s := getFakeClient(t, []string{"/api/v1beta1/pods", "/api/v1beta1/services"})
noClientFunc := func(mapping *meta.RESTMapping) (RESTClientPoster, error) {
return nil, fmt.Errorf("no client")
}
errs := CreateObjects(typer, mapper, noClientFunc, items)
s.Close()
if len(errs) == 0 {
t.Errorf("Expected invalid value error for client")
}
errStr := errs[0].Error()
if !strings.Contains(errStr, "Config.item[0] client") {
t.Errorf("Expected 'Config.item[0] client' in error string, got '%s'", errStr)
}
}