github.com/argoproj/argo-cd/v2@v2.10.9/applicationset/services/scm_provider/utils_test.go (about)

     1  package scm_provider
     2  
     3  import (
     4  	"context"
     5  	"regexp"
     6  	"testing"
     7  
     8  	"github.com/stretchr/testify/assert"
     9  
    10  	argoprojiov1alpha1 "github.com/argoproj/argo-cd/v2/pkg/apis/application/v1alpha1"
    11  )
    12  
    13  func strp(s string) *string {
    14  	return &s
    15  }
    16  
    17  func TestFilterRepoMatch(t *testing.T) {
    18  	provider := &MockProvider{
    19  		Repos: []*Repository{
    20  			{
    21  				Repository: "one",
    22  			},
    23  			{
    24  				Repository: "two",
    25  			},
    26  			{
    27  				Repository: "three",
    28  			},
    29  			{
    30  				Repository: "four",
    31  			},
    32  		},
    33  	}
    34  	filters := []argoprojiov1alpha1.SCMProviderGeneratorFilter{
    35  		{
    36  			RepositoryMatch: strp("n|hr"),
    37  		},
    38  	}
    39  	repos, err := ListRepos(context.Background(), provider, filters, "")
    40  	assert.Nil(t, err)
    41  	assert.Len(t, repos, 2)
    42  	assert.Equal(t, "one", repos[0].Repository)
    43  	assert.Equal(t, "three", repos[1].Repository)
    44  }
    45  
    46  func TestFilterLabelMatch(t *testing.T) {
    47  	provider := &MockProvider{
    48  		Repos: []*Repository{
    49  			{
    50  				Repository: "one",
    51  				Labels:     []string{"prod-one", "prod-two", "staging"},
    52  			},
    53  			{
    54  				Repository: "two",
    55  				Labels:     []string{"prod-two"},
    56  			},
    57  			{
    58  				Repository: "three",
    59  				Labels:     []string{"staging"},
    60  			},
    61  		},
    62  	}
    63  	filters := []argoprojiov1alpha1.SCMProviderGeneratorFilter{
    64  		{
    65  			LabelMatch: strp("^prod-.*$"),
    66  		},
    67  	}
    68  	repos, err := ListRepos(context.Background(), provider, filters, "")
    69  	assert.Nil(t, err)
    70  	assert.Len(t, repos, 2)
    71  	assert.Equal(t, "one", repos[0].Repository)
    72  	assert.Equal(t, "two", repos[1].Repository)
    73  }
    74  
    75  func TestFilterPathExists(t *testing.T) {
    76  	provider := &MockProvider{
    77  		Repos: []*Repository{
    78  			{
    79  				Repository: "one",
    80  			},
    81  			{
    82  				Repository: "two",
    83  			},
    84  			{
    85  				Repository: "three",
    86  			},
    87  		},
    88  	}
    89  	filters := []argoprojiov1alpha1.SCMProviderGeneratorFilter{
    90  		{
    91  			PathsExist: []string{"two"},
    92  		},
    93  	}
    94  	repos, err := ListRepos(context.Background(), provider, filters, "")
    95  	assert.Nil(t, err)
    96  	assert.Len(t, repos, 1)
    97  	assert.Equal(t, "two", repos[0].Repository)
    98  }
    99  
   100  func TestFilterPathDoesntExists(t *testing.T) {
   101  	provider := &MockProvider{
   102  		Repos: []*Repository{
   103  			{
   104  				Repository: "one",
   105  			},
   106  			{
   107  				Repository: "two",
   108  			},
   109  			{
   110  				Repository: "three",
   111  			},
   112  		},
   113  	}
   114  	filters := []argoprojiov1alpha1.SCMProviderGeneratorFilter{
   115  		{
   116  			PathsDoNotExist: []string{"two"},
   117  		},
   118  	}
   119  	repos, err := ListRepos(context.Background(), provider, filters, "")
   120  	assert.Nil(t, err)
   121  	assert.Len(t, repos, 2)
   122  }
   123  func TestFilterRepoMatchBadRegexp(t *testing.T) {
   124  	provider := &MockProvider{
   125  		Repos: []*Repository{
   126  			{
   127  				Repository: "one",
   128  			},
   129  		},
   130  	}
   131  	filters := []argoprojiov1alpha1.SCMProviderGeneratorFilter{
   132  		{
   133  			RepositoryMatch: strp("("),
   134  		},
   135  	}
   136  	_, err := ListRepos(context.Background(), provider, filters, "")
   137  	assert.NotNil(t, err)
   138  }
   139  
   140  func TestFilterLabelMatchBadRegexp(t *testing.T) {
   141  	provider := &MockProvider{
   142  		Repos: []*Repository{
   143  			{
   144  				Repository: "one",
   145  			},
   146  		},
   147  	}
   148  	filters := []argoprojiov1alpha1.SCMProviderGeneratorFilter{
   149  		{
   150  			LabelMatch: strp("("),
   151  		},
   152  	}
   153  	_, err := ListRepos(context.Background(), provider, filters, "")
   154  	assert.NotNil(t, err)
   155  }
   156  
   157  func TestFilterBranchMatch(t *testing.T) {
   158  	provider := &MockProvider{
   159  		Repos: []*Repository{
   160  			{
   161  				Repository: "one",
   162  				Branch:     "one",
   163  			},
   164  			{
   165  				Repository: "one",
   166  				Branch:     "two",
   167  			},
   168  			{
   169  				Repository: "two",
   170  				Branch:     "one",
   171  			},
   172  			{
   173  				Repository: "three",
   174  				Branch:     "one",
   175  			},
   176  			{
   177  				Repository: "three",
   178  				Branch:     "two",
   179  			},
   180  		},
   181  	}
   182  	filters := []argoprojiov1alpha1.SCMProviderGeneratorFilter{
   183  		{
   184  			BranchMatch: strp("w"),
   185  		},
   186  	}
   187  	repos, err := ListRepos(context.Background(), provider, filters, "")
   188  	assert.Nil(t, err)
   189  	assert.Len(t, repos, 2)
   190  	assert.Equal(t, "one", repos[0].Repository)
   191  	assert.Equal(t, "two", repos[0].Branch)
   192  	assert.Equal(t, "three", repos[1].Repository)
   193  	assert.Equal(t, "two", repos[1].Branch)
   194  }
   195  
   196  func TestMultiFilterAnd(t *testing.T) {
   197  	provider := &MockProvider{
   198  		Repos: []*Repository{
   199  			{
   200  				Repository: "one",
   201  				Labels:     []string{"prod-one", "prod-two", "staging"},
   202  			},
   203  			{
   204  				Repository: "two",
   205  				Labels:     []string{"prod-two"},
   206  			},
   207  			{
   208  				Repository: "three",
   209  				Labels:     []string{"staging"},
   210  			},
   211  		},
   212  	}
   213  	filters := []argoprojiov1alpha1.SCMProviderGeneratorFilter{
   214  		{
   215  			RepositoryMatch: strp("w"),
   216  			LabelMatch:      strp("^prod-.*$"),
   217  		},
   218  	}
   219  	repos, err := ListRepos(context.Background(), provider, filters, "")
   220  	assert.Nil(t, err)
   221  	assert.Len(t, repos, 1)
   222  	assert.Equal(t, "two", repos[0].Repository)
   223  }
   224  
   225  func TestMultiFilterOr(t *testing.T) {
   226  	provider := &MockProvider{
   227  		Repos: []*Repository{
   228  			{
   229  				Repository: "one",
   230  				Labels:     []string{"prod-one", "prod-two", "staging"},
   231  			},
   232  			{
   233  				Repository: "two",
   234  				Labels:     []string{"prod-two"},
   235  			},
   236  			{
   237  				Repository: "three",
   238  				Labels:     []string{"staging"},
   239  			},
   240  		},
   241  	}
   242  	filters := []argoprojiov1alpha1.SCMProviderGeneratorFilter{
   243  		{
   244  			RepositoryMatch: strp("e"),
   245  		},
   246  		{
   247  			LabelMatch: strp("^prod-.*$"),
   248  		},
   249  	}
   250  	repos, err := ListRepos(context.Background(), provider, filters, "")
   251  	assert.Nil(t, err)
   252  	assert.Len(t, repos, 3)
   253  	assert.Equal(t, "one", repos[0].Repository)
   254  	assert.Equal(t, "two", repos[1].Repository)
   255  	assert.Equal(t, "three", repos[2].Repository)
   256  }
   257  
   258  func TestNoFilters(t *testing.T) {
   259  	provider := &MockProvider{
   260  		Repos: []*Repository{
   261  			{
   262  				Repository: "one",
   263  				Labels:     []string{"prod-one", "prod-two", "staging"},
   264  			},
   265  			{
   266  				Repository: "two",
   267  				Labels:     []string{"prod-two"},
   268  			},
   269  			{
   270  				Repository: "three",
   271  				Labels:     []string{"staging"},
   272  			},
   273  		},
   274  	}
   275  	filters := []argoprojiov1alpha1.SCMProviderGeneratorFilter{}
   276  	repos, err := ListRepos(context.Background(), provider, filters, "")
   277  	assert.Nil(t, err)
   278  	assert.Len(t, repos, 3)
   279  	assert.Equal(t, "one", repos[0].Repository)
   280  	assert.Equal(t, "two", repos[1].Repository)
   281  	assert.Equal(t, "three", repos[2].Repository)
   282  }
   283  
   284  // tests the getApplicableFilters function, passing in all the filters, and an unset filter, plus an additional
   285  // branch filter
   286  func TestApplicableFilterMap(t *testing.T) {
   287  	branchFilter := Filter{
   288  		BranchMatch: &regexp.Regexp{},
   289  		FilterType:  FilterTypeBranch,
   290  	}
   291  	repoFilter := Filter{
   292  		RepositoryMatch: &regexp.Regexp{},
   293  		FilterType:      FilterTypeRepo,
   294  	}
   295  	pathExistsFilter := Filter{
   296  		PathsExist: []string{"test"},
   297  		FilterType: FilterTypeBranch,
   298  	}
   299  	pathDoesntExistsFilter := Filter{
   300  		PathsDoNotExist: []string{"test"},
   301  		FilterType:      FilterTypeBranch,
   302  	}
   303  	labelMatchFilter := Filter{
   304  		LabelMatch: &regexp.Regexp{},
   305  		FilterType: FilterTypeRepo,
   306  	}
   307  	unsetFilter := Filter{
   308  		LabelMatch: &regexp.Regexp{},
   309  	}
   310  	additionalBranchFilter := Filter{
   311  		BranchMatch: &regexp.Regexp{},
   312  		FilterType:  FilterTypeBranch,
   313  	}
   314  	filterMap := getApplicableFilters([]*Filter{&branchFilter, &repoFilter,
   315  		&pathExistsFilter, &labelMatchFilter, &unsetFilter, &additionalBranchFilter, &pathDoesntExistsFilter})
   316  
   317  	assert.Len(t, filterMap[FilterTypeRepo], 2)
   318  	assert.Len(t, filterMap[FilterTypeBranch], 4)
   319  }