...

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

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

     1  // Copyright 2019 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  	"testing"
    20  
    21  	"github.com/stretchr/testify/assert"
    22  
    23  	"edge-infra.dev/pkg/f8n/devinfra/repo/owners/policybot/pull"
    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 TestHasLabels(t *testing.T) {
    30  	p := HasLabels([]string{"foo", "bar"})
    31  
    32  	runLabelsTestCase(t, p, []HasLabelsTestCase{
    33  		{
    34  			"all labels",
    35  			&pulltest.Context{
    36  				LabelsValue: []string{"foo", "bar"},
    37  			},
    38  			&common.PredicateResult{
    39  				Satisfied:       true,
    40  				Values:          []string{"foo", "bar"},
    41  				ConditionValues: []string{"foo", "bar"},
    42  			},
    43  		},
    44  		{
    45  			"missing a label",
    46  			&pulltest.Context{
    47  				LabelsValue: []string{"foo"},
    48  			},
    49  			&common.PredicateResult{
    50  				Satisfied:       false,
    51  				Values:          []string{"foo"},
    52  				ConditionValues: []string{"bar"},
    53  			},
    54  		},
    55  		{
    56  			"no labels",
    57  			&pulltest.Context{
    58  				LabelsValue: []string{},
    59  			},
    60  			&common.PredicateResult{
    61  				Satisfied:       false,
    62  				Values:          []string{},
    63  				ConditionValues: []string{"foo"},
    64  			},
    65  		},
    66  	})
    67  }
    68  
    69  type HasLabelsTestCase struct {
    70  	name                    string
    71  	context                 pull.Context
    72  	ExpectedPredicateResult *common.PredicateResult
    73  }
    74  
    75  func runLabelsTestCase(t *testing.T, p Predicate, cases []HasLabelsTestCase) {
    76  	ctx := context.Background()
    77  
    78  	for _, tc := range cases {
    79  		t.Run(tc.name, func(t *testing.T) {
    80  			predicateResult, err := p.Evaluate(ctx, tc.context)
    81  			if assert.NoError(t, err, "evaluation failed") {
    82  				assertPredicateResult(t, tc.ExpectedPredicateResult, predicateResult)
    83  			}
    84  		})
    85  	}
    86  }
    87  

View as plain text