Skip to content

Commit

Permalink
Merge pull request #142 from bellycat77/add-test-env-registration
Browse files Browse the repository at this point in the history
Allow setting of registration url via env variable (unit test)
  • Loading branch information
denis-tingaikin authored Aug 31, 2023
2 parents 59a7bc3 + 05b7125 commit 0cce3da
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 35 deletions.
5 changes: 5 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ require (
github.com/pkg/errors v0.9.1
github.com/sirupsen/logrus v1.9.0
github.com/spiffe/go-spiffe/v2 v2.0.0
github.com/stretchr/testify v1.8.3
github.com/vishvananda/netlink v1.2.1-beta.2.0.20220630165224-c591ada0fb2b
google.golang.org/grpc v1.55.0
)
Expand All @@ -25,6 +26,7 @@ require (
github.com/OneOfOne/xxhash v1.2.8 // indirect
github.com/agnivade/levenshtein v1.1.1 // indirect
github.com/cenkalti/backoff/v4 v4.2.1 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/edwarnicke/exechelper v1.0.2 // indirect
github.com/edwarnicke/genericsync v0.0.0-20220910010113-61a344f9bc29 // indirect
github.com/edwarnicke/log v1.0.0 // indirect
Expand All @@ -36,10 +38,12 @@ require (
github.com/gobwas/glob v0.2.3 // indirect
github.com/golang-jwt/jwt/v4 v4.2.0 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/google/go-cmp v0.5.9 // indirect
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 // indirect
github.com/grpc-ecosystem/grpc-gateway/v2 v2.11.3 // indirect
github.com/lunixbochs/struc v0.0.0-20200521075829-a4cb8d33dbbe // indirect
github.com/open-policy-agent/opa v0.44.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/rcrowley/go-metrics v0.0.0-20201227073835-cf1acfcdf475 // indirect
github.com/tchap/go-patricia/v2 v2.3.1 // indirect
github.com/thanhpk/randstr v1.0.4 // indirect
Expand Down Expand Up @@ -71,4 +75,5 @@ require (
gopkg.in/fsnotify.v1 v1.4.7 // indirect
gopkg.in/square/go-jose.v2 v2.5.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ github.com/google/go-cmp v0.5.1/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/martian v2.1.0+incompatible/go.mod h1:9I4somxYTbIHy5NJKHRl3wXiIaQGbYVAs8BPL6v8lEs=
github.com/google/martian/v3 v3.0.0/go.mod h1:y5Zk1BBys9G+gd6Jrk0W3cC1+ELVxBWuIGO+w/tUAp0=
github.com/google/pprof v0.0.0-20181206194817-3ea8567a2e57/go.mod h1:zfwlbNMJ+OItoe0UupaVj+oy1omPYYDuagoSzA8v9mc=
Expand Down Expand Up @@ -240,6 +241,7 @@ github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:
github.com/prometheus/client_model v0.2.0 h1:uq5h0d+GuxiXLJLNABMgp2qUWDPiLvgCzz2dUR+/W/M=
github.com/prometheus/common v0.37.0 h1:ccBbHCgIiT9uSoFY0vX8H3zsNR5eLt17/RQLUvn8pXE=
github.com/prometheus/procfs v0.8.0 h1:ODq8ZFEaYeCaZOJlZZdJA2AbQR98dSHSM1KW/You5mo=
github.com/r3labs/diff v1.1.0 h1:V53xhrbTHrWFWq3gI4b94AjgEJOerO1+1l0xyHOBi8M=
github.com/rcrowley/go-metrics v0.0.0-20201227073835-cf1acfcdf475 h1:N/ElC8H3+5XpJzTSTfLsJV/mx9Q9g7kxmchpfZyxgzM=
github.com/rcrowley/go-metrics v0.0.0-20201227073835-cf1acfcdf475/go.mod h1:bCqnVzQkZxMG4s8nGwiZ5l3QUCyqpo9Y+/ZMZ9VjZe4=
github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6LYCDYWNEvQ=
Expand All @@ -259,6 +261,7 @@ github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81P
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.3 h1:RP3t2pwF7cMEbC1dqtB6poj3niw/9gnV4Cjg5oW5gtY=
github.com/stretchr/testify v1.8.3/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/tchap/go-patricia/v2 v2.3.1 h1:6rQp39lgIYZ+MHmdEq4xzuk1t7OdC35z/xm0BGhTkes=
github.com/tchap/go-patricia/v2 v2.3.1/go.mod h1:VZRHKAb53DLaG+nA9EaYYiaEx6YztwDlLElMsnSHD4k=
github.com/thanhpk/randstr v1.0.4 h1:IN78qu/bR+My+gHCvMEXhR/i5oriVHcTB/BJJIRTsNo=
Expand Down Expand Up @@ -612,6 +615,7 @@ gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
honnef.co/go/tools v0.0.0-20190106161140-3f1c8253044a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
honnef.co/go/tools v0.0.0-20190418001031-e561f6794a2a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
Expand Down
4 changes: 3 additions & 1 deletion internal/imports/imports_linux.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

74 changes: 40 additions & 34 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ import (
"time"

"github.com/google/uuid"
"github.com/kelseyhightower/envconfig"
"github.com/pkg/errors"

nested "github.com/antonfisher/nested-logrus-formatter"
"github.com/edwarnicke/grpcfd"
"github.com/kelseyhightower/envconfig"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/spiffe/go-spiffe/v2/spiffetls/tlsconfig"
"github.com/spiffe/go-spiffe/v2/workloadapi"
Expand Down Expand Up @@ -65,7 +65,6 @@ import (
registryauthorize "github.com/networkservicemesh/sdk/pkg/registry/common/authorize"
"github.com/networkservicemesh/sdk/pkg/registry/common/clientinfo"
"github.com/networkservicemesh/sdk/pkg/registry/common/dnsresolve"
registrysendfd "github.com/networkservicemesh/sdk/pkg/registry/common/sendfd"
"github.com/networkservicemesh/sdk/pkg/tools/debug"
"github.com/networkservicemesh/sdk/pkg/tools/grpcutils"
"github.com/networkservicemesh/sdk/pkg/tools/interdomain"
Expand Down Expand Up @@ -107,8 +106,8 @@ type Config struct {
TunnelIP net.IP `desc:"IP to use for tunnels" split_words:"true"`
Vl3Prefix string `default:"169.254.0.0/16" desc:"vl3 prefix"`
InterfaceName string `default:"nsm" desc:"Name of the nsm network interface"`
FederatesWith string `default:"k8s.nsm" desc:"Name of the federated domain"`
TrustDomain string `default:"docker.nsm" desc:"Name of the trust domain"`
FederatesWith string `default:"k8s.nsm" desc:"Name of the federated domain" split_words:"true"`
TrustDomain string `default:"docker.nsm" desc:"Name of the trust domain" split_words:"true"`
LogLevel string `default:"INFO" desc:"Log level" split_words:"true"`
}

Expand Down Expand Up @@ -309,18 +308,7 @@ func main() {
// ********************************************************************************
log.FromContext(ctx).Infof("executing phase 7: register nse with nsm")
// ********************************************************************************
nseRegistryClient := registryclient.NewNetworkServiceEndpointRegistryClient(
ctx,
registryclient.WithNSEClientURLResolver(dnsresolve.NewNetworkServiceEndpointRegistryClient()),
registryclient.WithDialOptions(clientOptions...),
registryclient.WithNSEAdditionalFunctionality(
clientinfo.NewNetworkServiceEndpointRegistryClient(),
registrysendfd.NewNetworkServiceEndpointRegistryClient(),
),
registryclient.WithAuthorizeNSERegistryClient(registryauthorize.NewNetworkServiceEndpointRegistryClient(
registryauthorize.WithPolicies(config.RegistryClientPolicies...),
)),
)

if config.RegisterService {
for _, serviceName := range config.ServiceNames {
nsRegistryClient := registryclient.NewNetworkServiceRegistryClient(ctx,
Expand All @@ -340,23 +328,7 @@ func main() {
}
}

var RegisterAsURL *url.URL
if config.RegisterAsURL.Scheme == "" {
RegisterAsURL = listenOn
} else {
RegisterAsURL = &config.RegisterAsURL
}

nseRegistration := &registryapi.NetworkServiceEndpoint{
Name: config.Name,
NetworkServiceNames: config.ServiceNames,
NetworkServiceLabels: make(map[string]*registryapi.NetworkServiceLabels),
Url: RegisterAsURL.String(),
}
for _, serviceName := range config.ServiceNames {
nseRegistration.NetworkServiceLabels[serviceName] = &registryapi.NetworkServiceLabels{Labels: config.Labels}
}
nseRegistration, err = nseRegistryClient.Register(ctx, nseRegistration)
nseRegistration, err := RegisterNetworkServiceEndpoint(ctx, config, net.DefaultResolver, listenOn, clientOptions)
log.FromContext(ctx).Infof("registered nse: %+v", nseRegistration)

if err != nil {
Expand Down Expand Up @@ -445,3 +417,37 @@ func exitOnErr(ctx context.Context, cancel context.CancelFunc, errCh <-chan erro
cancel()
}(ctx, errCh)
}

func RegisterNetworkServiceEndpoint(ctx context.Context, config *Config, resolver dnsresolve.Resolver, listenOn *url.URL, clientOptions []grpc.DialOption) (*registryapi.NetworkServiceEndpoint, error) {
nseRegistryClient := registryclient.NewNetworkServiceEndpointRegistryClient(
ctx,
registryclient.WithNSEClientURLResolver(dnsresolve.NewNetworkServiceEndpointRegistryClient(dnsresolve.WithResolver(resolver))),
registryclient.WithDialOptions(clientOptions...),
registryclient.WithNSEAdditionalFunctionality(
clientinfo.NewNetworkServiceEndpointRegistryClient(),
),
registryclient.WithAuthorizeNSERegistryClient(registryauthorize.NewNetworkServiceEndpointRegistryClient(
registryauthorize.WithPolicies(config.RegistryClientPolicies...),
)),
)

var registerAsURL *url.URL
if config.RegisterAsURL.Scheme == "" {
registerAsURL = listenOn
} else {
registerAsURL = &config.RegisterAsURL
}

nseRegistration := &registryapi.NetworkServiceEndpoint{
Name: config.Name,
NetworkServiceNames: config.ServiceNames,
NetworkServiceLabels: make(map[string]*registryapi.NetworkServiceLabels),
Url: registerAsURL.String(),
}
for _, serviceName := range config.ServiceNames {
nseRegistration.NetworkServiceLabels[serviceName] = &registryapi.NetworkServiceLabels{Labels: config.Labels}
}
nseRegistration, err := nseRegistryClient.Register(ctx, nseRegistration)

return nseRegistration, err
}
53 changes: 53 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Copyright (c) 2022-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.

//go:build linux
// +build linux

package main_test

import (
"context"
"net/url"
"os"
"testing"

"github.com/stretchr/testify/require"

main "github.com/networkservicemesh/cmd-nse-simple-vl3-docker"
"github.com/networkservicemesh/sdk/pkg/tools/sandbox"
)

func TestEndpointRegister(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
const RegisterAsURL = "tcp://10.10.10.10:10101"

dnsServer := sandbox.NewFakeResolver()
domain := sandbox.NewBuilder(ctx, t).SetDNSResolver(dnsServer).Build()
require.NoError(t, sandbox.AddSRVEntry(dnsServer, "nsm-system.", "registry", domain.Registry.URL))
listenOn := &(url.URL{Scheme: "tcp", Host: "127.0.0.1:"})
require.Nil(t, os.Setenv("NSM_REGISTER_AS_URL", RegisterAsURL))
require.Nil(t, os.Setenv("NSM_REGISTRY_CLIENT_POLICIES", ""))
config := new(main.Config)
err := config.Process()
require.Nil(t, err)

nseRegistration, err := main.RegisterNetworkServiceEndpoint(ctx, config, dnsServer, listenOn, sandbox.DialOptions())
require.Nil(t, err)
require.NotNil(t, nseRegistration)
require.Equal(t, nseRegistration.Url, RegisterAsURL)
}

0 comments on commit 0cce3da

Please sign in to comment.