Skip to content

Commit

Permalink
NR-174996 remove dependence from amqp lib for integration tests (#170)
Browse files Browse the repository at this point in the history
* NR-174996 remove dependence from amqp lib for integration tests

* update changelog.md
  • Loading branch information
rajrohanyadav authored Oct 22, 2024
1 parent 77faaa6 commit 446416a
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 37 deletions.
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ go 1.23.2

require (
github.com/newrelic/infra-integrations-sdk/v3 v3.9.1
github.com/streadway/amqp v1.0.0
github.com/stretchr/testify v1.9.0
github.com/xeipuuv/gojsonschema v1.2.0
)
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e h1:fD57ERR4JtEqsWb
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/streadway/amqp v1.0.0 h1:kuuDrUJFZL1QYL9hUNuCxNObNzB0bV/ZG5jV3RWAQgo=
github.com/streadway/amqp v1.0.0/go.mod h1:AZpEONHx3DKn8O/DFsRAY58/XVQiIPMTMB1SddzLXVw=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
Expand Down
32 changes: 32 additions & 0 deletions tests/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,43 @@ import (
"os/exec"
"path/filepath"
"strings"
"time"

"github.com/newrelic/infra-integrations-sdk/v3/log"
"github.com/xeipuuv/gojsonschema"
)

func executeCommandInContainer(containerName string, command []string) (string, string, error) {
cmdArgs := append([]string{"exec", containerName}, command...)
cmd := exec.Command("docker", cmdArgs...)
var outbuf, errbuf bytes.Buffer
cmd.Stdout = &outbuf
cmd.Stderr = &errbuf

err := cmd.Run()
if err != nil {
return "", "", err
}

stdout := outbuf.String()
stderr := errbuf.String()
return stdout, stderr, nil
}

func waitForRabbitMQIsUpAndRunning(maxTries int, containerName string) bool {
for ; maxTries > 0; maxTries-- {
time.Sleep(5 * time.Second)
fmt.Println("Trying to establish connection with RabbitMQ...")
stdout, stderr, err := executeCommandInContainer(containerName, []string{"rabbitmq-diagnostics", "check_running"})
fmt.Println(stdout)
fmt.Println(stderr)
if err == nil && strings.Contains(stdout, "fully booted and running") {
return true
}
}
return false
}

func dockerComposeRunMode(vars []string, ports []string, container string, detached bool) (string, string, error) {
cmdLine := []string{"run"}
if detached {
Expand Down
47 changes: 13 additions & 34 deletions tests/rabbitmq_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,8 @@ import (
"fmt"
"os"
"testing"
"time"

"github.com/newrelic/infra-integrations-sdk/v3/log"
"github.com/streadway/amqp"
"github.com/stretchr/testify/assert"
)

Expand All @@ -28,24 +26,6 @@ func TestMain(m *testing.M) {
}

func TestSuccessConnection(t *testing.T) {
if !waitForRabbitMQIsUpAndRunning(20) {
t.Fatal("tests cannot be executed")
}
hostname := "rabbitmq-1"
envVars := []string{
fmt.Sprintf("HOSTNAME=%s", hostname),
"USERNAME=guest",
"PASSWORD=guest",
}
response, stderr, err := dockerComposeRun(envVars, containerName)
fmt.Println(stderr)
assert.Nil(t, err)
assert.NotEmpty(t, response)
err = validateJSONSchema(schema, response)
assert.NoError(t, err, "The output of kafka integration doesn't have expected format.")
}

func waitForRabbitMQIsUpAndRunning(maxTries int) bool {
envVars := []string{}
ports := []string{"5672:5672", "15672:15672"}
stdout, stderr, err := dockerComposeRunMode(envVars, ports, containerRabbitMQ, true)
Expand All @@ -54,19 +34,18 @@ func waitForRabbitMQIsUpAndRunning(maxTries int) bool {
}
fmt.Println(stdout)
fmt.Println(stderr)
for ; maxTries > 0; maxTries-- {
log.Info("try to establish de connection with the rabbitmq...")
conn, err := amqp.Dial(connURL)
if err != nil {
log.Warn(err.Error())
time.Sleep(5 * time.Second)
continue
}
if conn != nil {
conn.Close()
log.Info("rabbitmq is up & running!")
return true
}
if !waitForRabbitMQIsUpAndRunning(20, containerRabbitMQ) {
t.Fatal("tests cannot be executed, rabbitmq is not running.")
}
return false
envVars = []string{
fmt.Sprintf("HOSTNAME=%s", containerRabbitMQ),
"USERNAME=guest",
"PASSWORD=guest",
}
response, stderr, err := dockerComposeRun(envVars, containerName)
fmt.Println(stderr)
assert.Nil(t, err)
assert.NotEmpty(t, response)
err = validateJSONSchema(schema, response)
assert.NoError(t, err, "The output of rabbitmq integration doesn't have expected format.")
}

0 comments on commit 446416a

Please sign in to comment.