go.ligato.io/vpp-agent/v3@v3.5.0/proto/ligato/linux/l3/models_test.go (about)

     1  //  Copyright (c) 2019 Cisco and/or its affiliates.
     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 linux_l3
    16  
    17  import (
    18  	"testing"
    19  )
    20  
    21  func TestRouteKey(t *testing.T) {
    22  	tests := []struct {
    23  		name        string
    24  		outIface    string
    25  		dstNetwork  string
    26  		expectedKey string
    27  	}{
    28  		{
    29  			name:        "IPv4 dest address",
    30  			outIface:    "memif1",
    31  			dstNetwork:  "192.168.1.0/24",
    32  			expectedKey: "config/linux/l3/v2/route/192.168.1.0/24/memif1",
    33  		},
    34  		{
    35  			name:        "dest address obtained from netalloc",
    36  			outIface:    "memif1",
    37  			dstNetwork:  "alloc:net1/memif2",
    38  			expectedKey: "config/linux/l3/v2/route/alloc:net1/memif2/memif1",
    39  		},
    40  	}
    41  	for _, test := range tests {
    42  		t.Run(test.name, func(t *testing.T) {
    43  			key := RouteKey(test.dstNetwork, test.outIface)
    44  			if key != test.expectedKey {
    45  				t.Errorf("failed for: outIface=%s dstNet=%s\n"+
    46  					"expected key:\n\t%q\ngot key:\n\t%q",
    47  					test.outIface, test.dstNetwork, test.expectedKey, key)
    48  			}
    49  		})
    50  	}
    51  }
    52  
    53  func TestStaticLinkLocalRouteKey(t *testing.T) {
    54  	tests := []struct {
    55  		name        string
    56  		dstAddr     string
    57  		outIface    string
    58  		expectedKey string
    59  	}{
    60  		{
    61  			name:        "IPv4 address via memif",
    62  			outIface:    "memif0",
    63  			dstAddr:     "192.168.1.12/24",
    64  			expectedKey: "linux/link-local-route/memif0/dest-address/192.168.1.12/24",
    65  		},
    66  	}
    67  	for _, test := range tests {
    68  		t.Run(test.name, func(t *testing.T) {
    69  			key := StaticLinkLocalRouteKey(test.dstAddr, test.outIface)
    70  			if key != test.expectedKey {
    71  				t.Errorf("failed for: iface=%s address=%s\n"+
    72  					"expected key:\n\t%q\ngot key:\n\t%q",
    73  					test.outIface, test.dstAddr, test.expectedKey, key)
    74  			}
    75  		})
    76  	}
    77  }
    78  
    79  func TestParseStaticLinkLocalRouteKey(t *testing.T) {
    80  	tests := []struct {
    81  		name                        string
    82  		key                         string
    83  		expectedIface               string
    84  		expectedDstAddr             string
    85  		expectedIsLinkLocalRouteKey bool
    86  	}{
    87  		{
    88  			name:                        "IPv4 address via memif",
    89  			key:                         "linux/link-local-route/memif0/dest-address/192.168.1.12/24",
    90  			expectedIface:               "memif0",
    91  			expectedDstAddr:             "192.168.1.12/24",
    92  			expectedIsLinkLocalRouteKey: true,
    93  		},
    94  	}
    95  	for _, test := range tests {
    96  		t.Run(test.name, func(t *testing.T) {
    97  			dstAddr, outIface, isLinkLocalRouteKey := ParseStaticLinkLocalRouteKey(test.key)
    98  			if isLinkLocalRouteKey != test.expectedIsLinkLocalRouteKey {
    99  				t.Errorf("expected isLinkLocalRouteKey: %v\tgot: %v", test.expectedIsLinkLocalRouteKey, isLinkLocalRouteKey)
   100  			}
   101  			if outIface != test.expectedIface {
   102  				t.Errorf("expected iface: %s\tgot: %s", test.expectedIface, outIface)
   103  			}
   104  			if dstAddr != test.expectedDstAddr {
   105  				t.Errorf("expected dstAddr: %s\tgot: %s", test.expectedDstAddr, dstAddr)
   106  			}
   107  		})
   108  	}
   109  }