Skip to content

Commit 339ed5c

Browse files
committed
Add more unit tests for edge cases
Signed-off-by: Matej Gera <[email protected]>
1 parent 9075a84 commit 339ed5c

File tree

2 files changed

+46
-2
lines changed

2 files changed

+46
-2
lines changed

cmd/otel-allocator/allocation/allocatortest.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,9 @@ func MakeNNewTargetsWithEmptyCollectors(n int, startingIndex int) map[string]*ta
6161
toReturn := map[string]*target.Item{}
6262
for i := startingIndex; i < n+startingIndex; i++ {
6363
label := model.LabelSet{
64-
"i": model.LabelValue(strconv.Itoa(i)),
65-
"total": model.LabelValue(strconv.Itoa(n + startingIndex)),
64+
"i": model.LabelValue(strconv.Itoa(i)),
65+
"total": model.LabelValue(strconv.Itoa(n + startingIndex)),
66+
"__meta_kubernetes_pod_node_name": model.LabelValue("node-0"),
6667
}
6768
newTarget := target.NewItem(fmt.Sprintf("test-job-%d", i), fmt.Sprintf("test-url-%d", i), label, "")
6869
toReturn[newTarget.Hash()] = newTarget

cmd/otel-allocator/allocation/per_node_test.go

+43
Original file line numberDiff line numberDiff line change
@@ -83,3 +83,46 @@ func TestAllocationPerNode(t *testing.T) {
8383
assert.Equal(t, actualItem, itemsForCollector[0])
8484
}
8585
}
86+
87+
func TestTargetsWithNoCollectorsPerNode(t *testing.T) {
88+
// prepare allocator with initial targets and collectors
89+
c, _ := New("per-node", loggerPerNode)
90+
91+
// Adding 10 new targets
92+
numItems := 10
93+
c.SetTargets(MakeNNewTargetsWithEmptyCollectors(numItems, 0))
94+
actualTargetItems := c.TargetItems()
95+
assert.Len(t, actualTargetItems, numItems)
96+
97+
// Adding 5 new targets, and removing the old 10 targets
98+
numItemsUpdate := 5
99+
c.SetTargets(MakeNNewTargetsWithEmptyCollectors(numItemsUpdate, 10))
100+
actualTargetItemsUpdated := c.TargetItems()
101+
assert.Len(t, actualTargetItemsUpdated, numItemsUpdate)
102+
103+
// Adding 5 new targets, and one existing target
104+
numItemsUpdate = 6
105+
c.SetTargets(MakeNNewTargetsWithEmptyCollectors(numItemsUpdate, 14))
106+
actualTargetItemsUpdated = c.TargetItems()
107+
assert.Len(t, actualTargetItemsUpdated, numItemsUpdate)
108+
109+
// Adding collectors to test allocation
110+
numCols := 2
111+
cols := MakeNCollectors(2, 0)
112+
c.SetCollectors(cols)
113+
114+
// Checking to see that there is no change to number of targets
115+
actualTargetItems = c.TargetItems()
116+
assert.Len(t, actualTargetItems, numItemsUpdate)
117+
// Checking to see collectors are added correctly
118+
actualCollectors := c.Collectors()
119+
assert.Len(t, actualCollectors, numCols)
120+
// Based on lable all targets should be assigned to node-0
121+
for name, ac := range actualCollectors {
122+
if name == "node-0" {
123+
assert.Equal(t, 6, ac.NumTargets)
124+
} else {
125+
assert.Equal(t, 0, ac.NumTargets)
126+
}
127+
}
128+
}

0 commit comments

Comments
 (0)