...

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

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

     1  // Copyright 2018 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/pulltest"
    25  
    26  	"edge-infra.dev/pkg/f8n/devinfra/repo/owners/policybot/policy/common"
    27  )
    28  
    29  func TestBranches(t *testing.T) {
    30  	runBranchesTestCase(t, "^master$", []branchesTestCase{
    31  		{
    32  			"simple match - master",
    33  			"master",
    34  			&common.PredicateResult{
    35  				Satisfied:       true,
    36  				Values:          []string{"master"},
    37  				ConditionValues: []string{"^master$"},
    38  			},
    39  		},
    40  		{
    41  			"simple non match",
    42  			"another-branch",
    43  			&common.PredicateResult{
    44  				Satisfied:       false,
    45  				Values:          []string{"another-branch"},
    46  				ConditionValues: []string{"^master$"},
    47  			},
    48  		},
    49  		{
    50  			"tests anchoring",
    51  			"not-master",
    52  			&common.PredicateResult{
    53  				Satisfied:       false,
    54  				Values:          []string{"not-master"},
    55  				ConditionValues: []string{"^master$"},
    56  			},
    57  		},
    58  	})
    59  
    60  	runBranchesTestCase(t, ".*", []branchesTestCase{
    61  		{
    62  			"matches all example 1",
    63  			"master",
    64  			&common.PredicateResult{
    65  				Satisfied:       true,
    66  				Values:          []string{"master"},
    67  				ConditionValues: []string{".*"},
    68  			},
    69  		},
    70  		{
    71  			"matches all example 2",
    72  			"another-one",
    73  			&common.PredicateResult{
    74  				Satisfied:       true,
    75  				Values:          []string{"another-one"},
    76  				ConditionValues: []string{".*"},
    77  			},
    78  		},
    79  	})
    80  
    81  	runBranchesTestCase(t, "(prod|staging)", []branchesTestCase{
    82  		{
    83  			"matches pattern - prod",
    84  			"prod",
    85  			&common.PredicateResult{
    86  				Satisfied:       true,
    87  				Values:          []string{"prod"},
    88  				ConditionValues: []string{"(prod|staging)"},
    89  			},
    90  		},
    91  		{
    92  			"matches pattern - staging",
    93  			"staging",
    94  			&common.PredicateResult{
    95  				Satisfied:       true,
    96  				Values:          []string{"staging"},
    97  				ConditionValues: []string{"(prod|staging)"},
    98  			},
    99  		},
   100  		{
   101  			"matches pattern - not-a-match",
   102  			"not-a-match",
   103  			&common.PredicateResult{
   104  				Satisfied:       false,
   105  				Values:          []string{"not-a-match"},
   106  				ConditionValues: []string{"(prod|staging)"},
   107  			},
   108  		},
   109  	})
   110  }
   111  
   112  // TODO: generalize this and use it all our test cases
   113  type branchesTestCase struct {
   114  	name                    string
   115  	branchName              string
   116  	ExpectedPredicateResult *common.PredicateResult
   117  }
   118  
   119  func runBranchesTestCase(t *testing.T, regex string, cases []branchesTestCase) {
   120  	ctx := context.Background()
   121  
   122  	for _, tc := range cases {
   123  
   124  		compiledRegexp := common.NewCompiledRegexp(regexp.MustCompile(regex))
   125  		targetsPredicate := &TargetsBranch{
   126  			Pattern: compiledRegexp,
   127  		}
   128  		fromPredicate := &FromBranch{
   129  			Pattern: compiledRegexp,
   130  		}
   131  
   132  		targetsContext := &pulltest.Context{
   133  			BranchBaseName: tc.branchName,
   134  		}
   135  		fromContext := &pulltest.Context{
   136  			BranchHeadName: tc.branchName,
   137  		}
   138  
   139  		t.Run(tc.name+" targets_branch", func(t *testing.T) {
   140  			predicateResult, err := targetsPredicate.Evaluate(ctx, targetsContext)
   141  			if assert.NoError(t, err, "targets_branch predicate evaluation failed") {
   142  				assertPredicateResult(t, tc.ExpectedPredicateResult, predicateResult)
   143  			}
   144  		})
   145  
   146  		t.Run(tc.name+" from_branch", func(t *testing.T) {
   147  			predicateResult, err := fromPredicate.Evaluate(ctx, fromContext)
   148  			if assert.NoError(t, err, "from_branch predicate evaluation failed") {
   149  				assertPredicateResult(t, tc.ExpectedPredicateResult, predicateResult)
   150  			}
   151  		})
   152  	}
   153  }
   154  

View as plain text