Skip to content

Commit 84c2aaa

Browse files
test(crdinstall): assert no-delete label survives Install + merge
Address review feedback from @lllamnyp on internal/crdinstall/install.go: the deletion-protection label loop was untested — TestInstall_appliesAllCRDs only checked that Install returned no error, so a refactor dropping the loop would have shipped green and silently disabled VAP coverage on every Cozystack CRD. - Extend TestInstall_appliesAllCRDs to read each applied CRD back from the fake client and assert platform.cozystack.io/no-delete=true is present. - Add TestInstall_preservesExistingLabels: feeds in a CRD whose manifest already carries app.kubernetes.io/managed-by=foo, asserts both that label and the deletion-protection label end up on the applied object. Locks down the merge contract in install.go:92-101 (the GetLabels()==nil branch is still covered by the first test). Verified the new assertions fail when the label-stamping loop is removed. Assisted-By: Claude <noreply@anthropic.com> Signed-off-by: Myasnikov Daniil <myasnikovdaniil2001@gmail.com>
1 parent b6b0d8a commit 84c2aaa

1 file changed

Lines changed: 71 additions & 0 deletions

File tree

internal/crdinstall/install_test.go

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,77 @@ func TestInstall_appliesAllCRDs(t *testing.T) {
212212
if err != nil {
213213
t.Fatalf("Install() error = %v", err)
214214
}
215+
216+
for _, name := range []string{"packages.cozystack.io", "packagesources.cozystack.io"} {
217+
applied := &unstructured.Unstructured{}
218+
applied.SetGroupVersionKind(apiextensionsv1.SchemeGroupVersion.WithKind("CustomResourceDefinition"))
219+
if err := fakeClient.Get(ctx, client.ObjectKey{Name: name}, applied); err != nil {
220+
t.Fatalf("failed to read back applied CRD %s: %v", name, err)
221+
}
222+
labels := applied.GetLabels()
223+
if labels["platform.cozystack.io/no-delete"] != "true" {
224+
t.Errorf("CRD %s missing platform.cozystack.io/no-delete=true label; got labels=%v", name, labels)
225+
}
226+
}
227+
}
228+
229+
// testCRDWithLabels mirrors testCRD1 but already carries a non-cozystack label.
230+
// Exercises the merge path in Install: the existing label must survive, and the
231+
// deletion-protection label must be added alongside it.
232+
var testCRDWithLabels = `apiVersion: apiextensions.k8s.io/v1
233+
kind: CustomResourceDefinition
234+
metadata:
235+
name: packages.cozystack.io
236+
labels:
237+
app.kubernetes.io/managed-by: foo
238+
spec:
239+
group: cozystack.io
240+
names:
241+
kind: Package
242+
plural: packages
243+
scope: Namespaced
244+
versions:
245+
- name: v1alpha1
246+
served: true
247+
storage: true
248+
schema:
249+
openAPIV3Schema:
250+
type: object
251+
`
252+
253+
func TestInstall_preservesExistingLabels(t *testing.T) {
254+
log.SetLogger(zap.New(zap.UseDevMode(true)))
255+
256+
scheme := runtime.NewScheme()
257+
if err := apiextensionsv1.AddToScheme(scheme); err != nil {
258+
t.Fatalf("failed to add apiextensions to scheme: %v", err)
259+
}
260+
261+
fakeClient := fake.NewClientBuilder().
262+
WithScheme(scheme).
263+
WithInterceptorFuncs(establishedInterceptor()).
264+
Build()
265+
266+
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
267+
defer cancel()
268+
ctx = log.IntoContext(ctx, log.FromContext(context.Background()))
269+
270+
if err := Install(ctx, fakeClient, newCRDManifestWriter(testCRDWithLabels)); err != nil {
271+
t.Fatalf("Install() error = %v", err)
272+
}
273+
274+
applied := &unstructured.Unstructured{}
275+
applied.SetGroupVersionKind(apiextensionsv1.SchemeGroupVersion.WithKind("CustomResourceDefinition"))
276+
if err := fakeClient.Get(ctx, client.ObjectKey{Name: "packages.cozystack.io"}, applied); err != nil {
277+
t.Fatalf("failed to read back applied CRD: %v", err)
278+
}
279+
labels := applied.GetLabels()
280+
if labels["app.kubernetes.io/managed-by"] != "foo" {
281+
t.Errorf("existing label app.kubernetes.io/managed-by=foo was lost; got labels=%v", labels)
282+
}
283+
if labels["platform.cozystack.io/no-delete"] != "true" {
284+
t.Errorf("deletion-protection label missing after merge; got labels=%v", labels)
285+
}
215286
}
216287

217288
func TestInstall_noManifests(t *testing.T) {

0 commit comments

Comments
 (0)