...

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

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

     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 policy
    16  
    17  import (
    18  	"context"
    19  	"errors"
    20  	"testing"
    21  
    22  	"github.com/stretchr/testify/assert"
    23  	"github.com/stretchr/testify/require"
    24  
    25  	"edge-infra.dev/pkg/f8n/devinfra/repo/owners/policybot/pull"
    26  	"edge-infra.dev/pkg/f8n/devinfra/repo/owners/policybot/pull/pulltest"
    27  
    28  	"edge-infra.dev/pkg/f8n/devinfra/repo/owners/policybot/policy/common"
    29  )
    30  
    31  type StaticEvaluator common.Result
    32  
    33  func (eval *StaticEvaluator) Trigger() common.Trigger {
    34  	return common.TriggerStatic
    35  }
    36  
    37  func (eval *StaticEvaluator) Evaluate(ctx context.Context, prctx pull.Context) common.Result {
    38  	return common.Result(*eval)
    39  }
    40  
    41  func TestEvaluator(t *testing.T) {
    42  	ctx := context.Background()
    43  	prctx := &pulltest.Context{}
    44  
    45  	t.Run("disapprovalWins", func(t *testing.T) {
    46  		eval := evaluator{
    47  			approval: &StaticEvaluator{
    48  				Status: common.StatusApproved,
    49  			},
    50  			disapproval: &StaticEvaluator{
    51  				Status:            common.StatusDisapproved,
    52  				StatusDescription: "disapproved by test",
    53  			},
    54  		}
    55  
    56  		r := eval.Evaluate(ctx, prctx)
    57  		require.NoError(t, r.Error)
    58  
    59  		assert.Equal(t, common.StatusDisapproved, r.Status)
    60  		assert.Equal(t, "disapproved by test", r.StatusDescription)
    61  	})
    62  
    63  	t.Run("approvalWinsByDefault", func(t *testing.T) {
    64  		eval := evaluator{
    65  			approval: &StaticEvaluator{
    66  				Status:            common.StatusPending,
    67  				StatusDescription: "2 approvals needed",
    68  			},
    69  			disapproval: &StaticEvaluator{
    70  				Status: common.StatusSkipped,
    71  			},
    72  		}
    73  
    74  		r := eval.Evaluate(ctx, prctx)
    75  		require.NoError(t, r.Error)
    76  
    77  		assert.Equal(t, common.StatusPending, r.Status)
    78  		assert.Equal(t, "2 approvals needed", r.StatusDescription)
    79  	})
    80  
    81  	t.Run("propagateError", func(t *testing.T) {
    82  		eval := evaluator{
    83  			approval: &StaticEvaluator{
    84  				Error: errors.New("approval failed"),
    85  			},
    86  			disapproval: &StaticEvaluator{
    87  				Status: common.StatusDisapproved,
    88  			},
    89  		}
    90  
    91  		r := eval.Evaluate(ctx, prctx)
    92  
    93  		assert.EqualError(t, r.Error, "approval failed")
    94  		assert.Equal(t, common.StatusSkipped, r.Status)
    95  	})
    96  
    97  	t.Run("setsProperties", func(t *testing.T) {
    98  		eval := evaluator{
    99  			approval: &StaticEvaluator{
   100  				Status: common.StatusPending,
   101  			},
   102  			disapproval: &StaticEvaluator{
   103  				Status: common.StatusDisapproved,
   104  			},
   105  		}
   106  
   107  		r := eval.Evaluate(ctx, prctx)
   108  		require.NoError(t, r.Error)
   109  
   110  		assert.Equal(t, "policy", r.Name)
   111  		if assert.Len(t, r.Children, 2) {
   112  			assert.Equal(t, castToResult(eval.approval), r.Children[0])
   113  			assert.Equal(t, castToResult(eval.disapproval), r.Children[1])
   114  		}
   115  	})
   116  }
   117  
   118  func castToResult(e common.Evaluator) *common.Result {
   119  	return (*common.Result)(e.(*StaticEvaluator))
   120  }
   121  

View as plain text