From 379e35329ab640bd1cb55d87af5bc86b90a7dcce Mon Sep 17 00:00:00 2001 From: Daniel J Walsh Date: Thu, 28 May 2020 14:01:57 -0400 Subject: [PATCH] Add support for Remote flags in containers.conf Signed-off-by: Daniel J Walsh --- Makefile | 1 + pkg/config/config.go | 7 ++ pkg/config/config_local.go | 4 + pkg/config/config_local_test.go | 179 +++++++++++++++++++++++++++++++ pkg/config/config_remote.go | 4 + pkg/config/config_remote_test.go | 142 ++++++++++++++++++++++++ pkg/config/config_test.go | 140 +----------------------- pkg/config/default.go | 1 + 8 files changed, 339 insertions(+), 139 deletions(-) create mode 100644 pkg/config/config_local_test.go create mode 100644 pkg/config/config_remote_test.go diff --git a/Makefile b/Makefile index d89f0a90e..672501d3e 100644 --- a/Makefile +++ b/Makefile @@ -103,6 +103,7 @@ test: test-unit .PHONY: test-unit test-unit: go test -v $(PROJECT)/pkg/... + go test --tags remote -v $(PROJECT)/pkg/... clean: ## Clean artifacts $(MAKE) -C docs clean diff --git a/pkg/config/config.go b/pkg/config/config.go index 7e60de6b6..d0b56c7f6 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -262,6 +262,13 @@ type EngineConfig struct { // PullPolicy determines whether to pull image before creating or running a container // default is "missing" PullPolicy string `toml:"pull_policy"` + + // Indicates whether the application should be running in Remote mode + Remote bool `toml:"_"` + + // RemoteURI containers connection information used to connect to remote system. + RemoteURI string `toml:"remote_uri,omitempty"` + // RuntimePath is the path to OCI runtime binary for launching containers. // The first path pointing to a valid file will be used This is used only // when there are no OCIRuntime/OCIRuntimes defined. It is used only to be diff --git a/pkg/config/config_local.go b/pkg/config/config_local.go index db68cc347..8f4daa3d7 100644 --- a/pkg/config/config_local.go +++ b/pkg/config/config_local.go @@ -75,3 +75,7 @@ func (c *ContainersConfig) validateUlimits() error { } return nil } + +func isRemote() bool { + return false +} diff --git a/pkg/config/config_local_test.go b/pkg/config/config_local_test.go new file mode 100644 index 000000000..351d0f497 --- /dev/null +++ b/pkg/config/config_local_test.go @@ -0,0 +1,179 @@ +// +build !remote + +package config + +import ( + "io/ioutil" + "os" + "path" + + . "github.com/onsi/ginkgo" + "github.com/onsi/gomega" +) + +var _ = Describe("Config Local", func() { + BeforeEach(beforeEach) + + It("should fail on invalid NetworkConfigDir", func() { + // Given + tmpfile := path.Join(os.TempDir(), "wrong-file") + file, err := os.Create(tmpfile) + gomega.Expect(err).To(gomega.BeNil()) + file.Close() + defer os.Remove(tmpfile) + sut.Network.NetworkConfigDir = tmpfile + sut.Network.CNIPluginDirs = []string{} + + // When + err = sut.Network.Validate() + + // Then + gomega.Expect(err).NotTo(gomega.BeNil()) + }) + + It("should fail on invalid CNIPluginDirs", func() { + validDirPath, err := ioutil.TempDir("", "config-empty") + if err != nil { + panic(err) + } + defer os.RemoveAll(validDirPath) + // Given + sut.Network.NetworkConfigDir = validDirPath + sut.Network.CNIPluginDirs = []string{invalidPath} + + // When + err = sut.Network.Validate() + + // Then + gomega.Expect(err).NotTo(gomega.BeNil()) + }) + + It("should fail in validating invalid PluginDir", func() { + validDirPath, err := ioutil.TempDir("", "config-empty") + if err != nil { + panic(err) + } + defer os.RemoveAll(validDirPath) + // Given + sut.Network.NetworkConfigDir = validDirPath + sut.Network.CNIPluginDirs = []string{invalidPath} + + // When + err = sut.Network.Validate() + + // Then + gomega.Expect(err).ToNot(gomega.BeNil()) + }) + + It("should fail on invalid CNIPluginDirs", func() { + validDirPath, err := ioutil.TempDir("", "config-empty") + if err != nil { + panic(err) + } + defer os.RemoveAll(validDirPath) + // Given + sut.Network.NetworkConfigDir = validDirPath + sut.Network.CNIPluginDirs = []string{invalidPath} + + // When + err = sut.Network.Validate() + + // Then + gomega.Expect(err).NotTo(gomega.BeNil()) + }) + + It("should fail during runtime", func() { + validDirPath, err := ioutil.TempDir("", "config-empty") + if err != nil { + panic(err) + } + defer os.RemoveAll(validDirPath) + // Given + sut.Network.NetworkConfigDir = validDirPath + tmpDir := path.Join(os.TempDir(), "cni-test") + sut.Network.CNIPluginDirs = []string{tmpDir} + defer os.RemoveAll(tmpDir) + + // When + err = sut.Network.Validate() + + // Then + gomega.Expect(err).ToNot(gomega.BeNil()) + }) + + It("should fail on invalid device mode", func() { + // Given + sut.Containers.Devices = []string{"/dev/null:/dev/null:abc"} + + // When + err := sut.Containers.Validate() + + // Then + gomega.Expect(err).NotTo(gomega.BeNil()) + }) + + It("should fail on invalid first device", func() { + // Given + sut.Containers.Devices = []string{"wrong:/dev/null:rw"} + + // When + err := sut.Containers.Validate() + + // Then + gomega.Expect(err).NotTo(gomega.BeNil()) + }) + + It("should fail on invalid second device", func() { + // Given + sut.Containers.Devices = []string{"/dev/null:wrong:rw"} + + // When + err := sut.Containers.Validate() + + // Then + gomega.Expect(err).NotTo(gomega.BeNil()) + }) + + It("should fail on invalid device", func() { + // Given + sut.Containers.Devices = []string{invalidPath} + + // When + err := sut.Containers.Validate() + + // Then + gomega.Expect(err).NotTo(gomega.BeNil()) + }) + + It("should fail on wrong invalid device specification", func() { + // Given + sut.Containers.Devices = []string{"::::"} + + // When + err := sut.Containers.Validate() + + // Then + gomega.Expect(err).NotTo(gomega.BeNil()) + }) + + It("should fail on wrong DefaultUlimits", func() { + // Given + sut.Containers.DefaultUlimits = []string{invalidPath} + + // When + err := sut.Containers.Validate() + + // Then + gomega.Expect(err).NotTo(gomega.BeNil()) + }) + + It("Expect Remote to be False", func() { + // Given + // When + config, err := NewConfig("") + // Then + gomega.Expect(err).To(gomega.BeNil()) + gomega.Expect(config.Engine.Remote).To(gomega.BeFalse()) + }) + +}) diff --git a/pkg/config/config_remote.go b/pkg/config/config_remote.go index 2dc938f68..d012dbd2f 100644 --- a/pkg/config/config_remote.go +++ b/pkg/config/config_remote.go @@ -8,6 +8,10 @@ func isDirectory(path string) error { return nil } +func isRemote() bool { + return true +} + func (c *EngineConfig) validatePaths() error { return nil } diff --git a/pkg/config/config_remote_test.go b/pkg/config/config_remote_test.go new file mode 100644 index 000000000..f5b1a2237 --- /dev/null +++ b/pkg/config/config_remote_test.go @@ -0,0 +1,142 @@ +// +build remote + +package config + +import ( + "io/ioutil" + "os" + + . "github.com/onsi/ginkgo" + "github.com/onsi/gomega" +) + +var _ = Describe("Config Remote", func() { + BeforeEach(beforeEach) + + It("should succeed on invalid CNIPluginDirs", func() { + validDirPath, err := ioutil.TempDir("", "config-empty") + if err != nil { + panic(err) + } + defer os.RemoveAll(validDirPath) + // Given + sut.Network.NetworkConfigDir = validDirPath + sut.Network.CNIPluginDirs = []string{invalidPath} + + // When + err = sut.Network.Validate() + + // Then + gomega.Expect(err).To(gomega.BeNil()) + }) + + It("should succeed on invalid device mode", func() { + // Given + sut.Containers.Devices = []string{"/dev/null:/dev/null:abc"} + + // When + err := sut.Containers.Validate() + + // Then + gomega.Expect(err).To(gomega.BeNil()) + }) + + It("should succeed on invalid first device", func() { + // Given + sut.Containers.Devices = []string{"wrong:/dev/null:rw"} + + // When + err := sut.Containers.Validate() + + // Then + gomega.Expect(err).To(gomega.BeNil()) + }) + + It("should succeed on invalid second device", func() { + // Given + sut.Containers.Devices = []string{"/dev/null:wrong:rw"} + + // When + err := sut.Containers.Validate() + + // Then + gomega.Expect(err).To(gomega.BeNil()) + }) + + It("should succeed on invalid device", func() { + // Given + sut.Containers.Devices = []string{invalidPath} + + // When + err := sut.Containers.Validate() + + // Then + gomega.Expect(err).To(gomega.BeNil()) + }) + + It("should succeed on wrong invalid device specification", func() { + // Given + sut.Containers.Devices = []string{"::::"} + + // When + err := sut.Containers.Validate() + + // Then + gomega.Expect(err).To(gomega.BeNil()) + }) + + It("Expect Remote to be true", func() { + // Given + // When + config, err := NewConfig("") + // Then + gomega.Expect(err).To(gomega.BeNil()) + gomega.Expect(config.Engine.Remote).To(gomega.BeTrue()) + }) + + It("should succeed on wrong DefaultUlimits", func() { + // Given + sut.Containers.DefaultUlimits = []string{invalidPath} + + // When + err := sut.Containers.Validate() + + // Then + gomega.Expect(err).To(gomega.BeNil()) + }) + + It("should succeed on invalid CNIPluginDirs", func() { + validDirPath, err := ioutil.TempDir("", "config-empty") + if err != nil { + panic(err) + } + defer os.RemoveAll(validDirPath) + // Given + sut.Network.NetworkConfigDir = validDirPath + sut.Network.CNIPluginDirs = []string{invalidPath} + + // When + err = sut.Network.Validate() + + // Then + gomega.Expect(err).To(gomega.BeNil()) + }) + + It("should succeed in validating invalid PluginDir", func() { + validDirPath, err := ioutil.TempDir("", "config-empty") + if err != nil { + panic(err) + } + defer os.RemoveAll(validDirPath) + // Given + sut.Network.NetworkConfigDir = validDirPath + sut.Network.CNIPluginDirs = []string{invalidPath} + + // When + err = sut.Network.Validate() + + // Then + gomega.Expect(err).To(gomega.BeNil()) + }) + +}) diff --git a/pkg/config/config_test.go b/pkg/config/config_test.go index d43b06380..0703725ef 100644 --- a/pkg/config/config_test.go +++ b/pkg/config/config_test.go @@ -1,9 +1,7 @@ package config import ( - "io/ioutil" "os" - "path" "sort" "strings" @@ -43,72 +41,6 @@ var _ = Describe("Config", func() { gomega.Expect(err).To(gomega.BeNil()) }) - It("should fail on wrong DefaultUlimits", func() { - // Given - sut.Containers.DefaultUlimits = []string{invalidPath} - - // When - err := sut.Containers.Validate() - - // Then - gomega.Expect(err).NotTo(gomega.BeNil()) - }) - - It("should fail on wrong invalid device specification", func() { - // Given - sut.Containers.Devices = []string{"::::"} - - // When - err := sut.Containers.Validate() - - // Then - gomega.Expect(err).NotTo(gomega.BeNil()) - }) - - It("should fail on invalid device", func() { - // Given - sut.Containers.Devices = []string{invalidPath} - - // When - err := sut.Containers.Validate() - - // Then - gomega.Expect(err).NotTo(gomega.BeNil()) - }) - - It("should fail on invalid device mode", func() { - // Given - sut.Containers.Devices = []string{"/dev/null:/dev/null:abc"} - - // When - err := sut.Containers.Validate() - - // Then - gomega.Expect(err).NotTo(gomega.BeNil()) - }) - - It("should fail on invalid first device", func() { - // Given - sut.Containers.Devices = []string{"wrong:/dev/null:rw"} - - // When - err := sut.Containers.Validate() - - // Then - gomega.Expect(err).NotTo(gomega.BeNil()) - }) - - It("should fail on invalid second device", func() { - // Given - sut.Containers.Devices = []string{"/dev/null:wrong:rw"} - - // When - err := sut.Containers.Validate() - - // Then - gomega.Expect(err).NotTo(gomega.BeNil()) - }) - It("should fail wrong max log size", func() { // Given sut.Containers.LogSizeMax = 1 @@ -175,77 +107,6 @@ var _ = Describe("Config", func() { // Then gomega.Expect(err).To(gomega.BeNil()) }) - - It("should fail during runtime", func() { - validDirPath, err := ioutil.TempDir("", "config-empty") - if err != nil { - panic(err) - } - defer os.RemoveAll(validDirPath) - // Given - sut.Network.NetworkConfigDir = validDirPath - tmpDir := path.Join(os.TempDir(), "cni-test") - sut.Network.CNIPluginDirs = []string{tmpDir} - defer os.RemoveAll(tmpDir) - - // When - err = sut.Network.Validate() - - // Then - gomega.Expect(err).ToNot(gomega.BeNil()) - }) - - It("should fail on invalid NetworkConfigDir", func() { - // Given - tmpfile := path.Join(os.TempDir(), "wrong-file") - file, err := os.Create(tmpfile) - gomega.Expect(err).To(gomega.BeNil()) - file.Close() - defer os.Remove(tmpfile) - sut.Network.NetworkConfigDir = tmpfile - sut.Network.CNIPluginDirs = []string{} - - // When - err = sut.Network.Validate() - - // Then - gomega.Expect(err).NotTo(gomega.BeNil()) - }) - - It("should fail on invalid CNIPluginDirs", func() { - validDirPath, err := ioutil.TempDir("", "config-empty") - if err != nil { - panic(err) - } - defer os.RemoveAll(validDirPath) - // Given - sut.Network.NetworkConfigDir = validDirPath - sut.Network.CNIPluginDirs = []string{invalidPath} - - // When - err = sut.Network.Validate() - - // Then - gomega.Expect(err).NotTo(gomega.BeNil()) - }) - - It("should fail in validating invalid PluginDir", func() { - validDirPath, err := ioutil.TempDir("", "config-empty") - if err != nil { - panic(err) - } - defer os.RemoveAll(validDirPath) - // Given - sut.Network.NetworkConfigDir = validDirPath - sut.Network.CNIPluginDirs = []string{invalidPath} - - // When - err = sut.Network.Validate() - - // Then - gomega.Expect(err).ToNot(gomega.BeNil()) - }) - }) Describe("readConfigFromFile", func() { @@ -522,4 +383,5 @@ var _ = Describe("Config", func() { gomega.Expect(err).ToNot(gomega.BeNil()) }) }) + }) diff --git a/pkg/config/default.go b/pkg/config/default.go index 055b135e2..fe523cbf5 100644 --- a/pkg/config/default.go +++ b/pkg/config/default.go @@ -239,6 +239,7 @@ func defaultConfigFromMemory() (*EngineConfig, error) { c.CgroupManager = defaultCgroupManager() c.StopTimeout = uint(10) + c.Remote = isRemote() c.OCIRuntimes = map[string][]string{ "runc": { "/usr/bin/runc",