mirror of
https://github.com/hanzoai/deploy.git
synced 2026-10-07 08:44:50 +00:00
go.mod still declared `module github.com/hanzoai/deploy` after the repo was renamed. A Go module path is not cosmetic — it is the resolvable location. So `go get github.com/hanzoai/cd` could not work, and anything importing this module had to spell a name that no longer exists at that address. The build log of v3.6.0 is where it surfaced: the compiler was still emitting github.com/hanzoai/deploy/cmd/cd/commands. 3541 import paths across 690 .go, 19 .proto, 4 .yaml, 3 .sh, 2 .mod, Procfile and Makefile. Plus the 62 ghcr.io/hanzoai/deploy image references, moved to ghcr.io/hanzoai/cd to match hanzo.yml's build target — a distinct string from the module path, replaced separately so neither could clobber the other. go mod tidy clean, CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build ./... clean. Co-authored-by: Hanzo Dev <[email protected]>
330 lines
6.9 KiB
Go
330 lines
6.9 KiB
Go
package scm_provider
|
|
|
|
import (
|
|
"regexp"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
|
|
argoprojiov1alpha1 "github.com/hanzoai/cd/pkg/apis/application/v1alpha1"
|
|
)
|
|
|
|
func TestFilterRepoMatch(t *testing.T) {
|
|
t.Parallel()
|
|
provider := &MockProvider{
|
|
Repos: []*Repository{
|
|
{
|
|
Repository: "one",
|
|
},
|
|
{
|
|
Repository: "two",
|
|
},
|
|
{
|
|
Repository: "three",
|
|
},
|
|
{
|
|
Repository: "four",
|
|
},
|
|
},
|
|
}
|
|
filters := []argoprojiov1alpha1.SCMProviderGeneratorFilter{
|
|
{
|
|
RepositoryMatch: new("n|hr"),
|
|
},
|
|
}
|
|
repos, err := ListRepos(t.Context(), provider, filters, "")
|
|
require.NoError(t, err)
|
|
assert.Len(t, repos, 2)
|
|
assert.Equal(t, "one", repos[0].Repository)
|
|
assert.Equal(t, "three", repos[1].Repository)
|
|
}
|
|
|
|
func TestFilterLabelMatch(t *testing.T) {
|
|
t.Parallel()
|
|
provider := &MockProvider{
|
|
Repos: []*Repository{
|
|
{
|
|
Repository: "one",
|
|
Labels: []string{"prod-one", "prod-two", "staging"},
|
|
},
|
|
{
|
|
Repository: "two",
|
|
Labels: []string{"prod-two"},
|
|
},
|
|
{
|
|
Repository: "three",
|
|
Labels: []string{"staging"},
|
|
},
|
|
},
|
|
}
|
|
filters := []argoprojiov1alpha1.SCMProviderGeneratorFilter{
|
|
{
|
|
LabelMatch: new("^prod-.*$"),
|
|
},
|
|
}
|
|
repos, err := ListRepos(t.Context(), provider, filters, "")
|
|
require.NoError(t, err)
|
|
assert.Len(t, repos, 2)
|
|
assert.Equal(t, "one", repos[0].Repository)
|
|
assert.Equal(t, "two", repos[1].Repository)
|
|
}
|
|
|
|
func TestFilterPathExists(t *testing.T) {
|
|
t.Parallel()
|
|
provider := &MockProvider{
|
|
Repos: []*Repository{
|
|
{
|
|
Repository: "one",
|
|
},
|
|
{
|
|
Repository: "two",
|
|
},
|
|
{
|
|
Repository: "three",
|
|
},
|
|
},
|
|
}
|
|
filters := []argoprojiov1alpha1.SCMProviderGeneratorFilter{
|
|
{
|
|
PathsExist: []string{"two"},
|
|
},
|
|
}
|
|
repos, err := ListRepos(t.Context(), provider, filters, "")
|
|
require.NoError(t, err)
|
|
assert.Len(t, repos, 1)
|
|
assert.Equal(t, "two", repos[0].Repository)
|
|
}
|
|
|
|
func TestFilterPathDoesntExists(t *testing.T) {
|
|
t.Parallel()
|
|
provider := &MockProvider{
|
|
Repos: []*Repository{
|
|
{
|
|
Repository: "one",
|
|
},
|
|
{
|
|
Repository: "two",
|
|
},
|
|
{
|
|
Repository: "three",
|
|
},
|
|
},
|
|
}
|
|
filters := []argoprojiov1alpha1.SCMProviderGeneratorFilter{
|
|
{
|
|
PathsDoNotExist: []string{"two"},
|
|
},
|
|
}
|
|
repos, err := ListRepos(t.Context(), provider, filters, "")
|
|
require.NoError(t, err)
|
|
assert.Len(t, repos, 2)
|
|
}
|
|
|
|
func TestFilterRepoMatchBadRegexp(t *testing.T) {
|
|
t.Parallel()
|
|
provider := &MockProvider{
|
|
Repos: []*Repository{
|
|
{
|
|
Repository: "one",
|
|
},
|
|
},
|
|
}
|
|
filters := []argoprojiov1alpha1.SCMProviderGeneratorFilter{
|
|
{
|
|
RepositoryMatch: new("("),
|
|
},
|
|
}
|
|
_, err := ListRepos(t.Context(), provider, filters, "")
|
|
require.Error(t, err)
|
|
}
|
|
|
|
func TestFilterLabelMatchBadRegexp(t *testing.T) {
|
|
t.Parallel()
|
|
provider := &MockProvider{
|
|
Repos: []*Repository{
|
|
{
|
|
Repository: "one",
|
|
},
|
|
},
|
|
}
|
|
filters := []argoprojiov1alpha1.SCMProviderGeneratorFilter{
|
|
{
|
|
LabelMatch: new("("),
|
|
},
|
|
}
|
|
_, err := ListRepos(t.Context(), provider, filters, "")
|
|
require.Error(t, err)
|
|
}
|
|
|
|
func TestFilterBranchMatch(t *testing.T) {
|
|
t.Parallel()
|
|
provider := &MockProvider{
|
|
Repos: []*Repository{
|
|
{
|
|
Repository: "one",
|
|
Branch: "one",
|
|
},
|
|
{
|
|
Repository: "one",
|
|
Branch: "two",
|
|
},
|
|
{
|
|
Repository: "two",
|
|
Branch: "one",
|
|
},
|
|
{
|
|
Repository: "three",
|
|
Branch: "one",
|
|
},
|
|
{
|
|
Repository: "three",
|
|
Branch: "two",
|
|
},
|
|
},
|
|
}
|
|
filters := []argoprojiov1alpha1.SCMProviderGeneratorFilter{
|
|
{
|
|
BranchMatch: new("w"),
|
|
},
|
|
}
|
|
repos, err := ListRepos(t.Context(), provider, filters, "")
|
|
require.NoError(t, err)
|
|
assert.Len(t, repos, 2)
|
|
assert.Equal(t, "one", repos[0].Repository)
|
|
assert.Equal(t, "two", repos[0].Branch)
|
|
assert.Equal(t, "three", repos[1].Repository)
|
|
assert.Equal(t, "two", repos[1].Branch)
|
|
}
|
|
|
|
func TestMultiFilterAnd(t *testing.T) {
|
|
t.Parallel()
|
|
provider := &MockProvider{
|
|
Repos: []*Repository{
|
|
{
|
|
Repository: "one",
|
|
Labels: []string{"prod-one", "prod-two", "staging"},
|
|
},
|
|
{
|
|
Repository: "two",
|
|
Labels: []string{"prod-two"},
|
|
},
|
|
{
|
|
Repository: "three",
|
|
Labels: []string{"staging"},
|
|
},
|
|
},
|
|
}
|
|
filters := []argoprojiov1alpha1.SCMProviderGeneratorFilter{
|
|
{
|
|
RepositoryMatch: new("w"),
|
|
LabelMatch: new("^prod-.*$"),
|
|
},
|
|
}
|
|
repos, err := ListRepos(t.Context(), provider, filters, "")
|
|
require.NoError(t, err)
|
|
assert.Len(t, repos, 1)
|
|
assert.Equal(t, "two", repos[0].Repository)
|
|
}
|
|
|
|
func TestMultiFilterOr(t *testing.T) {
|
|
t.Parallel()
|
|
provider := &MockProvider{
|
|
Repos: []*Repository{
|
|
{
|
|
Repository: "one",
|
|
Labels: []string{"prod-one", "prod-two", "staging"},
|
|
},
|
|
{
|
|
Repository: "two",
|
|
Labels: []string{"prod-two"},
|
|
},
|
|
{
|
|
Repository: "three",
|
|
Labels: []string{"staging"},
|
|
},
|
|
},
|
|
}
|
|
filters := []argoprojiov1alpha1.SCMProviderGeneratorFilter{
|
|
{
|
|
RepositoryMatch: new("e"),
|
|
},
|
|
{
|
|
LabelMatch: new("^prod-.*$"),
|
|
},
|
|
}
|
|
repos, err := ListRepos(t.Context(), provider, filters, "")
|
|
require.NoError(t, err)
|
|
assert.Len(t, repos, 3)
|
|
assert.Equal(t, "one", repos[0].Repository)
|
|
assert.Equal(t, "two", repos[1].Repository)
|
|
assert.Equal(t, "three", repos[2].Repository)
|
|
}
|
|
|
|
func TestNoFilters(t *testing.T) {
|
|
t.Parallel()
|
|
provider := &MockProvider{
|
|
Repos: []*Repository{
|
|
{
|
|
Repository: "one",
|
|
Labels: []string{"prod-one", "prod-two", "staging"},
|
|
},
|
|
{
|
|
Repository: "two",
|
|
Labels: []string{"prod-two"},
|
|
},
|
|
{
|
|
Repository: "three",
|
|
Labels: []string{"staging"},
|
|
},
|
|
},
|
|
}
|
|
filters := []argoprojiov1alpha1.SCMProviderGeneratorFilter{}
|
|
repos, err := ListRepos(t.Context(), provider, filters, "")
|
|
require.NoError(t, err)
|
|
assert.Len(t, repos, 3)
|
|
assert.Equal(t, "one", repos[0].Repository)
|
|
assert.Equal(t, "two", repos[1].Repository)
|
|
assert.Equal(t, "three", repos[2].Repository)
|
|
}
|
|
|
|
// tests the getApplicableFilters function, passing in all the filters, and an unset filter, plus an additional
|
|
// branch filter
|
|
func TestApplicableFilterMap(t *testing.T) {
|
|
t.Parallel()
|
|
branchFilter := Filter{
|
|
BranchMatch: ®exp.Regexp{},
|
|
FilterType: FilterTypeBranch,
|
|
}
|
|
repoFilter := Filter{
|
|
RepositoryMatch: ®exp.Regexp{},
|
|
FilterType: FilterTypeRepo,
|
|
}
|
|
pathExistsFilter := Filter{
|
|
PathsExist: []string{"test"},
|
|
FilterType: FilterTypeBranch,
|
|
}
|
|
pathDoesntExistsFilter := Filter{
|
|
PathsDoNotExist: []string{"test"},
|
|
FilterType: FilterTypeBranch,
|
|
}
|
|
labelMatchFilter := Filter{
|
|
LabelMatch: ®exp.Regexp{},
|
|
FilterType: FilterTypeRepo,
|
|
}
|
|
unsetFilter := Filter{
|
|
LabelMatch: ®exp.Regexp{},
|
|
}
|
|
additionalBranchFilter := Filter{
|
|
BranchMatch: ®exp.Regexp{},
|
|
FilterType: FilterTypeBranch,
|
|
}
|
|
filterMap := getApplicableFilters([]*Filter{
|
|
&branchFilter, &repoFilter,
|
|
&pathExistsFilter, &labelMatchFilter, &unsetFilter, &additionalBranchFilter, &pathDoesntExistsFilter,
|
|
})
|
|
|
|
assert.Len(t, filterMap[FilterTypeRepo], 2)
|
|
assert.Len(t, filterMap[FilterTypeBranch], 4)
|
|
}
|