mirror of
https://github.com/vmware-tanzu/velero.git
synced 2025-12-23 06:15:21 +00:00
Some checks failed
Run the E2E test on kind / get-go-version (push) Failing after 1m5s
Run the E2E test on kind / build (push) Has been skipped
Run the E2E test on kind / setup-test-matrix (push) Successful in 3s
Run the E2E test on kind / run-e2e-test (push) Has been skipped
Main CI / get-go-version (push) Successful in 11s
Main CI / Build (push) Failing after 25s
Close stale issues and PRs / stale (push) Successful in 14s
Trivy Nightly Scan / Trivy nightly scan (velero, main) (push) Failing after 1m36s
Trivy Nightly Scan / Trivy nightly scan (velero-plugin-for-aws, main) (push) Failing after 1m2s
Trivy Nightly Scan / Trivy nightly scan (velero-plugin-for-gcp, main) (push) Failing after 1m19s
Trivy Nightly Scan / Trivy nightly scan (velero-plugin-for-microsoft-azure, main) (push) Failing after 1m2s
* Add wildcard status fields Signed-off-by: Joseph <jvaikath@redhat.com> * Implement wildcard namespace expansion in item collector - Introduced methods to get active namespaces and expand wildcard includes/excludes in the item collector. - Updated getNamespacesToList to handle wildcard patterns and return expanded lists. - Added utility functions for setting includes and excludes in the IncludesExcludes struct. - Created a new package for wildcard handling, including functions to determine when to expand wildcards and to perform the expansion. This enhances the backup process by allowing more flexible namespace selection based on wildcard patterns. Signed-off-by: Joseph <jvaikath@redhat.com> * Enhance wildcard expansion logic and logging in item collector - Improved logging to include original includes and excludes when expanding wildcards. - Updated the ShouldExpandWildcards function to check for wildcard patterns in excludes. - Added comments for clarity in the expandWildcards function regarding pattern handling. These changes enhance the clarity and functionality of the wildcard expansion process in the backup system. Signed-off-by: Joseph <jvaikath@redhat.com> * Add wildcard namespace fields to Backup CRD and update deepcopy methods - Introduced `wildcardIncludedNamespaces` and `wildcardExcludedNamespaces` fields to the Backup CRD to support wildcard patterns for namespace inclusion and exclusion. - Updated deepcopy methods to handle the new fields, ensuring proper copying of data during object manipulation. These changes enhance the flexibility of namespace selection in backup operations, aligning with recent improvements in wildcard handling. Signed-off-by: Joseph <jvaikath@redhat.com> * Refactor Backup CRD to rename wildcard namespace fields - Updated `BackupStatus` struct to rename `WildcardIncludedNamespaces` to `WildcardExpandedIncludedNamespaces` and `WildcardExcludedNamespaces` to `WildcardExpandedExcludedNamespaces` for clarity. - Adjusted associated comments to reflect the new naming and ensure consistency in documentation. - Modified deepcopy methods to accommodate the renamed fields, ensuring proper data handling during object manipulation. These changes enhance the clarity and maintainability of the Backup CRD, aligning with recent improvements in wildcard handling. Signed-off-by: Joseph <jvaikath@redhat.com> * Fix Signed-off-by: Joseph <jvaikath@redhat.com> * Refactor where wildcard expansion happens Signed-off-by: Joseph <jvaikath@redhat.com> * Refactor Backup CRD and related components for expanded namespace handling - Updated `BackupStatus` struct to rename fields for clarity: `WildcardExpandedIncludedNamespaces` and `WildcardExpandedExcludedNamespaces` are now `ExpandedIncludedNamespaces` and `ExpandedExcludedNamespaces`, respectively. - Adjusted associated comments and deepcopy methods to reflect the new naming conventions. - Removed the `getActiveNamespaces` function from the item collector, streamlining the namespace handling process. - Enhanced logging during wildcard expansion to provide clearer insights into the process. These changes improve the clarity and maintainability of the Backup CRD and enhance the functionality of namespace selection in backup operations. Signed-off-by: Joseph <jvaikath@redhat.com> * Refactor wildcard expansion logic in item collector and enhance testing - Moved the wildcard expansion logic into a dedicated method, `expandNamespaceWildcards`, improving code organization and readability. - Updated logging to provide detailed insights during the wildcard expansion process. - Introduced comprehensive unit tests for wildcard handling, covering various scenarios and edge cases. - Enhanced the `ShouldExpandWildcards` function to better identify wildcard patterns and validate inputs. These changes improve the maintainability and robustness of the wildcard handling in the backup system. Signed-off-by: Joseph <jvaikath@redhat.com> * Enhance Restore CRD with expanded namespace fields and update logic - Added `ExpandedIncludedNamespaces` and `ExpandedExcludedNamespaces` fields to the `RestoreStatus` struct to support expanded wildcard namespace handling. - Updated the `DeepCopyInto` method to ensure proper copying of the new fields. - Implemented logic in the restore process to expand wildcard patterns for included and excluded namespaces, improving flexibility in namespace selection during restores. - Enhanced logging to provide insights into the expanded namespaces. These changes improve the functionality and maintainability of the restore process, aligning with recent enhancements in wildcard handling. Signed-off-by: Joseph <jvaikath@redhat.com> * Refactor Backup and Restore CRDs to enhance wildcard namespace handling - Renamed fields in `BackupStatus` and `RestoreStatus` from `ExpandedIncludedNamespaces` and `ExpandedExcludedNamespaces` to `IncludeWildcardMatches` and `ExcludeWildcardMatches` for clarity. - Introduced a new field `WildcardResult` to record the final namespaces after applying wildcard logic. - Updated the `DeepCopyInto` methods to accommodate the new field names and ensure proper data handling. - Enhanced comments to reflect the changes and improve documentation clarity. These updates improve the maintainability and clarity of the CRDs, aligning with recent enhancements in wildcard handling. Signed-off-by: Joseph <jvaikath@redhat.com> * Enhance wildcard namespace handling in Backup and Restore processes - Updated `BackupRequest` and `Restore` status structures to include a new field `WildcardResult`, which captures the final list of namespaces after applying wildcard logic. - Renamed existing fields to `IncludeWildcardMatches` and `ExcludeWildcardMatches` for improved clarity. - Enhanced logging to provide detailed insights into the expanded namespaces and final results during backup and restore operations. - Introduced a new utility function `GetWildcardResult` to streamline the selection of namespaces based on include/exclude criteria. These changes improve the clarity and functionality of namespace selection in both backup and restore processes, aligning with recent enhancements in wildcard handling. Signed-off-by: Joseph <jvaikath@redhat.com> * Refactor namespace wildcard expansion logic in restore process - Moved the wildcard expansion logic into a dedicated method, `expandNamespaceWildcards`, improving code organization and readability. - Enhanced error handling and logging to provide detailed insights into the expanded namespaces during the restore operation. - Updated the restore context with expanded namespace patterns and final results, ensuring clarity in the restore status. These changes improve the maintainability and clarity of the restore process, aligning with recent enhancements in wildcard handling. Signed-off-by: Joseph <jvaikath@redhat.com> * Add checks for "*" in exclude Signed-off-by: Joseph <jvaikath@redhat.com> * Rebase Signed-off-by: Joseph <jvaikath@redhat.com> * Create NamespaceIncludesExcludes to get full NS listing for backup w/ Signed-off-by: Scott Seago <sseago@redhat.com> Signed-off-by: Joseph <jvaikath@redhat.com> * Add new NamespaceIncludesExcludes struct Signed-off-by: Joseph <jvaikath@redhat.com> * Move namespace expansion logic Signed-off-by: Joseph <jvaikath@redhat.com> * Update backup status with expansion Signed-off-by: Joseph <jvaikath@redhat.com> * Wildcard status update Signed-off-by: Joseph <jvaikath@redhat.com> * Skip ns check if wildcard expansion Signed-off-by: Joseph <jvaikath@redhat.com> * Move wildcard expansion to getResourceItems Signed-off-by: Joseph <jvaikath@redhat.com> * lint Signed-off-by: Joseph <jvaikath@redhat.com> * Changelog Signed-off-by: Joseph <jvaikath@redhat.com> * linting issues Signed-off-by: Joseph <jvaikath@redhat.com> * Remove wildcard restore to check if tests pass Signed-off-by: Joseph <jvaikath@redhat.com> * Fix namespace mapping test bug from lint fix The previous commit (0a4aabcf4) attempted to fix linting issues by using strings.Builder, but incorrectly wrote commas to a separate builder and concatenated them at the end instead of between namespace mappings. This caused the namespace mapping string to be malformed: Before: ns-1:ns-1-mapped,ns-2:ns-2-mapped Bug: ns-1:ns-1-mappedns-2:ns-2-mapped,, The malformed string was parsed as a single mapping with an invalid namespace name containing a colon, causing Kubernetes to reject it: "ns-1-mappedns-2:ns-2-mapped" is invalid Fix by properly using strings.Builder to construct the mapping string with commas between entries, addressing both the linting concern and the functional bug. Fixes the MultiNamespacesMappingResticTest and MultiNamespacesMappingSnapshotTest failures. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com> Signed-off-by: Tiger Kaovilai <tkaovila@redhat.com> Signed-off-by: Joseph <jvaikath@redhat.com> * Fix wildcard namespace expansion edge cases This commit fixes two bugs in the wildcard namespace expansion feature: 1. Empty wildcard results: When a wildcard pattern (e.g., "invalid*") matched no namespaces, the backup would incorrectly back up ALL namespaces instead of backing up nothing. This was because the empty includes list was indistinguishable from "no filter specified". Fix: Added wildcardExpanded flag to NamespaceIncludesExcludes to track when wildcard expansion has occurred. When true and the includes list is empty, ShouldInclude now correctly returns false. 2. Premature namespace filtering: An earlier attempt to fix bug #1 filtered namespaces too early in collectNamespaces, breaking LabelSelector tests where namespaces should be included based on resources within them matching the label selector. Fix: Removed the premature filtering and rely on the existing filterNamespaces call at the end of getAllItems, which correctly handles both wildcard expansion and label selector scenarios. The fixes ensure: - Wildcard patterns matching nothing result in empty backups - Label selectors still work correctly (namespace included if any resource in it matches the selector) - State is preserved across multiple ResolveNamespaceList calls 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com> Signed-off-by: Tiger Kaovilai <tkaovila@redhat.com> Signed-off-by: Joseph <jvaikath@redhat.com> * Run wildcard expansion during backup processing Signed-off-by: Joseph <jvaikath@redhat.com> * Lint fix Signed-off-by: Joseph <jvaikath@redhat.com> * Improve coverage Signed-off-by: Joseph <jvaikath@redhat.com> * gofmt fix Signed-off-by: Joseph <jvaikath@redhat.com> * Add wildcard details to describe backup status Signed-off-by: Joseph <jvaikath@redhat.com> * Revert "Remove wildcard restore to check if tests pass" This reverts commit 4e22c2af855b71447762cb0a9fab7e7049f38a5f. Signed-off-by: Joseph <jvaikath@redhat.com> * Add restore describe for wildcard namespaces Revert restore wildcard removal Signed-off-by: Joseph <jvaikath@redhat.com> * Add coverage Signed-off-by: Joseph <jvaikath@redhat.com> * Lint Signed-off-by: Joseph <jvaikath@redhat.com> * Remove unintentional changes Signed-off-by: Joseph <jvaikath@redhat.com> * Remove wildcard status fields and mentionsRemove usage of wildcard fields for backup and restore status. Signed-off-by: Joseph <jvaikath@redhat.com> * Remove status update changelog line Signed-off-by: Joseph <jvaikath@redhat.com> * Rename getNamespaceIncludesExcludes Signed-off-by: Scott Seago <sseago@redhat.com> Signed-off-by: Scott Seago <sseago@redhat.com> * Rewrite brace pattern validation Signed-off-by: Joseph <jvaikath@redhat.com> * Different var for internal loop Signed-off-by: Joseph <jvaikath@redhat.com> --------- Signed-off-by: Joseph <jvaikath@redhat.com> Signed-off-by: Scott Seago <sseago@redhat.com> Signed-off-by: Tiger Kaovilai <tkaovila@redhat.com> Co-authored-by: Scott Seago <sseago@redhat.com> Co-authored-by: Tiger Kaovilai <tkaovila@redhat.com> Co-authored-by: Claude <noreply@anthropic.com>
800 lines
23 KiB
Go
800 lines
23 KiB
Go
package wildcard
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestShouldExpandWildcards(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
includes []string
|
|
excludes []string
|
|
expected bool
|
|
}{
|
|
{
|
|
name: "no wildcards",
|
|
includes: []string{"ns1", "ns2"},
|
|
excludes: []string{"ns3", "ns4"},
|
|
expected: false,
|
|
},
|
|
{
|
|
name: "includes has star - should not expand",
|
|
includes: []string{"*"},
|
|
excludes: []string{"ns1"},
|
|
expected: false,
|
|
},
|
|
{
|
|
name: "includes has star after a wildcard pattern - should not expand",
|
|
includes: []string{"ns*", "*"},
|
|
excludes: []string{"ns1"},
|
|
expected: false,
|
|
},
|
|
{
|
|
name: "includes has wildcard pattern",
|
|
includes: []string{"ns*"},
|
|
excludes: []string{"ns1"},
|
|
expected: true,
|
|
},
|
|
{
|
|
name: "excludes has wildcard pattern",
|
|
includes: []string{"ns1"},
|
|
excludes: []string{"ns*"},
|
|
expected: true,
|
|
},
|
|
{
|
|
name: "both have wildcard patterns",
|
|
includes: []string{"app-*"},
|
|
excludes: []string{"test-*"},
|
|
expected: true,
|
|
},
|
|
{
|
|
name: "includes has star and wildcard - star takes precedence",
|
|
includes: []string{"*", "ns*"},
|
|
excludes: []string{},
|
|
expected: false,
|
|
},
|
|
{
|
|
name: "double asterisk should be detected as wildcard",
|
|
includes: []string{"**"},
|
|
excludes: []string{},
|
|
expected: true, // ** is a wildcard pattern (but will error during validation)
|
|
},
|
|
{
|
|
name: "empty slices",
|
|
includes: []string{},
|
|
excludes: []string{},
|
|
expected: false,
|
|
},
|
|
{
|
|
name: "complex wildcard patterns",
|
|
includes: []string{"*-prod"},
|
|
excludes: []string{"test-*-staging"},
|
|
expected: true,
|
|
},
|
|
{
|
|
name: "question mark wildcard",
|
|
includes: []string{"ns?"},
|
|
excludes: []string{},
|
|
expected: true, // question mark is now considered a wildcard
|
|
},
|
|
{
|
|
name: "character class wildcard",
|
|
includes: []string{"ns[abc]"},
|
|
excludes: []string{},
|
|
expected: true, // character class is considered wildcard
|
|
},
|
|
{
|
|
name: "brace alternatives wildcard",
|
|
includes: []string{"ns{prod,staging}"},
|
|
excludes: []string{},
|
|
expected: true, // brace alternatives are considered wildcard
|
|
},
|
|
{
|
|
name: "dot is literal - not wildcard",
|
|
includes: []string{"app.prod"},
|
|
excludes: []string{},
|
|
expected: false, // dot is literal, not wildcard
|
|
},
|
|
{
|
|
name: "plus is literal - not wildcard",
|
|
includes: []string{"app+"},
|
|
excludes: []string{},
|
|
expected: false, // plus is literal, not wildcard
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
result := ShouldExpandWildcards(tt.includes, tt.excludes)
|
|
assert.Equal(t, tt.expected, result)
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestExpandWildcards(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
activeNamespaces []string
|
|
includes []string
|
|
excludes []string
|
|
expectedIncludes []string
|
|
expectedExcludes []string
|
|
expectError bool
|
|
}{
|
|
{
|
|
name: "no wildcards",
|
|
activeNamespaces: []string{"ns1", "ns2", "ns3"},
|
|
includes: []string{"ns1", "ns4"},
|
|
excludes: []string{"ns2"},
|
|
expectedIncludes: []string{"ns1", "ns4"},
|
|
expectedExcludes: []string{"ns2"},
|
|
expectError: false,
|
|
},
|
|
{
|
|
name: "wildcard in includes",
|
|
activeNamespaces: []string{"app-prod", "app-staging", "db-prod", "test-ns"},
|
|
includes: []string{"app-*"},
|
|
excludes: []string{"test-ns"},
|
|
expectedIncludes: []string{"app-prod", "app-staging"},
|
|
expectedExcludes: []string{"test-ns"},
|
|
expectError: false,
|
|
},
|
|
{
|
|
name: "wildcard in excludes",
|
|
activeNamespaces: []string{"app-prod", "app-staging", "db-prod", "test-ns"},
|
|
includes: []string{"app-prod"},
|
|
excludes: []string{"*-staging"},
|
|
expectedIncludes: []string{"app-prod"},
|
|
expectedExcludes: []string{"app-staging"},
|
|
expectError: false,
|
|
},
|
|
{
|
|
name: "wildcards in both",
|
|
activeNamespaces: []string{"app-prod", "app-staging", "db-prod", "db-staging", "test-ns"},
|
|
includes: []string{"*-prod"},
|
|
excludes: []string{"*-staging"},
|
|
expectedIncludes: []string{"app-prod", "db-prod"},
|
|
expectedExcludes: []string{"app-staging", "db-staging"},
|
|
expectError: false,
|
|
},
|
|
{
|
|
name: "star pattern in includes",
|
|
activeNamespaces: []string{"ns1", "ns2", "ns3"},
|
|
includes: []string{"*"},
|
|
excludes: []string{},
|
|
expectedIncludes: []string{"ns1", "ns2", "ns3"},
|
|
expectedExcludes: nil,
|
|
expectError: false,
|
|
},
|
|
{
|
|
name: "empty active namespaces",
|
|
activeNamespaces: []string{},
|
|
includes: []string{"app-*"},
|
|
excludes: []string{"test-*"},
|
|
expectedIncludes: nil,
|
|
expectedExcludes: nil,
|
|
expectError: false,
|
|
},
|
|
{
|
|
name: "empty includes and excludes",
|
|
activeNamespaces: []string{"ns1", "ns2"},
|
|
includes: []string{},
|
|
excludes: []string{},
|
|
expectedIncludes: nil,
|
|
expectedExcludes: nil,
|
|
expectError: false,
|
|
},
|
|
{
|
|
name: "complex patterns",
|
|
activeNamespaces: []string{"my-app-prod", "my-app-staging", "your-app-prod", "system-ns"},
|
|
includes: []string{"*-app-*"},
|
|
excludes: []string{"*-staging"},
|
|
expectedIncludes: []string{"my-app-prod", "my-app-staging", "your-app-prod"},
|
|
expectedExcludes: []string{"my-app-staging"},
|
|
expectError: false,
|
|
},
|
|
{
|
|
name: "double asterisk should error",
|
|
activeNamespaces: []string{"ns1", "ns2", "ns3"},
|
|
includes: []string{"**"},
|
|
excludes: []string{},
|
|
expectedIncludes: nil,
|
|
expectedExcludes: nil,
|
|
expectError: true, // ** is invalid
|
|
},
|
|
{
|
|
name: "double asterisk in pattern should error",
|
|
activeNamespaces: []string{"ns1", "ns2", "ns3"},
|
|
includes: []string{"app-**"},
|
|
excludes: []string{},
|
|
expectedIncludes: nil,
|
|
expectedExcludes: nil,
|
|
expectError: true, // app-** contains ** which is invalid
|
|
},
|
|
{
|
|
name: "question mark patterns",
|
|
activeNamespaces: []string{"ns1", "ns2", "ns10", "test"},
|
|
includes: []string{"ns?"},
|
|
excludes: []string{},
|
|
expectedIncludes: []string{"ns1", "ns2"}, // ? matches single character
|
|
expectedExcludes: nil,
|
|
expectError: false,
|
|
},
|
|
{
|
|
name: "character class patterns",
|
|
activeNamespaces: []string{"nsa", "nsb", "nsc", "nsx", "ns1"},
|
|
includes: []string{"ns[abc]"},
|
|
excludes: []string{},
|
|
expectedIncludes: []string{"nsa", "nsb", "nsc"}, // [abc] matches a, b, or c
|
|
expectedExcludes: nil,
|
|
expectError: false,
|
|
},
|
|
{
|
|
name: "brace alternative patterns",
|
|
activeNamespaces: []string{"app-prod", "app-staging", "app-dev", "db-prod"},
|
|
includes: []string{"app-{prod,staging}"},
|
|
excludes: []string{},
|
|
expectedIncludes: []string{"app-prod", "app-staging"}, // {prod,staging} matches either
|
|
expectedExcludes: nil,
|
|
expectError: false,
|
|
},
|
|
{
|
|
name: "literal dot and plus patterns",
|
|
activeNamespaces: []string{"app.prod", "app-prod", "app+", "app"},
|
|
includes: []string{"app.prod", "app+"},
|
|
excludes: []string{},
|
|
expectedIncludes: []string{"app.prod", "app+"}, // . and + are literal
|
|
expectedExcludes: nil,
|
|
expectError: false,
|
|
},
|
|
{
|
|
name: "unsupported regex patterns should error",
|
|
activeNamespaces: []string{"ns1", "ns2"},
|
|
includes: []string{"ns(1|2)"},
|
|
excludes: []string{},
|
|
expectedIncludes: nil,
|
|
expectedExcludes: nil,
|
|
expectError: true, // |, (, ) are not supported
|
|
},
|
|
{
|
|
name: "unclosed brace patterns should error",
|
|
activeNamespaces: []string{"app-prod"},
|
|
includes: []string{"app-{prod,staging"},
|
|
excludes: []string{},
|
|
expectedIncludes: nil,
|
|
expectedExcludes: nil,
|
|
expectError: true, // unclosed brace
|
|
},
|
|
{
|
|
name: "empty brace patterns should error",
|
|
activeNamespaces: []string{"app-prod"},
|
|
includes: []string{"app-{}"},
|
|
excludes: []string{},
|
|
expectedIncludes: nil,
|
|
expectedExcludes: nil,
|
|
expectError: true, // empty braces
|
|
},
|
|
{
|
|
name: "unmatched closing brace should error",
|
|
activeNamespaces: []string{"app-prod"},
|
|
includes: []string{"app-prod}"},
|
|
excludes: []string{},
|
|
expectedIncludes: nil,
|
|
expectedExcludes: nil,
|
|
expectError: true, // unmatched closing brace
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
includes, excludes, err := ExpandWildcards(tt.activeNamespaces, tt.includes, tt.excludes)
|
|
|
|
if tt.expectError {
|
|
assert.Error(t, err)
|
|
return
|
|
}
|
|
|
|
require.NoError(t, err)
|
|
assert.ElementsMatch(t, tt.expectedIncludes, includes)
|
|
assert.ElementsMatch(t, tt.expectedExcludes, excludes)
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestExpandWildcardsPrivate(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
patterns []string
|
|
activeNamespaces []string
|
|
expected []string
|
|
expectError bool
|
|
}{
|
|
{
|
|
name: "empty patterns",
|
|
patterns: []string{},
|
|
activeNamespaces: []string{"ns1", "ns2"},
|
|
expected: nil,
|
|
expectError: false,
|
|
},
|
|
{
|
|
name: "non-wildcard patterns",
|
|
patterns: []string{"ns1", "ns3"},
|
|
activeNamespaces: []string{"ns1", "ns2"},
|
|
expected: []string{"ns1", "ns3"}, // includes ns3 even if not in active
|
|
expectError: false,
|
|
},
|
|
{
|
|
name: "star pattern",
|
|
patterns: []string{"*"},
|
|
activeNamespaces: []string{"ns1", "ns2", "ns3"},
|
|
expected: []string{"ns1", "ns2", "ns3"},
|
|
expectError: false,
|
|
},
|
|
{
|
|
name: "simple wildcard",
|
|
patterns: []string{"app-*"},
|
|
activeNamespaces: []string{"app-prod", "app-staging", "db-prod"},
|
|
expected: []string{"app-prod", "app-staging"},
|
|
expectError: false,
|
|
},
|
|
{
|
|
name: "multiple patterns",
|
|
patterns: []string{"app-*", "db-prod", "*-test"},
|
|
activeNamespaces: []string{"app-prod", "app-staging", "db-prod", "service-test", "other"},
|
|
expected: []string{"app-prod", "app-staging", "db-prod", "service-test"},
|
|
expectError: false,
|
|
},
|
|
{
|
|
name: "wildcard with no matches",
|
|
patterns: []string{"missing-*"},
|
|
activeNamespaces: []string{"app-prod", "db-staging"},
|
|
expected: []string{}, // returns empty slice, not nil
|
|
expectError: false,
|
|
},
|
|
{
|
|
name: "brace patterns work correctly",
|
|
patterns: []string{"app-{prod,staging}"},
|
|
activeNamespaces: []string{"app-prod", "app-staging", "app-dev", "app-{prod,staging}"},
|
|
expected: []string{"app-prod", "app-staging"}, // brace patterns do expand
|
|
expectError: false,
|
|
},
|
|
{
|
|
name: "duplicate matches from multiple patterns",
|
|
patterns: []string{"app-*", "*-prod"},
|
|
activeNamespaces: []string{"app-prod", "app-staging", "db-prod"},
|
|
expected: []string{"app-prod", "app-staging", "db-prod"}, // no duplicates
|
|
expectError: false,
|
|
},
|
|
{
|
|
name: "question mark pattern - glob wildcard",
|
|
patterns: []string{"ns?"},
|
|
activeNamespaces: []string{"ns1", "ns2", "ns10"},
|
|
expected: []string{"ns1", "ns2"}, // ? is a glob pattern for single character
|
|
expectError: false,
|
|
},
|
|
{
|
|
name: "character class patterns",
|
|
patterns: []string{"ns[12]"},
|
|
activeNamespaces: []string{"ns1", "ns2", "ns3", "nsa"},
|
|
expected: []string{"ns1", "ns2"}, // [12] matches 1 or 2
|
|
expectError: false,
|
|
},
|
|
{
|
|
name: "character range patterns",
|
|
patterns: []string{"ns[a-c]"},
|
|
activeNamespaces: []string{"nsa", "nsb", "nsc", "nsd", "ns1"},
|
|
expected: []string{"nsa", "nsb", "nsc"}, // [a-c] matches a to c
|
|
expectError: false,
|
|
},
|
|
{
|
|
name: "negated character class",
|
|
patterns: []string{"ns[!abc]"},
|
|
activeNamespaces: []string{"nsa", "nsb", "nsc", "nsd", "ns1"},
|
|
expected: []string{"nsd", "ns1"}, // [!abc] matches anything except a, b, c
|
|
expectError: false,
|
|
},
|
|
{
|
|
name: "brace alternatives",
|
|
patterns: []string{"app-{prod,test}"},
|
|
activeNamespaces: []string{"app-prod", "app-test", "app-staging", "db-prod"},
|
|
expected: []string{"app-prod", "app-test"}, // {prod,test} matches either
|
|
expectError: false,
|
|
},
|
|
{
|
|
name: "double asterisk should error",
|
|
patterns: []string{"**"},
|
|
activeNamespaces: []string{"app-prod", "app.staging", "db/prod"},
|
|
expected: nil,
|
|
expectError: true, // ** is not allowed
|
|
},
|
|
{
|
|
name: "literal dot and plus",
|
|
patterns: []string{"app.prod", "service+"},
|
|
activeNamespaces: []string{"app.prod", "appXprod", "service+", "service"},
|
|
expected: []string{"app.prod", "service+"}, // . and + are literal
|
|
expectError: false,
|
|
},
|
|
{
|
|
name: "unsupported regex symbols should error",
|
|
patterns: []string{"ns(1|2)"},
|
|
activeNamespaces: []string{"ns1", "ns2"},
|
|
expected: nil,
|
|
expectError: true, // |, (, ) not supported
|
|
},
|
|
{
|
|
name: "double asterisk should error",
|
|
patterns: []string{"**"},
|
|
activeNamespaces: []string{"ns1", "ns2"},
|
|
expected: nil,
|
|
expectError: true, // ** not allowed
|
|
},
|
|
{
|
|
name: "double asterisk in pattern should error",
|
|
patterns: []string{"app-**-prod"},
|
|
activeNamespaces: []string{"app-prod"},
|
|
expected: nil,
|
|
expectError: true, // ** not allowed anywhere
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
result, err := expandWildcards(tt.patterns, tt.activeNamespaces)
|
|
|
|
if tt.expectError {
|
|
assert.Error(t, err)
|
|
return
|
|
}
|
|
|
|
require.NoError(t, err)
|
|
if tt.expected == nil {
|
|
assert.Nil(t, result)
|
|
} else if len(tt.expected) == 0 {
|
|
assert.Empty(t, result)
|
|
} else {
|
|
assert.ElementsMatch(t, tt.expected, result)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestValidateBracePatterns(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
pattern string
|
|
expectError bool
|
|
errorMsg string
|
|
}{
|
|
// Valid patterns
|
|
{
|
|
name: "valid single brace pattern",
|
|
pattern: "app-{prod,staging}",
|
|
expectError: false,
|
|
},
|
|
{
|
|
name: "valid brace with single option",
|
|
pattern: "app-{prod}",
|
|
expectError: false,
|
|
},
|
|
{
|
|
name: "valid brace with three options",
|
|
pattern: "app-{prod,staging,dev}",
|
|
expectError: false,
|
|
},
|
|
{
|
|
name: "valid pattern with text before and after brace",
|
|
pattern: "prefix-{a,b}-suffix",
|
|
expectError: false,
|
|
},
|
|
{
|
|
name: "valid pattern with no braces",
|
|
pattern: "app-prod",
|
|
expectError: false,
|
|
},
|
|
{
|
|
name: "valid pattern with asterisk",
|
|
pattern: "app-*",
|
|
expectError: false,
|
|
},
|
|
{
|
|
name: "valid brace with spaces around content",
|
|
pattern: "app-{ prod , staging }",
|
|
expectError: false,
|
|
},
|
|
{
|
|
name: "valid brace with numbers",
|
|
pattern: "ns-{1,2,3}",
|
|
expectError: false,
|
|
},
|
|
{
|
|
name: "valid brace with hyphens in options",
|
|
pattern: "{app-prod,db-staging}",
|
|
expectError: false,
|
|
},
|
|
|
|
// Unclosed opening braces
|
|
{
|
|
name: "unclosed opening brace at end",
|
|
pattern: "app-{prod,staging",
|
|
expectError: true,
|
|
errorMsg: "unclosed brace",
|
|
},
|
|
{
|
|
name: "unclosed opening brace at start",
|
|
pattern: "{prod,staging",
|
|
expectError: true,
|
|
errorMsg: "unclosed brace",
|
|
},
|
|
{
|
|
name: "unclosed opening brace in middle",
|
|
pattern: "app-{prod-test",
|
|
expectError: true,
|
|
errorMsg: "unclosed brace",
|
|
},
|
|
{
|
|
name: "multiple unclosed braces",
|
|
pattern: "app-{prod-{staging",
|
|
expectError: true,
|
|
errorMsg: "unclosed brace",
|
|
},
|
|
|
|
// Unmatched closing braces
|
|
{
|
|
name: "unmatched closing brace at end",
|
|
pattern: "app-prod}",
|
|
expectError: true,
|
|
errorMsg: "unmatched closing brace",
|
|
},
|
|
{
|
|
name: "unmatched closing brace at start",
|
|
pattern: "}app-prod",
|
|
expectError: true,
|
|
errorMsg: "unmatched closing brace",
|
|
},
|
|
{
|
|
name: "unmatched closing brace in middle",
|
|
pattern: "app-}prod",
|
|
expectError: true,
|
|
errorMsg: "unmatched closing brace",
|
|
},
|
|
{
|
|
name: "extra closing brace after valid pair",
|
|
pattern: "app-{prod,staging}}",
|
|
expectError: true,
|
|
errorMsg: "unmatched closing brace",
|
|
},
|
|
|
|
// Empty brace patterns
|
|
{
|
|
name: "completely empty braces",
|
|
pattern: "app-{}",
|
|
expectError: true,
|
|
errorMsg: "empty brace pattern",
|
|
},
|
|
{
|
|
name: "braces with only spaces",
|
|
pattern: "app-{ }",
|
|
expectError: true,
|
|
errorMsg: "empty brace pattern",
|
|
},
|
|
{
|
|
name: "braces with only comma",
|
|
pattern: "app-{,}",
|
|
expectError: true,
|
|
errorMsg: "empty brace pattern",
|
|
},
|
|
{
|
|
name: "braces with only commas",
|
|
pattern: "app-{,,,}",
|
|
expectError: true,
|
|
errorMsg: "empty brace pattern",
|
|
},
|
|
{
|
|
name: "braces with commas and spaces",
|
|
pattern: "app-{ , , }",
|
|
expectError: true,
|
|
errorMsg: "empty brace pattern",
|
|
},
|
|
{
|
|
name: "braces with tabs and commas",
|
|
pattern: "app-{\t,\t}",
|
|
expectError: true,
|
|
errorMsg: "empty brace pattern",
|
|
},
|
|
{
|
|
name: "empty braces at start",
|
|
pattern: "{}app-prod",
|
|
expectError: true,
|
|
errorMsg: "empty brace pattern",
|
|
},
|
|
{
|
|
name: "empty braces standalone",
|
|
pattern: "{}",
|
|
expectError: true,
|
|
errorMsg: "empty brace pattern",
|
|
},
|
|
|
|
// Edge cases
|
|
{
|
|
name: "empty pattern",
|
|
pattern: "",
|
|
expectError: false,
|
|
},
|
|
{
|
|
name: "pattern with only opening brace",
|
|
pattern: "{",
|
|
expectError: true,
|
|
errorMsg: "unclosed brace",
|
|
},
|
|
{
|
|
name: "pattern with only closing brace",
|
|
pattern: "}",
|
|
expectError: true,
|
|
errorMsg: "unmatched closing brace",
|
|
},
|
|
{
|
|
name: "valid brace with special characters inside",
|
|
pattern: "app-{prod-1,staging_2,dev.3}",
|
|
expectError: false,
|
|
},
|
|
{
|
|
name: "brace with asterisk inside option",
|
|
pattern: "app-{prod*,staging}",
|
|
expectError: false,
|
|
},
|
|
{
|
|
name: "multiple valid brace patterns",
|
|
pattern: "{app,db}-{prod,staging}",
|
|
expectError: false,
|
|
},
|
|
{
|
|
name: "brace with single character",
|
|
pattern: "app-{a}",
|
|
expectError: false,
|
|
},
|
|
{
|
|
name: "brace with trailing comma but has content",
|
|
pattern: "app-{prod,staging,}",
|
|
expectError: false, // Has content, so it's valid
|
|
},
|
|
{
|
|
name: "brace with leading comma but has content",
|
|
pattern: "app-{,prod,staging}",
|
|
expectError: false, // Has content, so it's valid
|
|
},
|
|
{
|
|
name: "brace with leading comma but has content",
|
|
pattern: "app-{{,prod,staging}",
|
|
expectError: true, // unclosed brace
|
|
},
|
|
{
|
|
name: "brace with leading comma but has content",
|
|
pattern: "app-{,prod,staging}}",
|
|
expectError: true, // unmatched closing brace
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
err := validateBracePatterns(tt.pattern)
|
|
|
|
if tt.expectError {
|
|
require.Error(t, err, "Expected error for pattern: %s", tt.pattern)
|
|
if tt.errorMsg != "" {
|
|
assert.Contains(t, err.Error(), tt.errorMsg, "Error message should contain: %s", tt.errorMsg)
|
|
}
|
|
} else {
|
|
assert.NoError(t, err, "Expected no error for pattern: %s", tt.pattern)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// Edge case tests
|
|
func TestExpandWildcardsEdgeCases(t *testing.T) {
|
|
t.Run("nil inputs", func(t *testing.T) {
|
|
includes, excludes, err := ExpandWildcards(nil, nil, nil)
|
|
require.NoError(t, err)
|
|
assert.Nil(t, includes)
|
|
assert.Nil(t, excludes)
|
|
})
|
|
|
|
t.Run("empty string patterns", func(t *testing.T) {
|
|
activeNamespaces := []string{"ns1", "ns2"}
|
|
result, err := expandWildcards([]string{""}, activeNamespaces)
|
|
require.NoError(t, err)
|
|
assert.ElementsMatch(t, []string{""}, result) // empty string is treated as literal
|
|
})
|
|
|
|
t.Run("whitespace patterns", func(t *testing.T) {
|
|
activeNamespaces := []string{"ns1", " ", "ns2"}
|
|
result, err := expandWildcards([]string{" "}, activeNamespaces)
|
|
require.NoError(t, err)
|
|
assert.ElementsMatch(t, []string{" "}, result)
|
|
})
|
|
|
|
t.Run("special characters in namespace names", func(t *testing.T) {
|
|
activeNamespaces := []string{"ns-1", "ns_2", "ns.3", "ns@4"}
|
|
result, err := expandWildcards([]string{"ns*"}, activeNamespaces)
|
|
require.NoError(t, err)
|
|
assert.ElementsMatch(t, []string{"ns-1", "ns_2", "ns.3", "ns@4"}, result)
|
|
})
|
|
|
|
t.Run("complex glob combinations", func(t *testing.T) {
|
|
activeNamespaces := []string{"app1-prod", "app2-prod", "app1-test", "db-prod", "service"}
|
|
result, err := expandWildcards([]string{"app?-{prod,test}"}, activeNamespaces)
|
|
require.NoError(t, err)
|
|
assert.ElementsMatch(t, []string{"app1-prod", "app2-prod", "app1-test"}, result)
|
|
})
|
|
|
|
t.Run("escaped characters", func(t *testing.T) {
|
|
activeNamespaces := []string{"app*", "app-prod", "app?test", "app-test"}
|
|
result, err := expandWildcards([]string{"app\\*"}, activeNamespaces)
|
|
require.NoError(t, err)
|
|
assert.ElementsMatch(t, []string{"app*"}, result)
|
|
})
|
|
|
|
t.Run("mixed literal and wildcard patterns", func(t *testing.T) {
|
|
activeNamespaces := []string{"app.prod", "app-prod", "app_prod", "test.ns"}
|
|
result, err := expandWildcards([]string{"app.prod", "app?prod"}, activeNamespaces)
|
|
require.NoError(t, err)
|
|
assert.ElementsMatch(t, []string{"app.prod", "app-prod", "app_prod"}, result)
|
|
})
|
|
|
|
t.Run("conservative asterisk validation", func(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
pattern string
|
|
shouldError bool
|
|
}{
|
|
{"single asterisk", "*", false},
|
|
{"double asterisk", "**", true},
|
|
{"triple asterisk", "***", true},
|
|
{"quadruple asterisk", "****", true},
|
|
{"mixed with double", "app-**", true},
|
|
{"double in middle", "app-**-prod", true},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
_, err := expandWildcards([]string{tt.pattern}, []string{"test"})
|
|
if tt.shouldError {
|
|
assert.Error(t, err)
|
|
} else {
|
|
assert.NoError(t, err)
|
|
}
|
|
})
|
|
}
|
|
})
|
|
|
|
t.Run("malformed pattern validation", func(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
pattern string
|
|
shouldError bool
|
|
}{
|
|
{"unclosed bracket", "ns[abc", true},
|
|
{"unclosed brace", "app-{prod,staging", true},
|
|
{"nested unclosed", "ns[a{bc", true},
|
|
{"valid bracket", "ns[abc]", false},
|
|
{"valid brace", "app-{prod,staging}", false},
|
|
{"empty bracket", "ns[]", true}, // empty brackets are invalid
|
|
{"empty brace", "app-{}", true}, // empty braces are invalid
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
_, err := expandWildcards([]string{tt.pattern}, []string{"test"})
|
|
if tt.shouldError {
|
|
assert.Error(t, err, "Expected error for pattern: %s", tt.pattern)
|
|
} else {
|
|
assert.NoError(t, err, "Expected no error for pattern: %s", tt.pattern)
|
|
}
|
|
})
|
|
}
|
|
})
|
|
}
|