Path: blob/main/components/ee/agent-smith/pkg/agent/agent_test.go
2501 views
// Copyright (c) 2022 Gitpod GmbH. All rights reserved.1// Licensed under the GNU Affero General Public License (AGPL).2// See License.AGPL.txt in the project root for license information.34package agent56import (7"sort"8"testing"910"github.com/gitpod-io/gitpod/agent-smith/pkg/common"11"github.com/gitpod-io/gitpod/agent-smith/pkg/config"12"github.com/google/go-cmp/cmp"13)1415func TestGetPenalty(t *testing.T) {16tests := []struct {17Desc string18Default config.EnforcementRules19Repo config.EnforcementRules20Infringement []Infringement21Penalties []config.PenaltyKind22}{23{24Desc: "audit only",25Default: config.EnforcementRules{config.GradeKind(config.InfringementExec, common.SeverityAudit): config.PenaltyStopWorkspace},26Infringement: []Infringement{{Kind: config.GradeKind(config.InfringementExec, common.SeverityAudit)}},27Penalties: []config.PenaltyKind{config.PenaltyStopWorkspace},28},29{30Desc: "repo only",31Repo: config.EnforcementRules{config.GradeKind(config.InfringementExec, common.SeverityAudit): config.PenaltyStopWorkspace},32Infringement: []Infringement{{Kind: config.GradeKind(config.InfringementExec, common.SeverityAudit)}},33Penalties: []config.PenaltyKind{config.PenaltyStopWorkspace},34},35{36Desc: "repo override",37Default: config.EnforcementRules{config.GradeKind(config.InfringementExec, common.SeverityAudit): config.PenaltyStopWorkspace},38Repo: config.EnforcementRules{config.GradeKind(config.InfringementExec, common.SeverityAudit): config.PenaltyNone},39Infringement: []Infringement{{Kind: config.GradeKind(config.InfringementExec, common.SeverityAudit)}},40Penalties: nil,41},42}4344for _, test := range tests {45t.Run(test.Desc, func(t *testing.T) {46penalties := getPenalty(test.Default, test.Repo, test.Infringement)47sort.Slice(penalties, func(i, j int) bool { return penalties[i] < penalties[j] })4849if diff := cmp.Diff(test.Penalties, penalties); diff != "" {50t.Errorf("unexpected penalties (-want +got):\n%s", diff)51}52})53}54}5556func TestFindEnforcementRules(t *testing.T) {57ra := config.EnforcementRules{config.GradeKind(config.InfringementExec, common.SeverityAudit): config.PenaltyLimitCPU}58tests := []struct {59Desc string60Rules map[string]config.EnforcementRules61RemoteURL string62Expectation config.EnforcementRules63}{64{"direct match", map[string]config.EnforcementRules{"foo": ra}, "foo", ra},65{"no match", map[string]config.EnforcementRules{"foo*": ra}, "not found", nil},66{"star", map[string]config.EnforcementRules{"foo*": ra}, "foobar", ra},67{"prefix match", map[string]config.EnforcementRules{"*foo": ra}, "hello/foo", ra},68{"suffix match", map[string]config.EnforcementRules{"foo*": ra}, "foobar", ra},69{"case-insensitive match", map[string]config.EnforcementRules{"foo*": ra}, "Foobar", ra},70{"submatch", map[string]config.EnforcementRules{"*foo*": ra}, "hello/foo/bar", ra},71}7273for _, test := range tests {74t.Run(test.Desc, func(t *testing.T) {75res := findEnforcementRules(test.Rules, test.RemoteURL)7677if diff := cmp.Diff(test.Expectation, res); diff != "" {78t.Errorf("unexpected result (-want +got):\n%s", diff)79}80})81}82}8384func BenchmarkFindEnforcementRules(b *testing.B) {85ra := config.EnforcementRules{config.GradeKind(config.InfringementExec, common.SeverityAudit): config.PenaltyLimitCPU}86rules := map[string]config.EnforcementRules{87"*foo*": ra,88"bar": ra, "bar1": ra, "bar2": ra, "bar3": ra, "bar4": ra, "bar5": ra, "bar6": ra,89"foo1*": ra, "foo2*": ra, "foo3*": ra, "foo4*": ra, "foo5*": ra, "foo6*": ra, "foo7*": ra,90}9192for i := 0; i < b.N; i++ {93findEnforcementRules(rules, "foobar")94}95}969798