Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixed updates of excluded prefixes #1180

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions pkg/networkservice/common/excludedprefixes/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ func (epc *excludedPrefixesClient) Request(ctx context.Context, request *network
logger.Debugf("Added excluded prefixes: %+v", epc.excludedPrefixes)
})

ipCtx.ExcludedPrefixes = oldExcludedPrefixes

return resp, err
}

Expand Down
94 changes: 78 additions & 16 deletions pkg/networkservice/common/excludedprefixes/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ import (
"github.com/networkservicemesh/sdk/pkg/networkservice/core/adapters"
"github.com/networkservicemesh/sdk/pkg/networkservice/core/chain"
"github.com/networkservicemesh/sdk/pkg/networkservice/ipam/point2pointipam"
"github.com/networkservicemesh/sdk/pkg/networkservice/utils/checks/checkconnection"
"github.com/networkservicemesh/sdk/pkg/networkservice/utils/checks/checkrequest"
"github.com/networkservicemesh/sdk/pkg/networkservice/utils/inject/injecterror"
"github.com/networkservicemesh/sdk/pkg/networkservice/utils/inject/injectexcludedprefixes"
"github.com/networkservicemesh/sdk/pkg/networkservice/utils/inject/injectipcontext"
Expand Down Expand Up @@ -108,17 +110,23 @@ func TestExcludedPrefixesClient_Request_SrcAndDestPrefixesAreDifferent(t *testin
Connection: &networkservice.Connection{},
}

resp, err = chain.NewNetworkServiceClient(client, server2).Request(ctx, request2)
resp, err = chain.NewNetworkServiceClient(
client,
checkconnection.NewClient(t, func(t *testing.T, conn *networkservice.Connection) {
if conn.GetContext() == nil || conn.GetContext().GetIpContext() == nil {
return
}
require.ElementsMatch(t, expectedExcludedIPs, conn.GetContext().GetIpContext().GetExcludedPrefixes())
}),
server2,
).Request(ctx, request2)
require.NoError(t, err)

srcIPs = resp.GetContext().GetIpContext().GetSrcIpAddrs()
require.Len(t, srcIPs, 1)
srcIP2 := srcIPs[0]

require.NotEqual(t, srcIP1, srcIP2)

excludedIPs := resp.GetContext().GetIpContext().GetExcludedPrefixes()
require.ElementsMatch(t, expectedExcludedIPs, excludedIPs)
}

func TestExcludedPrefixesClient_Close_PrefixesAreRemoved(t *testing.T) {
Expand Down Expand Up @@ -197,10 +205,17 @@ func TestExcludedPrefixesClient_Request_WithExcludedPrefixes(t *testing.T) {
point2pointipam.NewServer(ipNet),
)

resp, err = chain.NewNetworkServiceClient(client, server2).Request(ctx, request2)
_, err = chain.NewNetworkServiceClient(
client,
checkconnection.NewClient(t, func(t *testing.T, conn *networkservice.Connection) {
if conn.GetContext() == nil || conn.GetContext().GetIpContext() == nil {
return
}
require.ElementsMatch(t, expectedExcludedPrefixes, conn.GetContext().GetIpContext().GetExcludedPrefixes())
}),
server2,
).Request(ctx, request2)
require.NoError(t, err)

require.ElementsMatch(t, expectedExcludedPrefixes, resp.GetContext().GetIpContext().GetExcludedPrefixes())
}

func TestExcludedPrefixesClient_Request_PrefixesUnchangedAfterError(t *testing.T) {
Expand Down Expand Up @@ -261,10 +276,17 @@ func TestExcludedPrefixesClient_Request_PrefixesUnchangedAfterError(t *testing.T
point2pointipam.NewServer(ipNet)),
)

resp, err = chain.NewNetworkServiceClient(client, server3).Request(ctx, request3)
_, err = chain.NewNetworkServiceClient(
client,
checkconnection.NewClient(t, func(t *testing.T, conn *networkservice.Connection) {
if conn.GetContext() == nil || conn.GetContext().GetIpContext() == nil {
return
}
require.ElementsMatch(t, expectedExcludedPrefixes, conn.GetContext().GetIpContext().GetExcludedPrefixes())
}),
server3,
).Request(ctx, request3)
require.NoError(t, err)

require.ElementsMatch(t, expectedExcludedPrefixes, resp.GetContext().GetIpContext().GetExcludedPrefixes())
}

func TestExcludedPrefixesClient_Request_SuccessfulRefresh(t *testing.T) {
Expand Down Expand Up @@ -307,11 +329,18 @@ func TestExcludedPrefixesClient_Request_SuccessfulRefresh(t *testing.T) {
// src/dest IPs from first server should still be present in a request to another server
expectedExcludedPrefixes2 := []string{"172.16.0.99/32", "172.16.0.97/32", "172.16.0.96/32", "172.16.0.98/32", "172.16.0.100/32"}

resp, err = chain.NewNetworkServiceClient(client, server1).Request(ctx, request)
_, err = chain.NewNetworkServiceClient(
client,
checkconnection.NewClient(t, func(t *testing.T, conn *networkservice.Connection) {
if conn.GetContext() == nil || conn.GetContext().GetIpContext() == nil {
return
}
require.ElementsMatch(t, expectedExcludedPrefixes, conn.GetContext().GetIpContext().GetExcludedPrefixes())
}),
server1,
).Request(ctx, request)
require.NoError(t, err)

require.ElementsMatch(t, expectedExcludedPrefixes, resp.GetContext().GetIpContext().GetExcludedPrefixes())

server2 := chain.NewNetworkServiceClient(
adapters.NewServerToClient(
point2pointipam.NewServer(ipNet)),
Expand All @@ -321,10 +350,17 @@ func TestExcludedPrefixesClient_Request_SuccessfulRefresh(t *testing.T) {
Connection: &networkservice.Connection{},
}

resp, err = chain.NewNetworkServiceClient(client, server2).Request(ctx, request2)
_, err = chain.NewNetworkServiceClient(
client,
checkconnection.NewClient(t, func(t *testing.T, conn *networkservice.Connection) {
if conn.GetContext() == nil || conn.GetContext().GetIpContext() == nil {
return
}
require.ElementsMatch(t, expectedExcludedPrefixes2, conn.GetContext().GetIpContext().GetExcludedPrefixes())
}),
server2,
).Request(ctx, request2)
require.NoError(t, err)

require.ElementsMatch(t, expectedExcludedPrefixes2, resp.GetContext().GetIpContext().GetExcludedPrefixes())
}

func TestExcludedPrefixesClient_Request_EndpointConflicts(t *testing.T) {
Expand Down Expand Up @@ -451,3 +487,29 @@ func TestExcludedPrefixesClient_Request_EndpointConflictCloseError(t *testing.T)
require.Error(t, err)
require.Contains(t, err.Error(), "connection closed")
}

func TestClient(t *testing.T) {
reqPrefixes := []string{"100.1.1.0/13", "10.32.0.0/12", "10.96.0.0/12"}

client := chain.NewNetworkServiceClient(
excludedprefixes.NewClient(),
checkrequest.NewClient(t, func(t *testing.T, request *networkservice.NetworkServiceRequest) {
request.Connection.Context.IpContext.ExcludedPrefixes = append(request.Connection.Context.IpContext.ExcludedPrefixes, []string{"172.16.2.0/24"}...)
}),
)

req := &networkservice.NetworkServiceRequest{
Connection: &networkservice.Connection{
Context: &networkservice.ConnectionContext{
IpContext: &networkservice.IPContext{
ExcludedPrefixes: reqPrefixes,
},
},
},
}

_, err := client.Request(context.Background(), req)

require.ElementsMatch(t, reqPrefixes, req.Connection.Context.IpContext.ExcludedPrefixes)
require.NoError(t, err)
}
60 changes: 60 additions & 0 deletions pkg/networkservice/common/excludedprefixes/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,17 @@ package excludedprefixes_test

import (
"context"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"reflect"
"sort"
"strings"
"testing"
"time"

"github.com/google/go-cmp/cmp"
"github.com/stretchr/testify/require"

"github.com/networkservicemesh/api/pkg/api/networkservice"
Expand Down Expand Up @@ -131,6 +134,63 @@ func TestUniqueRequestPrefixes(t *testing.T) {
require.NoError(t, err)
}

func TestFilePrefixesChanged(t *testing.T) {
filePrefixes := []string{"172.16.1.0/24", "10.32.0.0/12", "10.96.0.0/12", "10.20.128.0/17"}
reqPrefixes := []string{"100.1.1.0/13", "10.32.0.0/12", "10.96.0.0/12"}
diffPrefxies := []string{"100.1.1.0/13", "10.32.0.0/12", "10.96.0.0/12", "10.20.128.0/17", "172.16.1.0/24"}
newFilePrefixes := []string{"172.16.2.0/24"}

dir := filepath.Join(os.TempDir(), t.Name())
defer func() { _ = os.RemoveAll(dir) }()
require.NoError(t, os.MkdirAll(dir, os.ModePerm))

testConfig := strings.Join(append([]string{"prefixes:"}, filePrefixes...), "\n- ")
configPath := filepath.Join(dir, defaultPrefixesFileName)
require.NoError(t, ioutil.WriteFile(configPath, []byte(testConfig), os.ModePerm))
defer func() { _ = os.Remove(configPath) }()

var prefixesAfterServer []string
isFirst := true
server := chain.NewNetworkServiceServer(
excludedprefixes.NewServer(context.Background(), excludedprefixes.WithConfigPath(configPath)),
checkrequest.NewServer(t, func(t *testing.T, request *networkservice.NetworkServiceRequest) {
if isFirst {
require.ElementsMatch(t, diffPrefxies, request.Connection.Context.IpContext.ExcludedPrefixes)
isFirst = false
}

prefixesAfterServer = request.Connection.Context.IpContext.ExcludedPrefixes
}),
)

req := &networkservice.NetworkServiceRequest{
Connection: &networkservice.Connection{
Id: "1",
Context: &networkservice.ConnectionContext{
IpContext: &networkservice.IPContext{
ExcludedPrefixes: reqPrefixes,
},
},
},
}

_, err := server.Request(context.Background(), req)

require.NoError(t, ioutil.WriteFile(configPath, []byte(strings.Join(append([]string{"prefixes:"}, newFilePrefixes...), "\n- ")), os.ModePerm))

require.Eventually(t, func() bool {
_, err = server.Request(context.Background(), req)
fmt.Printf("%v\n", prefixesAfterServer)
return cmp.Equal(prefixesAfterServer, append(diffPrefxies, newFilePrefixes...), cmp.Transformer("Sort", func(in []string) []string {
out := append([]string(nil), in...)
sort.Strings(out)
return out
}))
}, time.Second, time.Millisecond*100)

require.NoError(t, err)
}

func testWaitForFile(t *testing.T, filePath string) {
prefixes := []string{"10.80.0.0/12", "172.16.1.0/24"}

Expand Down