2017-02-16 11:46:54 +00:00
|
|
|
/*
|
|
|
|
Copyright 2017 The Kubernetes Authors.
|
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
2018-05-16 14:47:29 +00:00
|
|
|
package polymorphichelpers
|
2017-02-16 11:46:54 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
2017-04-29 22:43:28 +00:00
|
|
|
"time"
|
2017-02-16 11:46:54 +00:00
|
|
|
|
2018-09-26 20:42:25 +00:00
|
|
|
appsv1 "k8s.io/api/apps/v1"
|
|
|
|
batchv1 "k8s.io/api/batch/v1"
|
2018-07-23 20:43:50 +00:00
|
|
|
corev1 "k8s.io/api/core/v1"
|
2018-09-26 20:42:25 +00:00
|
|
|
extensionsv1beta1 "k8s.io/api/extensions/v1beta1"
|
2017-02-16 11:46:54 +00:00
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
|
|
"k8s.io/apimachinery/pkg/runtime"
|
|
|
|
"k8s.io/apimachinery/pkg/runtime/schema"
|
|
|
|
"k8s.io/apimachinery/pkg/util/diff"
|
2018-07-23 20:43:50 +00:00
|
|
|
fakeexternal "k8s.io/client-go/kubernetes/fake"
|
2017-02-16 11:46:54 +00:00
|
|
|
testclient "k8s.io/client-go/testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2018-07-23 20:43:50 +00:00
|
|
|
podsResource = schema.GroupVersionResource{Version: "v1", Resource: "pods"}
|
|
|
|
podsKind = schema.GroupVersionKind{Version: "v1", Kind: "Pod"}
|
2017-02-16 11:46:54 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestLogsForObject(t *testing.T) {
|
|
|
|
tests := []struct {
|
2018-08-12 18:23:40 +00:00
|
|
|
name string
|
|
|
|
obj runtime.Object
|
|
|
|
opts *corev1.PodLogOptions
|
|
|
|
allContainers bool
|
|
|
|
pods []runtime.Object
|
|
|
|
actions []testclient.Action
|
2017-02-16 11:46:54 +00:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "pod logs",
|
2018-07-23 20:43:50 +00:00
|
|
|
obj: &corev1.Pod{
|
2017-02-16 11:46:54 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{Name: "hello", Namespace: "test"},
|
|
|
|
},
|
|
|
|
pods: []runtime.Object{testPod()},
|
|
|
|
actions: []testclient.Action{
|
|
|
|
getLogsAction("test", nil),
|
|
|
|
},
|
|
|
|
},
|
2018-08-12 18:23:40 +00:00
|
|
|
{
|
|
|
|
name: "pod logs: all containers",
|
|
|
|
obj: &corev1.Pod{
|
|
|
|
ObjectMeta: metav1.ObjectMeta{Name: "hello", Namespace: "test"},
|
|
|
|
Spec: corev1.PodSpec{
|
|
|
|
InitContainers: []corev1.Container{
|
|
|
|
{Name: "initc1"},
|
|
|
|
{Name: "initc2"},
|
|
|
|
},
|
|
|
|
Containers: []corev1.Container{
|
|
|
|
{Name: "c1"},
|
|
|
|
{Name: "c2"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
opts: &corev1.PodLogOptions{},
|
|
|
|
allContainers: true,
|
|
|
|
pods: []runtime.Object{testPod()},
|
|
|
|
actions: []testclient.Action{
|
|
|
|
getLogsAction("test", &corev1.PodLogOptions{Container: "initc1"}),
|
|
|
|
getLogsAction("test", &corev1.PodLogOptions{Container: "initc2"}),
|
|
|
|
getLogsAction("test", &corev1.PodLogOptions{Container: "c1"}),
|
|
|
|
getLogsAction("test", &corev1.PodLogOptions{Container: "c2"}),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "pods list logs",
|
|
|
|
obj: &corev1.PodList{
|
|
|
|
Items: []corev1.Pod{
|
|
|
|
{
|
|
|
|
ObjectMeta: metav1.ObjectMeta{Name: "hello", Namespace: "test"},
|
|
|
|
Spec: corev1.PodSpec{
|
|
|
|
InitContainers: []corev1.Container{
|
|
|
|
{Name: "initc1"},
|
|
|
|
{Name: "initc2"},
|
|
|
|
},
|
|
|
|
Containers: []corev1.Container{
|
|
|
|
{Name: "c1"},
|
|
|
|
{Name: "c2"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
pods: []runtime.Object{testPod()},
|
|
|
|
actions: []testclient.Action{
|
|
|
|
getLogsAction("test", nil),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "pods list logs: all containers",
|
|
|
|
obj: &corev1.PodList{
|
|
|
|
Items: []corev1.Pod{
|
|
|
|
{
|
|
|
|
ObjectMeta: metav1.ObjectMeta{Name: "hello", Namespace: "test"},
|
|
|
|
Spec: corev1.PodSpec{
|
|
|
|
InitContainers: []corev1.Container{
|
|
|
|
{Name: "initc1"},
|
|
|
|
{Name: "initc2"},
|
|
|
|
},
|
|
|
|
Containers: []corev1.Container{
|
|
|
|
{Name: "c1"},
|
|
|
|
{Name: "c2"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
opts: &corev1.PodLogOptions{},
|
|
|
|
allContainers: true,
|
|
|
|
pods: []runtime.Object{testPod()},
|
|
|
|
actions: []testclient.Action{
|
|
|
|
getLogsAction("test", &corev1.PodLogOptions{Container: "initc1"}),
|
|
|
|
getLogsAction("test", &corev1.PodLogOptions{Container: "initc2"}),
|
|
|
|
getLogsAction("test", &corev1.PodLogOptions{Container: "c1"}),
|
|
|
|
getLogsAction("test", &corev1.PodLogOptions{Container: "c2"}),
|
|
|
|
},
|
|
|
|
},
|
2017-02-16 11:46:54 +00:00
|
|
|
{
|
|
|
|
name: "replication controller logs",
|
2018-07-23 20:43:50 +00:00
|
|
|
obj: &corev1.ReplicationController{
|
2017-02-16 11:46:54 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{Name: "hello", Namespace: "test"},
|
2018-07-23 20:43:50 +00:00
|
|
|
Spec: corev1.ReplicationControllerSpec{
|
2017-02-16 11:46:54 +00:00
|
|
|
Selector: map[string]string{"foo": "bar"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
pods: []runtime.Object{testPod()},
|
|
|
|
actions: []testclient.Action{
|
2017-04-29 22:43:28 +00:00
|
|
|
testclient.NewListAction(podsResource, podsKind, "test", metav1.ListOptions{LabelSelector: "foo=bar"}),
|
2017-02-16 11:46:54 +00:00
|
|
|
getLogsAction("test", nil),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "replica set logs",
|
2018-09-26 20:42:25 +00:00
|
|
|
obj: &extensionsv1beta1.ReplicaSet{
|
2017-02-16 11:46:54 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{Name: "hello", Namespace: "test"},
|
2018-09-26 20:42:25 +00:00
|
|
|
Spec: extensionsv1beta1.ReplicaSetSpec{
|
2017-02-16 11:46:54 +00:00
|
|
|
Selector: &metav1.LabelSelector{MatchLabels: map[string]string{"foo": "bar"}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
pods: []runtime.Object{testPod()},
|
|
|
|
actions: []testclient.Action{
|
2017-04-29 22:43:28 +00:00
|
|
|
testclient.NewListAction(podsResource, podsKind, "test", metav1.ListOptions{LabelSelector: "foo=bar"}),
|
2017-02-16 11:46:54 +00:00
|
|
|
getLogsAction("test", nil),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "deployment logs",
|
2018-09-26 20:42:25 +00:00
|
|
|
obj: &extensionsv1beta1.Deployment{
|
2017-02-16 11:46:54 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{Name: "hello", Namespace: "test"},
|
2018-09-26 20:42:25 +00:00
|
|
|
Spec: extensionsv1beta1.DeploymentSpec{
|
2017-02-16 11:46:54 +00:00
|
|
|
Selector: &metav1.LabelSelector{MatchLabels: map[string]string{"foo": "bar"}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
pods: []runtime.Object{testPod()},
|
|
|
|
actions: []testclient.Action{
|
2017-04-29 22:43:28 +00:00
|
|
|
testclient.NewListAction(podsResource, podsKind, "test", metav1.ListOptions{LabelSelector: "foo=bar"}),
|
2017-02-16 11:46:54 +00:00
|
|
|
getLogsAction("test", nil),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "job logs",
|
2018-09-26 20:42:25 +00:00
|
|
|
obj: &batchv1.Job{
|
2017-02-16 11:46:54 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{Name: "hello", Namespace: "test"},
|
2018-09-26 20:42:25 +00:00
|
|
|
Spec: batchv1.JobSpec{
|
2017-02-16 11:46:54 +00:00
|
|
|
Selector: &metav1.LabelSelector{MatchLabels: map[string]string{"foo": "bar"}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
pods: []runtime.Object{testPod()},
|
|
|
|
actions: []testclient.Action{
|
2017-04-29 22:43:28 +00:00
|
|
|
testclient.NewListAction(podsResource, podsKind, "test", metav1.ListOptions{LabelSelector: "foo=bar"}),
|
2017-02-16 11:46:54 +00:00
|
|
|
getLogsAction("test", nil),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "stateful set logs",
|
2018-09-26 20:42:25 +00:00
|
|
|
obj: &appsv1.StatefulSet{
|
2017-02-16 11:46:54 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{Name: "hello", Namespace: "test"},
|
2018-09-26 20:42:25 +00:00
|
|
|
Spec: appsv1.StatefulSetSpec{
|
2017-02-16 11:46:54 +00:00
|
|
|
Selector: &metav1.LabelSelector{MatchLabels: map[string]string{"foo": "bar"}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
pods: []runtime.Object{testPod()},
|
|
|
|
actions: []testclient.Action{
|
2017-04-29 22:43:28 +00:00
|
|
|
testclient.NewListAction(podsResource, podsKind, "test", metav1.ListOptions{LabelSelector: "foo=bar"}),
|
2017-02-16 11:46:54 +00:00
|
|
|
getLogsAction("test", nil),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
2018-07-23 20:43:50 +00:00
|
|
|
fakeClientset := fakeexternal.NewSimpleClientset(test.pods...)
|
2018-08-12 18:23:40 +00:00
|
|
|
_, err := logsForObjectWithClient(fakeClientset.CoreV1(), test.obj, test.opts, 20*time.Second, test.allContainers)
|
2017-02-16 11:46:54 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("%s: unexpected error: %v", test.name, err)
|
|
|
|
continue
|
|
|
|
}
|
2018-08-12 18:23:40 +00:00
|
|
|
|
2017-02-16 11:46:54 +00:00
|
|
|
for i := range test.actions {
|
2018-05-16 14:47:29 +00:00
|
|
|
if len(fakeClientset.Actions()) < i {
|
2017-02-16 11:46:54 +00:00
|
|
|
t.Errorf("%s: action %d does not exists in actual actions: %#v",
|
2018-05-16 14:47:29 +00:00
|
|
|
test.name, i, fakeClientset.Actions())
|
2017-02-16 11:46:54 +00:00
|
|
|
continue
|
|
|
|
}
|
2018-05-16 14:47:29 +00:00
|
|
|
got := fakeClientset.Actions()[i]
|
2017-02-16 11:46:54 +00:00
|
|
|
want := test.actions[i]
|
|
|
|
if !reflect.DeepEqual(got, want) {
|
|
|
|
t.Errorf("%s: unexpected action: %s", test.name, diff.ObjectDiff(got, want))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testPod() runtime.Object {
|
2018-07-23 20:43:50 +00:00
|
|
|
return &corev1.Pod{
|
2017-02-16 11:46:54 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
|
|
Name: "foo",
|
|
|
|
Namespace: "test",
|
|
|
|
Labels: map[string]string{"foo": "bar"},
|
|
|
|
},
|
2018-07-23 20:43:50 +00:00
|
|
|
Spec: corev1.PodSpec{
|
|
|
|
RestartPolicy: corev1.RestartPolicyAlways,
|
|
|
|
DNSPolicy: corev1.DNSClusterFirst,
|
2018-08-12 18:23:40 +00:00
|
|
|
Containers: []corev1.Container{
|
|
|
|
{Name: "c1"},
|
|
|
|
{Name: "c2"},
|
|
|
|
},
|
2017-02-16 11:46:54 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-23 20:43:50 +00:00
|
|
|
func getLogsAction(namespace string, opts *corev1.PodLogOptions) testclient.Action {
|
2017-02-16 11:46:54 +00:00
|
|
|
action := testclient.GenericActionImpl{}
|
|
|
|
action.Verb = "get"
|
|
|
|
action.Namespace = namespace
|
|
|
|
action.Resource = podsResource
|
2018-10-02 15:46:17 +00:00
|
|
|
action.Subresource = "log"
|
2017-02-16 11:46:54 +00:00
|
|
|
action.Value = opts
|
|
|
|
return action
|
|
|
|
}
|