github.com/kyma-incubator/compass/components/director@v0.0.0-20230623144113-d764f56ff805/internal/destinationfetchersvc/syncjob_test.go (about)

     1  package destinationfetchersvc_test
     2  
     3  import (
     4  	"errors"
     5  	"testing"
     6  	"time"
     7  
     8  	"github.com/kyma-incubator/compass/components/director/internal/destinationfetchersvc"
     9  	"github.com/kyma-incubator/compass/components/director/internal/destinationfetchersvc/automock"
    10  	"github.com/kyma-incubator/compass/components/director/pkg/cronjob"
    11  	"github.com/stretchr/testify/assert"
    12  	"github.com/stretchr/testify/mock"
    13  	"golang.org/x/net/context"
    14  )
    15  
    16  func TestDestinationSyncJob(t *testing.T) {
    17  	const testTimeout = time.Second * 5
    18  
    19  	var (
    20  		tenantsToResync = []string{"t1", "t2", "t3"}
    21  
    22  		cfg = destinationfetchersvc.SyncJobConfig{
    23  			ParallelTenants:   2,
    24  			JobSchedulePeriod: time.Minute,
    25  			ElectionCfg: cronjob.ElectionConfig{
    26  				ElectionEnabled: false,
    27  			},
    28  		}
    29  		expectedError = errors.New("expected")
    30  
    31  		cancelCtxAfterAllDoneReceived = func(done <-chan struct{}, doneCount int, cancel context.CancelFunc) {
    32  			go func() {
    33  				defer cancel()
    34  				for i := 0; i < doneCount; i++ {
    35  					select {
    36  					case <-done:
    37  					case <-time.After(testTimeout):
    38  						t.Errorf("Test timed out - not all tenants re-synced")
    39  						return
    40  					}
    41  				}
    42  			}()
    43  		}
    44  	)
    45  
    46  	t.Run("Should re-sync all tenants", func(t *testing.T) {
    47  		ctx, cancel := context.WithCancel(context.Background())
    48  
    49  		done := make(chan struct{}, len(tenantsToResync))
    50  		addDone := func(args mock.Arguments) {
    51  			done <- struct{}{}
    52  		}
    53  		cancelCtxAfterAllDoneReceived(done, len(tenantsToResync), cancel)
    54  
    55  		destinationSyncer := &automock.DestinationSyncer{}
    56  		destinationSyncer.Mock.On("SyncTenantDestinations",
    57  			mock.Anything, tenantsToResync[0]).Return(nil).Run(addDone)
    58  		destinationSyncer.Mock.On("SyncTenantDestinations",
    59  			mock.Anything, tenantsToResync[1]).Return(nil).Run(addDone)
    60  		destinationSyncer.Mock.On("SyncTenantDestinations",
    61  			mock.Anything, tenantsToResync[2]).Return(nil).Run(addDone)
    62  		destinationSyncer.Mock.On("GetSubscribedTenantIDs", mock.Anything).
    63  			Return(tenantsToResync, nil)
    64  
    65  		err := destinationfetchersvc.StartDestinationFetcherSyncJob(ctx, cfg, destinationSyncer)
    66  		assert.Nil(t, err)
    67  		destinationSyncer.Mock.AssertNumberOfCalls(t, "GetSubscribedTenantIDs", 1)
    68  		destinationSyncer.Mock.AssertNumberOfCalls(t, "SyncTenantDestinations", len(tenantsToResync))
    69  		destinationSyncer.AssertExpectations(t)
    70  	})
    71  
    72  	t.Run("Should not fail on one tenant re-sync failure", func(t *testing.T) {
    73  		ctx, cancel := context.WithCancel(context.Background())
    74  
    75  		done := make(chan struct{}, len(tenantsToResync))
    76  		addDone := func(args mock.Arguments) {
    77  			done <- struct{}{}
    78  		}
    79  		cancelCtxAfterAllDoneReceived(done, len(tenantsToResync), cancel)
    80  
    81  		destinationSyncer := &automock.DestinationSyncer{}
    82  		destinationSyncer.Mock.On("SyncTenantDestinations",
    83  			mock.Anything, tenantsToResync[0]).Return(nil).Run(addDone)
    84  		destinationSyncer.Mock.On("SyncTenantDestinations",
    85  			mock.Anything, tenantsToResync[1]).Return(expectedError).Run(addDone)
    86  		destinationSyncer.Mock.On("SyncTenantDestinations",
    87  			mock.Anything, tenantsToResync[2]).Return(nil).Run(addDone)
    88  		destinationSyncer.Mock.On("GetSubscribedTenantIDs", mock.Anything).
    89  			Return(tenantsToResync, nil)
    90  
    91  		err := destinationfetchersvc.StartDestinationFetcherSyncJob(ctx, cfg, destinationSyncer)
    92  		assert.Nil(t, err)
    93  		destinationSyncer.Mock.AssertNumberOfCalls(t, "GetSubscribedTenantIDs", 1)
    94  		destinationSyncer.Mock.AssertNumberOfCalls(t, "SyncTenantDestinations", len(tenantsToResync))
    95  		destinationSyncer.AssertExpectations(t)
    96  	})
    97  
    98  	t.Run("Should not re-sync if subscribed tenants could not be fetched", func(t *testing.T) {
    99  		ctx, cancel := context.WithCancel(context.Background())
   100  
   101  		destinationSyncer := &automock.DestinationSyncer{}
   102  		destinationSyncer.Mock.On("GetSubscribedTenantIDs", mock.Anything).
   103  			Return(nil, expectedError).Run(func(args mock.Arguments) { cancel() })
   104  
   105  		err := destinationfetchersvc.StartDestinationFetcherSyncJob(ctx, cfg, destinationSyncer)
   106  		assert.Nil(t, err)
   107  		destinationSyncer.Mock.AssertNumberOfCalls(t, "GetSubscribedTenantIDs", 1)
   108  		destinationSyncer.AssertExpectations(t)
   109  	})
   110  
   111  	t.Run("Should not re-sync if there are no subscribed tenants", func(t *testing.T) {
   112  		ctx, cancel := context.WithCancel(context.Background())
   113  
   114  		destinationSyncer := &automock.DestinationSyncer{}
   115  		destinationSyncer.Mock.On("GetSubscribedTenantIDs", mock.Anything).
   116  			Return(nil, nil).Run(func(args mock.Arguments) { cancel() })
   117  
   118  		err := destinationfetchersvc.StartDestinationFetcherSyncJob(ctx, cfg, destinationSyncer)
   119  		assert.Nil(t, err)
   120  		destinationSyncer.Mock.AssertNumberOfCalls(t, "GetSubscribedTenantIDs", 1)
   121  		destinationSyncer.AssertExpectations(t)
   122  	})
   123  }