github.com/argoproj/argo-cd@v1.8.7/controller/cache/cache_test.go (about)

     1  package cache
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/assert"
     7  
     8  	"github.com/argoproj/gitops-engine/pkg/cache"
     9  	"github.com/argoproj/gitops-engine/pkg/cache/mocks"
    10  	"github.com/stretchr/testify/mock"
    11  
    12  	appv1 "github.com/argoproj/argo-cd/pkg/apis/application/v1alpha1"
    13  )
    14  
    15  func TestHandleModEvent_HasChanges(t *testing.T) {
    16  	clusterCache := &mocks.ClusterCache{}
    17  	clusterCache.On("Invalidate", mock.Anything, mock.Anything).Return(nil).Once()
    18  	clusterCache.On("EnsureSynced").Return(nil).Once()
    19  
    20  	clustersCache := liveStateCache{
    21  		clusters: map[string]cache.ClusterCache{
    22  			"https://mycluster": clusterCache,
    23  		},
    24  	}
    25  
    26  	clustersCache.handleModEvent(&appv1.Cluster{
    27  		Server: "https://mycluster",
    28  		Config: appv1.ClusterConfig{Username: "foo"},
    29  	}, &appv1.Cluster{
    30  		Server:     "https://mycluster",
    31  		Config:     appv1.ClusterConfig{Username: "bar"},
    32  		Namespaces: []string{"default"},
    33  	})
    34  }
    35  
    36  func TestHandleModEvent_ClusterExcluded(t *testing.T) {
    37  	clusterCache := &mocks.ClusterCache{}
    38  	clusterCache.On("Invalidate", mock.Anything, mock.Anything).Return(nil).Once()
    39  	clusterCache.On("EnsureSynced").Return(nil).Once()
    40  
    41  	clustersCache := liveStateCache{
    42  		clusters: map[string]cache.ClusterCache{
    43  			"https://mycluster": clusterCache,
    44  		},
    45  		clusterFilter: func(cluster *appv1.Cluster) bool {
    46  			return false
    47  		},
    48  	}
    49  
    50  	clustersCache.handleModEvent(&appv1.Cluster{
    51  		Server: "https://mycluster",
    52  		Config: appv1.ClusterConfig{Username: "foo"},
    53  	}, &appv1.Cluster{
    54  		Server:     "https://mycluster",
    55  		Config:     appv1.ClusterConfig{Username: "bar"},
    56  		Namespaces: []string{"default"},
    57  	})
    58  
    59  	assert.Len(t, clustersCache.clusters, 0)
    60  }
    61  
    62  func TestHandleModEvent_NoChanges(t *testing.T) {
    63  	clusterCache := &mocks.ClusterCache{}
    64  	clusterCache.On("Invalidate", mock.Anything).Panic("should not invalidate")
    65  	clusterCache.On("EnsureSynced").Return(nil).Panic("should not re-sync")
    66  
    67  	clustersCache := liveStateCache{
    68  		clusters: map[string]cache.ClusterCache{
    69  			"https://mycluster": clusterCache,
    70  		},
    71  	}
    72  
    73  	clustersCache.handleModEvent(&appv1.Cluster{
    74  		Server: "https://mycluster",
    75  		Config: appv1.ClusterConfig{Username: "bar"},
    76  	}, &appv1.Cluster{
    77  		Server: "https://mycluster",
    78  		Config: appv1.ClusterConfig{Username: "bar"},
    79  	})
    80  }
    81  
    82  func TestHandleAddEvent_ClusterExcluded(t *testing.T) {
    83  	clustersCache := liveStateCache{
    84  		clusters: map[string]cache.ClusterCache{},
    85  		clusterFilter: func(cluster *appv1.Cluster) bool {
    86  			return false
    87  		},
    88  	}
    89  	clustersCache.handleAddEvent(&appv1.Cluster{
    90  		Server: "https://mycluster",
    91  		Config: appv1.ClusterConfig{Username: "bar"},
    92  	})
    93  
    94  	assert.Len(t, clustersCache.clusters, 0)
    95  }