Skip to content

Commit

Permalink
Enable errcheck linter for test packages (#3034)
Browse files Browse the repository at this point in the history
Signed-off-by: anishbista60 <[email protected]>
Co-authored-by: Julio López <[email protected]>
  • Loading branch information
anishbista60 and julio-lopez authored Sep 4, 2024
1 parent f277d41 commit c5f051a
Show file tree
Hide file tree
Showing 22 changed files with 190 additions and 91 deletions.
1 change: 0 additions & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 12 additions & 8 deletions pkg/controller/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)
Expand Down
17 changes: 12 additions & 5 deletions pkg/ephemeral/envvar_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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)
}
Expand Down
6 changes: 4 additions & 2 deletions pkg/function/data_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
6 changes: 4 additions & 2 deletions pkg/function/kube_task_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
19 changes: 13 additions & 6 deletions pkg/kopia/command/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand Down
5 changes: 4 additions & 1 deletion pkg/kube/exec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
15 changes: 11 additions & 4 deletions pkg/kube/log_tail_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
11 changes: 8 additions & 3 deletions pkg/kube/pod_command_executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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()

Expand Down
3 changes: 2 additions & 1 deletion pkg/kube/pod_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
3 changes: 2 additions & 1 deletion pkg/kube/pod_file_writer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
3 changes: 2 additions & 1 deletion pkg/kube/pod_runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
14 changes: 9 additions & 5 deletions pkg/kube/pod_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
}
Expand Down
20 changes: 14 additions & 6 deletions pkg/kube/podinfo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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) {
Expand All @@ -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)
}
6 changes: 4 additions & 2 deletions pkg/location/location_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 5 additions & 1 deletion pkg/log/fluentbit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Loading

0 comments on commit c5f051a

Please sign in to comment.