chore: update golangci-lint and fix all issues (#3575)

* chore: Update golangci-lint

Signed-off-by: Lars Francke <git@lars-francke.de>

* chore: fix all golangci-lint issues

- Remove deprecated `// +build` tags
- Fix godoclint, modernize, wsl_v5, govet, lll, gci, noctx issues
- Update linter configuration
- Modernize code to use Go 1.22+ features (for range N, slices.Contains, etc.)
- Update make check lint the privileged tests

Signed-off-by: Andrei Aaron <andreifdaaron@gmail.com>

---------

Signed-off-by: Lars Francke <git@lars-francke.de>
Signed-off-by: Andrei Aaron <andreifdaaron@gmail.com>
Co-authored-by: Lars Francke <git@lars-francke.de>
This commit is contained in:
Andrei Aaron
2025-11-22 23:36:48 +02:00
committed by GitHub
parent 566286ae42
commit da426850e7
242 changed files with 811 additions and 1010 deletions
-1
View File
@@ -1,5 +1,4 @@
//go:build search
// +build search
package server_test
-1
View File
@@ -1,5 +1,4 @@
//go:build sync && scrub && metrics && search && userprefs && mgmt && imagetrust && events
// +build sync,scrub,metrics,search,userprefs,mgmt,imagetrust,events
package server_test
+3 -3
View File
@@ -10,6 +10,7 @@ import (
"path"
"path/filepath"
"regexp"
"slices"
"strconv"
"strings"
"time"
@@ -24,7 +25,6 @@ import (
"zotregistry.dev/zot/v2/pkg/api"
"zotregistry.dev/zot/v2/pkg/api/config"
"zotregistry.dev/zot/v2/pkg/api/constants"
"zotregistry.dev/zot/v2/pkg/common"
extconf "zotregistry.dev/zot/v2/pkg/extensions/config"
eventsconf "zotregistry.dev/zot/v2/pkg/extensions/config/events"
"zotregistry.dev/zot/v2/pkg/extensions/monitoring"
@@ -203,7 +203,7 @@ func newVerifyFeatureCmd(conf *config.Config) *cobra.Command {
return verifyFeatureCmd
}
// "zot" - registry server.
// NewServerRootCmd creates a "zot" registry server command.
func NewServerRootCmd() *cobra.Command {
showVersion := false
conf := config.New()
@@ -908,7 +908,7 @@ func LoadConfiguration(config *config.Config, configPath string) error {
/* if file extension is not supported, try everything
it's also possible that the filename is starting with a dot eg: ".config". */
if !common.Contains(viper.SupportedExts, ext) {
if !slices.Contains(viper.SupportedExts, ext) {
ext = ""
}
+29 -28
View File
@@ -668,7 +668,7 @@ storage:
},
{
"Should fail verify if session driver is enabled and sessionKeysFile present",
[]byte(fmt.Sprintf(`{
fmt.Appendf([]byte{}, `{
"storage":{
"rootDirectory":"/tmp/zot"
},
@@ -698,7 +698,7 @@ storage:
"enable": true
}
}
}`, tmpSessionKeysFile.Name())),
}`, tmpSessionKeysFile.Name()),
false,
zerr.ErrBadConfig.Error() + ": session keys not supported when redis session driver is used!",
},
@@ -1562,12 +1562,12 @@ storage:
err = tmpCredsFile.Close()
So(err, ShouldBeNil)
content = []byte(fmt.Sprintf(`{"distSpecVersion":"1.1.1","storage":{"rootDirectory":"/tmp/zot"},
content = fmt.Appendf([]byte{}, `{"distSpecVersion":"1.1.1","storage":{"rootDirectory":"/tmp/zot"},
"http":{"address":"127.0.0.1","port":"8080","realm":"zot",
"auth":{"openid":{"providers":{"oidc":{"issuer":"http://127.0.0.1:5556/dex",
"credentialsFile":"%s","scopes":["openid"]}}}}},
"log":{"level":"debug"}}`,
tmpCredsFile.Name()),
tmpCredsFile.Name(),
)
_, err = tmpfile.Write(content)
So(err, ShouldBeNil)
@@ -1677,11 +1677,11 @@ storage:
err = tmpCredsFile.Close()
So(err, ShouldBeNil)
content = []byte(fmt.Sprintf(`{ "distSpecVersion": "1.1.1",
content = fmt.Appendf([]byte{}, `{ "distSpecVersion": "1.1.1",
"storage": { "rootDirectory": "/tmp/zot" }, "http": { "address": "127.0.0.1", "port": "8080",
"auth":{"htpasswd":{"path":"test/data/htpasswd"}, "sessionKeysFile": "%s",
"failDelay": 5 } }, "log": { "level": "debug" } }`,
tmpCredsFile.Name()),
tmpCredsFile.Name(),
)
_, err = tmpFile.Write(content)
@@ -1712,11 +1712,11 @@ storage:
err = tmpCredsFile.Close()
So(err, ShouldBeNil)
content = []byte(fmt.Sprintf(`{ "distSpecVersion": "1.1.1",
content = fmt.Appendf([]byte{}, `{ "distSpecVersion": "1.1.1",
"storage": { "rootDirectory": "/tmp/zot" }, "http": { "address": "127.0.0.1", "port": "8080",
"auth":{"htpasswd":{"path":"test/data/htpasswd"}, "sessionKeysFile": "%s",
"failDelay": 5 } }, "log": { "level": "debug" } }`,
tmpCredsFile.Name()),
tmpCredsFile.Name(),
)
_, err = tmpFile.Write(content)
@@ -1748,13 +1748,13 @@ storage:
err = tmpCredsFile.Close()
So(err, ShouldBeNil)
content = []byte(fmt.Sprintf(`{ "distSpecVersion": "1.1.1",
content = fmt.Appendf([]byte{}, `{ "distSpecVersion": "1.1.1",
"storage": { "rootDirectory": "/tmp/zot" }, "http": { "address": "127.0.0.1", "port": "8080",
"auth": { "ldap": { "credentialsFile": "%v", "address": "ldap.example.org", "port": 389,
"startTLS": false, "baseDN": "ou=Users,dc=example,dc=org",
"userAttribute": "uid", "userGroupAttribute": "memberOf", "skipVerify": true, "subtreeSearch": true },
"failDelay": 5 } }, "log": { "level": "debug" } }`,
tmpCredsFile.Name()),
tmpCredsFile.Name(),
)
_, err = tmpFile.Write(content)
@@ -1786,13 +1786,13 @@ storage:
err = tmpCredsFile.Close()
So(err, ShouldBeNil)
content = []byte(fmt.Sprintf(`{ "distSpecVersion": "1.1.1",
content = fmt.Appendf([]byte{}, `{ "distSpecVersion": "1.1.1",
"storage": { "rootDirectory": "/tmp/zot" }, "http": { "address": "127.0.0.1", "port": "8080",
"auth": { "ldap": { "credentialsFile": "%v", "address": "ldap.example.org", "port": 389,
"startTLS": false, "baseDN": "ou=Users,dc=example,dc=org",
"userAttribute": "uid", "userGroupAttribute": "memberOf", "skipVerify": true, "subtreeSearch": true },
"failDelay": 5 } }, "log": { "level": "debug" } }`,
tmpCredsFile.Name()),
tmpCredsFile.Name(),
)
_, err = tmpFile.Write(content)
@@ -1826,13 +1826,13 @@ storage:
err = tmpCredsFile.Close()
So(err, ShouldBeNil)
content = []byte(fmt.Sprintf(`{ "distSpecVersion": "1.1.1",
content = fmt.Appendf([]byte{}, `{ "distSpecVersion": "1.1.1",
"storage": { "rootDirectory": "/tmp/zot" }, "http": { "address": "127.0.0.1", "port": "8080",
"auth": { "ldap": { "credentialsFile": "%v", "address": "ldap.example.org", "port": 389,
"startTLS": false, "baseDN": "ou=Users,dc=example,dc=org",
"userAttribute": "uid", "userGroupAttribute": "memberOf", "skipVerify": true, "subtreeSearch": true },
"failDelay": 5 } }, "log": { "level": "debug" } }`,
tmpCredsFile.Name()),
tmpCredsFile.Name(),
)
_, err = tmpFile.Write(content)
@@ -1863,13 +1863,13 @@ storage:
err = tmpCredsFile.Close()
So(err, ShouldBeNil)
content = []byte(fmt.Sprintf(`{ "distSpecVersion": "1.1.1",
content = fmt.Appendf([]byte{}, `{ "distSpecVersion": "1.1.1",
"storage": { "rootDirectory": "/tmp/zot" }, "http": { "address": "127.0.0.1", "port": "8080",
"auth": { "ldap": { "credentialsFile": "%v", "address": "ldap.example.org", "port": 389,
"startTLS": false, "baseDN": "ou=Users,dc=example,dc=org",
"userAttribute": "uid", "userGroupAttribute": "memberOf", "skipVerify": true, "subtreeSearch": true },
"failDelay": 5 } }, "log": { "level": "debug" } }`,
tmpCredsFile.Name()),
tmpCredsFile.Name(),
)
_, err = tmpFile.Write(content)
@@ -1900,13 +1900,13 @@ storage:
err = tmpCredsFile.Close()
So(err, ShouldBeNil)
content = []byte(fmt.Sprintf(`{ "distSpecVersion": "1.1.1",
content = fmt.Appendf([]byte{}, `{ "distSpecVersion": "1.1.1",
"storage": { "rootDirectory": "/tmp/zot" }, "http": { "address": "127.0.0.1", "port": "8080",
"auth": { "ldap": { "credentialsFile": "%v", "address": "ldap.example.org", "port": 389,
"startTLS": false, "baseDN": "ou=Users,dc=example,dc=org",
"userAttribute": "uid", "userGroupAttribute": "memberOf", "skipVerify": true, "subtreeSearch": true },
"failDelay": 5 } }, "log": { "level": "debug" } }`,
tmpCredsFile.Name()),
tmpCredsFile.Name(),
)
_, err = tmpFile.Write(content)
@@ -1945,12 +1945,12 @@ func TestApiKeyConfig(t *testing.T) {
defer os.Remove(tmpfile.Name())
content = []byte(fmt.Sprintf(`{"distSpecVersion":"1.1.1","storage":{"rootDirectory":"/tmp/zot"},
content = fmt.Appendf([]byte{}, `{"distSpecVersion":"1.1.1","storage":{"rootDirectory":"/tmp/zot"},
"http":{"address":"127.0.0.1","port":"8080","realm":"zot",
"auth":{"openid":{"providers":{"oidc":{"issuer":"http://127.0.0.1:5556/dex",
"credentialsFile":"%s","scopes":["openid"]}}}}},
"log":{"level":"debug"}}`,
tmpCredsFile.Name()),
tmpCredsFile.Name(),
)
err = os.WriteFile(tmpfile.Name(), content, 0o0600)
@@ -2349,7 +2349,7 @@ func TestScrub(t *testing.T) {
defer os.Remove(tmpfile.Name()) // clean up
content := []byte(fmt.Sprintf(`{
content := fmt.Appendf([]byte{}, `{
"storage": {
"rootDirectory": "%s"
},
@@ -2360,7 +2360,7 @@ func TestScrub(t *testing.T) {
"level": "debug"
}
}
`, dir, port))
`, dir, port)
_, err = tmpfile.Write(content)
So(err, ShouldBeNil)
err = tmpfile.Close()
@@ -2381,7 +2381,7 @@ func TestScrub(t *testing.T) {
defer os.Remove(tmpfile.Name()) // clean up
content := []byte(fmt.Sprintf(`{
content := fmt.Appendf([]byte{}, `{
"storage": {
"rootDirectory": ""
},
@@ -2392,7 +2392,7 @@ func TestScrub(t *testing.T) {
"level": "debug"
}
}
`, port))
`, port)
_, err = tmpfile.Write(content)
So(err, ShouldBeNil)
err = tmpfile.Close()
@@ -2438,7 +2438,7 @@ func TestScrub(t *testing.T) {
defer os.Remove(tmpfile.Name()) // clean up
content := []byte(fmt.Sprintf(`{
content := fmt.Appendf([]byte{}, `{
"storage": {
"rootDirectory": "%s"
},
@@ -2449,7 +2449,7 @@ func TestScrub(t *testing.T) {
"level": "debug"
}
}
`, dir, port))
`, dir, port)
_, err = tmpfile.Write(content)
So(err, ShouldBeNil)
err = tmpfile.Close()
@@ -2548,7 +2548,7 @@ func TestClusterConfig(t *testing.T) {
baseExamplePath := "../../../examples/scale-out-cluster-cloud/"
Convey("Should successfully load example configs for cloud", t, func() {
for memberIdx := 0; memberIdx < 3; memberIdx++ {
for memberIdx := range 3 {
cfgFileToLoad := fmt.Sprintf("%s/config-cluster-member%d.json", baseExamplePath, memberIdx)
cfg := config.New()
err := cli.LoadConfiguration(cfg, cfgFileToLoad)
@@ -2557,7 +2557,7 @@ func TestClusterConfig(t *testing.T) {
})
Convey("Should successfully load example TLS configs for cloud", t, func() {
for memberIdx := 0; memberIdx < 3; memberIdx++ {
for memberIdx := range 3 {
cfgFileToLoad := fmt.Sprintf("%s/tls/config-cluster-member%d.json", baseExamplePath, memberIdx)
cfg := config.New()
err := cli.LoadConfiguration(cfg, cfgFileToLoad)
@@ -2719,6 +2719,7 @@ func runCLIWithConfig(tempDir string, config string) (string, error) {
// Run CLI in a goroutine, but handle errors via a channel
errCh := make(chan error, 1)
go func() {
errCh <- cli.NewServerRootCmd().Execute()
}()
+3 -1
View File
@@ -1,5 +1,4 @@
//go:build stress
// +build stress
package server_test
@@ -64,6 +63,7 @@ func TestStressTooManyOpenFiles(t *testing.T) {
defer func() {
// list the content of the directory (useful in case of test fail)
//nolint: noctx // old code, no context available
out, err := exec.Command("du", "-ab", dir).Output()
if err != nil {
t.Logf("error when listing storage files:\n%s\n", err)
@@ -108,6 +108,7 @@ func TestStressTooManyOpenFiles(t *testing.T) {
"docker://public.ecr.aws/zomato/alpine:3.11.3", fmt.Sprintf("oci:%s:alpine", dir),
}
//nolint: noctx // old code, no context available
out, err := exec.Command("skopeo", skopeoArgs...).Output()
if err != nil {
t.Logf("\nerror on skopeo copy:\n%s\n", err)
@@ -178,6 +179,7 @@ func worker(id int, zotPort, rootDir string) {
sourceImg, destImg,
}
//nolint: noctx // old code, no context available
err := exec.Command("skopeo", skopeoArgs...).Run()
if err != nil { //nolint: wsl
continue // we expect clients to receive errors due to FD limit reached on server
-1
View File
@@ -1,5 +1,4 @@
//go:build !sync
// +build !sync
package server
-1
View File
@@ -1,5 +1,4 @@
//go:build sync
// +build sync
package server
+5 -2
View File
@@ -113,6 +113,7 @@ func newVerifyFeatureRetentionCmd(conf *config.Config) *cobra.Command {
// Initialize MetaDB only if retention policies are configured
var metaDB mTypes.MetaDB
if conf.IsRetentionEnabled() {
// Enable retention dry-run mode only when retention is enabled
conf.Storage.Retention.DryRun = true
@@ -164,8 +165,10 @@ func newVerifyFeatureRetentionCmd(conf *config.Config) *cobra.Command {
return fmt.Errorf("failed to get timeout flag: %w", err)
}
var waitCtx context.Context
var cancel context.CancelFunc
var (
waitCtx context.Context
cancel context.CancelFunc
)
if timeout > 0 {
logger.Info().Dur("timeout", timeout).Msg("waiting for garbage collection tasks to complete...")
+24 -24
View File
@@ -80,7 +80,7 @@ func TestRetentionCheckNegative(t *testing.T) {
logFile := path.Join(testDir, "retention-check.log")
port := GetFreePort()
content := []byte(fmt.Sprintf(`{
content := fmt.Appendf([]byte{}, `{
"distSpecVersion": "1.1.1",
"storage": {
"rootDirectory": "%s",
@@ -90,7 +90,7 @@ func TestRetentionCheckNegative(t *testing.T) {
"address": "127.0.0.1",
"port": "%s"
}
}`, testDir, port))
}`, testDir, port)
err := os.WriteFile(configFile, content, 0o600)
So(err, ShouldBeNil)
@@ -128,7 +128,7 @@ func TestRetentionCheckNegative(t *testing.T) {
defer ctrlManager.StopServer()
content := []byte(fmt.Sprintf(`{
content := fmt.Appendf([]byte{}, `{
"storage": {
"rootDirectory": "%s",
"gc": true,
@@ -154,7 +154,7 @@ func TestRetentionCheckNegative(t *testing.T) {
"level": "debug"
}
}
`, storageDir, port))
`, storageDir, port)
err := os.WriteFile(configFile, content, 0o600)
So(err, ShouldBeNil)
@@ -187,7 +187,7 @@ func TestRetentionCheckNegative(t *testing.T) {
configFile := path.Join(testDir, "zot-config.json")
port := GetFreePort()
content := []byte(fmt.Sprintf(`{
content := fmt.Appendf([]byte{}, `{
"distSpecVersion": "1.1.1",
"storage": {
"rootDirectory": "%s",
@@ -197,7 +197,7 @@ func TestRetentionCheckNegative(t *testing.T) {
"address": "127.0.0.1",
"port": "%s"
}
}`, testDir, port))
}`, testDir, port)
err := os.WriteFile(configFile, content, 0o600)
So(err, ShouldBeNil)
@@ -227,7 +227,7 @@ func TestRetentionCheckNegative(t *testing.T) {
logFile := path.Join(testDir, "retention-check.log")
port := GetFreePort()
content := []byte(fmt.Sprintf(`{
content := fmt.Appendf([]byte{}, `{
"distSpecVersion": "1.1.1",
"storage": {
"rootDirectory": "%s",
@@ -237,7 +237,7 @@ func TestRetentionCheckNegative(t *testing.T) {
"address": "127.0.0.1",
"port": "%s"
}
}`, testDir, port))
}`, testDir, port)
err := os.WriteFile(configFile, content, 0o600)
So(err, ShouldBeNil)
@@ -275,7 +275,7 @@ func TestRetentionCheckWithRetentionEnabledAndRedisDriver(t *testing.T) {
configFile := path.Join(testDir, "zot-config.json")
logFile := path.Join(testDir, "retention-check.log")
content := []byte(fmt.Sprintf(`{
content := fmt.Appendf([]byte{}, `{
"distSpecVersion": "1.1.1",
"storage": {
"rootDirectory": "%s",
@@ -310,7 +310,7 @@ func TestRetentionCheckWithRetentionEnabledAndRedisDriver(t *testing.T) {
"level": "debug"
}
}
`, storageDir, testGCDelay, miniRedis.Addr(), port))
`, storageDir, testGCDelay, miniRedis.Addr(), port)
err := os.WriteFile(configFile, content, 0o600)
So(err, ShouldBeNil)
@@ -513,7 +513,7 @@ func TestRetentionCheckWithRetentionEnabled(t *testing.T) {
configFile := path.Join(testDir, "zot-config.json")
logFile := path.Join(testDir, "retention-check.log")
content := []byte(fmt.Sprintf(`{
content := fmt.Appendf([]byte{}, `{
"distSpecVersion": "1.1.1",
"storage": {
"rootDirectory": "%s",
@@ -543,7 +543,7 @@ func TestRetentionCheckWithRetentionEnabled(t *testing.T) {
"level": "debug"
}
}
`, storageDir, testGCDelay, port))
`, storageDir, testGCDelay, port)
err := os.WriteFile(configFile, content, 0o600)
So(err, ShouldBeNil)
@@ -804,7 +804,7 @@ func TestRetentionCheckWithDeleteReferrers(t *testing.T) {
configFile := path.Join(testDir, "zot-config.json")
logFile := path.Join(testDir, "retention-check.log")
content := []byte(fmt.Sprintf(`{
content := fmt.Appendf([]byte{}, `{
"distSpecVersion": "1.1.1",
"storage": {
"rootDirectory": "%s",
@@ -835,7 +835,7 @@ func TestRetentionCheckWithDeleteReferrers(t *testing.T) {
"level": "debug"
}
}
`, storageDir, testGCDelay, port))
`, storageDir, testGCDelay, port)
err := os.WriteFile(configFile, content, 0o600)
So(err, ShouldBeNil)
@@ -985,7 +985,7 @@ func TestRetentionCheckWithRetentionDisabled(t *testing.T) {
configFile := path.Join(testDir, "zot-config.json")
logFile := path.Join(testDir, "retention-check.log")
content := []byte(fmt.Sprintf(`{
content := fmt.Appendf([]byte{}, `{
"distSpecVersion": "1.1.1",
"storage": {
"rootDirectory": "%s",
@@ -1001,7 +1001,7 @@ func TestRetentionCheckWithRetentionDisabled(t *testing.T) {
"level": "debug"
}
}
`, storageDir, testGCDelay, port))
`, storageDir, testGCDelay, port)
err := os.WriteFile(configFile, content, 0o600)
So(err, ShouldBeNil)
@@ -1141,7 +1141,7 @@ func TestRetentionCheckWithSubpaths(t *testing.T) {
configFile := path.Join(testDir, "zot-config.json")
logFile := path.Join(testDir, "retention-check.log")
content := []byte(fmt.Sprintf(`{
content := fmt.Appendf([]byte{}, `{
"distSpecVersion": "1.1.1",
"storage": {
"rootDirectory": "%s",
@@ -1195,7 +1195,7 @@ func TestRetentionCheckWithSubpaths(t *testing.T) {
"level": "debug"
}
}
`, storageDir, testGCDelay, storageDir, testGCDelay, port))
`, storageDir, testGCDelay, storageDir, testGCDelay, port)
err := os.WriteFile(configFile, content, 0o600)
So(err, ShouldBeNil)
@@ -1442,7 +1442,7 @@ func TestRetentionCheckWithGCIntervalOverride(t *testing.T) {
logFile := path.Join(testDir, "retention-check.log")
port := GetFreePort()
content := []byte(fmt.Sprintf(`{
content := fmt.Appendf([]byte{}, `{
"distSpecVersion": "1.1.1",
"storage": {
"rootDirectory": "%s",
@@ -1466,7 +1466,7 @@ func TestRetentionCheckWithGCIntervalOverride(t *testing.T) {
"level": "debug"
}
}
`, storageDir, testGCDelay, storageDir, testGCDelay, port))
`, storageDir, testGCDelay, storageDir, testGCDelay, port)
err := os.WriteFile(configFile, content, 0o600)
So(err, ShouldBeNil)
@@ -1506,10 +1506,10 @@ func TestRetentionCheckWithGCIntervalOverride(t *testing.T) {
//nolint:tagliatelle // JSON field names match Go struct names
type ConfigParams struct {
Storage struct {
GCInterval int64 `json:"GCInterval"`
GCDelay int64 `json:"GCDelay"`
GCMaxSchedulerDelay int64 `json:"GCMaxSchedulerDelay"`
SubPaths map[string]interface{} `json:"SubPaths"`
GCInterval int64 `json:"GCInterval"`
GCDelay int64 `json:"GCDelay"`
GCMaxSchedulerDelay int64 `json:"GCMaxSchedulerDelay"`
SubPaths map[string]any `json:"SubPaths"`
} `json:"Storage"`
}