github.com/sealerio/sealer@v0.11.1-0.20240507115618-f4f89c5853ae/utils/net/iputils_test.go (about)

     1  // Copyright © 2021 Alibaba Group Holding Ltd.
     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  package net
    16  
    17  import (
    18  	"net"
    19  	"testing"
    20  
    21  	"github.com/stretchr/testify/assert"
    22  
    23  	"github.com/sirupsen/logrus"
    24  )
    25  
    26  func TestAssemblyIPList(t *testing.T) {
    27  	tests := []struct {
    28  		name    string
    29  		ipStr   string
    30  		wantErr bool
    31  	}{
    32  		{
    33  			name:    "baseData1",
    34  			ipStr:   "10.110.101.1-10.110.101.5",
    35  			wantErr: false,
    36  		},
    37  		/*{
    38  			name:    "baseData2",
    39  			ipStr:   "0.0.0.0-0.0.0.1",
    40  			wantErr: false,
    41  		},*/
    42  		{
    43  			name:    "errorData",
    44  			ipStr:   "10.110.101.10-10.110.101.5",
    45  			wantErr: true,
    46  		},
    47  		{
    48  			name:    "errorData2",
    49  			ipStr:   "10.110.101.10-10.110.101.5-10.110.101.55",
    50  			wantErr: true,
    51  		},
    52  		{
    53  			name:    "errorData3",
    54  			ipStr:   "-10.110.101.",
    55  			wantErr: true,
    56  		},
    57  		{
    58  			name:    "errorData4",
    59  			ipStr:   "10.110.101.1-",
    60  			wantErr: true,
    61  		},
    62  		{
    63  			name:    "errorData5",
    64  			ipStr:   "a-b",
    65  			wantErr: true,
    66  		},
    67  		{
    68  			name:    "errorData6",
    69  			ipStr:   "-b",
    70  			wantErr: true,
    71  		},
    72  	}
    73  	for _, tt := range tests {
    74  		t.Run(tt.name, func(t *testing.T) {
    75  			logrus.Infof("start to test case %s", tt.name)
    76  			resultIPStr, err := TransferToIPList(tt.ipStr)
    77  			if err != nil && tt.wantErr == false {
    78  				t.Errorf("input ipStr(%s), found non-nil error(%v), but expect nil error. returned ipStr(%s)", tt.ipStr, err, resultIPStr)
    79  			}
    80  			if err == nil && tt.wantErr == true {
    81  				t.Errorf("input ipStr(%s), found nil error, but expect non-nil error,returned ipStr(%s)", tt.ipStr, resultIPStr)
    82  			}
    83  		})
    84  	}
    85  }
    86  
    87  func TestIPStrsToIPs(t *testing.T) {
    88  	tests := []struct {
    89  		name        string
    90  		inputIPStrs []string
    91  		wantedIPs   []net.IP
    92  	}{
    93  		{
    94  			name:        "baseData1",
    95  			inputIPStrs: []string{"10.110.101.1"},
    96  			wantedIPs:   []net.IP{net.ParseIP("10.110.101.1")},
    97  		},
    98  		{
    99  			name:        "baseData2",
   100  			inputIPStrs: []string{"10.110.101.1", "sdfghjkl"},
   101  			wantedIPs:   []net.IP{net.ParseIP("10.110.101.1"), nil},
   102  		},
   103  		{
   104  			name:        "baseData2",
   105  			inputIPStrs: []string{"10.110.101.1", "10.110.101.100"},
   106  			wantedIPs:   []net.IP{net.ParseIP("10.110.101.1"), net.ParseIP("10.110.101.100")},
   107  		},
   108  		{
   109  			name:        "empty input of nil",
   110  			inputIPStrs: nil,
   111  			wantedIPs:   nil,
   112  		},
   113  		{
   114  			name:        "non-empty input with empty string",
   115  			inputIPStrs: []string{""},
   116  			wantedIPs:   []net.IP{},
   117  		},
   118  	}
   119  	for _, tt := range tests {
   120  		t.Run(tt.name, func(t *testing.T) {
   121  			logrus.Infof("start to test case %s", tt.name)
   122  			ips := IPStrsToIPs(tt.inputIPStrs)
   123  			if !equalNetIPs(ips, tt.wantedIPs) {
   124  				t.Errorf("wanted ips is (%s), but got (%s)", tt.wantedIPs, ips)
   125  			}
   126  		})
   127  	}
   128  }
   129  
   130  func TestIPsToIPStrs(t *testing.T) {
   131  	tests := []struct {
   132  		name         string
   133  		inputIPs     []net.IP
   134  		wantedIPStrs []string
   135  	}{
   136  		{
   137  			name:         "baseData1",
   138  			inputIPs:     []net.IP{net.ParseIP("10.110.101.1")},
   139  			wantedIPStrs: []string{"10.110.101.1"},
   140  		},
   141  		{
   142  			name:         "baseData2",
   143  			inputIPs:     []net.IP{net.ParseIP("10.110.101.1"), net.ParseIP("10.110.101.2")},
   144  			wantedIPStrs: []string{"10.110.101.1", "10.110.101.2"},
   145  		},
   146  		{
   147  			name:         "baseData3",
   148  			inputIPs:     []net.IP{net.ParseIP("10.110.101.1"), net.ParseIP("10.110.101.653")},
   149  			wantedIPStrs: []string{"10.110.101.1", "<nil>"},
   150  		},
   151  		{
   152  			name:         "empty input of nil",
   153  			inputIPs:     nil,
   154  			wantedIPStrs: nil,
   155  		},
   156  		{
   157  			name:         "non-empty input with empty string",
   158  			inputIPs:     []net.IP{nil},
   159  			wantedIPStrs: []string{"<nil>"},
   160  		},
   161  	}
   162  	for _, tt := range tests {
   163  		t.Run(tt.name, func(t *testing.T) {
   164  			logrus.Infof("start to test case %s", tt.name)
   165  			ipStrs := IPsToIPStrs(tt.inputIPs)
   166  			if !equalIPStrs(ipStrs, tt.wantedIPStrs) {
   167  				t.Errorf("wanted IP strings is (%s), but got (%s)", tt.wantedIPStrs, ipStrs)
   168  			}
   169  		})
   170  	}
   171  }
   172  
   173  func Test_returnFilteredIPList(t *testing.T) {
   174  	tests := []struct {
   175  		name              string
   176  		clusterIPList     []net.IP
   177  		toBeDeletedIPList []net.IP
   178  		IPListExpected    []net.IP
   179  	}{
   180  		{
   181  			"test",
   182  			[]net.IP{net.ParseIP("10.10.10.1"), net.ParseIP("10.10.10.2"), net.ParseIP("10.10.10.3"), net.ParseIP("10.10.10.4")},
   183  			[]net.IP{net.ParseIP("10.10.10.1"), net.ParseIP("10.10.10.2"), net.ParseIP("10.10.10.3"), net.ParseIP("10.10.10.4")},
   184  			[]net.IP{},
   185  		},
   186  		{
   187  			"test1",
   188  			[]net.IP{net.ParseIP("10.10.10.1"), net.ParseIP("10.10.10.2"), net.ParseIP("10.10.10.3"), net.ParseIP("10.10.10.4")},
   189  			[]net.IP{},
   190  			[]net.IP{net.ParseIP("10.10.10.1"), net.ParseIP("10.10.10.2"), net.ParseIP("10.10.10.3"), net.ParseIP("10.10.10.4")},
   191  		},
   192  		{
   193  			"test2",
   194  			[]net.IP{net.ParseIP("10.10.10.1"), net.ParseIP("10.10.10.2"), net.ParseIP("10.10.10.3"), net.ParseIP("10.10.10.4")},
   195  			[]net.IP{net.ParseIP("10.10.10.4")},
   196  			[]net.IP{net.ParseIP("10.10.10.1"), net.ParseIP("10.10.10.2"), net.ParseIP("10.10.10.3")},
   197  		},
   198  		{
   199  			"test3",
   200  			[]net.IP{},
   201  			[]net.IP{net.ParseIP("10.10.10.1"), net.ParseIP("10.10.10.2"), net.ParseIP("10.10.10.3"), net.ParseIP("10.10.10.4")},
   202  			[]net.IP{},
   203  		},
   204  	}
   205  	for _, tt := range tests {
   206  		t.Run(tt.name, func(t *testing.T) {
   207  			if res := RemoveIPs(tt.clusterIPList, tt.toBeDeletedIPList); res != nil {
   208  				assert.Equal(t, tt.IPListExpected, res)
   209  			}
   210  		})
   211  	}
   212  }
   213  
   214  func equalNetIPs(a, b []net.IP) bool {
   215  	if len(a) != len(b) {
   216  		return false
   217  	}
   218  	for index := range a {
   219  		if !a[index].Equal(b[index]) {
   220  			return false
   221  		}
   222  	}
   223  	return true
   224  }
   225  
   226  func equalIPStrs(a, b []string) bool {
   227  	if len(a) != len(b) {
   228  		return false
   229  	}
   230  	for index := range a {
   231  		if a[index] != b[index] {
   232  			return false
   233  		}
   234  	}
   235  	return true
   236  }