Documentation
¶
Overview ¶
Package framework provides a deckhouse-style testing framework for module-sdk hooks.
It is inspired by deckhouse/testing/hooks but does not depend on addon-operator or shell-operator. Internally it uses a fake Kubernetes client (k8s.io/client-go/dynamic/fake) to simulate cluster state.
Typical usage:
func TestMyHook(t *testing.T) {
f := framework.HookExecutionConfigInit(t, hookConfig, MyHookHandler, `{}`, `{}`)
f.KubeStateSet(`
---
apiVersion: v1
kind: Node
metadata:
name: kube-worker-1
`)
f.RunHook()
require.NoError(t, f.HookError())
require.Len(t, f.Snapshots().Get("nodes"), 1)
require.Equal(t, "value", f.ValuesGet("my.field").String())
}
Index ¶
- type HookExecutionConfig
- func (h *HookExecutionConfig) AddKubeObject(yamlObject string)
- func (h *HookExecutionConfig) CollectedMetrics() []MetricOperation
- func (h *HookExecutionConfig) ConfigValuesDelete(path string)
- func (h *HookExecutionConfig) ConfigValuesGet(path string) gjson.Result
- func (h *HookExecutionConfig) ConfigValuesJSON() []byte
- func (h *HookExecutionConfig) ConfigValuesSet(path string, value any)
- func (h *HookExecutionConfig) ConfigValuesSetFromYaml(path string, raw []byte)
- func (h *HookExecutionConfig) DependencyContainer() *frameworkDC
- func (h *HookExecutionConfig) HookError() error
- func (h *HookExecutionConfig) KubeClient() dynamic.Interface
- func (h *HookExecutionConfig) KubeStateSet(yamlState string)
- func (h *HookExecutionConfig) KubernetesGlobalResource(kind, name string) *unstructured.Unstructured
- func (h *HookExecutionConfig) KubernetesResource(kind, namespace, name string) *unstructured.Unstructured
- func (h *HookExecutionConfig) Logger() *log.Logger
- func (h *HookExecutionConfig) LoggerOutput() *bytes.Buffer
- func (h *HookExecutionConfig) PatchOperations() []pkg.PatchCollectorOperation
- func (h *HookExecutionConfig) PatchedOperations() []RecordedPatch
- func (h *HookExecutionConfig) RegisterCRD(group, version, kind string, namespaced bool)
- func (h *HookExecutionConfig) RunHook()
- func (h *HookExecutionConfig) RunHookCtx(ctx context.Context)
- func (h *HookExecutionConfig) Snapshots() pkg.Snapshots
- func (h *HookExecutionConfig) ValuesDelete(path string)
- func (h *HookExecutionConfig) ValuesGet(path string) gjson.Result
- func (h *HookExecutionConfig) ValuesJSON() []byte
- func (h *HookExecutionConfig) ValuesSet(path string, value any)
- func (h *HookExecutionConfig) ValuesSetFromYaml(path string, raw []byte)
- type HookFunc
- type MetricOperation
- type Option
- type PatchType
- type RecordedPatch
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type HookExecutionConfig ¶
type HookExecutionConfig struct {
// contains filtered or unexported fields
}
HookExecutionConfig is the main entry point for hook tests. It encapsulates the hook under test, a fake Kubernetes cluster, values stores, and collectors for patch operations and metrics.
A HookExecutionConfig is created with HookExecutionConfigInit (deckhouse-style) or NewHookExecutionConfig (with options).
func HookExecutionConfigInit ¶
func HookExecutionConfigInit(t testing.TB, config *pkg.HookConfig, handler HookFunc, initValues, initConfigValues string) *HookExecutionConfig
HookExecutionConfigInit creates a deckhouse-style execution config.
initValues and initConfigValues are JSON or YAML strings representing the initial Helm values and module config values. Pass "{}" or "" if not needed.
func NewHookExecutionConfig ¶
func NewHookExecutionConfig(t testing.TB, config *pkg.HookConfig, handler HookFunc, opts ...Option) *HookExecutionConfig
NewHookExecutionConfig creates an execution config with options.
func (*HookExecutionConfig) AddKubeObject ¶
func (h *HookExecutionConfig) AddKubeObject(yamlObject string)
AddKubeObject appends one or more objects (multi-document YAML) to the fake cluster without resetting existing state.
func (*HookExecutionConfig) CollectedMetrics ¶
func (h *HookExecutionConfig) CollectedMetrics() []MetricOperation
CollectedMetrics returns the metric operations recorded by the hook during the most recent RunHook call.
func (*HookExecutionConfig) ConfigValuesDelete ¶
func (h *HookExecutionConfig) ConfigValuesDelete(path string)
ConfigValuesDelete removes a config value at path.
func (*HookExecutionConfig) ConfigValuesGet ¶
func (h *HookExecutionConfig) ConfigValuesGet(path string) gjson.Result
ConfigValuesGet returns the current config value at path.
func (*HookExecutionConfig) ConfigValuesJSON ¶
func (h *HookExecutionConfig) ConfigValuesJSON() []byte
ConfigValuesJSON returns the current config values as a JSON string.
func (*HookExecutionConfig) ConfigValuesSet ¶
func (h *HookExecutionConfig) ConfigValuesSet(path string, value any)
ConfigValuesSet sets a config value at path.
func (*HookExecutionConfig) ConfigValuesSetFromYaml ¶
func (h *HookExecutionConfig) ConfigValuesSetFromYaml(path string, raw []byte)
ConfigValuesSetFromYaml parses YAML and sets the result at path.
func (*HookExecutionConfig) DependencyContainer ¶
func (h *HookExecutionConfig) DependencyContainer() *frameworkDC
DependencyContainer returns the framework's dependency container so that tests can override its HTTP / registry / clock components before RunHook.
Example:
hec.DependencyContainer().SetHTTPClient(myFakeHTTP)
func (*HookExecutionConfig) HookError ¶
func (h *HookExecutionConfig) HookError() error
HookError returns the error returned by the hook handler from the most recent RunHook call.
func (*HookExecutionConfig) KubeClient ¶
func (h *HookExecutionConfig) KubeClient() dynamic.Interface
KubeClient returns the underlying fake dynamic client. Use it to inspect or seed cluster state directly.
func (*HookExecutionConfig) KubeStateSet ¶
func (h *HookExecutionConfig) KubeStateSet(yamlState string)
KubeStateSet replaces the fake cluster state with the resources defined in the provided multi-document YAML manifest.
Documents may be separated by '---'. Each document must include apiVersion, kind, metadata.name, and (for namespaced resources) metadata.namespace.
All previously-stored objects are removed before the new state is applied, so a single test can call KubeStateSet multiple times to simulate state transitions.
Snapshots used by RunHook are regenerated from the new cluster state and the hook's KubernetesConfig bindings.
func (*HookExecutionConfig) KubernetesGlobalResource ¶
func (h *HookExecutionConfig) KubernetesGlobalResource(kind, name string) *unstructured.Unstructured
KubernetesGlobalResource returns a cluster-scoped resource by kind and name. Returns nil if not found.
func (*HookExecutionConfig) KubernetesResource ¶
func (h *HookExecutionConfig) KubernetesResource(kind, namespace, name string) *unstructured.Unstructured
KubernetesResource returns a fake-cluster resource by kind, namespace, and name. Namespace can be empty for cluster-scoped resources. Returns nil if the resource is not found.
func (*HookExecutionConfig) Logger ¶
func (h *HookExecutionConfig) Logger() *log.Logger
Logger returns the test logger (its output is captured in LoggerOutput).
func (*HookExecutionConfig) LoggerOutput ¶
func (h *HookExecutionConfig) LoggerOutput() *bytes.Buffer
LoggerOutput returns the buffer of captured log output, useful for assertions.
func (*HookExecutionConfig) PatchOperations ¶
func (h *HookExecutionConfig) PatchOperations() []pkg.PatchCollectorOperation
PatchOperations returns the patch operations recorded by the hook during the most recent RunHook call.
func (*HookExecutionConfig) PatchedOperations ¶
func (h *HookExecutionConfig) PatchedOperations() []RecordedPatch
PatchedOperations returns the typed slice of recorded patch operations (one entry per Create/Delete/Patch/... call). This is more convenient for assertions than PatchOperations.
func (*HookExecutionConfig) RegisterCRD ¶
func (h *HookExecutionConfig) RegisterCRD(group, version, kind string, namespaced bool)
RegisterCRD makes a custom resource known to the fake cluster. After this call, objects of this kind can be supplied via KubeStateSet, listed via KubernetesResource, and used in KubernetesConfig snapshot bindings.
Use it for CRDs that are not registered through a typed runtime.SchemeBuilder.
Example:
hec.RegisterCRD("example.com", "v1alpha1", "Widget", true)
func (*HookExecutionConfig) RunHook ¶
func (h *HookExecutionConfig) RunHook()
RunHook executes the registered hook handler against the current state.
The framework:
- Generates snapshots from the fake cluster according to the hook's KubernetesConfig bindings.
- Builds a real pkg.HookInput backed by working values stores, a recording PatchCollector, and a Collector for metrics.
- Invokes the hook handler with that input.
- Applies the values patches produced by the hook to the values store.
- Replays the recorded patch operations against the fake cluster.
After RunHook, use HookError, ValuesGet, ConfigValuesGet, KubernetesResource, PatchedOperations and CollectedMetrics to assert behaviour.
func (*HookExecutionConfig) RunHookCtx ¶
func (h *HookExecutionConfig) RunHookCtx(ctx context.Context)
RunHookCtx is like RunHook but accepts an explicit context.
func (*HookExecutionConfig) Snapshots ¶
func (h *HookExecutionConfig) Snapshots() pkg.Snapshots
Snapshots returns the snapshots that were passed to the hook on the most recent RunHook call.
func (*HookExecutionConfig) ValuesDelete ¶
func (h *HookExecutionConfig) ValuesDelete(path string)
ValuesDelete removes a value at path.
func (*HookExecutionConfig) ValuesGet ¶
func (h *HookExecutionConfig) ValuesGet(path string) gjson.Result
ValuesGet returns the current value at path (gjson dotted path).
func (*HookExecutionConfig) ValuesJSON ¶
func (h *HookExecutionConfig) ValuesJSON() []byte
ValuesJSON returns the current values as a JSON string. Mostly useful for debugging or asserting full document state.
func (*HookExecutionConfig) ValuesSet ¶
func (h *HookExecutionConfig) ValuesSet(path string, value any)
ValuesSet sets a value at path. The value is written directly into the values store; it persists across RunHook calls.
func (*HookExecutionConfig) ValuesSetFromYaml ¶
func (h *HookExecutionConfig) ValuesSetFromYaml(path string, raw []byte)
ValuesSetFromYaml parses YAML and sets the result at path.
type MetricOperation ¶
type MetricOperation struct {
Name string
Group string
Action string
Value *float64
Labels map[string]string
}
MetricOperation is a stable, framework-friendly view of a metric operation.
type Option ¶
type Option interface {
// contains filtered or unexported methods
}
Option configures a HookExecutionConfig at construction time.
func WithCRD ¶
WithCRD registers a custom resource definition with the fake cluster so that resources of this kind can be created/listed via the dynamic client.
Use this when your hook reads or writes CRs not registered through a runtime.SchemeBuilder.
func WithInitialConfigValues ¶
WithInitialConfigValues sets the initial module config values JSON or YAML.
func WithInitialValues ¶
WithInitialValues sets the initial Helm values JSON or YAML.
func WithSchemeBuilder ¶
func WithSchemeBuilder(builder runtime.SchemeBuilder) Option
WithSchemeBuilder registers an additional runtime.SchemeBuilder so that typed CRDs from your module can be used in YAML state and assertions.
type PatchType ¶
type PatchType string
PatchType identifies a recorded patch operation kind.
const ( PatchTypeCreate PatchType = "Create" PatchTypeCreateOrUpdate PatchType = "CreateOrUpdate" PatchTypeCreateIfNotExists PatchType = "CreateIfNotExists" PatchTypeDelete PatchType = "Delete" PatchTypeDeleteInBackground PatchType = "DeleteInBackground" PatchTypeDeleteNonCascading PatchType = "DeleteNonCascading" PatchTypeJSONPatch PatchType = "JSONPatch" PatchTypeMergePatch PatchType = "MergePatch" PatchTypeJQFilter PatchType = "JQFilter" )
type RecordedPatch ¶
type RecordedPatch struct {
Type PatchType
// For Create*: holds the runtime.Object / map / Unstructured.
Object any
// For Delete* / Patch* operations.
APIVersion string
Kind string
Namespace string
Name string
// For Patch operations.
JSONPatch any
MergePatch any
JQFilter string
// Original options as passed by the hook.
Options []pkg.PatchCollectorOption
}
RecordedPatch is a structured copy of a single patch operation issued by a hook. It captures the operation type and all parameters so that tests can assert on the hook's intent.
func (*RecordedPatch) Description ¶
func (r *RecordedPatch) Description() string
pkg.PatchCollectorOperation implementation.
func (*RecordedPatch) SetObjectPrefix ¶
func (r *RecordedPatch) SetObjectPrefix(prefix string)