From 76bf68d220e7650f7f68766c229c9caf040796a3 Mon Sep 17 00:00:00 2001 From: Nitin Garg Date: Fri, 29 Sep 2023 05:50:16 +0000 Subject: [PATCH] Fix linter errors This commit fixes the linter errors popping up in https://github.com/GoogleCloudPlatform/gcsfuse/pull/1400 to unblock this PR, to allow rebase other branches to master without getting stuck on linter errors. Details of changes * add missing error-return-check * remove references to deprecated ioutil package --- internal/gcsx/append_object_creator_test.go | 3 +- internal/monitor/exporter.go | 11 +++++-- main.go | 9 +++--- .../mounting/gcsfuse_linux_test.go | 4 ++- .../mounting/gcsfuse_test.go | 30 +++++++++++-------- .../mounting/mount_helper_test.go | 7 ++--- 6 files changed, 38 insertions(+), 26 deletions(-) diff --git a/internal/gcsx/append_object_creator_test.go b/internal/gcsx/append_object_creator_test.go index bf633689cb..2813322257 100644 --- a/internal/gcsx/append_object_creator_test.go +++ b/internal/gcsx/append_object_creator_test.go @@ -240,7 +240,8 @@ func (t *AppendObjectCreatorTest) CallsComposeObjectsWithObjectProperties() { WillOnce(Return(nil)) // Call - t.call() + _, err := t.call() + ExpectEq(nil, err) AssertNe(nil, req) ExpectEq(t.srcObject.Name, req.DstName) diff --git a/internal/monitor/exporter.go b/internal/monitor/exporter.go index a0e1204454..80077fc265 100644 --- a/internal/monitor/exporter.go +++ b/internal/monitor/exporter.go @@ -99,10 +99,15 @@ func EnableOpenTelemetryCollectorExporter(address string) error { // CloseOpenTelemetryCollectorExporter ensures all collected metrics are sent to // the OpenTelemetry Collect and closes the exporter. -func CloseOpenTelemetryCollectorExporter() { +func CloseOpenTelemetryCollectorExporter() error { if ocExporter != nil { - ocExporter.Stop() + if err := ocExporter.Stop(); err != nil { + return fmt.Errorf("failed to stop opencensus-exporter: %w", err) + } + ocExporter.Flush() + ocExporter = nil } - ocExporter = nil + + return nil } diff --git a/main.go b/main.go index cc628c1397..8da199f7b9 100644 --- a/main.go +++ b/main.go @@ -336,13 +336,14 @@ func runCLIApp(c *cli.Context) (err error) { // Wait for the file system to be unmounted. err = mfs.Join(context.Background()) + if err != nil { + return fmt.Errorf("failed MountedFileSystem.Join: %w", err) + } monitor.CloseStackdriverExporter() - monitor.CloseOpenTelemetryCollectorExporter() - if err != nil { - err = fmt.Errorf("MountedFileSystem.Join: %w", err) - return + if err := monitor.CloseOpenTelemetryCollectorExporter(); err != nil { + return fmt.Errorf("failed to close open-telemetry collector exporter: %w", err) } return diff --git a/tools/integration_tests/mounting/gcsfuse_linux_test.go b/tools/integration_tests/mounting/gcsfuse_linux_test.go index 91a629117c..19b7e1983f 100644 --- a/tools/integration_tests/mounting/gcsfuse_linux_test.go +++ b/tools/integration_tests/mounting/gcsfuse_linux_test.go @@ -40,7 +40,9 @@ func (t *GcsfuseTest) Statfs() { err = t.runGcsfuse(args) AssertEq(nil, err) - defer util.Unmount(t.dir) + defer func() { + AssertEq(nil, util.Unmount(t.dir)) + }() // Stat the file system. err = syscall.Statfs(t.dir, &stat) diff --git a/tools/integration_tests/mounting/gcsfuse_test.go b/tools/integration_tests/mounting/gcsfuse_test.go index 1444caea72..1d6c8cabf6 100644 --- a/tools/integration_tests/mounting/gcsfuse_test.go +++ b/tools/integration_tests/mounting/gcsfuse_test.go @@ -17,12 +17,10 @@ package integration_test import ( "bytes" "fmt" - "io/ioutil" "os" "os/exec" "path" "path/filepath" - //"runtime" "syscall" "testing" "time" @@ -156,7 +154,7 @@ func (t *GcsfuseTest) NonEmptyMountPoint() { // Write a file into the mount point. p := path.Join(t.dir, "foo") - err = ioutil.WriteFile(p, nil, 0600) + err = os.WriteFile(p, nil, 0600) AssertEq(nil, err) defer os.Remove(p) @@ -175,7 +173,7 @@ func (t *GcsfuseTest) MountPointIsAFile() { // Write a file. p := path.Join(t.dir, "foo") - err = ioutil.WriteFile(p, []byte{}, 0500) + err = os.WriteFile(p, []byte{}, 0500) AssertEq(nil, err) defer os.Remove(p) @@ -216,11 +214,11 @@ func (t *GcsfuseTest) KeyFile() { cmd := t.gcsfuseCommand(args, tc.env) output, err := cmd.CombinedOutput() - util.Unmount(t.dir) - ExpectThat(err, Error(HasSubstr("exit status")), "case %d", i) ExpectThat(string(output), HasSubstr(nonexistent), "case %d", i) ExpectThat(string(output), HasSubstr("no such file"), "case %d", i) + + ExpectEq(nil, util.Unmount(t.dir)) } } @@ -233,14 +231,16 @@ func (t *GcsfuseTest) CannedContents() { err = t.runGcsfuse(args) AssertEq(nil, err) - defer util.Unmount(t.dir) + defer func() { + ExpectEq(nil, util.Unmount(t.dir)) + }() // Check the expected contents of the file system. fi, err = os.Lstat(path.Join(t.dir, canned.TopLevelFile)) AssertEq(nil, err) ExpectEq(os.FileMode(0644), fi.Mode()) - contents, err := ioutil.ReadFile(path.Join(t.dir, canned.TopLevelFile)) + contents, err := os.ReadFile(path.Join(t.dir, canned.TopLevelFile)) AssertEq(nil, err) ExpectEq(canned.TopLevelFile_Contents, string(contents)) @@ -265,7 +265,7 @@ func (t *GcsfuseTest) ReadOnlyMode() { defer util.Unmount(t.dir) // Writing to the file system should fail. - err = ioutil.WriteFile(path.Join(t.dir, "blah"), []byte{}, 0400) + err = os.WriteFile(path.Join(t.dir, "blah"), []byte{}, 0400) ExpectThat(err, Error(HasSubstr("read-only"))) } @@ -282,10 +282,10 @@ func (t *GcsfuseTest) ReadWriteMode() { // Overwrite the canned file. p := path.Join(t.dir, canned.TopLevelFile) - err = ioutil.WriteFile(p, []byte("enchilada"), 0400) + err = os.WriteFile(p, []byte("enchilada"), 0400) AssertEq(nil, err) - contents, err := ioutil.ReadFile(p) + contents, err := os.ReadFile(p) AssertEq(nil, err) ExpectEq("enchilada", string(contents)) } @@ -491,8 +491,12 @@ func (t *GcsfuseTest) ForegroundMode() { err = cmd.Start() AssertEq(nil, err) - defer cmd.Wait() - defer cmd.Process.Kill() + defer func() { + ExpectEq(nil, cmd.Wait()) + }() + defer func() { + ExpectEq(nil, cmd.Process.Kill()) + }() // Accumulate output from stderr until we see a successful mount message, // hackily synchronizing. Yes, this is an O(n^2) loop. diff --git a/tools/integration_tests/mounting/mount_helper_test.go b/tools/integration_tests/mounting/mount_helper_test.go index 44a459fac5..08613d33dd 100644 --- a/tools/integration_tests/mounting/mount_helper_test.go +++ b/tools/integration_tests/mounting/mount_helper_test.go @@ -16,7 +16,6 @@ package integration_test import ( "fmt" - "io/ioutil" "os" "os/exec" "path" @@ -66,7 +65,7 @@ func (t *MountHelperTest) SetUp(_ *TestInfo) { } // Set up the temporary directory. - t.dir, err = ioutil.TempDir("", "mount_helper_test") + t.dir, err = os.MkdirTemp("", "mount_helper_test") AssertEq(nil, err) } @@ -199,7 +198,7 @@ func (t *MountHelperTest) ReadOnlyMode() { defer util.Unmount(t.dir) // Writing to the file system should fail. - err = ioutil.WriteFile(path.Join(t.dir, "blah"), []byte{}, 0400) + err = os.WriteFile(path.Join(t.dir, "blah"), []byte{}, 0400) ExpectThat(err, Error(HasSubstr("read-only"))) } @@ -236,7 +235,7 @@ func (t *MountHelperTest) LinuxArgumentOrder() { defer util.Unmount(t.dir) // Writing to the file system should fail. - err = ioutil.WriteFile(path.Join(t.dir, "blah"), []byte{}, 0400) + err = os.WriteFile(path.Join(t.dir, "blah"), []byte{}, 0400) ExpectThat(err, Error(HasSubstr("read-only"))) }