Files
deploy/util/glob/glob_test.go
Anand Francis JosephandGitHub 0931a86aa4 chore: Added unit tests and fix e2e tests for application sync decoupling feature (#19966)
* fixed doc comments and added unit tests

Signed-off-by: anandf <[email protected]>

* Added comments for the newly added unit tests

Signed-off-by: anandf <[email protected]>

* Refactored method name to deriveServiceAccountToImpersonate

Signed-off-by: anandf <[email protected]>

* Using const name in return value

Signed-off-by: anandf <[email protected]>

* Added unit tests for argocd proj add-destination-service-accounts

Signed-off-by: anandf <[email protected]>

* Fixed failing e2e tests

Signed-off-by: anandf <[email protected]>

* Fix linting errors

Signed-off-by: anandf <[email protected]>

* Using require package instead of assert and fixed code generation

Signed-off-by: anandf <[email protected]>

* Removed parallel execution of tests for sync with impersonate

Signed-off-by: anandf <[email protected]>

* Added err checks for glob validations

Signed-off-by: anandf <[email protected]>

* Fixed e2e tests for sync impersonation

Signed-off-by: anandf <[email protected]>

* Using consistently based expects in E2E tests

Signed-off-by: anandf <[email protected]>

* Added more unit tests and fixed go generate

Signed-off-by: anandf <[email protected]>

* Fixed failed lint errors, unit and e2e test failures

Signed-off-by: anandf <[email protected]>

* Fixed goimports linter issue

Signed-off-by: anandf <[email protected]>

* Added code comments and added few missing unit tests

Signed-off-by: anandf <[email protected]>

* Added missing unit test for GetDestinationServiceAccounts method

Signed-off-by: anandf <[email protected]>

* Fixed goimports formatting with local for project_test.go

Signed-off-by: anandf <[email protected]>

* Corrected typo in a field name additionalObjs

Signed-off-by: anandf <[email protected]>

* Fixed failing unit tests

Signed-off-by: anandf <[email protected]>

---------

Signed-off-by: anandf <[email protected]>
2024-10-03 07:37:50 -07:00

89 lines
2.6 KiB
Go

package glob
import (
"testing"
"github.com/stretchr/testify/require"
)
func Test_Match(t *testing.T) {
tests := []struct {
name string
input string
pattern string
result bool
}{
{"Exact match", "hello", "hello", true},
{"Non-match exact", "hello", "hell", false},
{"Long glob match", "hello", "hell*", true},
{"Short glob match", "hello", "h*", true},
{"Glob non-match", "hello", "e*", false},
{"Invalid pattern", "e[[a*", "e[[a*", false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
res := Match(tt.pattern, tt.input)
require.Equal(t, tt.result, res)
})
}
}
func Test_MatchList(t *testing.T) {
tests := []struct {
name string
input string
list []string
patternMatch string
result bool
}{
{"Exact name in list", "test", []string{"test"}, EXACT, true},
{"Exact name not in list", "test", []string{"other"}, EXACT, false},
{"Exact name not in list, multiple elements", "test", []string{"some", "other"}, EXACT, false},
{"Exact name not in list, list empty", "test", []string{}, EXACT, false},
{"Exact name not in list, empty element", "test", []string{""}, EXACT, false},
{"Glob name in list, but exact wanted", "test", []string{"*"}, EXACT, false},
{"Glob name in list with simple wildcard", "test", []string{"*"}, GLOB, true},
{"Glob name in list without wildcard", "test", []string{"test"}, GLOB, true},
{"Glob name in list, multiple elements", "test", []string{"other*", "te*"}, GLOB, true},
{"match everything but specified word: fail", "disallowed", []string{"/^((?!disallowed).)*$/"}, REGEXP, false},
{"match everything but specified word: pass", "allowed", []string{"/^((?!disallowed).)*$/"}, REGEXP, true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
res := MatchStringInList(tt.list, tt.input, tt.patternMatch)
require.Equal(t, tt.result, res)
})
}
}
func Test_MatchWithError(t *testing.T) {
tests := []struct {
name string
input string
pattern string
result bool
expectedErr string
}{
{"Exact match", "hello", "hello", true, ""},
{"Non-match exact", "hello", "hell", false, ""},
{"Long glob match", "hello", "hell*", true, ""},
{"Short glob match", "hello", "h*", true, ""},
{"Glob non-match", "hello", "e*", false, ""},
{"Invalid pattern", "e[[a*", "e[[a*", false, "unexpected end of input"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
res, err := MatchWithError(tt.pattern, tt.input)
require.Equal(t, tt.result, res)
if tt.expectedErr == "" {
require.NoError(t, err)
} else {
require.ErrorContains(t, err, tt.expectedErr)
}
})
}
}