nsqadmin_test.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. package nsqadmin
  2. import (
  3. "fmt"
  4. "io/ioutil"
  5. "net"
  6. "net/http"
  7. "net/url"
  8. "os"
  9. "testing"
  10. "github.com/nsqio/nsq/internal/lg"
  11. "github.com/nsqio/nsq/internal/test"
  12. "github.com/nsqio/nsq/nsqd"
  13. )
  14. func TestNeitherNSQDAndNSQLookup(t *testing.T) {
  15. opts := NewOptions()
  16. opts.Logger = lg.NilLogger{}
  17. opts.HTTPAddress = "127.0.0.1:0"
  18. _, err := New(opts)
  19. test.NotNil(t, err)
  20. test.Equal(t, "--nsqd-http-address or --lookupd-http-address required", fmt.Sprintf("%s", err))
  21. }
  22. func TestBothNSQDAndNSQLookup(t *testing.T) {
  23. opts := NewOptions()
  24. opts.Logger = lg.NilLogger{}
  25. opts.HTTPAddress = "127.0.0.1:0"
  26. opts.NSQLookupdHTTPAddresses = []string{"127.0.0.1:4161"}
  27. opts.NSQDHTTPAddresses = []string{"127.0.0.1:4151"}
  28. _, err := New(opts)
  29. test.NotNil(t, err)
  30. test.Equal(t, "use --nsqd-http-address or --lookupd-http-address not both", fmt.Sprintf("%s", err))
  31. }
  32. func TestTLSHTTPClient(t *testing.T) {
  33. lgr := test.NewTestLogger(t)
  34. nsqdOpts := nsqd.NewOptions()
  35. nsqdOpts.TLSCert = "./test/server.pem"
  36. nsqdOpts.TLSKey = "./test/server.key"
  37. nsqdOpts.TLSRootCAFile = "./test/ca.pem"
  38. nsqdOpts.TLSClientAuthPolicy = "require-verify"
  39. nsqdOpts.Logger = lgr
  40. _, nsqdHTTPAddr, nsqd := mustStartNSQD(nsqdOpts)
  41. defer os.RemoveAll(nsqdOpts.DataPath)
  42. defer nsqd.Exit()
  43. opts := NewOptions()
  44. opts.HTTPAddress = "127.0.0.1:0"
  45. opts.NSQDHTTPAddresses = []string{nsqdHTTPAddr.String()}
  46. opts.HTTPClientTLSRootCAFile = "./test/ca.pem"
  47. opts.HTTPClientTLSCert = "./test/client.pem"
  48. opts.HTTPClientTLSKey = "./test/client.key"
  49. opts.Logger = lgr
  50. nsqadmin, err := New(opts)
  51. test.Nil(t, err)
  52. go func() {
  53. err := nsqadmin.Main()
  54. if err != nil {
  55. panic(err)
  56. }
  57. }()
  58. defer nsqadmin.Exit()
  59. httpAddr := nsqadmin.RealHTTPAddr()
  60. u := url.URL{
  61. Scheme: "http",
  62. Host: httpAddr.String(),
  63. Path: "/api/nodes/" + nsqdHTTPAddr.String(),
  64. }
  65. resp, err := http.Get(u.String())
  66. test.Nil(t, err)
  67. defer resp.Body.Close()
  68. test.Equal(t, resp.StatusCode < 500, true)
  69. }
  70. func mustStartNSQD(opts *nsqd.Options) (*net.TCPAddr, *net.TCPAddr, *nsqd.NSQD) {
  71. opts.TCPAddress = "127.0.0.1:0"
  72. opts.HTTPAddress = "127.0.0.1:0"
  73. opts.HTTPSAddress = "127.0.0.1:0"
  74. if opts.DataPath == "" {
  75. tmpDir, err := ioutil.TempDir("", "nsq-test-")
  76. if err != nil {
  77. panic(err)
  78. }
  79. opts.DataPath = tmpDir
  80. }
  81. nsqd, err := nsqd.New(opts)
  82. if err != nil {
  83. panic(err)
  84. }
  85. go func() {
  86. err := nsqd.Main()
  87. if err != nil {
  88. panic(err)
  89. }
  90. }()
  91. return nsqd.RealTCPAddr(), nsqd.RealHTTPAddr(), nsqd
  92. }