|
| 1 | +/* |
| 2 | +Copyright 2019 The Kubernetes Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package collectors |
| 18 | + |
| 19 | +import ( |
| 20 | + "fmt" |
| 21 | + "strings" |
| 22 | + "testing" |
| 23 | + "time" |
| 24 | + |
| 25 | + "github.com/stretchr/testify/mock" |
| 26 | + |
| 27 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 28 | + "k8s.io/component-base/metrics/testutil" |
| 29 | + statsapi "k8s.io/kubernetes/pkg/kubelet/apis/stats/v1alpha1" |
| 30 | +) |
| 31 | + |
| 32 | +type mockSummaryProvider struct { |
| 33 | + mock.Mock |
| 34 | +} |
| 35 | + |
| 36 | +func (m *mockSummaryProvider) Get(updateStats bool) (*statsapi.Summary, error) { |
| 37 | + args := m.Called(updateStats) |
| 38 | + return args.Get(0).(*statsapi.Summary), args.Error(1) |
| 39 | +} |
| 40 | + |
| 41 | +func (m *mockSummaryProvider) GetCPUAndMemoryStats() (*statsapi.Summary, error) { |
| 42 | + args := m.Called() |
| 43 | + return args.Get(0).(*statsapi.Summary), args.Error(1) |
| 44 | +} |
| 45 | + |
| 46 | +func TestCollectResourceMetrics(t *testing.T) { |
| 47 | + testTime := metav1.NewTime(time.Unix(2, 0)) // a static timestamp: 2000 |
| 48 | + interestedMetrics := []string{ |
| 49 | + "scrape_error", |
| 50 | + "node_cpu_usage_seconds", |
| 51 | + "node_memory_working_set_bytes", |
| 52 | + "container_cpu_usage_seconds", |
| 53 | + "container_memory_working_set_bytes", |
| 54 | + } |
| 55 | + |
| 56 | + tests := []struct { |
| 57 | + name string |
| 58 | + summary *statsapi.Summary |
| 59 | + summaryErr error |
| 60 | + expectedMetrics string |
| 61 | + }{ |
| 62 | + { |
| 63 | + name: "error getting summary", |
| 64 | + summary: nil, |
| 65 | + summaryErr: fmt.Errorf("failed to get summary"), |
| 66 | + expectedMetrics: ` |
| 67 | + # HELP scrape_error [ALPHA] 1 if there was an error while getting container metrics, 0 otherwise |
| 68 | + # TYPE scrape_error gauge |
| 69 | + scrape_error 1 |
| 70 | + `, |
| 71 | + }, |
| 72 | + { |
| 73 | + name: "arbitrary node metrics", |
| 74 | + summary: &statsapi.Summary{ |
| 75 | + Node: statsapi.NodeStats{ |
| 76 | + CPU: &statsapi.CPUStats{ |
| 77 | + Time: testTime, |
| 78 | + UsageCoreNanoSeconds: uint64Ptr(10000000000), |
| 79 | + }, |
| 80 | + Memory: &statsapi.MemoryStats{ |
| 81 | + Time: testTime, |
| 82 | + WorkingSetBytes: uint64Ptr(1000), |
| 83 | + }, |
| 84 | + }, |
| 85 | + }, |
| 86 | + summaryErr: nil, |
| 87 | + expectedMetrics: ` |
| 88 | + # HELP node_cpu_usage_seconds [ALPHA] Cumulative cpu time consumed by the node in core-seconds |
| 89 | + # TYPE node_cpu_usage_seconds gauge |
| 90 | + node_cpu_usage_seconds 10 2000 |
| 91 | + # HELP node_memory_working_set_bytes [ALPHA] Current working set of the node in bytes |
| 92 | + # TYPE node_memory_working_set_bytes gauge |
| 93 | + node_memory_working_set_bytes 1000 2000 |
| 94 | + # HELP scrape_error [ALPHA] 1 if there was an error while getting container metrics, 0 otherwise |
| 95 | + # TYPE scrape_error gauge |
| 96 | + scrape_error 0 |
| 97 | + `, |
| 98 | + }, |
| 99 | + { |
| 100 | + name: "arbitrary container metrics for different container, pods and namespaces", |
| 101 | + summary: &statsapi.Summary{ |
| 102 | + Pods: []statsapi.PodStats{ |
| 103 | + { |
| 104 | + PodRef: statsapi.PodReference{ |
| 105 | + Name: "pod_a", |
| 106 | + Namespace: "namespace_a", |
| 107 | + }, |
| 108 | + Containers: []statsapi.ContainerStats{ |
| 109 | + { |
| 110 | + Name: "container_a", |
| 111 | + CPU: &statsapi.CPUStats{ |
| 112 | + Time: testTime, |
| 113 | + UsageCoreNanoSeconds: uint64Ptr(10000000000), |
| 114 | + }, |
| 115 | + Memory: &statsapi.MemoryStats{ |
| 116 | + Time: testTime, |
| 117 | + WorkingSetBytes: uint64Ptr(1000), |
| 118 | + }, |
| 119 | + }, |
| 120 | + { |
| 121 | + Name: "container_b", |
| 122 | + CPU: &statsapi.CPUStats{ |
| 123 | + Time: testTime, |
| 124 | + UsageCoreNanoSeconds: uint64Ptr(10000000000), |
| 125 | + }, |
| 126 | + Memory: &statsapi.MemoryStats{ |
| 127 | + Time: testTime, |
| 128 | + WorkingSetBytes: uint64Ptr(1000), |
| 129 | + }, |
| 130 | + }, |
| 131 | + }, |
| 132 | + }, |
| 133 | + { |
| 134 | + PodRef: statsapi.PodReference{ |
| 135 | + Name: "pod_b", |
| 136 | + Namespace: "namespace_b", |
| 137 | + }, |
| 138 | + Containers: []statsapi.ContainerStats{ |
| 139 | + { |
| 140 | + Name: "container_a", |
| 141 | + CPU: &statsapi.CPUStats{ |
| 142 | + Time: testTime, |
| 143 | + UsageCoreNanoSeconds: uint64Ptr(10000000000), |
| 144 | + }, |
| 145 | + Memory: &statsapi.MemoryStats{ |
| 146 | + Time: testTime, |
| 147 | + WorkingSetBytes: uint64Ptr(1000), |
| 148 | + }, |
| 149 | + }, |
| 150 | + }, |
| 151 | + }, |
| 152 | + }, |
| 153 | + }, |
| 154 | + summaryErr: nil, |
| 155 | + expectedMetrics: ` |
| 156 | + # HELP scrape_error [ALPHA] 1 if there was an error while getting container metrics, 0 otherwise |
| 157 | + # TYPE scrape_error gauge |
| 158 | + scrape_error 0 |
| 159 | + # HELP container_cpu_usage_seconds [ALPHA] Cumulative cpu time consumed by the container in core-seconds |
| 160 | + # TYPE container_cpu_usage_seconds gauge |
| 161 | + container_cpu_usage_seconds{container="container_a",namespace="namespace_a",pod="pod_a"} 10 2000 |
| 162 | + container_cpu_usage_seconds{container="container_a",namespace="namespace_b",pod="pod_b"} 10 2000 |
| 163 | + container_cpu_usage_seconds{container="container_b",namespace="namespace_a",pod="pod_a"} 10 2000 |
| 164 | + # HELP container_memory_working_set_bytes [ALPHA] Current working set of the container in bytes |
| 165 | + # TYPE container_memory_working_set_bytes gauge |
| 166 | + container_memory_working_set_bytes{container="container_a",namespace="namespace_a",pod="pod_a"} 1000 2000 |
| 167 | + container_memory_working_set_bytes{container="container_a",namespace="namespace_b",pod="pod_b"} 1000 2000 |
| 168 | + container_memory_working_set_bytes{container="container_b",namespace="namespace_a",pod="pod_a"} 1000 2000 |
| 169 | + `, |
| 170 | + }, |
| 171 | + } |
| 172 | + |
| 173 | + for _, test := range tests { |
| 174 | + tc := test |
| 175 | + t.Run(tc.name, func(t *testing.T) { |
| 176 | + provider := &mockSummaryProvider{} |
| 177 | + provider.On("GetCPUAndMemoryStats").Return(tc.summary, tc.summaryErr) |
| 178 | + collector := NewResourceMetricsCollector(provider) |
| 179 | + |
| 180 | + if err := testutil.CustomCollectAndCompare(collector, strings.NewReader(tc.expectedMetrics), interestedMetrics...); err != nil { |
| 181 | + t.Fatal(err) |
| 182 | + } |
| 183 | + }) |
| 184 | + } |
| 185 | +} |
| 186 | + |
| 187 | +func uint64Ptr(u uint64) *uint64 { |
| 188 | + return &u |
| 189 | +} |
0 commit comments