github.com/operator-framework/operator-lifecycle-manager@v0.30.0/pkg/controller/operators/openshift/synctracker_test.go (about)

     1  package openshift
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"testing"
     7  	"testing/quick"
     8  
     9  	configv1 "github.com/openshift/api/config/v1"
    10  	"github.com/stretchr/testify/require"
    11  	"sigs.k8s.io/controller-runtime/pkg/event"
    12  )
    13  
    14  func TestInvalidFields(t *testing.T) {
    15  	// Create a cancelled context that we can signal the tracker to shutdown immediately after Start is called
    16  	cancelled, cancel := context.WithCancel(context.Background())
    17  	cancel()
    18  
    19  	// Attempt to start with missing fields
    20  	tracker := &SyncTracker{}
    21  	require.Error(t, tracker.Start(cancelled))
    22  
    23  	// Add missing fields, then try again
    24  	tracker.syncCh = make(chan error)
    25  	tracker.events = make(chan event.GenericEvent)
    26  	tracker.co = &configv1.ClusterOperator{}
    27  	require.NoError(t, tracker.Start(cancelled))
    28  }
    29  
    30  func TestSyncCount(t *testing.T) {
    31  	f := func(failed, successful uint8) bool {
    32  		syncCh := make(chan error)
    33  		defer close(syncCh)
    34  
    35  		co := NewClusterOperator("operator").DeepCopy()
    36  		tracker := NewSyncTracker(syncCh, co)
    37  
    38  		ctx, cancel := context.WithCancel(context.Background())
    39  		defer cancel()
    40  
    41  		go func() {
    42  			require.NoError(t, tracker.Start(ctx))
    43  		}()
    44  
    45  		go func() {
    46  			err := fmt.Errorf("failure")
    47  			f := uint(failed)
    48  			s := uint(successful)
    49  			for f > 0 || s > 0 {
    50  				if f > 0 {
    51  					syncCh <- err
    52  					f--
    53  					continue
    54  				}
    55  				syncCh <- nil
    56  				s--
    57  			}
    58  		}()
    59  
    60  		total := int(failed) + int(successful)
    61  		received := 0
    62  		for range tracker.Events() {
    63  			received++
    64  			if received >= total {
    65  				break
    66  			}
    67  		}
    68  
    69  		require.Equal(t, int(successful), tracker.SuccessfulSyncs(), "incorrect amount of successful sync messages received")
    70  		require.Equal(t, total, tracker.TotalSyncs(), "incorrect total amount of sync messages received")
    71  
    72  		return true
    73  	}
    74  
    75  	require.NoError(t, quick.Check(f, nil))
    76  }