2013-05-14 18:37:35 -04:00
|
|
|
package utils
|
2013-01-18 19:13:39 -05:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
2014-02-24 16:10:06 -05:00
|
|
|
"os"
|
2013-01-18 19:13:39 -05:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestBufReader(t *testing.T) {
|
|
|
|
reader, writer := io.Pipe()
|
2013-05-15 20:46:01 -04:00
|
|
|
bufreader := NewBufReader(reader)
|
2013-01-18 19:13:39 -05: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))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-05 19:32:25 -04:00
|
|
|
func TestCheckLocalDns(t *testing.T) {
|
2013-08-02 18:23:36 -04:00
|
|
|
for resolv, result := range map[string]bool{`# Dynamic
|
|
|
|
nameserver 10.0.2.3
|
2014-07-24 18:19:50 -04:00
|
|
|
search docker.com`: false,
|
2013-08-02 18:23:36 -04:00
|
|
|
`# Dynamic
|
2013-08-21 09:23:12 -04:00
|
|
|
#nameserver 127.0.0.1
|
|
|
|
nameserver 10.0.2.3
|
2014-07-24 18:19:50 -04:00
|
|
|
search docker.com`: false,
|
2013-08-21 09:23:12 -04:00
|
|
|
`# Dynamic
|
|
|
|
nameserver 10.0.2.3 #not used 127.0.1.1
|
2014-07-24 18:19:50 -04:00
|
|
|
search docker.com`: false,
|
2013-08-21 09:23:12 -04:00
|
|
|
`# Dynamic
|
|
|
|
#nameserver 10.0.2.3
|
2014-07-24 18:19:50 -04:00
|
|
|
#search docker.com`: true,
|
2013-08-21 09:23:12 -04:00
|
|
|
`# Dynamic
|
2013-08-02 18:23:36 -04:00
|
|
|
nameserver 127.0.0.1
|
2014-07-24 18:19:50 -04:00
|
|
|
search docker.com`: true,
|
2013-08-02 18:23:36 -04:00
|
|
|
`# Dynamic
|
|
|
|
nameserver 127.0.1.1
|
2014-07-24 18:19:50 -04:00
|
|
|
search docker.com`: true,
|
2013-08-02 18:23:36 -04:00
|
|
|
`# Dynamic
|
|
|
|
`: true,
|
|
|
|
``: true,
|
|
|
|
} {
|
|
|
|
if CheckLocalDns([]byte(resolv)) != result {
|
|
|
|
t.Fatalf("Wrong local dns detection: {%s} should be %v", resolv, result)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-03-01 02:29:00 -05:00
|
|
|
func TestReplaceAndAppendEnvVars(t *testing.T) {
|
|
|
|
var (
|
|
|
|
d = []string{"HOME=/"}
|
|
|
|
o = []string{"HOME=/root", "TERM=xterm"}
|
|
|
|
)
|
|
|
|
|
|
|
|
env := ReplaceOrAppendEnvValues(d, o)
|
|
|
|
if len(env) != 2 {
|
|
|
|
t.Fatalf("expected len of 2 got %d", len(env))
|
|
|
|
}
|
|
|
|
if env[0] != "HOME=/root" {
|
|
|
|
t.Fatalf("expected HOME=/root got '%s'", env[0])
|
|
|
|
}
|
|
|
|
if env[1] != "TERM=xterm" {
|
|
|
|
t.Fatalf("expected TERM=xterm got '%s'", env[1])
|
|
|
|
}
|
|
|
|
}
|
2014-02-24 16:10:06 -05:00
|
|
|
|
|
|
|
// Reading a symlink to a directory must return the directory
|
|
|
|
func TestReadSymlinkedDirectoryExistingDirectory(t *testing.T) {
|
|
|
|
var err error
|
|
|
|
if err = os.Mkdir("/tmp/testReadSymlinkToExistingDirectory", 0777); err != nil {
|
|
|
|
t.Errorf("failed to create directory: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = os.Symlink("/tmp/testReadSymlinkToExistingDirectory", "/tmp/dirLinkTest"); err != nil {
|
|
|
|
t.Errorf("failed to create symlink: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var path string
|
|
|
|
if path, err = ReadSymlinkedDirectory("/tmp/dirLinkTest"); err != nil {
|
|
|
|
t.Fatalf("failed to read symlink to directory: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if path != "/tmp/testReadSymlinkToExistingDirectory" {
|
|
|
|
t.Fatalf("symlink returned unexpected directory: %s", path)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = os.Remove("/tmp/testReadSymlinkToExistingDirectory"); err != nil {
|
|
|
|
t.Errorf("failed to remove temporary directory: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = os.Remove("/tmp/dirLinkTest"); err != nil {
|
|
|
|
t.Errorf("failed to remove symlink: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reading a non-existing symlink must fail
|
|
|
|
func TestReadSymlinkedDirectoryNonExistingSymlink(t *testing.T) {
|
|
|
|
var path string
|
|
|
|
var err error
|
|
|
|
if path, err = ReadSymlinkedDirectory("/tmp/test/foo/Non/ExistingPath"); err == nil {
|
|
|
|
t.Fatalf("error expected for non-existing symlink")
|
|
|
|
}
|
|
|
|
|
|
|
|
if path != "" {
|
|
|
|
t.Fatalf("expected empty path, but '%s' was returned", path)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reading a symlink to a file must fail
|
|
|
|
func TestReadSymlinkedDirectoryToFile(t *testing.T) {
|
|
|
|
var err error
|
|
|
|
var file *os.File
|
|
|
|
|
|
|
|
if file, err = os.Create("/tmp/testReadSymlinkToFile"); err != nil {
|
|
|
|
t.Fatalf("failed to create file: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
file.Close()
|
|
|
|
|
|
|
|
if err = os.Symlink("/tmp/testReadSymlinkToFile", "/tmp/fileLinkTest"); err != nil {
|
|
|
|
t.Errorf("failed to create symlink: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var path string
|
|
|
|
if path, err = ReadSymlinkedDirectory("/tmp/fileLinkTest"); err == nil {
|
|
|
|
t.Fatalf("ReadSymlinkedDirectory on a symlink to a file should've failed")
|
|
|
|
}
|
|
|
|
|
|
|
|
if path != "" {
|
|
|
|
t.Fatalf("path should've been empty: %s", path)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = os.Remove("/tmp/testReadSymlinkToFile"); err != nil {
|
|
|
|
t.Errorf("failed to remove file: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = os.Remove("/tmp/fileLinkTest"); err != nil {
|
|
|
|
t.Errorf("failed to remove symlink: %s", err)
|
|
|
|
}
|
|
|
|
}
|