...

Source file src/github.com/chaos-mesh/chaos-mesh/controllers/podnetworkchaos/netutils/cidr_test.go

Documentation: github.com/chaos-mesh/chaos-mesh/controllers/podnetworkchaos/netutils

     1  // Copyright 2021 Chaos Mesh Authors.
     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  
    16  package netutils
    17  
    18  import (
    19  	"net"
    20  	"reflect"
    21  	"testing"
    22  
    23  	"github.com/chaos-mesh/chaos-mesh/api/v1alpha1"
    24  	"github.com/chaos-mesh/chaos-mesh/pkg/mock"
    25  )
    26  
    27  func TestResolveCidr(t *testing.T) {
    28  	defer mock.With("LookupIP", []net.IP{{1, 1, 1, 1}, {2, 2, 2, 2}})()
    29  
    30  	type args struct {
    31  		name string
    32  	}
    33  	tests := []struct {
    34  		name    string
    35  		args    args
    36  		want    []v1alpha1.CidrAndPort
    37  		wantErr bool
    38  	}{
    39  		{
    40  			name: "ip address",
    41  			args: args{name: "1.1.1.1"},
    42  			want: []v1alpha1.CidrAndPort{{Cidr: "1.1.1.1/32"}},
    43  		},
    44  		{
    45  			name: "ip address and port",
    46  			args: args{name: "1.1.1.1:80"},
    47  			want: []v1alpha1.CidrAndPort{{Cidr: "1.1.1.1/32", Port: 80}},
    48  		},
    49  		{
    50  			name: "subnet",
    51  			args: args{name: "0.0.0.0/24"},
    52  			want: []v1alpha1.CidrAndPort{{Cidr: "0.0.0.0/24"}},
    53  		},
    54  		{
    55  			name: "subnet and port",
    56  			args: args{name: "0.0.0.0/24:443"},
    57  			want: []v1alpha1.CidrAndPort{{Cidr: "0.0.0.0/24", Port: 443}},
    58  		},
    59  		{
    60  			name: "hostname",
    61  			args: args{name: "example.com"},
    62  			want: []v1alpha1.CidrAndPort{{Cidr: "1.1.1.1/32"}, {Cidr: "2.2.2.2/32"}},
    63  		},
    64  		{
    65  			name: "hostname and port",
    66  			args: args{name: "example.com:80"},
    67  			want: []v1alpha1.CidrAndPort{{Cidr: "1.1.1.1/32", Port: 80}, {Cidr: "2.2.2.2/32", Port: 80}},
    68  		},
    69  		{
    70  			name:    "missing port",
    71  			args:    args{name: "1.1.1.1:"},
    72  			wantErr: true,
    73  		},
    74  		{
    75  			name:    "port out of range",
    76  			args:    args{name: "1.1.1.1:65536"},
    77  			wantErr: true,
    78  		},
    79  		{
    80  			name:    "not a port",
    81  			args:    args{name: "1.1.1.1:foo"},
    82  			wantErr: true,
    83  		},
    84  	}
    85  	for _, tt := range tests {
    86  		t.Run(tt.name, func(t *testing.T) {
    87  			got, err := ResolveCidr(tt.args.name)
    88  			if (err != nil) != tt.wantErr {
    89  				t.Errorf("ResolveCidr() error = %v, wantErr %v", err, tt.wantErr)
    90  				return
    91  			}
    92  			if !reflect.DeepEqual(got, tt.want) {
    93  				t.Errorf("ResolveCidr() got = %v, want %v", got, tt.want)
    94  			}
    95  		})
    96  	}
    97  }
    98