Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add test against dnsdist #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@ go:
- 1.x # latest version
- 1.12 # lowest supported version
script:
- ./test.sh
# -short skips testing against dnsdist
# TODO: install dnsdist
- ./test.sh -v -short
97 changes: 97 additions & 0 deletions dnsdist/client_dnsdist_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package dnsdist

import (
"bytes"
"fmt"
"io/ioutil"
"math/rand"
"os"
"os/exec"
"strings"
"testing"
"time"
)

func randomAddress() string {
return fmt.Sprintf("127.0.0.1:%d", 30000+rand.Intn(20000))

}

const secret = "FUih6Cj9mNbrHWavhyhUrUhsuWK0pXSXc/d10WeV3h4="

func TestConn_real(t *testing.T) {
if testing.Short() {
t.Skip("skipping test against dnsdist in short mode")
}

sockAddr := randomAddress()

conf, err := ioutil.TempFile("", "dnsdist.conf-")
if err != nil {
t.Fatal(err)
}
defer conf.Close()

var buf bytes.Buffer
_, _ = buf.WriteString(fmt.Sprintf("controlSocket(%q)\n", sockAddr))
_, _ = buf.WriteString(fmt.Sprintf("setKey(%q)\n", secret))
if _, err := conf.Write(buf.Bytes()); err != nil {
t.Fatal(err)
}

dnsdistCommand := os.Getenv("TEST_DNSDIST_COMMAND")
if dnsdistCommand == "" {
dnsdistCommand = "dnsdist"
}
args := []string{
"--local=" + randomAddress(),
"--config=" + conf.Name(),
"--supervised",
"--disable-syslog",
}
t.Logf("dnsdistCommand = %q", dnsdistCommand)
t.Logf("args = %v", args)

var outb, errb bytes.Buffer
cmd := exec.Command(dnsdistCommand, args...)
cmd.Stdout = &outb
cmd.Stderr = &errb
if err := cmd.Start(); err != nil {
t.Fatalf("Could not start command: %v", err)
}
defer func() {
_ = cmd.Process.Kill()
}()
time.Sleep(time.Second)

// Not entirely safe during command execution
t.Log("stdout:\n", outb.String())
t.Log("stderr:\n", errb.String())

// Actual test
conn, err := Dial(sockAddr, secret)
if err != nil {
t.Fatal(err)
}
defer conn.Close()

// Try a command
resp, err := conn.Command("showVersion()")
if err != nil {
t.Fatal(err)
}
t.Logf("showVersion -> %s", resp)
if !strings.HasPrefix(resp, "dnsdist") {
t.Errorf("Unexpected version string returned: %q", resp)
}

// Lua errors do not result in a Go error
resp, err = conn.Command("error('some error')")
if err != nil {
t.Fatal(err)
}
t.Logf("error -> %s", resp)
if !strings.Contains(resp, "some error") {
t.Errorf("Unexpected error string returned: %q", resp)
}
}
4 changes: 3 additions & 1 deletion test.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#!/bin/sh

echo "Use -short to skip testing against dnsdist"

set -ex
go test ./...
go test ./... "$@"
go run github.com/golangci/golangci-lint/cmd/golangci-lint run