github.com/kubearmor/cilium@v1.6.12/operator/kvstore_watchdog_test.go (about)

     1  // Copyright 2020 Authors of Cilium
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  // +build !privileged_tests
    16  
    17  package main
    18  
    19  import (
    20  	"reflect"
    21  	"testing"
    22  
    23  	"github.com/cilium/cilium/pkg/kvstore"
    24  )
    25  
    26  func Test_getOldestLeases(t *testing.T) {
    27  	type args struct {
    28  		m map[string]kvstore.Value
    29  	}
    30  	tests := []struct {
    31  		name string
    32  		args args
    33  		want map[string]kvstore.Value
    34  	}{
    35  		{
    36  			name: "test-1",
    37  			args: args{
    38  				m: map[string]kvstore.Value{},
    39  			},
    40  			want: map[string]kvstore.Value{},
    41  		},
    42  		{
    43  			name: "test-2",
    44  			args: args{
    45  				m: map[string]kvstore.Value{
    46  					"foo/bar/1": {
    47  						Data:        nil,
    48  						ModRevision: 1,
    49  						LeaseID:     1,
    50  					},
    51  					"foo/bar/2": {
    52  						Data:        nil,
    53  						ModRevision: 2,
    54  						LeaseID:     2,
    55  					},
    56  					"foo/bar/3": {
    57  						Data:        nil,
    58  						ModRevision: 3,
    59  						LeaseID:     3,
    60  					},
    61  					"foo/bar/4": {
    62  						Data:        nil,
    63  						ModRevision: 4,
    64  						LeaseID:     4,
    65  					},
    66  					"foo/bar/5": {
    67  						Data:        nil,
    68  						ModRevision: 5,
    69  						LeaseID:     5,
    70  					},
    71  					"foo/baz/6": {
    72  						Data:        nil,
    73  						ModRevision: 6,
    74  						LeaseID:     6,
    75  					},
    76  					"foo/bbz/7": {
    77  						Data:        nil,
    78  						ModRevision: 3,
    79  						LeaseID:     3,
    80  					},
    81  				},
    82  			},
    83  			want: map[string]kvstore.Value{
    84  				"foo/bar/1": {
    85  					Data:        nil,
    86  					ModRevision: 1,
    87  					LeaseID:     1,
    88  				},
    89  				"foo/baz/6": {
    90  					Data:        nil,
    91  					ModRevision: 6,
    92  					LeaseID:     6,
    93  				},
    94  				"foo/bbz/7": {
    95  					Data:        nil,
    96  					ModRevision: 3,
    97  					LeaseID:     3,
    98  				},
    99  			},
   100  		},
   101  	}
   102  	for _, tt := range tests {
   103  		t.Run(tt.name, func(t *testing.T) {
   104  			if got := getOldestLeases(tt.args.m); !reflect.DeepEqual(got, tt.want) {
   105  				t.Errorf("getOldestLeases() = %v, want %v", got, tt.want)
   106  			}
   107  		})
   108  	}
   109  }
   110  
   111  func Test_getPath(t *testing.T) {
   112  	type args struct {
   113  		k string
   114  	}
   115  	tests := []struct {
   116  		name string
   117  		args args
   118  		want string
   119  	}{
   120  		{
   121  			name: "test-1",
   122  			args: args{
   123  				k: "cilium/state/identities/v1/locks/" +
   124  					"k8s:io.cilium.k8s.policy.cluster=default;" +
   125  					"k8s:io.cilium.k8s.policy.serviceaccount=default;" +
   126  					"k8s:io.kubernetes.pod.namespace=default;k8s:k8s-app.guestbook=redis;" +
   127  					"k8s:role=master;" +
   128  					"/29c66fd840fa06f7",
   129  			},
   130  			want: "cilium/state/identities/v1/locks/" +
   131  				"k8s:io.cilium.k8s.policy.cluster=default;" +
   132  				"k8s:io.cilium.k8s.policy.serviceaccount=default;" +
   133  				"k8s:io.kubernetes.pod.namespace=default;k8s:k8s-app.guestbook=redis;" +
   134  				"k8s:role=master;",
   135  		},
   136  		{
   137  			name: "test-2",
   138  			args: args{
   139  				k: "cilium/state/identities/v1/locks/" +
   140  					"k8s:io.cilium.k8s.policy.cluster=default;" +
   141  					"k8s:role=master/////;" +
   142  					"/29c66fd840fa06f7",
   143  			},
   144  			want: "cilium/state/identities/v1/locks/" +
   145  				"k8s:io.cilium.k8s.policy.cluster=default;" +
   146  				"k8s:role=master/////;",
   147  		},
   148  	}
   149  	for _, tt := range tests {
   150  		t.Run(tt.name, func(t *testing.T) {
   151  			if got := keyPathFromLockPath(tt.args.k); got != tt.want {
   152  				t.Errorf("keyPathFromLockPath() = %v, want %v", got, tt.want)
   153  			}
   154  		})
   155  	}
   156  }