File: reporter_test.go

package info (click to toggle)
gitlab-agent 16.1.3-2
  • links: PTS, VCS
  • area: contrib
  • in suites: forky, sid, trixie
  • size: 6,324 kB
  • sloc: makefile: 175; sh: 52; ruby: 3
file content (160 lines) | stat: -rw-r--r-- 4,921 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package agent

import (
	"context"
	"io"
	"net/http"
	"strings"
	"testing"

	"github.com/golang/mock/gomock"
	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
	"gitlab.com/gitlab-org/cluster-integration/gitlab-agent/v16/internal/module/modagent"
	"gitlab.com/gitlab-org/cluster-integration/gitlab-agent/v16/internal/tool/testing/mock_modagent"
	report "gitlab.com/gitlab-org/security-products/analyzers/report/v3"
	"go.uber.org/zap/zaptest"
)

var payloads = []*Payload{{
	Scanner: TrivyScanner,
	Vulnerability: &report.Vulnerability{
		Name:        "CVE-2020-27350",
		Message:     "CVE-2020-27350 in apt",
		Description: "apt: integer overflows and underflows while parsing .deb packages",
		Solution:    "Upgrade apt from 1.8.2 to 1.8.2.2",
		Severity:    report.SeverityLevelMedium,
		Confidence:  report.ConfidenceLevelUnknown,
		Identifiers: []report.Identifier{
			{
				Type:  report.IdentifierTypeCVE,
				Name:  "CVE-2020-27350",
				Value: "CVE-2020-27350",
				URL:   "https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2020-27350",
			},
		},
		Links: []report.Link{{URL: "https://avd.aquasec.com/nvd/cve-2020-27350"}},
	},
}}

func TestCreateVulnerability(t *testing.T) {
	ctrl := gomock.NewController(t)
	api := mock_modagent.NewMockApi(ctrl)
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()

	api.EXPECT().MakeGitLabRequest(ctx, "/",
		gomock.Any(),
		gomock.Any(),
	).Times(1).
		DoAndReturn(func(ctx context.Context, path string, opts ...modagent.GitLabRequestOption) (*modagent.GitLabResponse, error) {
			cancel()

			return &modagent.GitLabResponse{
				StatusCode: http.StatusOK,
				Body:       io.NopCloser(strings.NewReader(`{"uuid": "ed1da145-ed9c-4d4c-adbf-6b9f8a305745"}`)),
			}, nil
		})

	reporter := NewReporter(zaptest.NewLogger(t), api)
	uuid, err := reporter.createVulnerability(ctx, payloads[0])
	require.NoError(t, err)
	assert.Equal(t, uuid, "ed1da145-ed9c-4d4c-adbf-6b9f8a305745")
}

func TestCreateVulnerabilityEmptyResponseBody(t *testing.T) {
	ctrl := gomock.NewController(t)
	api := mock_modagent.NewMockApi(ctrl)
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()

	api.EXPECT().MakeGitLabRequest(ctx, "/",
		gomock.Any(),
		gomock.Any(),
	).Times(1).
		DoAndReturn(func(ctx context.Context, path string, opts ...modagent.GitLabRequestOption) (*modagent.GitLabResponse, error) {
			cancel()

			return &modagent.GitLabResponse{
				StatusCode: http.StatusOK,
				Body:       io.NopCloser(strings.NewReader("")),
			}, nil
		})

	reporter := NewReporter(zaptest.NewLogger(t), api)
	uuid, err := reporter.createVulnerability(ctx, payloads[0])
	require.Error(t, err)
	assert.Empty(t, uuid)
}

func TestResolveVulnerabilities(t *testing.T) {
	ctrl := gomock.NewController(t)
	api := mock_modagent.NewMockApi(ctrl)
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()

	api.EXPECT().MakeGitLabRequest(ctx, "/scan_result",
		gomock.Any(),
		gomock.Any(),
	).Times(1).
		DoAndReturn(func(ctx context.Context, path string, opts ...modagent.GitLabRequestOption) (*modagent.GitLabResponse, error) {
			cancel()

			return &modagent.GitLabResponse{
				StatusCode: http.StatusOK,
			}, nil
		})

	reporter := NewReporter(zaptest.NewLogger(t), api)
	err := reporter.ResolveVulnerabilities(ctx, []string{"586444af-3561-4b71-8cfe-4e5570c0c114", "7cc3a693-d616-4cfc-beef-3a391b3d0348"})
	require.NoError(t, err)
}

func TestTransmit(t *testing.T) {
	ctrl := gomock.NewController(t)
	api := mock_modagent.NewMockApi(ctrl)
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()

	api.EXPECT().MakeGitLabRequest(ctx, "/",
		gomock.Any(),
		gomock.Any(),
	).DoAndReturn(func(ctx context.Context, path string, opts ...modagent.GitLabRequestOption) (*modagent.GitLabResponse, error) {
		cancel()

		return &modagent.GitLabResponse{
			StatusCode: http.StatusOK,
			Body:       io.NopCloser(strings.NewReader(`{"uuid": "ed1da145-ed9c-4d4c-adbf-6b9f8a305745"}`)),
		}, nil
	})

	reporter := NewReporter(zaptest.NewLogger(t), api)
	uuids, err := reporter.Transmit(ctx, payloads)
	require.NoError(t, err)
	assert.Equal(t, uuids, []string{"ed1da145-ed9c-4d4c-adbf-6b9f8a305745"})
}

func TestTransmitEmptyResponseBody(t *testing.T) {
	ctrl := gomock.NewController(t)
	api := mock_modagent.NewMockApi(ctrl)
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()

	api.EXPECT().MakeGitLabRequest(ctx, "/",
		gomock.Any(),
		gomock.Any(),
	).Times(1).
		DoAndReturn(func(ctx context.Context, path string, opts ...modagent.GitLabRequestOption) (*modagent.GitLabResponse, error) {
			cancel()

			return &modagent.GitLabResponse{
				StatusCode: http.StatusOK,
				Body:       io.NopCloser(strings.NewReader("")),
			}, nil
		})

	reporter := NewReporter(zaptest.NewLogger(t), api)
	uuids, err := reporter.Transmit(ctx, payloads)
	require.Error(t, err)
	assert.Empty(t, uuids)
}