From c84f124cb4969e0b93441d66a6c67b5547999c58 Mon Sep 17 00:00:00 2001 From: BaaRai <130491490+BaaRai@users.noreply.github.com> Date: Tue, 10 Nov 2026 15:49:41 +0100 Subject: [PATCH] db: report hidden asset count in malformed-ip warning suffix malformedIPAssetWarning appended "and N more" with N = total, the number of offending assets, instead of the number left out of the sample. With 8 bad rows and 5 named it read "... and 8 more"; it now reads "... and 3 more". Same bug as the one fixed in notify/render.go. Move the wording into malformedIPAssetWarningText so it can be tested without Postgres, and add a test for the 8-of-5 and 5-of-5 cases. --- db/companies.go | 10 ++++++++-- db/companies_test.go | 18 ++++++++++++++++++ 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/db/companies.go b/db/companies.go index 57a6815..ad5d239 100644 --- a/db/companies.go +++ b/db/companies.go @@ -633,14 +633,20 @@ LIMIT $1`, malformedIPAssetsSampled) if total == 0 { return "", nil } + return malformedIPAssetWarningText(total, samples), nil +} + +// malformedIPAssetWarningText words the warning for total offending assets, of +// which only samples are named; the ones left out are counted in the suffix. +func malformedIPAssetWarningText(total int, samples []string) string { warning := fmt.Sprintf( "%d assets have an ip field that is not a valid IP; skipped for IP/CIDR scope matching (these assets will not be attributed to a company by subnet rules): %s", total, strings.Join(samples, ", "), ) if total > len(samples) { - warning += fmt.Sprintf(" and %d more", total) + warning += fmt.Sprintf(" and %d more", total-len(samples)) } - return warning, nil + return warning } // UpdateScope replaces all scope rules for a company and reattributes. diff --git a/db/companies_test.go b/db/companies_test.go index 465966d..25ee77a 100644 --- a/db/companies_test.go +++ b/db/companies_test.go @@ -458,3 +458,21 @@ func TestListCompanies(t *testing.T) { t.Error("ListCompanies: created company not found") } } + +func TestMalformedIPAssetWarningTextCountsOmitted(t *testing.T) { + samples := []string{"#1 a", "#2 b", "#3 c", "#4 d", "#5 e"} + // The suffix must count the assets left out, not repeat the total, otherwise + // the operator is told about more bad rows than exist. + got := malformedIPAssetWarningText(8, samples) + if !strings.HasPrefix(got, "8 assets have an ip field") { + t.Fatalf("the warning should lead with the total of 8, got %q", got) + } + if !strings.HasSuffix(got, ": #1 a, #2 b, #3 c, #4 d, #5 e and 3 more") { + t.Fatalf("8 assets with 5 named should note the 3 omitted, got %q", got) + } + // When every offending asset is named there is nothing to add. + got = malformedIPAssetWarningText(5, samples) + if !strings.HasSuffix(got, ": #1 a, #2 b, #3 c, #4 d, #5 e") { + t.Fatalf("5 assets with 5 named should have no suffix, got %q", got) + } +}