From c5f051af779d1632b8416cfbd7a494be84157103 Mon Sep 17 00:00:00 2001 From: Anish Bista <108048384+anishbista60@users.noreply.github.com> Date: Wed, 4 Sep 2024 10:22:51 +0530 Subject: [PATCH] Enable `errcheck` linter for test packages (#3034) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: anishbista60 Co-authored-by: Julio López <1953782+julio-lopez@users.noreply.github.com> --- .golangci.yml | 1 - pkg/controller/controller_test.go | 20 +++-- .../repositoryserver_controller_test.go | 12 ++- pkg/ephemeral/envvar_test.go | 17 +++-- pkg/function/data_test.go | 6 +- pkg/function/kube_task_test.go | 6 +- pkg/kopia/command/common_test.go | 19 +++-- pkg/kube/exec_test.go | 5 +- pkg/kube/log_tail_test.go | 15 +++- pkg/kube/pod_command_executor_test.go | 11 ++- pkg/kube/pod_controller_test.go | 3 +- pkg/kube/pod_file_writer_test.go | 3 +- pkg/kube/pod_runner_test.go | 3 +- pkg/kube/pod_test.go | 14 ++-- pkg/kube/podinfo_test.go | 20 +++-- pkg/location/location_test.go | 6 +- pkg/log/fluentbit_test.go | 6 +- pkg/log/log_test.go | 11 ++- pkg/objectstore/objectstore_test.go | 6 +- pkg/output/stream_test.go | 74 ++++++++++++------- pkg/testing/e2e_test.go | 3 +- pkg/testing/integration_test.go | 20 +++-- 22 files changed, 190 insertions(+), 91 deletions(-) diff --git a/.golangci.yml b/.golangci.yml index 02f9462685..508eb40f25 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -40,7 +40,6 @@ issues: exclude-rules: - path: '_test.go' linters: - - errcheck # Errors may be ignored in tests. - unparam # Tests might have unused function parameters. - lll - dupl diff --git a/pkg/controller/controller_test.go b/pkg/controller/controller_test.go index a925d4c2ef..2540240831 100644 --- a/pkg/controller/controller_test.go +++ b/pkg/controller/controller_test.go @@ -761,12 +761,13 @@ func (s *ControllerSuite) TestRuntimeObjEventLogs(c *C) { } func (s *ControllerSuite) TestDeferPhase(c *C) { - os.Setenv(kube.PodNSEnvVar, "test") + err := os.Setenv(kube.PodNSEnvVar, "test") + c.Assert(err, IsNil) ctx := context.Background() bp := newBPWithDeferPhase() - bp, err := s.crCli.Blueprints(s.namespace).Create(ctx, bp, metav1.CreateOptions{}) + bp, err = s.crCli.Blueprints(s.namespace).Create(ctx, bp, metav1.CreateOptions{}) c.Assert(err, IsNil) // create backup actionset and wait for it to be completed @@ -811,11 +812,12 @@ func (s *ControllerSuite) TestDeferPhase(c *C) { // 3. Phases have correct state in actionset status // 4. We don't render output artifacts if any of the phases failed func (s *ControllerSuite) TestDeferPhaseCoreErr(c *C) { - os.Setenv(kube.PodNSEnvVar, "test") + err := os.Setenv(kube.PodNSEnvVar, "test") + c.Assert(err, IsNil) ctx := context.Background() bp := newBPWithDeferPhaseAndErrInCorePhase() - bp, err := s.crCli.Blueprints(s.namespace).Create(ctx, bp, metav1.CreateOptions{}) + bp, err = s.crCli.Blueprints(s.namespace).Create(ctx, bp, metav1.CreateOptions{}) c.Assert(err, IsNil) as := testutil.NewTestActionSet(s.namespace, bp.GetName(), "Deployment", s.deployment.GetName(), s.namespace, kanister.DefaultVersion, "backup") @@ -846,11 +848,12 @@ func (s *ControllerSuite) TestDeferPhaseCoreErr(c *C) { } func (s *ControllerSuite) TestDeferPhaseDeferErr(c *C) { - os.Setenv(kube.PodNSEnvVar, "test") + err := os.Setenv(kube.PodNSEnvVar, "test") + c.Assert(err, IsNil) ctx := context.Background() bp := newBPWithDeferPhaseAndErrInDeferPhase() - bp, err := s.crCli.Blueprints(s.namespace).Create(ctx, bp, metav1.CreateOptions{}) + bp, err = s.crCli.Blueprints(s.namespace).Create(ctx, bp, metav1.CreateOptions{}) c.Assert(err, IsNil) as := testutil.NewTestActionSet(s.namespace, bp.GetName(), "Deployment", s.deployment.GetName(), s.namespace, kanister.DefaultVersion, "backup") @@ -1058,11 +1061,12 @@ func (s *ControllerSuite) TestRenderArtifactsFailure(c *C) { } func (s *ControllerSuite) TestProgressRunningPhase(c *C) { - os.Setenv(kube.PodNSEnvVar, "test") + err := os.Setenv(kube.PodNSEnvVar, "test") + c.Assert(err, IsNil) ctx := context.Background() bp := newBPForProgressRunningPhase() - bp, err := s.crCli.Blueprints(s.namespace).Create(ctx, bp, metav1.CreateOptions{}) + bp, err = s.crCli.Blueprints(s.namespace).Create(ctx, bp, metav1.CreateOptions{}) c.Assert(err, IsNil) // create actionset and wait for it to reach Running state diff --git a/pkg/controllers/repositoryserver/repositoryserver_controller_test.go b/pkg/controllers/repositoryserver/repositoryserver_controller_test.go index 11130aa423..4aad43d3d2 100644 --- a/pkg/controllers/repositoryserver/repositoryserver_controller_test.go +++ b/pkg/controllers/repositoryserver/repositoryserver_controller_test.go @@ -119,7 +119,8 @@ func (s *RepoServerControllerSuite) SetUpSuite(c *C) { ws := webhook.NewServer(webhook.Options{Port: 9443}) // Since we are not creating the controller in a pod // the repository server controller needs few env variables set explicitly - os.Setenv("POD_NAMESPACE", s.repoServerControllerNamespace) + err = os.Setenv("POD_NAMESPACE", s.repoServerControllerNamespace) + c.Assert(err, IsNil) mgr, err := ctrl.NewManager(config, ctrl.Options{ Scheme: scheme, @@ -148,10 +149,13 @@ func (s *RepoServerControllerSuite) SetUpSuite(c *C) { go func(ctx context.Context) { // Env setup required to start the controller service // We need to set this up since we are not creating controller in a pod - os.Setenv("HOSTNAME", controllerPodName) - os.Setenv("POD_SERVICE_ACCOUNT", defaultServiceAccount) + err := os.Setenv("HOSTNAME", controllerPodName) + c.Assert(err, IsNil) + err = os.Setenv("POD_SERVICE_ACCOUNT", defaultServiceAccount) + c.Assert(err, IsNil) // Set KANISTER_TOOLS env to override and use dev image - os.Setenv(consts.KanisterToolsImageEnvName, consts.LatestKanisterToolsImage) + err = os.Setenv(consts.KanisterToolsImageEnvName, consts.LatestKanisterToolsImage) + c.Assert(err, IsNil) err = mgr.Start(ctx) c.Assert(err, IsNil) }(ctx) diff --git a/pkg/ephemeral/envvar_test.go b/pkg/ephemeral/envvar_test.go index f2c88e26e6..d3fa275ff2 100644 --- a/pkg/ephemeral/envvar_test.go +++ b/pkg/ephemeral/envvar_test.go @@ -46,9 +46,12 @@ func (s *EnvVarSuite) TestOSEnvVarKubePodOptions(c *C) { c.Assert(options.EnvironmentVariables, DeepEquals, []corev1.EnvVar(nil)) // OS environment variable set - os.Setenv(expected[0].Name, expected[0].Value) - defer os.Unsetenv(expected[0].Name) - + err := os.Setenv(expected[0].Name, expected[0].Value) + c.Assert(err, IsNil) + defer func() { + err := os.Unsetenv(expected[0].Name) + c.Assert(err, IsNil) + }() registeredAppliers.Apply(&options) c.Assert(options.EnvironmentVariables, DeepEquals, expected) } @@ -71,9 +74,13 @@ func (s *EnvVarSuite) TestOSEnvVarCoreV1Container(c *C) { c.Assert(options.Env, DeepEquals, []corev1.EnvVar(nil)) // OS environment variable set - os.Setenv(expected[0].Name, expected[0].Value) - defer os.Unsetenv(expected[0].Name) + err := os.Setenv(expected[0].Name, expected[0].Value) + c.Assert(err, IsNil) + defer func() { + err := os.Unsetenv(expected[0].Name) + c.Assert(err, IsNil) + }() registeredAppliers.Apply(&options) c.Assert(options.Env, DeepEquals, expected) } diff --git a/pkg/function/data_test.go b/pkg/function/data_test.go index 40d9af9cd4..ffd3b73d9d 100644 --- a/pkg/function/data_test.go +++ b/pkg/function/data_test.go @@ -106,8 +106,10 @@ func (s *DataSuite) SetUpSuite(c *C) { location.Bucket = testBucketName s.profile = testutil.ObjectStoreProfileOrSkip(c, s.providerType, location) - os.Setenv("POD_NAMESPACE", s.namespace) - os.Setenv("POD_SERVICE_ACCOUNT", "default") + err = os.Setenv("POD_NAMESPACE", s.namespace) + c.Assert(err, IsNil) + err = os.Setenv("POD_SERVICE_ACCOUNT", "default") + c.Assert(err, IsNil) } func (s *DataSuite) TearDownSuite(c *C) { diff --git a/pkg/function/kube_task_test.go b/pkg/function/kube_task_test.go index c018859d32..92baaf0b49 100644 --- a/pkg/function/kube_task_test.go +++ b/pkg/function/kube_task_test.go @@ -53,8 +53,10 @@ func (s *KubeTaskSuite) SetUpSuite(c *C) { cns, err := s.cli.CoreV1().Namespaces().Create(context.TODO(), ns, metav1.CreateOptions{}) c.Assert(err, IsNil) s.namespace = cns.Name - os.Setenv("POD_NAMESPACE", cns.Name) - os.Setenv("POD_SERVICE_ACCOUNT", "default") + err = os.Setenv("POD_NAMESPACE", cns.Name) + c.Assert(err, IsNil) + err = os.Setenv("POD_SERVICE_ACCOUNT", "default") + c.Assert(err, IsNil) } func (s *KubeTaskSuite) TearDownSuite(c *C) { diff --git a/pkg/kopia/command/common_test.go b/pkg/kopia/command/common_test.go index 159f319d79..7af1b03d03 100644 --- a/pkg/kopia/command/common_test.go +++ b/pkg/kopia/command/common_test.go @@ -80,9 +80,12 @@ func (s *CommonUtilsSuite) TestCommonArgs(c *check.C) { }, { setup: func() func() { origLogLevel := os.Getenv(LogLevelVarName) - os.Setenv(LogLevelVarName, "debug") + err := os.Setenv(LogLevelVarName, "debug") + c.Assert(err, check.IsNil) + return func() { - os.Setenv(LogLevelVarName, origLogLevel) + err := os.Setenv(LogLevelVarName, origLogLevel) + c.Assert(err, check.IsNil) } }, comment: "Custom log level passed via env variable, file log level passed via args", @@ -103,11 +106,15 @@ func (s *CommonUtilsSuite) TestCommonArgs(c *check.C) { setup: func() func() { origLogLevel := os.Getenv(LogLevelVarName) origFileLogLevel := os.Getenv(FileLogLevelVarName) - os.Setenv(LogLevelVarName, "debug") - os.Setenv(FileLogLevelVarName, "debug") + err := os.Setenv(LogLevelVarName, "debug") + c.Assert(err, check.IsNil) + err = os.Setenv(FileLogLevelVarName, "debug") + c.Assert(err, check.IsNil) return func() { - os.Setenv(LogLevelVarName, origLogLevel) - os.Setenv(FileLogLevelVarName, origFileLogLevel) + err := os.Setenv(LogLevelVarName, origLogLevel) + c.Assert(err, check.IsNil) + err = os.Setenv(FileLogLevelVarName, origFileLogLevel) + c.Assert(err, check.IsNil) } }, comment: "Custom log level and file log level both passed via env variable", diff --git a/pkg/kube/exec_test.go b/pkg/kube/exec_test.go index e39ebdf81f..0c5990e2a4 100644 --- a/pkg/kube/exec_test.go +++ b/pkg/kube/exec_test.go @@ -274,7 +274,10 @@ func (s *ExecSuite) TestKopiaCommand(c *C) { } p, err := s.cli.CoreV1().Pods(s.namespace).Create(ctx, pod, metav1.CreateOptions{}) c.Assert(err, IsNil) - defer s.cli.CoreV1().Pods(s.namespace).Delete(ctx, pod.Name, metav1.DeleteOptions{}) + defer func() { + err := s.cli.CoreV1().Pods(s.namespace).Delete(ctx, pod.Name, metav1.DeleteOptions{}) + c.Assert(err, IsNil) + }() ctxT, cancel := context.WithTimeout(ctx, 5*time.Minute) defer cancel() c.Assert(WaitForPodReady(ctxT, s.cli, s.namespace, p.Name), IsNil) diff --git a/pkg/kube/log_tail_test.go b/pkg/kube/log_tail_test.go index 22f7d5e3ba..94e88fbf27 100644 --- a/pkg/kube/log_tail_test.go +++ b/pkg/kube/log_tail_test.go @@ -52,14 +52,21 @@ func (s *LogTailTestSuite) TestLogsTail(c *C) { lt := NewLogTail(3) c.Check(lt.ToString(), Equals, "") // If there were no writes at all, output should be empty line - lt.Write([]byte("line1")) - lt.Write([]byte("line2")) + _, err := lt.Write([]byte("line1")) + c.Assert(err, IsNil) + _, err = lt.Write([]byte("line2")) + c.Assert(err, IsNil) + c.Check(lt.ToString(), Equals, "line1\r\nline2") c.Check(lt.ToString(), Equals, "line1\r\nline2") // Second invocation should get the same result // Check that buffer is still working after ToString is called - lt.Write([]byte("line3")) + _, err = lt.Write([]byte("line3")) + c.Assert(err, IsNil) + c.Check(lt.ToString(), Equals, "line1\r\nline2\r\nline3") - lt.Write([]byte("line4")) + + _, err = lt.Write([]byte("line4")) + c.Assert(err, IsNil) c.Check(lt.ToString(), Equals, "line2\r\nline3\r\nline4") } diff --git a/pkg/kube/pod_command_executor_test.go b/pkg/kube/pod_command_executor_test.go index f7fe12396f..332eab2f6b 100644 --- a/pkg/kube/pod_command_executor_test.go +++ b/pkg/kube/pod_command_executor_test.go @@ -37,7 +37,8 @@ const ( ) func (s *PodCommandExecutorTestSuite) SetUpSuite(c *C) { - os.Setenv("POD_NAMESPACE", podCommandExecutorNS) + err := os.Setenv("POD_NAMESPACE", podCommandExecutorNS) + c.Assert(err, IsNil) } // testBarrier supports race-free synchronization between a controller and a background goroutine. @@ -81,10 +82,14 @@ func (fprp *fakePodCommandExecutorProcessor) ExecWithOptions(ctx context.Context fprp.inExecWithOptionsOpts = &opts fprp.execWithOptionsSyncStart.SyncWithController() if opts.Stdout != nil && len(fprp.execWithOptionsStdout) > 0 { - opts.Stdout.Write([]byte(fprp.execWithOptionsStdout)) + if _, err := opts.Stdout.Write([]byte(fprp.execWithOptionsStdout)); err != nil { + return err + } } if opts.Stderr != nil && len(fprp.execWithOptionsStderr) > 0 { - opts.Stderr.Write([]byte(fprp.execWithOptionsStderr)) + if _, err := opts.Stderr.Write([]byte(fprp.execWithOptionsStderr)); err != nil { + return err + } } fprp.execWithOptionsSyncEnd.SyncWithController() diff --git a/pkg/kube/pod_controller_test.go b/pkg/kube/pod_controller_test.go index 8eaea039a4..734889b2bc 100644 --- a/pkg/kube/pod_controller_test.go +++ b/pkg/kube/pod_controller_test.go @@ -38,7 +38,8 @@ const ( ) func (s *PodControllerTestSuite) SetUpSuite(c *C) { - os.Setenv("POD_NAMESPACE", podControllerNS) + err := os.Setenv("POD_NAMESPACE", podControllerNS) + c.Assert(err, IsNil) } func (s *PodControllerTestSuite) TestPodControllerStartPod(c *C) { diff --git a/pkg/kube/pod_file_writer_test.go b/pkg/kube/pod_file_writer_test.go index 7c9f1dc3be..2a349066ed 100644 --- a/pkg/kube/pod_file_writer_test.go +++ b/pkg/kube/pod_file_writer_test.go @@ -37,7 +37,8 @@ const ( ) func (s *PodFileWriterTestSuite) SetUpSuite(c *C) { - os.Setenv("POD_NAMESPACE", podFileWriterNS) + err := os.Setenv("POD_NAMESPACE", podFileWriterNS) + c.Assert(err, IsNil) } type fakePodFileWriterProcessor struct { diff --git a/pkg/kube/pod_runner_test.go b/pkg/kube/pod_runner_test.go index 74432cfd63..3cf049d741 100644 --- a/pkg/kube/pod_runner_test.go +++ b/pkg/kube/pod_runner_test.go @@ -40,7 +40,8 @@ const ( ) func (s *PodRunnerTestSuite) SetUpSuite(c *C) { - os.Setenv("POD_NAMESPACE", podRunnerNS) + err := os.Setenv("POD_NAMESPACE", podRunnerNS) + c.Assert(err, IsNil) } func (s *PodRunnerTestSuite) TestPodRunnerContextCanceled(c *C) { diff --git a/pkg/kube/pod_test.go b/pkg/kube/pod_test.go index d8527d4ac4..3033f03a20 100644 --- a/pkg/kube/pod_test.go +++ b/pkg/kube/pod_test.go @@ -63,8 +63,11 @@ func (s *PodSuite) SetUpSuite(c *C) { c.Assert(err, IsNil) s.namespace = ns.Name - os.Setenv("POD_NAMESPACE", ns.Name) - os.Setenv("POD_SERVICE_ACCOUNT", controllerSA) + err = os.Setenv("POD_NAMESPACE", ns.Name) + c.Assert(err, IsNil) + + err = os.Setenv("POD_SERVICE_ACCOUNT", controllerSA) + c.Assert(err, IsNil) err = s.createServiceAccount(testSAName, s.namespace) c.Assert(err, IsNil) @@ -850,10 +853,11 @@ func (s *PodSuite) TestPatchDefaultPodSpecs(c *C) { func (s *PodSuite) TestGetPodReadyWaitTimeout(c *C) { // Setup ENV to change the default timeout - os.Setenv(PodReadyWaitTimeoutEnv, "5") + err := os.Setenv(PodReadyWaitTimeoutEnv, "5") + c.Assert(err, IsNil) c.Assert(GetPodReadyWaitTimeout(), Equals, time.Minute*5) - os.Unsetenv(PodReadyWaitTimeoutEnv) - + err = os.Unsetenv(PodReadyWaitTimeoutEnv) + c.Assert(err, IsNil) // Check without ENV set c.Assert(GetPodReadyWaitTimeout(), Equals, DefaultPodReadyWaitTimeout) } diff --git a/pkg/kube/podinfo_test.go b/pkg/kube/podinfo_test.go index 760d304be5..c840f1d725 100644 --- a/pkg/kube/podinfo_test.go +++ b/pkg/kube/podinfo_test.go @@ -30,11 +30,13 @@ const testPodName = "test-pod-name" const testPodSA = "test-pod-sa" func (s *PodInfoSuite) TestGetControllerNamespaceFromEnv(c *C) { - os.Setenv(PodNSEnvVar, testPodNamespace) + err := os.Setenv(PodNSEnvVar, testPodNamespace) + c.Assert(err, IsNil) ns, err := GetControllerNamespace() c.Assert(err, IsNil) c.Assert(ns, Equals, testPodNamespace) - os.Unsetenv(PodNSEnvVar) + err = os.Unsetenv(PodNSEnvVar) + c.Assert(err, IsNil) } func (s *PodInfoSuite) TestGetControllerNamespaceFromFile(c *C) { @@ -50,11 +52,14 @@ func (s *PodInfoSuite) TestGetControllerNamespaceFromFile(c *C) { } func (s *PodInfoSuite) TestGetControllerPodNameFromEnv(c *C) { - os.Setenv(podNameEnvVar, testPodName) + err := os.Setenv(podNameEnvVar, testPodName) + c.Assert(err, IsNil) + podName, err := GetControllerPodName() c.Assert(err, IsNil) c.Assert(podName, Equals, testPodName) - os.Unsetenv(podNameEnvVar) + err = os.Unsetenv(podNameEnvVar) + c.Assert(err, IsNil) } func (s *PodInfoSuite) TestGetControllerPodNameFromSystem(c *C) { @@ -66,9 +71,12 @@ func (s *PodInfoSuite) TestGetControllerPodNameFromSystem(c *C) { } func (s *PodInfoSuite) TestGetControllerServiceAccountFromEnv(c *C) { - os.Setenv(PodSAEnvVar, testPodSA) + err := os.Setenv(PodSAEnvVar, testPodSA) + c.Assert(err, IsNil) + saName, err := GetControllerServiceAccount(fake.NewSimpleClientset()) c.Assert(err, IsNil) c.Assert(saName, Equals, testPodSA) - os.Unsetenv(testPodSA) + err = os.Unsetenv(testPodSA) + c.Assert(err, IsNil) } diff --git a/pkg/location/location_test.go b/pkg/location/location_test.go index eb11778b96..b7ab4d4099 100644 --- a/pkg/location/location_test.go +++ b/pkg/location/location_test.go @@ -151,8 +151,10 @@ func (s *LocationSuite) TestAzMultipartUpload(c *C) { // Create test file f, err := os.Create(s.testMultipartPath) c.Check(err, IsNil) - defer f.Close() - + defer func() { + err = f.Close() + c.Assert(err, IsNil) + }() ctx := context.Background() for _, fileSize := range []int64{ 0, // empty file diff --git a/pkg/log/fluentbit_test.go b/pkg/log/fluentbit_test.go index d8f0e071b8..638a5a2a9d 100644 --- a/pkg/log/fluentbit_test.go +++ b/pkg/log/fluentbit_test.go @@ -65,7 +65,11 @@ func runServer(failAfterNLogs int, endFlag chan<- bool, c *C) { defer close(endFlag) l := resolveAndListen(fakeEndPoint, c) - defer l.Close() + defer func() { + err := l.Close() + c.Assert(err, IsNil) + }() + Loop: for { select { diff --git a/pkg/log/log_test.go b/pkg/log/log_test.go index b4fdd7303b..a372911f79 100644 --- a/pkg/log/log_test.go +++ b/pkg/log/log_test.go @@ -165,7 +165,8 @@ func testLogMessage(c *C, msg string, print func(string, ...field.M), fields ... } func (s *LogSuite) TestLogLevel(c *C) { - os.Unsetenv(LevelEnvName) + err := os.Unsetenv(LevelEnvName) + c.Assert(err, IsNil) initLogLevel() log.SetFormatter(&logrus.JSONFormatter{TimestampFormat: time.RFC3339Nano}) @@ -175,15 +176,17 @@ func (s *LogSuite) TestLogLevel(c *C) { var entry map[string]interface{} // Check if debug level log is printed when log level is info Debug().WithContext(ctx).Print("Testing debug level") - err := json.Unmarshal(output.Bytes(), &entry) + err = json.Unmarshal(output.Bytes(), &entry) c.Assert(err, NotNil) c.Assert(output.String(), HasLen, 0) // Check if debug level log is printed when log level is debug - os.Setenv(LevelEnvName, "debug") + err = os.Setenv(LevelEnvName, "debug") + c.Assert(err, IsNil) defer func() { - os.Unsetenv(LevelEnvName) + err := os.Unsetenv(LevelEnvName) + c.Assert(err, IsNil) initLogLevel() }() initLogLevel() diff --git a/pkg/objectstore/objectstore_test.go b/pkg/objectstore/objectstore_test.go index af44ae732c..7050a1ed3c 100644 --- a/pkg/objectstore/objectstore_test.go +++ b/pkg/objectstore/objectstore_test.go @@ -393,7 +393,8 @@ func (s *ObjectStoreProviderSuite) TestObjectsStreaming(c *C) { c.Check(err, IsNil) data, err := io.ReadAll(r) c.Check(err, IsNil) - r.Close() + err = r.Close() + c.Assert(err, IsNil) c.Check(data, DeepEquals, data1B) err = rootDirectory.Put(ctx, obj2, bytes.NewReader(data2B), int64(len(data2B)), tags) @@ -402,7 +403,8 @@ func (s *ObjectStoreProviderSuite) TestObjectsStreaming(c *C) { c.Check(err, IsNil) data, err = io.ReadAll(r) c.Check(err, IsNil) - r.Close() + err = r.Close() + c.Assert(err, IsNil) c.Check(data, DeepEquals, data2B) c.Check(ntags, DeepEquals, tags) diff --git a/pkg/output/stream_test.go b/pkg/output/stream_test.go index b74fc2828c..853d8b77f1 100644 --- a/pkg/output/stream_test.go +++ b/pkg/output/stream_test.go @@ -17,6 +17,7 @@ package output_test import ( "context" "io" + "log" "math/rand" "time" @@ -105,39 +106,60 @@ func generateTestCases(numOfLines, avgPrefixLength, avgKeyLength, avgValueLength func getTestReaderCloser(done chan struct{}, cases []testCase) io.ReadCloser { pr, pw := io.Pipe() - r := rand.New(rand.NewSource(time.Now().UnixNano())) go func() { - defer pw.Close() + defer closePipe(pw) + writeTestCases(done, pw, cases) + }() + + return pr +} + +func closePipe(pw io.WriteCloser) { + if err := pw.Close(); err != nil { + log.Printf("Error %v closing connection", err) + } +} + +func writeTestCases(done chan struct{}, pw io.Writer, cases []testCase) { + r := rand.New(rand.NewSource(time.Now().UnixNano())) + + for _, tc := range cases { + if shouldExit(done) { + return + } - for _, tc := range cases { - select { - case <-done: + if tc.prefixLength != 0 { + writePrefix(pw, r, tc) + } + + if tc.key != "" { + if err := output.PrintOutputTo(pw, tc.key, string(tc.value)); err != nil { return - default: - if tc.prefixLength != 0 { - endline := NoEndline - if tc.prefixWithEndline { - endline = NewlineEndline - } - prefixLine := generateRandomRunes(r, tc.prefixLength, endline) - _, err := pw.Write([]byte(string(prefixLine))) - if err != nil { - return - } - } - - if tc.key != "" { - err := output.PrintOutputTo(pw, tc.key, string(tc.value)) - if err != nil { - return - } - } } } - }() + } +} - return pr +func shouldExit(done chan struct{}) bool { + select { + case <-done: + return true + default: + return false + } +} + +func writePrefix(pw io.Writer, r *rand.Rand, tc testCase) { + endline := NoEndline + if tc.prefixWithEndline { + endline = NewlineEndline + } + prefixLine := generateRandomRunes(r, tc.prefixLength, endline) + _, err := pw.Write([]byte(string(prefixLine))) + if err != nil { + return + } } // TestLongStreamsWithoutPhaseOutput Will produce 10 long lines diff --git a/pkg/testing/e2e_test.go b/pkg/testing/e2e_test.go index c5386922c5..042345f97b 100644 --- a/pkg/testing/e2e_test.go +++ b/pkg/testing/e2e_test.go @@ -79,7 +79,8 @@ func (s *E2ESuite) SetUpSuite(c *C) { func (s *E2ESuite) TearDownSuite(c *C) { if s.namespace != "" { - s.cli.CoreV1().Namespaces().Delete(context.TODO(), s.namespace, metav1.DeleteOptions{}) + err := s.cli.CoreV1().Namespaces().Delete(context.TODO(), s.namespace, metav1.DeleteOptions{}) + c.Assert(err, IsNil) } if s.cancel != nil { s.cancel() diff --git a/pkg/testing/integration_test.go b/pkg/testing/integration_test.go index 00ff4444da..4c4123fc39 100644 --- a/pkg/testing/integration_test.go +++ b/pkg/testing/integration_test.go @@ -93,8 +93,12 @@ func integrationSetup(t *test.T) { t.Fatalf("Integration test setup failure: Error creating clusterRoleBinding; err=%v", err) } // Set Controller namespace and service account - os.Setenv(kube.PodNSEnvVar, ns) - os.Setenv(kube.PodSAEnvVar, controllerSA) + if err := os.Setenv(kube.PodNSEnvVar, ns); err != nil { + t.Fatalf("Error %v setting env variable", err) + } + if err := os.Setenv(kube.PodSAEnvVar, controllerSA); err != nil { + t.Fatalf("Error %v setting env variable", err) + } if err = resource.CreateCustomResources(ctx, cfg); err != nil { t.Fatalf("Integration test setup failure: Error createing custom resources; err=%v", err) @@ -121,13 +125,19 @@ func integrationCleanup(t *test.T) { kontroller.cancel() } if kontroller.namespace != "" { - kontroller.kubeCli.CoreV1().Namespaces().Delete(ctx, kontroller.namespace, metav1.DeleteOptions{}) + if err := kontroller.kubeCli.CoreV1().Namespaces().Delete(ctx, kontroller.namespace, metav1.DeleteOptions{}); err != nil { + t.Fatalf("Error %v deleting namespaces %s", err, kontroller.namespace) + } } if kontroller.clusterRoleBinding != nil && kontroller.clusterRoleBinding.Name != "" { - kontroller.kubeCli.RbacV1().ClusterRoleBindings().Delete(ctx, kontroller.clusterRoleBinding.Name, metav1.DeleteOptions{}) + if err := kontroller.kubeCli.RbacV1().ClusterRoleBindings().Delete(ctx, kontroller.clusterRoleBinding.Name, metav1.DeleteOptions{}); err != nil { + t.Fatalf("Error %v deleting clusterrolebinding %s", err, kontroller.clusterRoleBinding) + } } if kontroller.clusterRole != nil && kontroller.clusterRole.Name != "" { - kontroller.kubeCli.RbacV1().ClusterRoles().Delete(ctx, kontroller.clusterRole.Name, metav1.DeleteOptions{}) + if err := kontroller.kubeCli.RbacV1().ClusterRoles().Delete(ctx, kontroller.clusterRole.Name, metav1.DeleteOptions{}); err != nil { + t.Fatalf("Error %v deleting clusterrole %s", err, kontroller.clusterRole) + } } }