@@ -37,6 +37,7 @@ import (
37
37
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
38
38
apiruntime "k8s.io/apimachinery/pkg/runtime"
39
39
"k8s.io/apimachinery/pkg/types"
40
+ "k8s.io/apimachinery/pkg/util/wait"
40
41
"k8s.io/client-go/informers"
41
42
"k8s.io/client-go/kubernetes/fake"
42
43
cgotesting "k8s.io/client-go/testing"
@@ -1317,7 +1318,7 @@ func TestPlugin(t *testing.T) {
1317
1318
},
1318
1319
postfilter : result {
1319
1320
status : fwk .NewStatus (fwk .Unschedulable , `deletion of ResourceClaim completed` ),
1320
- removed : []metav1.Object {extendedResourceClaimNode2 },
1321
+ removed : []metav1.Object {extendedResourceClaim },
1321
1322
},
1322
1323
},
1323
1324
},
@@ -1326,6 +1327,7 @@ func TestPlugin(t *testing.T) {
1326
1327
pod : podWithExtendedResourceName ,
1327
1328
claims : []* resourceapi.ResourceClaim {extendedResourceClaimNode2 },
1328
1329
classes : []* resourceapi.DeviceClass {deviceClassWithExtendResourceName },
1330
+ objs : []apiruntime.Object {workerNodeSlice , podWithExtendedResourceName },
1329
1331
want : want {
1330
1332
filter : perNodeResult {
1331
1333
workerNode .Name : {
@@ -1827,10 +1829,6 @@ func TestPlugin(t *testing.T) {
1827
1829
initialObjects = testCtx .listAll (t )
1828
1830
testCtx .p .Unreserve (testCtx .ctx , testCtx .state , tc .pod , selectedNode .Node ().Name )
1829
1831
t .Run ("unreserverAfterBindFailure" , func (t * testing.T ) {
1830
- // in case we delete the claim API object
1831
- // wait for assumed cache to sync with informer
1832
- // then assumed cache should be empty
1833
- time .Sleep (800 * time .Millisecond )
1834
1832
testCtx .verify (t , * tc .want .unreserveAfterBindFailure , initialObjects , nil , status )
1835
1833
})
1836
1834
} else if status .IsSuccess () {
@@ -1905,9 +1903,42 @@ func (tc *testContext) verify(t *testing.T, expected result, initialObjects []me
1905
1903
if expected .assumedClaim != nil {
1906
1904
expectAssumedClaims = append (expectAssumedClaims , expected .assumedClaim )
1907
1905
}
1908
- actualAssumedClaims := tc .listAssumedClaims ()
1909
- if diff := cmp .Diff (expectAssumedClaims , actualAssumedClaims , ignoreFieldsInResourceClaims ... ); diff != "" {
1910
- t .Errorf ("Assumed claims are different (- expected, + actual):\n %s" , diff )
1906
+ // actualAssumedClaims are claims in assumed cache with different latest and api object
1907
+ // sameAssumedClaims are claims in assumed cache with same latest and api object
1908
+ actualAssumedClaims , sameAssumedClaims := tc .listAssumedClaims ()
1909
+
1910
+ // error when expecting no claims in assumed cache with different latest and api object
1911
+ if len (expectAssumedClaims ) == 0 && len (actualAssumedClaims ) != 0 {
1912
+ // In case we delete the claim API object, wait for assumed cache to sync with informer,
1913
+ // then assumed cache should be empty.
1914
+ err := wait .PollUntilContextTimeout (tc .ctx , 200 * time .Millisecond , time .Minute , true ,
1915
+ func (ctx context.Context ) (bool , error ) {
1916
+ actualAssumedClaims , sameAssumedClaims = tc .listAssumedClaims ()
1917
+ return len (actualAssumedClaims ) == 0 , nil
1918
+ })
1919
+ if err != nil || len (actualAssumedClaims ) != 0 {
1920
+ t .Errorf ("Assumed claims are different, err=%v, expected: nil, actual:\n %v" , err , actualAssumedClaims )
1921
+ }
1922
+ }
1923
+ if len (expectAssumedClaims ) > 0 {
1924
+ // it is not an error as long as the expected claim is present in the assumed cache, no
1925
+ // matter its latest and api object are different or not.
1926
+ for _ , expected := range expectAssumedClaims {
1927
+ seen := false
1928
+ for _ , actual := range actualAssumedClaims {
1929
+ if cmp .Equal (expected , actual , ignoreFieldsInResourceClaims ... ) {
1930
+ seen = true
1931
+ }
1932
+ }
1933
+ for _ , same := range sameAssumedClaims {
1934
+ if cmp .Equal (expected , same , ignoreFieldsInResourceClaims ... ) {
1935
+ seen = true
1936
+ }
1937
+ }
1938
+ if ! seen {
1939
+ t .Errorf ("Assumed claims are different, expected: %v not found" , expected )
1940
+ }
1941
+ }
1911
1942
}
1912
1943
1913
1944
var expectInFlightClaims []metav1.Object
@@ -1932,18 +1963,22 @@ func (tc *testContext) listAll(t *testing.T) (objects []metav1.Object) {
1932
1963
return
1933
1964
}
1934
1965
1935
- func (tc * testContext ) listAssumedClaims () []metav1.Object {
1966
+ func (tc * testContext ) listAssumedClaims () ( []metav1.Object , []metav1. Object ) {
1936
1967
var assumedClaims []metav1.Object
1968
+ var sameClaims []metav1.Object
1937
1969
for _ , obj := range tc .draManager .resourceClaimTracker .cache .List (nil ) {
1938
1970
claim := obj .(* resourceapi.ResourceClaim )
1939
1971
obj , _ := tc .draManager .resourceClaimTracker .cache .Get (claim .Namespace + "/" + claim .Name )
1940
1972
apiObj , _ := tc .draManager .resourceClaimTracker .cache .GetAPIObj (claim .Namespace + "/" + claim .Name )
1941
1973
if obj != apiObj {
1942
1974
assumedClaims = append (assumedClaims , claim )
1975
+ } else {
1976
+ sameClaims = append (sameClaims , claim )
1943
1977
}
1944
1978
}
1945
1979
sortObjects (assumedClaims )
1946
- return assumedClaims
1980
+ sortObjects (sameClaims )
1981
+ return assumedClaims , sameClaims
1947
1982
}
1948
1983
1949
1984
func (tc * testContext ) listInFlightClaims () []metav1.Object {
0 commit comments