Skip to content

Commit 4bb0f15

Browse files
author
柳丰
committed
feat: add test
Signed-off-by: imneov <[email protected]>
1 parent 0a2e985 commit 4bb0f15

File tree

1 file changed

+242
-0
lines changed

1 file changed

+242
-0
lines changed

pkg/kubernetes/invoke_test.go

Lines changed: 242 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,242 @@
1+
/*
2+
Copyright 2021 The Dapr Authors
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
http://www.apache.org/licenses/LICENSE-2.0
7+
Unless required by applicable law or agreed to in writing, software
8+
distributed under the License is distributed on an "AS IS" BASIS,
9+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
See the License for the specific language governing permissions and
11+
limitations under the License.
12+
*/
13+
14+
package kubernetes
15+
16+
import (
17+
"net/http/httptest"
18+
"testing"
19+
"time"
20+
21+
"github.com/stretchr/testify/assert"
22+
"k8s.io/client-go/kubernetes/fake"
23+
"k8s.io/client-go/kubernetes/scheme"
24+
"k8s.io/client-go/rest"
25+
26+
v1 "k8s.io/api/core/v1"
27+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
28+
utiltesting "k8s.io/client-go/util/testing"
29+
)
30+
31+
func newDaprAppPod(name string, namespace string, appName string, creationTime time.Time, httpPort string, grpcPort string) *v1.Pod {
32+
return &v1.Pod{
33+
ObjectMeta: metav1.ObjectMeta{
34+
Name: name,
35+
Namespace: namespace,
36+
Annotations: map[string]string{},
37+
Labels: map[string]string{
38+
"app": appName,
39+
},
40+
CreationTimestamp: metav1.Time{
41+
Time: creationTime,
42+
},
43+
},
44+
Spec: v1.PodSpec{
45+
Containers: []v1.Container{
46+
{},
47+
{
48+
Name: "daprd",
49+
Args: []string{
50+
"--mode",
51+
"kubernetes",
52+
"--dapr-http-port",
53+
httpPort,
54+
"--dapr-grpc-port",
55+
grpcPort,
56+
"--dapr-internal-grpc-port",
57+
"50002",
58+
"--dapr-listen-addresses",
59+
"[::1],127.0.0.1",
60+
"--dapr-public-port",
61+
"3501",
62+
"--app-port",
63+
"8080",
64+
"--app-id",
65+
"testAppID",
66+
"--control-plane-address",
67+
"dapr-api.keel-system.svc.cluster.local:80",
68+
"--app-protocol",
69+
"http",
70+
"--placement-host-address",
71+
"dapr-placement-server.keel-system.svc.cluster.local:50005",
72+
"--config",
73+
"testAppID-Config",
74+
"--log-level",
75+
"info",
76+
"--app-max-concurrency",
77+
"-1",
78+
"--sentry-address",
79+
"dapr-sentry.keel-system.svc.cluster.local:80",
80+
"--enable-metrics=true",
81+
"--metrics-port",
82+
"9090",
83+
"--dapr-http-max-request-size",
84+
"-1",
85+
"--enable-mtls",
86+
},
87+
},
88+
},
89+
},
90+
}
91+
}
92+
93+
func Test_getAppInfo(t *testing.T) {
94+
client := fake.NewSimpleClientset(newDaprAppPod(
95+
"testAppPod", "testAppNameSpace",
96+
"testAppID", time.Now(),
97+
"80801", "80802"))
98+
99+
testCases := []struct {
100+
name string
101+
errorExpected bool
102+
errString string
103+
appID string
104+
want *AppInfo
105+
}{
106+
{
107+
name: "get test Pod",
108+
appID: "testAppID",
109+
errorExpected: false,
110+
errString: "",
111+
want: &AppInfo{
112+
AppID: "testAppID", AppPort: "80801", PodName: "testAppPod", Namespace: "testAppNameSpace",
113+
},
114+
},
115+
{
116+
name: "get error Pod",
117+
appID: "errorAppID",
118+
errorExpected: true,
119+
errString: "errorAppID not found",
120+
want: nil,
121+
},
122+
}
123+
for _, tc := range testCases {
124+
t.Run(tc.name, func(t *testing.T) {
125+
appInfo, err := GetAppInfo(client, tc.appID)
126+
if tc.errorExpected {
127+
assert.Error(t, err, "expected an error")
128+
assert.Equal(t, tc.errString, err.Error(), "expected error strings to match")
129+
} else {
130+
assert.NoError(t, err, "expected no error")
131+
assert.Equal(t, tc.want, appInfo, "expected appInfo to match")
132+
}
133+
})
134+
}
135+
}
136+
137+
func Test_invoke(t *testing.T) {
138+
app := &AppInfo{
139+
AppID: "testAppID", AppPort: "8080", HTTPPort: "3500", GRPCPort: "50001", PodName: "testAppPod", Namespace: "testAppNameSpace",
140+
}
141+
142+
testCases := []struct {
143+
name string
144+
errorExpected bool
145+
errString string
146+
appID string
147+
method string
148+
verb string
149+
data []byte
150+
URLExpected string
151+
}{
152+
{
153+
name: "get request",
154+
errorExpected: false,
155+
errString: "",
156+
method: "hello",
157+
verb: "GET",
158+
data: nil,
159+
URLExpected: "https://localhost/api/v1/" +
160+
"namespaces/testAppNameSpace/pods/testAppPod:8080/proxy/" +
161+
"hello",
162+
},
163+
{
164+
name: "get request",
165+
errorExpected: false,
166+
errString: "",
167+
method: "hello?abc=123&cdr=345#abb=aaa",
168+
verb: "GET",
169+
data: nil,
170+
URLExpected: "https://localhost/api/v1/" +
171+
"namespaces/testAppNameSpace/pods/testAppPod:8080/proxy/" +
172+
"hello?abc=123&cdr=345#abb=aaa",
173+
},
174+
{
175+
name: "post request",
176+
errorExpected: false,
177+
errString: "",
178+
method: "hello?abc=123&cdr=345#abb=aaa",
179+
verb: "POST",
180+
data: []byte("hello"),
181+
URLExpected: "https://localhost/api/v1/" +
182+
"namespaces/testAppNameSpace/pods/testAppPod:8080/proxy/" +
183+
"hello?abc=123&cdr=345#abb=aaa",
184+
},
185+
{
186+
name: "post request",
187+
errorExpected: false,
188+
errString: "errorAppID not found",
189+
method: "hello",
190+
verb: "POST",
191+
data: []byte("hello"),
192+
URLExpected: "https://localhost/api/v1/" +
193+
"namespaces/testAppNameSpace/pods/testAppPod:8080/proxy/" +
194+
"hello",
195+
},
196+
}
197+
for _, tc := range testCases {
198+
t.Run(tc.name, func(t *testing.T) {
199+
testServer, fakeHandler := testServerEnv(t, 200)
200+
defer testServer.Close()
201+
client, err := restClient(testServer)
202+
if err != nil {
203+
t.Fatalf("unexpected error: %v", err)
204+
}
205+
206+
_, err = invoke(client, app, tc.method, tc.data, tc.verb)
207+
if tc.errorExpected {
208+
assert.Error(t, err, "expected an error")
209+
assert.Equal(t, tc.errString, err.Error(), "expected error strings to match")
210+
} else {
211+
assert.NoError(t, err, "expected no error")
212+
data := string(tc.data)
213+
fakeHandler.ValidateRequest(t, tc.URLExpected, tc.verb, &data)
214+
}
215+
})
216+
}
217+
}
218+
219+
func testServerEnv(t *testing.T, statusCode int) (*httptest.Server, *utiltesting.FakeHandler) {
220+
t.Helper()
221+
fakeHandler := utiltesting.FakeHandler{
222+
StatusCode: statusCode,
223+
ResponseBody: "",
224+
T: t,
225+
}
226+
testServer := httptest.NewServer(&fakeHandler)
227+
return testServer, &fakeHandler
228+
}
229+
230+
func restClient(testServer *httptest.Server) (*rest.RESTClient, error) {
231+
c, err := rest.RESTClientFor(&rest.Config{
232+
Host: testServer.URL,
233+
ContentConfig: rest.ContentConfig{
234+
GroupVersion: &v1.SchemeGroupVersion,
235+
NegotiatedSerializer: scheme.Codecs.WithoutConversion(),
236+
},
237+
APIPath: "api",
238+
Username: "user",
239+
Password: "pass",
240+
})
241+
return c, err
242+
}

0 commit comments

Comments
 (0)