1
16
17 package metrics
18
19 import (
20 "testing"
21
22 discovery "k8s.io/api/discovery/v1"
23 "k8s.io/apimachinery/pkg/types"
24 endpointsliceutil "k8s.io/endpointslice/util"
25 )
26
27 func TestNumEndpointsAndSlices(t *testing.T) {
28 c := NewCache(int32(100))
29
30 p80 := int32(80)
31 p443 := int32(443)
32
33 pmKey80443 := endpointsliceutil.NewPortMapKey([]discovery.EndpointPort{{Port: &p80}, {Port: &p443}})
34 pmKey80 := endpointsliceutil.NewPortMapKey([]discovery.EndpointPort{{Port: &p80}})
35
36 spCacheEfficient := NewEndpointPortCache()
37 spCacheEfficient.Set(pmKey80, EfficiencyInfo{Endpoints: 45, Slices: 1})
38 spCacheEfficient.Set(pmKey80443, EfficiencyInfo{Endpoints: 35, Slices: 1})
39
40 spCacheInefficient := NewEndpointPortCache()
41 spCacheInefficient.Set(pmKey80, EfficiencyInfo{Endpoints: 12, Slices: 5})
42 spCacheInefficient.Set(pmKey80443, EfficiencyInfo{Endpoints: 18, Slices: 8})
43
44 c.UpdateEndpointPortCache(types.NamespacedName{Namespace: "ns1", Name: "svc1"}, spCacheInefficient)
45 expectNumEndpointsAndSlices(t, c, 2, 13, 30)
46
47 c.UpdateEndpointPortCache(types.NamespacedName{Namespace: "ns1", Name: "svc2"}, spCacheEfficient)
48 expectNumEndpointsAndSlices(t, c, 4, 15, 110)
49
50 c.UpdateEndpointPortCache(types.NamespacedName{Namespace: "ns1", Name: "svc3"}, spCacheInefficient)
51 expectNumEndpointsAndSlices(t, c, 6, 28, 140)
52
53 c.UpdateEndpointPortCache(types.NamespacedName{Namespace: "ns1", Name: "svc1"}, spCacheEfficient)
54 expectNumEndpointsAndSlices(t, c, 6, 17, 190)
55
56 c.DeleteEndpoints(types.NamespacedName{Namespace: "ns1", Name: "svc3"})
57 expectNumEndpointsAndSlices(t, c, 4, 4, 160)
58 }
59
60 func expectNumEndpointsAndSlices(t *testing.T, c *Cache, desired int, actual int, numEndpoints int) {
61 t.Helper()
62 mUpdate := c.desiredAndActualSlices()
63 if mUpdate.desired != desired {
64 t.Errorf("Expected numEndpointSlices to be %d, got %d", desired, mUpdate.desired)
65 }
66 if mUpdate.actual != actual {
67 t.Errorf("Expected desiredEndpointSlices to be %d, got %d", actual, mUpdate.actual)
68 }
69 if c.numEndpoints != numEndpoints {
70 t.Errorf("Expected numEndpoints to be %d, got %d", numEndpoints, c.numEndpoints)
71 }
72 }
73
View as plain text