mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-22 11:21:47 +00:00
Update desc, naming, cleanup handling
This commit is contained in:
parent
cd314c193c
commit
8945d98805
@ -1295,13 +1295,14 @@ func taintExists(taints []v1.Taint, taintToFind *v1.Taint) bool {
|
|||||||
// resourceName the name of the given resource
|
// resourceName the name of the given resource
|
||||||
// listOptions options used to find the resource, recommended to use listOptions.labelSelector
|
// listOptions options used to find the resource, recommended to use listOptions.labelSelector
|
||||||
// expectedWatchEvents array of events which are expected to occur
|
// expectedWatchEvents array of events which are expected to occur
|
||||||
// scenario the function to run
|
// scenario the test itself
|
||||||
func WatchEventSequenceVerifier(testContext context.Context, dc dynamic.Interface, resourceType schema.GroupVersionResource, namespace string, resourceName string, listOptions metav1.ListOptions, expectedWatchEvents []watch.Event, scenario func(*watchtools.RetryWatcher) []watch.Event) {
|
// retryCleanup a function to run which ensures that there are no dangling resources upon test failure
|
||||||
initResource, err := dc.Resource(resourceType).Namespace(namespace).List(testContext, listOptions)
|
func WatchEventSequenceVerifier(ctx context.Context, dc dynamic.Interface, resourceType schema.GroupVersionResource, namespace string, resourceName string, listOptions metav1.ListOptions, expectedWatchEvents []watch.Event, scenario func(*watchtools.RetryWatcher) []watch.Event, retryCleanup func() error) {
|
||||||
|
initResource, err := dc.Resource(resourceType).Namespace(namespace).List(ctx, listOptions)
|
||||||
ExpectNoError(err, "Failed to fetch initial resource")
|
ExpectNoError(err, "Failed to fetch initial resource")
|
||||||
listWatcher := &cache.ListWatch{
|
listWatcher := &cache.ListWatch{
|
||||||
WatchFunc: func(listOptions metav1.ListOptions) (watch.Interface, error) {
|
WatchFunc: func(listOptions metav1.ListOptions) (watch.Interface, error) {
|
||||||
return dc.Resource(resourceType).Namespace(namespace).Watch(testContext, listOptions)
|
return dc.Resource(resourceType).Namespace(namespace).Watch(ctx, listOptions)
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
resourceWatch, err := watchtools.NewRetryWatcher(initResource.GetResourceVersion(), listWatcher)
|
resourceWatch, err := watchtools.NewRetryWatcher(initResource.GetResourceVersion(), listWatcher)
|
||||||
@ -1319,7 +1320,7 @@ retriesLoop:
|
|||||||
actualWatchEventsHasDelete := false
|
actualWatchEventsHasDelete := false
|
||||||
for watchEventIndex := range expectedWatchEvents {
|
for watchEventIndex := range expectedWatchEvents {
|
||||||
foundExpectedWatchEvent := false
|
foundExpectedWatchEvent := false
|
||||||
ExpectEqual(len(expectedWatchEvents) <= len(actualWatchEvents), true, "Error: actual watch events amount must be greater than or equal to expected watch events amount")
|
ExpectEqual(len(expectedWatchEvents) <= len(actualWatchEvents), true, "Error: actual watch events amount (%d) must be greater than or equal to expected watch events amount (%d)", len(actualWatchEvents), len(expectedWatchEvents))
|
||||||
for actualWatchEventIndex := range actualWatchEvents {
|
for actualWatchEventIndex := range actualWatchEvents {
|
||||||
if actualWatchEvents[watchEventIndex].Type == expectedWatchEvents[actualWatchEventIndex].Type {
|
if actualWatchEvents[watchEventIndex].Type == expectedWatchEvents[actualWatchEventIndex].Type {
|
||||||
foundExpectedWatchEvent = true
|
foundExpectedWatchEvent = true
|
||||||
@ -1333,15 +1334,14 @@ retriesLoop:
|
|||||||
}
|
}
|
||||||
totalValidWatchEvents++
|
totalValidWatchEvents++
|
||||||
}
|
}
|
||||||
if actualWatchEventsHasDelete == false {
|
err := retryCleanup()
|
||||||
_ = dc.Resource(resourceType).Namespace(namespace).DeleteCollection(testContext, metav1.DeleteOptions{}, listOptions)
|
ExpectNoError(err, "Error occured when cleaning up resources")
|
||||||
}
|
|
||||||
if errs.Len() > 0 && try < retries {
|
if errs.Len() > 0 && try < retries {
|
||||||
fmt.Println("invariants violated:\n", strings.Join(errs.List(), "\n - "))
|
fmt.Println("invariants violated:\n", strings.Join(errs.List(), "\n - "))
|
||||||
continue retriesLoop
|
continue retriesLoop
|
||||||
}
|
}
|
||||||
ExpectEqual(errs.Len() > 0, false, strings.Join(errs.List(), "\n - "))
|
ExpectEqual(errs.Len() > 0, false, strings.Join(errs.List(), "\n - "))
|
||||||
ExpectEqual(totalValidWatchEvents, len(expectedWatchEvents), "Error: there must be an equal amount of total valid watch events (%v) and expected watch events (%v)", totalValidWatchEvents, len(expectedWatchEvents))
|
ExpectEqual(totalValidWatchEvents, len(expectedWatchEvents), "Error: there must be an equal amount of total valid watch events (%d) and expected watch events (%d)", totalValidWatchEvents, len(expectedWatchEvents))
|
||||||
break retriesLoop
|
break retriesLoop
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -1353,7 +1353,7 @@ retriesLoop:
|
|||||||
// Conditions are satisfied sequentially so as to provide a useful primitive for higher level composition.
|
// Conditions are satisfied sequentially so as to provide a useful primitive for higher level composition.
|
||||||
// Waits until context deadline or until context is canceled.
|
// Waits until context deadline or until context is canceled.
|
||||||
//
|
//
|
||||||
// the same as watchtools.UntilWithoutRetry, just without the closing of the watch
|
// the same as watchtools.UntilWithoutRetry, just without the closing of the watch - as for the purpose of being paired with WatchEventSequenceVerifier, the watch is needed for continual watch event collection
|
||||||
func WatchUntilWithoutRetry(ctx context.Context, watcher watch.Interface, conditions ...watchtools.ConditionFunc) (*watch.Event, error) {
|
func WatchUntilWithoutRetry(ctx context.Context, watcher watch.Interface, conditions ...watchtools.ConditionFunc) (*watch.Event, error) {
|
||||||
ch := watcher.ResultChan()
|
ch := watcher.ResultChan()
|
||||||
var lastEvent *watch.Event
|
var lastEvent *watch.Event
|
||||||
|
Loading…
Reference in New Issue
Block a user