123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484 |
- package nsqlookupd
- import (
- "encoding/json"
- "fmt"
- "io/ioutil"
- "net/http"
- "os"
- "strconv"
- "testing"
- "time"
- "github.com/nsqio/nsq/internal/test"
- "github.com/nsqio/nsq/internal/version"
- "github.com/nsqio/nsq/nsqd"
- )
- type InfoDoc struct {
- Version string `json:"version"`
- }
- type ChannelsDoc struct {
- Channels []interface{} `json:"channels"`
- }
- type ErrMessage struct {
- Message string `json:"message"`
- }
- func bootstrapNSQCluster(t *testing.T) (string, []*nsqd.NSQD, *NSQLookupd) {
- lgr := test.NewTestLogger(t)
- nsqlookupdOpts := NewOptions()
- nsqlookupdOpts.TCPAddress = "127.0.0.1:0"
- nsqlookupdOpts.HTTPAddress = "127.0.0.1:0"
- nsqlookupdOpts.BroadcastAddress = "127.0.0.1"
- nsqlookupdOpts.Logger = lgr
- nsqlookupd1, err := New(nsqlookupdOpts)
- if err != nil {
- panic(err)
- }
- go func() {
- err := nsqlookupd1.Main()
- if err != nil {
- panic(err)
- }
- }()
- time.Sleep(100 * time.Millisecond)
- nsqdOpts := nsqd.NewOptions()
- nsqdOpts.TCPAddress = "127.0.0.1:0"
- nsqdOpts.HTTPAddress = "127.0.0.1:0"
- nsqdOpts.BroadcastAddress = "127.0.0.1"
- nsqdOpts.NSQLookupdTCPAddresses = []string{nsqlookupd1.RealTCPAddr().String()}
- nsqdOpts.Logger = lgr
- tmpDir, err := ioutil.TempDir("", fmt.Sprintf("nsq-test-%d", time.Now().UnixNano()))
- if err != nil {
- panic(err)
- }
- nsqdOpts.DataPath = tmpDir
- nsqd1, err := nsqd.New(nsqdOpts)
- if err != nil {
- panic(err)
- }
- go func() {
- err := nsqd1.Main()
- if err != nil {
- panic(err)
- }
- }()
- time.Sleep(100 * time.Millisecond)
- return tmpDir, []*nsqd.NSQD{nsqd1}, nsqlookupd1
- }
- func makeTopic(nsqlookupd *NSQLookupd, topicName string) {
- key := Registration{"topic", topicName, ""}
- nsqlookupd.DB.AddRegistration(key)
- }
- func makeChannel(nsqlookupd *NSQLookupd, topicName string, channelName string) {
- key := Registration{"channel", topicName, channelName}
- nsqlookupd.DB.AddRegistration(key)
- makeTopic(nsqlookupd, topicName)
- }
- func TestPing(t *testing.T) {
- dataPath, nsqds, nsqlookupd1 := bootstrapNSQCluster(t)
- defer os.RemoveAll(dataPath)
- defer nsqds[0].Exit()
- defer nsqlookupd1.Exit()
- client := http.Client{}
- url := fmt.Sprintf("http://%s/ping", nsqlookupd1.RealHTTPAddr())
- req, _ := http.NewRequest("GET", url, nil)
- resp, err := client.Do(req)
- test.Nil(t, err)
- test.Equal(t, 200, resp.StatusCode)
- body, _ := ioutil.ReadAll(resp.Body)
- resp.Body.Close()
- test.Equal(t, []byte("OK"), body)
- }
- func TestInfo(t *testing.T) {
- dataPath, nsqds, nsqlookupd1 := bootstrapNSQCluster(t)
- defer os.RemoveAll(dataPath)
- defer nsqds[0].Exit()
- defer nsqlookupd1.Exit()
- client := http.Client{}
- url := fmt.Sprintf("http://%s/info", nsqlookupd1.RealHTTPAddr())
- req, _ := http.NewRequest("GET", url, nil)
- resp, err := client.Do(req)
- test.Nil(t, err)
- test.Equal(t, 200, resp.StatusCode)
- body, _ := ioutil.ReadAll(resp.Body)
- resp.Body.Close()
- t.Logf("%s", body)
- info := InfoDoc{}
- err = json.Unmarshal(body, &info)
- test.Nil(t, err)
- test.Equal(t, version.Binary, info.Version)
- }
- func TestCreateTopic(t *testing.T) {
- dataPath, nsqds, nsqlookupd1 := bootstrapNSQCluster(t)
- defer os.RemoveAll(dataPath)
- defer nsqds[0].Exit()
- defer nsqlookupd1.Exit()
- em := ErrMessage{}
- client := http.Client{}
- url := fmt.Sprintf("http://%s/topic/create", nsqlookupd1.RealHTTPAddr())
- req, _ := http.NewRequest("POST", url, nil)
- resp, err := client.Do(req)
- test.Nil(t, err)
- test.Equal(t, 400, resp.StatusCode)
- test.Equal(t, "Bad Request", http.StatusText(resp.StatusCode))
- body, _ := ioutil.ReadAll(resp.Body)
- resp.Body.Close()
- t.Logf("%s", body)
- err = json.Unmarshal(body, &em)
- test.Nil(t, err)
- test.Equal(t, "MISSING_ARG_TOPIC", em.Message)
- topicName := "sampletopicA" + strconv.Itoa(int(time.Now().Unix())) + "$"
- url = fmt.Sprintf("http://%s/topic/create?topic=%s", nsqlookupd1.RealHTTPAddr(), topicName)
- req, _ = http.NewRequest("POST", url, nil)
- resp, err = client.Do(req)
- test.Nil(t, err)
- test.Equal(t, 400, resp.StatusCode)
- test.Equal(t, "Bad Request", http.StatusText(resp.StatusCode))
- body, _ = ioutil.ReadAll(resp.Body)
- resp.Body.Close()
- t.Logf("%s", body)
- err = json.Unmarshal(body, &em)
- test.Nil(t, err)
- test.Equal(t, "INVALID_ARG_TOPIC", em.Message)
- topicName = "sampletopicA" + strconv.Itoa(int(time.Now().Unix()))
- url = fmt.Sprintf("http://%s/topic/create?topic=%s", nsqlookupd1.RealHTTPAddr(), topicName)
- req, _ = http.NewRequest("POST", url, nil)
- resp, err = client.Do(req)
- test.Nil(t, err)
- test.Equal(t, 200, resp.StatusCode)
- body, _ = ioutil.ReadAll(resp.Body)
- resp.Body.Close()
- t.Logf("%s", body)
- test.Equal(t, []byte(""), body)
- }
- func TestDeleteTopic(t *testing.T) {
- dataPath, nsqds, nsqlookupd1 := bootstrapNSQCluster(t)
- defer os.RemoveAll(dataPath)
- defer nsqds[0].Exit()
- defer nsqlookupd1.Exit()
- em := ErrMessage{}
- client := http.Client{}
- url := fmt.Sprintf("http://%s/topic/delete", nsqlookupd1.RealHTTPAddr())
- req, _ := http.NewRequest("POST", url, nil)
- resp, err := client.Do(req)
- test.Nil(t, err)
- test.Equal(t, 400, resp.StatusCode)
- test.Equal(t, "Bad Request", http.StatusText(resp.StatusCode))
- body, _ := ioutil.ReadAll(resp.Body)
- resp.Body.Close()
- t.Logf("%s", body)
- err = json.Unmarshal(body, &em)
- test.Nil(t, err)
- test.Equal(t, "MISSING_ARG_TOPIC", em.Message)
- topicName := "sampletopicA" + strconv.Itoa(int(time.Now().Unix()))
- makeTopic(nsqlookupd1, topicName)
- url = fmt.Sprintf("http://%s/topic/delete?topic=%s", nsqlookupd1.RealHTTPAddr(), topicName)
- req, _ = http.NewRequest("POST", url, nil)
- resp, err = client.Do(req)
- test.Nil(t, err)
- test.Equal(t, 200, resp.StatusCode)
- body, _ = ioutil.ReadAll(resp.Body)
- resp.Body.Close()
- t.Logf("%s", body)
- test.Equal(t, []byte(""), body)
- topicName = "sampletopicB" + strconv.Itoa(int(time.Now().Unix()))
- channelName := "foobar" + strconv.Itoa(int(time.Now().Unix()))
- makeChannel(nsqlookupd1, topicName, channelName)
- url = fmt.Sprintf("http://%s/topic/delete?topic=%s", nsqlookupd1.RealHTTPAddr(), topicName)
- req, _ = http.NewRequest("POST", url, nil)
- resp, err = client.Do(req)
- test.Nil(t, err)
- test.Equal(t, 200, resp.StatusCode)
- body, _ = ioutil.ReadAll(resp.Body)
- resp.Body.Close()
- t.Logf("%s", body)
- test.Equal(t, []byte(""), body)
- }
- func TestGetChannels(t *testing.T) {
- dataPath, nsqds, nsqlookupd1 := bootstrapNSQCluster(t)
- defer os.RemoveAll(dataPath)
- defer nsqds[0].Exit()
- defer nsqlookupd1.Exit()
- client := http.Client{}
- url := fmt.Sprintf("http://%s/channels", nsqlookupd1.RealHTTPAddr())
- em := ErrMessage{}
- req, _ := http.NewRequest("GET", url, nil)
- req.Header.Add("Accept", "application/vnd.nsq; version=1.0")
- resp, err := client.Do(req)
- test.Nil(t, err)
- test.Equal(t, 400, resp.StatusCode)
- test.Equal(t, "Bad Request", http.StatusText(resp.StatusCode))
- body, _ := ioutil.ReadAll(resp.Body)
- resp.Body.Close()
- t.Logf("%s", body)
- err = json.Unmarshal(body, &em)
- test.Nil(t, err)
- test.Equal(t, "MISSING_ARG_TOPIC", em.Message)
- ch := ChannelsDoc{}
- topicName := "sampletopicA" + strconv.Itoa(int(time.Now().Unix()))
- makeTopic(nsqlookupd1, topicName)
- url = fmt.Sprintf("http://%s/channels?topic=%s", nsqlookupd1.RealHTTPAddr(), topicName)
- req, _ = http.NewRequest("GET", url, nil)
- req.Header.Add("Accept", "application/vnd.nsq; version=1.0")
- resp, err = client.Do(req)
- test.Nil(t, err)
- test.Equal(t, 200, resp.StatusCode)
- body, _ = ioutil.ReadAll(resp.Body)
- resp.Body.Close()
- t.Logf("%s", body)
- err = json.Unmarshal(body, &ch)
- test.Nil(t, err)
- test.Equal(t, 0, len(ch.Channels))
- topicName = "sampletopicB" + strconv.Itoa(int(time.Now().Unix()))
- channelName := "foobar" + strconv.Itoa(int(time.Now().Unix()))
- makeChannel(nsqlookupd1, topicName, channelName)
- url = fmt.Sprintf("http://%s/channels?topic=%s", nsqlookupd1.RealHTTPAddr(), topicName)
- req, _ = http.NewRequest("GET", url, nil)
- req.Header.Add("Accept", "application/vnd.nsq; version=1.0")
- resp, err = client.Do(req)
- test.Nil(t, err)
- test.Equal(t, 200, resp.StatusCode)
- body, _ = ioutil.ReadAll(resp.Body)
- resp.Body.Close()
- t.Logf("%s", body)
- err = json.Unmarshal(body, &ch)
- test.Nil(t, err)
- test.Equal(t, 1, len(ch.Channels))
- test.Equal(t, channelName, ch.Channels[0])
- }
- func TestCreateChannel(t *testing.T) {
- dataPath, nsqds, nsqlookupd1 := bootstrapNSQCluster(t)
- defer os.RemoveAll(dataPath)
- defer nsqds[0].Exit()
- defer nsqlookupd1.Exit()
- em := ErrMessage{}
- client := http.Client{}
- url := fmt.Sprintf("http://%s/channel/create", nsqlookupd1.RealHTTPAddr())
- req, _ := http.NewRequest("POST", url, nil)
- resp, err := client.Do(req)
- test.Nil(t, err)
- test.Equal(t, 400, resp.StatusCode)
- test.Equal(t, "Bad Request", http.StatusText(resp.StatusCode))
- body, _ := ioutil.ReadAll(resp.Body)
- resp.Body.Close()
- t.Logf("%s", body)
- err = json.Unmarshal(body, &em)
- test.Nil(t, err)
- test.Equal(t, "MISSING_ARG_TOPIC", em.Message)
- topicName := "sampletopicB" + strconv.Itoa(int(time.Now().Unix())) + "$"
- url = fmt.Sprintf("http://%s/channel/create?topic=%s", nsqlookupd1.RealHTTPAddr(), topicName)
- req, _ = http.NewRequest("POST", url, nil)
- resp, err = client.Do(req)
- test.Nil(t, err)
- test.Equal(t, 400, resp.StatusCode)
- test.Equal(t, "Bad Request", http.StatusText(resp.StatusCode))
- body, _ = ioutil.ReadAll(resp.Body)
- resp.Body.Close()
- t.Logf("%s", body)
- err = json.Unmarshal(body, &em)
- test.Nil(t, err)
- test.Equal(t, "INVALID_ARG_TOPIC", em.Message)
- topicName = "sampletopicB" + strconv.Itoa(int(time.Now().Unix()))
- url = fmt.Sprintf("http://%s/channel/create?topic=%s", nsqlookupd1.RealHTTPAddr(), topicName)
- req, _ = http.NewRequest("POST", url, nil)
- resp, err = client.Do(req)
- test.Nil(t, err)
- test.Equal(t, 400, resp.StatusCode)
- test.Equal(t, "Bad Request", http.StatusText(resp.StatusCode))
- body, _ = ioutil.ReadAll(resp.Body)
- resp.Body.Close()
- t.Logf("%s", body)
- err = json.Unmarshal(body, &em)
- test.Nil(t, err)
- test.Equal(t, "MISSING_ARG_CHANNEL", em.Message)
- channelName := "foobar" + strconv.Itoa(int(time.Now().Unix())) + "$"
- url = fmt.Sprintf("http://%s/channel/create?topic=%s&channel=%s", nsqlookupd1.RealHTTPAddr(), topicName, channelName)
- req, _ = http.NewRequest("POST", url, nil)
- resp, err = client.Do(req)
- test.Nil(t, err)
- test.Equal(t, 400, resp.StatusCode)
- test.Equal(t, "Bad Request", http.StatusText(resp.StatusCode))
- body, _ = ioutil.ReadAll(resp.Body)
- resp.Body.Close()
- t.Logf("%s", body)
- err = json.Unmarshal(body, &em)
- test.Nil(t, err)
- test.Equal(t, "INVALID_ARG_CHANNEL", em.Message)
- channelName = "foobar" + strconv.Itoa(int(time.Now().Unix()))
- url = fmt.Sprintf("http://%s/channel/create?topic=%s&channel=%s", nsqlookupd1.RealHTTPAddr(), topicName, channelName)
- req, _ = http.NewRequest("POST", url, nil)
- resp, err = client.Do(req)
- test.Nil(t, err)
- test.Equal(t, 200, resp.StatusCode)
- body, _ = ioutil.ReadAll(resp.Body)
- resp.Body.Close()
- t.Logf("%s", body)
- test.Equal(t, []byte(""), body)
- }
- func TestDeleteChannel(t *testing.T) {
- dataPath, nsqds, nsqlookupd1 := bootstrapNSQCluster(t)
- defer os.RemoveAll(dataPath)
- defer nsqds[0].Exit()
- defer nsqlookupd1.Exit()
- em := ErrMessage{}
- client := http.Client{}
- url := fmt.Sprintf("http://%s/channel/delete", nsqlookupd1.RealHTTPAddr())
- req, _ := http.NewRequest("POST", url, nil)
- resp, err := client.Do(req)
- test.Nil(t, err)
- test.Equal(t, 400, resp.StatusCode)
- test.Equal(t, "Bad Request", http.StatusText(resp.StatusCode))
- body, _ := ioutil.ReadAll(resp.Body)
- resp.Body.Close()
- t.Logf("%s", body)
- err = json.Unmarshal(body, &em)
- test.Nil(t, err)
- test.Equal(t, "MISSING_ARG_TOPIC", em.Message)
- topicName := "sampletopicB" + strconv.Itoa(int(time.Now().Unix())) + "$"
- url = fmt.Sprintf("http://%s/channel/delete?topic=%s", nsqlookupd1.RealHTTPAddr(), topicName)
- req, _ = http.NewRequest("POST", url, nil)
- resp, err = client.Do(req)
- test.Nil(t, err)
- test.Equal(t, 400, resp.StatusCode)
- test.Equal(t, "Bad Request", http.StatusText(resp.StatusCode))
- body, _ = ioutil.ReadAll(resp.Body)
- resp.Body.Close()
- t.Logf("%s", body)
- err = json.Unmarshal(body, &em)
- test.Nil(t, err)
- test.Equal(t, "INVALID_ARG_TOPIC", em.Message)
- topicName = "sampletopicB" + strconv.Itoa(int(time.Now().Unix()))
- url = fmt.Sprintf("http://%s/channel/delete?topic=%s", nsqlookupd1.RealHTTPAddr(), topicName)
- req, _ = http.NewRequest("POST", url, nil)
- resp, err = client.Do(req)
- test.Nil(t, err)
- test.Equal(t, 400, resp.StatusCode)
- test.Equal(t, "Bad Request", http.StatusText(resp.StatusCode))
- body, _ = ioutil.ReadAll(resp.Body)
- resp.Body.Close()
- t.Logf("%s", body)
- err = json.Unmarshal(body, &em)
- test.Nil(t, err)
- test.Equal(t, "MISSING_ARG_CHANNEL", em.Message)
- channelName := "foobar" + strconv.Itoa(int(time.Now().Unix())) + "$"
- url = fmt.Sprintf("http://%s/channel/delete?topic=%s&channel=%s", nsqlookupd1.RealHTTPAddr(), topicName, channelName)
- req, _ = http.NewRequest("POST", url, nil)
- resp, err = client.Do(req)
- test.Nil(t, err)
- test.Equal(t, 400, resp.StatusCode)
- test.Equal(t, "Bad Request", http.StatusText(resp.StatusCode))
- body, _ = ioutil.ReadAll(resp.Body)
- resp.Body.Close()
- t.Logf("%s", body)
- err = json.Unmarshal(body, &em)
- test.Nil(t, err)
- test.Equal(t, "INVALID_ARG_CHANNEL", em.Message)
- channelName = "foobar" + strconv.Itoa(int(time.Now().Unix()))
- url = fmt.Sprintf("http://%s/channel/delete?topic=%s&channel=%s", nsqlookupd1.RealHTTPAddr(), topicName, channelName)
- req, _ = http.NewRequest("POST", url, nil)
- resp, err = client.Do(req)
- test.Nil(t, err)
- test.Equal(t, 404, resp.StatusCode)
- test.Equal(t, "Not Found", http.StatusText(resp.StatusCode))
- body, _ = ioutil.ReadAll(resp.Body)
- resp.Body.Close()
- t.Logf("%s", body)
- err = json.Unmarshal(body, &em)
- test.Nil(t, err)
- test.Equal(t, "CHANNEL_NOT_FOUND", em.Message)
- makeChannel(nsqlookupd1, topicName, channelName)
- req, _ = http.NewRequest("POST", url, nil)
- resp, err = client.Do(req)
- test.Nil(t, err)
- test.Equal(t, 200, resp.StatusCode)
- body, _ = ioutil.ReadAll(resp.Body)
- resp.Body.Close()
- t.Logf("%s", body)
- test.Equal(t, []byte(""), body)
- }
|