add test case for clear()

This commit is contained in:
Aleksandra Malinowska 2020-02-12 13:45:27 +01:00
parent 3ac9bdd57b
commit 51ae036ba6
2 changed files with 81 additions and 4 deletions

View File

@ -27,10 +27,10 @@ import (
apiv1 "k8s.io/api/core/v1"
)
func createTestNodesWithPrefix(name string, n int) []*apiv1.Node {
func createTestNodesWithPrefix(prefix string, n int) []*apiv1.Node {
nodes := make([]*apiv1.Node, n, n)
for i := 0; i < n; i++ {
nodes[i] = BuildTestNode(fmt.Sprintf("%s-%d", name, i), 2000, 2000000)
nodes[i] = BuildTestNode(fmt.Sprintf("%s-%d", prefix, i), 2000, 2000000)
SetNodeReadyState(nodes[i], true, time.Time{})
}
return nodes
@ -40,14 +40,18 @@ func createTestNodes(n int) []*apiv1.Node {
return createTestNodesWithPrefix("n", n)
}
func createTestPods(n int) []*apiv1.Pod {
func createTestPodsWithPrefix(prefix string, n int) []*apiv1.Pod {
pods := make([]*apiv1.Pod, n, n)
for i := 0; i < n; i++ {
pods[i] = BuildTestPod(fmt.Sprintf("p-%d", i), 1000, 2000000)
pods[i] = BuildTestPod(fmt.Sprintf("%s-%d", prefix, i), 1000, 2000000)
}
return pods
}
func createTestPods(n int) []*apiv1.Pod {
return createTestPodsWithPrefix("p", n)
}
func assignPodsToNodes(pods []*apiv1.Pod, nodes []*apiv1.Node) {
j := 0
for i := 0; i < len(pods); i++ {

View File

@ -18,7 +18,9 @@ package simulator
import (
"fmt"
"math/rand"
"testing"
"time"
. "k8s.io/autoscaler/cluster-autoscaler/utils/test"
@ -192,6 +194,77 @@ func TestForking(t *testing.T) {
}
}
func TestClear(t *testing.T) {
// Run with -count=1 to avoid caching.
localRand := rand.New(rand.NewSource(time.Now().Unix()))
nodeCount := localRand.Intn(100)
podCount := localRand.Intn(1000)
extraNodeCount := localRand.Intn(100)
extraPodCount := localRand.Intn(1000)
nodes := createTestNodes(nodeCount)
pods := createTestPods(podCount)
assignPodsToNodes(pods, nodes)
state := snapshotState{nodes, pods}
extraNodes := createTestNodesWithPrefix("extra", extraNodeCount)
allNodes := make([]*apiv1.Node, len(nodes)+len(extraNodes), len(nodes)+len(extraNodes))
copy(allNodes, nodes)
copy(allNodes[len(nodes):], extraNodes)
extraPods := createTestPodsWithPrefix("extra", extraPodCount)
assignPodsToNodes(extraPods, allNodes)
allPods := make([]*apiv1.Pod, len(pods)+len(extraPods), len(pods)+len(extraPods))
copy(allPods, pods)
copy(allPods[len(pods):], extraPods)
for name, snapshotFactory := range snapshots {
t.Run(fmt.Sprintf("%s: clear base %d nodes %d pods", name, nodeCount, podCount),
func(t *testing.T) {
snapshot := startSnapshot(t, snapshotFactory, state)
compareStates(t, state, getSnapshotState(t, snapshot))
snapshot.Clear()
compareStates(t, snapshotState{}, getSnapshotState(t, snapshot))
})
t.Run(fmt.Sprintf("%s: clear fork %d nodes %d pods %d extra nodes %d extra pods", name, nodeCount, podCount, extraNodeCount, extraPodCount),
func(t *testing.T) {
snapshot := startSnapshot(t, snapshotFactory, state)
compareStates(t, state, getSnapshotState(t, snapshot))
err := snapshot.Fork()
assert.NoError(t, err)
err = snapshot.AddNodes(extraNodes)
assert.NoError(t, err)
for _, pod := range extraPods {
err := snapshot.AddPod(pod, pod.Spec.NodeName)
assert.NoError(t, err)
}
compareStates(t, snapshotState{allNodes, allPods}, getSnapshotState(t, snapshot))
// Fork()ing twice is not allowed.
err = snapshot.Fork()
assert.Error(t, err)
snapshot.Clear()
compareStates(t, snapshotState{}, getSnapshotState(t, snapshot))
// Clear() should break out of forked state.
err = snapshot.Fork()
assert.NoError(t, err)
})
}
}
func TestNode404(t *testing.T) {
// Anything and everything that returns errNodeNotFound should be tested here.
ops := []struct {