mirror of
https://github.com/davidallendj/magellan.git
synced 2025-12-20 03:27:03 -07:00
Added function to wait for emulator to start
This commit is contained in:
parent
7a04afdfa8
commit
879cfac61e
1 changed files with 86 additions and 17 deletions
|
|
@ -10,12 +10,16 @@ package tests
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"net"
|
"net"
|
||||||
|
"net/http"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"testing"
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
"flag"
|
"flag"
|
||||||
|
|
||||||
magellan "github.com/OpenCHAMI/magellan/internal"
|
magellan "github.com/OpenCHAMI/magellan/internal"
|
||||||
|
"github.com/OpenCHAMI/magellan/internal/util"
|
||||||
|
"github.com/OpenCHAMI/magellan/pkg/client"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
|
@ -94,6 +98,12 @@ func TestCrawlCommand(t *testing.T) {
|
||||||
command string
|
command string
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// set up the emulator to run before test
|
||||||
|
err = waitUntilEmulatorIsReady()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to start emulator: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
// try and start the emulator in the background if arg passed
|
// try and start the emulator in the background if arg passed
|
||||||
if *emuPath != "" {
|
if *emuPath != "" {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
@ -127,7 +137,11 @@ func TestListCommand(t *testing.T) {
|
||||||
output []byte
|
output []byte
|
||||||
)
|
)
|
||||||
|
|
||||||
// set up the test
|
// set up the emulator to run before test
|
||||||
|
err = waitUntilEmulatorIsReady()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to start emulator: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
// set up temporary directory
|
// set up temporary directory
|
||||||
cmd = exec.Command("bash", "-c", fmt.Sprintf("%s list", *exePath))
|
cmd = exec.Command("bash", "-c", fmt.Sprintf("%s list", *exePath))
|
||||||
|
|
@ -151,12 +165,24 @@ func TestUpdateCommand(t *testing.T) {
|
||||||
err error
|
err error
|
||||||
output []byte
|
output []byte
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// set up the emulator to run before test
|
||||||
|
err = waitUntilEmulatorIsReady()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to start emulator: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
// set up temporary directory
|
// set up temporary directory
|
||||||
cmd = exec.Command("bash", "-c", fmt.Sprintf("%s list", *exePath))
|
cmd = exec.Command("bash", "-c", fmt.Sprintf("%s list", *exePath))
|
||||||
output, err = cmd.CombinedOutput()
|
output, err = cmd.CombinedOutput()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("failed to run 'list' command: %v", err)
|
t.Fatalf("failed to run 'list' command: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// make sure that the output is not empty
|
||||||
|
if len(output) <= 0 {
|
||||||
|
t.Fatalf("expected the 'list' output to not be empty")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestGofishFunctions(t *testing.T) {
|
func TestGofishFunctions(t *testing.T) {
|
||||||
|
|
@ -164,28 +190,71 @@ func TestGofishFunctions(t *testing.T) {
|
||||||
// gofish's output isn't changing spontaneously and remains predictable
|
// gofish's output isn't changing spontaneously and remains predictable
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TestGenerateHosts() tests creating a collection of hosts by changing arguments
|
||||||
|
// and calling GenerateHostsWithSubnet().
|
||||||
func TestGenerateHosts(t *testing.T) {
|
func TestGenerateHosts(t *testing.T) {
|
||||||
// TODO: add test to generate hosts using a collection of subnets/masks
|
// TODO: add test to generate hosts using a collection of subnets/masks
|
||||||
t.Run("generate-hosts.1", func(t *testing.T) {
|
var (
|
||||||
var (
|
subnet = "127.0.0.1"
|
||||||
subnet = "172.16.0.0"
|
subnetMask = &net.IPMask{255, 255, 255, 0}
|
||||||
subnetMask = &net.IPMask{255, 255, 255, 0}
|
ports = []int{443}
|
||||||
ports = []int{443}
|
scheme = "https"
|
||||||
scheme = "https"
|
hosts = [][]string{}
|
||||||
hosts = [][]string{}
|
)
|
||||||
)
|
t.Run("generate-hosts", func(t *testing.T) {
|
||||||
hosts = magellan.GenerateHostsWithSubnet(subnet, subnetMask, ports, scheme)
|
hosts = magellan.GenerateHostsWithSubnet(subnet, subnetMask, ports, scheme)
|
||||||
|
|
||||||
|
// check for at least one host to be generated
|
||||||
|
if len(hosts) <= 0 {
|
||||||
|
t.Fatalf("expected at least one host to be generated for subnet %s", subnet)
|
||||||
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("generate-hosts.2", func(t *testing.T) {
|
t.Run("generate-hosts-with-multiple-ports", func(t *testing.T) {
|
||||||
var (
|
ports = []int{443, 5000}
|
||||||
subnet = "127.0.0.1"
|
|
||||||
subnetMask = &net.IPMask{255, 255, 255, 0}
|
|
||||||
ports = []int{443, 5000}
|
|
||||||
scheme = "https"
|
|
||||||
hosts = [][]string{}
|
|
||||||
)
|
|
||||||
hosts = magellan.GenerateHostsWithSubnet(subnet, subnetMask, ports, scheme)
|
hosts = magellan.GenerateHostsWithSubnet(subnet, subnetMask, ports, scheme)
|
||||||
|
|
||||||
|
// check for at least one host to be generated
|
||||||
|
if len(hosts) <= 0 {
|
||||||
|
t.Fatalf("expected at least one host to be generated for subnet %s", subnet)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("generate-hosts-with-subnet-mask", func(t *testing.T) {
|
||||||
|
subnetMask = &net.IPMask{255, 255, 125, 0}
|
||||||
|
hosts = magellan.GenerateHostsWithSubnet(subnet, subnetMask, ports, scheme)
|
||||||
|
|
||||||
|
// check for at least one host to be generated
|
||||||
|
if len(hosts) <= 0 {
|
||||||
|
t.Fatalf("expected at least one host to be generated for subnet %s", subnet)
|
||||||
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// waitUntilEmulatorIsReady() polls with
|
||||||
|
func waitUntilEmulatorIsReady() error {
|
||||||
|
var (
|
||||||
|
interval = time.Second * 5
|
||||||
|
timeout = time.Second * 60
|
||||||
|
testClient = &http.Client{}
|
||||||
|
body client.HTTPBody
|
||||||
|
header client.HTTPHeader
|
||||||
|
err error
|
||||||
|
)
|
||||||
|
err = util.CheckUntil(interval, timeout, func() (bool, error) {
|
||||||
|
// send request to host until we get expected response
|
||||||
|
res, _, err := client.MakeRequest(testClient, "http://127.0.0.1", http.MethodPost, body, header)
|
||||||
|
if err != nil {
|
||||||
|
return false, fmt.Errorf("failed to start emulator: %w", err)
|
||||||
|
}
|
||||||
|
if res == nil {
|
||||||
|
return false, fmt.Errorf("response returned nil")
|
||||||
|
}
|
||||||
|
if res.StatusCode == http.StatusOK {
|
||||||
|
return true, nil
|
||||||
|
}
|
||||||
|
return false, nil
|
||||||
|
})
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue