This repository has been archived by the owner on Jun 21, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathmain_test.go
180 lines (158 loc) · 5.15 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
// pmm-managed
// Copyright (C) 2017 Percona LLC
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package main
import (
"fmt"
"go/build"
"os"
"os/exec"
"sort"
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestPackages(t *testing.T) {
cmd := exec.Command("pmm-managed", "-h")
b, err := cmd.CombinedOutput()
require.NoError(t, err, "%s", b)
out := string(b)
assert.False(t, strings.Contains(out, "-httptest.serve"), `pmm-managed should not import package "net/http/httptest"`)
assert.False(t, strings.Contains(out, "-test.run"), `pmm-managed should not import package "testing"`)
}
func TestImports(t *testing.T) {
type constraint struct {
blacklistPrefixes []string
}
constraints := make(map[string]constraint)
// models should not import services or APIs.
constraints["github.com/percona/pmm-managed/models"] = constraint{
blacklistPrefixes: []string{
"github.com/percona/pmm-managed/services",
"github.com/percona/pmm/api",
},
}
// services should be independent
for _, service := range []string{
"github.com/percona/pmm-managed/services/agents",
"github.com/percona/pmm-managed/services/alertmanager",
"github.com/percona/pmm-managed/services/checks",
"github.com/percona/pmm-managed/services/grafana",
"github.com/percona/pmm-managed/services/qan",
"github.com/percona/pmm-managed/services/server",
"github.com/percona/pmm-managed/services/supervisord",
"github.com/percona/pmm-managed/services/telemetry",
"github.com/percona/pmm-managed/services/victoriametrics",
"github.com/percona/pmm-managed/services/vmalert",
} {
constraints[service] = constraint{
blacklistPrefixes: []string{
"github.com/percona/pmm-managed/services",
},
}
}
for _, service := range []string{
// those services should be independent too, but has some common code
// as converters, errors, ...
"github.com/percona/pmm-managed/services/inventory",
"github.com/percona/pmm-managed/services/management",
"github.com/percona/pmm-managed/services/server",
"github.com/percona/pmm-managed/services/checks",
"github.com/percona/pmm-managed/services/alertmanager",
} {
constraints[service] = constraint{
blacklistPrefixes: []string{
"github.com/percona/pmm-managed/services/",
},
}
}
// validators should not import gRPC stack, including errors
constraints["github.com/percona/pmm-managed/utils/validators"] = constraint{
blacklistPrefixes: []string{
"google.golang.org/grpc",
},
}
// just to add them to packages.dot
for _, service := range []string{
"github.com/percona/pmm-managed",
"github.com/percona/pmm-managed/cmd/pmm-managed-init",
"github.com/percona/pmm-managed/cmd/pmm-managed-starlark",
"github.com/percona/pmm-managed/services/agents/grpc",
"github.com/percona/pmm-managed/services/inventory/grpc",
"github.com/percona/pmm-managed/services/management/grpc",
} {
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 _, b := range c.blacklistPrefixes {
for i := range allImports[path] {
// whitelist own subpackages
if strings.HasPrefix(i, path) {
continue
}
// check blacklist
if strings.HasPrefix(i, b) {
t.Errorf("Package %q should not import package %q (blacklisted by %q).", path, i, b)
}
}
}
}
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-managed")
if p == "" {
p = "/"
}
for _, i := range imports {
if strings.Contains(i, "/utils/") {
continue
}
if strings.HasPrefix(i, "github.com/percona/pmm-managed") {
i = strings.TrimPrefix(i, "github.com/percona/pmm-managed")
fmt.Fprintf(f, "\t%q -> %q;\n", p, i)
}
}
}
fmt.Fprintf(f, "}\n")
}