Skip to content

Commit

Permalink
add new and reorganize test
Browse files Browse the repository at this point in the history
  • Loading branch information
jandelgado committed Mar 1, 2019
1 parent 3106d38 commit f3b0df4
Show file tree
Hide file tree
Showing 9 changed files with 291 additions and 168 deletions.
60 changes: 3 additions & 57 deletions cmd/rabtap/broker_info_printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,60 +76,6 @@ func NewBrokerInfoPrinter(config BrokerInfoPrinterConfig) *BrokerInfoPrinter {
return &s
}

// findQueueByName searches in the queues array for a queue with the given
// name and vhost. RabbitQueue element is returned on succes, otherwise nil.
func findQueueByName(queues []rabtap.RabbitQueue,
vhost, queueName string) *rabtap.RabbitQueue {
for _, queue := range queues {
if queue.Name == queueName && queue.Vhost == vhost {
return &queue
}
}
return nil
}

func findExchangeByName(exchanges []rabtap.RabbitExchange,
vhost, exchangeName string) *rabtap.RabbitExchange {
for _, exchange := range exchanges {
if exchange.Name == exchangeName && exchange.Vhost == vhost {
return &exchange
}
}
return nil
}

// currently not used.
// func findChannelByName(channels []rabtap.RabbitChannel,
// vhost, channelName string) *rabtap.RabbitChannel {
// for _, channel := range channels {
// if channel.Name == channelName && channel.Vhost == vhost {
// return &channel
// }
// }
// return nil
// }

func findConnectionByName(conns []rabtap.RabbitConnection,
vhost, connName string) *rabtap.RabbitConnection {
for _, conn := range conns {
if conn.Name == connName && conn.Vhost == vhost {
return &conn
}
}
return nil
}

func findConsumerByQueue(consumers []rabtap.RabbitConsumer,
vhost, queueName string) *rabtap.RabbitConsumer {
for _, consumer := range consumers {
if consumer.Queue.Vhost == vhost &&
consumer.Queue.Name == queueName {
return &consumer
}
}
return nil
}

// uniqueVhosts returns the set of unique vhosts in the array of exchanges
func uniqueVhosts(exchanges []rabtap.RabbitExchange) (vhosts map[string]bool) {
vhosts = make(map[string]bool)
Expand Down Expand Up @@ -259,7 +205,7 @@ func (s BrokerInfoPrinter) createConsumerNodes(
func (s BrokerInfoPrinter) createConnectionNodes(
vhost string, connName string, brokerInfo *rabtap.BrokerInfo) []*TreeNode {
var conns []*TreeNode
connInfo := findConnectionByName(brokerInfo.Connections, vhost, connName)
connInfo := rabtap.FindConnectionByName(brokerInfo.Connections, vhost, connName)
if connInfo != nil {
conns = append(conns, NewTreeNode(s.renderConnectionElementAsString(connInfo)))
}
Expand Down Expand Up @@ -289,7 +235,7 @@ func (s BrokerInfoPrinter) createQueueNodeFromBinding(
brokerInfo *rabtap.BrokerInfo) []*TreeNode {

// standard binding of queue to exchange
queue := findQueueByName(brokerInfo.Queues,
queue := rabtap.FindQueueByName(brokerInfo.Queues,
binding.Vhost,
binding.Destination)
if queue == nil {
Expand Down Expand Up @@ -327,7 +273,7 @@ func (s BrokerInfoPrinter) createExchangeNode(
// exchange to exchange binding
exchangeNode.Add(
s.createExchangeNode(
findExchangeByName(
rabtap.FindExchangeByName(
brokerInfo.Exchanges,
binding.Vhost,
binding.Destination),
Expand Down
87 changes: 11 additions & 76 deletions cmd/rabtap/broker_info_printer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,60 +12,6 @@ import (
"github.com/stretchr/testify/assert"
)

func TestResolveTemplate(t *testing.T) {
type Info struct {
Name string
}
args := Info{"Jan"}

const tpl = "hello {{ .Name }}"

brokerInfoPrinter := NewBrokerInfoPrinter(
BrokerInfoPrinterConfig{
NoColor: true},
)

result := brokerInfoPrinter.resolveTemplate("test", tpl, args)
assert.Equal(t, "hello Jan", result)
}

func TestFindExchangeByName(t *testing.T) {
exchanges := []rabtap.RabbitExchange{
{Name: "exchange1", Vhost: "vhost"},
{Name: "exchange2", Vhost: "vhost"},
}
exchange := findExchangeByName(exchanges, "vhost", "exchange2")
assert.NotNil(t, exchange)
assert.Equal(t, "exchange2", exchange.Name)
}

func TestFindExchangeByNameNotFound(t *testing.T) {
exchanges := []rabtap.RabbitExchange{
{Name: "exchange1", Vhost: "vhost"},
}
exchange := findExchangeByName(exchanges, "/", "not-available")
assert.Nil(t, exchange)
}

func TestFindQueueByName(t *testing.T) {
queues := []rabtap.RabbitQueue{
{Name: "q1", Vhost: "vhost"},
{Name: "q2", Vhost: "vhost"},
}
queue := findQueueByName(queues, "vhost", "q2")
assert.Equal(t, "q2", queue.Name)
assert.Equal(t, "vhost", queue.Vhost)
}

func TestFindQueueByNameNotFound(t *testing.T) {
queues := []rabtap.RabbitQueue{
{Name: "q1", Vhost: "vhost"},
{Name: "q2", Vhost: "vhost"},
}
queue := findQueueByName(queues, "/", "not-available")
assert.Nil(t, queue)
}

func TestFilterStringListOfEmptyLists(t *testing.T) {
flags := []bool{}
strs := []string{}
Expand All @@ -78,32 +24,21 @@ func TestFilterStringListOneElementKeptInList(t *testing.T) {
assert.Equal(t, []string{"B"}, filterStringList(flags, strs))
}

func TestFindConnectionByName(t *testing.T) {
conns := []rabtap.RabbitConnection{
{Name: "c1", Vhost: "vhost"},
{Name: "c2", Vhost: "vhost"},
func TestResolveTemplate(t *testing.T) {
type Info struct {
Name string
}
conn := findConnectionByName(conns, "vhost", "c2")
assert.Equal(t, "c2", conn.Name)
assert.Equal(t, "vhost", conn.Vhost)
}
args := Info{"Jan"}

func TestFindConnectionByNameNotFoundReturnsNil(t *testing.T) {
assert.Nil(t, findConnectionByName([]rabtap.RabbitConnection{}, "vhost", "c2"))
}
const tpl = "hello {{ .Name }}"

func TestFindConsumerByQueue(t *testing.T) {
con := rabtap.RabbitConsumer{}
con.Queue.Name = "q1"
con.Queue.Vhost = "vhost"
cons := []rabtap.RabbitConsumer{con}
foundCon := findConsumerByQueue(cons, "vhost", "q1")
assert.Equal(t, "q1", foundCon.Queue.Name)
assert.Equal(t, "vhost", foundCon.Queue.Vhost)
}
brokerInfoPrinter := NewBrokerInfoPrinter(
BrokerInfoPrinterConfig{
NoColor: true},
)

func TestFindConsumerByQueueNotFoundReturnsNil(t *testing.T) {
assert.Nil(t, findConsumerByQueue([]rabtap.RabbitConsumer{}, "vhost", "q1"))
result := brokerInfoPrinter.resolveTemplate("test", tpl, args)
assert.Equal(t, "hello Jan", result)
}

func TestBrokerInfoPrintFailsOnInvalidUri(t *testing.T) {
Expand Down
28 changes: 10 additions & 18 deletions cmd/rabtap/cmd_info_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,25 @@
package main

import (
"bytes"
"crypto/tls"
"strings"
"os"
"regexp"
"testing"

"github.com/jandelgado/rabtap/pkg"
"github.com/jandelgado/rabtap/pkg/testcommon"
"github.com/stretchr/testify/assert"
)

func TestCmdInfoRootNodeOnly(t *testing.T) {

// REST api mock returning "empty" broker
apiMock := testcommon.NewRabbitAPIMock(testcommon.MockModeEmpty)
client := rabtap.NewRabbitHTTPClient(apiMock.URL, &tls.Config{})

printConfig := BrokerInfoPrinterConfig{
ShowStats: false,
ShowConsumers: false,
ShowDefaultExchange: false,
NoColor: true}
oldArgs := os.Args
defer func() { os.Args = oldArgs }()

buf := bytes.NewBufferString("")
cmdInfo(CmdInfoArg{
rootNode: "http://x:y@rootnode",
client: client,
printConfig: printConfig,
out: buf})
assert.Equal(t, "http://rootnode (broker ver='3.6.9', mgmt ver='3.6.9', cluster='rabbit@08f57d1fe8ab')",
strings.TrimSpace(buf.String()))
os.Args = []string{"rabtap", "info",
"--api", apiMock.URL,
"--no-color"}
out := testcommon.CaptureOutput(main)
assert.Regexp(t, regexp.MustCompile("http://(.*) \\(broker ver='3.6.9', mgmt ver='3.6.9', cluster='rabbit@08f57d1fe8ab'\\)"), out)
}
56 changes: 39 additions & 17 deletions cmd/rabtap/cmd_publish_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
package main

import (
"crypto/tls"
"strings"
"io/ioutil"
"os"
"testing"
"time"

Expand All @@ -18,6 +18,13 @@ import (

func TestCmdPublishRaw(t *testing.T) {

tmpfile, err := ioutil.TempFile("", "rabtap")
require.Nil(t, err)
defer os.Remove(tmpfile.Name())

_, err = tmpfile.Write([]byte("hello"))
require.Nil(t, err)

conn, ch := testcommon.IntegrationTestConnection(t, "exchange", "topic", 1, false)
defer conn.Close()

Expand All @@ -35,13 +42,15 @@ func TestCmdPublishRaw(t *testing.T) {
)
require.Nil(t, err)

reader := strings.NewReader("hello")
cmdPublish(CmdPublishArg{
amqpURI: testcommon.IntegrationURIFromEnv(),
exchange: "exchange",
routingKey: routingKey,
tlsConfig: &tls.Config{},
readNextMessageFunc: createMessageReaderFunc(false, reader)})
// execution: run publish command through call of main(), the actual
// message is in tmpfile.Name()
os.Args = []string{"rabtap", "pub",
"--uri", testcommon.IntegrationURIFromEnv(),
"exchange",
tmpfile.Name(),
"--routingkey", routingKey}

main()

select {
case message := <-deliveries:
Expand Down Expand Up @@ -82,6 +91,14 @@ func TestCmdPublishJSON(t *testing.T) {
{
"Body": "c2Vjb25kCg=="
}`

tmpfile, err := ioutil.TempFile("", "rabtap")
require.Nil(t, err)
defer os.Remove(tmpfile.Name())

_, err = tmpfile.Write([]byte(testmessage))
require.Nil(t, err)

conn, ch := testcommon.IntegrationTestConnection(t, "exchange", "topic", 1, false)
defer conn.Close()

Expand All @@ -99,15 +116,20 @@ func TestCmdPublishJSON(t *testing.T) {
)
require.Nil(t, err)

reader := strings.NewReader(testmessage)
cmdPublish(CmdPublishArg{
amqpURI: testcommon.IntegrationURIFromEnv(),
exchange: "exchange",
routingKey: routingKey,
tlsConfig: &tls.Config{},
readNextMessageFunc: createMessageReaderFunc(true, reader)})
oldArgs := os.Args
defer func() { os.Args = oldArgs }()

// execution: run publish command through call of main(), the actual
// message is in tmpfile.Name()
os.Args = []string{"rabtap", "pub",
"--uri", testcommon.IntegrationURIFromEnv(),
"exchange",
tmpfile.Name(),
"--routingkey", routingKey,
"--json"}
main()

// we expect 2 messages to be sent
// verification: we expect 2 messages to be sent by above call
var message [2]amqp.Delivery
for i := 0; i < 2; i++ {
select {
Expand Down
63 changes: 63 additions & 0 deletions cmd/rabtap/cmd_queue_integration_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// Copyright (C) 2017 Jan Delgado

// +build integration

package main

import (
"crypto/tls"
"os"
"testing"
"time"

rabtap "github.com/jandelgado/rabtap/pkg"
"github.com/jandelgado/rabtap/pkg/testcommon"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestCmdPurgeQueue(t *testing.T) {

// create a queue, publish some messages, purge queue and make
// sure queue is empty
oldArgs := os.Args
defer func() { os.Args = oldArgs }()

const testMessage = "SubHello"
const testQueue = "purge-queue-test"
const testKey = testQueue
const testExchange = "amq.direct"

amqpURI := testcommon.IntegrationURIFromEnv()
apiURI := testcommon.IntegrationAPIURIFromEnv()

os.Args = []string{"rabtap", "queue",
"create", testQueue,
"--uri", amqpURI}
main()

os.Args = []string{"rabtap", "queue",
"bind", testQueue,
"to", testExchange,
"--bindingkey", testQueue,
"--uri", amqpURI}
main()

// TODO publish messages

// purge queue and check size
os.Args = []string{"rabtap", "queue",
"purge", testQueue,
"--uri", amqpURI}
main()

time.Sleep(2 * time.Second)
client := rabtap.NewRabbitHTTPClient(apiURI, &tls.Config{})
queues, err := client.Queues()
assert.Nil(t, err)
queue := rabtap.FindQueueByName(queues, "/", testQueue)
require.NotNil(t, queue)

// TODO
assert.Equal(t, 0, queue.Messages)
}

0 comments on commit f3b0df4

Please sign in to comment.