From e8554fb2a9f9d9c4d121d4670d96fddd1b6933d4 Mon Sep 17 00:00:00 2001 From: yseto Date: Fri, 18 Jun 2021 11:28:59 +0900 Subject: [PATCH] rewrite check-memcached tests. used docker. --- check-memcached/lib/check-memcached_test.go | 78 --------------------- check-memcached/test.sh | 26 +++++++ 2 files changed, 26 insertions(+), 78 deletions(-) delete mode 100644 check-memcached/lib/check-memcached_test.go create mode 100755 check-memcached/test.sh diff --git a/check-memcached/lib/check-memcached_test.go b/check-memcached/lib/check-memcached_test.go deleted file mode 100644 index 1e5ea925..00000000 --- a/check-memcached/lib/check-memcached_test.go +++ /dev/null @@ -1,78 +0,0 @@ -package checkmemcached - -import ( - "context" - "net" - "os/exec" - "syscall" - "testing" - "time" -) - -func allocUnusedPort() (string, error) { - l, err := net.Listen("tcp", "127.0.0.1:0") - if err != nil { - return "", err - } - _, port, err := net.SplitHostPort(l.Addr().String()) - if err != nil { - return "", err - } - l.Close() - return port, nil -} - -func waitForPort(ctx context.Context, port string) error { - addr := net.JoinHostPort("127.0.0.1", port) - d, _ := ctx.Deadline() - dialer := net.Dialer{ - Deadline: d, - } - for ctx.Err() == nil { - c, err := dialer.Dial("tcp", addr) - if err != nil { - time.Sleep(100 * time.Millisecond) - continue - } - c.Close() - return nil - } - return ctx.Err() -} - -func TestMemd(t *testing.T) { - if _, err := exec.LookPath("memcached"); err != nil { - t.Skip("memcached is not installed") - } - port, err := allocUnusedPort() - if err != nil { - t.Fatal("allocUnusedPort:", err) - } - ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) - defer cancel() - cmd := exec.CommandContext(ctx, "memcached", "-p", port) - cmd.SysProcAttr = &syscall.SysProcAttr{ - Setpgid: true, - } - if err := cmd.Start(); err != nil { - t.Fatal("Start:", err) - } - if err := waitForPort(ctx, port); err != nil { - t.Fatal("waitForPort:", err) - } - - t.Logf("memcached started on port %s", port) - defer func() { - if cmd != nil && cmd.Process != nil { - cmd.Process.Signal(syscall.SIGTERM) - } - }() - - argv := []string{"-p", port, "-k", "test"} - ckr := run(argv) - if ckr.Status.String() != "OK" { - t.Errorf("failed to check memcache:%s", ckr) - } - cmd.Process.Signal(syscall.SIGTERM) - cmd.Wait() -} diff --git a/check-memcached/test.sh b/check-memcached/test.sh new file mode 100755 index 00000000..1fdea3b5 --- /dev/null +++ b/check-memcached/test.sh @@ -0,0 +1,26 @@ +#!/bin/sh + +prog=$(basename "$0") +if ! [ -S /var/run/docker.sock ] +then + echo "$prog: there are no running docker" >&2 + exit 2 +fi + +cd "$(dirname "$0")" || exit +PATH=$(pwd):$PATH +plugin=$(basename "$(pwd)") +if ! which "$plugin" >/dev/null +then + echo "$prog: $plugin is not installed" >&2 + exit 2 +fi + +port=21211 +image=memcached + +docker run --name "test-$plugin" -p "$port:11211" -d "$image" +trap 'docker stop test-$plugin; docker rm test-$plugin; exit' EXIT +sleep 10 + +exec $plugin -p $port -k test