Skip to content

Commit

Permalink
Update tests following auth changes in gnatsd
Browse files Browse the repository at this point in the history
Also added misspell check and updated Go versions in .travis.yml
  • Loading branch information
kozlovic committed Apr 21, 2017
1 parent 6949c8e commit a49a929
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 54 deletions.
10 changes: 6 additions & 4 deletions .travis.yml
@@ -1,20 +1,22 @@
language: go
sudo: false
go:
- 1.8
- 1.8.1
- 1.6.4
- 1.7.4
- 1.7.5
install:
- go get -t ./...
- go get github.com/nats-io/gnatsd
- go get github.com/mattn/goveralls
- go get github.com/wadey/gocovmerge
- go get honnef.co/go/tools/cmd/staticcheck
- go get honnef.co/go/tools/cmd/gosimple
- go get -u honnef.co/go/tools/cmd/staticcheck
- go get -u honnef.co/go/tools/cmd/gosimple
- go get -u github.com/client9/misspell/cmd/misspell
before_script:
- go fmt ./...
- go vet ./...
- gosimple ./...
- misspell -error -locale US ./...
- staticcheck -ignore "$(cat staticcheck.ignore)" ./...
script:
- go test -i -race ./...
Expand Down
52 changes: 20 additions & 32 deletions test/auth_test.go
Expand Up @@ -6,20 +6,16 @@ import (
"testing"
"time"

"github.com/nats-io/gnatsd/auth"
"github.com/nats-io/gnatsd/test"
"github.com/nats-io/go-nats"
)

func TestAuth(t *testing.T) {
s := RunServerOnPort(8232)

// Auth is pluggable, so need to set here..
auth := &auth.Plain{
Username: "derek",
Password: "foo",
}
s.SetClientAuthMethod(auth)

opts := test.DefaultTestOptions
opts.Port = 8232
opts.Username = "derek"
opts.Password = "foo"
s := RunServerWithOptions(opts)
defer s.Shutdown()

_, err := nats.Connect("nats://localhost:8232")
Expand Down Expand Up @@ -54,15 +50,11 @@ func TestAuth(t *testing.T) {
}

func TestAuthFailNoDisconnectCB(t *testing.T) {
s := RunServerOnPort(8232)

// Auth is pluggable, so need to set here..
auth := &auth.Plain{
Username: "derek",
Password: "foo",
}
s.SetClientAuthMethod(auth)

opts := test.DefaultTestOptions
opts.Port = 8232
opts.Username = "derek"
opts.Password = "foo"
s := RunServerWithOptions(opts)
defer s.Shutdown()

copts := nats.DefaultOptions
Expand Down Expand Up @@ -91,13 +83,11 @@ func TestAuthFailAllowReconnect(t *testing.T) {
"nats://localhost:23234",
}

ts2 := RunServerOnPort(23233)
// Auth is pluggable, so need to set here..
auth := &auth.Plain{
Username: "ivan",
Password: "foo",
}
ts2.SetClientAuthMethod(auth)
ots2 := test.DefaultTestOptions
ots2.Port = 23233
ots2.Username = "ivan"
ots2.Password = "foo"
ts2 := RunServerWithOptions(ots2)
defer ts2.Shutdown()

ts3 := RunServerOnPort(23234)
Expand Down Expand Up @@ -144,13 +134,11 @@ func TestAuthFailAllowReconnect(t *testing.T) {
}

func TestTokenAuth(t *testing.T) {
s := RunServerOnPort(8232)

opts := test.DefaultTestOptions
opts.Port = 8232
secret := "S3Cr3T0k3n!"
// Auth is pluggable, so need to set here..
auth := &auth.Token{Token: secret}
s.SetClientAuthMethod(auth)

opts.Authorization = secret
s := RunServerWithOptions(opts)
defer s.Shutdown()

_, err := nats.Connect("nats://localhost:8232")
Expand Down
2 changes: 1 addition & 1 deletion test/basic_test.go
Expand Up @@ -76,7 +76,7 @@ func TestConnectedServer(t *testing.T) {
}
srv := nc.ConnectedServerId()
if srv == "" {
t.Fatal("Expeced a connected server id")
t.Fatal("Expected a connected server id")
}
nc.Close()
u = nc.ConnectedUrl()
Expand Down
17 changes: 8 additions & 9 deletions test/cluster_test.go
Expand Up @@ -9,7 +9,7 @@ import (
"testing"
"time"

"github.com/nats-io/gnatsd/auth"
"github.com/nats-io/gnatsd/test"
"github.com/nats-io/go-nats"
)

Expand Down Expand Up @@ -123,16 +123,15 @@ func TestAuthServers(t *testing.T) {
"nats://localhost:1224",
}

auth := &auth.Plain{
Username: "derek",
Password: "foo",
}
opts := test.DefaultTestOptions
opts.Username = "derek"
opts.Password = "foo"

as1 := RunServerOnPort(1222)
as1.SetClientAuthMethod(auth)
opts.Port = 1222
as1 := RunServerWithOptions(opts)
defer as1.Shutdown()
as2 := RunServerOnPort(1224)
as2.SetClientAuthMethod(auth)
opts.Port = 1224
as2 := RunServerWithOptions(opts)
defer as2.Shutdown()

pservers := strings.Join(plainServers, ",")
Expand Down
15 changes: 7 additions & 8 deletions test/conn_test.go
Expand Up @@ -5,7 +5,6 @@ import (
"bytes"
"crypto/tls"
"crypto/x509"
"errors"
"fmt"
"io/ioutil"
"net"
Expand Down Expand Up @@ -132,7 +131,7 @@ func TestServerSecureConnections(t *testing.T) {

received := 0
nc.Subscribe("foo", func(m *nats.Msg) {
received += 1
received++
if !bytes.Equal(m.Data, omsg) {
t.Fatal("Message received does not match")
}
Expand Down Expand Up @@ -222,30 +221,30 @@ func TestClientCertificate(t *testing.T) {
nc, err := nats.Connect(secureURL, nats.Secure())
if err == nil {
nc.Close()
t.Fatal("Sould have failed (TLS) connection without client certificate")
t.Fatal("Should have failed (TLS) connection without client certificate")
}

// Check parameters validity
nc, err = nats.Connect(secureURL, nats.ClientCert("", ""))
if err == nil {
nc.Close()
t.Fatal("Sould have failed due to invalid parameters")
t.Fatal("Should have failed due to invalid parameters")
}

// Should fail because wrong key
nc, err = nats.Connect(secureURL,
nats.ClientCert("./configs/certs/client-cert.pem", "./configs/certs/key.pem"))
if err == nil {
nc.Close()
t.Fatal("Sould have failed due to invalid key")
t.Fatal("Should have failed due to invalid key")
}

// Should fail because no CA
nc, err = nats.Connect(secureURL,
nats.ClientCert("./configs/certs/client-cert.pem", "./configs/certs/client-key.pem"))
if err == nil {
nc.Close()
t.Fatal("Sould have failed due to missing ca")
t.Fatal("Should have failed due to missing ca")
}

nc, err = nats.Connect(secureURL,
Expand All @@ -261,7 +260,7 @@ func TestClientCertificate(t *testing.T) {

received := 0
nc.Subscribe("foo", func(m *nats.Msg) {
received += 1
received++
if !bytes.Equal(m.Data, omsg) {
t.Fatal("Message received does not match")
}
Expand Down Expand Up @@ -613,7 +612,7 @@ func isRunningInAsyncCBDispatcher() error {
return nil
}

return errors.New(fmt.Sprintf("Callback not executed from dispatcher:\n %s\n", strStacks))
return fmt.Errorf("callback not executed from dispatcher:\n %s", strStacks)
}

func TestCallbacksOrder(t *testing.T) {
Expand Down

0 comments on commit a49a929

Please sign in to comment.