From c6ed284d2b1714a81192ec7bc23d24d3ab10765b Mon Sep 17 00:00:00 2001 From: bruelea <166021996+bruelea@users.noreply.github.com> Date: Wed, 30 Apr 2025 11:18:37 +0200 Subject: [PATCH 01/11] check if custom fields in parent prefix selector exist --- pkg/netbox/api/prefix_claim.go | 66 +++++++++++++++++++++++++++++----- 1 file changed, 58 insertions(+), 8 deletions(-) diff --git a/pkg/netbox/api/prefix_claim.go b/pkg/netbox/api/prefix_claim.go index 621eaee6..73ce6133 100644 --- a/pkg/netbox/api/prefix_claim.go +++ b/pkg/netbox/api/prefix_claim.go @@ -19,10 +19,11 @@ package api import ( "errors" "fmt" + "slices" "strconv" "strings" - "github.com/go-openapi/runtime" + "github.com/netbox-community/go-netbox/v3/netbox/client/extras" "github.com/netbox-community/go-netbox/v3/netbox/client/ipam" "github.com/netbox-community/netbox-operator/pkg/config" "github.com/netbox-community/netbox-operator/pkg/netbox/models" @@ -125,16 +126,25 @@ func (r *NetboxClient) GetAvailablePrefixByParentPrefixSelector(prefixClaimSpec fieldEntries["family"] = family } - var conditions func(co *runtime.ClientOperation) - parentPrefixSelectorEntries := make([]CustomFieldEntry, 0, len(prefixClaimSpec.ParentPrefixSelector)) + parentPrefixSelectorCustomFields := make([]CustomFieldEntry, 0, len(prefixClaimSpec.ParentPrefixSelector)) for k, v := range prefixClaimSpec.ParentPrefixSelector { - parentPrefixSelectorEntries = append(parentPrefixSelectorEntries, CustomFieldEntry{ - key: k, - value: v, - }) + switch k { + case "tenant", "site", "family": + // already handled + default: + parentPrefixSelectorCustomFields = append(parentPrefixSelectorCustomFields, CustomFieldEntry{ + key: k, + value: v, + }) + } } - conditions = newQueryFilterOperation(fieldEntries, parentPrefixSelectorEntries) + err := r.customFieldsExistsOrErr(parentPrefixSelectorCustomFields) + if err != nil { + return nil, err + } + + conditions := newQueryFilterOperation(fieldEntries, parentPrefixSelectorCustomFields) list, err := r.Ipam.IpamPrefixesList(ipam.NewIpamPrefixesListParams(), nil, conditions) if err != nil { @@ -158,6 +168,46 @@ func (r *NetboxClient) GetAvailablePrefixByParentPrefixSelector(prefixClaimSpec return prefixes, nil } +func (r *NetboxClient) customFieldsExistsOrErr(customfieldFilterEntries []CustomFieldEntry) error { + if len(customfieldFilterEntries) == 0 { + // as the parent prefix selector does not filter for custom fields + // the check can be skipped + return nil + } + + responseGetCustomFieldsList, err := r.Extras.ExtrasCustomFieldsList(extras.NewExtrasCustomFieldsListParams(), nil) + if err != nil { + return err + } + + existingCustomFields := responseGetCustomFieldsList.Payload.Results + if existingCustomFields == nil || len(existingCustomFields) == 0 { + return fmt.Errorf("netbox custom fields list is nil or empty") + } + + customFieldNames := make([]string, len(existingCustomFields)) + for i, field := range existingCustomFields { + if field.Name == nil { + return fmt.Errorf("netbox custom field name is nil") + } + customFieldNames[i] = *field.Name + } + + missingCustomFields := make([]string, 0) + for _, entry := range customfieldFilterEntries { + //request to netbox to check if the custom field existse + if !slices.Contains(customFieldNames, entry.key) { + missingCustomFields = append(missingCustomFields, entry.key) + } + } + + if len(missingCustomFields) > 0 { + return fmt.Errorf("invalid parentPrefixSelector, netbox custom fields %v do not exist", missingCustomFields) + } + + return nil +} + func (r *NetboxClient) isParentPrefixCandidate(prefixClaimSpec *netboxv1.PrefixClaimSpec, prefix string) bool { // if we can allocate a prefix from it, we can take it as a parent prefix if _, err := r.GetAvailablePrefixByClaim(&models.PrefixClaim{ From 751a8575c556ee454ef86de79cdc13899dfa9390 Mon Sep 17 00:00:00 2001 From: bruelea <166021996+bruelea@users.noreply.github.com> Date: Fri, 2 May 2025 09:56:30 +0200 Subject: [PATCH 02/11] add unit tests --- api/v1/prefixclaim_types.go | 2 +- internal/controller/prefixclaim_controller.go | 7 +- pkg/netbox/api/prefix_claim.go | 6 +- pkg/netbox/api/prefix_claim_test.go | 212 ++++++++++++++++++ 4 files changed, 220 insertions(+), 7 deletions(-) diff --git a/api/v1/prefixclaim_types.go b/api/v1/prefixclaim_types.go index a5616b25..54cfe7f7 100644 --- a/api/v1/prefixclaim_types.go +++ b/api/v1/prefixclaim_types.go @@ -174,7 +174,7 @@ var ConditionPrefixAssignedFalse = metav1.Condition{ Type: "PrefixAssigned", Status: "False", Reason: "PrefixCRNotCreated", - Message: "Failed to fetch new Prefix from NetBox", + Message: "Failed to assign prefix, prefix CR creation skipped", } var ConditionParentPrefixSelectedTrue = metav1.Condition{ diff --git a/internal/controller/prefixclaim_controller.go b/internal/controller/prefixclaim_controller.go index f2b9589b..ce3a6ce2 100644 --- a/internal/controller/prefixclaim_controller.go +++ b/internal/controller/prefixclaim_controller.go @@ -154,10 +154,11 @@ func (r *PrefixClaimReconciler) Reconcile(ctx context.Context, req ctrl.Request) // The existing algorithm for prefix allocation within a ParentPrefix remains unchanged // fetch available prefixes from netbox - parentPrefixCandidates, err := r.NetboxClient.GetAvailablePrefixByParentPrefixSelector(&prefixClaim.Spec) + parentPrefixCandidates, err := r.NetboxClient.GetAvailablePrefixesByParentPrefixSelector(&prefixClaim.Spec) if err != nil || len(parentPrefixCandidates) == 0 { - if errReport := r.EventStatusRecorder.Report(ctx, prefixClaim, netboxv1.ConditionParentPrefixSelectedFalse, corev1.EventTypeWarning, fmt.Errorf("no parent prefix can be obtained with the query conditions set in ParentPrefixSelector, err = %w, number of candidates = %v", err, len(parentPrefixCandidates))); errReport != nil { - return ctrl.Result{}, errReport + r.EventStatusRecorder.Recorder().Event(prefixClaim, corev1.EventTypeWarning, netboxv1.ConditionPrefixAssignedFalse.Reason, err.Error()) + if err := r.EventStatusRecorder.Report(ctx, prefixClaim, netboxv1.ConditionPrefixAssignedFalse, corev1.EventTypeWarning, err); err != nil { + return ctrl.Result{}, err } // we requeue as this might be a temporary prefix exhausation diff --git a/pkg/netbox/api/prefix_claim.go b/pkg/netbox/api/prefix_claim.go index 73ce6133..c6d4d9a4 100644 --- a/pkg/netbox/api/prefix_claim.go +++ b/pkg/netbox/api/prefix_claim.go @@ -94,7 +94,7 @@ func validatePrefixLengthOrError(prefixClaim *models.PrefixClaim, prefixFamily i return nil } -func (r *NetboxClient) GetAvailablePrefixByParentPrefixSelector(prefixClaimSpec *netboxv1.PrefixClaimSpec) ([]*models.Prefix, error) { +func (r *NetboxClient) GetAvailablePrefixesByParentPrefixSelector(prefixClaimSpec *netboxv1.PrefixClaimSpec) ([]*models.Prefix, error) { fieldEntries := make(map[string]string) if tenant, ok := prefixClaimSpec.ParentPrefixSelector["tenant"]; ok { @@ -130,7 +130,7 @@ func (r *NetboxClient) GetAvailablePrefixByParentPrefixSelector(prefixClaimSpec for k, v := range prefixClaimSpec.ParentPrefixSelector { switch k { case "tenant", "site", "family": - // already handled + // skip built in fields default: parentPrefixSelectorCustomFields = append(parentPrefixSelectorCustomFields, CustomFieldEntry{ key: k, @@ -181,7 +181,7 @@ func (r *NetboxClient) customFieldsExistsOrErr(customfieldFilterEntries []Custom } existingCustomFields := responseGetCustomFieldsList.Payload.Results - if existingCustomFields == nil || len(existingCustomFields) == 0 { + if len(existingCustomFields) == 0 { return fmt.Errorf("netbox custom fields list is nil or empty") } diff --git a/pkg/netbox/api/prefix_claim_test.go b/pkg/netbox/api/prefix_claim_test.go index f68f8df0..95c5fabd 100644 --- a/pkg/netbox/api/prefix_claim_test.go +++ b/pkg/netbox/api/prefix_claim_test.go @@ -22,9 +22,11 @@ import ( "testing" "github.com/netbox-community/go-netbox/v3/netbox/client/dcim" + "github.com/netbox-community/go-netbox/v3/netbox/client/extras" "github.com/netbox-community/go-netbox/v3/netbox/client/ipam" "github.com/netbox-community/go-netbox/v3/netbox/client/tenancy" netboxModels "github.com/netbox-community/go-netbox/v3/netbox/models" + netboxv1 "github.com/netbox-community/netbox-operator/api/v1" "github.com/netbox-community/netbox-operator/gen/mock_interfaces" "github.com/netbox-community/netbox-operator/pkg/netbox/models" "github.com/stretchr/testify/assert" @@ -1047,3 +1049,213 @@ func TestPrefixClaim_GetAvailablePrefixIfNoSiteInSpec(t *testing.T) { assert.Nil(t, err) assert.Equal(t, prefix, actual.Prefix) } + +func TestPrefixClaim_GetAvailablePrefixByParentPrefixSelector(t *testing.T) { + ctrl := gomock.NewController(t) + defer ctrl.Finish() + + pxcSpec := netboxv1.PrefixClaimSpec{ + ParentPrefixSelector: map[string]string{ + "environment": "dev", + "family": "IPv4", + "tenant": "tenant", + "site": "Site1", + }, + PrefixLength: "/32", + } + + mockTenancy := mock_interfaces.NewMockTenancyInterface(ctrl) + mockPrefixIpam := mock_interfaces.NewMockIpamInterface(ctrl) + mockExtras := mock_interfaces.NewMockExtrasInterface(ctrl) + mockDcim := mock_interfaces.NewMockDcimInterface(ctrl) + + // example of site + siteId := int64(3) + siteName := "Site1" + siteOutputSlug := "site1" + expectedSite := &dcim.DcimSitesListOK{ + Payload: &dcim.DcimSitesListOKBody{ + Results: []*netboxModels.Site{ + { + ID: siteId, + Name: &siteName, + Slug: &siteOutputSlug, + }, + }, + }, + } + inputSite := dcim.NewDcimSitesListParams().WithName(&siteName) + + // tenant + tenantName := "tenant" + tenantId := int64(2) + tenantOutputSlug := "tenant1" + + expectedTenant := &tenancy.TenancyTenantsListOK{ + Payload: &tenancy.TenancyTenantsListOKBody{ + Results: []*netboxModels.Tenant{ + { + ID: tenantId, + Name: &tenantName, + Slug: &tenantOutputSlug, + }, + }, + }, + } + + parentPrefix := "10.112.140.0/24" + parentPrefixId := int64(1) + prefixListInput := ipam. + NewIpamPrefixesListParams() + + prefixFamily := int64(IPv4Family) + prefixFamilyLabel := netboxModels.PrefixFamilyLabelIPV4 + prefixListOutput := &ipam.IpamPrefixesListOK{ + Payload: &ipam.IpamPrefixesListOKBody{ + Results: []*netboxModels.Prefix{ + { + ID: parentPrefixId, + Prefix: &parentPrefix, + Family: &netboxModels.PrefixFamily{Label: &prefixFamilyLabel, Value: &prefixFamily}, + }, + }, + }, + } + + prefixListInputWithParam := ipam.NewIpamPrefixesListParams().WithPrefix(&parentPrefix) + prefixListOutputWithParam := &ipam.IpamPrefixesListOK{ + Payload: &ipam.IpamPrefixesListOKBody{ + Results: []*netboxModels.Prefix{ + { + Prefix: &parentPrefix, + ID: parentPrefixId, + Family: &netboxModels.PrefixFamily{Label: &prefixFamilyLabel, Value: &prefixFamily}, + }, + }, + }, + } + + prefixAvailableListInput := ipam.NewIpamPrefixesAvailablePrefixesListParams().WithID(parentPrefixId) + prefixAvailableListOutput := &ipam.IpamPrefixesAvailablePrefixesListOK{ + Payload: []*netboxModels.AvailablePrefix{ + { + Family: prefixFamily, + Prefix: parentPrefix, + }, + }, + } + + // get prefix to check if it's a candidate + expectedCustomFieldName := "environment" + expectedCustomFields := &extras.ExtrasCustomFieldsListOK{ + Payload: &extras.ExtrasCustomFieldsListOKBody{ + Results: []*netboxModels.CustomField{ + { + Name: &expectedCustomFieldName, + }, + }, + }, + } + + mockPrefixIpam.EXPECT().IpamPrefixesList(prefixListInput, nil, gomock.Any()).Return(prefixListOutput, nil).Times(1) + mockPrefixIpam.EXPECT().IpamPrefixesList(prefixListInputWithParam, nil).Return(prefixListOutputWithParam, nil).Times(1) + mockPrefixIpam.EXPECT().IpamPrefixesAvailablePrefixesList(prefixAvailableListInput, nil).Return(prefixAvailableListOutput, nil).AnyTimes() + mockTenancy.EXPECT().TenancyTenantsList(gomock.Any(), nil).Return(expectedTenant, nil).AnyTimes() + mockDcim.EXPECT().DcimSitesList(inputSite, nil).Return(expectedSite, nil).AnyTimes() + mockExtras.EXPECT().ExtrasCustomFieldsList(extras.NewExtrasCustomFieldsListParams(), gomock.Any(), gomock.Any()).Return(expectedCustomFields, nil).AnyTimes() + + netboxClient := &NetboxClient{ + Ipam: mockPrefixIpam, + Tenancy: mockTenancy, + Extras: mockExtras, + Dcim: mockDcim, + } + + actual, err := netboxClient.GetAvailablePrefixesByParentPrefixSelector(&pxcSpec) + + assert.Nil(t, err) + assert.Equal(t, parentPrefix, actual[0].Prefix) +} + +func TestPrefixClaim_GetAvailablePrefixByParentPrefixSelectorFailIfNonExistingFieldInParentPrefixSelector(t *testing.T) { + ctrl := gomock.NewController(t) + defer ctrl.Finish() + + pxcSpec := netboxv1.PrefixClaimSpec{ + ParentPrefixSelector: map[string]string{ + "non-existing": "non-existing", + "environment": "dev", + "family": "IPv4", + "tenant": "tenant", + "site": "Site1", + }, + PrefixLength: "/32", + } + + mockTenancy := mock_interfaces.NewMockTenancyInterface(ctrl) + mockPrefixIpam := mock_interfaces.NewMockIpamInterface(ctrl) + mockExtras := mock_interfaces.NewMockExtrasInterface(ctrl) + mockDcim := mock_interfaces.NewMockDcimInterface(ctrl) + + // example of site + siteId := int64(3) + siteName := "Site1" + siteOutputSlug := "site1" + expectedSite := &dcim.DcimSitesListOK{ + Payload: &dcim.DcimSitesListOKBody{ + Results: []*netboxModels.Site{ + { + ID: siteId, + Name: &siteName, + Slug: &siteOutputSlug, + }, + }, + }, + } + inputSite := dcim.NewDcimSitesListParams().WithName(&siteName) + + // tenant + tenantName := "tenant" + tenantId := int64(2) + tenantOutputSlug := "tenant1" + + expectedTenant := &tenancy.TenancyTenantsListOK{ + Payload: &tenancy.TenancyTenantsListOKBody{ + Results: []*netboxModels.Tenant{ + { + ID: tenantId, + Name: &tenantName, + Slug: &tenantOutputSlug, + }, + }, + }, + } + + // get prefix to check if it's a candidate + expectedCustomFieldName := "environment" + expectedCustomFields := &extras.ExtrasCustomFieldsListOK{ + Payload: &extras.ExtrasCustomFieldsListOKBody{ + Results: []*netboxModels.CustomField{ + { + Name: &expectedCustomFieldName, + }, + }, + }, + } + + mockTenancy.EXPECT().TenancyTenantsList(gomock.Any(), nil).Return(expectedTenant, nil).AnyTimes() + mockDcim.EXPECT().DcimSitesList(inputSite, nil).Return(expectedSite, nil).AnyTimes() + mockExtras.EXPECT().ExtrasCustomFieldsList(extras.NewExtrasCustomFieldsListParams(), gomock.Any(), gomock.Any()).Return(expectedCustomFields, nil).AnyTimes() + + netboxClient := &NetboxClient{ + Ipam: mockPrefixIpam, + Tenancy: mockTenancy, + Extras: mockExtras, + Dcim: mockDcim, + } + + actual, err := netboxClient.GetAvailablePrefixesByParentPrefixSelector(&pxcSpec) + + assert.Nil(t, actual) + AssertError(t, err, "invalid parentPrefixSelector, netbox custom fields [non-existing] do not exist") +} From 27a8d939b7aabf3d73cdcdedb5fb5c2f7d60de4f Mon Sep 17 00:00:00 2001 From: bruelea <166021996+bruelea@users.noreply.github.com> Date: Fri, 2 May 2025 10:39:13 +0200 Subject: [PATCH 03/11] add chainsaw test --- .../chainsaw-test.yaml | 47 +++++++++++++++++++ .../netbox_v1_prefixclaim.yaml | 23 +++++++++ 2 files changed, 70 insertions(+) create mode 100644 tests/e2e/Prefix/IPv4/prefixclaim-ipv4-parentprefixselector-nonexisingcustomfield/chainsaw-test.yaml create mode 100644 tests/e2e/Prefix/IPv4/prefixclaim-ipv4-parentprefixselector-nonexisingcustomfield/netbox_v1_prefixclaim.yaml diff --git a/tests/e2e/Prefix/IPv4/prefixclaim-ipv4-parentprefixselector-nonexisingcustomfield/chainsaw-test.yaml b/tests/e2e/Prefix/IPv4/prefixclaim-ipv4-parentprefixselector-nonexisingcustomfield/chainsaw-test.yaml new file mode 100644 index 00000000..5f3c470d --- /dev/null +++ b/tests/e2e/Prefix/IPv4/prefixclaim-ipv4-parentprefixselector-nonexisingcustomfield/chainsaw-test.yaml @@ -0,0 +1,47 @@ +--- +apiVersion: chainsaw.kyverno.io/v1alpha1 +kind: Test +metadata: + name: prefixclaim-ipv4-parentprefixselector-nonexisingcustomfield + annotations: + description: Tests if creation fails for parentPrefixSelector with non exising custom field +spec: + steps: + - name: Apply CR + try: + - apply: + file: netbox_v1_prefixclaim.yaml + - name: Check CR spec + try: + - assert: + resource: + apiVersion: netbox.dev/v1 + kind: PrefixClaim + metadata: + name: prefixclaim-ipv4-parentprefixselector-nonexisingcustomfield + spec: + comments: your comments + description: some description + parentPrefixSelector: # The keys and values are case-sensitive + tenant: "MY_TENANT" + site: "MY_SITE" + family: "IPv4" + environment: "Production" + poolName: "Pool 1" + nonexisingfield: "value" + prefixLength: /31 + preserveInNetbox: false + site: MY_SITE_2 + tenant: MY_TENANT_2 + - name: Check CR status + try: + - assert: + resource: + apiVersion: netbox.dev/v1 + kind: PrefixClaim + metadata: + name: prefixclaim-ipv4-parentprefixselector-nonexisingcustomfield + status: + (conditions[?type == 'PrefixAssigned']): + - status: 'False' + reason: 'PrefixCRNotCreated' diff --git a/tests/e2e/Prefix/IPv4/prefixclaim-ipv4-parentprefixselector-nonexisingcustomfield/netbox_v1_prefixclaim.yaml b/tests/e2e/Prefix/IPv4/prefixclaim-ipv4-parentprefixselector-nonexisingcustomfield/netbox_v1_prefixclaim.yaml new file mode 100644 index 00000000..6bf76dfd --- /dev/null +++ b/tests/e2e/Prefix/IPv4/prefixclaim-ipv4-parentprefixselector-nonexisingcustomfield/netbox_v1_prefixclaim.yaml @@ -0,0 +1,23 @@ +--- +apiVersion: netbox.dev/v1 +kind: PrefixClaim +metadata: + labels: + app.kubernetes.io/name: netbox-operator + app.kubernetes.io/managed-by: kustomize + name: prefixclaim-ipv4-parentprefixselector-nonexisingcustomfield +spec: + tenant: "MY_TENANT_2" # Use the `name` value instead of the `slug` value + site: "MY_SITE_2" # Use the `name` value instead of the `slug` value + description: "some description" + comments: "your comments" + preserveInNetbox: false + prefixLength: "/31" + parentPrefixSelector: # The keys and values are case-sensitive + tenant: "MY_TENANT" # Use the `name` value instead of the `slug` value + site: "MY_SITE" # Use the `name` value instead of the `slug` value + family: "IPv4" # Can only be either IPv4 or IPv6" + # custom fields of your interest + environment: "Production" + poolName: "Pool 1" + nonexisingfield: "value" From 4f883d8a14cef3baa57431bce03ca7fc874df871 Mon Sep 17 00:00:00 2001 From: bruelea <166021996+bruelea@users.noreply.github.com> Date: Fri, 2 May 2025 11:11:36 +0200 Subject: [PATCH 04/11] fix e2e tests --- .../chainsaw-test.yaml | 14 ++++++++++++++ .../chainsaw-test.yaml | 2 +- .../chainsaw-test.yaml | 2 +- .../chainsaw-test.yaml | 2 +- 4 files changed, 17 insertions(+), 3 deletions(-) diff --git a/tests/e2e/Prefix/IPv4/prefixclaim-ipv4-parentprefixselector-nonexisingcustomfield/chainsaw-test.yaml b/tests/e2e/Prefix/IPv4/prefixclaim-ipv4-parentprefixselector-nonexisingcustomfield/chainsaw-test.yaml index 5f3c470d..fdb4481f 100644 --- a/tests/e2e/Prefix/IPv4/prefixclaim-ipv4-parentprefixselector-nonexisingcustomfield/chainsaw-test.yaml +++ b/tests/e2e/Prefix/IPv4/prefixclaim-ipv4-parentprefixselector-nonexisingcustomfield/chainsaw-test.yaml @@ -45,3 +45,17 @@ spec: (conditions[?type == 'PrefixAssigned']): - status: 'False' reason: 'PrefixCRNotCreated' + - assert: + resource: + apiVersion: v1 + count: 1 + kind: Event + type: Warning + reason: PrefixCRNotCreated + source: + component: prefix-claim-controller + message: "Failed to assign prefix, prefix CR creation skipped: invalid parentPrefixSelector, netbox custom fields [nonexisingfield] do not exist" + involvedObject: + apiVersion: netbox.dev/v1 + kind: PrefixClaim + name: prefixclaim-ipv4-parentprefixselector-nonexisingcustomfield diff --git a/tests/e2e/Prefix/IPv4/prefixclaim-ipv4-prefixexhausted/chainsaw-test.yaml b/tests/e2e/Prefix/IPv4/prefixclaim-ipv4-prefixexhausted/chainsaw-test.yaml index a371ca73..38ba3b2b 100644 --- a/tests/e2e/Prefix/IPv4/prefixclaim-ipv4-prefixexhausted/chainsaw-test.yaml +++ b/tests/e2e/Prefix/IPv4/prefixclaim-ipv4-prefixexhausted/chainsaw-test.yaml @@ -94,7 +94,7 @@ spec: reason: PrefixCRNotCreated source: component: prefix-claim-controller - message: "Failed to fetch new Prefix from NetBox: parent prefix exhausted, will restart the parent prefix selection process" + message: "Failed to assign prefix, prefix CR creation skipped: parent prefix exhausted, will restart the parent prefix selection process" involvedObject: apiVersion: netbox.dev/v1 kind: PrefixClaim diff --git a/tests/e2e/ipaddress/ipv4/ipaddressclaim-ipv4-prefixexhausted/chainsaw-test.yaml b/tests/e2e/ipaddress/ipv4/ipaddressclaim-ipv4-prefixexhausted/chainsaw-test.yaml index 2c9cafe4..6c615d5e 100644 --- a/tests/e2e/ipaddress/ipv4/ipaddressclaim-ipv4-prefixexhausted/chainsaw-test.yaml +++ b/tests/e2e/ipaddress/ipv4/ipaddressclaim-ipv4-prefixexhausted/chainsaw-test.yaml @@ -129,7 +129,7 @@ spec: reason: IPAddressCRNotCreated source: component: ip-address-claim-controller - message: "Failed to fetch new IP from NetBox: parent prefix exhausted" + message: "Failed to assign prefix, prefix CR creation skipped: parent prefix exhausted" involvedObject: apiVersion: netbox.dev/v1 kind: IpAddressClaim diff --git a/tests/e2e/ipaddress/ipv6/ipaddressclaim-ipv6-prefixexhausted/chainsaw-test.yaml b/tests/e2e/ipaddress/ipv6/ipaddressclaim-ipv6-prefixexhausted/chainsaw-test.yaml index a5b0f503..fcf024ac 100644 --- a/tests/e2e/ipaddress/ipv6/ipaddressclaim-ipv6-prefixexhausted/chainsaw-test.yaml +++ b/tests/e2e/ipaddress/ipv6/ipaddressclaim-ipv6-prefixexhausted/chainsaw-test.yaml @@ -129,7 +129,7 @@ spec: reason: IPAddressCRNotCreated source: component: ip-address-claim-controller - message: "Failed to fetch new IP from NetBox: parent prefix exhausted" + message: "Failed to assign prefix, prefix CR creation skipped: parent prefix exhausted" involvedObject: apiVersion: netbox.dev/v1 kind: IpAddressClaim From 996e5ba8cd0adb15bde37f654244cab7cd2e2a58 Mon Sep 17 00:00:00 2001 From: bruelea <166021996+bruelea@users.noreply.github.com> Date: Fri, 2 May 2025 13:41:28 +0200 Subject: [PATCH 05/11] add cleanup step to all e2e test cases --- internal/controller/prefixclaim_controller.go | 2 +- .../chainsaw-test.yaml | 16 ++++++++++++---- .../chainsaw-test.yaml | 9 +++++++++ .../chainsaw-test.yaml | 10 ++++++++++ .../chainsaw-test.yaml | 10 +++++++++- .../chainsaw-test.yaml | 12 +++++++++++- .../chainsaw-test.yaml | 9 +++++++++ .../chainsaw-test.yaml | 12 +++++++++++- .../chainsaw-test.yaml | 9 +++++++++ .../chainsaw-test.yaml | 9 +++++++++ .../chainsaw-test.yaml | 14 ++++++++++++-- .../chainsaw-test.yaml | 10 ++++++++++ .../chainsaw-test.yaml | 9 +++++++++ .../chainsaw-test.yaml | 14 ++++++++++++-- .../chainsaw-test.yaml | 10 ++++++++++ .../chainsaw-test.yaml | 1 - .../chainsaw-test.yaml | 1 - .../chainsaw-test.yaml | 1 - .../chainsaw-test.yaml | 1 - 19 files changed, 143 insertions(+), 16 deletions(-) diff --git a/internal/controller/prefixclaim_controller.go b/internal/controller/prefixclaim_controller.go index ce3a6ce2..f769bd96 100644 --- a/internal/controller/prefixclaim_controller.go +++ b/internal/controller/prefixclaim_controller.go @@ -156,7 +156,7 @@ func (r *PrefixClaimReconciler) Reconcile(ctx context.Context, req ctrl.Request) // fetch available prefixes from netbox parentPrefixCandidates, err := r.NetboxClient.GetAvailablePrefixesByParentPrefixSelector(&prefixClaim.Spec) if err != nil || len(parentPrefixCandidates) == 0 { - r.EventStatusRecorder.Recorder().Event(prefixClaim, corev1.EventTypeWarning, netboxv1.ConditionPrefixAssignedFalse.Reason, err.Error()) + r.EventStatusRecorder.Recorder().Event(prefixClaim, corev1.EventTypeWarning, netboxv1.ConditionPrefixAssignedFalse.Reason, netboxv1.ConditionPrefixAssignedFalse.Message+": "+err.Error()) if err := r.EventStatusRecorder.Report(ctx, prefixClaim, netboxv1.ConditionPrefixAssignedFalse, corev1.EventTypeWarning, err); err != nil { return ctrl.Result{}, err } diff --git a/tests/e2e/Prefix/IPv4/prefixclaim-ipv4-invalid-parentprefixselector/chainsaw-test.yaml b/tests/e2e/Prefix/IPv4/prefixclaim-ipv4-invalid-parentprefixselector/chainsaw-test.yaml index 34a876ef..2f3232e1 100644 --- a/tests/e2e/Prefix/IPv4/prefixclaim-ipv4-invalid-parentprefixselector/chainsaw-test.yaml +++ b/tests/e2e/Prefix/IPv4/prefixclaim-ipv4-invalid-parentprefixselector/chainsaw-test.yaml @@ -34,19 +34,27 @@ spec: environment: "nilenv" poolName: "nilpool" status: - (conditions[?type == 'ParentPrefixSelected']): + (conditions[?type == 'PrefixAssigned']): - status: 'False' - assert: resource: apiVersion: v1 - count: 1 kind: Event type: Warning - reason: ParentPrefixNotSelected + reason: PrefixCRNotCreated source: component: prefix-claim-controller - message: "The parent prefix was not able to be selected: no parent prefix can be obtained with the query conditions set in ParentPrefixSelector, err = failed to fetch tenant 'niltenant': not found, number of candidates = 0" + message: "Failed to assign prefix, prefix CR creation skipped: failed to fetch tenant 'niltenant': not found" involvedObject: apiVersion: netbox.dev/v1 kind: PrefixClaim name: prefixclaim-ipv4-invalid-parentprefixselector + - name: Cleanup events + description: Events cleanup required to fix issues with failing tests that assert the wrong Error resource + cleanup: + - script: + env: + - name: NAMESPACE + value: ($namespace) + content: | + kubectl delete events --field-selector involvedObject.name=prefixclaim-ipv4-invalid-parentprefixselector -n $NAMESPACE diff --git a/tests/e2e/Prefix/IPv4/prefixclaim-ipv4-parentprefix-apply-update/chainsaw-test.yaml b/tests/e2e/Prefix/IPv4/prefixclaim-ipv4-parentprefix-apply-update/chainsaw-test.yaml index 11926289..43feb10e 100644 --- a/tests/e2e/Prefix/IPv4/prefixclaim-ipv4-parentprefix-apply-update/chainsaw-test.yaml +++ b/tests/e2e/Prefix/IPv4/prefixclaim-ipv4-parentprefix-apply-update/chainsaw-test.yaml @@ -79,3 +79,12 @@ spec: prefix: 2.0.2.0/28 site: MY_SITE tenant: MY_TENANT + - name: Cleanup events + description: Events cleanup required to fix issues with failing tests that assert the wrong Error resource + cleanup: + - script: + env: + - name: NAMESPACE + value: ($namespace) + content: | + kubectl delete events --field-selector involvedObject.name=prefixclaim-ipv4-parentprefix-apply-update -n $NAMESPACE diff --git a/tests/e2e/Prefix/IPv4/prefixclaim-ipv4-parentprefix-restore/chainsaw-test.yaml b/tests/e2e/Prefix/IPv4/prefixclaim-ipv4-parentprefix-restore/chainsaw-test.yaml index 4bad5a2d..c51cc549 100644 --- a/tests/e2e/Prefix/IPv4/prefixclaim-ipv4-parentprefix-restore/chainsaw-test.yaml +++ b/tests/e2e/Prefix/IPv4/prefixclaim-ipv4-parentprefix-restore/chainsaw-test.yaml @@ -130,3 +130,13 @@ spec: tenant: MY_TENANT customFields: netboxOperatorRestorationHash: 00b8772de73cdac083b0732d5bb85ab4f0caa16c + - name: Cleanup events + description: Events cleanup required to fix issues with failing tests that assert the wrong Error resource + cleanup: + - script: + env: + - name: NAMESPACE + value: ($namespace) + content: | + kubectl delete events --field-selector involvedObject.name=prefixclaim-ipv4-parentprefix-restore-1 -n $NAMESPACE + kubectl delete events --field-selector involvedObject.name=prefixclaim-ipv4-parentprefix-restore-2 -n $NAMESPACE diff --git a/tests/e2e/Prefix/IPv4/prefixclaim-ipv4-parentprefixselector-nonexisingcustomfield/chainsaw-test.yaml b/tests/e2e/Prefix/IPv4/prefixclaim-ipv4-parentprefixselector-nonexisingcustomfield/chainsaw-test.yaml index fdb4481f..6422f31d 100644 --- a/tests/e2e/Prefix/IPv4/prefixclaim-ipv4-parentprefixselector-nonexisingcustomfield/chainsaw-test.yaml +++ b/tests/e2e/Prefix/IPv4/prefixclaim-ipv4-parentprefixselector-nonexisingcustomfield/chainsaw-test.yaml @@ -48,7 +48,6 @@ spec: - assert: resource: apiVersion: v1 - count: 1 kind: Event type: Warning reason: PrefixCRNotCreated @@ -59,3 +58,12 @@ spec: apiVersion: netbox.dev/v1 kind: PrefixClaim name: prefixclaim-ipv4-parentprefixselector-nonexisingcustomfield + - name: Cleanup events + description: Events cleanup required to fix issues with failing tests that assert the wrong Error resource + cleanup: + - script: + env: + - name: NAMESPACE + value: ($namespace) + content: | + kubectl delete events --field-selector involvedObject.name=prefixclaim-ipv4-parentprefixselector-nonexisingcustomfield -n $NAMESPACE diff --git a/tests/e2e/Prefix/IPv4/prefixclaim-ipv4-parentprefixselector-restore/chainsaw-test.yaml b/tests/e2e/Prefix/IPv4/prefixclaim-ipv4-parentprefixselector-restore/chainsaw-test.yaml index 2d044a90..3329e139 100644 --- a/tests/e2e/Prefix/IPv4/prefixclaim-ipv4-parentprefixselector-restore/chainsaw-test.yaml +++ b/tests/e2e/Prefix/IPv4/prefixclaim-ipv4-parentprefixselector-restore/chainsaw-test.yaml @@ -102,7 +102,7 @@ spec: - name: Apply CR 1 for the second time again try: - apply: - file: netbox_v1_prefixclaim_1.yaml + file: netbox_v1_prefixclaim_1.yaml - name: Check CR 1 spec and status and make sure it is restored try: - assert: @@ -143,3 +143,13 @@ spec: tenant: MY_TENANT_2 customFields: netboxOperatorRestorationHash: 8a5e15cd391ec02a7a2b2e316bc163f4fe46ef0b + - name: Cleanup events + description: Events cleanup required to fix issues with failing tests that assert the wrong Error resource + cleanup: + - script: + env: + - name: NAMESPACE + value: ($namespace) + content: | + kubectl delete events --field-selector involvedObject.name=prefixclaim-ipv4-parentprefixselector-restore-1 -n $NAMESPACE + kubectl delete events --field-selector involvedObject.name=prefixclaim-ipv4-parentprefixselector-restore-2 -n $NAMESPACE diff --git a/tests/e2e/Prefix/IPv4/prefixclaim-ipv4-parentprefixselector/chainsaw-test.yaml b/tests/e2e/Prefix/IPv4/prefixclaim-ipv4-parentprefixselector/chainsaw-test.yaml index c7f2244e..683a912f 100644 --- a/tests/e2e/Prefix/IPv4/prefixclaim-ipv4-parentprefixselector/chainsaw-test.yaml +++ b/tests/e2e/Prefix/IPv4/prefixclaim-ipv4-parentprefixselector/chainsaw-test.yaml @@ -50,3 +50,12 @@ spec: tenant: MY_TENANT_2 customFields: netboxOperatorRestorationHash: 46116345cc81820fdb412dc83e7147d4b1dc1afa + - name: Cleanup events + description: Events cleanup required to fix issues with failing tests that assert the wrong Error resource + cleanup: + - script: + env: + - name: NAMESPACE + value: ($namespace) + content: | + kubectl delete events --field-selector involvedObject.name=prefixclaim-ipv4-parentprefixselector-apply -n $NAMESPACE diff --git a/tests/e2e/Prefix/IPv4/prefixclaim-ipv4-prefixexhausted/chainsaw-test.yaml b/tests/e2e/Prefix/IPv4/prefixclaim-ipv4-prefixexhausted/chainsaw-test.yaml index 38ba3b2b..11a7d476 100644 --- a/tests/e2e/Prefix/IPv4/prefixclaim-ipv4-prefixexhausted/chainsaw-test.yaml +++ b/tests/e2e/Prefix/IPv4/prefixclaim-ipv4-prefixexhausted/chainsaw-test.yaml @@ -88,7 +88,6 @@ spec: - assert: resource: apiVersion: v1 - count: 1 kind: Event type: Warning reason: PrefixCRNotCreated @@ -99,3 +98,14 @@ spec: apiVersion: netbox.dev/v1 kind: PrefixClaim name: prefixclaim-ipv4-prefixexhausted-3 + - name: Cleanup events + description: Events cleanup required to fix issues with failing tests that assert the wrong Error resource + cleanup: + - script: + env: + - name: NAMESPACE + value: ($namespace) + content: | + kubectl delete events --field-selector involvedObject.name=prefixclaim-ipv4-prefixexhausted-1 -n $NAMESPACE + kubectl delete events --field-selector involvedObject.name=prefixclaim-ipv4-prefixexhausted-2 -n $NAMESPACE + kubectl delete events --field-selector involvedObject.name=prefixclaim-ipv4-prefixexhausted-3 -n $NAMESPACE diff --git a/tests/e2e/Prefix/IPv6/prefixclaim-ipv6-apply-update/chainsaw-test.yaml b/tests/e2e/Prefix/IPv6/prefixclaim-ipv6-apply-update/chainsaw-test.yaml index c35c0392..892c65fc 100644 --- a/tests/e2e/Prefix/IPv6/prefixclaim-ipv6-apply-update/chainsaw-test.yaml +++ b/tests/e2e/Prefix/IPv6/prefixclaim-ipv6-apply-update/chainsaw-test.yaml @@ -86,3 +86,12 @@ spec: prefix: 2::/124 preserveInNetbox: true tenant: MY_TENANT_2 + - name: Cleanup events + description: Events cleanup required to fix issues with failing tests that assert the wrong Error resource + cleanup: + - script: + env: + - name: NAMESPACE + value: ($namespace) + content: | + kubectl delete events --field-selector involvedObject.name=prefixclaim-ipv6-apply-update -n $NAMESPACE diff --git a/tests/e2e/ipaddress/ipv4/ipaddressclaim-ipv4-apply-update/chainsaw-test.yaml b/tests/e2e/ipaddress/ipv4/ipaddressclaim-ipv4-apply-update/chainsaw-test.yaml index f4e27930..1686fc5c 100644 --- a/tests/e2e/ipaddress/ipv4/ipaddressclaim-ipv4-apply-update/chainsaw-test.yaml +++ b/tests/e2e/ipaddress/ipv4/ipaddressclaim-ipv4-apply-update/chainsaw-test.yaml @@ -107,3 +107,12 @@ spec: status: (conditions[?type == 'Ready']): - status: 'True' + - name: Cleanup events + description: Events cleanup required to fix issues with failing tests that assert the wrong Error resource + cleanup: + - script: + env: + - name: NAMESPACE + value: ($namespace) + content: | + kubectl delete events --field-selector involvedObject.name=ipaddressclaim-ipv4-apply-update -n $NAMESPACE diff --git a/tests/e2e/ipaddress/ipv4/ipaddressclaim-ipv4-prefixexhausted/chainsaw-test.yaml b/tests/e2e/ipaddress/ipv4/ipaddressclaim-ipv4-prefixexhausted/chainsaw-test.yaml index 6c615d5e..e2e81147 100644 --- a/tests/e2e/ipaddress/ipv4/ipaddressclaim-ipv4-prefixexhausted/chainsaw-test.yaml +++ b/tests/e2e/ipaddress/ipv4/ipaddressclaim-ipv4-prefixexhausted/chainsaw-test.yaml @@ -123,14 +123,24 @@ spec: - assert: resource: apiVersion: v1 - count: 1 kind: Event type: Warning reason: IPAddressCRNotCreated source: component: ip-address-claim-controller - message: "Failed to assign prefix, prefix CR creation skipped: parent prefix exhausted" + message: "Failed to fetch new IP from NetBox: parent prefix exhausted" involvedObject: apiVersion: netbox.dev/v1 kind: IpAddressClaim name: ipaddressclaim-ipv4-prefixexhausted-3 + - name: Cleanup events + description: Events cleanup required to fix issues with failing tests that assert the wrong Error resource + cleanup: + - script: + env: + - name: NAMESPACE + value: ($namespace) + content: | + kubectl delete events --field-selector involvedObject.name=ipaddressclaim-ipv4-prefixexhausted-1 -n $NAMESPACE + kubectl delete events --field-selector involvedObject.name=ipaddressclaim-ipv4-prefixexhausted-2 -n $NAMESPACE + kubectl delete events --field-selector involvedObject.name=ipaddressclaim-ipv4-prefixexhausted-3 -n $NAMESPACE \ No newline at end of file diff --git a/tests/e2e/ipaddress/ipv4/ipaddressclaim-ipv4-restore/chainsaw-test.yaml b/tests/e2e/ipaddress/ipv4/ipaddressclaim-ipv4-restore/chainsaw-test.yaml index e441caaf..f5e35aa1 100644 --- a/tests/e2e/ipaddress/ipv4/ipaddressclaim-ipv4-restore/chainsaw-test.yaml +++ b/tests/e2e/ipaddress/ipv4/ipaddressclaim-ipv4-restore/chainsaw-test.yaml @@ -155,3 +155,13 @@ spec: status: (conditions[?type == 'Ready']): - status: 'True' + - name: Cleanup events + description: Events cleanup required to fix issues with failing tests that assert the wrong Error resource + cleanup: + - script: + env: + - name: NAMESPACE + value: ($namespace) + content: | + kubectl delete events --field-selector involvedObject.name=ipaddressclaim-ipv4-restore-1 -n $NAMESPACE + kubectl delete events --field-selector involvedObject.name=ipaddressclaim-ipv4-restore-2 -n $NAMESPACE diff --git a/tests/e2e/ipaddress/ipv6/ipaddressclaim-ipv6-apply-update/chainsaw-test.yaml b/tests/e2e/ipaddress/ipv6/ipaddressclaim-ipv6-apply-update/chainsaw-test.yaml index 53ecbb93..c34bc166 100644 --- a/tests/e2e/ipaddress/ipv6/ipaddressclaim-ipv6-apply-update/chainsaw-test.yaml +++ b/tests/e2e/ipaddress/ipv6/ipaddressclaim-ipv6-apply-update/chainsaw-test.yaml @@ -107,3 +107,12 @@ spec: status: (conditions[?type == 'Ready']): - status: 'True' + - name: Cleanup events + description: Events cleanup required to fix issues with failing tests that assert the wrong Error resource + cleanup: + - script: + env: + - name: NAMESPACE + value: ($namespace) + content: | + kubectl delete events --field-selector involvedObject.name=ipaddressclaim-ipv6-apply-update -n $NAMESPACE diff --git a/tests/e2e/ipaddress/ipv6/ipaddressclaim-ipv6-prefixexhausted/chainsaw-test.yaml b/tests/e2e/ipaddress/ipv6/ipaddressclaim-ipv6-prefixexhausted/chainsaw-test.yaml index fcf024ac..37935404 100644 --- a/tests/e2e/ipaddress/ipv6/ipaddressclaim-ipv6-prefixexhausted/chainsaw-test.yaml +++ b/tests/e2e/ipaddress/ipv6/ipaddressclaim-ipv6-prefixexhausted/chainsaw-test.yaml @@ -123,14 +123,24 @@ spec: - assert: resource: apiVersion: v1 - count: 1 kind: Event type: Warning reason: IPAddressCRNotCreated source: component: ip-address-claim-controller - message: "Failed to assign prefix, prefix CR creation skipped: parent prefix exhausted" + message: "Failed to fetch new IP from NetBox: parent prefix exhausted" involvedObject: apiVersion: netbox.dev/v1 kind: IpAddressClaim name: ipaddressclaim-ipv6-prefixexhausted-3 + - name: Cleanup events + description: Events cleanup required to fix issues with failing tests that assert the wrong Error resource and lease cleanup for preventing delays when using the same prefixes (e.g. with "invalid" tests) + cleanup: + - script: + env: + - name: NAMESPACE + value: ($namespace) + content: | + kubectl delete events --field-selector involvedObject.name=ipaddressclaim-ipv6-prefixexhausted-1 -n $NAMESPACE + kubectl delete events --field-selector involvedObject.name=ipaddressclaim-ipv6-prefixexhausted-2 -n $NAMESPACE + kubectl delete events --field-selector involvedObject.name=ipaddressclaim-ipv6-prefixexhausted-3 -n $NAMESPACE \ No newline at end of file diff --git a/tests/e2e/ipaddress/ipv6/ipaddressclaim-ipv6-restore/chainsaw-test.yaml b/tests/e2e/ipaddress/ipv6/ipaddressclaim-ipv6-restore/chainsaw-test.yaml index fce8cfc6..5a89c7b1 100644 --- a/tests/e2e/ipaddress/ipv6/ipaddressclaim-ipv6-restore/chainsaw-test.yaml +++ b/tests/e2e/ipaddress/ipv6/ipaddressclaim-ipv6-restore/chainsaw-test.yaml @@ -155,3 +155,13 @@ spec: status: (conditions[?type == 'Ready']): - status: 'True' + - name: Cleanup events + description: Events cleanup required to fix issues with failing tests that assert the wrong Error resource + cleanup: + - script: + env: + - name: NAMESPACE + value: ($namespace) + content: | + kubectl delete events --field-selector involvedObject.name=ipaddressclaim-ipv6-restore-1 -n $NAMESPACE + kubectl delete events --field-selector involvedObject.name=ipaddressclaim-ipv6-restore-2 -n $NAMESPACE diff --git a/tests/e2e/iprange/ipv4/iprangeclaim-ipv4-invalid-parentprefix/chainsaw-test.yaml b/tests/e2e/iprange/ipv4/iprangeclaim-ipv4-invalid-parentprefix/chainsaw-test.yaml index 641d343e..3309e820 100644 --- a/tests/e2e/iprange/ipv4/iprangeclaim-ipv4-invalid-parentprefix/chainsaw-test.yaml +++ b/tests/e2e/iprange/ipv4/iprangeclaim-ipv4-invalid-parentprefix/chainsaw-test.yaml @@ -24,7 +24,6 @@ spec: - assert: resource: apiVersion: v1 - count: 1 kind: Event type: Warning reason: IPRangeCRNotCreated diff --git a/tests/e2e/iprange/ipv4/iprangeclaim-ipv4-invalid-tenant/chainsaw-test.yaml b/tests/e2e/iprange/ipv4/iprangeclaim-ipv4-invalid-tenant/chainsaw-test.yaml index baf1024a..64057fd0 100644 --- a/tests/e2e/iprange/ipv4/iprangeclaim-ipv4-invalid-tenant/chainsaw-test.yaml +++ b/tests/e2e/iprange/ipv4/iprangeclaim-ipv4-invalid-tenant/chainsaw-test.yaml @@ -30,7 +30,6 @@ spec: - assert: resource: apiVersion: v1 - count: 1 kind: Event type: Warning reason: IPRangeCRNotCreated diff --git a/tests/e2e/iprange/ipv4/iprangeclaim-ipv4-prefixexhausted/chainsaw-test.yaml b/tests/e2e/iprange/ipv4/iprangeclaim-ipv4-prefixexhausted/chainsaw-test.yaml index 77691c69..2b6e0ab2 100644 --- a/tests/e2e/iprange/ipv4/iprangeclaim-ipv4-prefixexhausted/chainsaw-test.yaml +++ b/tests/e2e/iprange/ipv4/iprangeclaim-ipv4-prefixexhausted/chainsaw-test.yaml @@ -132,7 +132,6 @@ spec: - assert: resource: apiVersion: v1 - count: 1 kind: Event type: Warning reason: IPRangeCRNotCreated diff --git a/tests/e2e/iprange/ipv6/iprangeclaim-ipv6-prefixexhausted/chainsaw-test.yaml b/tests/e2e/iprange/ipv6/iprangeclaim-ipv6-prefixexhausted/chainsaw-test.yaml index 173d7bc1..5de23fd5 100644 --- a/tests/e2e/iprange/ipv6/iprangeclaim-ipv6-prefixexhausted/chainsaw-test.yaml +++ b/tests/e2e/iprange/ipv6/iprangeclaim-ipv6-prefixexhausted/chainsaw-test.yaml @@ -133,7 +133,6 @@ spec: - assert: resource: apiVersion: v1 - count: 1 kind: Event type: Warning reason: IPRangeCRNotCreated From 36ae70c2cea216773d1b715485a3233bdfb70b37 Mon Sep 17 00:00:00 2001 From: bruelea <166021996+bruelea@users.noreply.github.com> Date: Tue, 6 May 2025 09:58:07 +0200 Subject: [PATCH 06/11] improve formatting of error message Co-authored-by: Fabian <30692464+faebr@users.noreply.github.com> --- pkg/netbox/api/prefix_claim.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/pkg/netbox/api/prefix_claim.go b/pkg/netbox/api/prefix_claim.go index c6d4d9a4..65b3d61f 100644 --- a/pkg/netbox/api/prefix_claim.go +++ b/pkg/netbox/api/prefix_claim.go @@ -202,7 +202,10 @@ func (r *NetboxClient) customFieldsExistsOrErr(customfieldFilterEntries []Custom } if len(missingCustomFields) > 0 { - return fmt.Errorf("invalid parentPrefixSelector, netbox custom fields %v do not exist", missingCustomFields) + return fmt.Errorf( + "invalid parentPrefixSelector, netbox custom fields %s do not exist", + strings.Join(missingCustomFields, ", "), + ) } return nil From 944fd55a7cc29d977a68cfaef2b0afa81b87a38d Mon Sep 17 00:00:00 2001 From: bruelea <166021996+bruelea@users.noreply.github.com> Date: Tue, 6 May 2025 10:03:19 +0200 Subject: [PATCH 07/11] remove outdated comment --- pkg/netbox/api/prefix_claim.go | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/pkg/netbox/api/prefix_claim.go b/pkg/netbox/api/prefix_claim.go index 65b3d61f..33f9f904 100644 --- a/pkg/netbox/api/prefix_claim.go +++ b/pkg/netbox/api/prefix_claim.go @@ -195,7 +195,6 @@ func (r *NetboxClient) customFieldsExistsOrErr(customfieldFilterEntries []Custom missingCustomFields := make([]string, 0) for _, entry := range customfieldFilterEntries { - //request to netbox to check if the custom field existse if !slices.Contains(customFieldNames, entry.key) { missingCustomFields = append(missingCustomFields, entry.key) } @@ -203,8 +202,8 @@ func (r *NetboxClient) customFieldsExistsOrErr(customfieldFilterEntries []Custom if len(missingCustomFields) > 0 { return fmt.Errorf( - "invalid parentPrefixSelector, netbox custom fields %s do not exist", - strings.Join(missingCustomFields, ", "), + "invalid parentPrefixSelector, netbox custom fields %s do not exist", + strings.Join(missingCustomFields, ", "), ) } From ccfbb7f8ca3e021b9a2c4276d11549cef1a4a463 Mon Sep 17 00:00:00 2001 From: bruelea <166021996+bruelea@users.noreply.github.com> Date: Tue, 6 May 2025 10:17:26 +0200 Subject: [PATCH 08/11] fix tests --- pkg/netbox/api/prefix_claim_test.go | 2 +- .../chainsaw-test.yaml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/pkg/netbox/api/prefix_claim_test.go b/pkg/netbox/api/prefix_claim_test.go index 95c5fabd..75860140 100644 --- a/pkg/netbox/api/prefix_claim_test.go +++ b/pkg/netbox/api/prefix_claim_test.go @@ -1257,5 +1257,5 @@ func TestPrefixClaim_GetAvailablePrefixByParentPrefixSelectorFailIfNonExistingFi actual, err := netboxClient.GetAvailablePrefixesByParentPrefixSelector(&pxcSpec) assert.Nil(t, actual) - AssertError(t, err, "invalid parentPrefixSelector, netbox custom fields [non-existing] do not exist") + AssertError(t, err, "invalid parentPrefixSelector, netbox custom fields non-existing do not exist") } diff --git a/tests/e2e/Prefix/IPv4/prefixclaim-ipv4-parentprefixselector-nonexisingcustomfield/chainsaw-test.yaml b/tests/e2e/Prefix/IPv4/prefixclaim-ipv4-parentprefixselector-nonexisingcustomfield/chainsaw-test.yaml index 6422f31d..f4d7122b 100644 --- a/tests/e2e/Prefix/IPv4/prefixclaim-ipv4-parentprefixselector-nonexisingcustomfield/chainsaw-test.yaml +++ b/tests/e2e/Prefix/IPv4/prefixclaim-ipv4-parentprefixselector-nonexisingcustomfield/chainsaw-test.yaml @@ -53,7 +53,7 @@ spec: reason: PrefixCRNotCreated source: component: prefix-claim-controller - message: "Failed to assign prefix, prefix CR creation skipped: invalid parentPrefixSelector, netbox custom fields [nonexisingfield] do not exist" + message: "Failed to assign prefix, prefix CR creation skipped: invalid parentPrefixSelector, netbox custom fields nonexisingfield do not exist" involvedObject: apiVersion: netbox.dev/v1 kind: PrefixClaim From e2691514010f5befdeeca8536482305edcde481e Mon Sep 17 00:00:00 2001 From: bruelea <166021996+bruelea@users.noreply.github.com> Date: Tue, 6 May 2025 14:49:12 +0200 Subject: [PATCH 09/11] e2e test only delete leases if they are present --- .../chainsaw-test.yaml | 3 --- .../chainsaw-test.yaml | 3 --- .../chainsaw-test.yaml | 7 ++----- .../chainsaw-test.yaml | 3 --- .../chainsaw-test.yaml | 3 --- .../chainsaw-test.yaml | 3 --- .../prefixclaim-ipv4-prefixexhausted/chainsaw-test.yaml | 3 --- .../IPv6/prefixclaim-ipv6-apply-update/chainsaw-test.yaml | 3 --- .../ipaddressclaim-ipv4-apply-update/chainsaw-test.yaml | 3 --- .../chainsaw-test.yaml | 5 +---- .../ipv4/ipaddressclaim-ipv4-restore/chainsaw-test.yaml | 3 --- .../ipaddressclaim-ipv6-apply-update/chainsaw-test.yaml | 3 --- .../chainsaw-test.yaml | 5 +---- .../ipv6/ipaddressclaim-ipv6-restore/chainsaw-test.yaml | 3 --- .../iprangeclaim-ipv4-apply-update/chainsaw-test.yaml | 8 ++++---- .../iprangeclaim-ipv4-invalid-cidr/chainsaw-test.yaml | 8 ++++---- .../chainsaw-test.yaml | 8 ++++---- .../chainsaw-test.yaml | 8 ++++---- .../chainsaw-test.yaml | 8 ++++---- .../iprangeclaim-ipv4-invalid-size/chainsaw-test.yaml | 8 ++++---- .../iprangeclaim-ipv4-invalid-tenant/chainsaw-test.yaml | 8 ++++---- .../iprangeclaim-ipv4-prefixexhausted/chainsaw-test.yaml | 8 ++++---- .../ipv4/iprangeclaim-ipv4-restore/chainsaw-test.yaml | 8 ++++---- .../iprangeclaim-ipv6-apply-update/chainsaw-test.yaml | 8 ++++---- .../iprangeclaim-ipv6-prefixexhausted/chainsaw-test.yaml | 8 ++++---- .../ipv6/iprangeclaim-ipv6-restore/chainsaw-test.yaml | 8 ++++---- 26 files changed, 52 insertions(+), 94 deletions(-) diff --git a/tests/e2e/Prefix/IPv4/prefixclaim-ipv4-invalid-parentprefixselector/chainsaw-test.yaml b/tests/e2e/Prefix/IPv4/prefixclaim-ipv4-invalid-parentprefixselector/chainsaw-test.yaml index 2f3232e1..3e80e47b 100644 --- a/tests/e2e/Prefix/IPv4/prefixclaim-ipv4-invalid-parentprefixselector/chainsaw-test.yaml +++ b/tests/e2e/Prefix/IPv4/prefixclaim-ipv4-invalid-parentprefixselector/chainsaw-test.yaml @@ -53,8 +53,5 @@ spec: description: Events cleanup required to fix issues with failing tests that assert the wrong Error resource cleanup: - script: - env: - - name: NAMESPACE - value: ($namespace) content: | kubectl delete events --field-selector involvedObject.name=prefixclaim-ipv4-invalid-parentprefixselector -n $NAMESPACE diff --git a/tests/e2e/Prefix/IPv4/prefixclaim-ipv4-parentprefix-apply-update/chainsaw-test.yaml b/tests/e2e/Prefix/IPv4/prefixclaim-ipv4-parentprefix-apply-update/chainsaw-test.yaml index 43feb10e..f8a823e5 100644 --- a/tests/e2e/Prefix/IPv4/prefixclaim-ipv4-parentprefix-apply-update/chainsaw-test.yaml +++ b/tests/e2e/Prefix/IPv4/prefixclaim-ipv4-parentprefix-apply-update/chainsaw-test.yaml @@ -83,8 +83,5 @@ spec: description: Events cleanup required to fix issues with failing tests that assert the wrong Error resource cleanup: - script: - env: - - name: NAMESPACE - value: ($namespace) content: | kubectl delete events --field-selector involvedObject.name=prefixclaim-ipv4-parentprefix-apply-update -n $NAMESPACE diff --git a/tests/e2e/Prefix/IPv4/prefixclaim-ipv4-parentprefix-restore/chainsaw-test.yaml b/tests/e2e/Prefix/IPv4/prefixclaim-ipv4-parentprefix-restore/chainsaw-test.yaml index c51cc549..47523c01 100644 --- a/tests/e2e/Prefix/IPv4/prefixclaim-ipv4-parentprefix-restore/chainsaw-test.yaml +++ b/tests/e2e/Prefix/IPv4/prefixclaim-ipv4-parentprefix-restore/chainsaw-test.yaml @@ -130,13 +130,10 @@ spec: tenant: MY_TENANT customFields: netboxOperatorRestorationHash: 00b8772de73cdac083b0732d5bb85ab4f0caa16c - - name: Cleanup events - description: Events cleanup required to fix issues with failing tests that assert the wrong Error resource + - name: Cleanup events and leases + description: Events cleanup required to fix issues with failing tests that assert the wrong Error resource and lease cleanup for preventing delays when using the same prefixes (e.g. with "invalid" tests) cleanup: - script: - env: - - name: NAMESPACE - value: ($namespace) content: | kubectl delete events --field-selector involvedObject.name=prefixclaim-ipv4-parentprefix-restore-1 -n $NAMESPACE kubectl delete events --field-selector involvedObject.name=prefixclaim-ipv4-parentprefix-restore-2 -n $NAMESPACE diff --git a/tests/e2e/Prefix/IPv4/prefixclaim-ipv4-parentprefixselector-nonexisingcustomfield/chainsaw-test.yaml b/tests/e2e/Prefix/IPv4/prefixclaim-ipv4-parentprefixselector-nonexisingcustomfield/chainsaw-test.yaml index f4d7122b..7f8807d6 100644 --- a/tests/e2e/Prefix/IPv4/prefixclaim-ipv4-parentprefixselector-nonexisingcustomfield/chainsaw-test.yaml +++ b/tests/e2e/Prefix/IPv4/prefixclaim-ipv4-parentprefixselector-nonexisingcustomfield/chainsaw-test.yaml @@ -62,8 +62,5 @@ spec: description: Events cleanup required to fix issues with failing tests that assert the wrong Error resource cleanup: - script: - env: - - name: NAMESPACE - value: ($namespace) content: | kubectl delete events --field-selector involvedObject.name=prefixclaim-ipv4-parentprefixselector-nonexisingcustomfield -n $NAMESPACE diff --git a/tests/e2e/Prefix/IPv4/prefixclaim-ipv4-parentprefixselector-restore/chainsaw-test.yaml b/tests/e2e/Prefix/IPv4/prefixclaim-ipv4-parentprefixselector-restore/chainsaw-test.yaml index 3329e139..ed66186a 100644 --- a/tests/e2e/Prefix/IPv4/prefixclaim-ipv4-parentprefixselector-restore/chainsaw-test.yaml +++ b/tests/e2e/Prefix/IPv4/prefixclaim-ipv4-parentprefixselector-restore/chainsaw-test.yaml @@ -147,9 +147,6 @@ spec: description: Events cleanup required to fix issues with failing tests that assert the wrong Error resource cleanup: - script: - env: - - name: NAMESPACE - value: ($namespace) content: | kubectl delete events --field-selector involvedObject.name=prefixclaim-ipv4-parentprefixselector-restore-1 -n $NAMESPACE kubectl delete events --field-selector involvedObject.name=prefixclaim-ipv4-parentprefixselector-restore-2 -n $NAMESPACE diff --git a/tests/e2e/Prefix/IPv4/prefixclaim-ipv4-parentprefixselector/chainsaw-test.yaml b/tests/e2e/Prefix/IPv4/prefixclaim-ipv4-parentprefixselector/chainsaw-test.yaml index 683a912f..cec6420b 100644 --- a/tests/e2e/Prefix/IPv4/prefixclaim-ipv4-parentprefixselector/chainsaw-test.yaml +++ b/tests/e2e/Prefix/IPv4/prefixclaim-ipv4-parentprefixselector/chainsaw-test.yaml @@ -54,8 +54,5 @@ spec: description: Events cleanup required to fix issues with failing tests that assert the wrong Error resource cleanup: - script: - env: - - name: NAMESPACE - value: ($namespace) content: | kubectl delete events --field-selector involvedObject.name=prefixclaim-ipv4-parentprefixselector-apply -n $NAMESPACE diff --git a/tests/e2e/Prefix/IPv4/prefixclaim-ipv4-prefixexhausted/chainsaw-test.yaml b/tests/e2e/Prefix/IPv4/prefixclaim-ipv4-prefixexhausted/chainsaw-test.yaml index 11a7d476..f8c7d436 100644 --- a/tests/e2e/Prefix/IPv4/prefixclaim-ipv4-prefixexhausted/chainsaw-test.yaml +++ b/tests/e2e/Prefix/IPv4/prefixclaim-ipv4-prefixexhausted/chainsaw-test.yaml @@ -102,9 +102,6 @@ spec: description: Events cleanup required to fix issues with failing tests that assert the wrong Error resource cleanup: - script: - env: - - name: NAMESPACE - value: ($namespace) content: | kubectl delete events --field-selector involvedObject.name=prefixclaim-ipv4-prefixexhausted-1 -n $NAMESPACE kubectl delete events --field-selector involvedObject.name=prefixclaim-ipv4-prefixexhausted-2 -n $NAMESPACE diff --git a/tests/e2e/Prefix/IPv6/prefixclaim-ipv6-apply-update/chainsaw-test.yaml b/tests/e2e/Prefix/IPv6/prefixclaim-ipv6-apply-update/chainsaw-test.yaml index 892c65fc..51b91fe1 100644 --- a/tests/e2e/Prefix/IPv6/prefixclaim-ipv6-apply-update/chainsaw-test.yaml +++ b/tests/e2e/Prefix/IPv6/prefixclaim-ipv6-apply-update/chainsaw-test.yaml @@ -90,8 +90,5 @@ spec: description: Events cleanup required to fix issues with failing tests that assert the wrong Error resource cleanup: - script: - env: - - name: NAMESPACE - value: ($namespace) content: | kubectl delete events --field-selector involvedObject.name=prefixclaim-ipv6-apply-update -n $NAMESPACE diff --git a/tests/e2e/ipaddress/ipv4/ipaddressclaim-ipv4-apply-update/chainsaw-test.yaml b/tests/e2e/ipaddress/ipv4/ipaddressclaim-ipv4-apply-update/chainsaw-test.yaml index 1686fc5c..5aa99f7f 100644 --- a/tests/e2e/ipaddress/ipv4/ipaddressclaim-ipv4-apply-update/chainsaw-test.yaml +++ b/tests/e2e/ipaddress/ipv4/ipaddressclaim-ipv4-apply-update/chainsaw-test.yaml @@ -111,8 +111,5 @@ spec: description: Events cleanup required to fix issues with failing tests that assert the wrong Error resource cleanup: - script: - env: - - name: NAMESPACE - value: ($namespace) content: | kubectl delete events --field-selector involvedObject.name=ipaddressclaim-ipv4-apply-update -n $NAMESPACE diff --git a/tests/e2e/ipaddress/ipv4/ipaddressclaim-ipv4-prefixexhausted/chainsaw-test.yaml b/tests/e2e/ipaddress/ipv4/ipaddressclaim-ipv4-prefixexhausted/chainsaw-test.yaml index e2e81147..dccd850a 100644 --- a/tests/e2e/ipaddress/ipv4/ipaddressclaim-ipv4-prefixexhausted/chainsaw-test.yaml +++ b/tests/e2e/ipaddress/ipv4/ipaddressclaim-ipv4-prefixexhausted/chainsaw-test.yaml @@ -137,10 +137,7 @@ spec: description: Events cleanup required to fix issues with failing tests that assert the wrong Error resource cleanup: - script: - env: - - name: NAMESPACE - value: ($namespace) content: | kubectl delete events --field-selector involvedObject.name=ipaddressclaim-ipv4-prefixexhausted-1 -n $NAMESPACE kubectl delete events --field-selector involvedObject.name=ipaddressclaim-ipv4-prefixexhausted-2 -n $NAMESPACE - kubectl delete events --field-selector involvedObject.name=ipaddressclaim-ipv4-prefixexhausted-3 -n $NAMESPACE \ No newline at end of file + kubectl delete events --field-selector involvedObject.name=ipaddressclaim-ipv4-prefixexhausted-3 -n $NAMESPACE diff --git a/tests/e2e/ipaddress/ipv4/ipaddressclaim-ipv4-restore/chainsaw-test.yaml b/tests/e2e/ipaddress/ipv4/ipaddressclaim-ipv4-restore/chainsaw-test.yaml index f5e35aa1..91a1631e 100644 --- a/tests/e2e/ipaddress/ipv4/ipaddressclaim-ipv4-restore/chainsaw-test.yaml +++ b/tests/e2e/ipaddress/ipv4/ipaddressclaim-ipv4-restore/chainsaw-test.yaml @@ -159,9 +159,6 @@ spec: description: Events cleanup required to fix issues with failing tests that assert the wrong Error resource cleanup: - script: - env: - - name: NAMESPACE - value: ($namespace) content: | kubectl delete events --field-selector involvedObject.name=ipaddressclaim-ipv4-restore-1 -n $NAMESPACE kubectl delete events --field-selector involvedObject.name=ipaddressclaim-ipv4-restore-2 -n $NAMESPACE diff --git a/tests/e2e/ipaddress/ipv6/ipaddressclaim-ipv6-apply-update/chainsaw-test.yaml b/tests/e2e/ipaddress/ipv6/ipaddressclaim-ipv6-apply-update/chainsaw-test.yaml index c34bc166..179bf2c1 100644 --- a/tests/e2e/ipaddress/ipv6/ipaddressclaim-ipv6-apply-update/chainsaw-test.yaml +++ b/tests/e2e/ipaddress/ipv6/ipaddressclaim-ipv6-apply-update/chainsaw-test.yaml @@ -111,8 +111,5 @@ spec: description: Events cleanup required to fix issues with failing tests that assert the wrong Error resource cleanup: - script: - env: - - name: NAMESPACE - value: ($namespace) content: | kubectl delete events --field-selector involvedObject.name=ipaddressclaim-ipv6-apply-update -n $NAMESPACE diff --git a/tests/e2e/ipaddress/ipv6/ipaddressclaim-ipv6-prefixexhausted/chainsaw-test.yaml b/tests/e2e/ipaddress/ipv6/ipaddressclaim-ipv6-prefixexhausted/chainsaw-test.yaml index 37935404..6f3e9a17 100644 --- a/tests/e2e/ipaddress/ipv6/ipaddressclaim-ipv6-prefixexhausted/chainsaw-test.yaml +++ b/tests/e2e/ipaddress/ipv6/ipaddressclaim-ipv6-prefixexhausted/chainsaw-test.yaml @@ -137,10 +137,7 @@ spec: description: Events cleanup required to fix issues with failing tests that assert the wrong Error resource and lease cleanup for preventing delays when using the same prefixes (e.g. with "invalid" tests) cleanup: - script: - env: - - name: NAMESPACE - value: ($namespace) content: | kubectl delete events --field-selector involvedObject.name=ipaddressclaim-ipv6-prefixexhausted-1 -n $NAMESPACE kubectl delete events --field-selector involvedObject.name=ipaddressclaim-ipv6-prefixexhausted-2 -n $NAMESPACE - kubectl delete events --field-selector involvedObject.name=ipaddressclaim-ipv6-prefixexhausted-3 -n $NAMESPACE \ No newline at end of file + kubectl delete events --field-selector involvedObject.name=ipaddressclaim-ipv6-prefixexhausted-3 -n $NAMESPACE diff --git a/tests/e2e/ipaddress/ipv6/ipaddressclaim-ipv6-restore/chainsaw-test.yaml b/tests/e2e/ipaddress/ipv6/ipaddressclaim-ipv6-restore/chainsaw-test.yaml index 5a89c7b1..6aae130b 100644 --- a/tests/e2e/ipaddress/ipv6/ipaddressclaim-ipv6-restore/chainsaw-test.yaml +++ b/tests/e2e/ipaddress/ipv6/ipaddressclaim-ipv6-restore/chainsaw-test.yaml @@ -159,9 +159,6 @@ spec: description: Events cleanup required to fix issues with failing tests that assert the wrong Error resource cleanup: - script: - env: - - name: NAMESPACE - value: ($namespace) content: | kubectl delete events --field-selector involvedObject.name=ipaddressclaim-ipv6-restore-1 -n $NAMESPACE kubectl delete events --field-selector involvedObject.name=ipaddressclaim-ipv6-restore-2 -n $NAMESPACE diff --git a/tests/e2e/iprange/ipv4/iprangeclaim-ipv4-apply-update/chainsaw-test.yaml b/tests/e2e/iprange/ipv4/iprangeclaim-ipv4-apply-update/chainsaw-test.yaml index a4b0044b..624075ea 100644 --- a/tests/e2e/iprange/ipv4/iprangeclaim-ipv4-apply-update/chainsaw-test.yaml +++ b/tests/e2e/iprange/ipv4/iprangeclaim-ipv4-apply-update/chainsaw-test.yaml @@ -121,9 +121,9 @@ spec: description: Events cleanup required to fix issues with failing tests that assert the wrong Error resource and lease cleanup for preventing delays when using the same prefixes (e.g. with "invalid" tests) cleanup: - script: - env: - - name: NAMESPACE - value: ($namespace) content: | - kubectl -n netbox-operator-system get lease -oname | grep -v netbox | xargs -n1 kubectl -n netbox-operator-system delete # to be enhanced in usage of leaselocker + LEASES=$(kubectl -n netbox-operator-system get lease -oname | grep -v netbox) # to be enhanced in usage of leaselocker + if [ -n "$LEASES" ]; then + echo "$LEASES" | xargs -n1 kubectl -n netbox-operator-system delete + fi kubectl delete events --field-selector involvedObject.name=iprangeclaim-ipv4-apply-update -n $NAMESPACE diff --git a/tests/e2e/iprange/ipv4/iprangeclaim-ipv4-invalid-cidr/chainsaw-test.yaml b/tests/e2e/iprange/ipv4/iprangeclaim-ipv4-invalid-cidr/chainsaw-test.yaml index 851f769e..458c3821 100644 --- a/tests/e2e/iprange/ipv4/iprangeclaim-ipv4-invalid-cidr/chainsaw-test.yaml +++ b/tests/e2e/iprange/ipv4/iprangeclaim-ipv4-invalid-cidr/chainsaw-test.yaml @@ -18,9 +18,9 @@ spec: description: Events cleanup required to fix issues with failing tests that assert the wrong Error resource and lease cleanup for preventing delays when using the same prefixes (e.g. with "invalid" tests) cleanup: - script: - env: - - name: NAMESPACE - value: ($namespace) content: | - kubectl -n netbox-operator-system get lease -oname | grep -v netbox | xargs -n1 kubectl -n netbox-operator-system delete # to be enhanced in usage of leaselocker + LEASES=$(kubectl -n netbox-operator-system get lease -oname | grep -v netbox) # to be enhanced in usage of leaselocker + if [ -n "$LEASES" ]; then + echo "$LEASES" | xargs -n1 kubectl -n netbox-operator-system delete + fi kubectl delete events --field-selector involvedObject.name=iprangeclaim-ipv4-invalid-cidr -n $NAMESPACE diff --git a/tests/e2e/iprange/ipv4/iprangeclaim-ipv4-invalid-customfieldnotexisting/chainsaw-test.yaml b/tests/e2e/iprange/ipv4/iprangeclaim-ipv4-invalid-customfieldnotexisting/chainsaw-test.yaml index e7f91f3e..8dc62cc9 100644 --- a/tests/e2e/iprange/ipv4/iprangeclaim-ipv4-invalid-customfieldnotexisting/chainsaw-test.yaml +++ b/tests/e2e/iprange/ipv4/iprangeclaim-ipv4-invalid-customfieldnotexisting/chainsaw-test.yaml @@ -64,9 +64,9 @@ spec: description: Events cleanup required to fix issues with failing tests that assert the wrong Error resource and lease cleanup for preventing delays when using the same prefixes (e.g. with "invalid" tests) cleanup: - script: - env: - - name: NAMESPACE - value: ($namespace) content: | - kubectl -n netbox-operator-system get lease -oname | grep -v netbox | xargs -n1 kubectl -n netbox-operator-system delete # to be enhanced in usage of leaselocker + LEASES=$(kubectl -n netbox-operator-system get lease -oname | grep -v netbox) # to be enhanced in usage of leaselocker + if [ -n "$LEASES" ]; then + echo "$LEASES" | xargs -n1 kubectl -n netbox-operator-system delete + fi kubectl delete events --field-selector involvedObject.name=iprangeclaim-ipv4-invalid-customfieldnotexisting -n $NAMESPACE diff --git a/tests/e2e/iprange/ipv4/iprangeclaim-ipv4-invalid-customfieldwrongdatatype/chainsaw-test.yaml b/tests/e2e/iprange/ipv4/iprangeclaim-ipv4-invalid-customfieldwrongdatatype/chainsaw-test.yaml index cabb572a..53c1150b 100644 --- a/tests/e2e/iprange/ipv4/iprangeclaim-ipv4-invalid-customfieldwrongdatatype/chainsaw-test.yaml +++ b/tests/e2e/iprange/ipv4/iprangeclaim-ipv4-invalid-customfieldwrongdatatype/chainsaw-test.yaml @@ -64,9 +64,9 @@ spec: description: Events cleanup required to fix issues with failing tests that assert the wrong Error resource and lease cleanup for preventing delays when using the same prefixes (e.g. with "invalid" tests) cleanup: - script: - env: - - name: NAMESPACE - value: ($namespace) content: | - kubectl -n netbox-operator-system get lease -oname | grep -v netbox | xargs -n1 kubectl -n netbox-operator-system delete # to be enhanced in usage of leaselocker + LEASES=$(kubectl -n netbox-operator-system get lease -oname | grep -v netbox) # to be enhanced in usage of leaselocker + if [ -n "$LEASES" ]; then + echo "$LEASES" | xargs -n1 kubectl -n netbox-operator-system delete + fi kubectl delete events --field-selector involvedObject.name=iprangeclaim-ipv4-invalid-customfieldwrongdatatype -n $NAMESPACE diff --git a/tests/e2e/iprange/ipv4/iprangeclaim-ipv4-invalid-parentprefix/chainsaw-test.yaml b/tests/e2e/iprange/ipv4/iprangeclaim-ipv4-invalid-parentprefix/chainsaw-test.yaml index 3309e820..d44868a4 100644 --- a/tests/e2e/iprange/ipv4/iprangeclaim-ipv4-invalid-parentprefix/chainsaw-test.yaml +++ b/tests/e2e/iprange/ipv4/iprangeclaim-ipv4-invalid-parentprefix/chainsaw-test.yaml @@ -38,9 +38,9 @@ spec: description: Events cleanup required to fix issues with failing tests that assert the wrong Error resource and lease cleanup for preventing delays when using the same prefixes (e.g. with "invalid" tests) cleanup: - script: - env: - - name: NAMESPACE - value: ($namespace) content: | - kubectl -n netbox-operator-system get lease -oname | grep -v netbox | xargs -n1 kubectl -n netbox-operator-system delete # to be enhanced in usage of leaselocker + LEASES=$(kubectl -n netbox-operator-system get lease -oname | grep -v netbox) # to be enhanced in usage of leaselocker + if [ -n "$LEASES" ]; then + echo "$LEASES" | xargs -n1 kubectl -n netbox-operator-system delete + fi kubectl delete events --field-selector involvedObject.name=iprangeclaim-ipv4-invalid-parentprefix -n $NAMESPACE diff --git a/tests/e2e/iprange/ipv4/iprangeclaim-ipv4-invalid-size/chainsaw-test.yaml b/tests/e2e/iprange/ipv4/iprangeclaim-ipv4-invalid-size/chainsaw-test.yaml index 6151f902..b31d4ccf 100644 --- a/tests/e2e/iprange/ipv4/iprangeclaim-ipv4-invalid-size/chainsaw-test.yaml +++ b/tests/e2e/iprange/ipv4/iprangeclaim-ipv4-invalid-size/chainsaw-test.yaml @@ -18,9 +18,9 @@ spec: description: Events cleanup required to fix issues with failing tests that assert the wrong Error resource and lease cleanup for preventing delays when using the same prefixes (e.g. with "invalid" tests) cleanup: - script: - env: - - name: NAMESPACE - value: ($namespace) content: | - kubectl -n netbox-operator-system get lease -oname | grep -v netbox | xargs -n1 kubectl -n netbox-operator-system delete # to be enhanced in usage of leaselocker + LEASES=$(kubectl -n netbox-operator-system get lease -oname | grep -v netbox) # to be enhanced in usage of leaselocker + if [ -n "$LEASES" ]; then + echo "$LEASES" | xargs -n1 kubectl -n netbox-operator-system delete + fi kubectl delete events --field-selector involvedObject.name=iprangeclaim-ipv4-invalid-size -n $NAMESPACE diff --git a/tests/e2e/iprange/ipv4/iprangeclaim-ipv4-invalid-tenant/chainsaw-test.yaml b/tests/e2e/iprange/ipv4/iprangeclaim-ipv4-invalid-tenant/chainsaw-test.yaml index 64057fd0..b799eae3 100644 --- a/tests/e2e/iprange/ipv4/iprangeclaim-ipv4-invalid-tenant/chainsaw-test.yaml +++ b/tests/e2e/iprange/ipv4/iprangeclaim-ipv4-invalid-tenant/chainsaw-test.yaml @@ -44,9 +44,9 @@ spec: description: Events cleanup required to fix issues with failing tests that assert the wrong Error resource and lease cleanup for preventing delays when using the same prefixes (e.g. with "invalid" tests) cleanup: - script: - env: - - name: NAMESPACE - value: ($namespace) content: | - kubectl -n netbox-operator-system get lease -oname | grep -v netbox | xargs -n1 kubectl -n netbox-operator-system delete # to be enhanced in usage of leaselocker + LEASES=$(kubectl -n netbox-operator-system get lease -oname | grep -v netbox) # to be enhanced in usage of leaselocker + if [ -n "$LEASES" ]; then + echo "$LEASES" | xargs -n1 kubectl -n netbox-operator-system delete + fi kubectl delete events --field-selector involvedObject.name=iprangeclaim-ipv4-invalid-tenant -n $NAMESPACE diff --git a/tests/e2e/iprange/ipv4/iprangeclaim-ipv4-prefixexhausted/chainsaw-test.yaml b/tests/e2e/iprange/ipv4/iprangeclaim-ipv4-prefixexhausted/chainsaw-test.yaml index 2b6e0ab2..19f493ba 100644 --- a/tests/e2e/iprange/ipv4/iprangeclaim-ipv4-prefixexhausted/chainsaw-test.yaml +++ b/tests/e2e/iprange/ipv4/iprangeclaim-ipv4-prefixexhausted/chainsaw-test.yaml @@ -146,11 +146,11 @@ spec: description: Events cleanup required to fix issues with failing tests that assert the wrong Error resource and lease cleanup for preventing delays when using the same prefixes (e.g. with "invalid" tests) cleanup: - script: - env: - - name: NAMESPACE - value: ($namespace) content: | - kubectl -n netbox-operator-system get lease -oname | grep -v netbox | xargs -n1 kubectl -n netbox-operator-system delete # to be enhanced in usage of leaselocker + LEASES=$(kubectl -n netbox-operator-system get lease -oname | grep -v netbox) # to be enhanced in usage of leaselocker + if [ -n "$LEASES" ]; then + echo "$LEASES" | xargs -n1 kubectl -n netbox-operator-system delete + fi kubectl delete events --field-selector involvedObject.name=iprangeclaim-ipv4-prefixexhausted-1 -n $NAMESPACE kubectl delete events --field-selector involvedObject.name=iprangeclaim-ipv4-prefixexhausted-2 -n $NAMESPACE kubectl delete events --field-selector involvedObject.name=iprangeclaim-ipv4-prefixexhausted-3 -n $NAMESPACE diff --git a/tests/e2e/iprange/ipv4/iprangeclaim-ipv4-restore/chainsaw-test.yaml b/tests/e2e/iprange/ipv4/iprangeclaim-ipv4-restore/chainsaw-test.yaml index 67b34ad6..22338434 100644 --- a/tests/e2e/iprange/ipv4/iprangeclaim-ipv4-restore/chainsaw-test.yaml +++ b/tests/e2e/iprange/ipv4/iprangeclaim-ipv4-restore/chainsaw-test.yaml @@ -176,10 +176,10 @@ spec: description: Events cleanup required to fix issues with failing tests that assert the wrong Error resource and lease cleanup for preventing delays when using the same prefixes (e.g. with "invalid" tests) cleanup: - script: - env: - - name: NAMESPACE - value: ($namespace) content: | - kubectl -n netbox-operator-system get lease -oname | grep -v netbox | xargs -n1 kubectl -n netbox-operator-system delete # to be enhanced in usage of leaselocker + LEASES=$(kubectl -n netbox-operator-system get lease -oname | grep -v netbox) # to be enhanced in usage of leaselocker + if [ -n "$LEASES" ]; then + echo "$LEASES" | xargs -n1 kubectl -n netbox-operator-system delete + fi kubectl delete events --field-selector involvedObject.name=iprangeclaim-ipv4-restore-1 -n $NAMESPACE kubectl delete events --field-selector involvedObject.name=iprangeclaim-ipv4-restore-2 -n $NAMESPACE diff --git a/tests/e2e/iprange/ipv6/iprangeclaim-ipv6-apply-update/chainsaw-test.yaml b/tests/e2e/iprange/ipv6/iprangeclaim-ipv6-apply-update/chainsaw-test.yaml index 54bd67f1..1498c518 100644 --- a/tests/e2e/iprange/ipv6/iprangeclaim-ipv6-apply-update/chainsaw-test.yaml +++ b/tests/e2e/iprange/ipv6/iprangeclaim-ipv6-apply-update/chainsaw-test.yaml @@ -121,9 +121,9 @@ spec: description: Events cleanup required to fix issues with failing tests that assert the wrong Error resource and lease cleanup for preventing delays when using the same prefixes (e.g. with "invalid" tests) cleanup: - script: - env: - - name: NAMESPACE - value: ($namespace) content: | - kubectl -n netbox-operator-system get lease -oname | grep -v netbox | xargs -n1 kubectl -n netbox-operator-system delete # to be enhanced in usage of leaselocker + LEASES=$(kubectl -n netbox-operator-system get lease -oname | grep -v netbox) # to be enhanced in usage of leaselocker + if [ -n "$LEASES" ]; then + echo "$LEASES" | xargs -n1 kubectl -n netbox-operator-system delete + fi kubectl delete events --field-selector involvedObject.name=iprangeclaim-ipv6-apply-update -n $NAMESPACE diff --git a/tests/e2e/iprange/ipv6/iprangeclaim-ipv6-prefixexhausted/chainsaw-test.yaml b/tests/e2e/iprange/ipv6/iprangeclaim-ipv6-prefixexhausted/chainsaw-test.yaml index 5de23fd5..a88d98fe 100644 --- a/tests/e2e/iprange/ipv6/iprangeclaim-ipv6-prefixexhausted/chainsaw-test.yaml +++ b/tests/e2e/iprange/ipv6/iprangeclaim-ipv6-prefixexhausted/chainsaw-test.yaml @@ -147,11 +147,11 @@ spec: description: Events cleanup required to fix issues with failing tests that assert the wrong Error resource and lease cleanup for preventing delays when using the same prefixes (e.g. with "invalid" tests) cleanup: - script: - env: - - name: NAMESPACE - value: ($namespace) content: | - kubectl -n netbox-operator-system get lease -oname | grep -v netbox | xargs -n1 kubectl -n netbox-operator-system delete # to be enhanced in usage of leaselocker + LEASES=$(kubectl -n netbox-operator-system get lease -oname | grep -v netbox) # to be enhanced in usage of leaselocker + if [ -n "$LEASES" ]; then + echo "$LEASES" | xargs -n1 kubectl -n netbox-operator-system delete + fi kubectl delete events --field-selector involvedObject.name=iprangeclaim-ipv6-prefixexhausted-1 -n $NAMESPACE kubectl delete events --field-selector involvedObject.name=iprangeclaim-ipv6-prefixexhausted-2 -n $NAMESPACE kubectl delete events --field-selector involvedObject.name=iprangeclaim-ipv6-prefixexhausted-3 -n $NAMESPACE diff --git a/tests/e2e/iprange/ipv6/iprangeclaim-ipv6-restore/chainsaw-test.yaml b/tests/e2e/iprange/ipv6/iprangeclaim-ipv6-restore/chainsaw-test.yaml index 31cbc2d5..957148bb 100644 --- a/tests/e2e/iprange/ipv6/iprangeclaim-ipv6-restore/chainsaw-test.yaml +++ b/tests/e2e/iprange/ipv6/iprangeclaim-ipv6-restore/chainsaw-test.yaml @@ -176,10 +176,10 @@ spec: description: Events cleanup required to fix issues with failing tests that assert the wrong Error resource and lease cleanup for preventing delays when using the same prefixes (e.g. with "invalid" tests) cleanup: - script: - env: - - name: NAMESPACE - value: ($namespace) content: | - kubectl -n netbox-operator-system get lease -oname | grep -v netbox | xargs -n1 kubectl -n netbox-operator-system delete # to be enhanced in usage of leaselocker + LEASES=$(kubectl -n netbox-operator-system get lease -oname | grep -v netbox) # to be enhanced in usage of leaselocker + if [ -n "$LEASES" ]; then + echo "$LEASES" | xargs -n1 kubectl -n netbox-operator-system delete + fi kubectl delete events --field-selector involvedObject.name=iprangeclaim-ipv6-restore-1 -n $NAMESPACE kubectl delete events --field-selector involvedObject.name=iprangeclaim-ipv6-restore-2 -n $NAMESPACE From a2c0d080090593068b894861e8139092ed0882f0 Mon Sep 17 00:00:00 2001 From: bruelea <166021996+bruelea@users.noreply.github.com> Date: Tue, 6 May 2025 15:59:54 +0200 Subject: [PATCH 10/11] e2e test add cleanup timeout --- Makefile | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Makefile b/Makefile index 7f99f55a..e8dcbc60 100644 --- a/Makefile +++ b/Makefile @@ -239,24 +239,24 @@ generate_mocks: ## TODO: auto install go install go.uber.org/mock/mockgen@latest mockgen -destination ${GEN_DIR}/${NETBOX_MOCKS_OUTPUT_FILE} -source=${INTERFACE_DEFITIONS_DIR} # e2e tests -E2E_PARAM := --namespace e2e --parallel 3 --apply-timeout 3m --assert-timeout 3m --delete-timeout 3m --error-timeout 3m --exec-timeout 3m # --skip-delete (add this argument for local debugging) +E2E_PARAM := --namespace e2e --parallel 3 --apply-timeout 3m --assert-timeout 3m --delete-timeout 3m --error-timeout 3m --exec-timeout 3m --cleanup-timeout 3m # --skip-delete (add this argument for local debugging) .PHONY: create-kind-3.7.8 create-kind-3.7.8: ./kind/local-env.sh --version 3.7.8 .PHONY: test-e2e-3.7.8 -test-e2e-3.7.8: create-kind-3.7.8 deploy-kind install-$(GO_PACKAGE_NAME_CHAINSAW) +test-e2e-3.7.8: create-kind-3.7.8 deploy-kind install-$(GO_PACKAGE_NAME_CHAINSAW) chainsaw test $(E2E_PARAM) .PHONY: create-kind-4.0.11 create-kind-4.0.11: ./kind/local-env.sh --version 4.0.11 .PHONY: test-e2e-4.0.11 -test-e2e-4.0.11: create-kind-4.0.11 deploy-kind install-$(GO_PACKAGE_NAME_CHAINSAW) +test-e2e-4.0.11: create-kind-4.0.11 deploy-kind install-$(GO_PACKAGE_NAME_CHAINSAW) chainsaw test $(E2E_PARAM) .PHONY: create-kind-4.1.8 create-kind-4.1.8: ./kind/local-env.sh --version 4.1.8 .PHONY: test-e2e-4.1.8 -test-e2e-4.1.8: create-kind-4.1.8 deploy-kind install-$(GO_PACKAGE_NAME_CHAINSAW) +test-e2e-4.1.8: create-kind-4.1.8 deploy-kind install-$(GO_PACKAGE_NAME_CHAINSAW) chainsaw test $(E2E_PARAM) From 0c119480575213bbc27da5b9032d2ddaa6e54145 Mon Sep 17 00:00:00 2001 From: bruelea <166021996+bruelea@users.noreply.github.com> Date: Tue, 6 May 2025 17:38:51 +0200 Subject: [PATCH 11/11] e2e test correct step name and description --- .../prefixclaim-ipv4-parentprefix-restore/chainsaw-test.yaml | 4 ++-- .../ipaddressclaim-ipv6-prefixexhausted/chainsaw-test.yaml | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/e2e/Prefix/IPv4/prefixclaim-ipv4-parentprefix-restore/chainsaw-test.yaml b/tests/e2e/Prefix/IPv4/prefixclaim-ipv4-parentprefix-restore/chainsaw-test.yaml index 47523c01..ff77b4d7 100644 --- a/tests/e2e/Prefix/IPv4/prefixclaim-ipv4-parentprefix-restore/chainsaw-test.yaml +++ b/tests/e2e/Prefix/IPv4/prefixclaim-ipv4-parentprefix-restore/chainsaw-test.yaml @@ -130,8 +130,8 @@ spec: tenant: MY_TENANT customFields: netboxOperatorRestorationHash: 00b8772de73cdac083b0732d5bb85ab4f0caa16c - - name: Cleanup events and leases - description: Events cleanup required to fix issues with failing tests that assert the wrong Error resource and lease cleanup for preventing delays when using the same prefixes (e.g. with "invalid" tests) + - name: Cleanup events + description: Events cleanup required to fix issues with failing tests that assert the wrong Error resource cleanup: - script: content: | diff --git a/tests/e2e/ipaddress/ipv6/ipaddressclaim-ipv6-prefixexhausted/chainsaw-test.yaml b/tests/e2e/ipaddress/ipv6/ipaddressclaim-ipv6-prefixexhausted/chainsaw-test.yaml index 6f3e9a17..44986ead 100644 --- a/tests/e2e/ipaddress/ipv6/ipaddressclaim-ipv6-prefixexhausted/chainsaw-test.yaml +++ b/tests/e2e/ipaddress/ipv6/ipaddressclaim-ipv6-prefixexhausted/chainsaw-test.yaml @@ -134,7 +134,7 @@ spec: kind: IpAddressClaim name: ipaddressclaim-ipv6-prefixexhausted-3 - name: Cleanup events - description: Events cleanup required to fix issues with failing tests that assert the wrong Error resource and lease cleanup for preventing delays when using the same prefixes (e.g. with "invalid" tests) + description: Events cleanup required to fix issues with failing tests that assert the wrong Error resource cleanup: - script: content: |