123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416 |
- /*
- Copyright 2017 The Kubernetes Authors.
- Licensed under the Apache License, Version 2.0 (the "License");
- you may not use this file except in compliance with the License.
- You may obtain a copy of the License at
- http://www.apache.org/licenses/LICENSE-2.0
- Unless required by applicable law or agreed to in writing, software
- distributed under the License is distributed on an "AS IS" BASIS,
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- See the License for the specific language governing permissions and
- limitations under the License.
- */
- package ipvs
- import (
- "net"
- "reflect"
- "sort"
- "testing"
- "k8s.io/apimachinery/pkg/util/version"
- )
- func TestVirtualServerEqual(t *testing.T) {
- Tests := []struct {
- svcA *VirtualServer
- svcB *VirtualServer
- equal bool
- reason string
- }{
- {
- svcA: &VirtualServer{
- Address: net.ParseIP("10.20.30.40"),
- Protocol: "",
- Port: 0,
- Scheduler: "wrr",
- Flags: 0,
- Timeout: 0,
- },
- svcB: &VirtualServer{
- Address: net.ParseIP("10.20.30.41"),
- Protocol: "",
- Port: 0,
- Scheduler: "wrr",
- Flags: 0,
- Timeout: 0,
- },
- equal: false,
- reason: "IPv4 address not equal",
- },
- {
- svcA: &VirtualServer{
- Address: net.ParseIP("2012::beef"),
- Protocol: "",
- Port: 0,
- Scheduler: "wrr",
- Flags: 0,
- Timeout: 0,
- },
- svcB: &VirtualServer{
- Address: net.ParseIP("2017::beef"),
- Protocol: "",
- Port: 0,
- Scheduler: "wrr",
- Flags: 0,
- Timeout: 0,
- },
- equal: false,
- reason: "IPv6 address not equal",
- },
- {
- svcA: &VirtualServer{
- Address: net.ParseIP("2012::beef"),
- Protocol: "TCP",
- Port: 0,
- Scheduler: "wrr",
- Flags: 0,
- Timeout: 0,
- },
- svcB: &VirtualServer{
- Address: net.ParseIP("2012::beeef"),
- Protocol: "UDP",
- Port: 0,
- Scheduler: "wrr",
- Flags: 0,
- Timeout: 0,
- },
- equal: false,
- reason: "Protocol not equal",
- },
- {
- svcA: &VirtualServer{
- Address: net.ParseIP("2012::beef"),
- Protocol: "TCP",
- Port: 80,
- Scheduler: "wrr",
- Flags: 0,
- Timeout: 0,
- },
- svcB: &VirtualServer{
- Address: net.ParseIP("2012::beef"),
- Protocol: "TCP",
- Port: 8080,
- Scheduler: "wrr",
- Flags: 0,
- Timeout: 0,
- },
- equal: false,
- reason: "Port not equal",
- },
- {
- svcA: &VirtualServer{
- Address: net.ParseIP("1.2.3.4"),
- Protocol: "TCP",
- Port: 80,
- Scheduler: "rr",
- Flags: 0,
- Timeout: 0,
- },
- svcB: &VirtualServer{
- Address: net.ParseIP("1.2.3.4"),
- Protocol: "TCP",
- Port: 80,
- Scheduler: "wlc",
- Flags: 0,
- Timeout: 0,
- },
- equal: false,
- reason: "Scheduler not equal",
- },
- {
- svcA: &VirtualServer{
- Address: net.ParseIP("1.2.3.4"),
- Protocol: "TCP",
- Port: 80,
- Scheduler: "rr",
- Flags: 2,
- Timeout: 0,
- },
- svcB: &VirtualServer{
- Address: net.ParseIP("1.2.3.4"),
- Protocol: "TCP",
- Port: 80,
- Scheduler: "rr",
- Flags: 3,
- Timeout: 0,
- },
- equal: false,
- reason: "Flags not equal",
- },
- {
- svcA: &VirtualServer{
- Address: net.ParseIP("2012::beef"),
- Protocol: "",
- Port: 0,
- Scheduler: "wrr",
- Flags: 0,
- Timeout: 0,
- },
- svcB: &VirtualServer{
- Address: net.ParseIP("2012::beef"),
- Protocol: "",
- Port: 0,
- Scheduler: "wrr",
- Flags: 0,
- Timeout: 10800,
- },
- equal: false,
- reason: "Timeout not equal",
- },
- {
- svcA: &VirtualServer{
- Address: net.ParseIP("1.2.3.4"),
- Protocol: "TCP",
- Port: 80,
- Scheduler: "rr",
- Flags: 0x1,
- Timeout: 10800,
- },
- svcB: &VirtualServer{
- Address: net.ParseIP("1.2.3.4"),
- Protocol: "TCP",
- Port: 80,
- Scheduler: "rr",
- Flags: 0x1,
- Timeout: 10800,
- },
- equal: true,
- reason: "All fields equal",
- },
- {
- svcA: &VirtualServer{
- Address: net.ParseIP("2012::beef"),
- Protocol: "TCP",
- Port: 0,
- Scheduler: "wrr",
- Flags: 0,
- Timeout: 0,
- },
- svcB: &VirtualServer{
- Address: net.ParseIP("2012::beeef"),
- Protocol: "SCTP",
- Port: 0,
- Scheduler: "wrr",
- Flags: 0,
- Timeout: 0,
- },
- equal: false,
- reason: "Protocol not equal",
- },
- {
- svcA: &VirtualServer{
- Address: net.ParseIP("1.2.3.4"),
- Protocol: "SCTP",
- Port: 80,
- Scheduler: "rr",
- Flags: 0x1,
- Timeout: 10800,
- },
- svcB: &VirtualServer{
- Address: net.ParseIP("1.2.3.4"),
- Protocol: "SCTP",
- Port: 80,
- Scheduler: "rr",
- Flags: 0x1,
- Timeout: 10800,
- },
- equal: true,
- reason: "All fields equal",
- },
- }
- for i := range Tests {
- equal := Tests[i].svcA.Equal(Tests[i].svcB)
- if equal != Tests[i].equal {
- t.Errorf("case: %d got %v, expected %v, reason: %s", i, equal, Tests[i].equal, Tests[i].reason)
- }
- }
- }
- func TestRealServerEqual(t *testing.T) {
- Tests := []struct {
- rsA *RealServer
- rsB *RealServer
- equal bool
- reason string
- }{
- {
- rsA: &RealServer{
- Address: net.ParseIP("10.20.30.40"),
- Port: 80,
- },
- rsB: &RealServer{
- Address: net.ParseIP("10.20.30.41"),
- Port: 80,
- },
- equal: false,
- reason: "IPv4 address not equal",
- },
- {
- rsA: &RealServer{
- Address: net.ParseIP("2012::beef"),
- Port: 80,
- },
- rsB: &RealServer{
- Address: net.ParseIP("2017::beef"),
- Port: 80,
- },
- equal: false,
- reason: "IPv6 address not equal",
- },
- {
- rsA: &RealServer{
- Address: net.ParseIP("2012::beef"),
- Port: 80,
- },
- rsB: &RealServer{
- Address: net.ParseIP("2012::beef"),
- Port: 8080,
- },
- equal: false,
- reason: "Port not equal",
- },
- {
- rsA: &RealServer{
- Address: net.ParseIP("1.2.3.4"),
- Port: 3080,
- },
- rsB: &RealServer{
- Address: net.ParseIP("1.2.3.4"),
- Port: 3080,
- },
- equal: true,
- reason: "All fields equal",
- },
- {
- rsA: &RealServer{
- Address: net.ParseIP("2012::beef"),
- Port: 3080,
- },
- rsB: &RealServer{
- Address: net.ParseIP("2012::beef"),
- Port: 3080,
- },
- equal: true,
- reason: "All fields equal",
- },
- }
- for i := range Tests {
- equal := Tests[i].rsA.Equal(Tests[i].rsB)
- if equal != Tests[i].equal {
- t.Errorf("case: %d got %v, expected %v, reason: %s", i, equal, Tests[i].equal, Tests[i].reason)
- }
- }
- }
- func TestFrontendServiceString(t *testing.T) {
- Tests := []struct {
- svc *VirtualServer
- expected string
- }{
- {
- svc: &VirtualServer{
- Address: net.ParseIP("10.20.30.40"),
- Protocol: "TCP",
- Port: 80,
- },
- expected: "10.20.30.40:80/TCP",
- },
- {
- svc: &VirtualServer{
- Address: net.ParseIP("2012::beef"),
- Protocol: "UDP",
- Port: 8080,
- },
- expected: "[2012::beef]:8080/UDP",
- },
- {
- svc: &VirtualServer{
- Address: net.ParseIP("10.20.30.41"),
- Protocol: "ESP",
- Port: 1234,
- },
- expected: "10.20.30.41:1234/ESP",
- },
- }
- for i := range Tests {
- if Tests[i].expected != Tests[i].svc.String() {
- t.Errorf("case: %d got %v, expected %v", i, Tests[i].svc.String(), Tests[i].expected)
- }
- }
- }
- func TestFrontendDestinationString(t *testing.T) {
- Tests := []struct {
- svc *RealServer
- expected string
- }{
- {
- svc: &RealServer{
- Address: net.ParseIP("10.20.30.40"),
- Port: 80,
- },
- expected: "10.20.30.40:80",
- },
- {
- svc: &RealServer{
- Address: net.ParseIP("2012::beef"),
- Port: 8080,
- },
- expected: "[2012::beef]:8080",
- },
- }
- for i := range Tests {
- if Tests[i].expected != Tests[i].svc.String() {
- t.Errorf("case: %d got %v, expected %v", i, Tests[i].svc.String(), Tests[i].expected)
- }
- }
- }
- func TestGetRequiredIPVSModules(t *testing.T) {
- Tests := []struct {
- name string
- kernelVersion *version.Version
- want []string
- }{
- {
- name: "kernel version < 4.19",
- kernelVersion: version.MustParseGeneric("4.18"),
- want: []string{KernelModuleIPVS, KernelModuleIPVSRR, KernelModuleIPVSWRR, KernelModuleIPVSSH, KernelModuleNfConntrackIPV4},
- },
- {
- name: "kernel version 4.19",
- kernelVersion: version.MustParseGeneric("4.19"),
- want: []string{KernelModuleIPVS, KernelModuleIPVSRR, KernelModuleIPVSWRR, KernelModuleIPVSSH, KernelModuleNfConntrack},
- },
- }
- for _, test := range Tests {
- t.Run(test.name, func(t *testing.T) {
- got := GetRequiredIPVSModules(test.kernelVersion)
- sort.Strings(got)
- sort.Strings(test.want)
- if !reflect.DeepEqual(got, test.want) {
- t.Errorf("GetRequiredIPVSMods() = %v for kenel version: %s, want %v", got, test.kernelVersion, test.want)
- }
- })
- }
- }
|