2013-05-14 22:37:35 +00:00
|
|
|
package utils
|
2013-01-19 00:13:39 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"errors"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
2013-07-16 00:56:18 +00:00
|
|
|
"strings"
|
2013-01-19 00:13:39 +00:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestBufReader(t *testing.T) {
|
|
|
|
reader, writer := io.Pipe()
|
2013-05-16 00:46:01 +00:00
|
|
|
bufreader := NewBufReader(reader)
|
2013-01-19 00:13:39 +00:00
|
|
|
|
|
|
|
// Write everything down to a Pipe
|
|
|
|
// Usually, a pipe should block but because of the buffered reader,
|
|
|
|
// the writes will go through
|
|
|
|
done := make(chan bool)
|
|
|
|
go func() {
|
|
|
|
writer.Write([]byte("hello world"))
|
|
|
|
writer.Close()
|
|
|
|
done <- true
|
|
|
|
}()
|
|
|
|
|
|
|
|
// Drain the reader *after* everything has been written, just to verify
|
|
|
|
// it is indeed buffering
|
|
|
|
<-done
|
|
|
|
output, err := ioutil.ReadAll(bufreader)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if !bytes.Equal(output, []byte("hello world")) {
|
|
|
|
t.Error(string(output))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type dummyWriter struct {
|
|
|
|
buffer bytes.Buffer
|
|
|
|
failOnWrite bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func (dw *dummyWriter) Write(p []byte) (n int, err error) {
|
|
|
|
if dw.failOnWrite {
|
|
|
|
return 0, errors.New("Fake fail")
|
|
|
|
}
|
|
|
|
return dw.buffer.Write(p)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (dw *dummyWriter) String() string {
|
|
|
|
return dw.buffer.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (dw *dummyWriter) Close() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestWriteBroadcaster(t *testing.T) {
|
2013-05-16 00:46:01 +00:00
|
|
|
writer := NewWriteBroadcaster()
|
2013-01-19 00:13:39 +00:00
|
|
|
|
|
|
|
// Test 1: Both bufferA and bufferB should contain "foo"
|
|
|
|
bufferA := &dummyWriter{}
|
2013-07-19 13:56:36 +00:00
|
|
|
writer.AddWriter(bufferA, "")
|
2013-01-19 00:13:39 +00:00
|
|
|
bufferB := &dummyWriter{}
|
2013-07-19 13:56:36 +00:00
|
|
|
writer.AddWriter(bufferB, "")
|
2013-01-19 00:13:39 +00:00
|
|
|
writer.Write([]byte("foo"))
|
|
|
|
|
|
|
|
if bufferA.String() != "foo" {
|
|
|
|
t.Errorf("Buffer contains %v", bufferA.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
if bufferB.String() != "foo" {
|
|
|
|
t.Errorf("Buffer contains %v", bufferB.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test2: bufferA and bufferB should contain "foobar",
|
|
|
|
// while bufferC should only contain "bar"
|
|
|
|
bufferC := &dummyWriter{}
|
2013-07-19 13:56:36 +00:00
|
|
|
writer.AddWriter(bufferC, "")
|
2013-01-19 00:13:39 +00:00
|
|
|
writer.Write([]byte("bar"))
|
|
|
|
|
|
|
|
if bufferA.String() != "foobar" {
|
|
|
|
t.Errorf("Buffer contains %v", bufferA.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
if bufferB.String() != "foobar" {
|
|
|
|
t.Errorf("Buffer contains %v", bufferB.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
if bufferC.String() != "bar" {
|
|
|
|
t.Errorf("Buffer contains %v", bufferC.String())
|
|
|
|
}
|
|
|
|
|
2013-07-19 13:56:36 +00:00
|
|
|
// Test3: Test eviction on failure
|
2013-01-19 00:13:39 +00:00
|
|
|
bufferA.failOnWrite = true
|
|
|
|
writer.Write([]byte("fail"))
|
2013-07-19 13:56:36 +00:00
|
|
|
if bufferA.String() != "foobar" {
|
2013-01-19 00:13:39 +00:00
|
|
|
t.Errorf("Buffer contains %v", bufferA.String())
|
|
|
|
}
|
2013-07-19 13:56:36 +00:00
|
|
|
if bufferC.String() != "barfail" {
|
2013-01-19 00:13:39 +00:00
|
|
|
t.Errorf("Buffer contains %v", bufferC.String())
|
|
|
|
}
|
|
|
|
// Even though we reset the flag, no more writes should go in there
|
|
|
|
bufferA.failOnWrite = false
|
|
|
|
writer.Write([]byte("test"))
|
2013-07-19 13:56:36 +00:00
|
|
|
if bufferA.String() != "foobar" {
|
2013-01-19 00:13:39 +00:00
|
|
|
t.Errorf("Buffer contains %v", bufferA.String())
|
|
|
|
}
|
2013-07-19 13:56:36 +00:00
|
|
|
if bufferC.String() != "barfailtest" {
|
2013-01-19 00:13:39 +00:00
|
|
|
t.Errorf("Buffer contains %v", bufferC.String())
|
|
|
|
}
|
|
|
|
|
2013-04-02 08:45:17 +00:00
|
|
|
writer.CloseWriters()
|
2013-01-19 00:13:39 +00:00
|
|
|
}
|
2013-03-30 13:37:06 +00:00
|
|
|
|
|
|
|
type devNullCloser int
|
|
|
|
|
|
|
|
func (d devNullCloser) Close() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d devNullCloser) Write(buf []byte) (int, error) {
|
|
|
|
return len(buf), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// This test checks for races. It is only useful when run with the race detector.
|
|
|
|
func TestRaceWriteBroadcaster(t *testing.T) {
|
2013-05-16 00:46:01 +00:00
|
|
|
writer := NewWriteBroadcaster()
|
2013-03-30 13:37:06 +00:00
|
|
|
c := make(chan bool)
|
|
|
|
go func() {
|
2013-07-19 13:56:36 +00:00
|
|
|
writer.AddWriter(devNullCloser(0), "")
|
2013-03-30 13:37:06 +00:00
|
|
|
c <- true
|
|
|
|
}()
|
|
|
|
writer.Write([]byte("hello"))
|
|
|
|
<-c
|
2013-01-19 00:13:39 +00:00
|
|
|
}
|
2013-03-31 09:02:01 +00:00
|
|
|
|
|
|
|
// Test the behavior of TruncIndex, an index for querying IDs from a non-conflicting prefix.
|
|
|
|
func TestTruncIndex(t *testing.T) {
|
|
|
|
index := NewTruncIndex()
|
|
|
|
// Get on an empty index
|
|
|
|
if _, err := index.Get("foobar"); err == nil {
|
|
|
|
t.Fatal("Get on an empty index should return an error")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Spaces should be illegal in an id
|
|
|
|
if err := index.Add("I have a space"); err == nil {
|
|
|
|
t.Fatalf("Adding an id with ' ' should return an error")
|
|
|
|
}
|
|
|
|
|
|
|
|
id := "99b36c2c326ccc11e726eee6ee78a0baf166ef96"
|
|
|
|
// Add an id
|
|
|
|
if err := index.Add(id); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
// Get a non-existing id
|
|
|
|
assertIndexGet(t, index, "abracadabra", "", true)
|
|
|
|
// Get the exact id
|
|
|
|
assertIndexGet(t, index, id, id, false)
|
|
|
|
// The first letter should match
|
|
|
|
assertIndexGet(t, index, id[:1], id, false)
|
|
|
|
// The first half should match
|
|
|
|
assertIndexGet(t, index, id[:len(id)/2], id, false)
|
|
|
|
// The second half should NOT match
|
|
|
|
assertIndexGet(t, index, id[len(id)/2:], "", true)
|
|
|
|
|
|
|
|
id2 := id[:6] + "blabla"
|
|
|
|
// Add an id
|
|
|
|
if err := index.Add(id2); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
// Both exact IDs should work
|
|
|
|
assertIndexGet(t, index, id, id, false)
|
|
|
|
assertIndexGet(t, index, id2, id2, false)
|
|
|
|
|
|
|
|
// 6 characters or less should conflict
|
|
|
|
assertIndexGet(t, index, id[:6], "", true)
|
|
|
|
assertIndexGet(t, index, id[:4], "", true)
|
|
|
|
assertIndexGet(t, index, id[:1], "", true)
|
|
|
|
|
|
|
|
// 7 characters should NOT conflict
|
|
|
|
assertIndexGet(t, index, id[:7], id, false)
|
|
|
|
assertIndexGet(t, index, id2[:7], id2, false)
|
|
|
|
|
|
|
|
// Deleting a non-existing id should return an error
|
|
|
|
if err := index.Delete("non-existing"); err == nil {
|
|
|
|
t.Fatalf("Deleting a non-existing id should return an error")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Deleting id2 should remove conflicts
|
|
|
|
if err := index.Delete(id2); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
// id2 should no longer work
|
|
|
|
assertIndexGet(t, index, id2, "", true)
|
|
|
|
assertIndexGet(t, index, id2[:7], "", true)
|
|
|
|
assertIndexGet(t, index, id2[:11], "", true)
|
|
|
|
|
|
|
|
// conflicts between id and id2 should be gone
|
|
|
|
assertIndexGet(t, index, id[:6], id, false)
|
|
|
|
assertIndexGet(t, index, id[:4], id, false)
|
|
|
|
assertIndexGet(t, index, id[:1], id, false)
|
|
|
|
|
|
|
|
// non-conflicting substrings should still not conflict
|
|
|
|
assertIndexGet(t, index, id[:7], id, false)
|
|
|
|
assertIndexGet(t, index, id[:15], id, false)
|
|
|
|
assertIndexGet(t, index, id, id, false)
|
|
|
|
}
|
|
|
|
|
|
|
|
func assertIndexGet(t *testing.T, index *TruncIndex, input, expectedResult string, expectError bool) {
|
|
|
|
if result, err := index.Get(input); err != nil && !expectError {
|
|
|
|
t.Fatalf("Unexpected error getting '%s': %s", input, err)
|
|
|
|
} else if err == nil && expectError {
|
|
|
|
t.Fatalf("Getting '%s' should return an error", input)
|
|
|
|
} else if result != expectedResult {
|
|
|
|
t.Fatalf("Getting '%s' returned '%s' instead of '%s'", input, result, expectedResult)
|
|
|
|
}
|
|
|
|
}
|
2013-04-19 04:34:34 +00:00
|
|
|
|
|
|
|
func assertKernelVersion(t *testing.T, a, b *KernelVersionInfo, result int) {
|
|
|
|
if r := CompareKernelVersion(a, b); r != result {
|
|
|
|
t.Fatalf("Unepected kernel version comparaison result. Found %d, expected %d", r, result)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCompareKernelVersion(t *testing.T) {
|
|
|
|
assertKernelVersion(t,
|
2013-04-22 01:19:38 +00:00
|
|
|
&KernelVersionInfo{Kernel: 3, Major: 8, Minor: 0},
|
|
|
|
&KernelVersionInfo{Kernel: 3, Major: 8, Minor: 0},
|
2013-04-19 04:34:34 +00:00
|
|
|
0)
|
|
|
|
assertKernelVersion(t,
|
2013-04-22 01:19:38 +00:00
|
|
|
&KernelVersionInfo{Kernel: 2, Major: 6, Minor: 0},
|
|
|
|
&KernelVersionInfo{Kernel: 3, Major: 8, Minor: 0},
|
2013-04-19 04:34:34 +00:00
|
|
|
-1)
|
|
|
|
assertKernelVersion(t,
|
2013-04-22 01:19:38 +00:00
|
|
|
&KernelVersionInfo{Kernel: 3, Major: 8, Minor: 0},
|
|
|
|
&KernelVersionInfo{Kernel: 2, Major: 6, Minor: 0},
|
2013-04-19 04:34:34 +00:00
|
|
|
1)
|
|
|
|
assertKernelVersion(t,
|
2013-04-22 01:19:38 +00:00
|
|
|
&KernelVersionInfo{Kernel: 3, Major: 8, Minor: 0, Flavor: "0"},
|
|
|
|
&KernelVersionInfo{Kernel: 3, Major: 8, Minor: 0, Flavor: "16"},
|
|
|
|
0)
|
2013-04-19 04:34:34 +00:00
|
|
|
assertKernelVersion(t,
|
2013-04-22 01:19:38 +00:00
|
|
|
&KernelVersionInfo{Kernel: 3, Major: 8, Minor: 5},
|
|
|
|
&KernelVersionInfo{Kernel: 3, Major: 8, Minor: 0},
|
2013-04-19 04:34:34 +00:00
|
|
|
1)
|
|
|
|
assertKernelVersion(t,
|
2013-04-22 01:19:38 +00:00
|
|
|
&KernelVersionInfo{Kernel: 3, Major: 0, Minor: 20, Flavor: "25"},
|
|
|
|
&KernelVersionInfo{Kernel: 3, Major: 8, Minor: 0, Flavor: "0"},
|
2013-04-19 04:34:34 +00:00
|
|
|
-1)
|
|
|
|
}
|
2013-05-23 10:29:09 +00:00
|
|
|
|
|
|
|
func TestHumanSize(t *testing.T) {
|
|
|
|
|
2013-07-16 00:56:18 +00:00
|
|
|
size := strings.Trim(HumanSize(1000), " \t")
|
|
|
|
expect := "1 kB"
|
|
|
|
if size != expect {
|
|
|
|
t.Errorf("1000 -> expected '%s', got '%s'", expect, size)
|
2013-05-23 10:29:09 +00:00
|
|
|
}
|
|
|
|
|
2013-07-16 00:56:18 +00:00
|
|
|
size = strings.Trim(HumanSize(1024), " \t")
|
|
|
|
expect = "1.024 kB"
|
|
|
|
if size != expect {
|
|
|
|
t.Errorf("1024 -> expected '%s', got '%s'", expect, size)
|
2013-05-23 10:29:09 +00:00
|
|
|
}
|
|
|
|
}
|
2013-06-19 12:40:01 +00:00
|
|
|
|
|
|
|
func TestParseHost(t *testing.T) {
|
|
|
|
if addr := ParseHost("127.0.0.1", 4243, "0.0.0.0"); addr != "tcp://0.0.0.0:4243" {
|
|
|
|
t.Errorf("0.0.0.0 -> expected tcp://0.0.0.0:4243, got %s", addr)
|
|
|
|
}
|
|
|
|
if addr := ParseHost("127.0.0.1", 4243, "0.0.0.1:5555"); addr != "tcp://0.0.0.1:5555" {
|
|
|
|
t.Errorf("0.0.0.1:5555 -> expected tcp://0.0.0.1:5555, got %s", addr)
|
|
|
|
}
|
|
|
|
if addr := ParseHost("127.0.0.1", 4243, ":6666"); addr != "tcp://127.0.0.1:6666" {
|
|
|
|
t.Errorf(":6666 -> expected tcp://127.0.0.1:6666, got %s", addr)
|
|
|
|
}
|
|
|
|
if addr := ParseHost("127.0.0.1", 4243, "tcp://:7777"); addr != "tcp://127.0.0.1:7777" {
|
|
|
|
t.Errorf("tcp://:7777 -> expected tcp://127.0.0.1:7777, got %s", addr)
|
|
|
|
}
|
|
|
|
if addr := ParseHost("127.0.0.1", 4243, "unix:///var/run/docker.sock"); addr != "unix:///var/run/docker.sock" {
|
|
|
|
t.Errorf("unix:///var/run/docker.sock -> expected unix:///var/run/docker.sock, got %s", addr)
|
|
|
|
}
|
|
|
|
}
|