github.com/cilium/cilium@v1.16.2/operator/cmd/kvstore_watchdog_test.go (about)

     1  // SPDX-License-Identifier: Apache-2.0
     2  // Copyright Authors of Cilium
     3  
     4  package cmd
     5  
     6  import (
     7  	"reflect"
     8  	"testing"
     9  
    10  	"github.com/cilium/cilium/pkg/kvstore"
    11  )
    12  
    13  func Test_getOldestLeases(t *testing.T) {
    14  	type args struct {
    15  		m map[string]kvstore.Value
    16  	}
    17  	tests := []struct {
    18  		name string
    19  		args args
    20  		want map[string]kvstore.Value
    21  	}{
    22  		{
    23  			name: "test-1",
    24  			args: args{
    25  				m: map[string]kvstore.Value{},
    26  			},
    27  			want: map[string]kvstore.Value{},
    28  		},
    29  		{
    30  			name: "test-2",
    31  			args: args{
    32  				m: map[string]kvstore.Value{
    33  					"foo/bar/1": {
    34  						Data:        nil,
    35  						ModRevision: 1,
    36  						LeaseID:     1,
    37  					},
    38  					"foo/bar/2": {
    39  						Data:        nil,
    40  						ModRevision: 2,
    41  						LeaseID:     2,
    42  					},
    43  					"foo/bar/3": {
    44  						Data:        nil,
    45  						ModRevision: 3,
    46  						LeaseID:     3,
    47  					},
    48  					"foo/bar/4": {
    49  						Data:        nil,
    50  						ModRevision: 4,
    51  						LeaseID:     4,
    52  					},
    53  					"foo/bar/5": {
    54  						Data:        nil,
    55  						ModRevision: 5,
    56  						LeaseID:     5,
    57  					},
    58  					"foo/baz/6": {
    59  						Data:        nil,
    60  						ModRevision: 6,
    61  						LeaseID:     6,
    62  					},
    63  					"foo/bbz/7": {
    64  						Data:        nil,
    65  						ModRevision: 3,
    66  						LeaseID:     3,
    67  					},
    68  				},
    69  			},
    70  			want: map[string]kvstore.Value{
    71  				"foo/bar/1": {
    72  					Data:        nil,
    73  					ModRevision: 1,
    74  					LeaseID:     1,
    75  				},
    76  				"foo/baz/6": {
    77  					Data:        nil,
    78  					ModRevision: 6,
    79  					LeaseID:     6,
    80  				},
    81  				"foo/bbz/7": {
    82  					Data:        nil,
    83  					ModRevision: 3,
    84  					LeaseID:     3,
    85  				},
    86  			},
    87  		},
    88  	}
    89  	for _, tt := range tests {
    90  		t.Run(tt.name, func(t *testing.T) {
    91  			if got := getOldestLeases(tt.args.m); !reflect.DeepEqual(got, tt.want) {
    92  				t.Errorf("getOldestLeases() = %v, want %v", got, tt.want)
    93  			}
    94  		})
    95  	}
    96  }
    97  
    98  func Test_getPath(t *testing.T) {
    99  	type args struct {
   100  		k string
   101  	}
   102  	tests := []struct {
   103  		name string
   104  		args args
   105  		want string
   106  	}{
   107  		{
   108  			name: "test-1",
   109  			args: args{
   110  				k: "cilium/state/identities/v1/locks/" +
   111  					"k8s:io.cilium.k8s.policy.cluster=default;" +
   112  					"k8s:io.cilium.k8s.policy.serviceaccount=default;" +
   113  					"k8s:io.kubernetes.pod.namespace=default;k8s:k8s-app.guestbook=redis;" +
   114  					"k8s:role=master;" +
   115  					"/29c66fd840fa06f7",
   116  			},
   117  			want: "cilium/state/identities/v1/locks/" +
   118  				"k8s:io.cilium.k8s.policy.cluster=default;" +
   119  				"k8s:io.cilium.k8s.policy.serviceaccount=default;" +
   120  				"k8s:io.kubernetes.pod.namespace=default;k8s:k8s-app.guestbook=redis;" +
   121  				"k8s:role=master;",
   122  		},
   123  		{
   124  			name: "test-2",
   125  			args: args{
   126  				k: "cilium/state/identities/v1/locks/" +
   127  					"k8s:io.cilium.k8s.policy.cluster=default;" +
   128  					"k8s:role=master/////;" +
   129  					"/29c66fd840fa06f7",
   130  			},
   131  			want: "cilium/state/identities/v1/locks/" +
   132  				"k8s:io.cilium.k8s.policy.cluster=default;" +
   133  				"k8s:role=master/////;",
   134  		},
   135  	}
   136  	for _, tt := range tests {
   137  		t.Run(tt.name, func(t *testing.T) {
   138  			if got := keyPathFromLockPath(tt.args.k); got != tt.want {
   139  				t.Errorf("keyPathFromLockPath() = %v, want %v", got, tt.want)
   140  			}
   141  		})
   142  	}
   143  }