Skip to content
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
24 changes: 24 additions & 0 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
name: test
on:
push:
branches:
- "**"
jobs:
test:
name: test
runs-on: ubuntu-latest
steps:
- name: install apt packages
run: |
sudo apt update -y -qq
sudo 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 ./...
7 changes: 7 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -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
5 changes: 5 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -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
)
106 changes: 106 additions & 0 deletions targetd/client_test.go
Original file line number Diff line number Diff line change
@@ -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
}
49 changes: 49 additions & 0 deletions targetd/pool_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
}
6 changes: 6 additions & 0 deletions testing/docker/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
FROM whywaita/targetd:latest

ADD ./targetd.yaml /etc/target/targetd.yaml
#ADD ./entrypoint.sh /entrypoint.sh
#
#ENTRYPOINT ["/entrypoint.sh"]
8 changes: 8 additions & 0 deletions testing/docker/entrypoint.sh
Original file line number Diff line number Diff line change
@@ -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
6 changes: 6 additions & 0 deletions testing/docker/targetd.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
user: "testing"
password: "secret_password"
ssl: false
target_name: "iqn.0000-00.com.example:target0"

fs_pools: ["/diskfs"]