From c7102730d0bfce8f79b047052a55ffcc65c1aea7 Mon Sep 17 00:00:00 2001 From: whywaita Date: Mon, 28 Dec 2020 16:54:11 +0900 Subject: [PATCH 1/2] add test --- .github/workflows/test.yaml | 24 ++++++++ Makefile | 7 +++ go.mod | 5 ++ targetd/client_test.go | 106 +++++++++++++++++++++++++++++++++++ targetd/pool_test.go | 49 ++++++++++++++++ testing/docker/Dockerfile | 6 ++ testing/docker/entrypoint.sh | 8 +++ testing/docker/targetd.yaml | 6 ++ 8 files changed, 211 insertions(+) create mode 100644 .github/workflows/test.yaml create mode 100644 Makefile create mode 100644 targetd/client_test.go create mode 100644 targetd/pool_test.go create mode 100644 testing/docker/Dockerfile create mode 100644 testing/docker/entrypoint.sh create mode 100644 testing/docker/targetd.yaml diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml new file mode 100644 index 0000000..e2dec17 --- /dev/null +++ b/.github/workflows/test.yaml @@ -0,0 +1,24 @@ +name: test +on: + push: + branches: + - "**" +jobs: + test: + name: test + runs-on: ubuntu-latest + steps: + - name: install apt packages + run: | + apt update -y -qq + apt install -y gcc sudo + - name: setup go + uses: actions/setup-go@v2 + with: + go-version: '^1.15' + - name: checkout + uses: actions/checkout@v2 + with: + fetch-depth: 1 + - name: test + run: $(echo $(which go)) test -v ./... \ No newline at end of file diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..dd7f63c --- /dev/null +++ b/Makefile @@ -0,0 +1,7 @@ +.DEFAULT_GOAL := help + +help: + @grep -E '^[a-zA-Z0-9_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-20s\033[0m %s\n", $$1, $$2}' + +test: ## test + act -j test \ No newline at end of file diff --git a/go.mod b/go.mod index 21ef939..2e4f5e1 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,8 @@ module github.com/lovi-cloud/go-targetd go 1.15 + +require ( + github.com/google/go-cmp v0.4.0 + github.com/ory/dockertest/v3 v3.6.3 +) diff --git a/targetd/client_test.go b/targetd/client_test.go new file mode 100644 index 0000000..d519433 --- /dev/null +++ b/targetd/client_test.go @@ -0,0 +1,106 @@ +package targetd + +import ( + "fmt" + "log" + "net" + "os" + "os/exec" + "testing" + "time" + + "github.com/ory/dockertest/v3" +) + +const ( + testFilePath = "/datafile-go-targetd-test" +) + +var ( + testHost = "http://127.0.0.1:18700" +) + +func setup() (*Client, error) { + client, err := New(testHost, "testing", "secret_password", nil, nil) + if err != nil { + return nil, fmt.Errorf("failed to targetd.New: %w", err) + } + + return client, nil +} + +func TestMain(m *testing.M) { + os.Exit(IntegrationTestRunner(m)) +} + +// IntegrationTestRunner is all integration test +func IntegrationTestRunner(m *testing.M) int { + pool, err := dockertest.NewPool("") + if err != nil { + log.Fatalf("Could not connect to docker: %s", err) + } + + runOption := &dockertest.RunOptions{ + Name: "go-targetd", + Mounts: []string{ + "/sys/kernel/config:/sys/kernel/config", + "/lib/modules:/lib/modules", + "/dev:/dev", + }, + ExposedPorts: []string{ + "18700/tcp", + }, + Privileged: true, + } + + // Build and run the given Dockerfile + resource, err := pool.BuildAndRunWithOptions("../testing/docker/Dockerfile", runOption) + if err != nil { + log.Fatalf("Could not start resource: %s", err) + } + + if err = pool.Retry(func() error { + testHost = fmt.Sprintf("http://localhost:%s", resource.GetPort("18700/tcp")) + + if _, err := net.DialTimeout("tcp", testHost, 10*time.Second); err != nil { + return fmt.Errorf("cloud not dial targetd host: %w", err) + } + + return nil + }); err != nil { + log.Fatalf("Could not connect to docker: %s", err) + } + defer func() { + if err = pool.Purge(resource); err != nil { + log.Fatalf("Could not purge resource: %s", err) + } + }() + + //if out, err := exec.CommandContext(context.Background(), "../test/scripts/init.sh").CombinedOutput(); err != nil { + // log.Printf("init.sh return err: %+v (out: %+v)", err, out) + // return 1 + //} + + // setup filesystem + if err := initializeFile(); err != nil { + log.Fatalf("Cloud not initialize device: %+v", err) + } + + code := m.Run() + + if err := initializeFile(); err != nil { + log.Fatalf("Cloud not initalize device: %+v", err) + } + + return code +} + +func initializeFile() error { + // TODO: execute in targetd container + out, err := exec.Command("rm", "-f", testFilePath).CombinedOutput() + if err != nil { + return fmt.Errorf("failed to delete %s (out: %s): %w", testFilePath, string(out), err) + } + + return nil +} diff --git a/targetd/pool_test.go b/targetd/pool_test.go new file mode 100644 index 0000000..a5ac752 --- /dev/null +++ b/targetd/pool_test.go @@ -0,0 +1,49 @@ +package targetd + +import ( + "context" + "testing" + + "github.com/google/go-cmp/cmp" +) + +func TestClient_GetPoolList(t *testing.T) { + client, err := setup() + if err != nil { + t.Fatalf("failed to setup client: %+v", err) + } + + tests := []struct { + input interface{} + want []Pool + err bool + }{ + { + input: nil, + want: []Pool{ + { + Name: "", + Size: 0, + FreeSize: 0, + Type: "", + UUID: 0, + }, + }, + err: false, + }, + } + + for _, test := range tests { + got, err := client.GetPoolList(context.Background()) + if !test.err && err != nil { + t.Fatalf("should not be error for %+v but: %+v", test.input, err) + } + if test.err && err == nil { + t.Fatalf("should be error for %+v but not:", test.input) + } + + if diff := cmp.Diff(test.want, got); diff != "" { + t.Errorf("mismatch (-want +got):\n%s", diff) + } + } +} diff --git a/testing/docker/Dockerfile b/testing/docker/Dockerfile new file mode 100644 index 0000000..641cd7d --- /dev/null +++ b/testing/docker/Dockerfile @@ -0,0 +1,6 @@ +FROM whywaita/targetd:latest + +ADD ./targetd.yaml /etc/target/targetd.yaml +#ADD ./entrypoint.sh /entrypoint.sh +# +#ENTRYPOINT ["/entrypoint.sh"] \ No newline at end of file diff --git a/testing/docker/entrypoint.sh b/testing/docker/entrypoint.sh new file mode 100644 index 0000000..7fb669f --- /dev/null +++ b/testing/docker/entrypoint.sh @@ -0,0 +1,8 @@ +#!/bin/bash + +apt update -y -qq +apt install -y targetcli-fb dbus kmod + +# enable d-bus daemon +mkdir /run/dbus +dbus-daemon --system \ No newline at end of file diff --git a/testing/docker/targetd.yaml b/testing/docker/targetd.yaml new file mode 100644 index 0000000..e7c5a22 --- /dev/null +++ b/testing/docker/targetd.yaml @@ -0,0 +1,6 @@ +user: "testing" +password: "secret_password" +ssl: false +target_name: "iqn.0000-00.com.example:target0" + +fs_pools: ["/diskfs"] \ No newline at end of file From e4589c2f2f98ca341ccf7cf3f6b4d778dd3cc9bf Mon Sep 17 00:00:00 2001 From: whywaita Date: Mon, 28 Dec 2020 16:56:07 +0900 Subject: [PATCH 2/2] apt need sudo --- .github/workflows/test.yaml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index e2dec17..2d6f5cd 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -10,8 +10,8 @@ jobs: steps: - name: install apt packages run: | - apt update -y -qq - apt install -y gcc sudo + sudo apt update -y -qq + sudo apt install -y gcc sudo - name: setup go uses: actions/setup-go@v2 with: