chore: add tests to validate ConfigMap/Secret key removal in server-side diff (#25216)

Signed-off-by: Pedro Ribeiro <pedro.ribeiro@cross-join.com>
Co-authored-by: Pedro Ribeiro <pedro.ribeiro@cross-join.com>
Co-authored-by: Leonardo Luz Almeida <leoluz@users.noreply.github.com>
This commit is contained in:
pedro-cs-ribeiro
2026-01-15 21:53:13 +00:00
committed by GitHub
parent 6994a42fa9
commit 1049d40b7d
5 changed files with 110 additions and 0 deletions

View File

@@ -1164,6 +1164,48 @@ func TestServerSideDiff(t *testing.T) {
assert.Empty(t, predictedDeploy.Annotations[AnnotationLastAppliedConfig])
assert.Empty(t, liveDeploy.Annotations[AnnotationLastAppliedConfig])
})
t.Run("will detect ConfigMap data key removal", func(t *testing.T) {
// given
t.Parallel()
liveState := StrToUnstructured(testdata.ConfigMapLiveYAMLSSD)
desiredState := StrToUnstructured(testdata.ConfigMapConfigYAMLSSD)
opts := buildOpts(testdata.ConfigMapPredictedLiveJSONSSD)
// when
result, err := serverSideDiff(desiredState, liveState, opts...)
// then
require.NoError(t, err)
assert.NotNil(t, result)
assert.True(t, result.Modified, "Diff should detect key removal as a modification")
// Parse the results
var predictedCM map[string]any
err = json.Unmarshal(result.PredictedLive, &predictedCM)
require.NoError(t, err)
var liveCM map[string]any
err = json.Unmarshal(result.NormalizedLive, &liveCM)
require.NoError(t, err)
// Verify predicted live has only key1 and key2 (key3 removed)
predictedData, ok := predictedCM["data"].(map[string]any)
require.True(t, ok, "Predicted ConfigMap should have data field")
assert.Len(t, predictedData, 2, "Predicted data should have 2 keys")
assert.Contains(t, predictedData, "key1")
assert.Contains(t, predictedData, "key2")
assert.NotContains(t, predictedData, "key3", "key3 should be removed from predicted live")
// Verify live still has all 3 keys
liveData, ok := liveCM["data"].(map[string]any)
require.True(t, ok, "Live ConfigMap should have data field")
assert.Len(t, liveData, 3, "Live data should still have 3 keys")
assert.Contains(t, liveData, "key1")
assert.Contains(t, liveData, "key2")
assert.Contains(t, liveData, "key3", "key3 should still be in live state")
})
}
// testIgnoreDifferencesNormalizer implements a simple normalizer that removes specified fields

View File

@@ -86,4 +86,13 @@ var (
//go:embed ssd-deploy-composite-key-predicted-live.json
DeploymentCompositeKeyPredictedLiveJSONSSD string
//go:embed ssd-configmap-config.yaml
ConfigMapConfigYAMLSSD string
//go:embed ssd-configmap-live.yaml
ConfigMapLiveYAMLSSD string
//go:embed ssd-configmap-predicted-live.json
ConfigMapPredictedLiveJSONSSD string
)

View File

@@ -0,0 +1,8 @@
apiVersion: v1
kind: ConfigMap
metadata:
name: test-configmap
namespace: default
data:
key1: value1
key2: value2

View File

@@ -0,0 +1,22 @@
apiVersion: v1
kind: ConfigMap
metadata:
name: test-configmap
namespace: default
uid: 12345678-1234-1234-1234-123456789012
resourceVersion: "1000"
managedFields:
- manager: argocd-controller
operation: Apply
apiVersion: v1
time: "2024-01-01T00:00:00Z"
fieldsType: FieldsV1
fieldsV1:
f:data:
f:key1: {}
f:key2: {}
f:key3: {}
data:
key1: value1
key2: value2
key3: value3

View File

@@ -0,0 +1,29 @@
{
"apiVersion": "v1",
"kind": "ConfigMap",
"metadata": {
"name": "test-configmap",
"namespace": "default",
"uid": "12345678-1234-1234-1234-123456789012",
"resourceVersion": "1000",
"managedFields": [
{
"manager": "argocd-controller",
"operation": "Apply",
"apiVersion": "v1",
"time": "2024-01-01T00:00:00Z",
"fieldsType": "FieldsV1",
"fieldsV1": {
"f:data": {
"f:key1": {},
"f:key2": {}
}
}
}
]
},
"data": {
"key1": "value1",
"key2": "value2"
}
}