{"modulePath":"github.com/deckhouse/module-sdk","version":"v0.14.1","isLatest":true,"isStandardLibrary":false,"goos":"all","goarch":"all","docs":"# package framework\n\nPackage framework provides a deckhouse-style testing framework for module-sdk hooks.\n\nIt 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.\n\nTypical usage:\n\n\tfunc TestMyHook(t *testing.T) {\n\t    f := framework.HookExecutionConfigInit(t, hookConfig, MyHookHandler, `{}`, `{}`)\n\n\t    f.KubeStateSet(`\n\t---\n\tapiVersion: v1\n\tkind: Node\n\tmetadata:\n\t  name: kube-worker-1\n\t`)\n\n\t    f.RunHook()\n\n\t    require.NoError(t, f.HookError())\n\t    require.Len(t, f.Snapshots().Get(\"nodes\"), 1)\n\t    require.Equal(t, \"value\", f.ValuesGet(\"my.field\").String())\n\t}\n\n## Functions\n\n```go\nfunc LoadOpenAPISchema(path string) (map[string]any, error)\n```\nLoadOpenAPISchema reads an OpenAPI v3 schema from a YAML or JSON file and returns the parsed document.\n\nIf the schema document declares the addon-operator x-extend extension, e.g.:\n\n\tx-extend:\n\t  schema: config-values.yaml\n\nLoadOpenAPISchema also loads the referenced schema (resolved relative to the current schema's directory) and merges it as a parent: the parent's \\`properties\\`, \\`patternProperties\\`, \\`definitions\\`, \\`required\\` and extensions are folded into the current schema, with the current schema winning on conflicts. This mirrors the behaviour of addon-operator's ExtendTransformer.\n\nLoadOpenAPISchema does not resolve \\`$ref\\`s.\n\n```go\nfunc MergeValues(base, override map[string]any) map[string]any\n```\nMergeValues deep-merges override into base and returns the result.\n\nObject-typed values are merged property-by-property (recursing). For arrays and scalar values the override replaces the base entirely.\n\nNeither input is modified.\n\nMergeValues is the natural counterpart to SchemaDefaults: combine defaults extracted from a schema with values supplied by the test, letting the test's values win on every conflict.\n\n```go\nfunc SchemaDefaults(schema map[string]any) map[string]any\n```\nSchemaDefaults walks an OpenAPI schema (as returned by LoadOpenAPISchema) and produces a values document populated with the schema's \\`default:\\` values.\n\nThe algorithm mirrors addon-operator's defaulting:\n\n  - A property's \\`default:\\` (if any) is used as the starting value.\n  - For object-typed properties, sub-properties' defaults are then applied for any keys the explicit \\`default:\\` left empty.\n  - For arrays, items' defaults are applied to each existing item of the explicit \\`default:\\` list (item entries themselves are never synthesised from \\`items.default\\`).\n\nSchemaDefaults always returns a non-nil map (possibly empty).\n\n## Types\n\n```go\ntype HookExecutionConfig struct {\n\t// contains filtered or unexported fields\n}\n```\nHookExecutionConfig 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.\n\nA HookExecutionConfig is created with HookExecutionConfigInit (deckhouse-style) or NewHookExecutionConfig (with options).\n\n```go\nfunc HookExecutionConfigInit(t testing.TB, config *pkg.HookConfig, handler HookFunc, initValues, initConfigValues string) *HookExecutionConfig\n```\nHookExecutionConfigInit creates a deckhouse-style execution config.\n\ninitValues and initConfigValues are JSON or YAML strings representing the initial Helm values and module config values. Pass \"{}\" or \"\" if not needed.\n\n```go\nfunc NewHookExecutionConfig(t testing.TB, config *pkg.HookConfig, handler HookFunc, opts ...Option) *HookExecutionConfig\n```\nNewHookExecutionConfig creates an execution config with options.\n\n```go\nfunc (h *HookExecutionConfig) AddKubeObject(yamlObject string)\n```\nAddKubeObject appends one or more objects (multi-document YAML) to the fake cluster without resetting existing state.\n\n```go\nfunc (h *HookExecutionConfig) CollectedMetrics() []MetricOperation\n```\nCollectedMetrics returns the metric operations recorded by the hook during the most recent RunHook call.\n\n```go\nfunc (h *HookExecutionConfig) ConfigValuesDelete(path string)\n```\nConfigValuesDelete removes a config value at path.\n\n```go\nfunc (h *HookExecutionConfig) ConfigValuesGet(path string) gjson.Result\n```\nConfigValuesGet returns the current config value at path.\n\n```go\nfunc (h *HookExecutionConfig) ConfigValuesJSON() []byte\n```\nConfigValuesJSON returns the current config values as a JSON string.\n\n```go\nfunc (h *HookExecutionConfig) ConfigValuesSet(path string, value any)\n```\nConfigValuesSet sets a config value at path.\n\n```go\nfunc (h *HookExecutionConfig) ConfigValuesSetFromYaml(path string, raw []byte)\n```\nConfigValuesSetFromYaml parses YAML and sets the result at path.\n\n```go\nfunc (h *HookExecutionConfig) DependencyContainer() *frameworkDC\n```\nDependencyContainer returns the framework's dependency container so that tests can override its HTTP / registry / clock components before RunHook.\n\nExample:\n\n\thec.DependencyContainer().SetHTTPClient(myFakeHTTP)\n\n```go\nfunc (h *HookExecutionConfig) HookError() error\n```\nHookError returns the error returned by the hook handler from the most recent RunHook call.\n\n```go\nfunc (h *HookExecutionConfig) KubeClient() dynamic.Interface\n```\nKubeClient returns the underlying fake dynamic client. Use it to inspect or seed cluster state directly.\n\n```go\nfunc (h *HookExecutionConfig) KubeStateSet(yamlState string)\n```\nKubeStateSet replaces the fake cluster state with the resources defined in the provided multi-document YAML manifest.\n\nDocuments may be separated by '---'. Each document must include apiVersion, kind, metadata.name, and (for namespaced resources) metadata.namespace.\n\nAll previously-stored objects are removed before the new state is applied, so a single test can call KubeStateSet multiple times to simulate state transitions.\n\nSnapshots used by RunHook are regenerated from the new cluster state and the hook's KubernetesConfig bindings.\n\n```go\nfunc (h *HookExecutionConfig) KubernetesGlobalResource(kind, name string) *unstructured.Unstructured\n```\nKubernetesGlobalResource returns a cluster-scoped resource by kind and name. Returns nil if not found.\n\n```go\nfunc (h *HookExecutionConfig) KubernetesResource(kind, namespace, name string) *unstructured.Unstructured\n```\nKubernetesResource 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.\n\n```go\nfunc (h *HookExecutionConfig) Logger() *log.Logger\n```\nLogger returns the test logger (its output is captured in LoggerOutput).\n\n```go\nfunc (h *HookExecutionConfig) LoggerOutput() *bytes.Buffer\n```\nLoggerOutput returns the buffer of captured log output, useful for assertions.\n\n```go\nfunc (h *HookExecutionConfig) PatchOperations() []pkg.PatchCollectorOperation\n```\nPatchOperations returns the patch operations recorded by the hook during the most recent RunHook call.\n\n```go\nfunc (h *HookExecutionConfig) PatchedOperations() []RecordedPatch\n```\nPatchedOperations returns the typed slice of recorded patch operations (one entry per Create/Delete/Patch/... call). This is more convenient for assertions than PatchOperations.\n\n```go\nfunc (h *HookExecutionConfig) RegisterCRD(group, version, kind string, namespaced bool)\n```\nRegisterCRD 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.\n\nUse it for CRDs that are not registered through a typed runtime.SchemeBuilder.\n\nExample:\n\n\thec.RegisterCRD(\"example.com\", \"v1alpha1\", \"Widget\", true)\n\n```go\nfunc (h *HookExecutionConfig) RunHook()\n```\nRunHook executes the registered hook handler against the current state.\n\nThe framework:\n\n 1. Generates snapshots from the fake cluster according to the hook's KubernetesConfig bindings.\n 2. Builds a real pkg.HookInput backed by working values stores, a recording PatchCollector, and a Collector for metrics.\n 3. Invokes the hook handler with that input.\n 4. Applies the values patches produced by the hook to the values store.\n 5. Replays the recorded patch operations against the fake cluster.\n\nAfter RunHook, use HookError, ValuesGet, ConfigValuesGet, KubernetesResource, PatchedOperations and CollectedMetrics to assert behaviour.\n\n```go\nfunc (h *HookExecutionConfig) RunHookCtx(ctx context.Context)\n```\nRunHookCtx is like RunHook but accepts an explicit context.\n\n```go\nfunc (h *HookExecutionConfig) Snapshots() pkg.Snapshots\n```\nSnapshots returns the snapshots that were passed to the hook on the most recent RunHook call.\n\n```go\nfunc (h *HookExecutionConfig) ValuesDelete(path string)\n```\nValuesDelete removes a value at path.\n\n```go\nfunc (h *HookExecutionConfig) ValuesGet(path string) gjson.Result\n```\nValuesGet returns the current value at path (gjson dotted path).\n\n```go\nfunc (h *HookExecutionConfig) ValuesJSON() []byte\n```\nValuesJSON returns the current values as a JSON string. Mostly useful for debugging or asserting full document state.\n\n```go\nfunc (h *HookExecutionConfig) ValuesSet(path string, value any)\n```\nValuesSet sets a value at path. The value is written directly into the values store; it persists across RunHook calls.\n\n```go\nfunc (h *HookExecutionConfig) ValuesSetFromYaml(path string, raw []byte)\n```\nValuesSetFromYaml parses YAML and sets the result at path.\n\n```go\ntype HookFunc = pkg.HookFunc[*pkg.HookInput]\n```\nHookFunc is the type of hook handler functions tested by the framework.\n\n```go\ntype MetricOperation struct {\n\tName   string\n\tGroup  string\n\tAction string\n\tValue  *float64\n\tLabels map[string]string\n}\n```\nMetricOperation is a stable, framework-friendly view of a metric operation.\n\n```go\ntype Option interface {\n\t// contains filtered or unexported methods\n}\n```\nOption configures a HookExecutionConfig at construction time.\n\n```go\nfunc WithCRD(group, version, kind string, namespaced bool) Option\n```\nWithCRD registers a custom resource definition with the fake cluster so that resources of this kind can be created/listed via the dynamic client.\n\nUse this when your hook reads or writes CRs not registered through a runtime.SchemeBuilder.\n\n```go\nfunc WithConfigValuesSchema(path string) Option\n```\nWithConfigValuesSchema does the same as WithValuesSchema for the module's config values schema (typically \\`openapi/config-values.yaml\\`).\n\n```go\nfunc WithInitialConfigValues(v string) Option\n```\nWithInitialConfigValues sets the initial module config values JSON or YAML.\n\n```go\nfunc WithInitialValues(v string) Option\n```\nWithInitialValues sets the initial Helm values JSON or YAML.\n\n```go\nfunc WithOpenAPIDir(dir string) Option\n```\nWithOpenAPIDir is a convenience wrapper that points the framework at a directory containing the standard module OpenAPI files:\n\n\t\u003cdir\u003e/values.yaml\n\t\u003cdir\u003e/config-values.yaml\n\nEither file may be absent. Whichever ones are present are loaded and their defaults are merged under any test-supplied values.\n\nExample:\n\n\thec := framework.NewHookExecutionConfig(t, cfg, handler,\n\t    framework.WithOpenAPIDir(\"../openapi\"),\n\t    framework.WithInitialValues(`{\"https\": {\"mode\": \"CertManager\"}}`),\n\t)\n\n```go\nfunc WithSchemeBuilder(builder runtime.SchemeBuilder) Option\n```\nWithSchemeBuilder registers an additional runtime.SchemeBuilder so that typed CRDs from your module can be used in YAML state and assertions.\n\n```go\nfunc WithValuesSchema(path string) Option\n```\nWithValuesSchema reads an OpenAPI v3 schema from the given file path, extracts a values document populated with all \\`default:\\` values, and uses it as the baseline for the framework's \\`Values\\`. Anything passed via WithInitialValues (or HookExecutionConfigInit's initValues) is then deep-merged on top, so test-supplied values override schema defaults.\n\nThe schema may use the addon-operator \\`x-extend\\` extension to inherit \\`properties\\` / \\`required\\` from a sibling schema (typically config-values.yaml). See LoadOpenAPISchema for details.\n\nConstruction fails the test (via testing.TB.Fatalf) if the file is missing or malformed. Use WithOpenAPIDir if you want missing files to be silently ignored.\n\n```go\ntype PatchType string\n```\nPatchType identifies a recorded patch operation kind.\n\n```go\nconst (\n\tPatchTypeCreate             PatchType = \"Create\"\n\tPatchTypeCreateOrUpdate     PatchType = \"CreateOrUpdate\"\n\tPatchTypeCreateIfNotExists  PatchType = \"CreateIfNotExists\"\n\tPatchTypeDelete             PatchType = \"Delete\"\n\tPatchTypeDeleteInBackground PatchType = \"DeleteInBackground\"\n\tPatchTypeDeleteNonCascading PatchType = \"DeleteNonCascading\"\n\tPatchTypeJSONPatch          PatchType = \"JSONPatch\"\n\tPatchTypeMergePatch         PatchType = \"MergePatch\"\n\tPatchTypeJQFilter           PatchType = \"JQFilter\"\n)\n```\n\n```go\ntype RecordedPatch struct {\n\tType PatchType\n\n\t// For Create*: holds the runtime.Object / map / Unstructured.\n\tObject any\n\n\t// For Delete* / Patch* operations.\n\tAPIVersion string\n\tKind       string\n\tNamespace  string\n\tName       string\n\n\t// For Patch operations.\n\tJSONPatch  any\n\tMergePatch any\n\tJQFilter   string\n\n\t// Original options as passed by the hook.\n\tOptions []pkg.PatchCollectorOption\n}\n```\nRecordedPatch 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.\n\n```go\nfunc (r *RecordedPatch) Description() string\n```\npkg.PatchCollectorOperation implementation.\n\n```go\nfunc (r *RecordedPatch) SetObjectPrefix(prefix string)\n```\n\n","path":"github.com/deckhouse/module-sdk/testing/framework","name":"framework","synopsis":"Package framework provides a deckhouse-style testing framework for module-sdk hooks.","isRedistributable":true}
