@@ -14,6 +14,10 @@ public class KubernetesMetricsTests
14
14
public const string NodeMetricsResponse = "{\n \" kind\" : \" NodeMetricsList\" ,\n \" apiVersion\" : \" metrics.k8s.io/v1beta1\" ,\n \" metadata\" : {\n \" selfLink\" : \" /apis/metrics.k8s.io/v1beta1/nodes/\" \n },\n \" items\" : [\n {\n \" metadata\" : {\n \" name\" : \" minikube\" ,\n \" selfLink\" : \" /apis/metrics.k8s.io/v1beta1/nodes/minikube\" ,\n \" creationTimestamp\" : \" 2020-07-28T20:01:05Z\" \n },\n \" timestamp\" : \" 2020-07-28T20:01:00Z\" ,\n \" window\" : \" 1m0s\" ,\n \" usage\" : {\n \" cpu\" : \" 394m\" ,\n \" memory\" : \" 1948140Ki\" \n }\n }\n ]\n }" ;
15
15
// Copy / Paste from metrics server minikube
16
16
public const string PodMetricsResponse = "{\n \" kind\" : \" PodMetricsList\" ,\n \" apiVersion\" : \" metrics.k8s.io/v1beta1\" ,\n \" metadata\" : {\n \" selfLink\" : \" /apis/metrics.k8s.io/v1beta1/namespaces/default/pods/\" \n },\n \" items\" : [\n {\n \" metadata\" : {\n \" name\" : \" dotnet-test-d4894bfbd-2q2dw\" ,\n \" namespace\" : \" default\" ,\n \" selfLink\" : \" /apis/metrics.k8s.io/v1beta1/namespaces/default/pods/dotnet-test-d4894bfbd-2q2dw\" ,\n \" creationTimestamp\" : \" 2020-08-01T07:40:05Z\" \n },\n \" timestamp\" : \" 2020-08-01T07:40:00Z\" ,\n \" window\" : \" 1m0s\" ,\n \" containers\" : [\n {\n \" name\" : \" dotnet-test\" ,\n \" usage\" : {\n \" cpu\" : \" 0\" ,\n \" memory\" : \" 14512Ki\" \n }\n }\n ]\n }\n ]\n }" ;
17
+ // Copy / Paste from metrics server minikube
18
+ public const string EmptyPodMetricsResponse = "{\n \" kind\" : \" PodMetricsList\" ,\n \" apiVersion\" : \" metrics.k8s.io/v1beta1\" ,\n \" metadata\" : {\n \" selfLink\" : \" /apis/metrics.k8s.io/v1beta1/namespaces/empty/pods/\" \n },\n \" items\" : []\n }" ;
19
+ // Copy / Paste from metrics server minikube
20
+ public const string NonExistingNamespaceResponse = "{\n \" kind\" : \" PodMetricsList\" ,\n \" apiVersion\" : \" metrics.k8s.io/v1beta1\" ,\n \" metadata\" : {\n \" selfLink\" : \" /apis/metrics.k8s.io/v1beta1/namespaces/nonexisting/pods/\" \n },\n \" items\" : []\n }" ;
17
21
18
22
public const string DefaultNodeName = "minikube" ;
19
23
public const string DefaultPodName = "dotnet-test" ;
@@ -68,5 +72,58 @@ public async Task PodsMetrics()
68
72
Assert . True ( containerMetrics . Usage . ContainsKey ( DefaultMemoryKey ) ) ;
69
73
}
70
74
}
75
+
76
+ [ Fact ( DisplayName = "Pod metrics empty response" ) ]
77
+ public async Task PodsMetricsEmptyResponse ( )
78
+ {
79
+ using ( var server = new MockKubeApiServer ( testOutput , resp : EmptyPodMetricsResponse ) )
80
+ {
81
+ var client = new Kubernetes ( new KubernetesClientConfiguration { Host = server . Uri . ToString ( ) } ) ;
82
+
83
+ var podsMetricsList = await client . GetKubernetesPodsMetricsByNamespaceAsync ( "empty" ) . ConfigureAwait ( false ) ;
84
+
85
+ Assert . Empty ( podsMetricsList . Items ) ;
86
+ }
87
+ }
88
+
89
+ [ Fact ( DisplayName = "Pod metrics by namespace" ) ]
90
+ public async Task PodsMetricsByNamespace ( )
91
+ {
92
+ var namespaceName = "default" ;
93
+
94
+ using ( var server = new MockKubeApiServer ( testOutput , resp : PodMetricsResponse ) )
95
+ {
96
+ var client = new Kubernetes ( new KubernetesClientConfiguration { Host = server . Uri . ToString ( ) } ) ;
97
+
98
+ var podsMetricsList = await client . GetKubernetesPodsMetricsByNamespaceAsync ( namespaceName ) . ConfigureAwait ( false ) ;
99
+
100
+ Assert . Single ( podsMetricsList . Items ) ;
101
+
102
+ var podMetrics = podsMetricsList . Items . First ( ) ;
103
+ Assert . Equal ( namespaceName , podMetrics . Metadata . NamespaceProperty ) ;
104
+
105
+ Assert . Single ( podMetrics . Containers ) ;
106
+
107
+ var containerMetrics = podMetrics . Containers . First ( ) ;
108
+ Assert . Equal ( DefaultPodName , containerMetrics . Name ) ;
109
+
110
+ Assert . Equal ( 2 , containerMetrics . Usage . Count ) ;
111
+ Assert . True ( containerMetrics . Usage . ContainsKey ( DefaultCpuKey ) ) ;
112
+ Assert . True ( containerMetrics . Usage . ContainsKey ( DefaultMemoryKey ) ) ;
113
+ }
114
+ }
115
+
116
+ [ Fact ( DisplayName = "Pod metrics non existing namespace response" ) ]
117
+ public async Task PodsMetricsNonExistingNamespaceResponse ( )
118
+ {
119
+ using ( var server = new MockKubeApiServer ( testOutput , resp : NonExistingNamespaceResponse ) )
120
+ {
121
+ var client = new Kubernetes ( new KubernetesClientConfiguration { Host = server . Uri . ToString ( ) } ) ;
122
+
123
+ var podsMetricsList = await client . GetKubernetesPodsMetricsByNamespaceAsync ( "nonexisting" ) . ConfigureAwait ( false ) ;
124
+
125
+ Assert . Empty ( podsMetricsList . Items ) ;
126
+ }
127
+ }
71
128
}
72
129
}
0 commit comments