github.com/operator-framework/operator-lifecycle-manager@v0.30.0/pkg/controller/registry/resolver/instrumented_resolver_test.go (about)

     1  package resolver
     2  
     3  import (
     4  	"errors"
     5  	"testing"
     6  	"time"
     7  
     8  	"github.com/operator-framework/api/pkg/operators/v1alpha1"
     9  	"github.com/stretchr/testify/require"
    10  )
    11  
    12  const (
    13  	failure = time.Duration(0)
    14  	success = time.Duration(1)
    15  )
    16  
    17  type fakeResolverWithError struct{}
    18  type fakeResolverWithoutError struct{}
    19  
    20  func (r *fakeResolverWithError) ResolveSteps(namespace string) ([]*v1alpha1.Step, []v1alpha1.BundleLookup, []*v1alpha1.Subscription, error) {
    21  	return nil, nil, nil, errors.New("Fake error")
    22  }
    23  
    24  func (r *fakeResolverWithoutError) ResolveSteps(namespace string) ([]*v1alpha1.Step, []v1alpha1.BundleLookup, []*v1alpha1.Subscription, error) {
    25  	return nil, nil, nil, nil
    26  }
    27  
    28  func newFakeResolverWithError() *fakeResolverWithError {
    29  	return &fakeResolverWithError{}
    30  }
    31  
    32  func newFakeResolverWithoutError() *fakeResolverWithoutError {
    33  	return &fakeResolverWithoutError{}
    34  }
    35  
    36  func TestInstrumentedResolverFailure(t *testing.T) {
    37  	result := []time.Duration{}
    38  
    39  	changeToFailure := func(num time.Duration) {
    40  		result = append(result, failure)
    41  	}
    42  
    43  	changeToSuccess := func(num time.Duration) {
    44  		result = append(result, success)
    45  	}
    46  
    47  	instrumentedResolver := NewInstrumentedResolver(newFakeResolverWithError(), changeToSuccess, changeToFailure)
    48  	instrumentedResolver.ResolveSteps("")
    49  	require.Equal(t, len(result), 1)     // check that only one call was made to a change function
    50  	require.Equal(t, result[0], failure) // check that the call was made to changeToFailure function
    51  }
    52  
    53  func TestInstrumentedResolverSuccess(t *testing.T) {
    54  	result := []time.Duration{}
    55  
    56  	changeToFailure := func(num time.Duration) {
    57  		result = append(result, failure)
    58  	}
    59  
    60  	changeToSuccess := func(num time.Duration) {
    61  		result = append(result, success)
    62  	}
    63  
    64  	instrumentedResolver := NewInstrumentedResolver(newFakeResolverWithoutError(), changeToSuccess, changeToFailure)
    65  	instrumentedResolver.ResolveSteps("")
    66  	require.Equal(t, len(result), 1)     // check that only one call was made to a change function
    67  	require.Equal(t, result[0], success) // check that the call was made to changeToSuccess function
    68  }