This repository has been archived by the owner on Aug 24, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
/
main_test.go
259 lines (224 loc) · 7.19 KB
/
main_test.go
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
// pmm-agent
// Copyright 2019 Percona LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"fmt"
"go/build"
"os"
"sort"
"strings"
"testing"
"github.com/stretchr/testify/require"
)
/*
Commenting out these tests because not always we have a proper executable in the path.
These tests should be moved to QA testing framework.
--- FAIL: TestPackages (0.00s)
main_test.go:35:
Error Trace: main_test.go:35
Error: Received unexpected error:
exec: "pmm-agent": executable file not found in $PATH
Test: TestPackages
--- FAIL: TestVersionJson (0.00s)
main_test.go:67:
Error Trace: main_test.go:67
Error: Received unexpected error:
exec: "pmm-agent": executable file not found in $PATH
Test: TestVersionJson
--- FAIL: TestVersionPlain (0.00s)
main_test.go:46:
Error Trace: main_test.go:46
Error: Received unexpected error:
exec: "pmm-agent": executable file not found in $PATH
Test: TestVersionPlain
func TestPackages(t *testing.T) {
cmd := exec.Command("pmm-agent", "-h") //nolint:gosec
b, err := cmd.CombinedOutput()
require.NoError(t, err, "%s", b)
out := string(b)
assert.False(t, strings.Contains(out, "httptest.serve"), `pmm-agent should not import package "net/http/httptest"`)
assert.False(t, strings.Contains(out, "test.run"), `pmm-agent should not import package "testing"`)
}
func TestVersionPlain(t *testing.T) {
t.Parallel()
cmd := exec.Command("pmm-agent", "--version")
b, err := cmd.CombinedOutput()
require.NoError(t, err, "%s", b)
out := string(b)
assert.True(t, strings.Contains(out, `Version:`), `'pmm-agent --version --json' produces incorrect output format`)
}
// TODO: Review/Rewrite this test.
// 1. Only works with a built agent installed in the path
// 2. Just building the agent does not guarantees that there is a version set.
// go run main.go --version
// panic: pmm-agent version is not set during build.
//
// goroutine 1 [running]:
// main.main()
// /home/user/go/src/github.com/percona/pmm-agent/main.go:34 +0x22a
// exit status 2
// 3. Do we really need to test the output of a command? It is making local tests to always fail.
func TestVersionJson(t *testing.T) {
t.Parallel()
cmd := exec.Command("pmm-agent", "--version", "--json")
b, err := cmd.CombinedOutput()
require.NoError(t, err, "%s", b)
var jsonStruct interface{}
err = json.Unmarshal(b, &jsonStruct)
require.NoError(t, err, `'pmm-agent --version --json' produces incorrect output format`)
}
*/
func TestImports(t *testing.T) {
type constraint struct {
denyPrefixes []string
allowPrefixes []string
}
constraints := make(map[string]constraint)
agentsUsingCache := []string{"/perfschema", "/pgstatstatements"}
// agents code should be independent
for _, a := range []string{
"github.com/percona/pmm-agent/agents/mongodb",
"github.com/percona/pmm-agent/agents/mysql/perfschema",
"github.com/percona/pmm-agent/agents/mysql/slowlog",
"github.com/percona/pmm-agent/agents/noop",
"github.com/percona/pmm-agent/agents/postgres/pgstatmonitor",
"github.com/percona/pmm-agent/agents/postgres/pgstatstatements",
"github.com/percona/pmm-agent/agents/process",
"github.com/percona/pmm-agent/agents/cache",
} {
c := constraint{
denyPrefixes: []string{
"github.com/percona/pmm-agent/agentlocal",
"github.com/percona/pmm-agent/agents/",
"github.com/percona/pmm-agent/client",
"github.com/percona/pmm-agent/config",
},
}
// TODO move parser to pgstatstatements/parser unless pgstatmonitor will actually use it
if strings.HasSuffix(a, "/pgstatstatements") {
c.allowPrefixes = []string{
"github.com/percona/pmm-agent/agents/postgres/parser",
}
}
// allows agents to use cache
for _, cachedAgent := range agentsUsingCache {
if strings.HasSuffix(a, cachedAgent) {
c.allowPrefixes = append(c.allowPrefixes, "github.com/percona/pmm-agent/agents/cache")
}
}
constraints[a] = c
}
// slowlog parser should be fully independent
constraints["github.com/percona/pmm-agent/agents/mysql/slowlog/parser"] = constraint{
denyPrefixes: []string{
"github.com/percona/pmm-agent",
},
}
// those packages should be independent from each other
packs := []string{
// TODO https://jira.percona.com/browse/PMM-7206
// "github.com/percona/pmm-agent/actions",
"github.com/percona/pmm-agent/agentlocal",
"github.com/percona/pmm-agent/agents/supervisor",
"github.com/percona/pmm-agent/client",
"github.com/percona/pmm-agent/connectionchecker",
}
for _, p := range packs {
var c constraint
for _, d := range packs {
if p == d {
continue
}
c.denyPrefixes = append(c.denyPrefixes, d)
}
constraints[p] = c
}
// just to add them to packages.dot
for _, service := range []string{
"github.com/percona/pmm-agent/commands",
} {
constraints[service] = constraint{}
}
allImports := make(map[string]map[string]struct{})
for path, c := range constraints {
p, err := build.Import(path, ".", build.IgnoreVendor)
require.NoError(t, err)
if allImports[path] == nil {
allImports[path] = make(map[string]struct{})
}
for _, i := range p.Imports {
allImports[path][i] = struct{}{}
}
for _, i := range p.TestImports {
allImports[path][i] = struct{}{}
}
for _, i := range p.XTestImports {
allImports[path][i] = struct{}{}
}
for _, d := range c.denyPrefixes {
for i := range allImports[path] {
// allow own subpackages
if strings.HasPrefix(i, path) {
continue
}
// check allowlist
var allow bool
for _, a := range c.allowPrefixes {
if strings.HasPrefix(i, a) {
allow = true
break
}
}
if allow {
continue
}
// check denylist
if strings.HasPrefix(i, d) {
t.Errorf("Package %q should not import package %q (denied by %q).", path, i, d)
}
}
}
}
f, err := os.Create("packages.dot")
require.NoError(t, err)
defer func() { require.NoError(t, f.Close()) }()
fmt.Fprintf(f, "digraph packages {\n")
packages := make([]string, 0, len(allImports))
for p := range allImports {
packages = append(packages, p)
}
sort.Strings(packages)
for _, p := range packages {
imports := make([]string, 0, len(allImports[p]))
for p := range allImports[p] {
imports = append(imports, p)
}
sort.Strings(imports)
p = strings.TrimPrefix(p, "github.com/percona/pmm-agent")
if p == "" {
p = "/"
}
for _, i := range imports {
if strings.Contains(i, "/utils/") {
continue
}
if strings.HasPrefix(i, "github.com/percona/pmm-agent") {
i = strings.TrimPrefix(i, "github.com/percona/pmm-agent")
fmt.Fprintf(f, "\t%q -> %q;\n", p, i)
}
}
}
fmt.Fprintf(f, "}\n")
}