Skip to content

Commit 16238e1

Browse files
committed
fix: make fake.Clientset support streaming logs
Signed-off-by: knight42 <[email protected]>
1 parent 7c3f706 commit 16238e1

File tree

3 files changed

+84
-2
lines changed

3 files changed

+84
-2
lines changed

staging/src/k8s.io/client-go/kubernetes/typed/core/v1/fake/BUILD

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package(default_visibility = ["//visibility:public"])
33
load(
44
"@io_bazel_rules_go//go:def.bzl",
55
"go_library",
6+
"go_test",
67
)
78

89
go_library(
@@ -46,8 +47,10 @@ go_library(
4647
"//staging/src/k8s.io/apimachinery/pkg/runtime/schema:go_default_library",
4748
"//staging/src/k8s.io/apimachinery/pkg/types:go_default_library",
4849
"//staging/src/k8s.io/apimachinery/pkg/watch:go_default_library",
50+
"//staging/src/k8s.io/client-go/kubernetes/scheme:go_default_library",
4951
"//staging/src/k8s.io/client-go/kubernetes/typed/core/v1:go_default_library",
5052
"//staging/src/k8s.io/client-go/rest:go_default_library",
53+
"//staging/src/k8s.io/client-go/rest/fake:go_default_library",
5154
"//staging/src/k8s.io/client-go/testing:go_default_library",
5255
],
5356
)
@@ -64,3 +67,13 @@ filegroup(
6467
srcs = [":package-srcs"],
6568
tags = ["automanaged"],
6669
)
70+
71+
go_test(
72+
name = "go_default_test",
73+
srcs = ["fake_pod_expansion_test.go"],
74+
embed = [":go_default_library"],
75+
deps = [
76+
"//staging/src/k8s.io/api/core/v1:go_default_library",
77+
"//staging/src/k8s.io/client-go/testing:go_default_library",
78+
],
79+
)

staging/src/k8s.io/client-go/kubernetes/typed/core/v1/fake/fake_pod_expansion.go

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,17 @@ package fake
1818

1919
import (
2020
"context"
21+
"fmt"
22+
"io/ioutil"
23+
"net/http"
24+
"strings"
2125

22-
"k8s.io/api/core/v1"
26+
v1 "k8s.io/api/core/v1"
2327
policy "k8s.io/api/policy/v1beta1"
2428
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
29+
"k8s.io/client-go/kubernetes/scheme"
2530
restclient "k8s.io/client-go/rest"
31+
fakerest "k8s.io/client-go/rest/fake"
2632
core "k8s.io/client-go/testing"
2733
)
2834

@@ -57,7 +63,19 @@ func (c *FakePods) GetLogs(name string, opts *v1.PodLogOptions) *restclient.Requ
5763
action.Value = opts
5864

5965
_, _ = c.Fake.Invokes(action, &v1.Pod{})
60-
return &restclient.Request{}
66+
fakeClient := &fakerest.RESTClient{
67+
Client: fakerest.CreateHTTPClient(func(request *http.Request) (*http.Response, error) {
68+
resp := &http.Response{
69+
StatusCode: http.StatusOK,
70+
Body: ioutil.NopCloser(strings.NewReader("fake logs")),
71+
}
72+
return resp, nil
73+
}),
74+
NegotiatedSerializer: scheme.Codecs.WithoutConversion(),
75+
GroupVersion: podsKind.GroupVersion(),
76+
VersionedAPIPath: fmt.Sprintf("/api/v1/namespaces/%s/pods/%s/log", c.ns, name),
77+
}
78+
return fakeClient.Request()
6179
}
6280

6381
func (c *FakePods) Evict(ctx context.Context, eviction *policy.Eviction) error {
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
Copyright 2020 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+
"context"
22+
"io"
23+
"testing"
24+
25+
corev1 "k8s.io/api/core/v1"
26+
cgtesting "k8s.io/client-go/testing"
27+
)
28+
29+
func TestFakePodsGetLogs(t *testing.T) {
30+
fp := FakePods{
31+
Fake: &FakeCoreV1{Fake: &cgtesting.Fake{}},
32+
ns: "default",
33+
}
34+
req := fp.GetLogs("foo", &corev1.PodLogOptions{})
35+
body, err := req.Stream(context.Background())
36+
if err != nil {
37+
t.Fatal("Stream pod logs:", err)
38+
}
39+
var buf bytes.Buffer
40+
n, err := io.Copy(&buf, body)
41+
if err != nil {
42+
t.Fatal("Read pod logs:", err)
43+
}
44+
if n == 0 {
45+
t.Fatal("Empty log")
46+
}
47+
err = body.Close()
48+
if err != nil {
49+
t.Fatal("Close response body:", err)
50+
}
51+
}

0 commit comments

Comments
 (0)