github.com/cilium/cilium@v1.16.2/pkg/datapath/tunnel/tunnel_test.go (about) 1 // SPDX-License-Identifier: Apache-2.0 2 // Copyright Authors of Cilium 3 4 package tunnel 5 6 import ( 7 "errors" 8 "testing" 9 10 "github.com/cilium/hive/cell" 11 "github.com/cilium/hive/hivetest" 12 "github.com/stretchr/testify/assert" 13 14 dpcfgdef "github.com/cilium/cilium/pkg/datapath/linux/config/defines" 15 "github.com/cilium/cilium/pkg/defaults" 16 "github.com/cilium/cilium/pkg/hive" 17 ) 18 19 func TestConfig(t *testing.T) { 20 enabler := func(enable bool, opts ...enablerOpt) any { 21 return func() EnablerOut { return NewEnabler(enable, opts...) } 22 } 23 24 tests := []struct { 25 name string 26 ucfg userCfg 27 enablers []any 28 29 shallFail bool 30 proto Protocol 31 port uint16 32 deviceName string 33 shouldAdaptMTU bool 34 }{ 35 { 36 name: "invalid protocol", 37 ucfg: userCfg{TunnelProtocol: "invalid"}, 38 shallFail: true, 39 }, 40 { 41 name: "tunnel not enabled", 42 ucfg: userCfg{TunnelProtocol: string(Geneve)}, 43 proto: Disabled, 44 }, 45 { 46 name: "tunnel not enabled, with enablers", 47 ucfg: userCfg{TunnelProtocol: string(Geneve)}, 48 enablers: []any{enabler(false), enabler(false)}, 49 proto: Disabled, 50 }, 51 { 52 name: "tunnel enabled, vxlan", 53 ucfg: userCfg{TunnelProtocol: string(VXLAN)}, 54 enablers: []any{enabler(true), enabler(false)}, 55 proto: VXLAN, 56 port: defaults.TunnelPortVXLAN, 57 deviceName: defaults.VxlanDevice, 58 shouldAdaptMTU: true, 59 }, 60 { 61 name: "tunnel enabled, vxlan, custom port", 62 ucfg: userCfg{TunnelProtocol: string(VXLAN), TunnelPort: 1234}, 63 enablers: []any{enabler(false), enabler(true)}, 64 proto: VXLAN, 65 port: 1234, 66 deviceName: defaults.VxlanDevice, 67 shouldAdaptMTU: true, 68 }, 69 { 70 name: "tunnel enabled, geneve", 71 ucfg: userCfg{TunnelProtocol: string(Geneve)}, 72 enablers: []any{enabler(true), enabler(true)}, 73 proto: Geneve, 74 port: defaults.TunnelPortGeneve, 75 deviceName: defaults.GeneveDevice, 76 shouldAdaptMTU: true, 77 }, 78 { 79 name: "tunnel enabled, geneve, custom port", 80 ucfg: userCfg{TunnelProtocol: string(Geneve), TunnelPort: 1234}, 81 enablers: []any{enabler(true), enabler(false)}, 82 proto: Geneve, 83 port: 1234, 84 deviceName: defaults.GeneveDevice, 85 shouldAdaptMTU: true, 86 }, 87 { 88 name: "tunnel enabled, validation function", 89 ucfg: userCfg{TunnelProtocol: string(Geneve)}, 90 enablers: []any{enabler(true, WithValidator(func(proto Protocol) error { 91 if proto == Geneve { 92 return errors.New("invalid protocol") 93 } 94 return nil 95 }))}, 96 shallFail: true, 97 }, 98 { 99 name: "tunnel enabled, don't need MTU adaptation, one", 100 ucfg: userCfg{TunnelProtocol: string(Geneve)}, 101 enablers: []any{enabler(true, WithoutMTUAdaptation()), enabler(true)}, 102 proto: Geneve, 103 port: defaults.TunnelPortGeneve, 104 deviceName: defaults.GeneveDevice, 105 shouldAdaptMTU: true, 106 }, 107 { 108 name: "tunnel enabled, don't need MTU adaptation, all", 109 ucfg: userCfg{TunnelProtocol: string(Geneve)}, 110 enablers: []any{enabler(true, WithoutMTUAdaptation()), enabler(false)}, 111 proto: Geneve, 112 port: defaults.TunnelPortGeneve, 113 deviceName: defaults.GeneveDevice, 114 shouldAdaptMTU: false, 115 }, 116 } 117 118 for _, tt := range tests { 119 t.Run(tt.name, func(t *testing.T) { 120 var out Config 121 122 err := hive.New( 123 cell.Config(tt.ucfg), 124 cell.Provide(newConfig), 125 cell.Provide(tt.enablers...), 126 cell.Invoke(func(tc Config) { out = tc }), 127 ).Populate(hivetest.Logger(t)) 128 129 if tt.shallFail { 130 assert.Error(t, err) 131 return 132 } 133 134 assert.NoError(t, err) 135 assert.Equal(t, tt.proto, out.Protocol()) 136 assert.Equal(t, tt.port, out.Port()) 137 assert.Equal(t, tt.deviceName, out.DeviceName()) 138 assert.Equal(t, tt.shouldAdaptMTU, out.ShouldAdaptMTU()) 139 }) 140 } 141 } 142 143 func TestConfigDatapathProvider(t *testing.T) { 144 tests := []struct { 145 name string 146 proto Protocol 147 expected dpcfgdef.Map 148 }{ 149 { 150 name: "disabled", 151 proto: Disabled, 152 expected: dpcfgdef.Map{}, 153 }, 154 { 155 name: "vxlan", 156 proto: VXLAN, 157 expected: dpcfgdef.Map{ 158 "TUNNEL_PROTOCOL_VXLAN": "1", 159 "TUNNEL_PROTOCOL_GENEVE": "2", 160 "TUNNEL_PROTOCOL": "1", 161 "TUNNEL_PORT": "1234", 162 }, 163 }, 164 { 165 name: "geneve", 166 proto: Geneve, 167 expected: dpcfgdef.Map{ 168 "TUNNEL_PROTOCOL_VXLAN": "1", 169 "TUNNEL_PROTOCOL_GENEVE": "2", 170 "TUNNEL_PROTOCOL": "2", 171 "TUNNEL_PORT": "1234", 172 }, 173 }, 174 } 175 176 for _, tt := range tests { 177 t.Run(tt.name, func(t *testing.T) { 178 out, _ := Config{ 179 protocol: tt.proto, 180 port: 1234, 181 deviceName: "device", 182 }.datapathConfigProvider() 183 184 assert.Equal(t, out.NodeDefines, tt.expected) 185 }) 186 } 187 }