github.com/cilium/cilium@v1.16.2/pkg/hubble/observer/namespace_manager_test.go (about)

     1  // SPDX-License-Identifier: Apache-2.0
     2  // Copyright Authors of Hubble
     3  
     4  package observer
     5  
     6  import (
     7  	"testing"
     8  	"time"
     9  
    10  	"github.com/stretchr/testify/assert"
    11  
    12  	observerpb "github.com/cilium/cilium/api/v1/observer"
    13  )
    14  
    15  func TestNamespaceManager(t *testing.T) {
    16  	// mock time
    17  	currentTime := time.Time{}
    18  	mgr := NewNamespaceManager()
    19  	// use the mocked time
    20  	mgr.nowFunc = func() time.Time {
    21  		return currentTime
    22  	}
    23  	advanceTime := func(d time.Duration) {
    24  		// update our currentTime
    25  		currentTime = currentTime.Add(d)
    26  		// trigger cleanupNamespaces after we advance time to ensure it's run
    27  		mgr.cleanupNamespaces()
    28  	}
    29  
    30  	// we start with no namespaces
    31  	expected := []*observerpb.Namespace{}
    32  	assert.Equal(t, expected, mgr.GetNamespaces())
    33  
    34  	// add a few namespaces
    35  
    36  	// out of order, we'll verify it's sorted when we call GetNamespaces later
    37  	mgr.AddNamespace(&observerpb.Namespace{Namespace: "ns-2"})
    38  	mgr.AddNamespace(&observerpb.Namespace{Namespace: "ns-1"})
    39  
    40  	// namespaces that we added should be returned, sorted
    41  	expected = []*observerpb.Namespace{
    42  		{Namespace: "ns-1"},
    43  		{Namespace: "ns-2"},
    44  	}
    45  	assert.Equal(t, expected, mgr.GetNamespaces())
    46  
    47  	// advance the clock by 1/2 the namespaceTTL and verify our namespaces are still known
    48  	advanceTime(namespaceTTL / 2)
    49  	assert.Equal(t, expected, mgr.GetNamespaces())
    50  
    51  	// add more namespaces now that the clock has been advanced
    52  	mgr.AddNamespace(&observerpb.Namespace{Namespace: "ns-1"})
    53  	mgr.AddNamespace(&observerpb.Namespace{Namespace: "ns-3"})
    54  	mgr.AddNamespace(&observerpb.Namespace{Namespace: "ns-4"})
    55  
    56  	// we expect all namespaces to exist, the first 2 are 30 minutes old, and the
    57  	// next two are 0 minutes old
    58  	expected = []*observerpb.Namespace{
    59  		{Namespace: "ns-1"},
    60  		{Namespace: "ns-2"},
    61  		{Namespace: "ns-3"},
    62  		{Namespace: "ns-4"},
    63  	}
    64  	assert.Equal(t, expected, mgr.GetNamespaces())
    65  
    66  	// advance the clock another 1/2 TTL and add a minute to push us past the TTL
    67  	advanceTime((namespaceTTL / 2) + time.Minute)
    68  
    69  	// we expect ns2 to be gone because it's an hour old, and ns-1 got refreshed
    70  	// when we added ns-3 and ns-4 30 minutes ago
    71  	expected = []*observerpb.Namespace{
    72  		{Namespace: "ns-1"},
    73  		{Namespace: "ns-3"},
    74  		{Namespace: "ns-4"},
    75  	}
    76  	assert.Equal(t, expected, mgr.GetNamespaces())
    77  
    78  	// advance the clock another 1/2 TTL and add a minute to push us past the TTL again
    79  	advanceTime((namespaceTTL / 2) + time.Minute)
    80  
    81  	// no namespaces left, nothing has been refreshed
    82  	assert.Equal(t, []*observerpb.Namespace{}, mgr.GetNamespaces())
    83  }