1
2
3
4
5
6
7
8
9
10
11
12
13
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