Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/components/ee/agent-smith/pkg/agent/agent_test.go
2501 views
1
// Copyright (c) 2022 Gitpod GmbH. All rights reserved.
2
// Licensed under the GNU Affero General Public License (AGPL).
3
// See License.AGPL.txt in the project root for license information.
4
5
package agent
6
7
import (
8
"sort"
9
"testing"
10
11
"github.com/gitpod-io/gitpod/agent-smith/pkg/common"
12
"github.com/gitpod-io/gitpod/agent-smith/pkg/config"
13
"github.com/google/go-cmp/cmp"
14
)
15
16
func TestGetPenalty(t *testing.T) {
17
tests := []struct {
18
Desc string
19
Default config.EnforcementRules
20
Repo config.EnforcementRules
21
Infringement []Infringement
22
Penalties []config.PenaltyKind
23
}{
24
{
25
Desc: "audit only",
26
Default: config.EnforcementRules{config.GradeKind(config.InfringementExec, common.SeverityAudit): config.PenaltyStopWorkspace},
27
Infringement: []Infringement{{Kind: config.GradeKind(config.InfringementExec, common.SeverityAudit)}},
28
Penalties: []config.PenaltyKind{config.PenaltyStopWorkspace},
29
},
30
{
31
Desc: "repo only",
32
Repo: config.EnforcementRules{config.GradeKind(config.InfringementExec, common.SeverityAudit): config.PenaltyStopWorkspace},
33
Infringement: []Infringement{{Kind: config.GradeKind(config.InfringementExec, common.SeverityAudit)}},
34
Penalties: []config.PenaltyKind{config.PenaltyStopWorkspace},
35
},
36
{
37
Desc: "repo override",
38
Default: config.EnforcementRules{config.GradeKind(config.InfringementExec, common.SeverityAudit): config.PenaltyStopWorkspace},
39
Repo: config.EnforcementRules{config.GradeKind(config.InfringementExec, common.SeverityAudit): config.PenaltyNone},
40
Infringement: []Infringement{{Kind: config.GradeKind(config.InfringementExec, common.SeverityAudit)}},
41
Penalties: nil,
42
},
43
}
44
45
for _, test := range tests {
46
t.Run(test.Desc, func(t *testing.T) {
47
penalties := getPenalty(test.Default, test.Repo, test.Infringement)
48
sort.Slice(penalties, func(i, j int) bool { return penalties[i] < penalties[j] })
49
50
if diff := cmp.Diff(test.Penalties, penalties); diff != "" {
51
t.Errorf("unexpected penalties (-want +got):\n%s", diff)
52
}
53
})
54
}
55
}
56
57
func TestFindEnforcementRules(t *testing.T) {
58
ra := config.EnforcementRules{config.GradeKind(config.InfringementExec, common.SeverityAudit): config.PenaltyLimitCPU}
59
tests := []struct {
60
Desc string
61
Rules map[string]config.EnforcementRules
62
RemoteURL string
63
Expectation config.EnforcementRules
64
}{
65
{"direct match", map[string]config.EnforcementRules{"foo": ra}, "foo", ra},
66
{"no match", map[string]config.EnforcementRules{"foo*": ra}, "not found", nil},
67
{"star", map[string]config.EnforcementRules{"foo*": ra}, "foobar", ra},
68
{"prefix match", map[string]config.EnforcementRules{"*foo": ra}, "hello/foo", ra},
69
{"suffix match", map[string]config.EnforcementRules{"foo*": ra}, "foobar", ra},
70
{"case-insensitive match", map[string]config.EnforcementRules{"foo*": ra}, "Foobar", ra},
71
{"submatch", map[string]config.EnforcementRules{"*foo*": ra}, "hello/foo/bar", ra},
72
}
73
74
for _, test := range tests {
75
t.Run(test.Desc, func(t *testing.T) {
76
res := findEnforcementRules(test.Rules, test.RemoteURL)
77
78
if diff := cmp.Diff(test.Expectation, res); diff != "" {
79
t.Errorf("unexpected result (-want +got):\n%s", diff)
80
}
81
})
82
}
83
}
84
85
func BenchmarkFindEnforcementRules(b *testing.B) {
86
ra := config.EnforcementRules{config.GradeKind(config.InfringementExec, common.SeverityAudit): config.PenaltyLimitCPU}
87
rules := map[string]config.EnforcementRules{
88
"*foo*": ra,
89
"bar": ra, "bar1": ra, "bar2": ra, "bar3": ra, "bar4": ra, "bar5": ra, "bar6": ra,
90
"foo1*": ra, "foo2*": ra, "foo3*": ra, "foo4*": ra, "foo5*": ra, "foo6*": ra, "foo7*": ra,
91
}
92
93
for i := 0; i < b.N; i++ {
94
findEnforcementRules(rules, "foobar")
95
}
96
}
97
98