testutils

package module
v0.86.0 Latest Latest
Warning

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

Go to latest
Published: Nov 20, 2026 License: Apache-2.0 Imports: 6 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package testutils provides common test helpers for testing Single Step Instrumentation in Kubernetes.

Index

Constants

View Source
const (
	// EffectiveInjectionModeAnnotation records the injection mode actually used by the webhook.
	EffectiveInjectionModeAnnotation = "internal.apm.datadoghq.com/effective-injection-mode"
	// CSIDriverStatusAnnotation records the observed Datadog CSI driver state.
	CSIDriverStatusAnnotation = "internal.apm.datadoghq.com/csi-driver-status"
	// InjectionStatusAnnotation records the overall outcome of the injection attempt.
	InjectionStatusAnnotation = "internal.apm.datadoghq.com/injection-status"
	// InjectedLibrariesAnnotation records the JSON array of components the webhook attempted to inject.
	InjectedLibrariesAnnotation = "internal.apm.datadoghq.com/injected-libraries"
	// AppliedConfigAnnotation is the JSON of the configuration selected for injection.
	AppliedConfigAnnotation = "internal.apm.datadoghq.com/applied-config"
	// InjectionSourceAnnotation identifies the source that selected the libraries.
	InjectionSourceAnnotation = "internal.apm.datadoghq.com/injection-source"
)

Annotation keys written by the admission webhook to record the outcome of an injection attempt. They are duplicated here (rather than imported) because the source of truth in pkg/clusteragent/admission/mutate/autoinstrumentation/annotation is built behind the "kubeapiserver" build tag, which these test utilities must not require.

View Source
const (
	// CSIDriverStatusAPMEnabled means the Datadog CSI driver is installed and advertises APM SSI support.
	CSIDriverStatusAPMEnabled = "apm-enabled"
	// CSIDriverStatusAPMDisabled means the driver is installed but APM SSI support is not advertised.
	CSIDriverStatusAPMDisabled = "apm-disabled"
	// CSIDriverStatusNotInstalled means the Datadog CSI driver object was not found in the cluster.
	CSIDriverStatusNotInstalled = "not-installed"
)

CSIDriverStatus annotation values (kept in sync with the annotation package).

View Source
const (
	// InjectionStatusInjected means the injector and all requested libraries were successfully injected.
	InjectionStatusInjected = "injected"
	// InjectionStatusPartial means the injector succeeded but at least one library was skipped.
	InjectionStatusPartial = "partial"
	// InjectionStatusSkipped means injection was skipped before it started.
	InjectionStatusSkipped = "skipped"
	// InjectionStatusError means a fatal error prevented the injector from running.
	InjectionStatusError = "error"
	// InjectionStatusBlocked means a remote-config policy denied injection.
	InjectionStatusBlocked = "blocked"
)

InjectionStatus annotation values (kept in sync with the annotation package).

View Source
const K8sAutoscalerSafeToEvictVolumesAnnotation = "cluster-autoscaler.kubernetes.io/safe-to-evict-local-volumes"

K8sAutoscalerSafeToEvictVolumesAnnotation is the annotation used by the Kubernetes cluster-autoscaler to mark a volume as safe to evict.

Variables

This section is empty.

Functions

func EffectiveAutoMode added in v0.82.0

func EffectiveAutoMode(resolved InjectionMode) string

EffectiveAutoMode returns the value the webhook records in the effective-injection-mode annotation when "auto" mode resolves to the given concrete mode (e.g. EffectiveAutoMode(InjectionModeCSI) == "csi (auto)").

Types

type ContainerInjectionValidator added in v0.77.0

type ContainerInjectionValidator interface {
	RequireContainerInjection(t *testing.T, container *ContainerValidator)
	RequireNoContainerInjection(t *testing.T, container *ContainerValidator)
}

ContainerInjectionValidator validates container-level injection.

type ContainerValidator

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

ContainerValidator provides a test friendly structure to run assertions on container states for SSI.

func NewContainerValidator

func NewContainerValidator(container *corev1.Container, injection ContainerInjectionValidator) *ContainerValidator

NewContainerValidator initializes a container validator and converts the Kubernetes spec into a test friendly struct.

func (*ContainerValidator) RequireCommand

func (v *ContainerValidator) RequireCommand(t *testing.T, expected string)

RequireCommand ensures the provided command line is the one present in the container. The validator converts the command with args list into a string for your convenience.

func (*ContainerValidator) RequireEnvs

func (v *ContainerValidator) RequireEnvs(t *testing.T, expected map[string]string)

RequireEnvs ensures the map of key/value pairs both exist and are set to the expected value.

func (*ContainerValidator) RequireInjection

func (v *ContainerValidator) RequireInjection(t *testing.T)

RequireInjection ensures a container was injected for SSI. It's a high level function that should be modified if the implementation or meaning of injection changes over time.

func (*ContainerValidator) RequireMissingEnvs

func (v *ContainerValidator) RequireMissingEnvs(t *testing.T, missing []string)

RequireMissingEnvs ensures the list of keys are missing in the container.

func (*ContainerValidator) RequireMissingVolumeMounts

func (v *ContainerValidator) RequireMissingVolumeMounts(t *testing.T, missing []corev1.VolumeMount)

RequireMissingVolumeMounts ensures the list of provided volume mounts do not exist in the container.

func (*ContainerValidator) RequireNoInjection

func (v *ContainerValidator) RequireNoInjection(t *testing.T)

RequireNoInjection ensures a container was not injected for SSI, including KPI env vars.

func (*ContainerValidator) RequireNoInjectionArtifacts added in v0.80.0

func (v *ContainerValidator) RequireNoInjectionArtifacts(t *testing.T)

RequireNoInjectionArtifacts ensures a container has no SSI injection artifacts.

func (*ContainerValidator) RequireNoVolumeMounts

func (v *ContainerValidator) RequireNoVolumeMounts(t *testing.T)

RequireNoVolumeMounts ensures the container has no volume mounts.

func (*ContainerValidator) RequireResourceRequirements

func (v *ContainerValidator) RequireResourceRequirements(t *testing.T, expected *corev1.ResourceRequirements)

RequireResourceRequirements ensures the resource requirements for the container match the expected.

func (*ContainerValidator) RequireSecurityContext

func (v *ContainerValidator) RequireSecurityContext(t *testing.T, expected *corev1.SecurityContext)

RequireSecurityContext ensures the provided security context matches the security context on the container.

func (*ContainerValidator) RequireUnmutated added in v0.76.0

func (v *ContainerValidator) RequireUnmutated(t *testing.T)

RequireUnmutated ensures no Datadog-related environment variables exist in the container. This includes any env var starting with "DD_" or "LD_PRELOAD".

func (*ContainerValidator) RequireVolumeMounts

func (v *ContainerValidator) RequireVolumeMounts(t *testing.T, expected []corev1.VolumeMount)

RequireVolumeMounts ensures the list of provided volume mounts exist in the container.

type ImageValidator

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

ImageValidator provides a validation struct for container images.

func NewImageValidator

func NewImageValidator(i string) *ImageValidator

NewImageValidator takes an image string provided from a container spec and initializes a new ImageValidator.

func (*ImageValidator) RequireName

func (v *ImageValidator) RequireName(t *testing.T, expected string)

RequireName ensures the image has the expected image name.

func (*ImageValidator) RequireRegistry

func (v *ImageValidator) RequireRegistry(t *testing.T, expected string)

RequireRegistry ensures the image has the expected registry.

func (*ImageValidator) RequireTag

func (v *ImageValidator) RequireTag(t *testing.T, expected string)

RequireTag ensures the image has the expected tag/version.

type InjectionMode added in v0.77.0

type InjectionMode string

InjectionMode represents the method used to inject APM libraries into pods.

const (
	// InjectionModeAuto lets the webhook pick the injection method.
	InjectionModeAuto InjectionMode = "auto"
	// InjectionModeInitContainer uses init containers to copy library files.
	InjectionModeInitContainer InjectionMode = "init_container"
	// InjectionModeCSI uses the Datadog CSI driver to mount library files.
	InjectionModeCSI InjectionMode = "csi"
	// InjectionModeImageVolume uses image volumes to mount library files.
	InjectionModeImageVolume InjectionMode = "image_volume"
)

type InjectionValidator added in v0.77.0

type InjectionValidator interface {
	ContainerInjectionValidator
	// RequireInjection validates mode-specific pod-level injection (volumes, annotations, init containers).
	RequireInjection(t *testing.T)
	// RequireNoInjection validates that no mode-specific injection artifacts exist.
	RequireNoInjection(t *testing.T)
	// RequireLibraryVersions validates the injected library versions.
	RequireLibraryVersions(t *testing.T, expected map[string]string)
	// RequireInjectorVersion validates the injector version.
	RequireInjectorVersion(t *testing.T, expected string)
}

InjectionValidator validates injection-specific aspects of a pod. It also implements ContainerInjectionValidator for container-level validation.

type PodValidator

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

PodValidator provides a test friendly structure to run assertions on pod states for SSI.

func NewPodValidator

func NewPodValidator(pod *corev1.Pod, mode InjectionMode) *PodValidator

NewPodValidator initializes a new PodValidator from a Kubernetes pod spec. It creates container validators for every container and init container in the pod. With InjectionModeAuto, the mode is read from the effective-injection-mode annotation, and defaults to init containers when the annotation is absent.

func (*PodValidator) RequireAnnotations

func (v *PodValidator) RequireAnnotations(t *testing.T, expected map[string]string)

RequireAnnotations ensures the pod has the expected annotations keys and that the values match the expected value.

func (*PodValidator) RequireAppliedConfigName added in v0.86.0

func (v *PodValidator) RequireAppliedConfigName(t *testing.T, expected string)

RequireAppliedConfigName ensures applied-config is set and its JSON name matches expected.

func (*PodValidator) RequireCSIDriverStatus added in v0.82.0

func (v *PodValidator) RequireCSIDriverStatus(t *testing.T, expected string)

RequireCSIDriverStatus ensures the webhook recorded the given Datadog CSI driver status on the pod. Use the CSIDriverStatus* constants for the expected value.

func (*PodValidator) RequireEffectiveInjectionMode added in v0.82.0

func (v *PodValidator) RequireEffectiveInjectionMode(t *testing.T, expected string)

RequireEffectiveInjectionMode ensures the webhook recorded the given resolved injection mode on the pod. The expected value is the raw annotation value: the bare mode for explicit modes (e.g. "csi") and the resolved mode suffixed with " (auto)" when "auto" mode was used (use EffectiveAutoMode to build it).

func (*PodValidator) RequireEnvs

func (v *PodValidator) RequireEnvs(t *testing.T, expected map[string]string, expectedContainers []string)

RequireEnvs ensures the expected env vars exist in the expected containers with the expected values.

func (*PodValidator) RequireInitContainerImages

func (v *PodValidator) RequireInitContainerImages(t *testing.T, expected []string)

RequireInitContainerImages ensures the list of init container image strings matches the expected list.

func (*PodValidator) RequireInitResourceRequirements

func (v *PodValidator) RequireInitResourceRequirements(t *testing.T, expected *corev1.ResourceRequirements)

RequireInitResourceRequirements ensures all Datadog init containers in the pod have the expected resource requirements.

func (*PodValidator) RequireInitSecurityContext

func (v *PodValidator) RequireInitSecurityContext(t *testing.T, expected *corev1.SecurityContext)

RequireInitSecurityContext ensures all Datadog init containers in the pod have the expected security context.

func (*PodValidator) RequireInjectedLibraries added in v0.82.0

func (v *PodValidator) RequireInjectedLibraries(t *testing.T, expected map[string]string)

RequireInjectedLibraries ensures the injected-libraries annotation lists exactly the expected component-name -> status pairs (e.g. {"injector":"injected","python":"injected"}). Image references are intentionally not asserted as they depend on the resolved registry and digest.

func (*PodValidator) RequireInjection

func (v *PodValidator) RequireInjection(t *testing.T, expectedContainers []string)

RequireInjection is a high level function that ensures injection has occurred for the pod and expected containers. This can and should change when the definition of injection changes.

func (*PodValidator) RequireInjectionStatus added in v0.82.0

func (v *PodValidator) RequireInjectionStatus(t *testing.T, expected string)

RequireInjectionStatus ensures the webhook recorded the given overall injection status on the pod. Use the InjectionStatus* constants for the expected value.

func (*PodValidator) RequireInjectorVersion

func (v *PodValidator) RequireInjectorVersion(t *testing.T, expected string)

RequireInjectorVersion is a high level function to ensure the injector version found for the pod matches expected.

func (*PodValidator) RequireInstallType added in v0.84.0

func (v *PodValidator) RequireInstallType(t *testing.T, expected string, expectedContainers []string)

RequireInstallType ensures DD_INSTRUMENTATION_INSTALL_TYPE is set on the given containers.

func (*PodValidator) RequireLibraryVersions

func (v *PodValidator) RequireLibraryVersions(t *testing.T, expected map[string]string)

RequireLibraryVersions ensures the map of library name to version matches what is found in the pod. Ex. python -> v3.

func (*PodValidator) RequireMissingAnnotations added in v0.82.0

func (v *PodValidator) RequireMissingAnnotations(t *testing.T, keys []string)

RequireMissingAnnotations ensures the pod does not carry any of the given annotation keys.

func (*PodValidator) RequireMissingEnvs

func (v *PodValidator) RequireMissingEnvs(t *testing.T, missing []string, expectedContainers []string)

RequireMissingEnvs ensures that the list of missing keys do not exist in any of the containers provided.

func (*PodValidator) RequireMissingVolumeNames

func (v *PodValidator) RequireMissingVolumeNames(t *testing.T, missing []string)

RequireVolumeNames ensures the list of volume names exist in the pod.

func (*PodValidator) RequireNoInjection

func (v *PodValidator) RequireNoInjection(t *testing.T)

RequireNoInjection is a high level function that ensures a pod was not injected for SSI, including KPI env vars.

func (*PodValidator) RequireNoInjectionArtifacts added in v0.80.0

func (v *PodValidator) RequireNoInjectionArtifacts(t *testing.T)

RequireNoInjectionArtifacts ensures a pod has no SSI injection artifacts.

func (*PodValidator) RequireUnmutatedContainers added in v0.76.0

func (v *PodValidator) RequireUnmutatedContainers(t *testing.T, containerNames []string)

RequireUnmutatedContainers ensures that the specified containers have no Datadog-related environment variables.

func (*PodValidator) RequireVolumeNames

func (v *PodValidator) RequireVolumeNames(t *testing.T, expected []string)

RequireVolumeNames ensures the list of volume names exist in the pod.

Jump to

Keyboard shortcuts

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