How to use TestMetricsHandler method of v1 Package

Best Testkube code snippet using v1.TestMetricsHandler

top_test.go

Source:top_test.go Github

copy

Full Screen

1/*2Copyright (c) 2016-2017 Bitnami3Licensed under the Apache License, Version 2.0 (the "License");4you may not use this file except in compliance with the License.5You may obtain a copy of the License at6 http://www.apache.org/licenses/LICENSE-2.07Unless required by applicable law or agreed to in writing, software8distributed under the License is distributed on an "AS IS" BASIS,9WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.10See the License for the specific language governing permissions and11limitations under the License.12*/13package function14import (15 "bytes"16 "fmt"17 "io/ioutil"18 "net/http"19 "net/http/httptest"20 "strings"21 "testing"22 "k8s.io/api/core/v1"23 "k8s.io/api/extensions/v1beta1"24 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"25 "k8s.io/apimachinery/pkg/util/intstr"26 "k8s.io/client-go/kubernetes"27 "k8s.io/client-go/kubernetes/fake"28 kubelessApi "github.com/kubeless/kubeless/pkg/apis/kubeless/v1beta1"29 "github.com/kubeless/kubeless/pkg/client/clientset/versioned"30 fFake "github.com/kubeless/kubeless/pkg/client/clientset/versioned/fake"31 "github.com/kubeless/kubeless/pkg/utils"32)33type testMetricsHandler struct{}34// handler used for testing purposes only35// satisfies the MetricsRetriever interface, gets metrics from the test http server (URL to test http server stored in svc.SelfLink field)36func (h *testMetricsHandler) GetRawMetrics(apiClient kubernetes.Interface, namespace, functionName string) ([]byte, error) {37 svc, err := apiClient.CoreV1().Services(namespace).Get(functionName, metav1.GetOptions{})38 if err != nil {39 return []byte{}, err40 }41 b, err := http.Get(svc.SelfLink)42 if err != nil {43 return nil, err44 }45 defer b.Body.Close()46 return ioutil.ReadAll(b.Body)47}48func topOutput(t *testing.T, client versioned.Interface, apiV1Client kubernetes.Interface, h utils.MetricsRetriever, ns, functionName, output string) string {49 var buf bytes.Buffer50 if err := doTop(&buf, client, apiV1Client, h, ns, functionName, output); err != nil {51 t.Fatalf("doTop returned error: %v", err)52 }53 return buf.String()54}55func TestTop(t *testing.T) {56 // setup test server to serve the /metrics endpoint57 ts2 := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {58 fmt.Fprint(w,59 `# HELP go_gc_duration_seconds A summary of the GC invocation durations.60 # TYPE go_gc_duration_seconds summary61 go_gc_duration_seconds{quantile="0"} 1.6846e-0562 go_gc_duration_seconds{quantile="0.25"} 3.9124e-0563 go_gc_duration_seconds{quantile="0.5"} 0.00014718364 go_gc_duration_seconds{quantile="0.75"} 0.00095841965 go_gc_duration_seconds{quantile="1"} 0.0079603566 go_gc_duration_seconds_sum 2.5078130367 go_gc_duration_seconds_count 342468 # HELP go_goroutines Number of goroutines that currently exist.69 # TYPE go_goroutines gauge70 go_goroutines 771 # HELP go_info Information about the Go environment.72 # TYPE go_info gauge73 go_info{version="go1.10.2"} 174 # HELP go_memstats_alloc_bytes Number of bytes allocated and still in use.75 # TYPE go_memstats_alloc_bytes gauge76 go_memstats_alloc_bytes 2.28336e+0677 # HELP go_memstats_alloc_bytes_total Total number of bytes allocated, even if freed.78 # TYPE go_memstats_alloc_bytes_total counter79 go_memstats_alloc_bytes_total 9.9682544e+0980 # HELP go_memstats_buck_hash_sys_bytes Number of bytes used by the profiling bucket hash table.81 # TYPE go_memstats_buck_hash_sys_bytes gauge82 go_memstats_buck_hash_sys_bytes 1.500081e+0683 # HELP go_memstats_frees_total Total number of frees.84 # TYPE go_memstats_frees_total counter85 go_memstats_frees_total 1.2698678e+0786 # HELP go_memstats_gc_cpu_fraction The fraction of this program's available CPU time used by the GC since the program started.87 # TYPE go_memstats_gc_cpu_fraction gauge88 go_memstats_gc_cpu_fraction 0.000121450686134019889 # HELP go_memstats_gc_sys_bytes Number of bytes used for garbage collection system metadata.90 # TYPE go_memstats_gc_sys_bytes gauge91 go_memstats_gc_sys_bytes 40550492 # HELP go_memstats_heap_alloc_bytes Number of heap bytes allocated and still in use.93 # TYPE go_memstats_heap_alloc_bytes gauge94 go_memstats_heap_alloc_bytes 2.28336e+0695 # HELP go_memstats_heap_idle_bytes Number of heap bytes waiting to be used.96 # TYPE go_memstats_heap_idle_bytes gauge97 go_memstats_heap_idle_bytes 2.6624e+0698 # HELP go_memstats_heap_inuse_bytes Number of heap bytes that are in use.99 # TYPE go_memstats_heap_inuse_bytes gauge100 go_memstats_heap_inuse_bytes 3.072e+06101 # HELP go_memstats_heap_objects Number of allocated objects.102 # TYPE go_memstats_heap_objects gauge103 go_memstats_heap_objects 6280104 # HELP go_memstats_heap_released_bytes Number of heap bytes released to OS.105 # TYPE go_memstats_heap_released_bytes gauge106 go_memstats_heap_released_bytes 0107 # HELP go_memstats_heap_sys_bytes Number of heap bytes obtained from system.108 # TYPE go_memstats_heap_sys_bytes gauge109 go_memstats_heap_sys_bytes 5.7344e+06110 # HELP go_memstats_last_gc_time_seconds Number of seconds since 1970 of last garbage collection.111 # TYPE go_memstats_last_gc_time_seconds gauge112 go_memstats_last_gc_time_seconds 1.528573398809276e+09113 # HELP go_memstats_lookups_total Total number of pointer lookups.114 # TYPE go_memstats_lookups_total counter115 go_memstats_lookups_total 88701116 # HELP go_memstats_mallocs_total Total number of mallocs.117 # TYPE go_memstats_mallocs_total counter118 go_memstats_mallocs_total 1.2704958e+07119 # HELP go_memstats_mcache_inuse_bytes Number of bytes in use by mcache structures.120 # TYPE go_memstats_mcache_inuse_bytes gauge121 go_memstats_mcache_inuse_bytes 3472122 # HELP go_memstats_mcache_sys_bytes Number of bytes used for mcache structures obtained from system.123 # TYPE go_memstats_mcache_sys_bytes gauge124 go_memstats_mcache_sys_bytes 16384125 # HELP go_memstats_mspan_inuse_bytes Number of bytes in use by mspan structures.126 # TYPE go_memstats_mspan_inuse_bytes gauge127 go_memstats_mspan_inuse_bytes 25688128 # HELP go_memstats_mspan_sys_bytes Number of bytes used for mspan structures obtained from system.129 # TYPE go_memstats_mspan_sys_bytes gauge130 go_memstats_mspan_sys_bytes 32768131 # HELP go_memstats_next_gc_bytes Number of heap bytes when next garbage collection will take place.132 # TYPE go_memstats_next_gc_bytes gauge133 go_memstats_next_gc_bytes 4.194304e+06134 # HELP go_memstats_other_sys_bytes Number of bytes used for other system allocations.135 # TYPE go_memstats_other_sys_bytes gauge136 go_memstats_other_sys_bytes 738631137 # HELP go_memstats_stack_inuse_bytes Number of bytes in use by the stack allocator.138 # TYPE go_memstats_stack_inuse_bytes gauge139 go_memstats_stack_inuse_bytes 557056140 # HELP go_memstats_stack_sys_bytes Number of bytes obtained from system for stack allocator.141 # TYPE go_memstats_stack_sys_bytes gauge142 go_memstats_stack_sys_bytes 557056143 # HELP go_memstats_sys_bytes Number of bytes obtained from system.144 # TYPE go_memstats_sys_bytes gauge145 go_memstats_sys_bytes 8.984824e+06146 # HELP go_threads Number of OS threads created.147 # TYPE go_threads gauge148 go_threads 10149 # HELP process_cpu_seconds_total Total user and system CPU time spent in seconds.150 # TYPE process_cpu_seconds_total counter151 process_cpu_seconds_total 25.88152 # HELP process_max_fds Maximum number of open file descriptors.153 # TYPE process_max_fds gauge154 process_max_fds 1.048576e+06155 # HELP process_open_fds Number of open file descriptors.156 # TYPE process_open_fds gauge157 process_open_fds 8158 # HELP process_resident_memory_bytes Resident memory size in bytes.159 # TYPE process_resident_memory_bytes gauge160 process_resident_memory_bytes 1.3942784e+07161 # HELP process_start_time_seconds Start time of the process since unix epoch in seconds.162 # TYPE process_start_time_seconds gauge163 process_start_time_seconds 1.52853941225e+09164 # HELP process_virtual_memory_bytes Virtual memory size in bytes.165 # TYPE process_virtual_memory_bytes gauge166 process_virtual_memory_bytes 1.57294592e+08167 # HELP promhttp_metric_handler_requests_in_flight Current number of scrapes being served.168 # TYPE promhttp_metric_handler_requests_in_flight gauge169 promhttp_metric_handler_requests_in_flight 1170 # HELP promhttp_metric_handler_requests_total Total number of scrapes by HTTP status code.171 # TYPE promhttp_metric_handler_requests_total counter172 promhttp_metric_handler_requests_total{code="200"} 10798173 promhttp_metric_handler_requests_total{code="500"} 0174 promhttp_metric_handler_requests_total{code="503"} 0175`)176 }))177 defer ts2.Close()178 // setup test server to serve the /metrics endpoint179 ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {180 fmt.Fprint(w,181 `# HELP process_virtual_memory_bytes Virtual memory size in bytes.182 # TYPE process_virtual_memory_bytes gauge183 process_virtual_memory_bytes 815255552.0184 # HELP process_resident_memory_bytes Resident memory size in bytes.185 # TYPE process_resident_memory_bytes gauge186 process_resident_memory_bytes 25001984.0187 # HELP process_start_time_seconds Start time of the process since unix epoch in seconds.188 # TYPE process_start_time_seconds gauge189 process_start_time_seconds 1528507334.03190 # HELP process_cpu_seconds_total Total user and system CPU time spent in seconds.191 # TYPE process_cpu_seconds_total counter192 process_cpu_seconds_total 54.72193 # HELP process_open_fds Number of open file descriptors.194 # TYPE process_open_fds gauge195 process_open_fds 8.0196 # HELP process_max_fds Maximum number of open file descriptors.197 # TYPE process_max_fds gauge198 process_max_fds 1048576.0199 # HELP python_info Python platform information200 # TYPE python_info gauge201 python_info{implementation="CPython",major="2",minor="7",patchlevel="9",version="2.7.9"} 1.0202 # HELP function_failures_total Number of exceptions in user function203 # TYPE function_failures_total counter204 function_failures_total{method="GET"} 0.0205 function_failures_total{method="POST"} 0.0206 # HELP function_calls_total Number of calls to user function207 # TYPE function_calls_total counter208 function_calls_total{method="GET"} 254.0209 function_calls_total{method="POST"} 296.0210 # HELP function_duration_seconds Duration of user function in seconds211 # TYPE function_duration_seconds histogram212 function_duration_seconds_bucket{le="0.005",method="GET"} 8.0213 function_duration_seconds_bucket{le="0.01",method="GET"} 191.0214 function_duration_seconds_bucket{le="0.025",method="GET"} 248.0215 function_duration_seconds_bucket{le="0.05",method="GET"} 253.0216 function_duration_seconds_bucket{le="0.075",method="GET"} 253.0217 function_duration_seconds_bucket{le="0.1",method="GET"} 253.0218 function_duration_seconds_bucket{le="0.25",method="GET"} 254.0219 function_duration_seconds_bucket{le="0.5",method="GET"} 254.0220 function_duration_seconds_bucket{le="0.75",method="GET"} 254.0221 function_duration_seconds_bucket{le="1.0",method="GET"} 254.0222 function_duration_seconds_bucket{le="2.5",method="GET"} 254.0223 function_duration_seconds_bucket{le="5.0",method="GET"} 254.0224 function_duration_seconds_bucket{le="7.5",method="GET"} 254.0225 function_duration_seconds_bucket{le="10.0",method="GET"} 254.0226 function_duration_seconds_bucket{le="+Inf",method="GET"} 254.0227 function_duration_seconds_count{method="GET"} 254.0228 function_duration_seconds_sum{method="GET"} 2.863368272781372229 function_duration_seconds_bucket{le="0.005",method="POST"} 1.0230 function_duration_seconds_bucket{le="0.01",method="POST"} 157.0231 function_duration_seconds_bucket{le="0.025",method="POST"} 296.0232 function_duration_seconds_bucket{le="0.05",method="POST"} 296.0233 function_duration_seconds_bucket{le="0.075",method="POST"} 296.0234 function_duration_seconds_bucket{le="0.1",method="POST"} 296.0235 function_duration_seconds_bucket{le="0.25",method="POST"} 296.0236 function_duration_seconds_bucket{le="0.5",method="POST"} 296.0237 function_duration_seconds_bucket{le="0.75",method="POST"} 296.0238 function_duration_seconds_bucket{le="1.0",method="POST"} 296.0239 function_duration_seconds_bucket{le="2.5",method="POST"} 296.0240 function_duration_seconds_bucket{le="5.0",method="POST"} 296.0241 function_duration_seconds_bucket{le="7.5",method="POST"} 296.0242 function_duration_seconds_bucket{le="10.0",method="POST"} 296.0243 function_duration_seconds_bucket{le="+Inf",method="POST"} 296.0244 function_duration_seconds_count{method="POST"} 296.0245 function_duration_seconds_sum{method="POST"} 3.4116291999816895246`)247 }))248 defer ts.Close()249 function1Name := "pyFunc"250 function2Name := "goFunc"251 namespace := "myns"252 listObj := kubelessApi.FunctionList{253 Items: []*kubelessApi.Function{254 {255 ObjectMeta: metav1.ObjectMeta{256 Name: function1Name,257 Namespace: namespace,258 },259 Spec: kubelessApi.FunctionSpec{260 Handler: "fhandler",261 Function: function1Name,262 Runtime: "pyruntime",263 Deps: "pydeps",264 Deployment: v1beta1.Deployment{265 Spec: v1beta1.DeploymentSpec{266 Template: v1.PodTemplateSpec{267 Spec: v1.PodSpec{268 Containers: []v1.Container{{}},269 },270 },271 },272 },273 },274 },275 {276 ObjectMeta: metav1.ObjectMeta{277 Name: function2Name,278 Namespace: namespace,279 },280 Spec: kubelessApi.FunctionSpec{281 Handler: "gohandler",282 Function: function2Name,283 Runtime: "goruntime",284 Deps: "godeps",285 Deployment: v1beta1.Deployment{286 Spec: v1beta1.DeploymentSpec{287 Template: v1.PodTemplateSpec{288 Spec: v1.PodSpec{289 Containers: []v1.Container{{}},290 },291 },292 },293 },294 },295 },296 },297 }298 client := fFake.NewSimpleClientset(listObj.Items[0], listObj.Items[1])299 deploymentPy := v1beta1.Deployment{300 ObjectMeta: metav1.ObjectMeta{301 Name: function1Name,302 Namespace: namespace,303 },304 Status: v1beta1.DeploymentStatus{305 Replicas: int32(1),306 ReadyReplicas: int32(1),307 },308 }309 deploymentGo := v1beta1.Deployment{310 ObjectMeta: metav1.ObjectMeta{311 Name: function2Name,312 Namespace: namespace,313 },314 Status: v1beta1.DeploymentStatus{315 Replicas: int32(1),316 ReadyReplicas: int32(1),317 },318 }319 serviceGo := v1.Service{320 ObjectMeta: metav1.ObjectMeta{321 Name: function2Name,322 Namespace: namespace,323 SelfLink: ts2.URL,324 },325 Spec: v1.ServiceSpec{326 Ports: []v1.ServicePort{327 {328 Name: "p1",329 Port: int32(8080),330 TargetPort: intstr.FromInt(8080),331 NodePort: 0,332 Protocol: v1.ProtocolTCP,333 },334 },335 },336 }337 servicePy := v1.Service{338 ObjectMeta: metav1.ObjectMeta{339 Name: function1Name,340 Namespace: namespace,341 SelfLink: ts.URL,342 },343 Spec: v1.ServiceSpec{344 Ports: []v1.ServicePort{345 {346 Name: "p1",347 Port: int32(8080),348 TargetPort: intstr.FromInt(8080),349 NodePort: 0,350 Protocol: v1.ProtocolTCP,351 },352 },353 },354 }355 apiV1Client := fake.NewSimpleClientset(&deploymentPy, &servicePy, &deploymentGo, &serviceGo)356 handler := &testMetricsHandler{}357 // List multiple functions358 output := topOutput(t, client, apiV1Client, handler, namespace, "", "")359 t.Log("output is", output)360 if !strings.Contains(output, function1Name) || !strings.Contains(output, function2Name) || !strings.Contains(output, namespace) {361 t.Errorf("table output didn't match FUNCTION or NAMESPACE")362 }363 if !strings.Contains(output, "GET") || !strings.Contains(output, "POST") {364 t.Errorf("table output didn't match on METHOD")365 }366 if !strings.Contains(output, "2.86336") || !strings.Contains(output, "3.41162") {367 t.Errorf("table output didn't match on TOTAL_DURATION_SECONDS")368 }369 // verify calculated fields370 if !strings.Contains(output, "0.0112731") || !strings.Contains(output, "0.0115257") {371 t.Errorf("table output didn't match on AVG_DURATION_SECONDS")372 }373 // Get single function374 output = topOutput(t, client, apiV1Client, handler, namespace, function2Name, "")375 t.Log("output is", output)376 if strings.Contains(output, function1Name) || !strings.Contains(output, function2Name) || !strings.Contains(output, namespace) {377 t.Errorf("table output didn't match FUNCTION or NAMESPACE")378 }379 // json output380 output = topOutput(t, client, apiV1Client, handler, namespace, "", "json")381 t.Log("output is", output)382 if !strings.Contains(output, function1Name) || !strings.Contains(output, function2Name) || !strings.Contains(output, namespace) {383 t.Errorf("table output didn't match FUNCTION or NAMESPACE")384 }385 if !strings.Contains(output, "GET") || !strings.Contains(output, "POST") {386 t.Errorf("table output didn't match on METHOD")387 }388 if !strings.Contains(output, "2.86336") || !strings.Contains(output, "3.41162") {389 t.Errorf("table output didn't match on TOTAL_DURATION_SECONDS")390 }391 // verify calculated fields392 if !strings.Contains(output, "0.0112731") || !strings.Contains(output, "0.0115257") {393 t.Errorf("table output didn't match on AVG_DURATION_SECONDS")394 }395 // yaml output396 output = topOutput(t, client, apiV1Client, handler, namespace, "", "yaml")397 t.Log("output is", output)398 if !strings.Contains(output, function1Name) || !strings.Contains(output, function2Name) || !strings.Contains(output, namespace) {399 t.Errorf("table output didn't match FUNCTION or NAMESPACE")400 }401 if !strings.Contains(output, "GET") || !strings.Contains(output, "POST") {402 t.Errorf("table output didn't match on METHOD")403 }404 if !strings.Contains(output, "2.86336") || !strings.Contains(output, "3.41162") {405 t.Errorf("table output didn't match on TOTAL_DURATION_SECONDS")406 }407 // verify calculated fields408 if !strings.Contains(output, "0.0112731") || !strings.Contains(output, "0.0115257") {409 t.Errorf("table output didn't match on AVG_DURATION_SECONDS")410 }411}...

Full Screen

Full Screen

pod_collector_test.go

Source:pod_collector_test.go Github

copy

Full Screen

1package collector2import (3 "context"4 "encoding/json"5 "fmt"6 "net/http"7 "net/http/httptest"8 "net/url"9 "sync"10 "testing"11 "time"12 "github.com/stretchr/testify/require"13 appsv1 "k8s.io/api/apps/v1"14 autoscalingv2 "k8s.io/api/autoscaling/v2beta2"15 corev1 "k8s.io/api/core/v1"16 v1 "k8s.io/apimachinery/pkg/apis/meta/v1"17 "k8s.io/client-go/kubernetes"18 "k8s.io/client-go/kubernetes/fake"19)20const (21 testNamespace = "test-namespace"22 applicationLabelName = "application"23 applicationLabelValue = "test-application"24 testDeploymentName = "test-application"25 testInterval = 10 * time.Second26)27func TestPodCollector(t *testing.T) {28 for _, tc := range []struct {29 name string30 metrics [][]int6431 result []int6432 }{33 {34 name: "simple",35 metrics: [][]int64{{1}, {3}, {8}, {5}, {2}},36 result: []int64{1, 3, 8, 5, 2},37 },38 } {39 t.Run(tc.name, func(t *testing.T) {40 client := fake.NewSimpleClientset()41 plugin := NewPodCollectorPlugin(client)42 makeTestDeployment(t, client)43 host, port, metricsHandler := makeTestHTTPServer(t, tc.metrics)44 lastReadyTransitionTimeTimestamp := v1.NewTime(time.Now().Add(time.Duration(-30) * time.Second))45 minPodReadyAge := time.Duration(0 * time.Second)46 podCondition := corev1.PodCondition{Type: corev1.PodReady, Status: corev1.ConditionTrue, LastTransitionTime: lastReadyTransitionTimeTimestamp}47 podDeletionTimestamp := time.Time{}48 makeTestPods(t, host, port, "test-metric", client, 5, podCondition, podDeletionTimestamp)49 testHPA := makeTestHPA(t, client)50 testConfig := makeTestConfig(port, minPodReadyAge)51 collector, err := plugin.NewCollector(testHPA, testConfig, testInterval)52 require.NoError(t, err)53 metrics, err := collector.GetMetrics()54 require.NoError(t, err)55 require.Equal(t, len(metrics), int(metricsHandler.calledCounter))56 var values []int6457 for _, m := range metrics {58 values = append(values, m.Custom.Value.Value())59 }60 require.ElementsMatch(t, tc.result, values)61 })62 }63}64func TestPodCollectorWithMinPodReadyAge(t *testing.T) {65 for _, tc := range []struct {66 name string67 metrics [][]int6468 result []int6469 }{70 {71 name: "simple-with-min-pod-ready-age",72 metrics: [][]int64{{1}, {3}, {8}, {5}, {2}},73 result: []int64{},74 },75 } {76 t.Run(tc.name, func(t *testing.T) {77 client := fake.NewSimpleClientset()78 plugin := NewPodCollectorPlugin(client)79 makeTestDeployment(t, client)80 host, port, metricsHandler := makeTestHTTPServer(t, tc.metrics)81 // Setting pods age to 30 seconds82 lastReadyTransitionTimeTimestamp := v1.NewTime(time.Now().Add(time.Duration(-30) * time.Second))83 // Pods that are not older that 60 seconds (all in this case) should not be processed84 minPodReadyAge := time.Duration(60 * time.Second)85 podCondition := corev1.PodCondition{Type: corev1.PodReady, Status: corev1.ConditionTrue, LastTransitionTime: lastReadyTransitionTimeTimestamp}86 podDeletionTimestamp := time.Time{}87 makeTestPods(t, host, port, "test-metric", client, 5, podCondition, podDeletionTimestamp)88 testHPA := makeTestHPA(t, client)89 testConfig := makeTestConfig(port, minPodReadyAge)90 collector, err := plugin.NewCollector(testHPA, testConfig, testInterval)91 require.NoError(t, err)92 metrics, err := collector.GetMetrics()93 require.NoError(t, err)94 require.Equal(t, len(metrics), int(metricsHandler.calledCounter))95 var values []int6496 for _, m := range metrics {97 values = append(values, m.Custom.Value.Value())98 }99 require.ElementsMatch(t, tc.result, values)100 })101 }102}103func TestPodCollectorWithPodCondition(t *testing.T) {104 for _, tc := range []struct {105 name string106 metrics [][]int64107 result []int64108 }{109 {110 name: "simple-with-pod-condition",111 metrics: [][]int64{{1}, {3}, {8}, {5}, {2}},112 result: []int64{},113 },114 } {115 t.Run(tc.name, func(t *testing.T) {116 client := fake.NewSimpleClientset()117 plugin := NewPodCollectorPlugin(client)118 makeTestDeployment(t, client)119 host, port, metricsHandler := makeTestHTTPServer(t, tc.metrics)120 lastScheduledTransitionTimeTimestamp := v1.NewTime(time.Now().Add(time.Duration(-30) * time.Second))121 minPodReadyAge := time.Duration(0 * time.Second)122 podDeletionTimestamp := time.Time{}123 //Pods in state corev1.PodReady == corev1.ConditionFalse should not be processed124 podCondition := corev1.PodCondition{Type: corev1.PodReady, Status: corev1.ConditionFalse, LastTransitionTime: lastScheduledTransitionTimeTimestamp}125 makeTestPods(t, host, port, "test-metric", client, 5, podCondition, podDeletionTimestamp)126 testHPA := makeTestHPA(t, client)127 testConfig := makeTestConfig(port, minPodReadyAge)128 collector, err := plugin.NewCollector(testHPA, testConfig, testInterval)129 require.NoError(t, err)130 metrics, err := collector.GetMetrics()131 require.NoError(t, err)132 require.Equal(t, len(metrics), int(metricsHandler.calledCounter))133 var values []int64134 for _, m := range metrics {135 values = append(values, m.Custom.Value.Value())136 }137 require.ElementsMatch(t, tc.result, values)138 })139 }140}141func TestPodCollectorWithPodTerminatingCondition(t *testing.T) {142 for _, tc := range []struct {143 name string144 metrics [][]int64145 result []int64146 }{147 {148 name: "simple-with-pod-condition",149 metrics: [][]int64{{1}, {3}, {8}, {5}, {2}},150 result: []int64{},151 },152 } {153 t.Run(tc.name, func(t *testing.T) {154 client := fake.NewSimpleClientset()155 plugin := NewPodCollectorPlugin(client)156 makeTestDeployment(t, client)157 host, port, metricsHandler := makeTestHTTPServer(t, tc.metrics)158 lastScheduledTransitionTimeTimestamp := v1.NewTime(time.Now().Add(time.Duration(-30) * time.Second))159 minPodReadyAge := time.Duration(0 * time.Second)160 //Pods with podDeletionTimestamp should not be processed161 podDeletionTimestamp := time.Now()162 podCondition := corev1.PodCondition{Type: corev1.PodReady, Status: corev1.ConditionTrue, LastTransitionTime: lastScheduledTransitionTimeTimestamp}163 makeTestPods(t, host, port, "test-metric", client, 5, podCondition, podDeletionTimestamp)164 testHPA := makeTestHPA(t, client)165 testConfig := makeTestConfig(port, minPodReadyAge)166 collector, err := plugin.NewCollector(testHPA, testConfig, testInterval)167 require.NoError(t, err)168 metrics, err := collector.GetMetrics()169 require.NoError(t, err)170 require.Equal(t, len(metrics), int(metricsHandler.calledCounter))171 var values []int64172 for _, m := range metrics {173 values = append(values, m.Custom.Value.Value())174 }175 require.ElementsMatch(t, tc.result, values)176 })177 }178}179type testMetricResponse struct {180 Values []int64 `json:"values"`181}182type testMetricsHandler struct {183 values [][]int64184 calledCounter uint185 t *testing.T186 metricsPath string187 sync.RWMutex188}189func (h *testMetricsHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {190 h.Lock()191 defer h.Unlock()192 require.Equal(h.t, h.metricsPath, r.URL.Path)193 require.Less(h.t, int(h.calledCounter), len(h.values))194 response, err := json.Marshal(testMetricResponse{Values: h.values[h.calledCounter]})195 require.NoError(h.t, err)196 w.Header().Set("Content-Type", "application/json")197 _, err = w.Write(response)198 require.NoError(h.t, err)199 h.calledCounter++200}201func makeTestHTTPServer(t *testing.T, values [][]int64) (string, string, *testMetricsHandler) {202 metricsHandler := &testMetricsHandler{values: values, t: t, metricsPath: "/metrics"}203 server := httptest.NewServer(metricsHandler)204 url, err := url.Parse(server.URL)205 require.NoError(t, err)206 return url.Hostname(), url.Port(), metricsHandler207}208func makeTestConfig(port string, minPodReadyAge time.Duration) *MetricConfig {209 return &MetricConfig{210 CollectorType: "json-path",211 Config: map[string]string{"json-key": "$.values", "port": port, "path": "/metrics", "aggregator": "sum"},212 MinPodReadyAge: minPodReadyAge,213 }214}215func makeTestPods(t *testing.T, testServer string, metricName string, port string, client kubernetes.Interface, replicas int, podCondition corev1.PodCondition, podDeletionTimestamp time.Time) {216 for i := 0; i < replicas; i++ {217 testPod := &corev1.Pod{218 ObjectMeta: v1.ObjectMeta{219 Name: fmt.Sprintf("test-pod-%d", i),220 Labels: map[string]string{applicationLabelName: applicationLabelValue},221 Annotations: map[string]string{222 fmt.Sprintf("metric-config.pods.%s.json-path/port", metricName): port,223 },224 },225 Status: corev1.PodStatus{226 PodIP: testServer,227 Conditions: []corev1.PodCondition{podCondition},228 },229 }230 if podDeletionTimestamp.IsZero() {231 testPod.ObjectMeta.DeletionTimestamp = nil232 } else {233 testPod.ObjectMeta.DeletionTimestamp = &v1.Time{Time: podDeletionTimestamp}234 }235 _, err := client.CoreV1().Pods(testNamespace).Create(context.TODO(), testPod, v1.CreateOptions{})236 require.NoError(t, err)237 }238}239func makeTestDeployment(t *testing.T, client kubernetes.Interface) *appsv1.Deployment {240 deployment := appsv1.Deployment{241 ObjectMeta: v1.ObjectMeta{Name: testDeploymentName},242 Spec: appsv1.DeploymentSpec{243 Selector: &v1.LabelSelector{244 MatchLabels: map[string]string{applicationLabelName: applicationLabelValue},245 },246 },247 }248 _, err := client.AppsV1().Deployments(testNamespace).Create(context.TODO(), &deployment, v1.CreateOptions{})249 require.NoError(t, err)250 return &deployment251}252func makeTestHPA(t *testing.T, client kubernetes.Interface) *autoscalingv2.HorizontalPodAutoscaler {253 hpa := &autoscalingv2.HorizontalPodAutoscaler{254 ObjectMeta: v1.ObjectMeta{255 Name: "test-hpa",256 Namespace: testNamespace,257 },258 Spec: autoscalingv2.HorizontalPodAutoscalerSpec{259 ScaleTargetRef: autoscalingv2.CrossVersionObjectReference{260 Kind: "Deployment",261 Name: testDeploymentName,262 APIVersion: "apps/v1",263 },264 },265 }266 _, err := client.AutoscalingV2beta2().HorizontalPodAutoscalers("test-namespace").Create(context.TODO(), hpa, v1.CreateOptions{})267 require.NoError(t, err)268 return hpa269}...

Full Screen

Full Screen

metrics_test.go

Source:metrics_test.go Github

copy

Full Screen

1package e2e_test2import (3 "context"4 "sync/atomic"5 v2 "github.com/envoyproxy/go-control-plane/envoy/service/metrics/v2"6 "github.com/fgrosse/zaptest"7 . "github.com/onsi/ginkgo"8 "github.com/onsi/ginkgo/config"9 . "github.com/onsi/gomega"10 gatewayv2 "github.com/solo-io/gloo/projects/gateway/pkg/api/v2"11 gatewaydefaults "github.com/solo-io/gloo/projects/gateway/pkg/defaults"12 "github.com/solo-io/gloo/projects/gloo/pkg/defaults"13 "github.com/solo-io/gloo/projects/metrics/pkg/metricsservice"14 "github.com/solo-io/gloo/projects/metrics/pkg/runner"15 "github.com/solo-io/gloo/test/services"16 "github.com/solo-io/gloo/test/v1helpers"17 "github.com/solo-io/go-utils/contextutils"18 "github.com/solo-io/solo-kit/pkg/api/v1/clients"19)20type testMetricsHandler struct {21 channel chan *struct{}22}23func (t *testMetricsHandler) HandleMetrics(context.Context, *v2.StreamMetricsMessage) error {24 // just signal that we did receive metrics from envoy25 t.channel <- &struct {26 }{}27 return nil28}29var _ = Describe("Gateway", func() {30 var (31 gw *gatewayv2.Gateway32 ctx context.Context33 cancel context.CancelFunc34 testClients services.TestClients35 settings runner.Settings36 writeNamespace string37 baseMetricsPort = uint32(27000)38 )39 Describe("in memory", func() {40 BeforeEach(func() {41 ctx, cancel = context.WithCancel(context.Background())42 defaults.HttpPort = services.NextBindPort()43 defaults.HttpsPort = services.NextBindPort()44 writeNamespace = "gloo-system"45 ro := &services.RunOptions{46 NsToWrite: writeNamespace,47 NsToWatch: []string{"default", writeNamespace},48 WhatToRun: services.What{49 DisableGateway: false,50 DisableFds: true,51 DisableUds: true,52 },53 }54 testClients = services.RunGlooGatewayUdsFds(ctx, ro)55 // wait for the two gateways to be created.56 Eventually(func() (gatewayv2.GatewayList, error) {57 return testClients.GatewayClient.List(writeNamespace, clients.ListOpts{})58 }, "10s", "0.1s").Should(HaveLen(2))59 })60 AfterEach(func() {61 cancel()62 })63 Context("Metrics", func() {64 var (65 envoyInstance *services.EnvoyInstance66 tu *v1helpers.TestUpstream67 )68 TestUpstreamReachable := func() {69 v1helpers.TestUpstreamReachable(defaults.HttpPort, tu, nil)70 }71 BeforeEach(func() {72 ctx, cancel = context.WithCancel(context.Background())73 var err error74 envoyInstance, err = envoyFactory.NewEnvoyInstance()75 Expect(err).NotTo(HaveOccurred())76 tu = v1helpers.NewTestHttpUpstream(ctx, envoyInstance.LocalAddr())77 _, err = testClients.UpstreamClient.Write(tu.Upstream, clients.WriteOpts{})78 Expect(err).NotTo(HaveOccurred())79 })80 AfterEach(func() {81 if envoyInstance != nil {82 _ = envoyInstance.Clean()83 }84 })85 Context("Grpc", func() {86 var (87 channel chan *struct{}88 testHandler *testMetricsHandler89 )90 BeforeEach(func() {91 metricsPort := atomic.AddUint32(&baseMetricsPort, 1) + uint32(config.GinkgoConfig.ParallelNode*1000)92 logger := zaptest.LoggerWriter(GinkgoWriter)93 contextutils.SetFallbackLogger(logger.Sugar())94 envoyInstance.MetricsPort = metricsPort95 err := envoyInstance.RunWithRole(writeNamespace+"~gateway-proxy-v2", testClients.GlooPort)96 Expect(err).NotTo(HaveOccurred())97 gatewaycli := testClients.GatewayClient98 gw, err = gatewaycli.Read("gloo-system", gatewaydefaults.GatewayProxyName, clients.ReadOpts{})99 Expect(err).NotTo(HaveOccurred())100 settings = runner.Settings{101 ServerPort: int(metricsPort),102 }103 opts := metricsservice.Options{104 Ctx: ctx,105 }106 //usageMerger := metricsservice.NewUsageMerger(time.Now)107 //storage := metricsservice.NewConfigMapStorage(writeNamespace, helpers.MustKubeClient().CoreV1().ConfigMaps(writeNamespace))108 //109 //defaulthandler := metricsservice.NewDefaultMetricsHandler(storage, usageMerger)110 channel = make(chan *struct{}, 1000)111 testHandler = &testMetricsHandler{channel: channel}112 service := metricsservice.NewServer(opts, testHandler)113 go func(testctx context.Context) {114 defer GinkgoRecover()115 err := runner.RunWithSettings(testctx, service, settings)116 if testctx.Err() == nil {117 Expect(err).NotTo(HaveOccurred())118 }119 }(ctx)120 })121 AfterEach(func() {122 gatewaycli := testClients.GatewayClient123 var err error124 gw, err = gatewaycli.Read("gloo-system", gatewaydefaults.GatewayProxyName, clients.ReadOpts{})125 Expect(err).NotTo(HaveOccurred())126 gw.Plugins = nil127 _, err = gatewaycli.Write(gw, clients.WriteOpts{OverwriteExisting: true})128 Expect(err).NotTo(HaveOccurred())129 })130 It("can stream metrics", func() {131 vs := getTrivialVirtualServiceForUpstream("default", tu.Upstream.Metadata.Ref())132 _, err := testClients.VirtualServiceClient.Write(vs, clients.WriteOpts{})133 Expect(err).NotTo(HaveOccurred())134 TestUpstreamReachable()135 Expect(<-channel).To(Equal(&struct {136 }{}))137 }, 20)138 })139 })140 })141})...

Full Screen

Full Screen

TestMetricsHandler

Using AI Code Generation

copy

Full Screen

1func main() {2 v1.TestMetricsHandler()3}4func main() {5 v2.TestMetricsHandler()6}7func main() {8 v3.TestMetricsHandler()9}10func main() {11 v4.TestMetricsHandler()12}13func main() {14 v5.TestMetricsHandler()15}16func main() {17 v6.TestMetricsHandler()18}19func main() {20 v7.TestMetricsHandler()21}22func main() {23 v8.TestMetricsHandler()24}25func main() {26 v9.TestMetricsHandler()27}28func main() {29 v10.TestMetricsHandler()30}31func main() {32 v11.TestMetricsHandler()33}34func main() {35 v12.TestMetricsHandler()36}37func main() {38 v13.TestMetricsHandler()39}40func main() {41 v14.TestMetricsHandler()42}43func main() {44 v15.TestMetricsHandler()45}46func main() {47 v16.TestMetricsHandler()48}

Full Screen

Full Screen

TestMetricsHandler

Using AI Code Generation

copy

Full Screen

1func main() {2 v1 := v1.TestMetricsHandler{}3 v1.GetMetrics()4}5func main() {6 v1 := v1.TestMetricsHandler{}7 v1.GetMetrics()8}9func main() {10 v1 := v1.TestMetricsHandler{}11 v1.GetMetrics()12}13func main() {14 v1 := v1.TestMetricsHandler{}15 v1.GetMetrics()16}17func main() {18 v1 := v1.TestMetricsHandler{}19 v1.GetMetrics()20}21func main() {22 v1 := v1.TestMetricsHandler{}23 v1.GetMetrics()24}25func main() {26 v1 := v1.TestMetricsHandler{}27 v1.GetMetrics()28}29func main() {30 v1 := v1.TestMetricsHandler{}31 v1.GetMetrics()32}33func main() {34 v1 := v1.TestMetricsHandler{}35 v1.GetMetrics()36}37func main() {38 v1 := v1.TestMetricsHandler{}39 v1.GetMetrics()40}41func main() {42 v1 := v1.TestMetricsHandler{}43 v1.GetMetrics()44}45func main() {46 v1 := v1.TestMetricsHandler{}47 v1.GetMetrics()48}49func main() {

Full Screen

Full Screen

TestMetricsHandler

Using AI Code Generation

copy

Full Screen

1func TestMetricsHandler(t *testing.T) {2}3func TestMetricsHandler(t *testing.T) {4}5func TestMetricsHandler(t *testing.T) {6}7func TestMetricsHandler(t *testing.T) {8}9func TestMetricsHandler(t *testing.T) {10}11func TestMetricsHandler(t *testing.T) {12}13func TestMetricsHandler(t *testing.T) {14}15func TestMetricsHandler(t *testing.T) {16}17func TestMetricsHandler(t *testing.T) {18}19func TestMetricsHandler(t *testing.T) {20}21func TestMetricsHandler(t *testing.T) {22}23func TestMetricsHandler(t *testing.T) {24}

Full Screen

Full Screen

TestMetricsHandler

Using AI Code Generation

copy

Full Screen

1func main() {2 v1.TestMetricsHandler()3}4func TestMetricsHandler() {5}6func TestMetricsHandler() {7}8func TestMetricsHandler() {9}

Full Screen

Full Screen

TestMetricsHandler

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 v1.TestMetricsHandler()4 fmt.Println("main method")5}6import (7func TestMetricsHandler() {8 fmt.Println("v1 method")9}10import (11func TestMetricsHandler() {12 fmt.Println("v2 method")13}14import (15func main() {16 v1.TestMetricsHandler()17 v2.TestMetricsHandler()18 fmt.Println("main method")19}

Full Screen

Full Screen

TestMetricsHandler

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 r := metrics.NewRegistry()4 m := metrics.NewMeter()5 r.Register("foo", m)6 m.Mark(47)7 exp.Exp(r)8 fmt.Println("Serving on port 8080")9 http.ListenAndServe(":8080", nil)10}11{"foo.m1_rate":0.000000,"foo.m5_rate":0.000000,"foo.m15_rate":0.000000,"foo.mean_rate":0.000000,"foo.count":47}12import (13func main() {14 r := metrics.NewRegistry()15 m := metrics.NewMeter()16 r.Register("foo", m)17 m.Mark(47)18 exp.Exp(r)19 fmt.Println("Serving on port 8080")20 http.ListenAndServe(":8080", nil)21}22{"foo.m1_rate":0.000000,"foo.m5_rate":0.000000,"foo.m15_rate":0.000000,"foo.mean_rate":0.000000,"foo.count":47}23import (

Full Screen

Full Screen

TestMetricsHandler

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 fmt.Println("Hello World")4 v1.TestMetricsHandler()5}6import (7func main() {8 fmt.Println("Hello World")9 v2.TestMetricsHandler()10}

Full Screen

Full Screen

TestMetricsHandler

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 fmt.Println("Hello, playground")4 v1 := metrics.NewV1()5 v1.TestMetricsHandler()6}7type V1 struct {8}9func (v1 *V1) TestMetricsHandler() {10 fmt.Println("Metrics Handler")11}12import (

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Testkube automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Most used method in

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful