container

package
v0.0.0-rc22 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 28, 2025 License: Apache-2.0 Imports: 25 Imported by: 0

README

This package runs both unit and integration tests. Integration tests against Kubernetes (using kind) are executed only if the environment variable TEST_KUBERNETES_INTEGRATION is set.

Prerequisites

  • Go (v1.18+)
  • Docker
  • kind (for Kubernetes integration tests)

Installing kind

To install kind, run:

go install sigs.k8s.io/kind@v0.26.0

Make sure your GOPATH/bin is in your PATH.

More information on kind can be found here.

Running Tests

Unit Tests

By default, integration tests are skipped. Run all tests with:

go test -v ./...

Integration Tests

To run integration tests:

  1. Set the environment variable:   export TEST_KUBERNETES_INTEGRATION=1
  2. Run the tests:   go test -v ./...

The integration tests will create a temporary kind cluster, perform the tests, and then clean up the cluster.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ContainerImpl

type ContainerImpl interface {
	Run(context.Context, ContainerOpts) (io.Reader, io.Reader, error)
	Pull(context.Context, string, types.ImagePullOptions) error
}

func NewContainerImpl

func NewContainerImpl(l log.Logger) (ContainerImpl, error)

type ContainerOpts

type ContainerOpts struct {
	Env           map[string]string
	LogOptions    map[string]string
	Namespace     string
	RestartPolicy corev1.RestartPolicy
	PodName       string
	Image         string
	LogDriver     string
	Network       string
	ContainerName string
	Volumes       []Volume
	Command       []string
	Args          []string
	Timeout       time.Duration
	AttachStdout  bool
	AttachStderr  bool
	AutoRemove    bool
}

func (*ContainerOpts) String

func (c *ContainerOpts) String() string

func (*ContainerOpts) ToDockerOpts

func (c *ContainerOpts) ToDockerOpts() DockerOpts

func (*ContainerOpts) ToK8sOpts

func (c *ContainerOpts) ToK8sOpts() K8sOpts

type DockerClient

type DockerClient interface {
	ImageInspectWithRaw(ctx context.Context, image string) (types.ImageInspect, []byte, error)
	ImagePull(ctx context.Context, ref string, options types.ImagePullOptions) (io.ReadCloser, error)
	ContainerCreate(ctx context.Context, config *container.Config, hostConfig *container.HostConfig,
		networkingConfig *network.NetworkingConfig, platform *v1.Platform, containerName string) (container.CreateResponse, error)
	ContainerStart(ctx context.Context, containerID string, options container.StartOptions) error
	ContainerAttach(ctx context.Context, containerID string, options container.AttachOptions) (types.HijackedResponse, error)
	ContainerWait(ctx context.Context, containerID string, condition container.WaitCondition) (<-chan container.WaitResponse, <-chan error)
	Ping(ctx context.Context) (types.Ping, error)
	ContainerKill(ctx context.Context, containerID, signal string) error
}

type DockerOpts

type DockerOpts struct {
	RestartPolicy container.RestartPolicy
	Mounts        []mount.Mount
	LogDriver     string
	LogOptions    map[string]string
	Image         string
	Network       string
	ContainerName string
	Command       []string
	Env           []string
	Timeout       time.Duration
	AutoRemove    bool
	AttachStdout  bool
	AttachStderr  bool
}

func (DockerOpts) String

func (o DockerOpts) String() string

type DockerWrapper

type DockerWrapper struct {
	// contains filtered or unexported fields
}

func NewDockerWrapper

func NewDockerWrapper(logger log.Logger) (*DockerWrapper, error)

func (*DockerWrapper) Pull

func (d *DockerWrapper) Pull(ctx context.Context, image string, opts types.ImagePullOptions) error

func (*DockerWrapper) Run

func (d *DockerWrapper) Run(ctx context.Context, opts ContainerOpts) (stdout io.Reader, stderr io.Reader, err error)

func (*DockerWrapper) RunContainer

func (d *DockerWrapper) RunContainer(ctx context.Context, opts DockerOpts) (stdout io.Reader, stderr io.Reader, err error)

type K8sOpts

type K8sOpts struct {
	Namespace     string
	PodName       string
	Image         string
	RestartPolicy corev1.RestartPolicy
	Command       []string
	Args          []string
	Env           []corev1.EnvVar
	Volumes       []corev1.Volume
	VolumeMounts  []corev1.VolumeMount
	Timeout       time.Duration
}

func (K8sOpts) String

func (k K8sOpts) String() string

type K8sWrapper

type K8sWrapper struct {
	// contains filtered or unexported fields
}

func NewK8sWrapper

func NewK8sWrapper() (*K8sWrapper, error)

func NewK8sWrapperFromKubeconfig

func NewK8sWrapperFromKubeconfig(kubeconfigPath string) (*K8sWrapper, error)

func (*K8sWrapper) Pull

func (*K8sWrapper) Run

func (w *K8sWrapper) Run(ctx context.Context, opts ContainerOpts) (io.Reader, io.Reader, error)

func (*K8sWrapper) RunPod

func (w *K8sWrapper) RunPod(ctx context.Context, opts K8sOpts) (*bytes.Buffer, error)

type KubernetesClient

type KubernetesClient interface {
	CreatePod(ctx context.Context, namespace string, pod *corev1.Pod) (*corev1.Pod, error)
	GetPodLogs(ctx context.Context, namespace, podName, containerName string, opts *corev1.PodLogOptions) (io.ReadCloser, error)
	GetPod(ctx context.Context, namespace, podName string) (*corev1.Pod, error)
	DeletePod(ctx context.Context, namespace, podName string, opts *metav1.DeleteOptions) error
}

type MockDockerClient

type MockDockerClient struct {
	mock.Mock
}

func (*MockDockerClient) ContainerAttach

func (m *MockDockerClient) ContainerAttach(ctx context.Context, containerID string, options container.AttachOptions) (types.HijackedResponse, error)

func (*MockDockerClient) ContainerCreate

func (m *MockDockerClient) ContainerCreate(ctx context.Context, config *container.Config, hostConfig *container.HostConfig,
	networkingConfig *network.NetworkingConfig, platform *v1.Platform, containerName string,
) (container.CreateResponse, error)

func (*MockDockerClient) ContainerKill

func (m *MockDockerClient) ContainerKill(ctx context.Context, containerID, signal string) error

func (*MockDockerClient) ContainerStart

func (m *MockDockerClient) ContainerStart(ctx context.Context, containerID string, options container.StartOptions) error

func (*MockDockerClient) ContainerWait

func (m *MockDockerClient) ContainerWait(ctx context.Context, containerID string, condition container.WaitCondition) (<-chan container.WaitResponse, <-chan error)

func (*MockDockerClient) ImageInspectWithRaw

func (m *MockDockerClient) ImageInspectWithRaw(ctx context.Context, image string) (types.ImageInspect, []byte, error)

func (*MockDockerClient) ImagePull

func (m *MockDockerClient) ImagePull(ctx context.Context, ref string, options types.ImagePullOptions) (io.ReadCloser, error)

func (*MockDockerClient) Ping

func (m *MockDockerClient) Ping(ctx context.Context) (types.Ping, error)

type MockKubernetesClient

type MockKubernetesClient struct {
	mock.Mock
}

func (*MockKubernetesClient) CreatePod

func (m *MockKubernetesClient) CreatePod(ctx context.Context, namespace string, pod *corev1.Pod) (*corev1.Pod, error)

func (*MockKubernetesClient) DeletePod

func (m *MockKubernetesClient) DeletePod(ctx context.Context, namespace, podName string, opts *metav1.DeleteOptions) error

func (*MockKubernetesClient) GetPod

func (m *MockKubernetesClient) GetPod(ctx context.Context, namespace, podName string) (*corev1.Pod, error)

func (*MockKubernetesClient) GetPodLogs

func (m *MockKubernetesClient) GetPodLogs(ctx context.Context, namespace, podName, containerName string, opts *corev1.PodLogOptions) (io.ReadCloser, error)

type Volume

type Volume struct {
	Name          string
	HostPath      string
	ContainerPath string
	VolumeType    string
	ReadOnly      bool
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL