Skip to content

Commit

Permalink
feat: Added few unit tests and PR workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
Kavinraja-G authored Sep 10, 2023
2 parents 41b4d46 + ef50399 commit b9e366b
Show file tree
Hide file tree
Showing 7 changed files with 170 additions and 4 deletions.
29 changes: 29 additions & 0 deletions .github/workflows/pr.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: pr

on:
push:
branches:
- main
pull_request:
branches:
- main

jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
with:
fetch-depth: 0

- name: Set up Go
uses: actions/setup-go@v1
with:
go-version: 1.20

- name: Go Build
run: go build -v ./...

- name: Go Test
run: go test -v ./...
3 changes: 3 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,19 @@ jobs:
uses: actions/checkout@v2
with:
fetch-depth: 0

- name: Set up Go
uses: actions/setup-go@v1
with:
go-version: 1.20

- name: Run GoReleaser
uses: goreleaser/goreleaser-action@v2
with:
version: latest
args: release --clean
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

- name: Create PR in krew-index
uses: rajatjindal/[email protected]
3 changes: 1 addition & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,8 @@ 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
)

Expand Down Expand Up @@ -38,6 +36,7 @@ require (
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
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
63 changes: 63 additions & 0 deletions pkg/common_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
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)
}
})
}
}
39 changes: 39 additions & 0 deletions pkg/outputs/table_test.go
Original file line number Diff line number Diff line change
@@ -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)
})
}
}
35 changes: 35 additions & 0 deletions pkg/utils/utils_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
})
}
}

0 comments on commit b9e366b

Please sign in to comment.