Skip to content

Commit

Permalink
[system/container] Add container sub-package with methods for checkin…
Browse files Browse the repository at this point in the history
…g container engine info
  • Loading branch information
andyone committed Feb 7, 2023
1 parent 90f5376 commit 6fbb762
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 0 deletions.
47 changes: 47 additions & 0 deletions system/container/container.go
Original file line number Diff line number Diff line change
@@ -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 <https://www.apache.org/licenses/LICENSE-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)
}
48 changes: 48 additions & 0 deletions system/container/container_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package container

// ////////////////////////////////////////////////////////////////////////////////// //
// //
// Copyright (c) 2023 ESSENTIAL KAOS //
// Apache License, Version 2.0 <https://www.apache.org/licenses/LICENSE-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)
}

0 comments on commit 6fbb762

Please sign in to comment.