github.com/cilium/cilium@v1.16.2/pkg/clustermesh/common/services_test.go (about)

     1  // SPDX-License-Identifier: Apache-2.0
     2  // Copyright Authors of Cilium
     3  
     4  package common
     5  
     6  import (
     7  	"testing"
     8  
     9  	"github.com/stretchr/testify/require"
    10  
    11  	"github.com/cilium/cilium/pkg/metrics"
    12  	serviceStore "github.com/cilium/cilium/pkg/service/store"
    13  )
    14  
    15  type fakeUpstream struct {
    16  	updated map[string]int
    17  	deleted map[string]int
    18  }
    19  
    20  func (f *fakeUpstream) init() {
    21  	f.updated = make(map[string]int)
    22  	f.deleted = make(map[string]int)
    23  }
    24  
    25  func (f *fakeUpstream) OnUpdate(service *serviceStore.ClusterService) { f.updated[service.String()]++ }
    26  func (f *fakeUpstream) OnDelete(service *serviceStore.ClusterService) { f.deleted[service.String()]++ }
    27  
    28  func TestRemoteServiceObserver(t *testing.T) {
    29  	wrap := func(svc serviceStore.ClusterService) *serviceStore.ValidatingClusterService {
    30  		return &serviceStore.ValidatingClusterService{ClusterService: svc}
    31  	}
    32  	svc1 := serviceStore.ClusterService{Cluster: "remote", Namespace: "namespace", Name: "name", IncludeExternal: false, Shared: true}
    33  	svc2 := serviceStore.ClusterService{Cluster: "remote", Namespace: "namespace", Name: "name"}
    34  	cache := NewGlobalServiceCache(metrics.NoOpGauge)
    35  
    36  	var upstream fakeUpstream
    37  	observer := NewSharedServicesObserver(log, cache, upstream.OnUpdate, upstream.OnDelete)
    38  
    39  	// Observe a new service update (for a non-shared service), and assert it is not added to the cache
    40  	upstream.init()
    41  	observer.OnUpdate(wrap(svc2))
    42  
    43  	require.Equal(t, 0, upstream.updated[svc1.String()])
    44  	require.Equal(t, 0, cache.Size())
    45  
    46  	// Observe a new service update (for a shared service), and assert it is correctly added to the cache
    47  	upstream.init()
    48  	observer.OnUpdate(wrap(svc1))
    49  
    50  	require.Equal(t, 1, upstream.updated[svc1.String()])
    51  	require.Equal(t, 0, upstream.deleted[svc1.String()])
    52  	require.Equal(t, 1, cache.Size())
    53  
    54  	gs := cache.GetGlobalService(svc1.NamespaceServiceName())
    55  	require.Equal(t, 1, len(gs.ClusterServices))
    56  	found, ok := gs.ClusterServices[svc1.Cluster]
    57  	require.True(t, ok)
    58  	require.Equal(t, &svc1, found)
    59  
    60  	// Observe a new service deletion, and assert it is correctly removed from the cache
    61  	upstream.init()
    62  	observer.OnDelete(wrap(svc1))
    63  
    64  	require.Equal(t, 0, upstream.updated[svc1.String()])
    65  	require.Equal(t, 1, upstream.deleted[svc1.String()])
    66  	require.Equal(t, 0, cache.Size())
    67  
    68  	// Observe two service updates in sequence (first shared, then non-shared),
    69  	// and assert that at the end it is not present in the cache (equivalent to update, then delete).
    70  	upstream.init()
    71  	observer.OnUpdate(wrap(svc1))
    72  	observer.OnUpdate(wrap(svc2))
    73  
    74  	require.Equal(t, 1, upstream.updated[svc1.String()])
    75  	require.Equal(t, 1, upstream.deleted[svc1.String()])
    76  	require.Equal(t, 0, cache.Size())
    77  }