...

Source file src/edge-infra.dev/pkg/f8n/devinfra/repo/owners/policybot/policy/predicate/repository_test.go

Documentation: edge-infra.dev/pkg/f8n/devinfra/repo/owners/policybot/policy/predicate

     1  // Copyright 2022 Palantir Technologies, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package predicate
    16  
    17  import (
    18  	"context"
    19  	"regexp"
    20  	"testing"
    21  
    22  	"github.com/stretchr/testify/assert"
    23  
    24  	"edge-infra.dev/pkg/f8n/devinfra/repo/owners/policybot/pull"
    25  	"edge-infra.dev/pkg/f8n/devinfra/repo/owners/policybot/pull/pulltest"
    26  
    27  	"edge-infra.dev/pkg/f8n/devinfra/repo/owners/policybot/policy/common"
    28  )
    29  
    30  func TestRepositoryWithNotMatchRule(t *testing.T) {
    31  	p := &Repository{
    32  		NotMatches: []common.Regexp{
    33  			common.NewCompiledRegexp(regexp.MustCompile("palantir/docs")),
    34  		},
    35  		Matches: []common.Regexp{},
    36  	}
    37  
    38  	runRepositoryTestCase(t, p, []RepositoryTestCase{
    39  		{
    40  			"matches pattern",
    41  			&pulltest.Context{
    42  				OwnerValue: "palantir",
    43  				RepoValue:  "docs",
    44  			},
    45  			&common.PredicateResult{
    46  				Satisfied: false,
    47  				Values:    []string{"palantir/docs"},
    48  				ConditionsMap: map[string][]string{
    49  					"not match": {"palantir/docs"},
    50  					"match":     nil,
    51  				},
    52  			},
    53  		},
    54  		{
    55  			"does not match pattern",
    56  			&pulltest.Context{
    57  				OwnerValue: "palantir",
    58  				RepoValue:  "policy-bot",
    59  			},
    60  			&common.PredicateResult{
    61  				Satisfied: true,
    62  				Values:    []string{"palantir/policy-bot"},
    63  				ConditionsMap: map[string][]string{
    64  					"not match": {"palantir/docs"},
    65  				},
    66  			},
    67  		},
    68  	})
    69  }
    70  
    71  func TestRepositoryWithMatchRule(t *testing.T) {
    72  	p := &Repository{
    73  		NotMatches: []common.Regexp{},
    74  		Matches: []common.Regexp{
    75  			common.NewCompiledRegexp(regexp.MustCompile("palantir/policy.*")),
    76  		},
    77  	}
    78  
    79  	runRepositoryTestCase(t, p, []RepositoryTestCase{
    80  		{
    81  			"matches pattern",
    82  			&pulltest.Context{
    83  				OwnerValue: "palantir",
    84  				RepoValue:  "policy-bot",
    85  			},
    86  			&common.PredicateResult{
    87  				Satisfied: true,
    88  				Values:    []string{"palantir/policy-bot"},
    89  				ConditionsMap: map[string][]string{
    90  					"match": {"palantir/policy.*"},
    91  				},
    92  			},
    93  		},
    94  		{
    95  			"does not match pattern",
    96  			&pulltest.Context{
    97  				OwnerValue: "palantir",
    98  				RepoValue:  "nanda-bot",
    99  			},
   100  			&common.PredicateResult{
   101  				Satisfied: false,
   102  				Values:    []string{"palantir/nanda-bot"},
   103  				ConditionsMap: map[string][]string{
   104  					"not match": nil,
   105  					"match":     {"palantir/policy.*"},
   106  				},
   107  			},
   108  		},
   109  	})
   110  }
   111  
   112  func TestRepositoryWithMixedRules(t *testing.T) {
   113  	p := &Repository{
   114  		NotMatches: []common.Regexp{
   115  			common.NewCompiledRegexp(regexp.MustCompile("palantir/.*docs")),
   116  			common.NewCompiledRegexp(regexp.MustCompile("palantir/special-repo")),
   117  		},
   118  		Matches: []common.Regexp{
   119  			common.NewCompiledRegexp(regexp.MustCompile("palantir/policy.*")),
   120  		},
   121  	}
   122  
   123  	runRepositoryTestCase(t, p, []RepositoryTestCase{
   124  		{
   125  			"matches pattern in match list",
   126  			&pulltest.Context{
   127  				OwnerValue: "palantir",
   128  				RepoValue:  "policy-bot",
   129  			},
   130  			&common.PredicateResult{
   131  				Satisfied: true,
   132  				Values:    []string{"palantir/policy-bot"},
   133  				ConditionsMap: map[string][]string{
   134  					"match": {"palantir/policy.*"},
   135  				},
   136  			},
   137  		},
   138  		{
   139  			"matches pattern in not_match list",
   140  			&pulltest.Context{
   141  				OwnerValue: "palantir",
   142  				RepoValue:  "docs",
   143  			},
   144  			&common.PredicateResult{
   145  				Satisfied: false,
   146  				Values:    []string{"palantir/docs"},
   147  				ConditionsMap: map[string][]string{
   148  					"not match": {"palantir/.*docs", "palantir/special-repo"},
   149  					"match":     {"palantir/policy.*"},
   150  				},
   151  			},
   152  		},
   153  		{
   154  			"matches pattern in both lists",
   155  			&pulltest.Context{
   156  				OwnerValue: "palantir",
   157  				RepoValue:  "policy-bot-docs",
   158  			},
   159  			&common.PredicateResult{
   160  				Satisfied: true,
   161  				Values:    []string{"palantir/policy-bot-docs"},
   162  				ConditionsMap: map[string][]string{
   163  					"match": {"palantir/policy.*"},
   164  				},
   165  			},
   166  		},
   167  		{
   168  			"does not match any pattern",
   169  			&pulltest.Context{
   170  				OwnerValue: "palantir",
   171  				RepoValue:  "some-other-repo",
   172  			},
   173  			&common.PredicateResult{
   174  				Satisfied: true,
   175  				Values:    []string{"palantir/some-other-repo"},
   176  				ConditionsMap: map[string][]string{
   177  					"not match": {"palantir/.*docs", "palantir/special-repo"},
   178  				},
   179  			},
   180  		},
   181  	})
   182  }
   183  
   184  type RepositoryTestCase struct {
   185  	name                    string
   186  	context                 pull.Context
   187  	ExpectedPredicateResult *common.PredicateResult
   188  }
   189  
   190  func runRepositoryTestCase(t *testing.T, p Predicate, cases []RepositoryTestCase) {
   191  	ctx := context.Background()
   192  
   193  	for _, tc := range cases {
   194  		t.Run(tc.name, func(t *testing.T) {
   195  			predicateResult, err := p.Evaluate(ctx, tc.context)
   196  			if assert.NoError(t, err, "evaluation failed") {
   197  				assertPredicateResult(t, tc.ExpectedPredicateResult, predicateResult)
   198  			}
   199  		})
   200  	}
   201  }
   202  

View as plain text