k3s/test/e2e_node/docker_util.go

147 lines
4.0 KiB
Go
Raw Normal View History

/*
Copyright 2017 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package e2e_node
import (
"context"
"fmt"
"github.com/blang/semver"
systemdutil "github.com/coreos/go-systemd/util"
Skip log path tests when they are expected to fail. The log path test is not expected to pass unless the Docker is using the JSON logging driver, since that's what the log path is trying to find. When Docker is using the journald logging driver, there will be no JSON files in the logging directories for it to find. Furthermore, when SELinux support is enabled in the Docker daemon, SELinux will prevent processes running inside Docker containers from accessing the log files owned by Docker (which is what this test is trying to accomplish), so let's also skip this test in case SELinux support is enabled. Tested: - With Docker daemon started using --log-driver=journald: S [SKIPPING] in Spec Setup (BeforeEach) [8.193 seconds] [k8s.io] ContainerLogPath Pod with a container printed log to stdout should print log to correct log path [BeforeEach] Jan 3 18:33:44.869: Skipping because Docker daemon is using a logging driver other than "json-file": journald - With Docker daemon started using --selinux-enabled: S [SKIPPING] in Spec Setup (BeforeEach) [8.488 seconds] [k8s.io] ContainerLogPath Pod with a container printed log to stdout should print log to correct log path [BeforeEach] Jan 3 18:35:58.909: Skipping because Docker daemon is running with SELinux support enabled - With Docker started using JSON logging driver and with SELinux disabled: • [SLOW TEST:16.352 seconds] (passed) [k8s.io] ContainerLogPath Pod with a container printed log to stdout should print log to correct log path Ran 1 of 256 Specs in 36.428 seconds SUCCESS! -- 1 Passed | 0 Failed | 0 Pending | 255 Skipped
2018-01-03 18:09:46 +00:00
"github.com/docker/docker/api/types"
"github.com/docker/docker/client"
)
const (
defaultDockerEndpoint = "unix:///var/run/docker.sock"
)
// getDockerAPIVersion returns the Docker's API version.
func getDockerAPIVersion() (semver.Version, error) {
c, err := client.NewClient(defaultDockerEndpoint, "", nil, nil)
if err != nil {
return semver.Version{}, fmt.Errorf("failed to create docker client: %v", err)
}
version, err := c.ServerVersion(context.Background())
if err != nil {
return semver.Version{}, fmt.Errorf("failed to get docker server version: %v", err)
}
return semver.MustParse(version.APIVersion + ".0"), nil
}
// isSharedPIDNamespaceSupported returns true if the Docker version is 1.13.1+
// (API version 1.26+), and false otherwise.
func isSharedPIDNamespaceSupported() (bool, error) {
version, err := getDockerAPIVersion()
if err != nil {
return false, err
}
return version.GTE(semver.MustParse("1.26.0")), nil
}
// isDockerNoNewPrivilegesSupported returns true if Docker version is 1.11+
// (API version 1.23+), and false otherwise.
func isDockerNoNewPrivilegesSupported() (bool, error) {
version, err := getDockerAPIVersion()
if err != nil {
return false, err
}
return version.GTE(semver.MustParse("1.23.0")), nil
}
// isDockerLiveRestoreSupported returns true if live-restore is supported in
// the current Docker version.
func isDockerLiveRestoreSupported() (bool, error) {
version, err := getDockerAPIVersion()
if err != nil {
return false, err
}
return version.GTE(semver.MustParse("1.26.0")), nil
}
Skip log path tests when they are expected to fail. The log path test is not expected to pass unless the Docker is using the JSON logging driver, since that's what the log path is trying to find. When Docker is using the journald logging driver, there will be no JSON files in the logging directories for it to find. Furthermore, when SELinux support is enabled in the Docker daemon, SELinux will prevent processes running inside Docker containers from accessing the log files owned by Docker (which is what this test is trying to accomplish), so let's also skip this test in case SELinux support is enabled. Tested: - With Docker daemon started using --log-driver=journald: S [SKIPPING] in Spec Setup (BeforeEach) [8.193 seconds] [k8s.io] ContainerLogPath Pod with a container printed log to stdout should print log to correct log path [BeforeEach] Jan 3 18:33:44.869: Skipping because Docker daemon is using a logging driver other than "json-file": journald - With Docker daemon started using --selinux-enabled: S [SKIPPING] in Spec Setup (BeforeEach) [8.488 seconds] [k8s.io] ContainerLogPath Pod with a container printed log to stdout should print log to correct log path [BeforeEach] Jan 3 18:35:58.909: Skipping because Docker daemon is running with SELinux support enabled - With Docker started using JSON logging driver and with SELinux disabled: • [SLOW TEST:16.352 seconds] (passed) [k8s.io] ContainerLogPath Pod with a container printed log to stdout should print log to correct log path Ran 1 of 256 Specs in 36.428 seconds SUCCESS! -- 1 Passed | 0 Failed | 0 Pending | 255 Skipped
2018-01-03 18:09:46 +00:00
// getDockerInfo returns the Info struct for the running Docker daemon.
func getDockerInfo() (types.Info, error) {
var info types.Info
c, err := client.NewClient(defaultDockerEndpoint, "", nil, nil)
if err != nil {
return info, fmt.Errorf("failed to create docker client: %v", err)
}
info, err = c.Info(context.Background())
if err != nil {
return info, fmt.Errorf("failed to get docker info: %v", err)
}
return info, nil
}
// isDockerLiveRestoreEnabled returns true if live-restore is enabled in the
// Docker.
func isDockerLiveRestoreEnabled() (bool, error) {
Skip log path tests when they are expected to fail. The log path test is not expected to pass unless the Docker is using the JSON logging driver, since that's what the log path is trying to find. When Docker is using the journald logging driver, there will be no JSON files in the logging directories for it to find. Furthermore, when SELinux support is enabled in the Docker daemon, SELinux will prevent processes running inside Docker containers from accessing the log files owned by Docker (which is what this test is trying to accomplish), so let's also skip this test in case SELinux support is enabled. Tested: - With Docker daemon started using --log-driver=journald: S [SKIPPING] in Spec Setup (BeforeEach) [8.193 seconds] [k8s.io] ContainerLogPath Pod with a container printed log to stdout should print log to correct log path [BeforeEach] Jan 3 18:33:44.869: Skipping because Docker daemon is using a logging driver other than "json-file": journald - With Docker daemon started using --selinux-enabled: S [SKIPPING] in Spec Setup (BeforeEach) [8.488 seconds] [k8s.io] ContainerLogPath Pod with a container printed log to stdout should print log to correct log path [BeforeEach] Jan 3 18:35:58.909: Skipping because Docker daemon is running with SELinux support enabled - With Docker started using JSON logging driver and with SELinux disabled: • [SLOW TEST:16.352 seconds] (passed) [k8s.io] ContainerLogPath Pod with a container printed log to stdout should print log to correct log path Ran 1 of 256 Specs in 36.428 seconds SUCCESS! -- 1 Passed | 0 Failed | 0 Pending | 255 Skipped
2018-01-03 18:09:46 +00:00
info, err := getDockerInfo()
if err != nil {
Skip log path tests when they are expected to fail. The log path test is not expected to pass unless the Docker is using the JSON logging driver, since that's what the log path is trying to find. When Docker is using the journald logging driver, there will be no JSON files in the logging directories for it to find. Furthermore, when SELinux support is enabled in the Docker daemon, SELinux will prevent processes running inside Docker containers from accessing the log files owned by Docker (which is what this test is trying to accomplish), so let's also skip this test in case SELinux support is enabled. Tested: - With Docker daemon started using --log-driver=journald: S [SKIPPING] in Spec Setup (BeforeEach) [8.193 seconds] [k8s.io] ContainerLogPath Pod with a container printed log to stdout should print log to correct log path [BeforeEach] Jan 3 18:33:44.869: Skipping because Docker daemon is using a logging driver other than "json-file": journald - With Docker daemon started using --selinux-enabled: S [SKIPPING] in Spec Setup (BeforeEach) [8.488 seconds] [k8s.io] ContainerLogPath Pod with a container printed log to stdout should print log to correct log path [BeforeEach] Jan 3 18:35:58.909: Skipping because Docker daemon is running with SELinux support enabled - With Docker started using JSON logging driver and with SELinux disabled: • [SLOW TEST:16.352 seconds] (passed) [k8s.io] ContainerLogPath Pod with a container printed log to stdout should print log to correct log path Ran 1 of 256 Specs in 36.428 seconds SUCCESS! -- 1 Passed | 0 Failed | 0 Pending | 255 Skipped
2018-01-03 18:09:46 +00:00
return false, err
}
Skip log path tests when they are expected to fail. The log path test is not expected to pass unless the Docker is using the JSON logging driver, since that's what the log path is trying to find. When Docker is using the journald logging driver, there will be no JSON files in the logging directories for it to find. Furthermore, when SELinux support is enabled in the Docker daemon, SELinux will prevent processes running inside Docker containers from accessing the log files owned by Docker (which is what this test is trying to accomplish), so let's also skip this test in case SELinux support is enabled. Tested: - With Docker daemon started using --log-driver=journald: S [SKIPPING] in Spec Setup (BeforeEach) [8.193 seconds] [k8s.io] ContainerLogPath Pod with a container printed log to stdout should print log to correct log path [BeforeEach] Jan 3 18:33:44.869: Skipping because Docker daemon is using a logging driver other than "json-file": journald - With Docker daemon started using --selinux-enabled: S [SKIPPING] in Spec Setup (BeforeEach) [8.488 seconds] [k8s.io] ContainerLogPath Pod with a container printed log to stdout should print log to correct log path [BeforeEach] Jan 3 18:35:58.909: Skipping because Docker daemon is running with SELinux support enabled - With Docker started using JSON logging driver and with SELinux disabled: • [SLOW TEST:16.352 seconds] (passed) [k8s.io] ContainerLogPath Pod with a container printed log to stdout should print log to correct log path Ran 1 of 256 Specs in 36.428 seconds SUCCESS! -- 1 Passed | 0 Failed | 0 Pending | 255 Skipped
2018-01-03 18:09:46 +00:00
return info.LiveRestoreEnabled, nil
}
// getDockerLoggingDriver returns the name of the logging driver.
func getDockerLoggingDriver() (string, error) {
info, err := getDockerInfo()
if err != nil {
Skip log path tests when they are expected to fail. The log path test is not expected to pass unless the Docker is using the JSON logging driver, since that's what the log path is trying to find. When Docker is using the journald logging driver, there will be no JSON files in the logging directories for it to find. Furthermore, when SELinux support is enabled in the Docker daemon, SELinux will prevent processes running inside Docker containers from accessing the log files owned by Docker (which is what this test is trying to accomplish), so let's also skip this test in case SELinux support is enabled. Tested: - With Docker daemon started using --log-driver=journald: S [SKIPPING] in Spec Setup (BeforeEach) [8.193 seconds] [k8s.io] ContainerLogPath Pod with a container printed log to stdout should print log to correct log path [BeforeEach] Jan 3 18:33:44.869: Skipping because Docker daemon is using a logging driver other than "json-file": journald - With Docker daemon started using --selinux-enabled: S [SKIPPING] in Spec Setup (BeforeEach) [8.488 seconds] [k8s.io] ContainerLogPath Pod with a container printed log to stdout should print log to correct log path [BeforeEach] Jan 3 18:35:58.909: Skipping because Docker daemon is running with SELinux support enabled - With Docker started using JSON logging driver and with SELinux disabled: • [SLOW TEST:16.352 seconds] (passed) [k8s.io] ContainerLogPath Pod with a container printed log to stdout should print log to correct log path Ran 1 of 256 Specs in 36.428 seconds SUCCESS! -- 1 Passed | 0 Failed | 0 Pending | 255 Skipped
2018-01-03 18:09:46 +00:00
return "", err
}
Skip log path tests when they are expected to fail. The log path test is not expected to pass unless the Docker is using the JSON logging driver, since that's what the log path is trying to find. When Docker is using the journald logging driver, there will be no JSON files in the logging directories for it to find. Furthermore, when SELinux support is enabled in the Docker daemon, SELinux will prevent processes running inside Docker containers from accessing the log files owned by Docker (which is what this test is trying to accomplish), so let's also skip this test in case SELinux support is enabled. Tested: - With Docker daemon started using --log-driver=journald: S [SKIPPING] in Spec Setup (BeforeEach) [8.193 seconds] [k8s.io] ContainerLogPath Pod with a container printed log to stdout should print log to correct log path [BeforeEach] Jan 3 18:33:44.869: Skipping because Docker daemon is using a logging driver other than "json-file": journald - With Docker daemon started using --selinux-enabled: S [SKIPPING] in Spec Setup (BeforeEach) [8.488 seconds] [k8s.io] ContainerLogPath Pod with a container printed log to stdout should print log to correct log path [BeforeEach] Jan 3 18:35:58.909: Skipping because Docker daemon is running with SELinux support enabled - With Docker started using JSON logging driver and with SELinux disabled: • [SLOW TEST:16.352 seconds] (passed) [k8s.io] ContainerLogPath Pod with a container printed log to stdout should print log to correct log path Ran 1 of 256 Specs in 36.428 seconds SUCCESS! -- 1 Passed | 0 Failed | 0 Pending | 255 Skipped
2018-01-03 18:09:46 +00:00
return info.LoggingDriver, nil
}
// isDockerSELinuxSupportEnabled checks whether the Docker daemon was started
// with SELinux support enabled.
func isDockerSELinuxSupportEnabled() (bool, error) {
info, err := getDockerInfo()
if err != nil {
return false, err
}
for _, s := range info.SecurityOptions {
if s == "selinux" {
return true, nil
}
}
return false, nil
}
2017-09-26 11:49:56 +00:00
// startDockerDaemon starts the Docker daemon.
func startDockerDaemon() error {
switch {
case systemdutil.IsRunningSystemd():
_, err := runCommand("systemctl", "start", "docker")
return err
default:
_, err := runCommand("service", "docker", "start")
return err
}
}
// stopDockerDaemon stops the Docker daemon.
func stopDockerDaemon() error {
switch {
case systemdutil.IsRunningSystemd():
_, err := runCommand("systemctl", "stop", "docker")
return err
default:
_, err := runCommand("service", "docker", "stop")
return err
}
}