-
Notifications
You must be signed in to change notification settings - Fork 1
/
03_pods_test.go
114 lines (100 loc) · 2.19 KB
/
03_pods_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package playing
import (
"iter"
"maps"
"slices"
"testing"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
)
var pods = []corev1.Pod{
{
ObjectMeta: metav1.ObjectMeta{
Name: "pod-1",
UID: types.UID("11111111-1111-1111-1111-111111111111"),
Annotations: map[string]string{
"pick-me": "true",
},
},
},
{
ObjectMeta: metav1.ObjectMeta{
Name: "pod-2",
UID: types.UID("222222222-2222-2222-2222-22222222222"),
},
},
{
ObjectMeta: metav1.ObjectMeta{
Name: "pod-3",
UID: types.UID("33333333-3333-3333-3333-333333333333"),
Annotations: map[string]string{
"pick-me": "true",
},
},
},
{
ObjectMeta: metav1.ObjectMeta{
Name: "pod-4",
UID: types.UID("444444444-4444-4444-4444-44444444444"),
},
},
}
func filter[T any](seq iter.Seq[T], filterFn func(T) bool) iter.Seq[T] {
return func(yield func(T) bool) {
for v := range seq {
if filterFn(v) {
if !yield(v) {
return
}
}
}
}
}
func TestFilterSlice(t *testing.T) {
itr := slices.Values(pods)
filteredItr := filter(itr, func(pod corev1.Pod) bool {
return metav1.HasAnnotation(pod.ObjectMeta, "pick-me")
})
for v := range filteredItr {
t.Logf("%#v\n", v)
}
s := slices.Collect(filteredItr)
t.Logf("%#v\n", s)
}
func transform[K, V1, V2 any](seq iter.Seq[V1], transFn func(V1) (K, V2)) iter.Seq2[K, V2] {
return func(yield func(K, V2) bool) {
for v1 := range seq {
if !yield(transFn(v1)) {
return
}
}
}
}
func TestSliceToMap(t *testing.T) {
itr := slices.Values(pods)
podsByName := maps.Collect(transform(itr, func(pod corev1.Pod) (string, corev1.Pod) {
return pod.Name, pod
}))
t.Logf("%#v\n", podsByName)
nameByUID := maps.Collect(transform(itr, func(pod corev1.Pod) (types.UID, string) {
return pod.UID, pod.Name
}))
t.Logf("%#v\n", nameByUID)
}
func TestFilterAndTransform(t *testing.T) {
pickMe := func(pod corev1.Pod) bool {
return metav1.HasAnnotation(pod.ObjectMeta, "pick-me")
}
podNameUID := func(pod corev1.Pod) (string, types.UID) {
return pod.Name, pod.UID
}
m := maps.Collect(
transform(
filter(
slices.Values(pods),
pickMe),
podNameUID),
)
t.Logf("%#v\n", m)
}