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

     1  //  Copyright (c) 2018 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 vpp_acl
    16  
    17  import (
    18  	"testing"
    19  )
    20  
    21  /*func TestACLKey(t *testing.T) {
    22  	tests := []struct {
    23  		name        string
    24  		aclName     string
    25  		expectedKey string
    26  	}{
    27  		{
    28  			name:        "valid ACL name",
    29  			aclName:     "acl1",
    30  			expectedKey: "vpp/config/v2/acl/acl1",
    31  		},
    32  		{
    33  			name:        "invalid ACL name",
    34  			aclName:     "",
    35  			expectedKey: "vpp/config/v2/acl/<invalid>",
    36  		},
    37  	}
    38  	for _, test := range tests {
    39  		t.Run(test.name, func(t *testing.T) {
    40  			key := Key(test.aclName)
    41  			if key != test.expectedKey {
    42  				t.Errorf("failed for: aclName=%s\n"+
    43  					"expected key:\n\t%q\ngot key:\n\t%q",
    44  					test.aclName, test.expectedKey, key)
    45  			}
    46  		})
    47  	}
    48  }
    49  
    50  func TestParseNameFromKey(t *testing.T) {
    51  	tests := []struct {
    52  		name             string
    53  		key              string
    54  		expectedACLName  string
    55  		expectedIsACLKey bool
    56  	}{
    57  		{
    58  			name:             "valid ACL name",
    59  			key:              "vpp/config/v2/acl/acl1",
    60  			expectedACLName:  "acl1",
    61  			expectedIsACLKey: true,
    62  		},
    63  		{
    64  			name:             "invalid ACL name",
    65  			key:              "vpp/config/v2/acl/<invalid>",
    66  			expectedACLName:  "<invalid>",
    67  			expectedIsACLKey: true,
    68  		},
    69  		{
    70  			name:             "not an ACL key",
    71  			key:              "vpp/config/v2/bd/bd1",
    72  			expectedACLName:  "",
    73  			expectedIsACLKey: false,
    74  		},
    75  		{
    76  			name:             "not an ACL key (empty name)",
    77  			key:              "vpp/config/v2/acl/",
    78  			expectedACLName:  "",
    79  			expectedIsACLKey: false,
    80  		},
    81  	}
    82  	for _, test := range tests {
    83  		t.Run(test.name, func(t *testing.T) {
    84  			aclName, isACLKey := models.Model(&Acl{}).ParseKey(test.key)
    85  			if isACLKey != test.expectedIsACLKey {
    86  				t.Errorf("expected isACLKey: %v\tgot: %v", test.expectedIsACLKey, isACLKey)
    87  			}
    88  			if aclName != test.expectedACLName {
    89  				t.Errorf("expected aclName: %s\tgot: %s", test.expectedACLName, aclName)
    90  			}
    91  		})
    92  	}
    93  }*/
    94  
    95  func TestACLToInterfaceKey(t *testing.T) {
    96  	tests := []struct {
    97  		name        string
    98  		aclName     string
    99  		iface       string
   100  		flow        string
   101  		expectedKey string
   102  	}{
   103  		{
   104  			name:        "ingress interface",
   105  			aclName:     "acl1",
   106  			iface:       "tap0",
   107  			flow:        "ingress",
   108  			expectedKey: "vpp/acl/acl1/interface/ingress/tap0",
   109  		},
   110  		{
   111  			name:        "egress interface",
   112  			aclName:     "acl2",
   113  			iface:       "memif0",
   114  			flow:        "egress",
   115  			expectedKey: "vpp/acl/acl2/interface/egress/memif0",
   116  		},
   117  		{
   118  			name:        "Gbe interface",
   119  			aclName:     "acl1",
   120  			iface:       "GigabitEthernet0/8/0",
   121  			flow:        "ingress",
   122  			expectedKey: "vpp/acl/acl1/interface/ingress/GigabitEthernet0/8/0",
   123  		},
   124  		{
   125  			name:        "empty acl name",
   126  			aclName:     "",
   127  			iface:       "memif0",
   128  			flow:        "egress",
   129  			expectedKey: "vpp/acl/<invalid>/interface/egress/memif0",
   130  		},
   131  		{
   132  			name:        "invalid flow",
   133  			aclName:     "acl2",
   134  			iface:       "memif0",
   135  			flow:        "invalid-value",
   136  			expectedKey: "vpp/acl/acl2/interface/<invalid>/memif0",
   137  		},
   138  		{
   139  			name:        "empty interface",
   140  			aclName:     "acl2",
   141  			iface:       "",
   142  			flow:        "egress",
   143  			expectedKey: "vpp/acl/acl2/interface/egress/<invalid>",
   144  		},
   145  		{
   146  			name:        "empty parameters",
   147  			aclName:     "",
   148  			iface:       "",
   149  			flow:        "",
   150  			expectedKey: "vpp/acl/<invalid>/interface/<invalid>/<invalid>",
   151  		},
   152  	}
   153  	for _, test := range tests {
   154  		t.Run(test.name, func(t *testing.T) {
   155  			key := ToInterfaceKey(test.aclName, test.iface, test.flow)
   156  			if key != test.expectedKey {
   157  				t.Errorf("failed for: aclName=%s iface=%s flow=%s\n"+
   158  					"expected key:\n\t%q\ngot key:\n\t%q",
   159  					test.aclName, test.iface, test.flow, test.expectedKey, key)
   160  			}
   161  		})
   162  	}
   163  }
   164  
   165  func TestParseACLToInterfaceKey(t *testing.T) {
   166  	tests := []struct {
   167  		name                  string
   168  		key                   string
   169  		expectedACLName       string
   170  		expectedIface         string
   171  		expectedFlow          string
   172  		expectedIsACLIfaceKey bool
   173  	}{
   174  		{
   175  			name:                  "ingress interface",
   176  			key:                   "vpp/acl/acl1/interface/ingress/tap0",
   177  			expectedACLName:       "acl1",
   178  			expectedIface:         "tap0",
   179  			expectedFlow:          IngressFlow,
   180  			expectedIsACLIfaceKey: true,
   181  		},
   182  		{
   183  			name:                  "egress interface",
   184  			key:                   "vpp/acl/acl1/interface/egress/tap0",
   185  			expectedACLName:       "acl1",
   186  			expectedIface:         "tap0",
   187  			expectedFlow:          EgressFlow,
   188  			expectedIsACLIfaceKey: true,
   189  		},
   190  		{
   191  			name:                  "multi-part acl and interface name",
   192  			key:                   "vpp/acl/base/test/acl1/interface/egress/local/tap0",
   193  			expectedACLName:       "base/test/acl1",
   194  			expectedIface:         "local/tap0",
   195  			expectedFlow:          EgressFlow,
   196  			expectedIsACLIfaceKey: true,
   197  		},
   198  		{
   199  			name:                  "Gbe interface",
   200  			key:                   "vpp/acl/acl1/interface/ingress/GigabitEthernet0/8/0",
   201  			expectedACLName:       "acl1",
   202  			expectedIface:         "GigabitEthernet0/8/0",
   203  			expectedFlow:          IngressFlow,
   204  			expectedIsACLIfaceKey: true,
   205  		},
   206  		{
   207  			name:                  "invalid acl name",
   208  			key:                   "vpp/acl/<invalid>/interface/egress/tap0",
   209  			expectedACLName:       "<invalid>",
   210  			expectedIface:         "tap0",
   211  			expectedFlow:          EgressFlow,
   212  			expectedIsACLIfaceKey: true,
   213  		},
   214  		{
   215  			name:                  "invalid flow",
   216  			key:                   "vpp/acl/acl1/interface/<invalid>/tap0",
   217  			expectedACLName:       "acl1",
   218  			expectedIface:         "tap0",
   219  			expectedFlow:          "<invalid>",
   220  			expectedIsACLIfaceKey: true,
   221  		},
   222  		{
   223  			name:                  "invalid interface",
   224  			key:                   "vpp/acl/acl1/interface/ingress/<invalid>",
   225  			expectedACLName:       "acl1",
   226  			expectedIface:         "<invalid>",
   227  			expectedFlow:          IngressFlow,
   228  			expectedIsACLIfaceKey: true,
   229  		},
   230  		{
   231  			name:                  "all parameters invalid",
   232  			key:                   "vpp/acl/<invalid>/interface/<invalid>/<invalid>",
   233  			expectedACLName:       "<invalid>",
   234  			expectedIface:         "<invalid>",
   235  			expectedFlow:          "<invalid>",
   236  			expectedIsACLIfaceKey: true,
   237  		},
   238  		{
   239  			name:                  "not ACLToInterface key",
   240  			key:                   "vpp/config/v2/acl/acl1",
   241  			expectedACLName:       "",
   242  			expectedIface:         "",
   243  			expectedFlow:          "",
   244  			expectedIsACLIfaceKey: false,
   245  		},
   246  		{
   247  			name:                  "not ACLToInterface key (cut after interface)",
   248  			key:                   "vpp/acl/acl1/interface/",
   249  			expectedACLName:       "",
   250  			expectedIface:         "",
   251  			expectedFlow:          "",
   252  			expectedIsACLIfaceKey: false,
   253  		},
   254  		{
   255  			name:                  "not ACLToInterface key (cut after interface, no traling slash)",
   256  			key:                   "vpp/acl/base/acl1/interface",
   257  			expectedACLName:       "",
   258  			expectedIface:         "",
   259  			expectedFlow:          "",
   260  			expectedIsACLIfaceKey: false,
   261  		},
   262  		{
   263  			name:                  "not ACLToInterface key (cut after flow)",
   264  			key:                   "vpp/acl/acl1/interface/ingress",
   265  			expectedACLName:       "",
   266  			expectedIface:         "",
   267  			expectedFlow:          "",
   268  			expectedIsACLIfaceKey: false,
   269  		},
   270  		{
   271  			name:                  "empty key",
   272  			key:                   "",
   273  			expectedACLName:       "",
   274  			expectedIface:         "",
   275  			expectedFlow:          "",
   276  			expectedIsACLIfaceKey: false,
   277  		},
   278  	}
   279  	for _, test := range tests {
   280  		t.Run(test.name, func(t *testing.T) {
   281  			aclName, iface, flow, isACLIfaceKey := ParseACLToInterfaceKey(test.key)
   282  			if isACLIfaceKey != test.expectedIsACLIfaceKey {
   283  				t.Errorf("expected isACLKey: %v\tgot: %v", test.expectedIsACLIfaceKey, isACLIfaceKey)
   284  			}
   285  			if aclName != test.expectedACLName {
   286  				t.Errorf("expected aclName: %s\tgot: %s", test.expectedACLName, aclName)
   287  			}
   288  			if iface != test.expectedIface {
   289  				t.Errorf("expected iface: %s\tgot: %s", test.expectedIface, iface)
   290  			}
   291  			if flow != test.expectedFlow {
   292  				t.Errorf("expected flow: %s\tgot: %s", test.expectedFlow, flow)
   293  			}
   294  		})
   295  	}
   296  }