From e0348f1383303a6c6379ddb2260b25defc999e87 Mon Sep 17 00:00:00 2001 From: Adam Hughes Date: Fri, 27 Aug 2021 17:21:48 +0000 Subject: [PATCH] ci: enable staticcheck linter --- .golangci.yml | 2 +- cmd/internal/cli/singularity_test.go | 4 ++-- e2e/instance/instance_utils.go | 4 ++-- internal/app/singularity/capability_manage_linux.go | 5 +++-- internal/app/singularity/remote_add.go | 7 +++---- internal/app/singularity/remote_add_keyserver.go | 4 +++- internal/app/singularity/remote_login.go | 9 ++++----- internal/app/singularity/remote_logout.go | 9 ++++----- internal/app/singularity/remote_remove.go | 7 +++---- internal/app/singularity/remote_remove_keyserver.go | 4 +++- internal/app/singularity/remote_status.go | 6 ++---- internal/app/singularity/remote_use.go | 7 +++---- internal/app/starter/master_linux_test.go | 10 +--------- internal/pkg/fakeroot/fakeroot.go | 5 +++-- .../pkg/runtime/engine/singularity/container_linux.go | 5 ++--- internal/pkg/util/fs/layout/manager.go | 2 +- internal/pkg/util/fs/overlay/overlay_linux.go | 8 ++++---- internal/pkg/util/fs/overlay/overlay_linux_test.go | 2 -- internal/pkg/util/interactive/interactive.go | 5 +++-- internal/pkg/util/interactive/interactive_test.go | 5 +++-- pkg/image/squashfs_test.go | 5 +++-- 21 files changed, 53 insertions(+), 62 deletions(-) diff --git a/.golangci.yml b/.golangci.yml index 1cbf65863b..2552181fb7 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -24,9 +24,9 @@ linters: - misspell - nakedret - revive + - staticcheck # we would like to add these # - dupl - # - staticcheck linters-settings: misspell: diff --git a/cmd/internal/cli/singularity_test.go b/cmd/internal/cli/singularity_test.go index 8feef56cc6..2220e08d16 100644 --- a/cmd/internal/cli/singularity_test.go +++ b/cmd/internal/cli/singularity_test.go @@ -1,4 +1,4 @@ -// Copyright (c) 2018-2019, Sylabs Inc. All rights reserved. +// Copyright (c) 2018-2021, Sylabs Inc. All rights reserved. // This software is licensed under a 3-clause BSD license. Please consult the // LICENSE.md file distributed with the sources of this project regarding your // rights to use or distribute this software. @@ -29,7 +29,7 @@ func TestCreateConfDir(t *testing.T) { t.Errorf("failed to create directory %s", dir) } else { // stick something in the directory and make sure it isn't deleted - ioutil.WriteFile(dir+"/foo", []byte(""), 655) + ioutil.WriteFile(dir+"/foo", []byte(""), 0o655) handleConfDir(dir) if _, err := os.Stat(dir + "/foo"); os.IsNotExist(err) { t.Errorf("inadvertently overwrote existing directory %s", dir) diff --git a/e2e/instance/instance_utils.go b/e2e/instance/instance_utils.go index c2270e9ff7..ab84d6fa01 100644 --- a/e2e/instance/instance_utils.go +++ b/e2e/instance/instance_utils.go @@ -1,4 +1,4 @@ -// Copyright (c) 2019, Sylabs Inc. All rights reserved. +// Copyright (c) 2019-2021, Sylabs Inc. All rights reserved. // This software is licensed under a 3-clause BSD license. Please consult the // LICENSE.md file distributed with the sources of this project regarding your // rights to use or distribute this software. @@ -126,7 +126,7 @@ func echo(t *testing.T, port int) { return } - fmt.Fprintf(sock, message) + fmt.Fprint(sock, message) response, responseErr := bufio.NewReader(sock).ReadString('\n') if responseErr != nil || response != message { diff --git a/internal/app/singularity/capability_manage_linux.go b/internal/app/singularity/capability_manage_linux.go index 302d65166b..63886c585e 100644 --- a/internal/app/singularity/capability_manage_linux.go +++ b/internal/app/singularity/capability_manage_linux.go @@ -1,4 +1,4 @@ -// Copyright (c) 2018-2020, Sylabs Inc. All rights reserved. +// Copyright (c) 2018-2021, Sylabs Inc. All rights reserved. // This software is licensed under a 3-clause BSD license. Please consult the // LICENSE.md file distributed with the sources of this project regarding your // rights to use or distribute this software. @@ -7,6 +7,7 @@ package singularity import ( "fmt" + "io" "os" "syscall" @@ -107,7 +108,7 @@ func manageCaps(capFile string, c CapManageConfig, t manageType) error { return fmt.Errorf("while truncating capability config file: %s", err) } - if n, err := file.Seek(0, os.SEEK_SET); err != nil || n != 0 { + if n, err := file.Seek(0, io.SeekStart); err != nil || n != 0 { return fmt.Errorf("failed to reset %s cursor: %s", file.Name(), err) } diff --git a/internal/app/singularity/remote_add.go b/internal/app/singularity/remote_add.go index 2737767c5a..c015db6ba3 100644 --- a/internal/app/singularity/remote_add.go +++ b/internal/app/singularity/remote_add.go @@ -7,6 +7,7 @@ package singularity import ( "fmt" + "io" "net/url" "os" "path" @@ -26,8 +27,6 @@ func RemoteAdd(configFile, name, uri string, global, insecure bool) (err error) return fmt.Errorf("invalid URI: cannot have empty URI") } - c := &remote.Config{} - // system config should be world readable perm := os.FileMode(0o600) if global { @@ -42,7 +41,7 @@ func RemoteAdd(configFile, name, uri string, global, insecure bool) (err error) defer file.Close() // read file contents to config struct - c, err = remote.ReadFrom(file) + c, err := remote.ReadFrom(file) if err != nil { return fmt.Errorf("while parsing remote config data: %s", err) } @@ -62,7 +61,7 @@ func RemoteAdd(configFile, name, uri string, global, insecure bool) (err error) return fmt.Errorf("while truncating remote config file: %s", err) } - if n, err := file.Seek(0, os.SEEK_SET); err != nil || n != 0 { + if n, err := file.Seek(0, io.SeekStart); err != nil || n != 0 { return fmt.Errorf("failed to reset %s cursor: %s", file.Name(), err) } diff --git a/internal/app/singularity/remote_add_keyserver.go b/internal/app/singularity/remote_add_keyserver.go index 03d0ec8497..9755bef4c4 100644 --- a/internal/app/singularity/remote_add_keyserver.go +++ b/internal/app/singularity/remote_add_keyserver.go @@ -1,3 +1,4 @@ +// Copyright (c) 2021, Sylabs Inc. All rights reserved. // Copyright (c) 2020, Control Command Inc. All rights reserved. // This software is licensed under a 3-clause BSD license. Please consult the // LICENSE.md file distributed with the sources of this project regarding your @@ -7,6 +8,7 @@ package singularity import ( "fmt" + "io" "os" "strings" @@ -56,7 +58,7 @@ func RemoteAddKeyserver(name, uri string, order uint32, insecure bool) error { return fmt.Errorf("while truncating remote config file: %s", err) } - if n, err := file.Seek(0, os.SEEK_SET); err != nil || n != 0 { + if n, err := file.Seek(0, io.SeekStart); err != nil || n != 0 { return fmt.Errorf("failed to reset %s cursor: %s", file.Name(), err) } diff --git a/internal/app/singularity/remote_login.go b/internal/app/singularity/remote_login.go index 9e008c103a..b1e60436d8 100644 --- a/internal/app/singularity/remote_login.go +++ b/internal/app/singularity/remote_login.go @@ -1,5 +1,5 @@ // Copyright (c) 2020, Control Command Inc. All rights reserved. -// Copyright (c) 2019-2020, Sylabs Inc. All rights reserved. +// Copyright (c) 2019-2021, Sylabs Inc. All rights reserved. // This software is licensed under a 3-clause BSD license. Please consult the // LICENSE.md file distributed with the sources of this project regarding your // rights to use or distribute this software. @@ -9,6 +9,7 @@ package singularity import ( "errors" "fmt" + "io" "os" "github.com/sylabs/singularity/internal/pkg/remote" @@ -33,8 +34,6 @@ var ErrLoginAborted = errors.New("user aborted login") // If the supplied remote name is an empty string, it will attempt // to use the default remote. func RemoteLogin(usrConfigFile string, args *LoginArgs) (err error) { - c := &remote.Config{} - // opening config file file, err := os.OpenFile(usrConfigFile, os.O_RDWR|os.O_CREATE, 0o600) if err != nil { @@ -43,7 +42,7 @@ func RemoteLogin(usrConfigFile string, args *LoginArgs) (err error) { defer file.Close() // read file contents to config struct - c, err = remote.ReadFrom(file) + c, err := remote.ReadFrom(file) if err != nil { return fmt.Errorf("while parsing remote config data: %s", err) } @@ -83,7 +82,7 @@ func RemoteLogin(usrConfigFile string, args *LoginArgs) (err error) { return fmt.Errorf("while truncating remote config file: %s", err) } - if n, err := file.Seek(0, os.SEEK_SET); err != nil || n != 0 { + if n, err := file.Seek(0, io.SeekStart); err != nil || n != 0 { return fmt.Errorf("failed to reset %s cursor: %s", file.Name(), err) } diff --git a/internal/app/singularity/remote_logout.go b/internal/app/singularity/remote_logout.go index 4de4321940..5e8f277d0f 100644 --- a/internal/app/singularity/remote_logout.go +++ b/internal/app/singularity/remote_logout.go @@ -1,5 +1,5 @@ // Copyright (c) 2020, Control Command Inc. All rights reserved. -// Copyright (c) 2019-2020, Sylabs Inc. All rights reserved. +// Copyright (c) 2019-2021, Sylabs Inc. All rights reserved. // This software is licensed under a 3-clause BSD license. Please consult the // LICENSE.md file distributed with the sources of this project regarding your // rights to use or distribute this software. @@ -8,6 +8,7 @@ package singularity import ( "fmt" + "io" "os" "github.com/sylabs/singularity/internal/pkg/remote" @@ -16,8 +17,6 @@ import ( // RemoteLogout logs out from an endpoint or service. func RemoteLogout(usrConfigFile, name string) (err error) { - c := &remote.Config{} - // opening config file file, err := os.OpenFile(usrConfigFile, os.O_RDWR|os.O_CREATE, 0o600) if err != nil { @@ -26,7 +25,7 @@ func RemoteLogout(usrConfigFile, name string) (err error) { defer file.Close() // read file contents to config struct - c, err = remote.ReadFrom(file) + c, err := remote.ReadFrom(file) if err != nil { return fmt.Errorf("while parsing remote config data: %s", err) } @@ -57,7 +56,7 @@ func RemoteLogout(usrConfigFile, name string) (err error) { return fmt.Errorf("while truncating remote config file: %s", err) } - if n, err := file.Seek(0, os.SEEK_SET); err != nil || n != 0 { + if n, err := file.Seek(0, io.SeekStart); err != nil || n != 0 { return fmt.Errorf("failed to reset %s cursor: %s", file.Name(), err) } diff --git a/internal/app/singularity/remote_remove.go b/internal/app/singularity/remote_remove.go index 48afaf7603..e3e0e0e07f 100644 --- a/internal/app/singularity/remote_remove.go +++ b/internal/app/singularity/remote_remove.go @@ -7,6 +7,7 @@ package singularity import ( "fmt" + "io" "os" "github.com/sylabs/singularity/internal/pkg/remote" @@ -14,8 +15,6 @@ import ( // RemoteRemove deletes a remote endpoint from the configuration func RemoteRemove(configFile, name string) (err error) { - c := &remote.Config{} - // opening config file file, err := os.OpenFile(configFile, os.O_RDWR|os.O_CREATE, 0o600) if err != nil { @@ -24,7 +23,7 @@ func RemoteRemove(configFile, name string) (err error) { defer file.Close() // read file contents to config struct - c, err = remote.ReadFrom(file) + c, err := remote.ReadFrom(file) if err != nil { return fmt.Errorf("while parsing remote config data: %s", err) } @@ -38,7 +37,7 @@ func RemoteRemove(configFile, name string) (err error) { return fmt.Errorf("while truncating remote config file: %s", err) } - if n, err := file.Seek(0, os.SEEK_SET); err != nil || n != 0 { + if n, err := file.Seek(0, io.SeekStart); err != nil || n != 0 { return fmt.Errorf("failed to reset %s cursor: %s", file.Name(), err) } diff --git a/internal/app/singularity/remote_remove_keyserver.go b/internal/app/singularity/remote_remove_keyserver.go index 0ae003a1a0..b66e7d3575 100644 --- a/internal/app/singularity/remote_remove_keyserver.go +++ b/internal/app/singularity/remote_remove_keyserver.go @@ -1,3 +1,4 @@ +// Copyright (c) 2021, Sylabs Inc. All rights reserved. // Copyright (c) 2020, Control Command Inc. All rights reserved. // This software is licensed under a 3-clause BSD license. Please consult the // LICENSE.md file distributed with the sources of this project regarding your @@ -7,6 +8,7 @@ package singularity import ( "fmt" + "io" "os" "strings" @@ -56,7 +58,7 @@ func RemoteRemoveKeyserver(name, uri string) error { return fmt.Errorf("while truncating remote config file: %s", err) } - if n, err := file.Seek(0, os.SEEK_SET); err != nil || n != 0 { + if n, err := file.Seek(0, io.SeekStart); err != nil || n != 0 { return fmt.Errorf("failed to reset %s cursor: %s", file.Name(), err) } diff --git a/internal/app/singularity/remote_status.go b/internal/app/singularity/remote_status.go index b2deaf2337..5f3f9ad806 100644 --- a/internal/app/singularity/remote_status.go +++ b/internal/app/singularity/remote_status.go @@ -1,5 +1,5 @@ // Copyright (c) 2020, Control Command Inc. All rights reserved. -// Copyright (c) 2019, Sylabs Inc. All rights reserved. +// Copyright (c) 2019-2021, Sylabs Inc. All rights reserved. // This software is licensed under a 3-clause BSD license. Please consult the // LICENSE.md file distributed with the sources of this project regarding your // rights to use or distribute this software. @@ -31,8 +31,6 @@ type status struct { // If the supplied remote name is an empty string, it will attempt // to use the default remote. func RemoteStatus(usrConfigFile, name string) (err error) { - c := &remote.Config{} - if name != "" { sylog.Infof("Checking status of remote: %s", name) } else { @@ -50,7 +48,7 @@ func RemoteStatus(usrConfigFile, name string) (err error) { defer file.Close() // read file contents to config struct - c, err = remote.ReadFrom(file) + c, err := remote.ReadFrom(file) if err != nil { return fmt.Errorf("while parsing remote config data: %s", err) } diff --git a/internal/app/singularity/remote_use.go b/internal/app/singularity/remote_use.go index 50c8e318b4..85bb6490ee 100644 --- a/internal/app/singularity/remote_use.go +++ b/internal/app/singularity/remote_use.go @@ -8,6 +8,7 @@ package singularity import ( "fmt" + "io" "os" "github.com/sylabs/singularity/internal/pkg/remote" @@ -35,8 +36,6 @@ func syncSysConfig(cUsr *remote.Config) error { // RemoteUse sets remote to use func RemoteUse(usrConfigFile, name string, global, exclusive bool) (err error) { - c := &remote.Config{} - if exclusive { if os.Getuid() != 0 { return fmt.Errorf("unable to set endpoint as exclusive: not root user") @@ -59,7 +58,7 @@ func RemoteUse(usrConfigFile, name string, global, exclusive bool) (err error) { defer file.Close() // read file contents to config struct - c, err = remote.ReadFrom(file) + c, err := remote.ReadFrom(file) if err != nil { return fmt.Errorf("while parsing remote config data: %s", err) } @@ -79,7 +78,7 @@ func RemoteUse(usrConfigFile, name string, global, exclusive bool) (err error) { return fmt.Errorf("while truncating remote config file: %s", err) } - if n, err := file.Seek(0, os.SEEK_SET); err != nil || n != 0 { + if n, err := file.Seek(0, io.SeekStart); err != nil || n != 0 { return fmt.Errorf("failed to reset %s cursor: %s", file.Name(), err) } diff --git a/internal/app/starter/master_linux_test.go b/internal/app/starter/master_linux_test.go index cea980fffe..a44c932ea5 100644 --- a/internal/app/starter/master_linux_test.go +++ b/internal/app/starter/master_linux_test.go @@ -1,4 +1,4 @@ -// Copyright (c) 2018-2019, Sylabs Inc. All rights reserved. +// Copyright (c) 2018-2021, Sylabs Inc. All rights reserved. // This software is licensed under a 3-clause BSD license. Please consult the // LICENSE.md file distributed with the sources of this project regarding your // rights to use or distribute this software. @@ -62,10 +62,6 @@ func TestCreateContainer(t *testing.T) { t.Fatalf("test %s expected to succeed but failed: %s", tt.name, fatal) } else if !tt.shallPass && fatal == nil { t.Fatalf("test %s expected to fail but succeeded", tt.name) - } else if tt.shallPass && fatal == nil { - // test succeed - } else if !tt.shallPass && fatal != nil { - // test succeed } }) } @@ -109,10 +105,6 @@ func TestStartContainer(t *testing.T) { t.Fatalf("test %s expected to succeed but failed: %s", tt.name, fatal) } else if !tt.shallPass && fatal == nil { t.Fatalf("test %s expected to fail but succeeded", tt.name) - } else if tt.shallPass && fatal == nil { - // test succeed - } else if !tt.shallPass && fatal != nil { - // test succeed } }) } diff --git a/internal/pkg/fakeroot/fakeroot.go b/internal/pkg/fakeroot/fakeroot.go index 5a6ae05a9a..53bc3b37a2 100644 --- a/internal/pkg/fakeroot/fakeroot.go +++ b/internal/pkg/fakeroot/fakeroot.go @@ -1,4 +1,4 @@ -// Copyright (c) 2019, Sylabs Inc. All rights reserved. +// Copyright (c) 2019-2021, Sylabs Inc. All rights reserved. // This software is licensed under a 3-clause BSD license. Please consult the // LICENSE.md file distributed with the sources of this project regarding your // rights to use or distribute this software. @@ -9,6 +9,7 @@ import ( "bufio" "bytes" "fmt" + "io" "os" "strconv" "strings" @@ -183,7 +184,7 @@ func (c *Config) Close() error { if err := c.file.Truncate(0); err != nil { return fmt.Errorf("error while truncating %s to 0: %s", filename, err) } - if _, err := c.file.Seek(0, os.SEEK_SET); err != nil { + if _, err := c.file.Seek(0, io.SeekStart); err != nil { return fmt.Errorf("error while resetting file offset: %s", err) } if _, err := c.file.Write(buf.Bytes()); err != nil { diff --git a/internal/pkg/runtime/engine/singularity/container_linux.go b/internal/pkg/runtime/engine/singularity/container_linux.go index 7b3544363a..c4337831c7 100644 --- a/internal/pkg/runtime/engine/singularity/container_linux.go +++ b/internal/pkg/runtime/engine/singularity/container_linux.go @@ -2428,13 +2428,12 @@ func (c *container) openFuseFdFromRPC() (int, int, error) { fuseFd := -1 - for _, msg := range msgs { - fds, err := unix.ParseUnixRights(&msg) + if len(msgs) > 0 { + fds, err := unix.ParseUnixRights(&msgs[0]) if err != nil { return -1, -1, fmt.Errorf("while getting file descriptor: %s", err) } fuseFd = fds[0] - break } return fuseFd, fuseRPCFd, nil diff --git a/internal/pkg/util/fs/layout/manager.go b/internal/pkg/util/fs/layout/manager.go index b1da1dd19d..7f9e765a0e 100644 --- a/internal/pkg/util/fs/layout/manager.go +++ b/internal/pkg/util/fs/layout/manager.go @@ -18,7 +18,7 @@ import ( const ( dirMode os.FileMode = 0o755 - fileMode = 0o644 + fileMode os.FileMode = 0o644 ) type file struct { diff --git a/internal/pkg/util/fs/overlay/overlay_linux.go b/internal/pkg/util/fs/overlay/overlay_linux.go index b31385a63d..2fc4fc4d92 100644 --- a/internal/pkg/util/fs/overlay/overlay_linux.go +++ b/internal/pkg/util/fs/overlay/overlay_linux.go @@ -30,10 +30,10 @@ type fs struct { const ( nfs int64 = 0x6969 - fuse = 0x65735546 - ecrypt = 0xF15F - lustre = 0x0BD00BD0 //nolint:misspell - gpfs = 0x47504653 + fuse int64 = 0x65735546 + ecrypt int64 = 0xF15F + lustre int64 = 0x0BD00BD0 //nolint:misspell + gpfs int64 = 0x47504653 ) var incompatibleFs = map[int64]fs{ diff --git a/internal/pkg/util/fs/overlay/overlay_linux_test.go b/internal/pkg/util/fs/overlay/overlay_linux_test.go index 006ea750a7..6bf1d0ffa9 100644 --- a/internal/pkg/util/fs/overlay/overlay_linux_test.go +++ b/internal/pkg/util/fs/overlay/overlay_linux_test.go @@ -190,8 +190,6 @@ func TestCheckLowerUpper(t *testing.T) { } } t.Errorf("unexpected error for %q: %q instead of %q", tt.name, err, expectedError) - } else if err == nil && tt.expectedSuccess { - // test PASS without error } } } diff --git a/internal/pkg/util/interactive/interactive.go b/internal/pkg/util/interactive/interactive.go index d86ecc8c7b..2db3b7d54b 100644 --- a/internal/pkg/util/interactive/interactive.go +++ b/internal/pkg/util/interactive/interactive.go @@ -1,4 +1,4 @@ -// Copyright (c) 2019-2020, Sylabs Inc. All rights reserved. +// Copyright (c) 2019-2021, Sylabs Inc. All rights reserved. // This software is licensed under a 3-clause BSD license. Please consult the // LICENSE.md file distributed with the sources of this project regarding your // rights to use or distribute this software. @@ -10,6 +10,7 @@ import ( "bufio" "errors" "fmt" + "io" "os" "strconv" "strings" @@ -66,7 +67,7 @@ func askQuestionUsingGenericDescr(f *os.File) (string, error) { // Note that we do not check for errors since some cases (e.g., pipes) // will actually not allow to perform a Seek(). This is intended and // will not create a problem. - f.Seek(pos+int64(strLen), os.SEEK_SET) + f.Seek(pos+int64(strLen), io.SeekStart) return response, nil } diff --git a/internal/pkg/util/interactive/interactive_test.go b/internal/pkg/util/interactive/interactive_test.go index 7730737c44..62daa44ec4 100644 --- a/internal/pkg/util/interactive/interactive_test.go +++ b/internal/pkg/util/interactive/interactive_test.go @@ -1,4 +1,4 @@ -// Copyright (c) 2019, Sylabs Inc. All rights reserved. +// Copyright (c) 2019-2021, Sylabs Inc. All rights reserved. // This software is licensed under a 3-clause BSD license. Please consult the // LICENSE.md file distributed with the sources of this project regarding your // rights to use or distribute this software. @@ -6,6 +6,7 @@ package interactive import ( + "io" "io/ioutil" "os" "strings" @@ -33,7 +34,7 @@ func generateQuestionInput(t *testing.T, input string) (*os.File, *os.File) { } // Reposition to the beginning of file to ensure there is something to read - _, err = testFile.Seek(0, os.SEEK_SET) + _, err = testFile.Seek(0, io.SeekStart) if err != nil { testFile.Close() os.Remove(testFile.Name()) diff --git a/pkg/image/squashfs_test.go b/pkg/image/squashfs_test.go index d1a9b54059..c934304416 100644 --- a/pkg/image/squashfs_test.go +++ b/pkg/image/squashfs_test.go @@ -1,4 +1,4 @@ -// Copyright (c) 2019, Sylabs Inc. All rights reserved. +// Copyright (c) 2019-2021, Sylabs Inc. All rights reserved. // This software is licensed under a 3-clause BSD license. Please consult the // LICENSE.md file distributed with the sources of this project regarding your // rights to use or distribute this software. @@ -6,6 +6,7 @@ package image import ( + "io" "io/ioutil" "os" "os/exec" @@ -98,7 +99,7 @@ func TestSquashfsInitializer(t *testing.T) { t.Fatalf("unexpected success for squashfs initializer\n") } // reset cursor for header parsing - img.File.Seek(0, os.SEEK_SET) + img.File.Seek(0, io.SeekStart) // initialized must succeed if writable is false img.Writable = false err = squashfsfmt.initializer(img, fileinfo)