Created
December 16, 2018 22:05
-
-
Save mszostok/fc50d38cc9a0274031eb23ee71bc1cd5 to your computer and use it in GitHub Desktop.
Changes after review
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func TestCmdClusterInfoDump(t *testing.T) { | |
tf := cmdtesting.NewTestFactory() | |
defer tf.Cleanup() | |
ns := scheme.Codecs | |
encodeResp := func(obj runtime.Object, ver runtime.GroupVersioner) io.ReadCloser { | |
info, _ := runtime.SerializerInfoForMediaType(ns.SupportedMediaTypes(), runtime.ContentTypeJSON) | |
encoder := ns.EncoderForVersion(info.Serializer, ver) | |
return ioutil.NopCloser(bytes.NewReader([]byte(runtime.EncodeOrDie(encoder, obj)))) | |
} | |
encodeRespForCoreGV := func(obj runtime.Object) io.ReadCloser { | |
return encodeResp(obj, corev1.SchemeGroupVersion) | |
} | |
encodeRespForAppsGV := func(obj runtime.Object) io.ReadCloser { | |
return encodeResp(obj, appsv1.SchemeGroupVersion) | |
} | |
tests := map[string]struct { | |
populateCmdFlags func(f *flag.FlagSet) | |
expListReq map[string]io.ReadCloser | |
}{ | |
"should dump default namespaces": { | |
populateCmdFlags: func(f *flag.FlagSet) { | |
// use default options | |
}, | |
expListReq: map[string]io.ReadCloser{ | |
"/api/v1/namespaces/kube-system/events": encodeRespForCoreGV(&corev1.EventList{}), | |
"/api/v1/namespaces/kube-system/replicationcontrollers": encodeRespForCoreGV(&corev1.ReplicationControllerList{}), | |
"/api/v1/namespaces/kube-system/services": encodeRespForCoreGV(&corev1.ServiceList{}), | |
"/api/v1/namespaces/kube-system/pods": encodeRespForCoreGV(&corev1.PodList{}), | |
"/apis/apps/v1/namespaces/kube-system/daemonsets": encodeRespForAppsGV(&appsv1.DaemonSetList{}), | |
"/apis/apps/v1/namespaces/kube-system/deployments": encodeRespForAppsGV(&appsv1.DeploymentList{}), | |
"/apis/apps/v1/namespaces/kube-system/replicasets": encodeRespForAppsGV(&appsv1.ReplicaSetList{}), | |
"/api/v1/namespaces/default/events": encodeRespForCoreGV(&corev1.EventList{}), | |
"/api/v1/namespaces/default/replicationcontrollers": encodeRespForCoreGV(&corev1.ReplicationControllerList{}), | |
"/api/v1/namespaces/default/services": encodeRespForCoreGV(&corev1.ServiceList{}), | |
"/api/v1/namespaces/default/pods": encodeRespForCoreGV(&corev1.PodList{}), | |
"/apis/apps/v1/namespaces/default/daemonsets": encodeRespForAppsGV(&appsv1.DaemonSetList{}), | |
"/apis/apps/v1/namespaces/default/deployments": encodeRespForAppsGV(&appsv1.DeploymentList{}), | |
"/apis/apps/v1/namespaces/default/replicasets": encodeRespForAppsGV(&appsv1.ReplicaSetList{}), | |
}, | |
}, | |
"should dump requested namespaces": { | |
populateCmdFlags: func(f *flag.FlagSet) { | |
f.Set("namespaces", "qa,production") | |
}, | |
expListReq: map[string]io.ReadCloser{ | |
"/api/v1/namespaces/qa/events": encodeRespForCoreGV(&corev1.EventList{}), | |
"/api/v1/namespaces/qa/replicationcontrollers": encodeRespForCoreGV(&corev1.ReplicationControllerList{}), | |
"/api/v1/namespaces/qa/services": encodeRespForCoreGV(&corev1.ServiceList{}), | |
"/api/v1/namespaces/qa/pods": encodeRespForCoreGV(&corev1.PodList{}), | |
"/apis/apps/v1/namespaces/qa/daemonsets": encodeRespForAppsGV(&appsv1.DaemonSetList{}), | |
"/apis/apps/v1/namespaces/qa/deployments": encodeRespForAppsGV(&appsv1.DeploymentList{}), | |
"/apis/apps/v1/namespaces/qa/replicasets": encodeRespForAppsGV(&appsv1.ReplicaSetList{}), | |
"/api/v1/namespaces/production/events": encodeRespForCoreGV(&corev1.EventList{}), | |
"/api/v1/namespaces/production/replicationcontrollers": encodeRespForCoreGV(&corev1.ReplicationControllerList{}), | |
"/api/v1/namespaces/production/services": encodeRespForCoreGV(&corev1.ServiceList{}), | |
"/api/v1/namespaces/production/pods": encodeRespForCoreGV(&corev1.PodList{}), | |
"/apis/apps/v1/namespaces/production/daemonsets": encodeRespForAppsGV(&appsv1.DaemonSetList{}), | |
"/apis/apps/v1/namespaces/production/deployments": encodeRespForAppsGV(&appsv1.DeploymentList{}), | |
"/apis/apps/v1/namespaces/production/replicasets": encodeRespForAppsGV(&appsv1.ReplicaSetList{}), | |
}, | |
}, | |
"should dump all available namespaces": { | |
populateCmdFlags: func(f *flag.FlagSet) { | |
f.Set("all-namespaces", "true") | |
}, | |
expListReq: map[string]io.ReadCloser{ | |
"/api/v1/namespaces": encodeRespForCoreGV(&corev1.NamespaceList{ | |
Items: []corev1.Namespace{ | |
{ObjectMeta: metav1.ObjectMeta{Name: "kube-system"}}, | |
{ObjectMeta: metav1.ObjectMeta{Name: "default"}}, | |
{ObjectMeta: metav1.ObjectMeta{Name: "qa"}}, | |
}, | |
}), | |
"/api/v1/namespaces/kube-system/events": encodeRespForCoreGV(&corev1.EventList{}), | |
"/api/v1/namespaces/kube-system/replicationcontrollers": encodeRespForCoreGV(&corev1.ReplicationControllerList{}), | |
"/api/v1/namespaces/kube-system/services": encodeRespForCoreGV(&corev1.ServiceList{}), | |
"/api/v1/namespaces/kube-system/pods": encodeRespForCoreGV(&corev1.PodList{}), | |
"/apis/apps/v1/namespaces/kube-system/daemonsets": encodeRespForAppsGV(&appsv1.DaemonSetList{}), | |
"/apis/apps/v1/namespaces/kube-system/deployments": encodeRespForAppsGV(&appsv1.DeploymentList{}), | |
"/apis/apps/v1/namespaces/kube-system/replicasets": encodeRespForAppsGV(&appsv1.ReplicaSetList{}), | |
"/api/v1/namespaces/default/events": encodeRespForCoreGV(&corev1.EventList{}), | |
"/api/v1/namespaces/default/replicationcontrollers": encodeRespForCoreGV(&corev1.ReplicationControllerList{}), | |
"/api/v1/namespaces/default/services": encodeRespForCoreGV(&corev1.ServiceList{}), | |
"/api/v1/namespaces/default/pods": encodeRespForCoreGV(&corev1.PodList{}), | |
"/apis/apps/v1/namespaces/default/daemonsets": encodeRespForAppsGV(&appsv1.DaemonSetList{}), | |
"/apis/apps/v1/namespaces/default/deployments": encodeRespForAppsGV(&appsv1.DeploymentList{}), | |
"/apis/apps/v1/namespaces/default/replicasets": encodeRespForAppsGV(&appsv1.ReplicaSetList{}), | |
"/api/v1/namespaces/qa/events": encodeRespForCoreGV(&corev1.EventList{}), | |
"/api/v1/namespaces/qa/replicationcontrollers": encodeRespForCoreGV(&corev1.ReplicationControllerList{}), | |
"/api/v1/namespaces/qa/services": encodeRespForCoreGV(&corev1.ServiceList{}), | |
"/api/v1/namespaces/qa/pods": encodeRespForCoreGV(&corev1.PodList{}), | |
"/apis/apps/v1/namespaces/qa/daemonsets": encodeRespForAppsGV(&appsv1.DaemonSetList{}), | |
"/apis/apps/v1/namespaces/qa/deployments": encodeRespForAppsGV(&appsv1.DeploymentList{}), | |
"/apis/apps/v1/namespaces/qa/replicasets": encodeRespForAppsGV(&appsv1.ReplicaSetList{}), | |
}, | |
}, | |
} | |
for tn, tc := range tests { | |
t.Run(tn, func(t *testing.T) { | |
expListReq := map[string]io.ReadCloser{ | |
"/api/v1/nodes": encodeRespForCoreGV(&corev1.NodeList{}), | |
} | |
for url, respBody := range tc.expListReq { | |
expListReq[url] = respBody | |
} | |
tf.Client = &fake.RESTClient{ | |
NegotiatedSerializer: ns, | |
Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) { | |
respBody, found := expListReq[req.URL.Path] | |
if !found { | |
t.Fatalf("unexpected request: %#v\n%#v", req.URL, req) | |
} | |
return &http.Response{ | |
StatusCode: http.StatusOK, | |
Header: cmdtesting.DefaultHeader(), | |
Body: respBody, | |
}, nil | |
}), | |
} | |
tf.ClientConfigVal = cmdtesting.DefaultClientConfig() | |
ioStreams, _, _, _ := genericclioptions.NewTestIOStreams() | |
cmd := NewCmdClusterInfoDump(tf, ioStreams) | |
tc.populateCmdFlags(cmd.Flags()) | |
cmd.Run(cmd, []string{}) | |
}) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment