Skip to content

Commit

Permalink
Fix unit tests and comments
Browse files Browse the repository at this point in the history
Signed-off-by: z1cheng <[email protected]>
  • Loading branch information
z1cheng committed Jun 29, 2023
1 parent ae49d8b commit b0bbcbc
Show file tree
Hide file tree
Showing 7 changed files with 17 additions and 40 deletions.
4 changes: 3 additions & 1 deletion cmd/plugin/commands/certs/certs.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package certs

import (
"fmt"
"os"

"github.com/spf13/cobra"

Expand Down Expand Up @@ -47,7 +48,8 @@ func CreateCommand(flags *genericclioptions.ConfigFlags) *cobra.Command {

cmd.Flags().String("host", "", "Get the cert for this hostname")
if err := cobra.MarkFlagRequired(cmd.Flags(), "host"); err != nil {
fmt.Printf("error marking flag as required: %v", err)
util.PrintError(err)
os.Exit(1)
}
pod = util.AddPodFlag(cmd)
deployment = util.AddDeploymentFlag(cmd)
Expand Down
4 changes: 1 addition & 3 deletions cmd/plugin/kubectl/kubectl.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,7 @@ func execToWriter(args []string, writer io.Writer) error {
}

go func() {
if _, err := io.Copy(writer, op); err != nil {
fmt.Printf("Error copying output: %v\n", err)
}
io.Copy(writer, op) //nolint:errcheck
}()
err = cmd.Run()
if err != nil {
Expand Down
8 changes: 2 additions & 6 deletions internal/ingress/controller/checker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,7 @@ func TestNginxCheck(t *testing.T) {
}
}()
go func() {
if err := cmd.Wait(); err != nil {
t.Errorf("unexpected error waiting for the process: %v", err)
}
cmd.Wait() //nolint:errcheck
}()

if _, err := pidFile.Write([]byte(fmt.Sprintf("%v", pid))); err != nil {
Expand All @@ -123,9 +121,7 @@ func TestNginxCheck(t *testing.T) {
})

// pollute pid file
if _, err := pidFile.Write([]byte("999999")); err != nil {
t.Errorf("unexpected error polluting the pid file: %v", err)
}
pidFile.Write([]byte("999999")) //nolint:errcheck
pidFile.Close()

t.Run("bad pid", func(t *testing.T) {
Expand Down
6 changes: 1 addition & 5 deletions internal/ingress/controller/store/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,7 @@ func TestStore(t *testing.T) {

emptySelector, _ := labels.Parse("")

defer func() {
if err := te.Stop(); err != nil {
t.Errorf("error: %v", err)
}
}()
defer te.Stop() //nolint:errcheck

clientSet, err := kubernetes.NewForConfig(cfg)
if err != nil {
Expand Down
16 changes: 8 additions & 8 deletions internal/ingress/controller/template/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,14 @@ package template

import (
"bytes"
"crypto/rand"
"crypto/sha1" // #nosec
"encoding/base64"
"encoding/hex"
"encoding/json"
"fmt"
"io"
"math/rand" // #nosec
"math/big"
"net"
"net/url"
"os"
Expand All @@ -34,7 +35,6 @@ import (
"strconv"
"strings"
text_template "text/template"
"time"

networkingv1 "k8s.io/api/networking/v1"
"k8s.io/apimachinery/pkg/util/sets"
Expand Down Expand Up @@ -1186,16 +1186,16 @@ func buildAuthSignURLLocation(location, authSignURL string) string {
}

var letters = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
var random *rand.Rand

func init() {
random = rand.New(rand.NewSource(time.Now().UnixNano())) // #nosec
}

func randomString() string {
b := make([]rune, 32)
for i := range b {
b[i] = letters[random.Intn(len(letters))] // #nosec
idx, err := rand.Int(rand.Reader, big.NewInt(int64(len(letters))))
if err != nil {
klog.Errorf("unexpected error generating random index: %v", err)
return ""
}
b[i] = letters[idx.Int64()]
}

return string(b)
Expand Down
10 changes: 2 additions & 8 deletions internal/ingress/metric/collectors/process_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,7 @@ func TestProcessCollector(t *testing.T) {

done := make(chan struct{})
go func() {
err = cmd.Wait()
if err != nil {
t.Errorf("unexpected error waiting for dummy process: %v", err)
}
cmd.Wait() //nolint:errcheck
status := cmd.ProcessState.Sys().(syscall.WaitStatus)
if status.Signaled() {
t.Logf("Signal: %v", status.Signal())
Expand All @@ -72,11 +69,8 @@ func TestProcessCollector(t *testing.T) {
defer func() {
cm.Stop()

err = cmd.Process.Kill()
cmd.Process.Kill() //nolint:errcheck
<-done
if err != nil {
t.Errorf("unexpected error killing dummy process: %v", err)
}
close(done)
}()

Expand Down
9 changes: 0 additions & 9 deletions pkg/flags/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,20 +228,11 @@ https://blog.maxmind.com/2019/12/18/significant-changes-to-accessing-and-using-g
flags.IntVar(&nginx.MaxmindRetriesCount, "maxmind-retries-count", 1, "Number of attempts to download the GeoIP DB.")
flags.DurationVar(&nginx.MaxmindRetriesTimeout, "maxmind-retries-timeout", time.Second*0, "Maxmind downloading delay between 1st and 2nd attempt, 0s - do not retry to download if something went wrong.")

if err := flag.Set("logtostderr", "true"); err != nil {
return false, nil, err
}
flags.AddGoFlagSet(flag.CommandLine)
if err := flags.Parse(os.Args); err != nil {
return false, nil, err
}

// Workaround for this issue:
// https://github.com/kubernetes/kubernetes/issues/17162
if err := flag.CommandLine.Parse([]string{}); err != nil {
return false, nil, err
}

pflag.VisitAll(func(flag *pflag.Flag) {
klog.V(2).InfoS("FLAG", flag.Name, flag.Value)
})
Expand Down

0 comments on commit b0bbcbc

Please sign in to comment.