@@ -449,6 +449,108 @@ func TestTopPodWithMetricsServer(t *testing.T) {
449
449
}
450
450
}
451
451
452
+ func TestTopPodNoResourcesFound (t * testing.T ) {
453
+ testNS := "testns"
454
+ testCases := []struct {
455
+ name string
456
+ options * TopPodOptions
457
+ namespace string
458
+ expectedOutput string
459
+ expectedErr string
460
+ expectedPath string
461
+ }{
462
+ {
463
+ name : "all namespaces" ,
464
+ options : & TopPodOptions {AllNamespaces : true },
465
+ expectedOutput : "" ,
466
+ expectedErr : "No resources found\n " ,
467
+ expectedPath : topMetricsAPIPathPrefix + "/pods" ,
468
+ },
469
+ {
470
+ name : "all in namespace" ,
471
+ namespace : testNS ,
472
+ expectedOutput : "" ,
473
+ expectedErr : "No resources found in " + testNS + " namespace.\n " ,
474
+ expectedPath : topMetricsAPIPathPrefix + "/namespaces/" + testNS + "/pods" ,
475
+ },
476
+ }
477
+ cmdtesting .InitTestErrorHandler (t )
478
+ for _ , testCase := range testCases {
479
+ t .Run (testCase .name , func (t * testing.T ) {
480
+ fakemetricsClientset := & metricsfake.Clientset {}
481
+ fakemetricsClientset .AddReactor ("list" , "pods" , func (action core.Action ) (handled bool , ret runtime.Object , err error ) {
482
+ res := & metricsv1beta1api.PodMetricsList {
483
+ ListMeta : metav1.ListMeta {
484
+ ResourceVersion : "2" ,
485
+ },
486
+ Items : nil , // No metrics found
487
+ }
488
+ return true , res , nil
489
+ })
490
+
491
+ tf := cmdtesting .NewTestFactory ().WithNamespace (testNS )
492
+ defer tf .Cleanup ()
493
+
494
+ ns := scheme .Codecs .WithoutConversion ()
495
+
496
+ tf .Client = & fake.RESTClient {
497
+ NegotiatedSerializer : ns ,
498
+ Client : fake .CreateHTTPClient (func (req * http.Request ) (* http.Response , error ) {
499
+ switch p := req .URL .Path ; {
500
+ case p == "/api" :
501
+ return & http.Response {StatusCode : http .StatusOK , Header : cmdtesting .DefaultHeader (), Body : ioutil .NopCloser (bytes .NewReader ([]byte (apibody )))}, nil
502
+ case p == "/apis" :
503
+ return & http.Response {StatusCode : http .StatusOK , Header : cmdtesting .DefaultHeader (), Body : ioutil .NopCloser (bytes .NewReader ([]byte (apisbodyWithMetrics )))}, nil
504
+ case p == "/api/v1/namespaces/" + testNS + "/pods" :
505
+ // Top Pod calls this endpoint to check if there are pods whenever it gets no metrics,
506
+ // so we need to return no pods for this test scenario
507
+ body , _ := marshallBody (metricsv1alpha1api.PodMetricsList {
508
+ ListMeta : metav1.ListMeta {
509
+ ResourceVersion : "2" ,
510
+ },
511
+ Items : nil , // No pods found
512
+ })
513
+ return & http.Response {StatusCode : http .StatusOK , Header : cmdtesting .DefaultHeader (), Body : body }, nil
514
+ default :
515
+ t .Fatalf ("%s: unexpected request: %#v\n Got URL: %#v" ,
516
+ testCase .name , req , req .URL )
517
+ return nil , nil
518
+ }
519
+ }),
520
+ }
521
+ tf .ClientConfigVal = cmdtesting .DefaultClientConfig ()
522
+ streams , _ , buf , errbuf := genericclioptions .NewTestIOStreams ()
523
+
524
+ cmd := NewCmdTopPod (tf , nil , streams )
525
+ var cmdOptions * TopPodOptions
526
+ if testCase .options != nil {
527
+ cmdOptions = testCase .options
528
+ } else {
529
+ cmdOptions = & TopPodOptions {}
530
+ }
531
+ cmdOptions .IOStreams = streams
532
+
533
+ if err := cmdOptions .Complete (tf , cmd , nil ); err != nil {
534
+ t .Fatal (err )
535
+ }
536
+ cmdOptions .MetricsClient = fakemetricsClientset
537
+ if err := cmdOptions .Validate (); err != nil {
538
+ t .Fatal (err )
539
+ }
540
+ if err := cmdOptions .RunTopPod (); err != nil {
541
+ t .Fatal (err )
542
+ }
543
+
544
+ if e , a := testCase .expectedOutput , buf .String (); e != a {
545
+ t .Errorf ("Unexpected output:\n Expected:\n %v\n Actual:\n %v" , e , a )
546
+ }
547
+ if e , a := testCase .expectedErr , errbuf .String (); e != a {
548
+ t .Errorf ("Unexpected error:\n Expected:\n %v\n Actual:\n %v" , e , a )
549
+ }
550
+ })
551
+ }
552
+ }
553
+
452
554
type fakeDiscovery struct {}
453
555
454
556
// ServerGroups returns the supported groups, with information like supported versions and the
0 commit comments