Skip to content

Commit b5d7d6c

Browse files
authored
Merge pull request moby#50465 from thaJeztah/less_lazyregexp
remove uses of lazyregexp in tests, test-utilities and packages used externally
2 parents b0dbf75 + f651a5d commit b5d7d6c

File tree

5 files changed

+24
-13
lines changed

5 files changed

+24
-13
lines changed

.golangci.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,10 +333,18 @@ linters:
333333
path: "internal/lazyregexp"
334334
linters:
335335
- forbidigo
336+
- text: 'use of `regexp.MustCompile` forbidden'
337+
path: "internal/testutils"
338+
linters:
339+
- forbidigo
336340
- text: 'use of `regexp.MustCompile` forbidden'
337341
path: "libnetwork/cmd/networkdb-test/dbclient"
338342
linters:
339343
- forbidigo
344+
- text: 'use of `regexp.MustCompile` forbidden'
345+
path: "registry/"
346+
linters:
347+
- forbidigo
340348

341349
# Log a warning if an exclusion rule is unused.
342350
# Default: false

integration-cli/docker_cli_by_digest_test.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111

1212
"github.com/docker/docker/integration-cli/cli"
1313
"github.com/docker/docker/integration-cli/cli/build"
14-
"github.com/docker/docker/internal/lazyregexp"
1514
"github.com/moby/moby/api/types/image"
1615
"github.com/opencontainers/go-digest"
1716
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
@@ -26,8 +25,8 @@ const (
2625
)
2726

2827
var (
29-
pushDigestRegex = lazyregexp.New(`[\S]+: digest: ([\S]+) size: [0-9]+`)
30-
digestRegex = lazyregexp.New(`Digest: ([\S]+)`)
28+
pushDigestRegex = regexp.MustCompile(`[\S]+: digest: ([\S]+) size: [0-9]+`)
29+
digestRegex = regexp.MustCompile(`Digest: ([\S]+)`)
3130
)
3231

3332
func setupImage(t *testing.T) (digest.Digest, error) {

internal/testutils/networking/firewall.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ package networking
33
import (
44
"fmt"
55
"os/exec"
6+
"regexp"
67
"strings"
78
"testing"
89

9-
"github.com/docker/docker/internal/lazyregexp"
1010
"github.com/docker/docker/testutil/daemon"
1111
"gotest.tools/v3/assert"
1212
"gotest.tools/v3/icmd"
@@ -21,7 +21,7 @@ const (
2121
)
2222

2323
// Find the policy in, for example "Chain FORWARD (policy ACCEPT)".
24-
var rePolicy = lazyregexp.New("policy ([A-Za-z]+)")
24+
var rePolicy = regexp.MustCompile("policy ([A-Za-z]+)")
2525

2626
// SetFilterForwardPolicies sets the default policy for the FORWARD chain in
2727
// the filter tables for both IPv4 and IPv6. The original policy is restored

registry/config.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// FIXME(thaJeztah): remove once we are a module; the go:build directive prevents go from downgrading language version to go1.16:
2+
//go:build go1.23
3+
14
package registry
25

36
import (
@@ -6,13 +9,14 @@ import (
69
"net/url"
710
"os"
811
"path/filepath"
12+
"regexp"
913
"runtime"
1014
"strconv"
1115
"strings"
16+
"sync"
1217

1318
"github.com/containerd/log"
1419
"github.com/distribution/reference"
15-
"github.com/docker/docker/internal/lazyregexp"
1620
"github.com/moby/moby/api/types/registry"
1721
)
1822

@@ -57,7 +61,9 @@ var (
5761
Host: DefaultRegistryHost,
5862
}
5963

60-
validHostPortRegex = lazyregexp.New(`^` + reference.DomainRegexp.String() + `$`)
64+
validHostPortRegex = sync.OnceValue(func() *regexp.Regexp {
65+
return regexp.MustCompile(`^` + reference.DomainRegexp.String() + `$`)
66+
})
6167
)
6268

6369
// runningWithRootlessKit is a fork of [rootless.RunningWithRootlessKit],
@@ -334,7 +340,7 @@ func validateHostPort(s string) error {
334340
}
335341
// If match against the `host:port` pattern fails,
336342
// it might be `IPv6:port`, which will be captured by net.ParseIP(host)
337-
if !validHostPortRegex.MatchString(s) && net.ParseIP(host) == nil {
343+
if !validHostPortRegex().MatchString(s) && net.ParseIP(host) == nil {
338344
return invalidParamf("invalid host %q", host)
339345
}
340346
if port != "" {

testutil/environment/clean.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"testing"
77

88
cerrdefs "github.com/containerd/errdefs"
9-
"github.com/docker/docker/internal/lazyregexp"
109
"github.com/moby/moby/api/types"
1110
"github.com/moby/moby/api/types/container"
1211
"github.com/moby/moby/api/types/filters"
@@ -64,9 +63,6 @@ func getPausedContainers(ctx context.Context, t testing.TB, client client.Contai
6463
return containers
6564
}
6665

67-
// FIXME(thaJeztah): can we rewrite this check to not do string-matching, and instead detect error-type?
68-
var alreadyExists = lazyregexp.New(`Error response from daemon: removal of container (\w+) is already in progress`)
69-
7066
func deleteAllContainers(ctx context.Context, t testing.TB, apiclient client.ContainerAPIClient, protectedContainers map[string]struct{}) {
7167
t.Helper()
7268
containers := getAllContainers(ctx, t, apiclient)
@@ -82,7 +78,9 @@ func deleteAllContainers(ctx context.Context, t testing.TB, apiclient client.Con
8278
Force: true,
8379
RemoveVolumes: true,
8480
})
85-
if err == nil || cerrdefs.IsNotFound(err) || alreadyExists.MatchString(err.Error()) {
81+
82+
// Ignore if container is already gone, or removal of container is already in progress.
83+
if err == nil || cerrdefs.IsNotFound(err) || strings.Contains(err.Error(), "is already in progress") {
8684
continue
8785
}
8886
assert.Check(t, err, "failed to remove %s", ctr.ID)

0 commit comments

Comments
 (0)