|
|
@@ -71,84 +71,144 @@ func Test_podUIDEnricher(t *testing.T) {
|
|
|
cache := &clustercache.MockClusterCache{
|
|
|
Pods: []*clustercache.Pod{
|
|
|
{UID: "pod-uid-1", Name: "pod1", Namespace: "namespace1"},
|
|
|
+ {UID: "pod-uid-2", Name: "pod2", Namespace: "namespace2"},
|
|
|
},
|
|
|
}
|
|
|
- enrich := podUIDEnricher(cache)
|
|
|
|
|
|
tests := map[string]struct {
|
|
|
- update metric.Update
|
|
|
- want metric.Update
|
|
|
+ labels map[string]string
|
|
|
+ want map[string]string
|
|
|
}{
|
|
|
"resolves pod_uid from namespace and pod": {
|
|
|
- update: metric.Update{
|
|
|
- Labels: map[string]string{
|
|
|
- source.NamespaceLabel: "namespace1",
|
|
|
- source.PodLabel: "pod1",
|
|
|
- },
|
|
|
+ labels: map[string]string{
|
|
|
+ source.NamespaceLabel: "namespace1",
|
|
|
+ source.PodLabel: "pod1",
|
|
|
},
|
|
|
- want: metric.Update{
|
|
|
- Labels: map[string]string{
|
|
|
- source.NamespaceLabel: "namespace1",
|
|
|
- source.PodLabel: "pod1",
|
|
|
- source.PodUIDLabel: "pod-uid-1",
|
|
|
- },
|
|
|
+ want: map[string]string{
|
|
|
+ source.NamespaceLabel: "namespace1",
|
|
|
+ source.PodLabel: "pod1",
|
|
|
+ source.PodUIDLabel: "pod-uid-1",
|
|
|
},
|
|
|
},
|
|
|
"unknown pod is left unset": {
|
|
|
- update: metric.Update{
|
|
|
- Labels: map[string]string{
|
|
|
- source.NamespaceLabel: "namespace1",
|
|
|
- source.PodLabel: "unknown-pod",
|
|
|
- },
|
|
|
+ labels: map[string]string{
|
|
|
+ source.NamespaceLabel: "namespace1",
|
|
|
+ source.PodLabel: "unknown-pod",
|
|
|
},
|
|
|
- want: metric.Update{
|
|
|
- Labels: map[string]string{
|
|
|
- source.NamespaceLabel: "namespace1",
|
|
|
- source.PodLabel: "unknown-pod",
|
|
|
- },
|
|
|
+ want: map[string]string{
|
|
|
+ source.NamespaceLabel: "namespace1",
|
|
|
+ source.PodLabel: "unknown-pod",
|
|
|
},
|
|
|
},
|
|
|
- "missing namespace or pod label is left unset": {
|
|
|
- update: metric.Update{
|
|
|
- Labels: map[string]string{
|
|
|
- source.PodLabel: "pod1",
|
|
|
- },
|
|
|
+ "pod name from wrong namespace is left unset": {
|
|
|
+ labels: map[string]string{
|
|
|
+ source.NamespaceLabel: "namespace2",
|
|
|
+ source.PodLabel: "pod1",
|
|
|
},
|
|
|
- want: metric.Update{
|
|
|
- Labels: map[string]string{
|
|
|
- source.PodLabel: "pod1",
|
|
|
- },
|
|
|
+ want: map[string]string{
|
|
|
+ source.NamespaceLabel: "namespace2",
|
|
|
+ source.PodLabel: "pod1",
|
|
|
},
|
|
|
},
|
|
|
+ "missing namespace label is left unset": {
|
|
|
+ labels: map[string]string{
|
|
|
+ source.PodLabel: "pod1",
|
|
|
+ },
|
|
|
+ want: map[string]string{
|
|
|
+ source.PodLabel: "pod1",
|
|
|
+ },
|
|
|
+ },
|
|
|
+ "missing pod label is left unset": {
|
|
|
+ labels: map[string]string{
|
|
|
+ source.NamespaceLabel: "namespace1",
|
|
|
+ },
|
|
|
+ want: map[string]string{
|
|
|
+ source.NamespaceLabel: "namespace1",
|
|
|
+ },
|
|
|
+ },
|
|
|
+ "nil labels is left untouched": {
|
|
|
+ labels: nil,
|
|
|
+ want: nil,
|
|
|
+ },
|
|
|
"existing non-empty pod_uid is left untouched": {
|
|
|
- update: metric.Update{
|
|
|
- Labels: map[string]string{
|
|
|
- source.NamespaceLabel: "namespace1",
|
|
|
- source.PodLabel: "pod1",
|
|
|
- source.PodUIDLabel: "already-set",
|
|
|
- },
|
|
|
+ labels: map[string]string{
|
|
|
+ source.NamespaceLabel: "namespace1",
|
|
|
+ source.PodLabel: "pod1",
|
|
|
+ source.PodUIDLabel: "already-set",
|
|
|
},
|
|
|
- want: metric.Update{
|
|
|
- Labels: map[string]string{
|
|
|
- source.NamespaceLabel: "namespace1",
|
|
|
- source.PodLabel: "pod1",
|
|
|
- source.PodUIDLabel: "already-set",
|
|
|
- },
|
|
|
+ want: map[string]string{
|
|
|
+ source.NamespaceLabel: "namespace1",
|
|
|
+ source.PodLabel: "pod1",
|
|
|
+ source.PodUIDLabel: "already-set",
|
|
|
},
|
|
|
},
|
|
|
}
|
|
|
|
|
|
for name, tt := range tests {
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
- got := enrich(tt.update)
|
|
|
- if len(got.Labels) != len(tt.want.Labels) {
|
|
|
- t.Fatalf("got labels %v, want %v", got.Labels, tt.want.Labels)
|
|
|
- }
|
|
|
- for k, v := range tt.want.Labels {
|
|
|
- if got.Labels[k] != v {
|
|
|
- t.Errorf("label %q = %q, want %q", k, got.Labels[k], v)
|
|
|
- }
|
|
|
- }
|
|
|
+ enrich := podUIDEnricher(cache)
|
|
|
+ updates := []metric.Update{{Labels: cloneLabels(tt.labels)}}
|
|
|
+
|
|
|
+ enrich(updates)
|
|
|
+
|
|
|
+ assertLabels(t, updates[0].Labels, tt.want)
|
|
|
+ })
|
|
|
+ }
|
|
|
+
|
|
|
+ // A single call must correctly enrich every update in the batch, not just
|
|
|
+ // the first, since updates are processed together rather than one at a time.
|
|
|
+ t.Run("enriches every update in a multi-update batch", func(t *testing.T) {
|
|
|
+ enrich := podUIDEnricher(cache)
|
|
|
+ updates := []metric.Update{
|
|
|
+ {Labels: map[string]string{source.NamespaceLabel: "namespace1", source.PodLabel: "pod1"}},
|
|
|
+ {Labels: map[string]string{source.NamespaceLabel: "namespace2", source.PodLabel: "pod2"}},
|
|
|
+ {Labels: map[string]string{source.NamespaceLabel: "namespace1", source.PodLabel: "unknown-pod"}},
|
|
|
+ }
|
|
|
+
|
|
|
+ enrich(updates)
|
|
|
+
|
|
|
+ assertLabels(t, updates[0].Labels, map[string]string{
|
|
|
+ source.NamespaceLabel: "namespace1",
|
|
|
+ source.PodLabel: "pod1",
|
|
|
+ source.PodUIDLabel: "pod-uid-1",
|
|
|
+ })
|
|
|
+ assertLabels(t, updates[1].Labels, map[string]string{
|
|
|
+ source.NamespaceLabel: "namespace2",
|
|
|
+ source.PodLabel: "pod2",
|
|
|
+ source.PodUIDLabel: "pod-uid-2",
|
|
|
+ })
|
|
|
+ assertLabels(t, updates[2].Labels, map[string]string{
|
|
|
+ source.NamespaceLabel: "namespace1",
|
|
|
+ source.PodLabel: "unknown-pod",
|
|
|
})
|
|
|
+ })
|
|
|
+
|
|
|
+ t.Run("empty batch does not panic", func(t *testing.T) {
|
|
|
+ enrich := podUIDEnricher(cache)
|
|
|
+ enrich(nil)
|
|
|
+ enrich([]metric.Update{})
|
|
|
+ })
|
|
|
+}
|
|
|
+
|
|
|
+func cloneLabels(labels map[string]string) map[string]string {
|
|
|
+ if labels == nil {
|
|
|
+ return nil
|
|
|
+ }
|
|
|
+ clone := make(map[string]string, len(labels))
|
|
|
+ for k, v := range labels {
|
|
|
+ clone[k] = v
|
|
|
+ }
|
|
|
+ return clone
|
|
|
+}
|
|
|
+
|
|
|
+func assertLabels(t *testing.T, got, want map[string]string) {
|
|
|
+ t.Helper()
|
|
|
+ if len(got) != len(want) {
|
|
|
+ t.Fatalf("got labels %v, want %v", got, want)
|
|
|
+ }
|
|
|
+ for k, v := range want {
|
|
|
+ if got[k] != v {
|
|
|
+ t.Errorf("label %q = %q, want %q", k, got[k], v)
|
|
|
+ }
|
|
|
}
|
|
|
}
|