From e8293162e9e265423062f72ddd3ae75d534ba199 Mon Sep 17 00:00:00 2001 From: Kavinraja-G Date: Sun, 10 Sep 2023 20:51:52 +0530 Subject: [PATCH] Added unit tests for common helpers --- go.mod | 59 +-------------------------------------- go.sum | 2 -- pkg/common_test.go | 1 + pkg/outputs/table_test.go | 39 ++++++++++++++++++++++++++ pkg/utils/utils_test.go | 35 +++++++++++++++++++++++ 5 files changed, 76 insertions(+), 60 deletions(-) create mode 100644 pkg/common_test.go create mode 100644 pkg/outputs/table_test.go create mode 100644 pkg/utils/utils_test.go diff --git a/go.mod b/go.mod index f19ba74..e56c379 100644 --- a/go.mod +++ b/go.mod @@ -1,58 +1 @@ -module github.com/Kavinraja-G/node-gizmo - -go 1.20 - -require ( - github.com/Kavinraja-G/kube-bouncer v0.1.0 - github.com/olekukonko/tablewriter v0.0.5 - github.com/spf13/cobra v1.7.0 - github.com/spf13/pflag v1.0.5 - k8s.io/api v0.28.0 - k8s.io/apimachinery v0.28.0 - k8s.io/cli-runtime v0.28.0 - k8s.io/client-go v0.28.0 -) - -require ( - github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect - github.com/davecgh/go-spew v1.1.1 // indirect - github.com/emicklei/go-restful/v3 v3.9.0 // indirect - github.com/go-logr/logr v1.2.4 // indirect - github.com/go-openapi/jsonpointer v0.19.6 // indirect - github.com/go-openapi/jsonreference v0.20.2 // indirect - github.com/go-openapi/swag v0.22.3 // indirect - github.com/gogo/protobuf v1.3.2 // indirect - github.com/golang/protobuf v1.5.3 // indirect - github.com/google/gnostic-models v0.6.8 // indirect - github.com/google/go-cmp v0.5.9 // indirect - github.com/google/gofuzz v1.2.0 // indirect - github.com/google/uuid v1.3.0 // indirect - github.com/imdario/mergo v0.3.6 // indirect - github.com/inconshreveable/mousetrap v1.1.0 // indirect - github.com/josharian/intern v1.0.0 // indirect - github.com/json-iterator/go v1.1.12 // indirect - github.com/mailru/easyjson v0.7.7 // indirect - github.com/mattn/go-runewidth v0.0.9 // indirect - github.com/moby/spdystream v0.2.0 // indirect - github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect - github.com/modern-go/reflect2 v1.0.2 // indirect - github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect - github.com/russross/blackfriday/v2 v2.1.0 // indirect - golang.org/x/net v0.13.0 // indirect - golang.org/x/oauth2 v0.8.0 // indirect - golang.org/x/sys v0.10.0 // indirect - golang.org/x/term v0.10.0 // indirect - golang.org/x/text v0.11.0 // indirect - golang.org/x/time v0.3.0 // indirect - google.golang.org/appengine v1.6.7 // indirect - google.golang.org/protobuf v1.30.0 // indirect - gopkg.in/inf.v0 v0.9.1 // indirect - gopkg.in/yaml.v2 v2.4.0 // indirect - gopkg.in/yaml.v3 v3.0.1 // indirect - k8s.io/klog/v2 v2.100.1 // indirect - k8s.io/kube-openapi v0.0.0-20230717233707-2695361300d9 // indirect - k8s.io/utils v0.0.0-20230406110748-d93618cff8a2 // indirect - sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect - sigs.k8s.io/structured-merge-diff/v4 v4.2.3 // indirect - sigs.k8s.io/yaml v1.3.0 // indirect -) +module github.com/Kavinraja-G/node-gizmo go 1.20 require ( github.com/Kavinraja-G/kube-bouncer v0.1.0 github.com/olekukonko/tablewriter v0.0.5 github.com/spf13/cobra v1.7.0 k8s.io/api v0.28.0 k8s.io/apimachinery v0.28.0 k8s.io/client-go v0.28.0 ) require ( github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect github.com/davecgh/go-spew v1.1.1 // indirect github.com/emicklei/go-restful/v3 v3.9.0 // indirect github.com/go-logr/logr v1.2.4 // indirect github.com/go-openapi/jsonpointer v0.19.6 // indirect github.com/go-openapi/jsonreference v0.20.2 // indirect github.com/go-openapi/swag v0.22.3 // indirect github.com/gogo/protobuf v1.3.2 // indirect github.com/golang/protobuf v1.5.3 // indirect github.com/google/gnostic-models v0.6.8 // indirect github.com/google/go-cmp v0.5.9 // indirect github.com/google/gofuzz v1.2.0 // indirect github.com/google/uuid v1.3.0 // indirect github.com/imdario/mergo v0.3.6 // indirect github.com/inconshreveable/mousetrap v1.1.0 // indirect github.com/josharian/intern v1.0.0 // indirect github.com/json-iterator/go v1.1.12 // indirect github.com/mailru/easyjson v0.7.7 // indirect github.com/mattn/go-runewidth v0.0.9 // indirect github.com/moby/spdystream v0.2.0 // indirect github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect github.com/modern-go/reflect2 v1.0.2 // indirect github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect github.com/russross/blackfriday/v2 v2.1.0 // indirect github.com/spf13/pflag v1.0.5 // indirect golang.org/x/net v0.13.0 // indirect golang.org/x/oauth2 v0.8.0 // indirect golang.org/x/sys v0.10.0 // indirect golang.org/x/term v0.10.0 // indirect golang.org/x/text v0.11.0 // indirect golang.org/x/time v0.3.0 // indirect google.golang.org/appengine v1.6.7 // indirect google.golang.org/protobuf v1.30.0 // indirect gopkg.in/inf.v0 v0.9.1 // indirect gopkg.in/yaml.v2 v2.4.0 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect k8s.io/klog/v2 v2.100.1 // indirect k8s.io/kube-openapi v0.0.0-20230717233707-2695361300d9 // indirect k8s.io/utils v0.0.0-20230406110748-d93618cff8a2 // indirect sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect sigs.k8s.io/structured-merge-diff/v4 v4.2.3 // indirect sigs.k8s.io/yaml v1.3.0 // indirect ) \ No newline at end of file diff --git a/go.sum b/go.sum index c689b42..3198c59 100644 --- a/go.sum +++ b/go.sum @@ -150,8 +150,6 @@ k8s.io/api v0.28.0 h1:3j3VPWmN9tTDI68NETBWlDiA9qOiGJ7sdKeufehBYsM= k8s.io/api v0.28.0/go.mod h1:0l8NZJzB0i/etuWnIXcwfIv+xnDOhL3lLW919AWYDuY= k8s.io/apimachinery v0.28.0 h1:ScHS2AG16UlYWk63r46oU3D5y54T53cVI5mMJwwqFNA= k8s.io/apimachinery v0.28.0/go.mod h1:X0xh/chESs2hP9koe+SdIAcXWcQ+RM5hy0ZynB+yEvw= -k8s.io/cli-runtime v0.28.0 h1:Tcz1nnccXZDNIzoH6EwjCs+7ezkUGhorzCweEvlVOFg= -k8s.io/cli-runtime v0.28.0/go.mod h1:U+ySmOKBm/JUCmebhmecXeTwNN1RzI7DW4+OM8Oryas= k8s.io/client-go v0.28.0 h1:ebcPRDZsCjpj62+cMk1eGNX1QkMdRmQ6lmz5BLoFWeM= k8s.io/client-go v0.28.0/go.mod h1:0Asy9Xt3U98RypWJmU1ZrRAGKhP6NqDPmptlAzK2kMc= k8s.io/klog/v2 v2.100.1 h1:7WCHKK6K8fNhTqfBhISHQ97KrnJNFZMcQvKp7gP/tmg= diff --git a/pkg/common_test.go b/pkg/common_test.go new file mode 100644 index 0000000..362c5d4 --- /dev/null +++ b/pkg/common_test.go @@ -0,0 +1 @@ +package pkg import "testing" func TestGetNodeTopologyInfo(t *testing.T) { type args struct { labels map[string]string } tests := []struct { name string args args region string zone string }{ { name: "WithRegionAndZoneInfo", args: args{ labels: map[string]string{ TopologyRegionLabel: "us-east-1", TopologyZoneLabel: "us-east-1a", }, }, region: "us-east-1", zone: "us-east-1a", }, { name: "NoRegionAndZoneLabels", args: args{ labels: map[string]string{}, }, region: "", zone: "", }, { name: "WithOnlyRegionLabel", args: args{ labels: map[string]string{ TopologyRegionLabel: "us-east-1", }, }, region: "us-east-1", zone: "", }, { name: "WithOnlyZoneLabel", args: args{ labels: map[string]string{ TopologyZoneLabel: "us-east-1a", }, }, region: "", zone: "us-east-1a", }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { got, got1 := GetNodeTopologyInfo(tt.args.labels) if got != tt.region { t.Errorf("GetNodeTopologyInfo() got = %v, region %v", got, tt.region) } if got1 != tt.zone { t.Errorf("GetNodeTopologyInfo() got1 = %v, zone %v", got1, tt.zone) } }) } } \ No newline at end of file diff --git a/pkg/outputs/table_test.go b/pkg/outputs/table_test.go new file mode 100644 index 0000000..d275619 --- /dev/null +++ b/pkg/outputs/table_test.go @@ -0,0 +1,39 @@ +package outputs + +import "testing" + +func TestTableOutput(t *testing.T) { + type args struct { + headers []string + outputData [][]string + } + tests := []struct { + name string + args args + }{ + { + name: "GenericOutputTable", + args: args{ + headers: []string{"NAME", "VERSION", "OS", "ARCHITECTURE"}, + outputData: [][]string{ + { + "Node01", + "1.25.6", + "linux", + "amd64", + }, { + "Node02", + "1.25.6", + "linux", + "arm64", + }, + }, + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + TableOutput(tt.args.headers, tt.args.outputData) + }) + } +} diff --git a/pkg/utils/utils_test.go b/pkg/utils/utils_test.go new file mode 100644 index 0000000..d8bc9d6 --- /dev/null +++ b/pkg/utils/utils_test.go @@ -0,0 +1,35 @@ +package utils + +import "testing" + +func TestPrettyByteSize(t *testing.T) { + type args struct { + b int64 + } + tests := []struct { + name string + args args + want string + }{ + { + name: "ValidByteInPrettyFormatInKiB", + args: args{ + b: 1024, + }, + want: "1.0KiB", + }, { + name: "ValidByteInPrettyFormatInGiB", + args: args{ + b: 1073741824, + }, + want: "1.0GiB", + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := PrettyByteSize(tt.args.b); got != tt.want { + t.Errorf("PrettyByteSize() = %v, want %v", got, tt.want) + } + }) + } +}