Move watch creating into the retry loop

This commit is contained in:
Caleb Woodbine 2020-06-08 09:29:28 +12:00
parent 63c694acdf
commit 39fd803140

View File

@ -1298,20 +1298,22 @@ func taintExists(taints []v1.Taint, taintToFind *v1.Taint) bool {
// scenario the test itself // scenario the test itself
// retryCleanup a function to run which ensures that there are no dangling resources upon test failure // retryCleanup a function to run which ensures that there are no dangling resources upon test failure
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) { 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")
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(ctx, listOptions) return dc.Resource(resourceType).Namespace(namespace).Watch(ctx, listOptions)
}, },
} }
resourceWatch, err := watchtools.NewRetryWatcher(initResource.GetResourceVersion(), listWatcher)
ExpectNoError(err, "Failed to create a resource watch of %v in namespace %v", resourceType.Resource, namespace)
// NOTE value of 3 retries seems to make sense // NOTE value of 3 retries seems to make sense
retries := 3 retries := 3
retriesLoop: retriesLoop:
for try := 1; try <= retries; try++ { for try := 1; try <= retries; try++ {
initResource, err := dc.Resource(resourceType).Namespace(namespace).List(ctx, listOptions)
ExpectNoError(err, "Failed to fetch initial resource")
resourceWatch, err := watchtools.NewRetryWatcher(initResource.GetResourceVersion(), listWatcher)
ExpectNoError(err, "Failed to create a resource watch of %v in namespace %v", resourceType.Resource, namespace)
// NOTE the test may need access to the events to see what's going on, such as a change in status // NOTE the test may need access to the events to see what's going on, such as a change in status
actualWatchEvents := scenario(resourceWatch) actualWatchEvents := scenario(resourceWatch)
errs := sets.NewString() errs := sets.NewString()
@ -1334,7 +1336,7 @@ retriesLoop:
} }
totalValidWatchEvents++ totalValidWatchEvents++
} }
err := retryCleanup() err = retryCleanup()
ExpectNoError(err, "Error occurred when cleaning up resources") ExpectNoError(err, "Error occurred 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 - "))