Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
Signed-off-by: denis-tingajkin <denis.tingajkin@xored.com>
  • Loading branch information
denis-tingaikin committed Jun 24, 2020
1 parent b1db1ba commit 02bf9b4
Show file tree
Hide file tree
Showing 6 changed files with 228 additions and 48 deletions.
47 changes: 1 addition & 46 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -51,51 +51,6 @@ jobs:
go-version: 1.13.4
- run: |
go build -race ./...
test:
name: test
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-go@v1
with:
go-version: 1.13.4
- name: Install gotestsum
run: go get gotest.tools/gotestsum@v0.4.0
- name: Run tests
run: |
eval $(go env)
mkdir -p ~/junit/
${GOPATH}/bin/gotestsum --junitfile ~/junit/unit-tests.xml -- -race -short $(go list ./...)
test-win:
name: test-win
runs-on: windows-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-go@v1
with:
go-version: 1.13.4
- name: Install gotestsum
run: go get gotest.tools/gotestsum@v0.4.0
- name: Run tests
run: |
$env:Path += ";$(go env GOPATH)\bin"
mkdir $home/junit
gotestsum --junitfile $home\junit\unit-tests.xml -- -race -short $(go list .\...)
test-osx:
name: test-osx
runs-on: macos-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-go@v1
with:
go-version: 1.13.4
- name: Install gotestsum
run: go get gotest.tools/gotestsum@v0.4.0
- name: Run tests
run: |
eval $(go env)
mkdir -p ~/junit/
${GOPATH}/bin/gotestsum --junitfile ~/junit/unit-tests.xml -- -race -short $(go list ./...)
golangci-lint:
name: golangci-lint
runs-on: ubuntu-latest
Expand Down Expand Up @@ -217,7 +172,7 @@ jobs:
runs-on: ubuntu-latest
needs:
- build
- test
- docker
if: github.actor == 'nsmbot' && github.base_ref == 'master' && github.event_name == 'pull_request'
steps:
- name: Check out the code
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ go 1.14

require (
github.com/antonfisher/nested-logrus-formatter v1.1.0
github.com/edwarnicke/exechelper v1.0.1
github.com/kelseyhightower/envconfig v1.4.0
github.com/networkservicemesh/api v0.0.0-20200616160323-8c25fb122dcc
github.com/networkservicemesh/sdk v0.0.0-20200623115138-ef1781823819
github.com/sirupsen/logrus v1.6.0
github.com/spiffe/go-spiffe/v2 v2.0.0-alpha.4.0.20200528145730-dc11d0c74e85
github.com/stretchr/testify v1.5.1
golang.org/x/lint v0.0.0-20200302205851-738671d3881b // indirect
golang.org/x/tools v0.0.0-20200417140056-c07e33ef3290 // indirect
google.golang.org/grpc v1.30.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24
github.com/boltdb/bolt v1.3.1/go.mod h1:clJnj/oiGkjum5o1McbSZDSLxVThjynRyGBgiAx27Ps=
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc=
github.com/cheekybits/is v0.0.0-20150225183255-68e9c0620927 h1:SKI1/fuSdodxmNNyVBR8d7X/HuLnRpvvFO0AgyQk764=
github.com/cheekybits/is v0.0.0-20150225183255-68e9c0620927/go.mod h1:h/aW8ynjgkuj+NQRlZcDbAbM1ORAbXjXX77sX7T289U=
github.com/circonus-labs/circonus-gometrics v2.3.1+incompatible/go.mod h1:nmEj6Dob7S7YxXgwXpfOuvO54S+tGdZdw9fuRZt25Ag=
github.com/circonus-labs/circonusllhist v0.1.3/go.mod h1:kMXHVDlOchFAehlya5ePtbp5jckzBHf4XRpQvBOLI+I=
Expand Down Expand Up @@ -97,6 +98,7 @@ github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/lib/pq v1.3.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
github.com/matryer/try v0.0.0-20161228173917-9ac251b645a2 h1:JAEbJn3j/FrhdWA9jW8B5ajsLIjeuEHLi8xE4fk997o=
github.com/matryer/try v0.0.0-20161228173917-9ac251b645a2/go.mod h1:0KeJpeMD6o+O4hW7qJOT7vyQPKrWmj26uf5wMc/IiIs=
github.com/mattn/go-runewidth v0.0.0-20181025052659-b20a3daf6a39/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzpuz5H//U1FU=
github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0=
Expand Down
18 changes: 16 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ import (
"os"
"time"

"github.com/networkservicemesh/sdk/pkg/registry/common/setid"

"github.com/networkservicemesh/api/pkg/api"
"google.golang.org/grpc/health"
"google.golang.org/grpc/health/grpc_health_v1"

nested "github.com/antonfisher/nested-logrus-formatter"
"github.com/kelseyhightower/envconfig"
"github.com/networkservicemesh/api/pkg/api/registry"
Expand Down Expand Up @@ -88,18 +94,26 @@ func main() {

nseChain := chain.NewNetworkServiceEndpointRegistryServer(
seturl.NewServer(""),
setid.NewNetworkServiceEndpointRegistryServer(),
memory.NewNetworkServiceEndpointRegistryServer(),
)

nsChain := chain.NewNetworkServiceRegistryServer(
memory.NewNetworkServiceRegistryServer(),
)

// Create GRPC Server
// TODO - add ServerOptions for Tracing
// Create GRPC Server and register services
server := grpc.NewServer(grpc.Creds(credentials.NewTLS(tlsconfig.MTLSServerConfig(source, source, tlsconfig.AuthorizeAny()))))
registry.RegisterNetworkServiceRegistryServer(server, nsChain)
registry.RegisterNetworkServiceEndpointRegistryServer(server, nseChain)
healthServer := health.NewServer()
grpc_health_v1.RegisterHealthServer(server, healthServer)
for _, service := range api.ServiceNames(nseChain) {
healthServer.SetServingStatus(service, grpc_health_v1.HealthCheckResponse_SERVING)
}
for _, service := range api.ServiceNames(nsChain) {
healthServer.SetServingStatus(service, grpc_health_v1.HealthCheckResponse_SERVING)
}

srvErrCh := grpcutils.ListenAndServe(ctx, &config.ListenOn, server)
exitOnErr(ctx, cancel, srvErrCh)
Expand Down
195 changes: 195 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,195 @@
// Copyright (c) 2020 Doc.ai 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 main_test

import (
"context"
"fmt"
"os"
"path/filepath"
"testing"
"time"

"github.com/sirupsen/logrus"

nested "github.com/antonfisher/nested-logrus-formatter"
"github.com/edwarnicke/exechelper"
"github.com/kelseyhightower/envconfig"
"github.com/networkservicemesh/api/pkg/api/registry"
main "github.com/networkservicemesh/cmd-registry-memory"
"github.com/spiffe/go-spiffe/v2/bundle/x509bundle"
"github.com/spiffe/go-spiffe/v2/spiffetls/tlsconfig"
"github.com/spiffe/go-spiffe/v2/svid/x509svid"
"github.com/spiffe/go-spiffe/v2/workloadapi"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/health/grpc_health_v1"

"github.com/networkservicemesh/sdk/pkg/tools/spire"
)

type RegistryTestSuite struct {
suite.Suite
ctx context.Context
cancel context.CancelFunc
x509source x509svid.Source
x509bundle x509bundle.Source
config main.Config
spireErrCh <-chan error
sutErrCh <-chan error
}

func (t *RegistryTestSuite) SetupSuite() {
logrus.SetFormatter(&nested.Formatter{})
logrus.SetLevel(logrus.TraceLevel)
t.ctx, t.cancel = context.WithCancel(context.Background())

// Run spire
executable, err := os.Executable()
require.NoError(t.T(), err)
t.spireErrCh = spire.Start(
spire.WithContext(t.ctx),
spire.WithEntry("spiffe://example.org/registry-memory", "unix:path:/bin/registry-memory"),
spire.WithEntry(fmt.Sprintf("spiffe://example.org/%s", filepath.Base(executable)),
fmt.Sprintf("unix:path:%s", executable),
),
)
require.Len(t.T(), t.spireErrCh, 0)

// Get X509Source
source, err := workloadapi.NewX509Source(t.ctx)
t.x509source = source
t.x509bundle = source
require.NoError(t.T(), err)
svid, err := t.x509source.GetX509SVID()
if err != nil {
logrus.Fatalf("error getting x509 svid: %+v", err)
}
logrus.Infof("SVID: %q", svid.ID)

// Run system under test (sut)
cmdStr := "registry-memory"
t.sutErrCh = exechelper.Start(cmdStr,
exechelper.WithContext(t.ctx),
exechelper.WithEnvirons(os.Environ()...),
exechelper.WithStdout(os.Stdout),
exechelper.WithStderr(os.Stderr),
)
require.Len(t.T(), t.sutErrCh, 0)

// Get config from env
require.NoError(t.T(), envconfig.Process("nsm", &t.config))
}

func (t *RegistryTestSuite) TearDownSuite() {
t.cancel()
for {
_, ok := <-t.sutErrCh
if !ok {
break
}
}
for {
_, ok := <-t.spireErrCh
if !ok {
break
}
}
}

func (t *RegistryTestSuite) TestHealthCheck() {
ctx, cancel := context.WithTimeout(t.ctx, 100*time.Second)
defer cancel()
healthCC, err := grpc.DialContext(ctx,
t.config.ListenOn.String(),
grpc.WithTransportCredentials(credentials.NewTLS(tlsconfig.MTLSClientConfig(t.x509source, t.x509bundle, tlsconfig.AuthorizeAny()))),
)
if err != nil {
logrus.Fatalf("Failed healthcheck: %+v", err)
}
healthClient := grpc_health_v1.NewHealthClient(healthCC)
healthResponse, err := healthClient.Check(ctx,
&grpc_health_v1.HealthCheckRequest{
Service: "registry.NetworkServiceEndpointRegistry",
},
grpc.WaitForReady(true),
)
t.NoError(err)
t.NotNil(healthResponse)
t.Equal(grpc_health_v1.HealthCheckResponse_SERVING, healthResponse.Status)
}

func (t *RegistryTestSuite) TestNetworkServiceRegistration() {
ctx, cancel := context.WithTimeout(t.ctx, 100*time.Second)
defer cancel()
cc, err := grpc.DialContext(ctx,
t.config.ListenOn.String(),
grpc.WithTransportCredentials(credentials.NewTLS(tlsconfig.MTLSClientConfig(t.x509source, t.x509bundle, tlsconfig.AuthorizeAny()))),
)
t.NoError(err)
client := registry.NewNetworkServiceRegistryClient(cc)
_, err = client.Register(context.Background(), &registry.NetworkService{
Name: "ns-1",
})
t.Nil(err)
stream, err := client.Find(context.Background(), &registry.NetworkServiceQuery{NetworkService: &registry.NetworkService{Name: "ns-1"}})
t.Nil(err)
list := registry.ReadNetworkServiceList(stream)
t.Len(list, 1)
_, err = client.Unregister(context.Background(), &registry.NetworkService{
Name: "ns-1",
})
t.Nil(err)
stream, err = client.Find(context.Background(), &registry.NetworkServiceQuery{NetworkService: &registry.NetworkService{Name: "ns-1"}})
t.Nil(err)
list = registry.ReadNetworkServiceList(stream)
t.Len(list, 0)
}

func (t *RegistryTestSuite) TestNetworkServiceEndpointRegistration() {
ctx, cancel := context.WithTimeout(t.ctx, 100*time.Second)
defer cancel()
cc, err := grpc.DialContext(ctx,
t.config.ListenOn.String(),
grpc.WithTransportCredentials(credentials.NewTLS(tlsconfig.MTLSClientConfig(t.x509source, t.x509bundle, tlsconfig.AuthorizeAny()))),
)
t.NoError(err)
client := registry.NewNetworkServiceEndpointRegistryClient(cc)
result, err := client.Register(context.Background(), &registry.NetworkServiceEndpoint{
NetworkServiceName: []string{
"ns-1",
},
})
t.Nil(err)
t.NotEmpty(result.Name)
stream, err := client.Find(context.Background(), &registry.NetworkServiceEndpointQuery{NetworkServiceEndpoint: result})
t.Nil(err)
list := registry.ReadNetworkServiceEndpointList(stream)
t.Len(list, 1)
_, err = client.Unregister(context.Background(), result)
t.Nil(err)
stream, err = client.Find(context.Background(), &registry.NetworkServiceEndpointQuery{NetworkServiceEndpoint: result})
t.Nil(err)
list = registry.ReadNetworkServiceEndpointList(stream)
t.Len(list, 0)
}

func TestRegistryTestSuite(t *testing.T) {
suite.Run(t, new(RegistryTestSuite))
}
12 changes: 12 additions & 0 deletions pkg/imports/imports.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@ package imports

import (
_ "context"
_ "fmt"
_ "github.com/antonfisher/nested-logrus-formatter"
_ "github.com/edwarnicke/exechelper"
_ "github.com/kelseyhightower/envconfig"
_ "github.com/networkservicemesh/api/pkg/api"
_ "github.com/networkservicemesh/api/pkg/api/registry"
_ "github.com/networkservicemesh/sdk/pkg/registry/common/seturl"
_ "github.com/networkservicemesh/sdk/pkg/registry/core/chain"
Expand All @@ -13,12 +16,21 @@ import (
_ "github.com/networkservicemesh/sdk/pkg/tools/grpcutils"
_ "github.com/networkservicemesh/sdk/pkg/tools/log"
_ "github.com/networkservicemesh/sdk/pkg/tools/signalctx"
_ "github.com/networkservicemesh/sdk/pkg/tools/spire"
_ "github.com/sirupsen/logrus"
_ "github.com/spiffe/go-spiffe/v2/bundle/x509bundle"
_ "github.com/spiffe/go-spiffe/v2/spiffetls/tlsconfig"
_ "github.com/spiffe/go-spiffe/v2/svid/x509svid"
_ "github.com/spiffe/go-spiffe/v2/workloadapi"
_ "github.com/stretchr/testify/require"
_ "github.com/stretchr/testify/suite"
_ "google.golang.org/grpc"
_ "google.golang.org/grpc/credentials"
_ "google.golang.org/grpc/health"
_ "google.golang.org/grpc/health/grpc_health_v1"
_ "net/url"
_ "os"
_ "path/filepath"
_ "testing"
_ "time"
)

0 comments on commit 02bf9b4

Please sign in to comment.