From 6fbb76296fb3f8980c16b59bf14be345e3b1eeda Mon Sep 17 00:00:00 2001 From: Anton Novojilov Date: Tue, 7 Feb 2023 14:53:41 +0300 Subject: [PATCH] [system/container] Add container sub-package with methods for checking container engine info --- system/container/container.go | 47 +++++++++++++++++++++++++++++ system/container/container_test.go | 48 ++++++++++++++++++++++++++++++ 2 files changed, 95 insertions(+) create mode 100644 system/container/container.go create mode 100644 system/container/container_test.go diff --git a/system/container/container.go b/system/container/container.go new file mode 100644 index 00000000..2b3cb8c9 --- /dev/null +++ b/system/container/container.go @@ -0,0 +1,47 @@ +// Package container provides methods for checking container engine info +package container + +// ////////////////////////////////////////////////////////////////////////////////// // +// // +// Copyright (c) 2023 ESSENTIAL KAOS // +// Apache License, Version 2.0 // +// // +// ////////////////////////////////////////////////////////////////////////////////// // + +import ( + "os" +) + +// ////////////////////////////////////////////////////////////////////////////////// // + +const ( + DOCKER = "docker" // Docker + PODMAN = "podman" // Podman +) + +// ////////////////////////////////////////////////////////////////////////////////// // + +var dockerEnv = "/.dockerenv" +var podmanEnv = "/run/.containerenv" + +// ////////////////////////////////////////////////////////////////////////////////// // + +// GetEngine returns container engine name if used +func GetEngine() string { + switch { + case isFileExist(dockerEnv): + return DOCKER + case isFileExist(podmanEnv): + return PODMAN + } + + return "" +} + +// ////////////////////////////////////////////////////////////////////////////////// // + +// isFileExist returns true if given file exist +func isFileExist(file string) bool { + _, err := os.Stat(file) + return !os.IsNotExist(err) +} diff --git a/system/container/container_test.go b/system/container/container_test.go new file mode 100644 index 00000000..9987603d --- /dev/null +++ b/system/container/container_test.go @@ -0,0 +1,48 @@ +package container + +// ////////////////////////////////////////////////////////////////////////////////// // +// // +// Copyright (c) 2023 ESSENTIAL KAOS // +// Apache License, Version 2.0 // +// // +// ////////////////////////////////////////////////////////////////////////////////// // + +import ( + "os" + "testing" + + . "github.com/essentialkaos/check" +) + +// ////////////////////////////////////////////////////////////////////////////////// // + +func Test(t *testing.T) { TestingT(t) } + +type ContainerSuite struct{} + +// ////////////////////////////////////////////////////////////////////////////////// // + +var _ = Suite(&ContainerSuite{}) + +// ////////////////////////////////////////////////////////////////////////////////// // + +func (s *ContainerSuite) TestGetEngine(c *C) { + testEnv := c.MkDir() + "/test" + + dockerEnv = "/_unknown_" + podmanEnv = "/_unknown_" + + c.Assert(GetEngine(), Equals, "") + + os.WriteFile(testEnv, []byte("TEST"), 0644) + + dockerEnv = testEnv + podmanEnv = "/_unknown_" + + c.Assert(GetEngine(), Equals, DOCKER) + + dockerEnv = "/_unknown_" + podmanEnv = testEnv + + c.Assert(GetEngine(), Equals, PODMAN) +}