Skip to content

Commit

Permalink
fix a number of errcheck issues
Browse files Browse the repository at this point in the history
Numerous issues remain, especially in tests/e2e.

Signed-off-by: Valentin Rothberg <[email protected]>
  • Loading branch information
vrothberg committed Mar 22, 2022
1 parent 6c030cd commit 06dd913
Show file tree
Hide file tree
Showing 32 changed files with 77 additions and 59 deletions.
4 changes: 3 additions & 1 deletion cmd/podman/images/scp.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,9 @@ func saveToRemote(image, localFile string, tag string, uri *urlP.URL, iden strin
return err
}
n, err := scpD.CopyFrom(dial, remoteFile, localFile)
connection.ExecRemoteCommand(dial, "rm "+remoteFile)
if _, conErr := connection.ExecRemoteCommand(dial, "rm "+remoteFile); conErr != nil {
logrus.Errorf("Error removing file on endpoint: %v", conErr)
}
if err != nil {
errOut := strconv.Itoa(int(n)) + " Bytes copied before error"
return errors.Wrapf(err, errOut)
Expand Down
2 changes: 1 addition & 1 deletion cmd/podman/machine/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func init() {
"reexec", false,
"process was rexeced",
)
flags.MarkHidden("reexec")
_ = flags.MarkHidden("reexec")

ImagePathFlagName := "image-path"
flags.StringVar(&initOpts.ImagePath, ImagePathFlagName, cfg.Machine.Image, "Path to qcow image")
Expand Down
2 changes: 1 addition & 1 deletion cmd/podman/networks/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func networkCreateFlags(cmd *cobra.Command) {
macvlanFlagName := "macvlan"
flags.StringVar(&networkCreateOptions.MacVLAN, macvlanFlagName, "", "create a Macvlan connection based on this device")
// This option is deprecated
flags.MarkHidden(macvlanFlagName)
_ = flags.MarkHidden(macvlanFlagName)

labelFlagName := "label"
flags.StringArrayVar(&labels, labelFlagName, nil, "set metadata on a network")
Expand Down
2 changes: 1 addition & 1 deletion cmd/podman/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,7 @@ func rootFlags(cmd *cobra.Command, opts *entities.PodmanConfig) {
networkBackendFlagName := "network-backend"
pFlags.StringVar(&cfg.Network.NetworkBackend, networkBackendFlagName, cfg.Network.NetworkBackend, `Network backend to use ("cni"|"netavark")`)
_ = cmd.RegisterFlagCompletionFunc(networkBackendFlagName, common.AutocompleteNetworkBackend)
pFlags.MarkHidden(networkBackendFlagName)
_ = pFlags.MarkHidden(networkBackendFlagName)

rootFlagName := "root"
pFlags.StringVar(&cfg.Engine.StaticDir, rootFlagName, "", "Path to the root directory in which data, including images, is stored")
Expand Down
2 changes: 1 addition & 1 deletion cmd/podman/system/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func init() {

flags.StringVarP(&srvArgs.PProfAddr, "pprof-address", "", "",
"Binding network address for pprof profile endpoints, default: do not expose endpoints")
flags.MarkHidden("pprof-address")
_ = flags.MarkHidden("pprof-address")
}

func aliasTimeoutFlag(_ *pflag.FlagSet, name string) pflag.NormalizedName {
Expand Down
2 changes: 1 addition & 1 deletion cmd/podman/validate/args.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func SubCommandExists(cmd *cobra.Command, args []string) error {
}
return errors.Errorf("unrecognized command `%[1]s %[2]s`\n\nDid you mean this?\n\t%[3]s\n\nTry '%[1]s --help' for more information.", cmd.CommandPath(), args[0], strings.Join(suggestions, "\n\t"))
}
cmd.Help()
cmd.Help() // nolint: errcheck
return errors.Errorf("missing command '%[1]s COMMAND'", cmd.CommandPath())
}

Expand Down
4 changes: 2 additions & 2 deletions cmd/rootlessport/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,9 +253,9 @@ func serve(listener net.Listener, pm rkport.Manager) {
ctx := context.TODO()
err = handler(ctx, conn, pm)
if err != nil {
conn.Write([]byte(err.Error()))
_, _ = conn.Write([]byte(err.Error()))
} else {
conn.Write([]byte("OK"))
_, _ = conn.Write([]byte("OK"))
}
conn.Close()
}
Expand Down
3 changes: 1 addition & 2 deletions libpod/boltdb_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -366,8 +366,7 @@ func (s *BoltState) GetDBConfig() (*DBConfig, error) {
err = db.View(func(tx *bolt.Tx) error {
configBucket, err := getRuntimeConfigBucket(tx)
if err != nil {
// FIXME: this error should probably be returned
return nil // nolint: nilerr
return err
}

// Some of these may be nil
Expand Down
6 changes: 5 additions & 1 deletion libpod/container_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -921,7 +921,11 @@ func (c *Container) Stat(ctx context.Context, containerPath string) (*define.Fil
if err != nil {
return nil, err
}
defer c.unmount(false)
defer func() {
if err := c.unmount(false); err != nil {
logrus.Errorf("Unmounting container %s: %v", c.ID(), err)
}
}()
}

info, _, _, err := c.stat(ctx, mountPoint, containerPath)
Expand Down
1 change: 0 additions & 1 deletion libpod/container_internal_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -2740,7 +2740,6 @@ func (c *Container) generateUserPasswdEntry(addedUID int) (string, int, int, err
// If a non numeric User, then don't generate passwd
uid, err := strconv.ParseUint(userspec, 10, 32)
if err != nil {
// FIXME: this error should probably be returned
return "", 0, 0, nil // nolint: nilerr
}

Expand Down
5 changes: 4 additions & 1 deletion libpod/events/logfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/containers/podman/v4/pkg/util"
"github.com/containers/storage/pkg/lockfile"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)

// EventLogFile is the structure for event writing to a logfile. It contains the eventer
Expand Down Expand Up @@ -59,7 +60,9 @@ func (e EventLogFile) Read(ctx context.Context, options ReadOptions) error {
}
go func() {
time.Sleep(time.Until(untilTime))
t.Stop()
if err := t.Stop(); err != nil {
logrus.Errorf("Stopping logger: %v", err)
}
}()
}
funcDone := make(chan bool)
Expand Down
3 changes: 2 additions & 1 deletion pkg/bindings/test/attach_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ var _ = Describe("Podman containers attach", func() {
timeout := uint(5)
err := containers.Stop(bt.conn, id, new(containers.StopOptions).WithTimeout(timeout))
if err != nil {
GinkgoWriter.Write([]byte(err.Error()))
_, writeErr := GinkgoWriter.Write([]byte(err.Error()))
Expect(writeErr).ShouldNot(HaveOccurred())
}
}()

Expand Down
3 changes: 2 additions & 1 deletion pkg/bindings/test/auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ var _ = Describe("Podman images", func() {
AfterEach(func() {
s.Kill()
bt.cleanup()
registry.Stop()
err := registry.Stop()
Expect(err).To(BeNil())
})

// Test using credentials.
Expand Down
3 changes: 2 additions & 1 deletion pkg/bindings/test/containers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,8 @@ var _ = Describe("Podman containers ", func() {

// a container that has no healthcheck should be a 409
var name = "top"
bt.RunTopContainer(&name, nil)
_, err = bt.RunTopContainer(&name, nil)
Expect(err).To(BeNil())
_, err = containers.RunHealthCheck(bt.conn, name, nil)
Expect(err).ToNot(BeNil())
code, _ = bindings.CheckResponseCode(err)
Expand Down
2 changes: 1 addition & 1 deletion pkg/checkpoint/crutils/checkpoint_restore_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func CRApplyRootFsDiffTar(baseDirectory, containerRootDirectory string) error {
// Only do this if a rootfs-diff.tar actually exists
rootfsDiffFile, err := os.Open(rootfsDiffPath)
if err != nil {
if os.IsNotExist(err) {
if errors.Is(err, os.ErrNotExist) {
return nil
}
return errors.Wrap(err, "failed to open root file-system diff file")
Expand Down
2 changes: 1 addition & 1 deletion pkg/domain/filters/containers.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ func GenerateContainerFilterFuncs(filter string, filterValues []string, r *libpo
for _, val := range filterValues {
net, err := r.Network().NetworkInspect(val)
if err != nil {
if errors.Cause(err) == define.ErrNoSuchNetwork {
if errors.Is(err, define.ErrNoSuchNetwork) {
continue
}
return nil, err
Expand Down
2 changes: 1 addition & 1 deletion pkg/domain/filters/pods.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ func GeneratePodFilterFunc(filter string, filterValues []string, r *libpod.Runti
for _, val := range filterValues {
net, err := r.Network().NetworkInspect(val)
if err != nil {
if errors.Cause(err) == define.ErrNoSuchNetwork {
if errors.Is(err, define.ErrNoSuchNetwork) {
continue
}
return nil, err
Expand Down
11 changes: 5 additions & 6 deletions pkg/domain/infra/abi/images.go
Original file line number Diff line number Diff line change
Expand Up @@ -848,13 +848,12 @@ func execPodman(execUser *user.User, command []string) error {
if err != nil {
return err
}
defer func() error {
err := cmdLogin.Process.Kill()
if err != nil {
return err
}
return cmdLogin.Wait()

defer func() {
_ = cmdLogin.Process.Kill()
_ = cmdLogin.Wait()
}()

cmd := exec.Command(command[0], command[1:]...)
cmd.Env = []string{"PATH=" + os.Getenv("PATH"), "TERM=" + os.Getenv("TERM")}
cmd.Stderr = os.Stderr
Expand Down
4 changes: 2 additions & 2 deletions pkg/domain/infra/abi/play.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ func (ic *ContainerEngine) playKubePod(ctx context.Context, podName string, podY
// FIXME This is very hard to support properly with a good ux
if len(options.StaticIPs) > *ipIndex {
if !podOpt.Net.Network.IsBridge() {
errors.Wrap(define.ErrInvalidArg, "static ip addresses can only be set when the network mode is bridge")
return nil, errors.Wrap(define.ErrInvalidArg, "static ip addresses can only be set when the network mode is bridge")
}
if len(podOpt.Net.Networks) != 1 {
return nil, errors.Wrap(define.ErrInvalidArg, "cannot set static ip addresses for more than network, use netname:ip=<ip> syntax to specify ips for more than network")
Expand All @@ -230,7 +230,7 @@ func (ic *ContainerEngine) playKubePod(ctx context.Context, podName string, podY
}
if len(options.StaticMACs) > *ipIndex {
if !podOpt.Net.Network.IsBridge() {
errors.Wrap(define.ErrInvalidArg, "static mac address can only be set when the network mode is bridge")
return nil, errors.Wrap(define.ErrInvalidArg, "static mac address can only be set when the network mode is bridge")
}
if len(podOpt.Net.Networks) != 1 {
return nil, errors.Wrap(define.ErrInvalidArg, "cannot set static mac address for more than network, use netname:mac=<mac> syntax to specify mac for more than network")
Expand Down
2 changes: 1 addition & 1 deletion pkg/domain/infra/abi/terminal/sigproxy_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const signalBufferSize = 2048
func ProxySignals(ctr *libpod.Container) {
// Stop catching the shutdown signals (SIGINT, SIGTERM) - they're going
// to the container now.
shutdown.Stop()
shutdown.Stop() // nolint: errcheck

sigBuffer := make(chan os.Signal, signalBufferSize)
signal.CatchAll(sigBuffer)
Expand Down
2 changes: 1 addition & 1 deletion pkg/machine/fedora.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func (f FedoraDownload) Get() *Download {
func (f FedoraDownload) HasUsableCache() (bool, error) {
info, err := os.Stat(f.LocalPath)
if err != nil {
if os.IsNotExist(err) {
if errors.Is(err, os.ErrNotExist) {
return false, nil
}
return false, err
Expand Down
13 changes: 9 additions & 4 deletions pkg/machine/qemu/machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,9 @@ func (v *MachineVM) Init(opts machine.InitOptions) (bool, error) {
fmt.Println("An ignition path was provided. No SSH connection was added to Podman")
}
// Write the JSON file
v.writeConfig()
if err := v.writeConfig(); err != nil {
return false, fmt.Errorf("writing JSON file: %w", err)
}

// User has provided ignition file so keygen
// will be skipped.
Expand Down Expand Up @@ -1099,10 +1101,13 @@ func waitAndPingAPI(sock string) {
Transport: &http.Transport{
DialContext: func(context.Context, string, string) (net.Conn, error) {
con, err := net.DialTimeout("unix", sock, apiUpTimeout)
if err == nil {
con.SetDeadline(time.Now().Add(apiUpTimeout))
if err != nil {
return nil, err
}
if err := con.SetDeadline(time.Now().Add(apiUpTimeout)); err != nil {
return nil, err
}
return con, err
return con, nil
},
},
}
Expand Down
7 changes: 4 additions & 3 deletions pkg/rootless/rootless.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
package rootless

import (
"errors"
"fmt"
"os"
"sort"
"sync"

"github.com/containers/storage/pkg/lockfile"
"github.com/opencontainers/runc/libcontainer/user"
spec "github.com/opencontainers/runtime-spec/specs-go"
"github.com/pkg/errors"
)

// TryJoinPauseProcess attempts to join the namespaces of the pause PID via
// TryJoinFromFilePaths. If joining fails, it attempts to delete the specified
// file.
func TryJoinPauseProcess(pausePidPath string) (bool, int, error) {
if _, err := os.Stat(pausePidPath); err != nil {
if os.IsNotExist(err) {
if errors.Is(err, os.ErrNotExist) {
return false, -1, nil
}
return false, -1, err
Expand All @@ -34,7 +35,7 @@ func TryJoinPauseProcess(pausePidPath string) (bool, int, error) {
if os.IsNotExist(err) {
return false, -1, nil
}
return false, -1, errors.Wrapf(err, "error acquiring lock on %s", pausePidPath)
return false, -1, fmt.Errorf("error acquiring lock on %s: %w", pausePidPath, err)
}

pidFileLock.Lock()
Expand Down
2 changes: 1 addition & 1 deletion pkg/specgen/generate/ports_bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (

func benchmarkParsePortMapping(b *testing.B, ports []types.PortMapping) {
for n := 0; n < b.N; n++ {
ParsePortMapping(ports, nil)
_, _ = ParsePortMapping(ports, nil)
}
}

Expand Down
4 changes: 3 additions & 1 deletion pkg/systemd/generate/containers.go
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,9 @@ func executeContainerTemplate(info *containerInfo, options entities.GenerateSyst
fs.StringArrayP("env", "e", nil, "")
fs.String("sdnotify", "", "")
fs.String("restart", "", "")
fs.Parse(remainingCmd)
if err := fs.Parse(remainingCmd); err != nil {
return "", fmt.Errorf("parsing remaining command-line arguments: %w", err)
}

remainingCmd = filterCommonContainerFlags(remainingCmd, fs.NArg())
// If the container is in a pod, make sure that the
Expand Down
4 changes: 3 additions & 1 deletion pkg/systemd/generate/pods.go
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,9 @@ func executePodTemplate(info *podInfo, options entities.GenerateSystemdOptions)
fs.SetInterspersed(false)
fs.String("name", "", "")
fs.Bool("replace", false, "")
fs.Parse(podCreateArgs)
if err := fs.Parse(podCreateArgs); err != nil {
return "", fmt.Errorf("parsing remaining command-line arguments: %w", err)
}

hasNameParam := fs.Lookup("name").Changed
hasReplaceParam, err := fs.GetBool("replace")
Expand Down
8 changes: 3 additions & 5 deletions test/e2e/attach_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package integration

import (
"os"
"syscall"
"time"

Expand All @@ -20,12 +19,11 @@ var _ = Describe("Podman attach", func() {

BeforeEach(func() {
tempdir, err = CreateTempDirInTempDir()
if err != nil {
os.Exit(1)
}
Expect(err).To(BeNil())
podmanTest = PodmanTestCreate(tempdir)
podmanTest.Setup()
podmanTest.SeedImages()
err = podmanTest.SeedImages()
Expect(err).To(BeNil())
})

AfterEach(func() {
Expand Down
8 changes: 4 additions & 4 deletions test/e2e/checkpoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,12 @@ var _ = Describe("Podman checkpoint", func() {
BeforeEach(func() {
SkipIfRootless("checkpoint not supported in rootless mode")
tempdir, err = CreateTempDirInTempDir()
if err != nil {
os.Exit(1)
}
Expect(err).To(BeNil())

podmanTest = PodmanTestCreate(tempdir)
podmanTest.Setup()
podmanTest.SeedImages()
err = podmanTest.SeedImages()
Expect(err).To(BeNil())
// Check if the runtime implements checkpointing. Currently only
// runc's checkpoint/restore implementation is supported.
cmd := exec.Command(podmanTest.OCIRuntime, "checkpoint", "--help")
Expand Down
7 changes: 3 additions & 4 deletions test/e2e/commit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,11 @@ var _ = Describe("Podman commit", func() {

BeforeEach(func() {
tempdir, err = CreateTempDirInTempDir()
if err != nil {
os.Exit(1)
}
Expect(err).To(BeNil())
podmanTest = PodmanTestCreate(tempdir)
podmanTest.Setup()
podmanTest.SeedImages()
err = podmanTest.SeedImages()
Expect(err).To(BeNil())
})

AfterEach(func() {
Expand Down
3 changes: 2 additions & 1 deletion test/e2e/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -809,7 +809,8 @@ func (p *PodmanTestIntegration) RestoreArtifactToCache(image string) error {

func populateCache(podman *PodmanTestIntegration) {
for _, image := range CACHE_IMAGES {
podman.RestoreArtifactToCache(image)
err := podman.RestoreArtifactToCache(image)
Expect(err).To(BeNil())
}
// logformatter uses this to recognize the first test
fmt.Printf("-----------------------------\n")
Expand Down
Loading

0 comments on commit 06dd913

Please sign in to comment.