|
1 | | -// Copyright (C) 2025 The Falco Authors |
| 1 | +// Copyright (C) 2026 The Falco Authors |
2 | 2 | // |
3 | 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
4 | 4 | // you may not use this file except in compliance with the License. |
|
17 | 17 | package falco |
18 | 18 |
|
19 | 19 | import ( |
| 20 | + "fmt" |
20 | 21 | "testing" |
21 | 22 |
|
22 | 23 | "github.com/stretchr/testify/assert" |
23 | 24 | appsv1 "k8s.io/api/apps/v1" |
24 | 25 | corev1 "k8s.io/api/core/v1" |
25 | 26 | metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
26 | 27 | "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" |
| 28 | + "sigs.k8s.io/structured-merge-diff/v4/fieldpath" |
| 29 | + "sigs.k8s.io/structured-merge-diff/v4/typed" |
27 | 30 | ) |
28 | 31 |
|
29 | 32 | func TestNeedsUpdate(t *testing.T) { |
@@ -103,4 +106,121 @@ func TestDiff(t *testing.T) { |
103 | 106 | assert.Error(t, err) |
104 | 107 | assert.Nil(t, result) |
105 | 108 | }) |
| 109 | + |
| 110 | + t.Run("no managed fields returns ErrNoManagedFields", func(t *testing.T) { |
| 111 | + current := &appsv1.DaemonSet{ |
| 112 | + TypeMeta: metav1.TypeMeta{ |
| 113 | + APIVersion: "apps/v1", |
| 114 | + Kind: "DaemonSet", |
| 115 | + }, |
| 116 | + ObjectMeta: metav1.ObjectMeta{ |
| 117 | + Name: "test", |
| 118 | + // No ManagedFields set |
| 119 | + }, |
| 120 | + } |
| 121 | + desired := &unstructured.Unstructured{ |
| 122 | + Object: map[string]interface{}{ |
| 123 | + "apiVersion": "apps/v1", |
| 124 | + "kind": "DaemonSet", |
| 125 | + "metadata": map[string]interface{}{ |
| 126 | + "name": "test", |
| 127 | + }, |
| 128 | + }, |
| 129 | + } |
| 130 | + result, err := diff(current, desired) |
| 131 | + assert.Error(t, err) |
| 132 | + assert.ErrorIs(t, err, ErrNoManagedFields) |
| 133 | + assert.Nil(t, result) |
| 134 | + }) |
| 135 | +} |
| 136 | + |
| 137 | +func TestErrNoManagedFields(t *testing.T) { |
| 138 | + t.Run("error message is descriptive", func(t *testing.T) { |
| 139 | + assert.Contains(t, ErrNoManagedFields.Error(), "no managed fields") |
| 140 | + }) |
| 141 | + |
| 142 | + t.Run("can be wrapped and unwrapped", func(t *testing.T) { |
| 143 | + wrapped := fmt.Errorf("failed to compare: %w", ErrNoManagedFields) |
| 144 | + assert.ErrorIs(t, wrapped, ErrNoManagedFields) |
| 145 | + }) |
| 146 | + |
| 147 | + t.Run("is distinguishable from other errors", func(t *testing.T) { |
| 148 | + otherErr := fmt.Errorf("some other error") |
| 149 | + assert.NotErrorIs(t, otherErr, ErrNoManagedFields) |
| 150 | + }) |
| 151 | +} |
| 152 | + |
| 153 | +func TestFormatChangedFields(t *testing.T) { |
| 154 | + t.Run("nil comparison returns empty string", func(t *testing.T) { |
| 155 | + result := formatChangedFields(nil) |
| 156 | + assert.Equal(t, "", result) |
| 157 | + }) |
| 158 | + |
| 159 | + t.Run("empty comparison returns no changes", func(t *testing.T) { |
| 160 | + comparison := &typed.Comparison{ |
| 161 | + Added: &fieldpath.Set{}, |
| 162 | + Modified: &fieldpath.Set{}, |
| 163 | + Removed: &fieldpath.Set{}, |
| 164 | + } |
| 165 | + result := formatChangedFields(comparison) |
| 166 | + assert.Equal(t, "no changes", result) |
| 167 | + }) |
| 168 | + |
| 169 | + t.Run("only added fields", func(t *testing.T) { |
| 170 | + added := fieldpath.NewSet(fieldpath.MakePathOrDie("spec", "replicas")) |
| 171 | + comparison := &typed.Comparison{ |
| 172 | + Added: added, |
| 173 | + Modified: &fieldpath.Set{}, |
| 174 | + Removed: &fieldpath.Set{}, |
| 175 | + } |
| 176 | + result := formatChangedFields(comparison) |
| 177 | + assert.Contains(t, result, "added:") |
| 178 | + assert.Contains(t, result, "spec") |
| 179 | + assert.NotContains(t, result, "modified:") |
| 180 | + assert.NotContains(t, result, "removed:") |
| 181 | + }) |
| 182 | + |
| 183 | + t.Run("only modified fields", func(t *testing.T) { |
| 184 | + modified := fieldpath.NewSet(fieldpath.MakePathOrDie("spec", "template", "spec", "containers")) |
| 185 | + comparison := &typed.Comparison{ |
| 186 | + Added: &fieldpath.Set{}, |
| 187 | + Modified: modified, |
| 188 | + Removed: &fieldpath.Set{}, |
| 189 | + } |
| 190 | + result := formatChangedFields(comparison) |
| 191 | + assert.Contains(t, result, "modified:") |
| 192 | + assert.Contains(t, result, "spec") |
| 193 | + assert.NotContains(t, result, "added:") |
| 194 | + assert.NotContains(t, result, "removed:") |
| 195 | + }) |
| 196 | + |
| 197 | + t.Run("only removed fields", func(t *testing.T) { |
| 198 | + removed := fieldpath.NewSet(fieldpath.MakePathOrDie("metadata", "labels")) |
| 199 | + comparison := &typed.Comparison{ |
| 200 | + Added: &fieldpath.Set{}, |
| 201 | + Modified: &fieldpath.Set{}, |
| 202 | + Removed: removed, |
| 203 | + } |
| 204 | + result := formatChangedFields(comparison) |
| 205 | + assert.Contains(t, result, "removed:") |
| 206 | + assert.Contains(t, result, "metadata") |
| 207 | + assert.NotContains(t, result, "added:") |
| 208 | + assert.NotContains(t, result, "modified:") |
| 209 | + }) |
| 210 | + |
| 211 | + t.Run("multiple change types", func(t *testing.T) { |
| 212 | + added := fieldpath.NewSet(fieldpath.MakePathOrDie("spec", "newField")) |
| 213 | + modified := fieldpath.NewSet(fieldpath.MakePathOrDie("spec", "replicas")) |
| 214 | + removed := fieldpath.NewSet(fieldpath.MakePathOrDie("metadata", "annotations")) |
| 215 | + comparison := &typed.Comparison{ |
| 216 | + Added: added, |
| 217 | + Modified: modified, |
| 218 | + Removed: removed, |
| 219 | + } |
| 220 | + result := formatChangedFields(comparison) |
| 221 | + assert.Contains(t, result, "added:") |
| 222 | + assert.Contains(t, result, "modified:") |
| 223 | + assert.Contains(t, result, "removed:") |
| 224 | + assert.Contains(t, result, "; ") |
| 225 | + }) |
106 | 226 | } |
0 commit comments