From ad832471a11a908ed31c88ee170a466105c3698f Mon Sep 17 00:00:00 2001 From: Ivan Mikushin Date: Mon, 13 Jul 2015 19:14:24 +0500 Subject: [PATCH] isolate and mark platform dependent code now unit tests compile on OS X and (some of them) work --- config/types.go | 22 ++++++++++----------- docker/container_test.go | 18 ++++++++++++------ init/bootstrap.go | 4 +++- init/init.go | 2 ++ util/cutil.go | 2 ++ util/util.go | 32 ------------------------------- util/util_linux.go | 41 ++++++++++++++++++++++++++++++++++++++++ util/util_test.go | 22 +++++++++++++++++++++ 8 files changed, 93 insertions(+), 50 deletions(-) create mode 100644 util/util_linux.go create mode 100644 util/util_test.go diff --git a/config/types.go b/config/types.go index 7632bdaa..f2de1a8a 100644 --- a/config/types.go +++ b/config/types.go @@ -3,17 +3,17 @@ package config import "github.com/rancherio/rancher-compose/librcompose/project" const ( - CONSOLE_CONTAINER = "console" - DOCKER_BIN = "/usr/bin/docker" - DOCKER_SYSTEM_HOME = "/var/lib/system-docker" - DOCKER_SYSTEM_HOST = "unix:///var/run/system-docker.sock" - DOCKER_HOST = "unix:///var/run/docker.sock" - IMAGES_PATH = "/" - IMAGES_PATTERN = "images*.tar" - SYS_INIT = "/sbin/init-sys" - USER_INIT = "/sbin/init-user" - MODULES_ARCHIVE = "/modules.tar" - DEBUG = false + CONSOLE_CONTAINER = "console" + DOCKER_BIN = "/usr/bin/docker" + DOCKER_SYSTEM_HOME = "/var/lib/system-docker" + DOCKER_SYSTEM_HOST = "unix:///var/run/system-docker.sock" + DOCKER_HOST = "unix:///var/run/docker.sock" + IMAGES_PATH = "/" + IMAGES_PATTERN = "images*.tar" + SYS_INIT = "/sbin/init-sys" + USER_INIT = "/sbin/init-user" + MODULES_ARCHIVE = "/modules.tar" + DEBUG = false LABEL = "label" HASH = "io.rancher.os.hash" diff --git a/docker/container_test.go b/docker/container_test.go index cdfe578d..06c613f6 100644 --- a/docker/container_test.go +++ b/docker/container_test.go @@ -10,8 +10,14 @@ import ( "github.com/stretchr/testify/require" dockerClient "github.com/fsouza/go-dockerclient" + "os" ) +func testDockerHost(t *testing.T) { + assert := require.New(t) + assert.Equal(os.Getenv("DOCKER_HOST"), config.DOCKER_HOST) +} + func TestHash(t *testing.T) { assert := require.New(t) @@ -134,7 +140,7 @@ func TestIdFromName(t *testing.T) { assert.Equal("foo", cfg.Id) } -func TestMigrateVolumes(t *testing.T) { +func testMigrateVolumes(t *testing.T) { assert := require.New(t) c := NewContainer(config.DOCKER_HOST, &config.ContainerConfig{ @@ -163,7 +169,7 @@ func TestMigrateVolumes(t *testing.T) { c2.Delete() } -func TestRollback(t *testing.T) { +func testRollback(t *testing.T) { assert := require.New(t) c := NewContainer(config.DOCKER_HOST, &config.ContainerConfig{ @@ -197,7 +203,7 @@ func TestRollback(t *testing.T) { c2.Delete() } -func TestStart(t *testing.T) { +func testStart(t *testing.T) { assert := require.New(t) c := NewContainer(config.DOCKER_HOST, &config.ContainerConfig{ @@ -213,7 +219,7 @@ func TestStart(t *testing.T) { c.Delete() } -func TestLookup(t *testing.T) { +func testLookup(t *testing.T) { assert := require.New(t) cfg := &config.ContainerConfig{ @@ -242,7 +248,7 @@ func TestLookup(t *testing.T) { c2.Delete() } -func TestDelete(t *testing.T) { +func testDelete(t *testing.T) { assert := require.New(t) c := NewContainer(config.DOCKER_HOST, &config.ContainerConfig{ @@ -268,7 +274,7 @@ func TestDelete(t *testing.T) { assert.NoError(c.Err, "") } -func TestDockerClientNames(t *testing.T) { +func testDockerClientNames(t *testing.T) { assert := require.New(t) client, err := dockerClient.NewClient(config.DOCKER_HOST) diff --git a/init/bootstrap.go b/init/bootstrap.go index 11d4568d..b822307a 100644 --- a/init/bootstrap.go +++ b/init/bootstrap.go @@ -1,3 +1,5 @@ +// +build linux + package init import ( @@ -77,7 +79,7 @@ outer: "autoformat": { Net: "none", Privileged: true, - Image: "rancher/os-autoformat:"+config.VERSION, + Image: "rancher/os-autoformat:" + config.VERSION, Command: project.NewCommand(format), Labels: project.NewSliceorMap(map[string]string{ config.DETACH: "false", diff --git a/init/init.go b/init/init.go index be3a4f7d..5c56f24d 100644 --- a/init/init.go +++ b/init/init.go @@ -1,3 +1,5 @@ +// +build linux + package init import ( diff --git a/util/cutil.go b/util/cutil.go index d9f4cb11..ac770ef7 100644 --- a/util/cutil.go +++ b/util/cutil.go @@ -1,3 +1,5 @@ +// +build linux + package util /* diff --git a/util/util.go b/util/util.go index 179fc1fd..edd58786 100644 --- a/util/util.go +++ b/util/util.go @@ -12,7 +12,6 @@ import ( "os" "path" "strings" - "syscall" "gopkg.in/yaml.v2" @@ -44,37 +43,6 @@ func GetOSType() string { } -func mountProc() error { - if _, err := os.Stat("/proc/self/mountinfo"); os.IsNotExist(err) { - if _, err := os.Stat("/proc"); os.IsNotExist(err) { - if err = os.Mkdir("/proc", 0755); err != nil { - return err - } - } - - if err := syscall.Mount("none", "/proc", "proc", 0, ""); err != nil { - return err - } - } - - return nil -} - -func Mount(device, directory, fsType, options string) error { - if err := mountProc(); err != nil { - return nil - } - - if _, err := os.Stat(directory); os.IsNotExist(err) { - err = os.MkdirAll(directory, 0755) - if err != nil { - return err - } - } - - return mount.Mount(device, directory, fsType, options) -} - func Remount(directory, options string) error { return mount.Mount("", directory, "", fmt.Sprintf("remount,%s", options)) } diff --git a/util/util_linux.go b/util/util_linux.go new file mode 100644 index 00000000..e33d458c --- /dev/null +++ b/util/util_linux.go @@ -0,0 +1,41 @@ +// +build linux + +package util + +import ( + "os" + "syscall" + + "github.com/docker/docker/pkg/mount" +) + +func mountProc() error { + if _, err := os.Stat("/proc/self/mountinfo"); os.IsNotExist(err) { + if _, err := os.Stat("/proc"); os.IsNotExist(err) { + if err = os.Mkdir("/proc", 0755); err != nil { + return err + } + } + + if err := syscall.Mount("none", "/proc", "proc", 0, ""); err != nil { + return err + } + } + + return nil +} + +func Mount(device, directory, fsType, options string) error { + if err := mountProc(); err != nil { + return nil + } + + if _, err := os.Stat(directory); os.IsNotExist(err) { + err = os.MkdirAll(directory, 0755) + if err != nil { + return err + } + } + + return mount.Mount(device, directory, fsType, options) +} diff --git a/util/util_test.go b/util/util_test.go new file mode 100644 index 00000000..a2a06fdc --- /dev/null +++ b/util/util_test.go @@ -0,0 +1,22 @@ +package util + +import ( + "github.com/stretchr/testify/require" + "strings" + "testing" +) + +func TestLoadResourceSimple(t *testing.T) { + assert := require.New(t) + + expected := `services: +- debian-console +- ubuntu-console +` + expected = strings.TrimSpace(expected) + + b, e := LoadResource("https://raw.githubusercontent.com/rancherio/os-services/v0.3.4/index.yml", true, []string{}) + + assert.Nil(e) + assert.Equal(expected, strings.TrimSpace(string(b))) +}