Skip to content

Commit 7f83f71

Browse files
committed
fix lint issues
Signed-off-by: CrazyMax <1951866+crazy-max@users.noreply.github.com>
1 parent 1488e91 commit 7f83f71

15 files changed

Lines changed: 75 additions & 72 deletions

File tree

cmd/gotestmetrics/parse.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ func (c *parseCmd) Run(ctx *Context) error {
4040
if err != nil {
4141
return errors.Wrap(err, "failed to marshal result")
4242
}
43-
if err := os.WriteFile(c.Output, dt, 0644); err != nil {
43+
if err := os.WriteFile(c.Output, dt, 0600); err != nil {
4444
return errors.Wrap(err, "failed to write result to output file")
4545
}
4646
}

cmd/refcandidates/main.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
package main
22

33
import (
4+
"crypto/rand"
45
"encoding/json"
56
"log"
6-
"math/rand"
7+
"math/big"
78
"os"
89
"path/filepath"
910
"strings"
10-
"time"
1111

1212
"github.com/alecthomas/kong"
1313
"github.com/moby/buildkit-bench/util/candidates"
@@ -60,7 +60,7 @@ func writeFile(f string, c *candidates.Candidates) error {
6060
if err != nil {
6161
return errors.Wrap(err, "failed to marshal candidates")
6262
}
63-
if err := os.WriteFile(f, dt, 0644); err != nil {
63+
if err := os.WriteFile(f, dt, 0600); err != nil {
6464
return errors.Wrap(err, "failed to write candidates to output file")
6565
}
6666
log.Printf("Candidates written to %q", f)
@@ -106,12 +106,15 @@ func setGhaOutput(name string, c *candidates.Candidates) error {
106106
}
107107

108108
if gha.IsPullRequestEvent() {
109-
r := rand.New(rand.NewSource(time.Now().UnixNano()))
110109
if len(includes) > 2 {
111110
s := make([]include, 0, 2)
112111
si := make(map[int]struct{})
113112
for len(s) < 2 {
114-
idx := r.Intn(len(includes))
113+
n, err := rand.Int(rand.Reader, big.NewInt(int64(len(includes))))
114+
if err != nil {
115+
return errors.Wrap(err, "failed to sample candidate")
116+
}
117+
idx := int(n.Int64())
115118
if _, exists := si[idx]; !exists {
116119
si[idx] = struct{}{}
117120
s = append(s, includes[idx])

test/build_test.go

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@ package test
22

33
import (
44
"fmt"
5+
"os"
56
"strings"
67
"sync"
78
"testing"
89

910
"github.com/containerd/continuity/fs/fstest"
1011
"github.com/containerd/platforms"
1112
"github.com/moby/buildkit-bench/util/testutil"
13+
"github.com/pkg/errors"
1214
"github.com/stretchr/testify/require"
1315
)
1416

@@ -59,7 +61,7 @@ func BenchmarkBuild(b *testing.B) {
5961
benchmarkBuildExportUncompressed,
6062
benchmarkBuildExportGzip,
6163
benchmarkBuildExportEstargz,
62-
//benchmarkBuildExportZstd, https://github.com/moby/buildkit-bench/pull/146#discussion_r1771519112
64+
// benchmarkBuildExportZstd, https://github.com/moby/buildkit-bench/pull/146#discussion_r1771519112
6365
),
6466
testutil.WithMirroredImages(mirroredImages),
6567
)
@@ -172,27 +174,42 @@ RUN cp /etc/foo /etc/bar
172174
}
173175

174176
var wg sync.WaitGroup
177+
errCh := make(chan error, n)
175178
for i := 0; i < n; i++ {
176179
wg.Add(1)
177180
go func() {
178181
defer wg.Done()
179-
dir := tmpdir(
180-
b,
182+
dir, err := os.MkdirTemp("", "bkbench-build-context")
183+
if err != nil {
184+
errCh <- err
185+
return
186+
}
187+
defer os.RemoveAll(dir)
188+
if err := fstest.Apply(
181189
fstest.CreateFile("Dockerfile", dockerfile, 0600),
182190
fstest.CreateFile("foo", []byte("foo"), 0600),
183-
)
191+
).Apply(dir); err != nil {
192+
errCh <- err
193+
return
194+
}
184195
out, err := buildxBuildCmd(sb, withArgs("--output=type=image", dir))
185196
// TODO: use sb.WriteLogFile to write buildx logs in a defer with a
186197
// semaphore using a buffered channel to limit the number of
187198
// concurrent goroutines. This might affect timing.
188-
require.NoError(b, err, out)
199+
if err != nil {
200+
errCh <- errors.Wrap(err, out)
201+
}
189202
}()
190203
}
191204
reportBuildkitdAlloc(b, sb, func() {
192205
b.StartTimer()
193206
wg.Wait()
194207
b.StopTimer()
195208
})
209+
close(errCh)
210+
for err := range errCh {
211+
require.NoError(b, err)
212+
}
196213
}
197214

198215
func benchmarkBuildFileTransfer(b *testing.B, sb testutil.Sandbox) {
@@ -290,9 +307,7 @@ func benchmarkBuildExportGzip(b *testing.B, sb testutil.Sandbox) {
290307
func benchmarkBuildExportEstargz(b *testing.B, sb testutil.Sandbox) {
291308
benchmarkBuildExport(b, sb, "gzip")
292309
}
293-
func benchmarkBuildExportZstd(b *testing.B, sb testutil.Sandbox) {
294-
benchmarkBuildExport(b, sb, "zstd")
295-
}
310+
296311
func benchmarkBuildExport(b *testing.B, sb testutil.Sandbox, compression string) {
297312
dockerfile := []byte(`
298313
FROM python:latest

test/buildx_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package test
22

33
import (
4+
"context"
45
"os"
56
"os/exec"
67
"testing"
@@ -23,14 +24,14 @@ func BenchmarkBuildx(b *testing.B) {
2324
}
2425

2526
func testBuildxVersion(t *testing.T, sb testutil.Sandbox) {
26-
output, err := exec.Command(sb.BuildxBin(), "version").Output()
27+
output, err := exec.CommandContext(context.Background(), sb.BuildxBin(), "version").Output() //nolint:gosec // test utility
2728
require.NoError(t, err)
2829
t.Log(string(output))
2930
}
3031

3132
func benchmarkBuildxVersion(b *testing.B, sb testutil.Sandbox) {
3233
b.StartTimer()
33-
err := exec.Command(sb.BuildxBin(), "version").Run()
34+
err := exec.CommandContext(context.Background(), sb.BuildxBin(), "version").Run() //nolint:gosec // test utility
3435
b.StopTimer()
3536
require.NoError(b, err)
3637
}

test/daemon_test.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package test
22

33
import (
4+
"context"
45
"encoding/json"
56
"fmt"
67
"net/http"
@@ -32,7 +33,7 @@ func BenchmarkDaemon(b *testing.B) {
3233
func testDaemonVersion(t *testing.T, sb testutil.Sandbox) {
3334
buildkitdPath := path.Join(sb.BuildKitBinsDir(), sb.Name(), "buildkitd")
3435

35-
output, err := exec.Command(buildkitdPath, "--version").Output()
36+
output, err := exec.CommandContext(context.Background(), buildkitdPath, "--version").Output()
3637
require.NoError(t, err)
3738

3839
versionParts := strings.Fields(string(output))
@@ -46,7 +47,9 @@ func testDaemonVersion(t *testing.T, sb testutil.Sandbox) {
4647
func testDaemonDebugHeap(t *testing.T, sb testutil.Sandbox) {
4748
client := &http.Client{}
4849

49-
resp, err := client.Get(fmt.Sprintf("http://%s/debug/pprof/heap?debug=1", sb.DebugAddress()))
50+
req, err := http.NewRequestWithContext(context.Background(), http.MethodGet, fmt.Sprintf("http://%s/debug/pprof/heap?debug=1", sb.DebugAddress()), nil)
51+
require.NoError(t, err)
52+
resp, err := client.Do(req)
5053
require.NoError(t, err)
5154
defer resp.Body.Close()
5255

@@ -63,7 +66,7 @@ func testDaemonDebugHeap(t *testing.T, sb testutil.Sandbox) {
6366
func benchmarkDaemonVersion(b *testing.B, sb testutil.Sandbox) {
6467
buildkitdPath := path.Join(sb.BuildKitBinsDir(), sb.Name(), "buildkitd")
6568
b.StartTimer()
66-
err := exec.Command(buildkitdPath, "--version").Run()
69+
err := exec.CommandContext(context.Background(), buildkitdPath, "--version").Run()
6770
b.StopTimer()
6871
require.NoError(b, err)
6972
}

test/util.go

Lines changed: 7 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
package test
22

33
import (
4+
"context"
45
"fmt"
56
"net/http"
67
"os"
78
"os/exec"
8-
"path"
99
"testing"
1010

1111
"github.com/containerd/continuity/fs/fstest"
@@ -24,12 +24,6 @@ func tmpdir(tb testing.TB, appliers ...fstest.Applier) string {
2424

2525
type cmdOpt func(*exec.Cmd)
2626

27-
func withEnv(env ...string) cmdOpt {
28-
return func(cmd *exec.Cmd) {
29-
cmd.Env = append(cmd.Env, env...)
30-
}
31-
}
32-
3327
func withArgs(args ...string) cmdOpt {
3428
return func(cmd *exec.Cmd) {
3529
cmd.Args = append(cmd.Args, args...)
@@ -43,7 +37,7 @@ func withDir(dir string) cmdOpt {
4337
}
4438

4539
func buildxCmd(sb testutil.Sandbox, opts ...cmdOpt) *exec.Cmd {
46-
cmd := exec.Command(sb.BuildxBin())
40+
cmd := exec.CommandContext(context.Background(), sb.BuildxBin()) //nolint:gosec // test utility
4741
cmd.Env = append([]string{}, os.Environ()...)
4842
for _, opt := range opts {
4943
opt(cmd)
@@ -64,26 +58,6 @@ func buildxBuildCmd(sb testutil.Sandbox, opts ...cmdOpt) (string, error) {
6458
return string(out), err
6559
}
6660

67-
func buildctlCmd(sb testutil.Sandbox, opts ...cmdOpt) *exec.Cmd {
68-
cmd := exec.Command(path.Join(sb.BuildKitBinsDir(), sb.Name(), "buildctl"))
69-
cmd.Args = append(cmd.Args, "--debug")
70-
if buildkitAddr := sb.Address(); buildkitAddr != "" {
71-
cmd.Args = append(cmd.Args, "--addr", buildkitAddr)
72-
}
73-
cmd.Env = append([]string{}, os.Environ()...)
74-
for _, opt := range opts {
75-
opt(cmd)
76-
}
77-
return cmd
78-
}
79-
80-
func buildctlBuildCmd(sb testutil.Sandbox, opts ...cmdOpt) (string, error) {
81-
opts = append([]cmdOpt{withArgs("build", "--frontend=dockerfile.v0")}, opts...)
82-
cmd := buildctlCmd(sb, opts...)
83-
out, err := cmd.CombinedOutput()
84-
return string(out), err
85-
}
86-
8761
func reportBuildkitdAlloc(b *testing.B, sb testutil.Sandbox, cb func()) {
8862
beforeAlloc, errb := buildkitdAlloc(sb)
8963
cb()
@@ -95,7 +69,11 @@ func reportBuildkitdAlloc(b *testing.B, sb testutil.Sandbox, cb func()) {
9569

9670
func buildkitdAlloc(sb testutil.Sandbox) (int64, error) {
9771
client := &http.Client{}
98-
resp, err := client.Get(fmt.Sprintf("http://%s/debug/pprof/heap?gc=1", sb.DebugAddress()))
72+
req, err := http.NewRequestWithContext(context.Background(), http.MethodGet, fmt.Sprintf("http://%s/debug/pprof/heap?gc=1", sb.DebugAddress()), nil)
73+
if err != nil {
74+
return 0, err
75+
}
76+
resp, err := client.Do(req)
9977
if err != nil {
10078
return 0, err
10179
}

util/candidates/candidates.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -159,11 +159,12 @@ func (c *Candidates) setCommits(lastDays int) error {
159159
}
160160
res := make(map[string]Commit)
161161
for date, cm := range lastCommitByDay(filterMergeCommits(commits)) {
162-
if containsCommitSha(c.Refs, *cm.SHA) {
162+
switch {
163+
case containsCommitSha(c.Refs, *cm.SHA):
163164
log.Printf("Skipping commit %s, already in refs", *cm.SHA)
164-
} else if containsCommitSha(c.Releases, *cm.SHA) {
165+
case containsCommitSha(c.Releases, *cm.SHA):
165166
log.Printf("Skipping commit %s, already in releases", *cm.SHA)
166-
} else {
167+
default:
167168
res[date] = Commit{
168169
SHA: *cm.SHA,
169170
Date: *cm.Commit.Committer.Date.GetTime(),

util/gotest/benchmark.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -150,15 +150,16 @@ func (b *BenchmarkInfo) update(output string) bool {
150150
return false
151151
}
152152
// https://github.com/golang/go/blob/f38d42f2c4c6ad0d7cbdad5e1417cac3be2a5dcb/src/testing/benchmark.go#L246-L255
153-
if strings.HasPrefix(output, "goos: ") {
153+
switch {
154+
case strings.HasPrefix(output, "goos: "):
154155
b.OS = strings.TrimPrefix(output, "goos: ")
155-
} else if strings.HasPrefix(output, "goarch: ") {
156+
case strings.HasPrefix(output, "goarch: "):
156157
b.Architecture = strings.TrimPrefix(output, "goarch: ")
157-
} else if strings.HasPrefix(output, "pkg: ") {
158+
case strings.HasPrefix(output, "pkg: "):
158159
b.Package = strings.TrimPrefix(output, "pkg: ")
159-
} else if strings.HasPrefix(output, "cpu: ") {
160+
case strings.HasPrefix(output, "cpu: "):
160161
b.CPU = strings.TrimPrefix(output, "cpu: ")
161-
} else {
162+
default:
162163
return false
163164
}
164165
return true

util/gotest/benchmark/parse.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@ package benchmark
55

66
import (
77
"bufio"
8-
"fmt"
98
"io"
109
"strconv"
1110
"strings"
11+
12+
"github.com/pkg/errors"
1213
)
1314

1415
// Flags used by Benchmark.Measured to indicate
@@ -40,10 +41,10 @@ func ParseLine(line string) (*Benchmark, error) {
4041

4142
// Two required, positional fields: Name and iterations.
4243
if len(fields) < 2 {
43-
return nil, fmt.Errorf("two fields required, have %d", len(fields))
44+
return nil, errors.Errorf("two fields required, have %d", len(fields))
4445
}
4546
if !strings.HasPrefix(fields[0], "Benchmark") {
46-
return nil, fmt.Errorf(`first field does not start with "Benchmark"`)
47+
return nil, errors.Errorf(`first field does not start with "Benchmark"`)
4748
}
4849
n, err := strconv.Atoi(fields[1])
4950
if err != nil {

util/gotest/parse.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ func (e *eventHandler) Event(event testjson.TestEvent, _ *testjson.Execution) er
6565
return nil
6666
}
6767
if e.log != nil {
68-
log.Printf(event.Output)
68+
e.log.Printf("%s", event.Output)
6969
}
7070

7171
e.mu.Lock()

0 commit comments

Comments
 (0)