1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
|
package utils
import (
"bytes"
"fmt"
"os"
"path/filepath"
"syscall"
"testing"
)
func TestGenerateName(t *testing.T) {
name, err := GenerateRandomName("veth", 5)
if err != nil {
t.Fatal(err)
}
expected := 5 + len("veth")
if len(name) != expected {
t.Fatalf("expected name to be %d chars but received %d", expected, len(name))
}
name, err = GenerateRandomName("veth", 65)
if err != nil {
t.Fatal(err)
}
expected = 64 + len("veth")
if len(name) != expected {
t.Fatalf("expected name to be %d chars but received %d", expected, len(name))
}
}
var labelTest = []struct {
labels []string
query string
expectedValue string
}{
{[]string{"bundle=/path/to/bundle"}, "bundle", "/path/to/bundle"},
{[]string{"test=a", "test=b"}, "bundle", ""},
{[]string{"bundle=a", "test=b", "bundle=c"}, "bundle", "a"},
{[]string{"", "test=a", "bundle=b"}, "bundle", "b"},
{[]string{"test", "bundle=a"}, "bundle", "a"},
{[]string{"test=a", "bundle="}, "bundle", ""},
}
func TestSearchLabels(t *testing.T) {
for _, tt := range labelTest {
if v := SearchLabels(tt.labels, tt.query); v != tt.expectedValue {
t.Errorf("expected value '%s' for query '%s'; got '%s'", tt.expectedValue, tt.query, v)
}
}
}
func TestResolveRootfs(t *testing.T) {
dir := "rootfs"
os.Mkdir(dir, 0600)
defer os.Remove(dir)
path, err := ResolveRootfs(dir)
if err != nil {
t.Fatal(err)
}
pwd, err := os.Getwd()
if err != nil {
t.Fatal(err)
}
if path != fmt.Sprintf("%s/%s", pwd, "rootfs") {
t.Errorf("expected rootfs to be abs and was %s", path)
}
}
func TestResolveRootfsWithSymlink(t *testing.T) {
dir := "rootfs"
tmpDir, _ := filepath.EvalSymlinks(os.TempDir())
os.Symlink(tmpDir, dir)
defer os.Remove(dir)
path, err := ResolveRootfs(dir)
if err != nil {
t.Fatal(err)
}
if path != tmpDir {
t.Errorf("expected rootfs to be the real path %s and was %s", path, os.TempDir())
}
}
func TestResolveRootfsWithNonExistingDir(t *testing.T) {
_, err := ResolveRootfs("foo")
if err == nil {
t.Error("expected error to happen but received nil")
}
}
func TestExitStatus(t *testing.T) {
status := syscall.WaitStatus(0)
ex := ExitStatus(status)
if ex != 0 {
t.Errorf("expected exit status to equal 0 and received %d", ex)
}
}
func TestExitStatusSignaled(t *testing.T) {
status := syscall.WaitStatus(2)
ex := ExitStatus(status)
if ex != 130 {
t.Errorf("expected exit status to equal 130 and received %d", ex)
}
}
func TestWriteJSON(t *testing.T) {
person := struct {
Name string
Age int
}{
Name: "Alice",
Age: 30,
}
var b bytes.Buffer
err := WriteJSON(&b, person)
if err != nil {
t.Fatal(err)
}
expected := `{"Name":"Alice","Age":30}`
if b.String() != expected {
t.Errorf("expected to write %s but was %s", expected, b.String())
}
}
func TestCleanPath(t *testing.T) {
path := CleanPath("")
if path != "" {
t.Errorf("expected to received empty string and received %s", path)
}
path = CleanPath("rootfs")
if path != "rootfs" {
t.Errorf("expected to received 'rootfs' and received %s", path)
}
path = CleanPath("../../../var")
if path != "var" {
t.Errorf("expected to received 'var' and received %s", path)
}
path = CleanPath("/../../../var")
if path != "/var" {
t.Errorf("expected to received '/var' and received %s", path)
}
}
|