omni/client/pkg/compression/compression_test.go
Utku Ozdemir 98ef83ee42
fix: fix config patches encryption when encryption is disabled
When the resource compression was disabled in the Omni config, we were not generating the ClusterMachineConfigPatches correctly.

The issue was: it was attempting to "force-compress" the ClusterMachineConfigPatches when any of the patches' size was above the threshold. But when it was trying to do that, it did not override the global setting of false.

The default setting for resource compression is `true`, but when a config file is used to configure Omni, and it was not specified in the config YAML, it was getting overwritten to be `false` due to the boolean merging behavior, which was fixed in https://github.com/siderolabs/omni/pull/2150.

Also: fix the compression kicking in even in cases when it is disabled in config but above the threshold.

Signed-off-by: Utku Ozdemir <utku.ozdemir@siderolabs.com>
2026-01-26 19:04:21 +01:00

110 lines
3.1 KiB
Go

// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
//nolint:staticcheck // we are ok with accesssing the deprecated fields in these tests.
package compression_test
import (
"encoding/json"
"strings"
"testing"
"github.com/stretchr/testify/require"
"go.yaml.in/yaml/v4"
"github.com/siderolabs/omni/client/api/omni/specs"
"github.com/siderolabs/omni/client/pkg/omni/resources/omni"
)
func TestClusterMachineConfigPatchesYAML(t *testing.T) {
res := omni.NewClusterMachineConfigPatches("test")
// set some patches
compressionConfig := specs.GetCompressionConfig()
aString := strings.Repeat("a", compressionConfig.MinThreshold)
bString := strings.Repeat("b", compressionConfig.MinThreshold)
err := res.TypedSpec().Value.SetUncompressedPatches([]string{aString, bString})
require.NoError(t, err)
// assert that the patches are compressed
require.Empty(t, res.TypedSpec().Value.Patches)
require.NotEmpty(t, res.TypedSpec().Value.CompressedPatches)
// marshal the spec to yaml
spec := res.TypedSpec().Value
specYAML, err := yaml.Marshal(spec)
require.NoError(t, err)
// assert that the patches are in the YAML in uncompressed form, and do not contain the compressed form
require.Contains(t, string(specYAML), aString)
require.Contains(t, string(specYAML), bString)
require.NotContains(t, string(specYAML), "compressed")
t.Logf("yaml:\n%s", string(specYAML))
// unmarshal the spec from the yaml
var newSpec specs.ClusterMachineConfigPatchesSpec
err = yaml.Unmarshal(specYAML, &newSpec)
require.NoError(t, err)
// assert that the patches got compressed again
require.Empty(t, newSpec.Patches)
require.NotEmpty(t, newSpec.CompressedPatches)
}
func TestClusterMachineConfigPatchesJSON(t *testing.T) {
res := omni.NewClusterMachineConfigPatches("test")
compressionConfig := specs.GetCompressionConfig()
aString := strings.Repeat("a", compressionConfig.MinThreshold)
bString := strings.Repeat("b", compressionConfig.MinThreshold)
// set some patches
err := res.TypedSpec().Value.SetUncompressedPatches([]string{aString, bString})
require.NoError(t, err)
// assert that the patches are compressed
require.Empty(t, res.TypedSpec().Value.Patches)
require.NotEmpty(t, res.TypedSpec().Value.CompressedPatches)
// marshal the spec to json
spec := res.TypedSpec().Value
specJSON, err := json.Marshal(spec)
require.NoError(t, err)
// assert that the patches are in the JSON in uncompressed form, and do not contain the compressed form
require.Contains(t, string(specJSON), aString)
require.Contains(t, string(specJSON), bString)
require.NotContains(t, string(specJSON), "compressed")
t.Logf("json:\n%s", string(specJSON))
// unmarshal the spec from the json
var newSpec *specs.ClusterMachineConfigPatchesSpec
err = json.Unmarshal(specJSON, &newSpec)
require.NoError(t, err)
// assert that the patches got compressed again
require.Empty(t, newSpec.Patches)
require.NotEmpty(t, newSpec.CompressedPatches)
}