Skip to content

Commit

Permalink
Simplify ExecuteCmd output handling in tests.
Browse files Browse the repository at this point in the history
This commit removes unused return values from `ExecuteCmd` calls, retaining only relevant ones such as `out` or `err`. It also adjusts related assertions and parsing logic to reflect this change, improving clarity and consistency across test files. This is an initial step towards normalizing where the CLI is outputting results looked at by tests.

Signed-off-by: Alberto Ricart <[email protected]>
  • Loading branch information
aricart committed Jan 5, 2025
1 parent e838251 commit 30401dc
Show file tree
Hide file tree
Showing 67 changed files with 965 additions and 968 deletions.
10 changes: 5 additions & 5 deletions cmd/action_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func TestActionContextSet(t *testing.T) {
},
}

_, _, err := ExecuteCmd(cmd, "hello", "world")
_, err := ExecuteCmd(cmd, "hello", "world")
require.NoError(t, err)
}

Expand Down Expand Up @@ -118,7 +118,7 @@ func TestActionNothingToDoEmpty(t *testing.T) {
}

cmd.Flags().StringVarP(&v, "name", "n", "", "account name")
_, _, err := ExecuteCmd(cmd)
_, err := ExecuteCmd(cmd)
require.NoError(t, err)
require.True(t, nothingToDo)
}
Expand Down Expand Up @@ -148,7 +148,7 @@ func TestActionNothingToDoSet(t *testing.T) {
}

cmd.Flags().StringVarP(&v, "name", "n", "", "account name")
_, _, err := ExecuteCmd(cmd, "--name", "a")
_, err := ExecuteCmd(cmd, "--name", "a")
require.NoError(t, err)
require.False(t, nothingToDo)
}
Expand Down Expand Up @@ -226,7 +226,7 @@ func TestActionMessage(t *testing.T) {
},
}

out, _, err := ExecuteCmd(cmd)
out, err := ExecuteCmd(cmd)
require.NoError(t, err)
require.Contains(t, "This is a test message", out)
require.Contains(t, out.Err, "this is a test message")
}
24 changes: 12 additions & 12 deletions cmd/addaccount_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func Test_AddAccount(t *testing.T) {
func Test_AddAccountNoStore(t *testing.T) {
// reset the store
require.NoError(t, ForceStoreRoot(t, ""))
_, _, err := ExecuteCmd(CreateAddAccountCmd())
_, err := ExecuteCmd(CreateAddAccountCmd())
require.NotNil(t, err)
require.Equal(t, "no stores available", err.Error())
}
Expand All @@ -65,7 +65,7 @@ func Test_AddAccountValidateOutput(t *testing.T) {
ts := NewTestStore(t, "test")
defer ts.Done(t)

_, _, err := ExecuteCmd(CreateAddAccountCmd(), "--name", "A", "--start", "2018-01-01", "--expiry", "2050-01-01")
_, err := ExecuteCmd(CreateAddAccountCmd(), "--name", "A", "--start", "2018-01-01", "--expiry", "2050-01-01")
require.NoError(t, err)
validateAddAccountClaims(t, ts)
ts.List(t)
Expand Down Expand Up @@ -124,7 +124,7 @@ func Test_AddAccountManagedStore(t *testing.T) {
ts := NewTestStoreWithOperatorJWT(t, string(m["operator"]))
defer ts.Done(t)

_, _, err := ExecuteCmd(CreateAddAccountCmd(), "--name", "A", "--start", "2018-01-01", "--expiry", "2050-01-01")
_, err := ExecuteCmd(CreateAddAccountCmd(), "--name", "A", "--start", "2018-01-01", "--expiry", "2050-01-01")
require.NoError(t, err)
}

Expand All @@ -142,7 +142,7 @@ func Test_AddAccountManagedStoreWithSigningKey(t *testing.T) {
tf := filepath.Join(ts.Dir, "O.jwt")
err = Write(tf, []byte(token))
require.NoError(t, err)
_, _, err = ExecuteCmd(createAddOperatorCmd(), "--url", tf)
_, err = ExecuteCmd(createAddOperatorCmd(), "--url", tf)
require.NoError(t, err)
// sign with the signing key
inputs := []interface{}{"A", true, "0", "0", 0, string(s1)}
Expand Down Expand Up @@ -172,7 +172,7 @@ func Test_AddAccountInteractiveSigningKey(t *testing.T) {
defer ts.Done(t)

s1, pk1, _ := CreateOperatorKey(t)
_, _, err := ExecuteCmd(createEditOperatorCmd(), "--sk", pk1)
_, err := ExecuteCmd(createEditOperatorCmd(), "--sk", pk1)
require.NoError(t, err)

// sign with the custom key
Expand All @@ -196,7 +196,7 @@ func Test_AddAccountNameArg(t *testing.T) {
ts := NewTestStore(t, "O")
defer ts.Done(t)

_, _, err := ExecuteCmd(HoistRootFlags(CreateAddAccountCmd()), "A")
_, err := ExecuteCmd(HoistRootFlags(CreateAddAccountCmd()), "A")
require.NoError(t, err)

_, err = ts.Store.ReadAccountClaim("A")
Expand All @@ -214,7 +214,7 @@ func Test_AddAccountWithExistingKey(t *testing.T) {
pk, err := kp.PublicKey()
require.NoError(t, err)

_, _, err = ExecuteCmd(CreateAddAccountCmd(), "A", "--public-key", pk)
_, err = ExecuteCmd(CreateAddAccountCmd(), "A", "--public-key", pk)
require.NoError(t, err)
}

Expand All @@ -232,7 +232,7 @@ func Test_AddManagedAccountWithExistingKey(t *testing.T) {
pk, err := kp.PublicKey()
require.NoError(t, err)

_, _, err = ExecuteCmd(CreateAddAccountCmd(), "A", "--public-key", pk)
_, err = ExecuteCmd(CreateAddAccountCmd(), "A", "--public-key", pk)
require.NoError(t, err)

// inspect the pushed JWT before it was resigned
Expand All @@ -254,15 +254,15 @@ func Test_AddAccountWithSigningKeyOnly(t *testing.T) {
require.NoError(t, err)
require.True(t, ts.KeyStore.HasPrivateKey(pk))

_, _, err = ExecuteCmd(createEditOperatorCmd(), "--sk", pk)
_, err = ExecuteCmd(createEditOperatorCmd(), "--sk", pk)
require.NoError(t, err)
oc, err := ts.Store.ReadOperatorClaim()
require.NoError(t, err)
require.NotNil(t, oc)
require.NoError(t, ts.KeyStore.Remove(oc.Subject))
require.False(t, ts.KeyStore.HasPrivateKey(oc.Subject))

_, _, err = ExecuteCmd(CreateAddAccountCmd(), "--name", "A")
_, err = ExecuteCmd(CreateAddAccountCmd(), "--name", "A")
require.NoError(t, err)

_, err = ts.Store.ReadAccountClaim("A")
Expand All @@ -273,7 +273,7 @@ func Test_AddAccount_Pubs(t *testing.T) {
ts := NewTestStore(t, "edit user")
defer ts.Done(t)

_, _, err := ExecuteCmd(CreateAddAccountCmd(), "-n", "A", "--allow-pub", "a,b", "--allow-pubsub", "c", "--deny-pub", "foo", "--deny-pubsub", "bar")
_, err := ExecuteCmd(CreateAddAccountCmd(), "-n", "A", "--allow-pub", "a,b", "--allow-pubsub", "c", "--deny-pub", "foo", "--deny-pubsub", "bar")
require.NoError(t, err)

cc, err := ts.Store.ReadAccountClaim("A")
Expand All @@ -289,7 +289,7 @@ func Test_AddAccountBadName(t *testing.T) {
ts := NewTestStore(t, "O")
defer ts.Done(t)

_, _, err := ExecuteCmd(CreateAddAccountCmd(), "A/B")
_, err := ExecuteCmd(CreateAddAccountCmd(), "A/B")
require.Error(t, err)
require.Contains(t, err.Error(), "name cannot contain '/' or '\\'")
}
14 changes: 7 additions & 7 deletions cmd/addexport_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ func Test_AddExportManagedStore(t *testing.T) {
defer ts.Done(t)

ts.AddAccount(t, "A")
_, _, err := ExecuteCmd(createAddExportCmd(), "--subject", "aaaa")
_, err := ExecuteCmd(createAddExportCmd(), "--subject", "aaaa")
require.NoError(t, err)

ac, err := ts.Store.ReadAccountClaim("A")
Expand All @@ -143,7 +143,7 @@ func Test_AddExportAccountNameRequired(t *testing.T) {
ts.AddAccount(t, "B")
t.Log("B", ts.GetAccountPublicKey(t, "B"))

_, _, err := ExecuteCmd(createAddExportCmd(), "--subject", "bbbb")
_, err := ExecuteCmd(createAddExportCmd(), "--subject", "bbbb")
require.NoError(t, err)

ac, err := ts.Store.ReadAccountClaim("B")
Expand Down Expand Up @@ -206,7 +206,7 @@ func TestAddExportNonInteractive(t *testing.T) {

cmd := createAddExportCmd()
HoistRootFlags(cmd)
_, _, err := ExecuteCmd(cmd, "--service", "--name", "q", "--subject", "q", "--response-type", jwt.ResponseTypeChunked)
_, err := ExecuteCmd(cmd, "--service", "--name", "q", "--subject", "q", "--response-type", jwt.ResponseTypeChunked)
require.NoError(t, err)

ac, err := ts.Store.ReadAccountClaim("A")
Expand All @@ -225,7 +225,7 @@ func TestAddServiceLatency(t *testing.T) {
ts.AddAccount(t, "A")
cmd := createAddExportCmd()

_, _, err := ExecuteCmd(cmd, "--service", "--subject", "q", "--latency", "q.lat", "--sampling", "100", "--response-type", jwt.ResponseTypeStream)
_, err := ExecuteCmd(cmd, "--service", "--subject", "q", "--latency", "q.lat", "--sampling", "100", "--response-type", jwt.ResponseTypeStream)
require.NoError(t, err)

ac, err := ts.Store.ReadAccountClaim("A")
Expand All @@ -245,7 +245,7 @@ func Test_AddExportBadResponseType(t *testing.T) {
ts.AddAccount(t, "A")
cmd := createAddExportCmd()

_, _, err := ExecuteCmd(cmd, "--service", "--subject", "q", "--response-type", "foo")
_, err := ExecuteCmd(cmd, "--service", "--subject", "q", "--response-type", "foo")
require.Error(t, err)
require.Contains(t, err.Error(), "invalid response type")
}
Expand Down Expand Up @@ -279,7 +279,7 @@ func TestAddServiceExportWTracing(t *testing.T) {
defer ts.Done(t)

ts.AddAccount(t, "A")
_, _, err := ExecuteCmd(createAddExportCmd(), "--service", "--subject", "q", "--allow-trace")
_, err := ExecuteCmd(createAddExportCmd(), "--service", "--subject", "q", "--allow-trace")
require.NoError(t, err)

ac, err := ts.Store.ReadAccountClaim("A")
Expand All @@ -292,6 +292,6 @@ func TestAddStreamExportWTracing(t *testing.T) {
defer ts.Done(t)

ts.AddAccount(t, "A")
_, _, err := ExecuteCmd(createAddExportCmd(), "--subject", "q", "--allow-trace")
_, err := ExecuteCmd(createAddExportCmd(), "--subject", "q", "--allow-trace")
require.Error(t, err)
}
24 changes: 12 additions & 12 deletions cmd/addimport_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func Test_AddImportSelfImportsRejected(t *testing.T) {
fp := filepath.Join(ts.Dir, "token.jwt")
require.NoError(t, Write(fp, []byte(token)))

_, _, err := ExecuteCmd(createAddImportCmd(), "--token", fp)
_, err := ExecuteCmd(createAddImportCmd(), "--token", fp)
require.Error(t, err)
require.Equal(t, "export issuer is this account", err.Error())
}
Expand All @@ -88,7 +88,7 @@ func Test_AddImportFromURL(t *testing.T) {
}))
defer ht.Close()

_, _, err := ExecuteCmd(createAddImportCmd(), "--account", "B", "--token", ht.URL)
_, err := ExecuteCmd(createAddImportCmd(), "--account", "B", "--token", ht.URL)
require.NoError(t, err)

ac, err := ts.Store.ReadAccountClaim("B")
Expand Down Expand Up @@ -199,7 +199,7 @@ func Test_AddPublicImport(t *testing.T) {
ts.AddExport(t, "A", jwt.Stream, "foobar.>", 0, true)
ts.AddAccount(t, "B")

_, _, err := ExecuteCmd(createAddImportCmd(), "--account", "B", "--src-account", "A", "--remote-subject", "foobar.>")
_, err := ExecuteCmd(createAddImportCmd(), "--account", "B", "--src-account", "A", "--remote-subject", "foobar.>")
require.NoError(t, err)

ac, err := ts.Store.ReadAccountClaim("B")
Expand All @@ -212,7 +212,7 @@ func Test_AddImport_TokenAndPublic(t *testing.T) {
defer ts.Done(t)

ts.AddAccount(t, "A")
_, _, err := ExecuteCmd(createAddImportCmd(), "--token", "/foo", "--remote-subject", "foobar.>")
_, err := ExecuteCmd(createAddImportCmd(), "--token", "/foo", "--remote-subject", "foobar.>")
require.Error(t, err)
require.Contains(t, err.Error(), "private imports require src-account")
}
Expand All @@ -222,7 +222,7 @@ func Test_AddImport_MoreForPublic(t *testing.T) {
defer ts.Done(t)

ts.AddAccount(t, "A")
_, _, err := ExecuteCmd(createAddImportCmd(), "--remote-subject", "foobar.>")
_, err := ExecuteCmd(createAddImportCmd(), "--remote-subject", "foobar.>")
require.Error(t, err)
require.Contains(t, err.Error(), "public imports require src-account, remote-subject")
}
Expand Down Expand Up @@ -312,7 +312,7 @@ func Test_AddImportWithSigningKeyToken(t *testing.T) {

_, pk, sk := CreateAccountKey(t)
ts.AddAccount(t, "A")
_, _, err := ExecuteCmd(createEditAccount(), "--sk", pk)
_, err := ExecuteCmd(createEditAccount(), "--sk", pk)
require.NoError(t, err)
ts.AddExport(t, "A", jwt.Stream, "foobar.>", 0, false)

Expand All @@ -335,7 +335,7 @@ func Test_AddImportWithSigningKeyToken(t *testing.T) {
// account to import is B
require.Equal(t, acc.Subject, bc.Subject)

_, _, err = ExecuteCmd(createAddImportCmd(), "--account", "B", "--token", tp)
_, err = ExecuteCmd(createAddImportCmd(), "--account", "B", "--token", tp)
require.NoError(t, err)
acb, err := ts.Store.ReadAccountClaim("B")
require.NoError(t, err)
Expand All @@ -349,7 +349,7 @@ func Test_AddDecoratedToken(t *testing.T) {

_, pk, sk := CreateAccountKey(t)
ts.AddAccount(t, "A")
_, _, err := ExecuteCmd(createEditAccount(), "--sk", pk)
_, err := ExecuteCmd(createEditAccount(), "--sk", pk)
require.NoError(t, err)
ts.AddExport(t, "A", jwt.Stream, "foobar.>", 0, false)

Expand All @@ -361,7 +361,7 @@ func Test_AddDecoratedToken(t *testing.T) {
tp := filepath.Join(ts.Dir, "token.jwt")
require.NoError(t, Write(tp, []byte(token)))

_, _, err = ExecuteCmd(createAddImportCmd(), "--account", "B", "--token", tp)
_, err = ExecuteCmd(createAddImportCmd(), "--account", "B", "--token", tp)
require.NoError(t, err)
acb, err := ts.Store.ReadAccountClaim("B")
require.NoError(t, err)
Expand Down Expand Up @@ -434,7 +434,7 @@ func Test_ImportStreamHandlesDecorations(t *testing.T) {

ap := filepath.Join(ts.Dir, "activation.jwt")
Write(ap, d)
_, _, err = ExecuteCmd(createAddImportCmd(), "--account", "B", "--token", ap)
_, err = ExecuteCmd(createAddImportCmd(), "--account", "B", "--token", ap)
require.NoError(t, err)

bc, err := ts.Store.ReadAccountClaim("B")
Expand All @@ -458,7 +458,7 @@ func Test_ImportServiceHandlesDecorations(t *testing.T) {

ap := filepath.Join(ts.Dir, "activation.jwt")
Write(ap, d)
_, _, err = ExecuteCmd(createAddImportCmd(), "--account", "B", "--token", ap)
_, err = ExecuteCmd(createAddImportCmd(), "--account", "B", "--token", ap)
require.NoError(t, err)

bc, err := ts.Store.ReadAccountClaim("B")
Expand All @@ -476,7 +476,7 @@ func Test_AddImportToAccount(t *testing.T) {

bpk := ts.GetAccountPublicKey(t, "B")

_, _, err := ExecuteCmd(createAddImportCmd(), "--account", "A", "--src-account", bpk, "--remote-subject", "s.>")
_, err := ExecuteCmd(createAddImportCmd(), "--account", "A", "--src-account", bpk, "--remote-subject", "s.>")
require.NoError(t, err)

bc, err := ts.Store.ReadAccountClaim("A")
Expand Down
14 changes: 7 additions & 7 deletions cmd/addmapping_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ func Test_AddMappingCannotExceed100(t *testing.T) {

ts.AddAccount(t, "A")
// add a default mapping at a 100, default mapping at max
_, _, err := ExecuteCmd(createAddMappingCmd(), "--from", "q", "--to", "qq", "--weight", "100")
_, err := ExecuteCmd(createAddMappingCmd(), "--from", "q", "--to", "qq", "--weight", "100")
require.NoError(t, err)

// default mapping cannot be incremented
_, _, err = ExecuteCmd(createAddMappingCmd(), "--from", "q", "--to", "qqq", "--weight", "10")
_, err = ExecuteCmd(createAddMappingCmd(), "--from", "q", "--to", "qqq", "--weight", "10")
require.Error(t, err)
require.ErrorContains(t, err, "Mapping \"q\" exceeds 100%")

Expand All @@ -33,14 +33,14 @@ func Test_AddMappingCannotExceed100(t *testing.T) {
}, mappings[0])

// can add another mapping but has to be for cluster
_, _, err = ExecuteCmd(createAddMappingCmd(), "--from", "q", "--to", "qa", "--weight", "100", "--cluster", "A")
_, err = ExecuteCmd(createAddMappingCmd(), "--from", "q", "--to", "qa", "--weight", "100", "--cluster", "A")
require.NoError(t, err)
// and for another cluster
_, _, err = ExecuteCmd(createAddMappingCmd(), "--from", "q", "--to", "qb", "--weight", "100", "--cluster", "B")
_, err = ExecuteCmd(createAddMappingCmd(), "--from", "q", "--to", "qb", "--weight", "100", "--cluster", "B")
require.NoError(t, err)

// incrementing one of the above maxed clusters fails
_, _, err = ExecuteCmd(createAddMappingCmd(), "--from", "q", "--to", "qaa", "--weight", "10", "--cluster", "A")
_, err = ExecuteCmd(createAddMappingCmd(), "--from", "q", "--to", "qaa", "--weight", "10", "--cluster", "A")
require.Error(t, err)
require.ErrorContains(t, err, "Mapping \"q\" in cluster \"A\" exceeds 100%")
}
Expand All @@ -51,7 +51,7 @@ func Test_AddMappingCrud(t *testing.T) {

ts.AddAccount(t, "A")
// add a default mapping at a 100, default mapping at max
_, _, err := ExecuteCmd(createAddMappingCmd(), "--from", "q", "--to", "qq", "--weight", "100")
_, err := ExecuteCmd(createAddMappingCmd(), "--from", "q", "--to", "qq", "--weight", "100")
require.NoError(t, err)

ac, err := ts.Store.ReadAccountClaim("A")
Expand All @@ -66,7 +66,7 @@ func Test_AddMappingCrud(t *testing.T) {

ts.AddAccount(t, "B")
// add a default mapping at a 100, default mapping at max
_, _, err = ExecuteCmd(createAddMappingCmd(), "--from", "qq", "--to", "rr", "--weight", "100", "--cluster", "B")
_, err = ExecuteCmd(createAddMappingCmd(), "--from", "qq", "--to", "rr", "--weight", "100", "--cluster", "B")
require.NoError(t, err)

ac, err = ts.Store.ReadAccountClaim("B")
Expand Down
Loading

0 comments on commit 30401dc

Please sign in to comment.