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

[TEST] increase coverage #695

Merged
merged 1 commit into from
Jan 5, 2025
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
54 changes: 11 additions & 43 deletions .github/workflows/pushes.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -60,26 +60,10 @@ jobs:
go-version: ${{ matrix.go }}
# We're not doing releases, just checks, so we can live without check-latest here

- name: Export Go environment to Actions outputs
id: go-settings
run: |
echo "::set-output name=arch::$(go env GOARCH)"
echo "::set-output name=hostarch::$(go env GOHOSTARCH)"
echo "::set-output name=os::$(go env GOOS)"
echo "::set-output name=hostos::$(go env GOHOSTOS)"
echo "::set-output name=go-version::$(go env GOVERSION)"
# Use with:
# ${{ steps.go-settings.outputs.go-version }}
# which will look like `go1.17.1` if matrix `1.17.x` matches `1.17.1`.
# These are independent of how the matrix is setup, or if a matrix is even used.
#
# You can see the individual values in the "Set up Go" output, collapsed inside a "go env" group at the end.

- name: Install additional check/lint tools
id: tools-install
run: |
go install github.com/mattn/goveralls@latest
go install github.com/wadey/gocovmerge@latest
go install honnef.co/go/tools/cmd/staticcheck@2021.1.2
if: matrix.canonical

Expand All @@ -92,38 +76,22 @@ jobs:
- name: Run Tests (with coverage enabled)
id: coverage
run: |
mkdir cov
echo "::group::Coverage of ./cmd"
go test -v -failfast -covermode=atomic -coverprofile=./cov/cmd.out ./cmd
echo "::endgroup::"
echo "::group::Coverage of ./cmd/store"
go test -v -failfast -covermode=atomic -coverprofile=./cov/store.out ./cmd/store
echo "::endgroup::"
go test -v -failfast -coverpkg=./... -coverprofile=./coverage.out ./...
if: runner.os != 'Windows'

- name: Run Tests (Windows)
id: wintest
# nb2: if we use the coverage approach on Windows, the -coverprofile flag appears to be looked for as a package, and I've no idea why (am not a Windows dev)
# cannot find package "github.com/nats-io/nsc/cov/cmd.out" in any of:
# C:\hostedtoolcache\windows\go\1.16.13\x64\src\github.com\nats-io\nsc\cov\cmd.out (from $GOROOT)
# D:\a\nsc\nsc\go\src\github.com\nats-io\nsc\cov\cmd.out (from $GOPATH)
- name: Run Tests (windows)
run: |
echo "::group::Testing of ./cmd"
go test -v -failfast ./cmd
echo "::endgroup::"
echo "::group::Testing of ./cmd/store"
go test -v -failfast ./cmd/store
echo "::endgroup::"
go test -v -failfast ./...
if: runner.os == 'Windows'

- name: Upload coverage results
id: coverage-upload
run: |
gocovmerge ./cov/*.out > coverage.out
goveralls -coverprofile=coverage.out -service=github
env:
COVERALLS_TOKEN: ${{ secrets.GITHUB_TOKEN }}
if: matrix.canonical
- name: Upload coverage
uses: coverallsapp/github-action@v2
continue-on-error: true
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
flag-name: ${{ matrix.module }}
file: ./coverage.out
if: runner.os != 'Windows'

- name: Bad versions creep defense
id: go-module-versions
Expand Down
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ test:
go vet ./...
rm -rf ./coverage.out
staticcheck -f text ./...
unset $$(env | sed -nEe 's/^(NSC_[^=]*_OPERATOR)=.*/\1/p'); go test -coverpkg=./... -coverprofile=./coverage.out ./...
#unset $$(env | sed -nEe 's/^(NSC_[^=]*_OPERATOR)=.*/\1/p'); go test -coverpkg=./... -coverprofile=./coverage.out ./...
unset $$(env | sed -nEe 's/^(NSC_[^=]*_OPERATOR)=.*/\1/p'); go test -coverpkg=$(go list ./...) -covermode=atomic -coverprofile=./coverage.out ./...
staticcheck ./...

release: fmt test compile
Expand Down
66 changes: 66 additions & 0 deletions cmd/accountusercontextparams_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package cmd

import (
"github.com/nats-io/cliprompts/v2"
"github.com/nats-io/nsc/v2/cmd/store"
"github.com/spf13/cobra"
"github.com/stretchr/testify/require"
"testing"
)

type tp struct {
AccountUserContextParams
}

func (p *tp) SetDefaults(ctx ActionCtx) error {
return p.AccountUserContextParams.SetDefaults(ctx)
}

func (p *tp) Validate(ctx ActionCtx) error {
return p.AccountUserContextParams.Validate(ctx)
}

func (p *tp) Load(ctx ActionCtx) error {
return nil
}

func (p *tp) Run(ctx ActionCtx) (store.Status, error) {
return nil, nil
}

func (p *tp) PreInteractive(ctx ActionCtx) error {
return p.AccountUserContextParams.Edit(ctx)
}

func (p *tp) PostInteractive(ctx ActionCtx) error {
return nil
}

func Test_AccountUserContextParams(t *testing.T) {
ts := NewTestStore(t, "O")
defer ts.Done(t)

ts.AddAccount(t, "A")
ts.AddUser(t, "A", "a")
ts.AddAccount(t, "B")
ts.AddUser(t, "B", "b")

var params tp
cmd := &cobra.Command{
Use: "ucp",
SilenceUsage: true,

RunE: func(cmd *cobra.Command, args []string) error {
return RunAction(cmd, args, &params)
},
}

cliprompts.LogFn = t.Log
// A, a
inputs := []interface{}{0, 0}

_, _, err := ExecuteInteractiveCmd(cmd, inputs)
require.NoError(t, err)
require.Equal(t, "A", params.AccountContextParams.Name)
require.Equal(t, "a", params.UserContextParams.Name)
}
10 changes: 5 additions & 5 deletions cmd/deleteimport_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ func Test_DeleteImport(t *testing.T) {
ts.AddExport(t, "A", jwt.Stream, "bar", 0, false)

ts.AddAccount(t, "B")
ts.AddImport(t, "A", "foo", "B")
ts.AddImport(t, "A", "bar", "B")
ts.AddImport(t, "A", jwt.Stream, "foo", "B")
ts.AddImport(t, "A", jwt.Stream, "bar", "B")

tests := CmdTests{
{createDeleteImportCmd(), []string{"delete", "import", "--account", "A"}, nil, []string{"account \"A\" doesn't have imports"}, true},
Expand All @@ -51,7 +51,7 @@ func Test_DeleteImportAccountRequired(t *testing.T) {
ts.AddAccount(t, "A")
ts.AddExport(t, "A", jwt.Stream, "foo", 0, false)
ts.AddAccount(t, "B")
ts.AddImport(t, "A", "foo", "B")
ts.AddImport(t, "A", jwt.Stream, "foo", "B")

GetConfig().SetAccount("")
_, _, err := ExecuteCmd(createDeleteImportCmd(), "--subject", "A")
Expand All @@ -68,8 +68,8 @@ func Test_DeleteImportInteractive(t *testing.T) {
ts.AddExport(t, "A", jwt.Stream, "bar", 0, false)

ts.AddAccount(t, "B")
ts.AddImport(t, "A", "foo", "B")
ts.AddImport(t, "A", "bar", "B")
ts.AddImport(t, "A", jwt.Stream, "foo", "B")
ts.AddImport(t, "A", jwt.Stream, "bar", "B")

input := []interface{}{1, 0, 0}
cmd := createDeleteImportCmd()
Expand Down
86 changes: 86 additions & 0 deletions cmd/describeaccount_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@ package cmd
import (
"encoding/json"
"fmt"
"github.com/nats-io/nkeys"
"os"
"path/filepath"
"runtime"
"strings"
"testing"
"time"

"github.com/nats-io/jwt/v2"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -309,3 +311,87 @@ func TestDescribeAccount_Exports(t *testing.T) {
require.Contains(t, out, "| Account Token Position |")
require.Contains(t, out, "foo.bar.*.> | 3")
}

func TestDescribeAccountMore(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("running in windows - looking at output hangs")
}
ts := NewTestStore(t, "O")
defer ts.Done(t)
ts.AddAccount(t, "A")
ac, err := ts.Store.ReadAccountClaim("A")
require.NoError(t, err)
ac.Description = "hello"
ac.InfoURL = "https://example.com"
_, signingKey, _ := CreateAccountKey(t)
ac.SigningKeys.Add(signingKey)

_, issuer, _ := CreateAccountKey(t)
scope := jwt.NewUserScope()
scope.Key = issuer
scope.Role = "nothing"
scope.Description = "no permissions"
scope.Template = jwt.UserPermissionLimits{
Permissions: jwt.Permissions{
Sub: jwt.Permission{Deny: []string{">"}},
Pub: jwt.Permission{Deny: []string{">"}},
},
}
ac.SigningKeys.AddScopedSigner(scope)

ac.Limits.JetStreamLimits = jwt.JetStreamLimits{DiskStorage: -1, MemoryStorage: -1}
ac.Limits.LeafNodeConn = 1

_, user, _ := CreateUserKey(t)
ac.Revocations = jwt.RevocationList{}
ac.Revocations.Revoke(user, time.Now())

ac.Trace = &jwt.MsgTrace{
Destination: "foo",
Sampling: 100,
}

ekp, err := nkeys.CreateUser()
require.NoError(t, err)
a, err := ekp.PublicKey()
require.NoError(t, err)
ac.Imports.Add(&jwt.Import{
Name: "hello",
Subject: "bar.>",
LocalSubject: "fromA.>",
Type: jwt.Stream,
AllowTrace: true,
Share: true,
Account: a,
})

ac.Mappings = make(map[jwt.Subject][]jwt.WeightedMapping)
ac.Mappings["mapfoo"] = []jwt.WeightedMapping{jwt.WeightedMapping{Subject: "map.>", Weight: 20, Cluster: "a"}}

token, err := ac.Encode(ts.OperatorKey)
require.NoError(t, err)
_, err = ts.Store.StoreClaim([]byte(token))
require.NoError(t, err)

out, _, err := ExecuteCmd(rootCmd, "describe", "account", "-n", "A")
require.NoError(t, err)

out = StripMultipleSpaces(out)
t.Log(out)
require.Contains(t, out, "| Description | hello")
require.Contains(t, out, "| Info Url | https://example.com")
// order of the key may be unexpected, just find the key
require.Contains(t, out, signingKey)
require.Contains(t, out, "| Max Disk Storage | Unlimited")
require.Contains(t, out, "| Max Mem Storage | Unlimited")
require.Contains(t, out, "| Max Leaf Node Connections | 1")
require.Contains(t, out, "| Revocations | 1")
require.Contains(t, out, "| Subject | foo")
require.Contains(t, out, "| Sampling | 100%")
require.Contains(t, out, "| hello | Stream | bar.> | fromA.>")
require.Contains(t, out, "| mapfoo | map.> | 20")

require.Contains(t, out, "| Key | "+issuer)
require.Contains(t, out, "| Role | nothing")
require.Contains(t, out, "| Description | no permissions")
}
1 change: 0 additions & 1 deletion cmd/describeuser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (
"testing"

"github.com/nats-io/jwt/v2"

"github.com/stretchr/testify/require"
)

Expand Down
49 changes: 37 additions & 12 deletions cmd/fixenv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,12 @@ package cmd

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

"github.com/nats-io/jwt/v2"
"github.com/nats-io/nsc/v2/cmd/store"
"github.com/stretchr/testify/require"
"path/filepath"
"testing"
"time"
)

func Test_FixRequiresInArg(t *testing.T) {
Expand Down Expand Up @@ -62,13 +61,6 @@ func Test_FixBasics(t *testing.T) {
require.NoError(t, err)
require.NoError(t, Write(filepath.Join(in, "o.jwt"), []byte(otok)))

// and another with a tag
oc.Tags.Add("test")
time.Sleep(time.Second)
otok2, err := oc.Encode(okp)
require.NoError(t, err)
require.NoError(t, Write(filepath.Join(in, "o2.jwt"), []byte(otok2)))

ask, apk, akp := CreateAccountKey(t)
require.NoError(t, Write(filepath.Join(in, "apk.nk"), ask))
ac := jwt.NewAccountClaims(apk)
Expand All @@ -85,8 +77,35 @@ func Test_FixBasics(t *testing.T) {
require.NoError(t, err)
require.NoError(t, Write(filepath.Join(in, "u.jwt"), []byte(utok)))

usk2, upk2, _ := CreateUserKey(t)
uc2 := jwt.NewUserClaims(upk2)
uc2.Name = "U2"
u2tok, err := uc2.Encode(akp)
require.NoError(t, err)
creds, err := jwt.FormatUserConfig(u2tok, usk2)
require.NoError(t, err)
require.NoError(t, Write(filepath.Join(in, "u2.creds"), creds))

// and copy of operator with a tag (and newer date)
time.Sleep(time.Second)

oc.Tags.Add("test")
otok2, err := oc.Encode(okp)
require.NoError(t, err)
require.NoError(t, Write(filepath.Join(in, "o2.jwt"), []byte(otok2)))

ac.Tags.Add("test")
atok, err = ac.Encode(okp)
require.NoError(t, err)
require.NoError(t, Write(filepath.Join(in, "a2.jwt"), []byte(atok)))

uc.Tags.Add("test")
utok, err = uc.Encode(akp)
require.NoError(t, err)
require.NoError(t, Write(filepath.Join(in, "u2.jwt"), []byte(utok)))

ofp := filepath.Join(ts.Dir, "out")
_, _, err = ExecuteCmd(createFixCmd(), "--in", in, "--out", ofp)
_, _, err = ExecuteCmd(createFixCmd(), "--creds", "--in", in, "--out", ofp)
require.NoError(t, err)

s, err := store.LoadStore(filepath.Join(ofp, "operators", "O"))
Expand All @@ -102,11 +121,17 @@ func Test_FixBasics(t *testing.T) {
require.NoError(t, err)
require.Equal(t, apk, aac.Subject)
require.Equal(t, "A", aac.Name)
require.True(t, aac.Tags.Contains("test"))

uuc, err := s.ReadUserClaim("A", "U")
require.NoError(t, err)
require.Equal(t, upk, uuc.Subject)
require.Equal(t, "U", uuc.Name)
require.True(t, uuc.Tags.Contains("test"))

uuc2, err := s.ReadUserClaim("A", "U2")
require.NoError(t, err)
require.Equal(t, upk2, uuc2.Subject)

okf := filepath.Join(ofp, "keys", "keys", opk[:1], opk[1:3], fmt.Sprintf("%s.nk", opk))
require.FileExists(t, okf)
Expand Down
Loading
Loading