123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232 |
- /*
- Copyright 2018 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 nodeinfo
- import (
- "testing"
- )
- type hostPortInfoParam struct {
- protocol, ip string
- port int32
- }
- func TestHostPortInfo_AddRemove(t *testing.T) {
- tests := []struct {
- desc string
- added []hostPortInfoParam
- removed []hostPortInfoParam
- length int
- }{
- {
- desc: "normal add case",
- added: []hostPortInfoParam{
- {"TCP", "127.0.0.1", 79},
- {"UDP", "127.0.0.1", 80},
- {"TCP", "127.0.0.1", 81},
- {"TCP", "127.0.0.1", 82},
- // this might not make sense in real case, but the struct doesn't forbid it.
- {"TCP", "0.0.0.0", 79},
- {"UDP", "0.0.0.0", 80},
- {"TCP", "0.0.0.0", 81},
- {"TCP", "0.0.0.0", 82},
- {"TCP", "0.0.0.0", 0},
- {"TCP", "0.0.0.0", -1},
- },
- length: 8,
- },
- {
- desc: "empty ip and protocol add should work",
- added: []hostPortInfoParam{
- {"", "127.0.0.1", 79},
- {"UDP", "127.0.0.1", 80},
- {"", "127.0.0.1", 81},
- {"", "127.0.0.1", 82},
- {"", "", 79},
- {"UDP", "", 80},
- {"", "", 81},
- {"", "", 82},
- {"", "", 0},
- {"", "", -1},
- },
- length: 8,
- },
- {
- desc: "normal remove case",
- added: []hostPortInfoParam{
- {"TCP", "127.0.0.1", 79},
- {"UDP", "127.0.0.1", 80},
- {"TCP", "127.0.0.1", 81},
- {"TCP", "127.0.0.1", 82},
- {"TCP", "0.0.0.0", 79},
- {"UDP", "0.0.0.0", 80},
- {"TCP", "0.0.0.0", 81},
- {"TCP", "0.0.0.0", 82},
- },
- removed: []hostPortInfoParam{
- {"TCP", "127.0.0.1", 79},
- {"UDP", "127.0.0.1", 80},
- {"TCP", "127.0.0.1", 81},
- {"TCP", "127.0.0.1", 82},
- {"TCP", "0.0.0.0", 79},
- {"UDP", "0.0.0.0", 80},
- {"TCP", "0.0.0.0", 81},
- {"TCP", "0.0.0.0", 82},
- },
- length: 0,
- },
- {
- desc: "empty ip and protocol remove should work",
- added: []hostPortInfoParam{
- {"TCP", "127.0.0.1", 79},
- {"UDP", "127.0.0.1", 80},
- {"TCP", "127.0.0.1", 81},
- {"TCP", "127.0.0.1", 82},
- {"TCP", "0.0.0.0", 79},
- {"UDP", "0.0.0.0", 80},
- {"TCP", "0.0.0.0", 81},
- {"TCP", "0.0.0.0", 82},
- },
- removed: []hostPortInfoParam{
- {"", "127.0.0.1", 79},
- {"", "127.0.0.1", 81},
- {"", "127.0.0.1", 82},
- {"UDP", "127.0.0.1", 80},
- {"", "", 79},
- {"", "", 81},
- {"", "", 82},
- {"UDP", "", 80},
- },
- length: 0,
- },
- }
- for _, test := range tests {
- hp := make(HostPortInfo)
- for _, param := range test.added {
- hp.Add(param.ip, param.protocol, param.port)
- }
- for _, param := range test.removed {
- hp.Remove(param.ip, param.protocol, param.port)
- }
- if hp.Len() != test.length {
- t.Errorf("%v failed: expect length %d; got %d", test.desc, test.length, hp.Len())
- t.Error(hp)
- }
- }
- }
- func TestHostPortInfo_Check(t *testing.T) {
- tests := []struct {
- desc string
- added []hostPortInfoParam
- check hostPortInfoParam
- expect bool
- }{
- {
- desc: "empty check should check 0.0.0.0 and TCP",
- added: []hostPortInfoParam{
- {"TCP", "127.0.0.1", 80},
- },
- check: hostPortInfoParam{"", "", 81},
- expect: false,
- },
- {
- desc: "empty check should check 0.0.0.0 and TCP (conflicted)",
- added: []hostPortInfoParam{
- {"TCP", "127.0.0.1", 80},
- },
- check: hostPortInfoParam{"", "", 80},
- expect: true,
- },
- {
- desc: "empty port check should pass",
- added: []hostPortInfoParam{
- {"TCP", "127.0.0.1", 80},
- },
- check: hostPortInfoParam{"", "", 0},
- expect: false,
- },
- {
- desc: "0.0.0.0 should check all registered IPs",
- added: []hostPortInfoParam{
- {"TCP", "127.0.0.1", 80},
- },
- check: hostPortInfoParam{"TCP", "0.0.0.0", 80},
- expect: true,
- },
- {
- desc: "0.0.0.0 with different protocol should be allowed",
- added: []hostPortInfoParam{
- {"UDP", "127.0.0.1", 80},
- },
- check: hostPortInfoParam{"TCP", "0.0.0.0", 80},
- expect: false,
- },
- {
- desc: "0.0.0.0 with different port should be allowed",
- added: []hostPortInfoParam{
- {"TCP", "127.0.0.1", 79},
- {"TCP", "127.0.0.1", 81},
- {"TCP", "127.0.0.1", 82},
- },
- check: hostPortInfoParam{"TCP", "0.0.0.0", 80},
- expect: false,
- },
- {
- desc: "normal ip should check all registered 0.0.0.0",
- added: []hostPortInfoParam{
- {"TCP", "0.0.0.0", 80},
- },
- check: hostPortInfoParam{"TCP", "127.0.0.1", 80},
- expect: true,
- },
- {
- desc: "normal ip with different port/protocol should be allowed (0.0.0.0)",
- added: []hostPortInfoParam{
- {"TCP", "0.0.0.0", 79},
- {"UDP", "0.0.0.0", 80},
- {"TCP", "0.0.0.0", 81},
- {"TCP", "0.0.0.0", 82},
- },
- check: hostPortInfoParam{"TCP", "127.0.0.1", 80},
- expect: false,
- },
- {
- desc: "normal ip with different port/protocol should be allowed",
- added: []hostPortInfoParam{
- {"TCP", "127.0.0.1", 79},
- {"UDP", "127.0.0.1", 80},
- {"TCP", "127.0.0.1", 81},
- {"TCP", "127.0.0.1", 82},
- },
- check: hostPortInfoParam{"TCP", "127.0.0.1", 80},
- expect: false,
- },
- }
- for _, test := range tests {
- hp := make(HostPortInfo)
- for _, param := range test.added {
- hp.Add(param.ip, param.protocol, param.port)
- }
- if hp.CheckConflict(test.check.ip, test.check.protocol, test.check.port) != test.expect {
- t.Errorf("%v failed, expected %t; got %t", test.desc, test.expect, !test.expect)
- }
- }
- }
|