Skip to content

Commit

Permalink
*: test updates
Browse files Browse the repository at this point in the history
  • Loading branch information
mreiferson committed Mar 3, 2019
1 parent cf5526e commit 9d332eb
Show file tree
Hide file tree
Showing 10 changed files with 174 additions and 170 deletions.
2 changes: 2 additions & 0 deletions apps/nsqd/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ import (

"github.com/BurntSushi/toml"
"github.com/mreiferson/go-options"
"github.com/nsqio/nsq/internal/test"
"github.com/nsqio/nsq/nsqd"
)

func TestConfigFlagParsing(t *testing.T) {
opts := nsqd.NewOptions()
opts.Logger = test.NewTestLogger(t)

flagSet := nsqdFlagSet(opts)
flagSet.Parse([]string{})
Expand Down
60 changes: 49 additions & 11 deletions nsqadmin/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,16 @@ type ChannelStatsDoc struct {
func mustStartNSQLookupd(opts *nsqlookupd.Options) (*net.TCPAddr, *net.TCPAddr, *nsqlookupd.NSQLookupd) {
opts.TCPAddress = "127.0.0.1:0"
opts.HTTPAddress = "127.0.0.1:0"
lookupd := nsqlookupd.New(opts)
lookupd.Main()
lookupd, err := nsqlookupd.New(opts)
if err != nil {
panic(err)
}
go func() {
err := lookupd.Main()
if err != nil {
panic(err)
}
}()
return lookupd.RealTCPAddr(), lookupd.RealHTTPAddr(), lookupd
}

Expand All @@ -66,8 +74,16 @@ func bootstrapNSQClusterWithAuth(t *testing.T, withAuth bool) (string, []*nsqd.N
nsqlookupdOpts.HTTPAddress = "127.0.0.1:0"
nsqlookupdOpts.BroadcastAddress = "127.0.0.1"
nsqlookupdOpts.Logger = lgr
nsqlookupd1 := nsqlookupd.New(nsqlookupdOpts)
nsqlookupd1.Main()
nsqlookupd1, err := nsqlookupd.New(nsqlookupdOpts)
if err != nil {
panic(err)
}
go func() {
err := nsqlookupd1.Main()
if err != nil {
panic(err)
}
}()

time.Sleep(100 * time.Millisecond)

Expand All @@ -82,8 +98,16 @@ func bootstrapNSQClusterWithAuth(t *testing.T, withAuth bool) (string, []*nsqd.N
panic(err)
}
nsqdOpts.DataPath = tmpDir
nsqd1 := nsqd.New(nsqdOpts)
nsqd1.Main()
nsqd1, err := nsqd.New(nsqdOpts)
if err != nil {
panic(err)
}
go func() {
err := nsqd1.Main()
if err != nil {
panic(err)
}
}()

nsqadminOpts := NewOptions()
nsqadminOpts.HTTPAddress = "127.0.0.1:0"
Expand All @@ -92,8 +116,16 @@ func bootstrapNSQClusterWithAuth(t *testing.T, withAuth bool) (string, []*nsqd.N
if withAuth {
nsqadminOpts.AdminUsers = []string{"matt"}
}
nsqadmin1 := New(nsqadminOpts)
nsqadmin1.Main()
nsqadmin1, err := New(nsqadminOpts)
if err != nil {
panic(err)
}
go func() {
err := nsqadmin1.Main()
if err != nil {
panic(err)
}
}()

time.Sleep(100 * time.Millisecond)

Expand Down Expand Up @@ -573,7 +605,7 @@ func TestHTTPconfig(t *testing.T) {
defer resp.Body.Close()
body, _ = ioutil.ReadAll(resp.Body)
test.Equal(t, 200, resp.StatusCode)
test.Equal(t, LOG_FATAL, nsqadmin1.getOpts().logLevel)
test.Equal(t, LOG_FATAL, nsqadmin1.getOpts().LogLevel)

url = fmt.Sprintf("http://%s/config/log_level", nsqadmin1.RealHTTPAddr())
req, err = http.NewRequest("PUT", url, bytes.NewBuffer([]byte(`bad`)))
Expand All @@ -591,8 +623,14 @@ func TestHTTPconfigCIDR(t *testing.T) {
opts.NSQLookupdHTTPAddresses = []string{"127.0.0.1:4161"}
opts.Logger = test.NewTestLogger(t)
opts.AllowConfigFromCIDR = "10.0.0.0/8"
nsqadmin := New(opts)
nsqadmin.Main()
nsqadmin, err := New(opts)
test.Nil(t, err)
go func() {
err := nsqadmin.Main()
if err != nil {
panic(err)
}
}()
defer nsqadmin.Exit()

time.Sleep(100 * time.Millisecond)
Expand Down
89 changes: 33 additions & 56 deletions nsqadmin/nsqadmin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"net/http"
"net/url"
"os"
"os/exec"
"testing"

"github.com/nsqio/nsq/internal/lg"
Expand All @@ -16,41 +15,23 @@ import (
)

func TestNeitherNSQDAndNSQLookup(t *testing.T) {
if os.Getenv("BE_CRASHER") == "1" {
opts := NewOptions()
opts.Logger = lg.NilLogger{}
opts.HTTPAddress = "127.0.0.1:0"
New(opts)
return
}
cmd := exec.Command(os.Args[0], "-test.run=TestNeitherNSQDAndNSQLookup")
cmd.Env = append(os.Environ(), "BE_CRASHER=1")
err := cmd.Run()
test.Equal(t, "exit status 1", fmt.Sprintf("%v", err))
if e, ok := err.(*exec.ExitError); ok && !e.Success() {
return
}
t.Fatalf("process ran with err %v, want exit status 1", err)
opts := NewOptions()
opts.Logger = lg.NilLogger{}
opts.HTTPAddress = "127.0.0.1:0"
_, err := New(opts)
test.NotNil(t, err)
test.Equal(t, "--nsqd-http-address or --lookupd-http-address required", fmt.Sprintf("%s", err))
}

func TestBothNSQDAndNSQLookup(t *testing.T) {
if os.Getenv("BE_CRASHER") == "1" {
opts := NewOptions()
opts.Logger = lg.NilLogger{}
opts.HTTPAddress = "127.0.0.1:0"
opts.NSQLookupdHTTPAddresses = []string{"127.0.0.1:4161"}
opts.NSQDHTTPAddresses = []string{"127.0.0.1:4151"}
New(opts)
return
}
cmd := exec.Command(os.Args[0], "-test.run=TestBothNSQDAndNSQLookup")
cmd.Env = append(os.Environ(), "BE_CRASHER=1")
err := cmd.Run()
test.Equal(t, "exit status 1", fmt.Sprintf("%v", err))
if e, ok := err.(*exec.ExitError); ok && !e.Success() {
return
}
t.Fatalf("process ran with err %v, want exit status 1", err)
opts := NewOptions()
opts.Logger = lg.NilLogger{}
opts.HTTPAddress = "127.0.0.1:0"
opts.NSQLookupdHTTPAddresses = []string{"127.0.0.1:4161"}
opts.NSQDHTTPAddresses = []string{"127.0.0.1:4151"}
_, err := New(opts)
test.NotNil(t, err)
test.Equal(t, "use --nsqd-http-address or --lookupd-http-address not both", fmt.Sprintf("%s", err))
}

func TestTLSHTTPClient(t *testing.T) {
Expand All @@ -73,8 +54,14 @@ func TestTLSHTTPClient(t *testing.T) {
opts.HTTPClientTLSCert = "./test/client.pem"
opts.HTTPClientTLSKey = "./test/client.key"
opts.Logger = lgr
nsqadmin := New(opts)
nsqadmin.Main()
nsqadmin, err := New(opts)
test.Nil(t, err)
go func() {
err := nsqadmin.Main()
if err != nil {
panic(err)
}
}()
defer nsqadmin.Exit()

httpAddr := nsqadmin.RealHTTPAddr()
Expand All @@ -85,7 +72,7 @@ func TestTLSHTTPClient(t *testing.T) {
}

resp, err := http.Get(u.String())
test.Equal(t, nil, err)
test.Nil(t, err)
defer resp.Body.Close()

test.Equal(t, resp.StatusCode < 500, true)
Expand All @@ -102,25 +89,15 @@ func mustStartNSQD(opts *nsqd.Options) (*net.TCPAddr, *net.TCPAddr, *nsqd.NSQD)
}
opts.DataPath = tmpDir
}
nsqd := nsqd.New(opts)
nsqd.Main()
return nsqd.RealTCPAddr(), nsqd.RealHTTPAddr(), nsqd
}

func TestCrashingLogger(t *testing.T) {
if os.Getenv("BE_CRASHER") == "1" {
// Test invalid log level causes error
opts := NewOptions()
opts.LogLevel = "bad"
opts.NSQLookupdHTTPAddresses = []string{"127.0.0.1:4161"}
_ = New(opts)
return
}
cmd := exec.Command(os.Args[0], "-test.run=TestCrashingLogger")
cmd.Env = append(os.Environ(), "BE_CRASHER=1")
err := cmd.Run()
if e, ok := err.(*exec.ExitError); ok && !e.Success() {
return
nsqd, err := nsqd.New(opts)
if err != nil {
panic(err)
}
t.Fatalf("process ran with err %v, want exit status 1", err)
go func() {
err := nsqd.Main()
if err != nil {
panic(err)
}
}()
return nsqd.RealTCPAddr(), nsqd.RealHTTPAddr(), nsqd
}
27 changes: 4 additions & 23 deletions nsqd/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ func TestHTTPpubDefer(t *testing.T) {
func TestHTTPSRequire(t *testing.T) {
opts := NewOptions()
opts.Logger = test.NewTestLogger(t)
opts.LogLevel = "debug"
opts.LogLevel = LOG_DEBUG
opts.TLSCert = "./test/certs/server.pem"
opts.TLSKey = "./test/certs/server.key"
opts.TLSClientAuthPolicy = "require"
Expand Down Expand Up @@ -277,7 +277,7 @@ func TestHTTPSRequire(t *testing.T) {
func TestHTTPSRequireVerify(t *testing.T) {
opts := NewOptions()
opts.Logger = test.NewTestLogger(t)
opts.LogLevel = "debug"
opts.LogLevel = LOG_DEBUG
opts.TLSCert = "./test/certs/server.pem"
opts.TLSKey = "./test/certs/server.key"
opts.TLSRootCAFile = "./test/certs/ca.pem"
Expand Down Expand Up @@ -341,7 +341,7 @@ func TestHTTPSRequireVerify(t *testing.T) {
func TestTLSRequireVerifyExceptHTTP(t *testing.T) {
opts := NewOptions()
opts.Logger = test.NewTestLogger(t)
opts.LogLevel = "debug"
opts.LogLevel = LOG_DEBUG
opts.TLSCert = "./test/certs/server.pem"
opts.TLSKey = "./test/certs/server.key"
opts.TLSRootCAFile = "./test/certs/ca.pem"
Expand Down Expand Up @@ -633,7 +633,7 @@ func TestHTTPconfig(t *testing.T) {
defer resp.Body.Close()
body, _ = ioutil.ReadAll(resp.Body)
test.Equal(t, 200, resp.StatusCode)
test.Equal(t, LOG_FATAL, nsqd.getOpts().logLevel)
test.Equal(t, LOG_FATAL, nsqd.getOpts().LogLevel)

url = fmt.Sprintf("http://%s/config/log_level", httpAddr)
req, err = http.NewRequest("PUT", url, bytes.NewBuffer([]byte(`bad`)))
Expand All @@ -643,25 +643,6 @@ func TestHTTPconfig(t *testing.T) {
defer resp.Body.Close()
body, _ = ioutil.ReadAll(resp.Body)
test.Equal(t, 400, resp.StatusCode)

url = fmt.Sprintf("http://%s/config/verbose", httpAddr)
req, err = http.NewRequest("PUT", url, bytes.NewBuffer([]byte(`true`)))
test.Nil(t, err)
resp, err = client.Do(req)
test.Nil(t, err)
defer resp.Body.Close()
body, _ = ioutil.ReadAll(resp.Body)
test.Equal(t, 200, resp.StatusCode)
test.Equal(t, true, nsqd.getOpts().Verbose)

url = fmt.Sprintf("http://%s/config/verbose", httpAddr)
req, err = http.NewRequest("PUT", url, bytes.NewBuffer([]byte(`bad`)))
test.Nil(t, err)
resp, err = client.Do(req)
test.Nil(t, err)
defer resp.Body.Close()
body, _ = ioutil.ReadAll(resp.Body)
test.Equal(t, 400, resp.StatusCode)
}

func TestHTTPerrors(t *testing.T) {
Expand Down
37 changes: 14 additions & 23 deletions nsqd/nsqd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"io/ioutil"
"net"
"os"
"os/exec"
"strconv"
"sync/atomic"
"testing"
Expand Down Expand Up @@ -244,8 +243,16 @@ func TestPauseMetadata(t *testing.T) {
func mustStartNSQLookupd(opts *nsqlookupd.Options) (*net.TCPAddr, *net.TCPAddr, *nsqlookupd.NSQLookupd) {
opts.TCPAddress = "127.0.0.1:0"
opts.HTTPAddress = "127.0.0.1:0"
lookupd := nsqlookupd.New(opts)
lookupd.Main()
lookupd, err := nsqlookupd.New(opts)
if err != nil {
panic(err)
}
go func() {
err := lookupd.Main()
if err != nil {
panic(err)
}
}()
return lookupd.RealTCPAddr(), lookupd.RealHTTPAddr(), lookupd
}

Expand Down Expand Up @@ -411,14 +418,15 @@ func TestCluster(t *testing.T) {
func TestSetHealth(t *testing.T) {
opts := NewOptions()
opts.Logger = test.NewTestLogger(t)
nsqd := New(opts)
nsqd, err := New(opts)
test.Nil(t, err)
defer nsqd.Exit()

test.Equal(t, nil, nsqd.GetError())
test.Nil(t, nsqd.GetError())
test.Equal(t, true, nsqd.IsHealthy())

nsqd.SetHealth(nil)
test.Equal(t, nil, nsqd.GetError())
test.Nil(t, nsqd.GetError())
test.Equal(t, true, nsqd.IsHealthy())

nsqd.SetHealth(errors.New("health error"))
Expand All @@ -431,20 +439,3 @@ func TestSetHealth(t *testing.T) {
test.Equal(t, "OK", nsqd.GetHealth())
test.Equal(t, true, nsqd.IsHealthy())
}

func TestCrashingLogger(t *testing.T) {
if os.Getenv("BE_CRASHER") == "1" {
// Test invalid log level causes error
opts := NewOptions()
opts.LogLevel = "bad"
_ = New(opts)
return
}
cmd := exec.Command(os.Args[0], "-test.run=TestCrashingLogger")
cmd.Env = append(os.Environ(), "BE_CRASHER=1")
err := cmd.Run()
if e, ok := err.(*exec.ExitError); ok && !e.Success() {
return
}
t.Fatalf("process ran with err %v, want exit status 1", err)
}
Loading

0 comments on commit 9d332eb

Please sign in to comment.