mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-27 13:37:30 +00:00
Merge pull request #47106 from gyliu513/ecache-test
Automatic merge from submit-queue Improved code coverage for equivalence cache. **What this PR does / why we need it**: **Which issue this PR fixes** *(optional, in `fixes #<issue number>(, fixes #<issue_number>, ...)` format, will close that issue when PR gets merged)*: fixes # **Special notes for your reviewer**: **Release note**: ```release-note none ```
This commit is contained in:
commit
b039c6e185
@ -29,31 +29,55 @@ import (
|
|||||||
|
|
||||||
func TestUpdateCachedPredicateItem(t *testing.T) {
|
func TestUpdateCachedPredicateItem(t *testing.T) {
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
name string
|
name string
|
||||||
pod *v1.Pod
|
pod *v1.Pod
|
||||||
predicateKey string
|
predicateKey string
|
||||||
nodeName string
|
nodeName string
|
||||||
fit bool
|
fit bool
|
||||||
reasons []algorithm.PredicateFailureReason
|
reasons []algorithm.PredicateFailureReason
|
||||||
equivalenceHash uint64
|
equivalenceHash uint64
|
||||||
expectCacheItem HostPredicate
|
expectPredicateMap bool
|
||||||
|
expectCacheItem HostPredicate
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
name: "test 1",
|
name: "test 1",
|
||||||
pod: &v1.Pod{ObjectMeta: metav1.ObjectMeta{Name: "testPod"}},
|
pod: &v1.Pod{ObjectMeta: metav1.ObjectMeta{Name: "testPod"}},
|
||||||
predicateKey: "GeneralPredicates",
|
predicateKey: "GeneralPredicates",
|
||||||
nodeName: "node1",
|
nodeName: "node1",
|
||||||
fit: true,
|
fit: true,
|
||||||
equivalenceHash: 123,
|
equivalenceHash: 123,
|
||||||
|
expectPredicateMap: false,
|
||||||
expectCacheItem: HostPredicate{
|
expectCacheItem: HostPredicate{
|
||||||
Fit: true,
|
Fit: true,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
name: "test 2",
|
||||||
|
pod: &v1.Pod{ObjectMeta: metav1.ObjectMeta{Name: "testPod"}},
|
||||||
|
predicateKey: "GeneralPredicates",
|
||||||
|
nodeName: "node2",
|
||||||
|
fit: false,
|
||||||
|
equivalenceHash: 123,
|
||||||
|
expectPredicateMap: true,
|
||||||
|
expectCacheItem: HostPredicate{
|
||||||
|
Fit: false,
|
||||||
|
},
|
||||||
|
},
|
||||||
}
|
}
|
||||||
for _, test := range tests {
|
for _, test := range tests {
|
||||||
// this case does not need to calculate equivalence hash, just pass an empty function
|
// this case does not need to calculate equivalence hash, just pass an empty function
|
||||||
fakeGetEquivalencePodFunc := func(pod *v1.Pod) interface{} { return nil }
|
fakeGetEquivalencePodFunc := func(pod *v1.Pod) interface{} { return nil }
|
||||||
ecache := NewEquivalenceCache(fakeGetEquivalencePodFunc)
|
ecache := NewEquivalenceCache(fakeGetEquivalencePodFunc)
|
||||||
|
if test.expectPredicateMap {
|
||||||
|
ecache.algorithmCache[test.nodeName] = newAlgorithmCache()
|
||||||
|
predicateItem := HostPredicate{
|
||||||
|
Fit: true,
|
||||||
|
}
|
||||||
|
ecache.algorithmCache[test.nodeName].predicatesCache.Add(test.predicateKey,
|
||||||
|
PredicateMap{
|
||||||
|
test.equivalenceHash: predicateItem,
|
||||||
|
})
|
||||||
|
}
|
||||||
ecache.UpdateCachedPredicateItem(test.pod, test.nodeName, test.predicateKey, test.fit, test.reasons, test.equivalenceHash)
|
ecache.UpdateCachedPredicateItem(test.pod, test.nodeName, test.predicateKey, test.fit, test.reasons, test.equivalenceHash)
|
||||||
|
|
||||||
value, ok := ecache.algorithmCache[test.nodeName].predicatesCache.Get(test.predicateKey)
|
value, ok := ecache.algorithmCache[test.nodeName].predicatesCache.Get(test.predicateKey)
|
||||||
@ -73,28 +97,82 @@ type predicateItemType struct {
|
|||||||
reasons []algorithm.PredicateFailureReason
|
reasons []algorithm.PredicateFailureReason
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestInvalidateCachedPredicateItem(t *testing.T) {
|
func TestCachedPredicateItem(t *testing.T) {
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
name string
|
name string
|
||||||
pod *v1.Pod
|
pod *v1.Pod
|
||||||
nodeName string
|
nodeName string
|
||||||
predicateKey string
|
predicateKey string
|
||||||
equivalenceHash uint64
|
equivalenceHashForUpdatePredicate uint64
|
||||||
cachedItem predicateItemType
|
equivalenceHashForCalPredicate uint64
|
||||||
expectedInvalid bool
|
cachedItem predicateItemType
|
||||||
expectedPredicateItem predicateItemType
|
expectedInvalidPredicateKey bool
|
||||||
|
expectedInvalidEquivalenceHash bool
|
||||||
|
expectedPredicateItem predicateItemType
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
name: "test 1",
|
name: "test 1",
|
||||||
pod: &v1.Pod{ObjectMeta: metav1.ObjectMeta{Name: "testPod"}},
|
pod: &v1.Pod{ObjectMeta: metav1.ObjectMeta{Name: "testPod"}},
|
||||||
nodeName: "node1",
|
nodeName: "node1",
|
||||||
equivalenceHash: 123,
|
equivalenceHashForUpdatePredicate: 123,
|
||||||
predicateKey: "GeneralPredicates",
|
equivalenceHashForCalPredicate: 123,
|
||||||
|
predicateKey: "GeneralPredicates",
|
||||||
cachedItem: predicateItemType{
|
cachedItem: predicateItemType{
|
||||||
fit: false,
|
fit: false,
|
||||||
reasons: []algorithm.PredicateFailureReason{predicates.ErrPodNotFitsHostPorts},
|
reasons: []algorithm.PredicateFailureReason{predicates.ErrPodNotFitsHostPorts},
|
||||||
},
|
},
|
||||||
expectedInvalid: true,
|
expectedInvalidPredicateKey: true,
|
||||||
|
expectedPredicateItem: predicateItemType{
|
||||||
|
fit: false,
|
||||||
|
reasons: []algorithm.PredicateFailureReason{},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "test 2",
|
||||||
|
pod: &v1.Pod{ObjectMeta: metav1.ObjectMeta{Name: "testPod"}},
|
||||||
|
nodeName: "node2",
|
||||||
|
equivalenceHashForUpdatePredicate: 123,
|
||||||
|
equivalenceHashForCalPredicate: 123,
|
||||||
|
predicateKey: "GeneralPredicates",
|
||||||
|
cachedItem: predicateItemType{
|
||||||
|
fit: true,
|
||||||
|
},
|
||||||
|
expectedInvalidPredicateKey: false,
|
||||||
|
expectedPredicateItem: predicateItemType{
|
||||||
|
fit: true,
|
||||||
|
reasons: []algorithm.PredicateFailureReason{},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "test 3",
|
||||||
|
pod: &v1.Pod{ObjectMeta: metav1.ObjectMeta{Name: "testPod"}},
|
||||||
|
nodeName: "node3",
|
||||||
|
equivalenceHashForUpdatePredicate: 123,
|
||||||
|
equivalenceHashForCalPredicate: 123,
|
||||||
|
predicateKey: "GeneralPredicates",
|
||||||
|
cachedItem: predicateItemType{
|
||||||
|
fit: false,
|
||||||
|
reasons: []algorithm.PredicateFailureReason{predicates.ErrPodNotFitsHostPorts},
|
||||||
|
},
|
||||||
|
expectedInvalidPredicateKey: false,
|
||||||
|
expectedPredicateItem: predicateItemType{
|
||||||
|
fit: false,
|
||||||
|
reasons: []algorithm.PredicateFailureReason{predicates.ErrPodNotFitsHostPorts},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "test 4",
|
||||||
|
pod: &v1.Pod{ObjectMeta: metav1.ObjectMeta{Name: "testPod"}},
|
||||||
|
nodeName: "node4",
|
||||||
|
equivalenceHashForUpdatePredicate: 123,
|
||||||
|
equivalenceHashForCalPredicate: 456,
|
||||||
|
predicateKey: "GeneralPredicates",
|
||||||
|
cachedItem: predicateItemType{
|
||||||
|
fit: false,
|
||||||
|
reasons: []algorithm.PredicateFailureReason{predicates.ErrPodNotFitsHostPorts},
|
||||||
|
},
|
||||||
|
expectedInvalidPredicateKey: false,
|
||||||
|
expectedInvalidEquivalenceHash: true,
|
||||||
expectedPredicateItem: predicateItemType{
|
expectedPredicateItem: predicateItemType{
|
||||||
fit: false,
|
fit: false,
|
||||||
reasons: []algorithm.PredicateFailureReason{},
|
reasons: []algorithm.PredicateFailureReason{},
|
||||||
@ -107,18 +185,24 @@ func TestInvalidateCachedPredicateItem(t *testing.T) {
|
|||||||
fakeGetEquivalencePodFunc := func(pod *v1.Pod) interface{} { return nil }
|
fakeGetEquivalencePodFunc := func(pod *v1.Pod) interface{} { return nil }
|
||||||
ecache := NewEquivalenceCache(fakeGetEquivalencePodFunc)
|
ecache := NewEquivalenceCache(fakeGetEquivalencePodFunc)
|
||||||
// set cached item to equivalence cache
|
// set cached item to equivalence cache
|
||||||
ecache.UpdateCachedPredicateItem(test.pod, test.nodeName, test.predicateKey, test.cachedItem.fit, test.cachedItem.reasons, test.equivalenceHash)
|
ecache.UpdateCachedPredicateItem(test.pod, test.nodeName, test.predicateKey, test.cachedItem.fit, test.cachedItem.reasons, test.equivalenceHashForUpdatePredicate)
|
||||||
// if we want to do invalid, invalid the cached item
|
// if we want to do invalid, invalid the cached item
|
||||||
if test.expectedInvalid {
|
if test.expectedInvalidPredicateKey {
|
||||||
predicateKeys := sets.NewString()
|
predicateKeys := sets.NewString()
|
||||||
predicateKeys.Insert(test.predicateKey)
|
predicateKeys.Insert(test.predicateKey)
|
||||||
ecache.InvalidateCachedPredicateItem(test.nodeName, predicateKeys)
|
ecache.InvalidateCachedPredicateItem(test.nodeName, predicateKeys)
|
||||||
}
|
}
|
||||||
// calculate predicate with equivalence cache
|
// calculate predicate with equivalence cache
|
||||||
fit, reasons, invalid := ecache.PredicateWithECache(test.pod, test.nodeName, test.predicateKey, test.equivalenceHash)
|
fit, reasons, invalid := ecache.PredicateWithECache(test.pod, test.nodeName, test.predicateKey, test.equivalenceHashForCalPredicate)
|
||||||
// returned invalid should match expectedInvalid
|
// returned invalid should match expectedInvalidPredicateKey or expectedInvalidEquivalenceHash
|
||||||
if invalid != test.expectedInvalid {
|
if test.equivalenceHashForUpdatePredicate != test.equivalenceHashForCalPredicate {
|
||||||
t.Errorf("Failed : %s, expected invalid: %v, but got: %v", test.name, test.expectedInvalid, invalid)
|
if invalid != test.expectedInvalidEquivalenceHash {
|
||||||
|
t.Errorf("Failed : %s when using invalid equivalenceHash, expected invalid: %v, but got: %v", test.name, test.expectedInvalidEquivalenceHash, invalid)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if invalid != test.expectedInvalidPredicateKey {
|
||||||
|
t.Errorf("Failed : %s, expected invalid: %v, but got: %v", test.name, test.expectedInvalidPredicateKey, invalid)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
// returned predicate result should match expected predicate item
|
// returned predicate result should match expected predicate item
|
||||||
if fit != test.expectedPredicateItem.fit {
|
if fit != test.expectedPredicateItem.fit {
|
||||||
|
Loading…
Reference in New Issue
Block a user