// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. // SPDX-License-Identifier: MIT package extractors import ( "fmt" "reflect" "testing" "github.com/stretchr/testify/assert" ) const ( CurInfo = ` { "/kubepods/besteffort/podaf16b540-4ae2-11e9-977b-0672b6c6fc94/573ee6cd04a6208af809b2329652c74386f1992faca8662c733d7f250014e718": { "id": "573ee6cd04a6208af809b2329652c74386f1992faca8662c733d7f250014e718", "name": "/kubepods/besteffort/podaf16b540-4ae2-11e9-977b-0672b6c6fc94/573ee6cd04a6208af809b2329652c74386f1992faca8662c733d7f250014e718", "aliases": [ "k8s_ubuntu_stress-1-core-mh2pn_default_af16b540-4ae2-11e9-977b-0672b6c6fc94_0", "573ee6cd04a6208af809b2329652c74386f1992faca8662c733d7f250014e718" ], "namespace": "docker", "spec": { "creation_time": "2019-03-20T07:35:09.746280405Z", "labels": { "annotation.io.kubernetes.container.hash": "70bfcd85", "annotation.io.kubernetes.container.restartCount": "0", "annotation.io.kubernetes.container.terminationMessagePath": "/dev/termination-log", "annotation.io.kubernetes.container.terminationMessagePolicy": "File", "annotation.io.kubernetes.pod.terminationGracePeriod": "30", "io.kubernetes.container.logpath": "/var/log/pods/af16b540-4ae2-11e9-977b-0672b6c6fc94/ubuntu/0.log", "io.kubernetes.container.name": "ubuntu", "io.kubernetes.docker.type": "container", "io.kubernetes.pod.name": "stress-1-core-mh2pn", "io.kubernetes.pod.namespace": "default", "io.kubernetes.pod.uid": "af16b540-4ae2-11e9-977b-0672b6c6fc94", "io.kubernetes.sandbox.id": "a5bb552d7fb8e5014468756f165732e0c6bcd9dcbd229efc51afc014317d20d6" }, "has_cpu": true, "cpu": { "limit": 2, "max_limit": 0, "mask": "0-3", "period": 100000 }, "has_memory": true, "memory": { "limit": 9223372036854771712, "reservation": 9223372036854771712, "swap_limit": 9223372036854771712 }, "has_network": false, "has_filesystem": true, "has_diskio": true, "has_custom_metrics": false, "image": "ubuntu@sha256:017eef0b616011647b269b5c65826e2e2ebddbe5d1f8c1e56b3599fb14fabec8" }, "stats": [ { "timestamp": "2019-04-09T22:26:42.984081498Z", "cpu": { "usage": { "total": 1727429404576983, "user": 1005797430000000, "system": 183880000000 }, "cfs": { "periods": 0, "throttled_periods": 0, "throttled_time": 0 }, "schedstat": { "run_time": 0, "runqueue_time": 0, "run_periods": 0 }, "load_average": 0 }, "diskio": { "io_service_bytes": [ { "device": "/dev/xvda", "major": 202, "minor": 0, "stats": { "Async": 46864, "Read": 10, "Sync": 1690896, "Total": 1727770, "Write": 1727760 } } ], "io_serviced": [ { "device": "/dev/xvda", "major": 202, "minor": 0, "stats": { "Async": 14, "Read": 10, "Sync": 59, "Total": 73, "Write": 63 } } ] }, "memory": { "usage": 29728768, "max_usage": 90775552, "cache": 25645056, "rss": 221184, "swap": 0, "working_set": 28844032, "failcnt": 0, "container_data": { "pgfault": 98634, "pgmajfault": 10 }, "hierarchical_data": { "pgfault": 98634, "pgmajfault": 10 } }, "network": { "name": "", "rx_bytes": 0, "rx_packets": 0, "rx_errors": 0, "rx_dropped": 0, "tx_bytes": 0, "tx_packets": 0, "tx_errors": 0, "tx_dropped": 0, "tcp": { "Established": 0, "SynSent": 0, "SynRecv": 0, "FinWait1": 0, "FinWait2": 0, "TimeWait": 0, "Close": 0, "CloseWait": 0, "LastAck": 0, "Listen": 0, "Closing": 0 }, "tcp6": { "Established": 0, "SynSent": 0, "SynRecv": 0, "FinWait1": 0, "FinWait2": 0, "TimeWait": 0, "Close": 0, "CloseWait": 0, "LastAck": 0, "Listen": 0, "Closing": 0 }, "udp": { "Listen": 0, "Dropped": 0, "RxQueued": 0, "TxQueued": 0 }, "udp6": { "Listen": 0, "Dropped": 0, "RxQueued": 0, "TxQueued": 0 } }, "filesystem": [ { "device": "/dev/xvda1", "type": "vfs", "capacity": 21462233088, "usage": 25661440, "base_usage": 25640960, "available": 0, "has_inodes": false, "inodes": 67, "inodes_free": 0, "reads_completed": 0, "reads_merged": 0, "sectors_read": 0, "read_time": 0, "writes_completed": 0, "writes_merged": 0, "sectors_written": 0, "write_time": 0, "io_in_progress": 0, "io_time": 0, "weighted_io_time": 0 } ], "task_stats": { "nr_sleeping": 0, "nr_running": 0, "nr_stopped": 0, "nr_uninterruptible": 0, "nr_io_wait": 0 } } ] } } ` PreInfo = ` { "/kubepods/besteffort/podaf16b540-4ae2-11e9-977b-0672b6c6fc94/573ee6cd04a6208af809b2329652c74386f1992faca8662c733d7f250014e718": { "id": "573ee6cd04a6208af809b2329652c74386f1992faca8662c733d7f250014e718", "name": "/kubepods/besteffort/podaf16b540-4ae2-11e9-977b-0672b6c6fc94/573ee6cd04a6208af809b2329652c74386f1992faca8662c733d7f250014e718", "aliases": [ "k8s_ubuntu_stress-1-core-mh2pn_default_af16b540-4ae2-11e9-977b-0672b6c6fc94_0", "573ee6cd04a6208af809b2329652c74386f1992faca8662c733d7f250014e718" ], "namespace": "docker", "spec": { "creation_time": "2019-03-20T07:35:09.746280405Z", "labels": { "annotation.io.kubernetes.container.hash": "70bfcd85", "annotation.io.kubernetes.container.restartCount": "0", "annotation.io.kubernetes.container.terminationMessagePath": "/dev/termination-log", "annotation.io.kubernetes.container.terminationMessagePolicy": "File", "annotation.io.kubernetes.pod.terminationGracePeriod": "30", "io.kubernetes.container.logpath": "/var/log/pods/af16b540-4ae2-11e9-977b-0672b6c6fc94/ubuntu/0.log", "io.kubernetes.container.name": "ubuntu", "io.kubernetes.docker.type": "container", "io.kubernetes.pod.name": "stress-1-core-mh2pn", "io.kubernetes.pod.namespace": "default", "io.kubernetes.pod.uid": "af16b540-4ae2-11e9-977b-0672b6c6fc94", "io.kubernetes.sandbox.id": "a5bb552d7fb8e5014468756f165732e0c6bcd9dcbd229efc51afc014317d20d6" }, "has_cpu": true, "cpu": { "limit": 2, "max_limit": 0, "mask": "0-3", "period": 100000 }, "has_memory": true, "memory": { "limit": 9223372036854771712, "reservation": 9223372036854771712, "swap_limit": 9223372036854771712 }, "has_network": false, "has_filesystem": true, "has_diskio": true, "has_custom_metrics": false, "image": "ubuntu@sha256:017eef0b616011647b269b5c65826e2e2ebddbe5d1f8c1e56b3599fb14fabec8" }, "stats": [ { "timestamp": "2019-04-09T22:26:41.984081498Z", "cpu": { "usage": { "total": 1727429394576983, "user": 1005797420000000, "system": 183870000000 }, "cfs": { "periods": 0, "throttled_periods": 0, "throttled_time": 0 }, "schedstat": { "run_time": 0, "runqueue_time": 0, "run_periods": 0 }, "load_average": 0 }, "diskio": { "io_service_bytes": [ { "device": "/dev/xvda", "major": 202, "minor": 0, "stats": { "Async": 36864, "Read": 0, "Sync": 1680896, "Total": 1717760, "Write": 1717760 } } ], "io_serviced": [ { "device": "/dev/xvda", "major": 202, "minor": 0, "stats": { "Async": 4, "Read": 0, "Sync": 49, "Total": 53, "Write": 53 } } ] }, "memory": { "usage": 29728768, "max_usage": 90775552, "cache": 25645056, "rss": 221184, "swap": 0, "working_set": 28844032, "failcnt": 0, "container_data": { "pgfault": 97634, "pgmajfault": 0 }, "hierarchical_data": { "pgfault": 97634, "pgmajfault": 0 } }, "network": { "name": "", "rx_bytes": 0, "rx_packets": 0, "rx_errors": 0, "rx_dropped": 0, "tx_bytes": 0, "tx_packets": 0, "tx_errors": 0, "tx_dropped": 0, "tcp": { "Established": 0, "SynSent": 0, "SynRecv": 0, "FinWait1": 0, "FinWait2": 0, "TimeWait": 0, "Close": 0, "CloseWait": 0, "LastAck": 0, "Listen": 0, "Closing": 0 }, "tcp6": { "Established": 0, "SynSent": 0, "SynRecv": 0, "FinWait1": 0, "FinWait2": 0, "TimeWait": 0, "Close": 0, "CloseWait": 0, "LastAck": 0, "Listen": 0, "Closing": 0 }, "udp": { "Listen": 0, "Dropped": 0, "RxQueued": 0, "TxQueued": 0 }, "udp6": { "Listen": 0, "Dropped": 0, "RxQueued": 0, "TxQueued": 0 } }, "filesystem": [ { "device": "/dev/xvda1", "type": "vfs", "capacity": 21462233088, "usage": 25661440, "base_usage": 25640960, "available": 0, "has_inodes": false, "inodes": 67, "inodes_free": 0, "reads_completed": 0, "reads_merged": 0, "sectors_read": 0, "read_time": 0, "writes_completed": 0, "writes_merged": 0, "sectors_written": 0, "write_time": 0, "io_in_progress": 0, "io_time": 0, "weighted_io_time": 0 } ], "task_stats": { "nr_sleeping": 0, "nr_running": 0, "nr_stopped": 0, "nr_uninterruptible": 0, "nr_io_wait": 0 } } ] } } ` ) func AssertContainsTaggedFloat( t *testing.T, cadvisorMetric *CAdvisorMetric, field string, expectedValue float64, delta float64, ) { var actualValue float64 fields := cadvisorMetric.GetFields() if val, ok := fields[field]; ok { if val, ok := val.(float64); ok { actualValue = val if (val >= expectedValue-delta) && (val <= expectedValue+delta) { //Found the point, return without failing return } } else { assert.Fail(t, fmt.Sprintf("Field \"%s\" does not have type float64", field)) } } msg := fmt.Sprintf( "Could not find field \"%s\" with requested tags within %f of %f, Actual: %f", field, delta, expectedValue, actualValue) assert.Fail(t, msg) } func AssertContainsTaggedField( t *testing.T, cadvisorMetric *CAdvisorMetric, expectedFields map[string]interface{}, expectedTags map[string]string, ) { actualFields := cadvisorMetric.GetFields() actualTags := cadvisorMetric.GetTags() if !reflect.DeepEqual(expectedTags, actualTags) { msg := fmt.Sprintf("No field exists for metric %v", *cadvisorMetric) assert.Fail(t, msg) } if len(actualFields) > 0 { assert.Equal(t, expectedFields, actualFields) return } msg := fmt.Sprintf("No field exists for metric %v", *cadvisorMetric) assert.Fail(t, msg) }