From 1400d6154bc7786a1f60034ad6079980e2d1349f Mon Sep 17 00:00:00 2001 From: Nikita Skrynnik Date: Wed, 9 Aug 2023 15:28:12 +0700 Subject: [PATCH 1/5] fix selector Signed-off-by: Nikita Skrynnik --- pkg/api/networkservice/connection_helpers.go | 27 +++++++++++++------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/pkg/api/networkservice/connection_helpers.go b/pkg/api/networkservice/connection_helpers.go index 7a85cba..08ab5d5 100644 --- a/pkg/api/networkservice/connection_helpers.go +++ b/pkg/api/networkservice/connection_helpers.go @@ -124,7 +124,7 @@ func (x *Connection) IsComplete() error { return nil } -// MatchesMonitorScopeSelector - Returns true of the connection matches the selector +// MatchesMonitorScopeSelector - Returns true if the connection matches the selector func (x *Connection) MatchesMonitorScopeSelector(selector *MonitorScopeSelector) bool { if x == nil { return false @@ -133,24 +133,33 @@ func (x *Connection) MatchesMonitorScopeSelector(selector *MonitorScopeSelector) if len(selector.GetPathSegments()) == 0 { return true } - // Iterate through the Connection.NetworkServiceManagers array looking for a subarray that matches - // the selector.NetworkServiceManagers array, treating "" in the selector.NetworkServiceManagers array + // Iterate through the Connection.PathSegments array looking for a subarray that matches + // the selector.PathSegments array, treating "" in the selector.PathSegments array // as a wildcard for i := range x.GetPath().GetPathSegments() { - // If there aren't enough elements left in the Connection.NetworkServiceManagers array to match - // all of the elements in the select.NetworkServiceManager array...clearly we can't match + // If there aren't enough elements left in the Connection.PathSegments array to match + // all of the elements in the select.PathSegments array...clearly we can't match if i+len(selector.GetPathSegments()) > len(x.GetPath().GetPathSegments()) { return false } - // Iterate through the selector.NetworkServiceManagers array to see is the subarray starting at - // Connection.NetworkServiceManagers[i] matches selector.NetworkServiceManagers + // Iterate through the selector.PathSegments array to see is the subarray starting at + // Connection.PathSegments[i] matches selector.PathSegments for j := range selector.GetPathSegments() { // "" matches as a wildcard... failure to match either as wildcard or exact match means the subarray - // starting at Connection.NetworkServiceManagers[i] doesn't match selectors.NetworkServiceManagers + // starting at Connection.PathSegments[i] doesn't match selectors.NetworkServiceManagers if selector.GetPathSegments()[j].GetName() != "" && x.GetPath().GetPathSegments()[i+j].GetName() != selector.GetPathSegments()[j].GetName() { break } - // If this is the last element in the selector.NetworkServiceManagers array and we still are matching... + + if selector.GetPathSegments()[j].GetId() != "" && x.GetPath().GetPathSegments()[i+j].GetId() != selector.GetPathSegments()[j].GetId() { + break + } + + if selector.GetPathSegments()[j].GetToken() != "" && x.GetPath().GetPathSegments()[i+j].GetToken() != selector.GetPathSegments()[j].GetToken() { + break + } + + // If this is the last element in the selector.PathSegments array and we still are matching... // return true if j == len(selector.GetPathSegments())-1 { return true From af3bbcce8ac5c2d165d8509af3d20bb1a77d8801 Mon Sep 17 00:00:00 2001 From: Nikita Skrynnik Date: Wed, 9 Aug 2023 15:29:42 +0700 Subject: [PATCH 2/5] minor check Signed-off-by: Nikita Skrynnik --- pkg/api/networkservice/connection_helpers.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/api/networkservice/connection_helpers.go b/pkg/api/networkservice/connection_helpers.go index 08ab5d5..9770ac2 100644 --- a/pkg/api/networkservice/connection_helpers.go +++ b/pkg/api/networkservice/connection_helpers.go @@ -146,7 +146,7 @@ func (x *Connection) MatchesMonitorScopeSelector(selector *MonitorScopeSelector) // Connection.PathSegments[i] matches selector.PathSegments for j := range selector.GetPathSegments() { // "" matches as a wildcard... failure to match either as wildcard or exact match means the subarray - // starting at Connection.PathSegments[i] doesn't match selectors.NetworkServiceManagers + // starting at Connection.PathSegments[i] doesn't match selectors.PathSegments if selector.GetPathSegments()[j].GetName() != "" && x.GetPath().GetPathSegments()[i+j].GetName() != selector.GetPathSegments()[j].GetName() { break } From 6d893b1cc950fc77d596cafae42f1e8996e195b9 Mon Sep 17 00:00:00 2001 From: Nikita Skrynnik Date: Wed, 9 Aug 2023 15:31:26 +0700 Subject: [PATCH 3/5] fix golang linter Signed-off-by: Nikita Skrynnik --- pkg/api/networkservice/connection_helpers.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/pkg/api/networkservice/connection_helpers.go b/pkg/api/networkservice/connection_helpers.go index 9770ac2..98ee2b7 100644 --- a/pkg/api/networkservice/connection_helpers.go +++ b/pkg/api/networkservice/connection_helpers.go @@ -2,6 +2,8 @@ // // Copyright (c) 2021 Doc.ai and/or its affiliates. // +// Copyright (c) 2023 Cisco and/or its affiliates. +// // SPDX-License-Identifier: Apache-2.0 // // Licensed under the Apache License, Version 2.0 (the "License"); From 4107582b946f6ea06befe8829aa79a3a1fc45a71 Mon Sep 17 00:00:00 2001 From: Nikita Skrynnik Date: Thu, 10 Aug 2023 09:56:12 +0700 Subject: [PATCH 4/5] add tests for MatchesMonitorScopeSelector Signed-off-by: Nikita Skrynnik --- .../networkservice/connection_helpers_test.go | 98 +++++++++++++++++++ 1 file changed, 98 insertions(+) create mode 100644 pkg/api/networkservice/connection_helpers_test.go diff --git a/pkg/api/networkservice/connection_helpers_test.go b/pkg/api/networkservice/connection_helpers_test.go new file mode 100644 index 0000000..0b91215 --- /dev/null +++ b/pkg/api/networkservice/connection_helpers_test.go @@ -0,0 +1,98 @@ +// Copyright (c) 2023 Cisco and/or its affiliates. +// +// SPDX-License-Identifier: Apache-2.0 +// +// 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 networkservice_test + +import ( + "testing" + + "github.com/networkservicemesh/api/pkg/api/networkservice" + "github.com/stretchr/testify/require" +) + +func TestMonitorScopeSelector(t *testing.T) { + cases := []struct { + testname string + connSegments []*networkservice.PathSegment + selectorSegments []*networkservice.PathSegment + matches bool + }{ + { + testname: "IdenticalPaths", + connSegments: []*networkservice.PathSegment{{Name: "s1", Id: "id1", Token: "t1"}, {Name: "s2", Id: "id2", Token: "t2"}}, + selectorSegments: []*networkservice.PathSegment{{Name: "s1", Id: "id1", Token: "t1"}, {Name: "s2", Id: "id2", Token: "t2"}}, + matches: true, + }, + { + testname: "DifferentNames", + connSegments: []*networkservice.PathSegment{{Name: "s15", Id: "id1", Token: "t1"}, {Name: "s2", Id: "id2", Token: "t2"}}, + selectorSegments: []*networkservice.PathSegment{{Name: "s1", Id: "id1", Token: "t1"}, {Name: "s2", Id: "id2", Token: "t2"}}, + matches: false, + }, + { + testname: "DifferentIds", + connSegments: []*networkservice.PathSegment{{Name: "s1", Id: "id15", Token: "t1"}, {Name: "s2", Id: "id2", Token: "t2"}}, + selectorSegments: []*networkservice.PathSegment{{Name: "s1", Id: "id1", Token: "t1"}, {Name: "s2", Id: "id2", Token: "t2"}}, + matches: false, + }, + { + testname: "DifferentTokens", + connSegments: []*networkservice.PathSegment{{Name: "s1", Id: "id1", Token: "t1"}, {Name: "s2", Id: "id2", Token: "t2"}}, + selectorSegments: []*networkservice.PathSegment{{Name: "s1", Id: "id1", Token: "t15"}, {Name: "s2", Id: "id2", Token: "t2"}}, + matches: false, + }, + { + testname: "SelectorPathIsLonger", + connSegments: []*networkservice.PathSegment{{Name: "s1", Id: "id1"}, {Name: "s2", Id: "id2"}}, + selectorSegments: []*networkservice.PathSegment{{Name: "s1", Id: "id1"}, {Name: "s2", Id: "id2"}, {Name: "s3", Id: "id3"}}, + matches: false, + }, + { + testname: "ConnPathContainsSelectorPath", + connSegments: []*networkservice.PathSegment{{Name: "s1", Id: "id1"}, {Name: "s2", Id: "id2"}, {Name: "s3", Id: "id3"}}, + selectorSegments: []*networkservice.PathSegment{{Name: "s1", Id: "id1"}, {Name: "s2", Id: "id2"}}, + matches: true, + }, + { + testname: "EmptyID", + connSegments: []*networkservice.PathSegment{{Name: "s1", Id: "id1"}, {Name: "s2", Id: "id2"}}, + selectorSegments: []*networkservice.PathSegment{{Name: "s1", Id: "id1"}, {Name: "s2", Id: ""}}, + matches: true, + }, + { + testname: "EmptyName", + connSegments: []*networkservice.PathSegment{{Name: "s1", Id: "id1"}, {Name: "s2", Id: "id2"}}, + selectorSegments: []*networkservice.PathSegment{{Name: "s1", Id: "id1"}, {Name: "", Id: "id2"}}, + matches: true, + }, + { + testname: "EmptyToken", + connSegments: []*networkservice.PathSegment{{Name: "s1", Id: "id1", Token: "t1"}, {Name: "s2", Id: "id2", Token: "t2"}}, + selectorSegments: []*networkservice.PathSegment{{Name: "s1", Id: "id1", Token: "t1"}, {Name: "", Id: "id2", Token: ""}}, + matches: true, + }, + } + + for _, c := range cases { + t.Run(c.testname, func(t *testing.T) { + path := &networkservice.Path{PathSegments: c.connSegments} + conn := networkservice.Connection{Path: path} + selector := &networkservice.MonitorScopeSelector{PathSegments: c.selectorSegments} + + require.Equal(t, conn.MatchesMonitorScopeSelector(selector), c.matches) + }) + } +} From 482ca609b5002e75dd000d78f4c7eb88c6a1a889 Mon Sep 17 00:00:00 2001 From: Nikita Skrynnik Date: Thu, 10 Aug 2023 10:02:32 +0700 Subject: [PATCH 5/5] fix linter issue Signed-off-by: Nikita Skrynnik --- pkg/api/networkservice/connection_helpers_test.go | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/pkg/api/networkservice/connection_helpers_test.go b/pkg/api/networkservice/connection_helpers_test.go index 0b91215..2ab3fc1 100644 --- a/pkg/api/networkservice/connection_helpers_test.go +++ b/pkg/api/networkservice/connection_helpers_test.go @@ -19,10 +19,12 @@ package networkservice_test import ( "testing" - "github.com/networkservicemesh/api/pkg/api/networkservice" "github.com/stretchr/testify/require" + + "github.com/networkservicemesh/api/pkg/api/networkservice" ) +// nolint: funlen func TestMonitorScopeSelector(t *testing.T) { cases := []struct { testname string @@ -86,7 +88,8 @@ func TestMonitorScopeSelector(t *testing.T) { }, } - for _, c := range cases { + for _, testCase := range cases { + c := testCase t.Run(c.testname, func(t *testing.T) { path := &networkservice.Path{PathSegments: c.connSegments} conn := networkservice.Connection{Path: path}