...

Source file src/edge-infra.dev/pkg/f8n/devinfra/repo/owners/policybot/policy/predicate/status_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 TestHasSuccessfulStatus(t *testing.T) {
    30  	p := HasSuccessfulStatus([]string{"status-name", "status-name-2"})
    31  
    32  	runStatusTestCase(t, p, []StatusTestCase{
    33  		{
    34  			"all statuses succeed",
    35  			&pulltest.Context{
    36  				LatestStatusesValue: map[string]string{
    37  					"status-name":   "success",
    38  					"status-name-2": "success",
    39  				},
    40  			},
    41  			&common.PredicateResult{
    42  				Satisfied: true,
    43  				Values:    []string{"status-name", "status-name-2"},
    44  			},
    45  		},
    46  		{
    47  			"a status fails",
    48  			&pulltest.Context{
    49  				LatestStatusesValue: map[string]string{
    50  					"status-name":   "success",
    51  					"status-name-2": "failure",
    52  				},
    53  			},
    54  			&common.PredicateResult{
    55  				Satisfied: false,
    56  				Values:    []string{"status-name-2"},
    57  			},
    58  		},
    59  		{
    60  			"multiple statuses fail",
    61  			&pulltest.Context{
    62  				LatestStatusesValue: map[string]string{
    63  					"status-name":   "failure",
    64  					"status-name-2": "failure",
    65  				},
    66  			},
    67  			&common.PredicateResult{
    68  				Satisfied: false,
    69  				Values:    []string{"status-name", "status-name-2"},
    70  			},
    71  		},
    72  		{
    73  			"a status does not exist",
    74  			&pulltest.Context{
    75  				LatestStatusesValue: map[string]string{
    76  					"status-name": "success",
    77  				},
    78  			},
    79  			&common.PredicateResult{
    80  				Satisfied: false,
    81  				Values:    []string{"status-name-2"},
    82  			},
    83  		},
    84  		{
    85  			"multiple statuses do not exist",
    86  			&pulltest.Context{},
    87  			&common.PredicateResult{
    88  				Satisfied: false,
    89  				Values:    []string{"status-name", "status-name-2"},
    90  			},
    91  		},
    92  	})
    93  }
    94  
    95  type StatusTestCase struct {
    96  	name                    string
    97  	context                 pull.Context
    98  	ExpectedPredicateResult *common.PredicateResult
    99  }
   100  
   101  func runStatusTestCase(t *testing.T, p Predicate, cases []StatusTestCase) {
   102  	ctx := context.Background()
   103  
   104  	for _, tc := range cases {
   105  		t.Run(tc.name, func(t *testing.T) {
   106  			predicateResult, err := p.Evaluate(ctx, tc.context)
   107  			if assert.NoError(t, err, "evaluation failed") {
   108  				assertPredicateResult(t, tc.ExpectedPredicateResult, predicateResult)
   109  			}
   110  		})
   111  	}
   112  }
   113  

View as plain text