github.com/cilium/cilium@v1.16.2/pkg/clustermesh/types/option_test.go (about) 1 // SPDX-License-Identifier: Apache-2.0 2 // Copyright Authors of Cilium 3 4 package types 5 6 import ( 7 "fmt" 8 "testing" 9 10 "github.com/sirupsen/logrus" 11 "github.com/stretchr/testify/assert" 12 ) 13 14 func TestClusterInfoValidate(t *testing.T) { 15 log := logrus.New() 16 17 // this test involves changing the global ClusterIDMax variable, so we need 18 // to save its value and restore it at the end of the test 19 oldMaxClusterID := ClusterIDMax 20 defer func() { ClusterIDMax = oldMaxClusterID }() 21 22 tests := []struct { 23 cinfo ClusterInfo 24 wantMCCErr bool 25 wantErr bool 26 wantStrictErr bool 27 }{ 28 { 29 cinfo: ClusterInfo{ID: 0, Name: "default", MaxConnectedClusters: 255}, 30 wantErr: false, 31 wantStrictErr: true, 32 }, 33 { 34 cinfo: ClusterInfo{ID: 0, Name: "foo", MaxConnectedClusters: 255}, 35 wantErr: false, 36 wantStrictErr: true, 37 }, 38 { 39 cinfo: ClusterInfo{ID: 1, Name: "foo", MaxConnectedClusters: 255}, 40 wantErr: false, 41 wantStrictErr: false, 42 }, 43 { 44 cinfo: ClusterInfo{ID: 255, Name: "foo", MaxConnectedClusters: 255}, 45 wantErr: false, 46 wantStrictErr: false, 47 }, 48 { 49 cinfo: ClusterInfo{ID: 75, Name: "default", MaxConnectedClusters: 255}, 50 wantErr: true, 51 wantStrictErr: true, 52 }, 53 { 54 cinfo: ClusterInfo{ID: 256, Name: "foo", MaxConnectedClusters: 255}, 55 wantErr: true, 56 wantStrictErr: true, 57 }, 58 { 59 cinfo: ClusterInfo{ID: 1, Name: "foo", MaxConnectedClusters: 512}, 60 wantMCCErr: true, 61 wantErr: false, 62 wantStrictErr: false, 63 }, 64 { 65 cinfo: ClusterInfo{ID: 512, Name: "foo", MaxConnectedClusters: 511}, 66 wantErr: true, 67 wantStrictErr: true, 68 }, 69 { 70 cinfo: ClusterInfo{ID: 10, Name: "invAlid", MaxConnectedClusters: 511}, 71 wantErr: false, // Cluster name validation is not yet enforced in Cilium v1.16. 72 wantStrictErr: false, // Cluster name validation is not yet enforced in Cilium v1.16. 73 }, 74 } 75 76 for _, tt := range tests { 77 name := fmt.Sprintf("ID: %d, Name: %s, MaxConnectedClusters: %d", tt.cinfo.ID, tt.cinfo.Name, tt.cinfo.MaxConnectedClusters) 78 t.Run(name, func(t *testing.T) { 79 if tt.wantMCCErr { 80 assert.Error(t, tt.cinfo.InitClusterIDMax()) 81 } else { 82 assert.NoError(t, tt.cinfo.InitClusterIDMax()) 83 } 84 85 if tt.wantErr { 86 assert.Error(t, tt.cinfo.Validate(log)) 87 } else { 88 assert.NoError(t, tt.cinfo.Validate(log)) 89 } 90 91 if tt.wantStrictErr { 92 assert.Error(t, tt.cinfo.ValidateStrict(log)) 93 } else { 94 assert.NoError(t, tt.cinfo.ValidateStrict(log)) 95 } 96 }) 97 } 98 } 99 100 func TestValidateRemoteConfig(t *testing.T) { 101 tests := []struct { 102 name string 103 cfg CiliumClusterConfig 104 mcc uint32 105 assertion func(t assert.TestingT, err error, msgAndArgs ...interface{}) bool 106 }{ 107 { 108 name: "Empty config", 109 cfg: CiliumClusterConfig{}, 110 mcc: 255, 111 assertion: assert.Error, 112 }, 113 { 114 name: "Valid config", 115 cfg: CiliumClusterConfig{ID: 255, Capabilities: CiliumClusterConfigCapabilities{MaxConnectedClusters: 255}}, 116 mcc: 255, 117 assertion: assert.NoError, 118 }, 119 { 120 name: "Invalid config", 121 cfg: CiliumClusterConfig{ID: 256}, 122 mcc: 255, 123 assertion: assert.Error, 124 }, 125 { 126 name: "Invalid config, MaxConnectedClusters mismatch (ClusterMesh255)", 127 cfg: CiliumClusterConfig{ID: 511, Capabilities: CiliumClusterConfigCapabilities{MaxConnectedClusters: 511}}, 128 mcc: 255, 129 assertion: assert.Error, 130 }, 131 { 132 name: "Valid config (ClusterMesh511)", 133 cfg: CiliumClusterConfig{ID: 511, Capabilities: CiliumClusterConfigCapabilities{MaxConnectedClusters: 511}}, 134 mcc: 511, 135 assertion: assert.NoError, 136 }, 137 { 138 name: "Invalid config, MaxConnectedClusters mismatch (ClusterMesh511)", 139 cfg: CiliumClusterConfig{ID: 511, Capabilities: CiliumClusterConfigCapabilities{MaxConnectedClusters: 255}}, 140 mcc: 511, 141 assertion: assert.Error, 142 }, 143 { 144 name: "Invalid config (ClusterMesh511)", 145 cfg: CiliumClusterConfig{ID: 512, Capabilities: CiliumClusterConfigCapabilities{MaxConnectedClusters: 511}}, 146 mcc: 511, 147 assertion: assert.Error, 148 }, 149 } 150 151 for _, tt := range tests { 152 t.Run(tt.name, func(t *testing.T) { 153 cinfo := ClusterInfo{MaxConnectedClusters: tt.mcc} 154 // ClusterIDMax needs to be initialized here. This is ordinarily 155 // executed during agent initialization. 156 cinfo.InitClusterIDMax() 157 tt.assertion(t, cinfo.ValidateRemoteConfig(tt.cfg)) 158 }) 159 } 160 }