Skip to content

Commit

Permalink
Tests (#1)
Browse files Browse the repository at this point in the history
* started writing tests

* add debug binary to gitignore

* fix trailing newlines

* remove unused func

* add test case for GetYamlKindName

* Make exit codes mean different things

* follow naming convention for test-funcs

* remove bogus password to make gitguardian happy
  • Loading branch information
arizon-dread authored Nov 4, 2024
1 parent 8c64790 commit e6e43fd
Show file tree
Hide file tree
Showing 4 changed files with 261 additions and 31 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
files/
yamls.yaml
split-kube-yamls
split-kube-yamls.exe
split-kube-yamls.exe
*/__debug_bin*
38 changes: 11 additions & 27 deletions helpers/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,20 @@ func splitStr(s string) []string {
}
for _, str := range strArr {
var each string
if strings.Contains(str, "kind: List") {
continue
}
for _, s := range strings.Split(str, "\n") {
each += strings.TrimPrefix(s, " ") + "\n"
}
found := true
beforeStr := ""
for found {
beforeStr, found = strings.CutSuffix(each, "\n")
if found {
each = beforeStr
}
}
result = append(result, "apiVersion:"+each)
}
} else {
Expand All @@ -55,39 +66,12 @@ func GetYamlKindName(y string) (string, string, error) {
return strings.ToLower(r.Kind), r.Metadata.Name, nil
}

func GetKindAndNameFromYaml(y string) (string, string) {
kind := ""
name := ""

s := bufio.NewScanner(strings.NewReader(y))
for s.Scan() {
if s.Text() == "" {
break
}
s := s.Text()
b, a, found := strings.Cut(s, ":")
if found && strings.Trim(strings.ToLower(b), " ") == "kind" {
kind = strings.Trim(a, " ")
}
if found && strings.Trim(strings.ToLower(b), " ") == "name" {
name = strings.Trim(a, " ")
}
if kind != "" && name != "" {
break
}
}

return kind, name

}

func ReadStdin() []string {
s := bufio.NewScanner(os.Stdin)
var str string
var l []string
for s.Scan() {
str += "\n" + s.Text()

}

l = splitStr(str)
Expand Down
244 changes: 244 additions & 0 deletions helpers/helpers_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,244 @@
package helpers

import (
"reflect"
"testing"
)

func TestSplitStr(t *testing.T) {
type args struct {
s string
}
//t1
t1_input := args{`apiVersion: apps/v1
kind: Deployment
metadata:
creationTimestamp: null
labels:
app: test-app
name: test-app
spec:
replicas: 1
selector:
matchLabels:
app: test-app
strategy: {}
template:
metadata:
creationTimestamp: null
labels:
app: test-app
spec:
containers:
- image: test-app:v1.0
name: test-app
resources: {}
ports:
- containerPort: 8080
protocol: TCP
name: api
volumeMounts:
- mountPath: /go/bin/confFile
name: config
volumes:
- name: config
configMap:
name: api-config
items:
- key: config.yaml
path: config.yaml
status: {}
---
apiVersion: v1
kind: Service
metadata:
creationTimestamp: null
labels:
app: test-app
name: test-app
spec:
ports:
- port: 8080
protocol: TCP
targetPort: 8080
selector:
app: test-app
status:
loadBalancer: {}`,
}
t1_expected := []string{`apiVersion: apps/v1
kind: Deployment
metadata:
creationTimestamp: null
labels:
app: test-app
name: test-app
spec:
replicas: 1
selector:
matchLabels:
app: test-app
strategy: {}
template:
metadata:
creationTimestamp: null
labels:
app: test-app
spec:
containers:
- image: test-app:v1.0
name: test-app
resources: {}
ports:
- containerPort: 8080
protocol: TCP
name: api
volumeMounts:
- mountPath: /go/bin/confFile
name: config
volumes:
- name: config
configMap:
name: api-config
items:
- key: config.yaml
path: config.yaml
status: {}`, `apiVersion: v1
kind: Service
metadata:
creationTimestamp: null
labels:
app: test-app
name: test-app
spec:
ports:
- port: 8080
protocol: TCP
targetPort: 8080
selector:
app: test-app
status:
loadBalancer: {}`}

//t2
t2_input := args{`apiVersion: v1
kind: List
items:
- apiVersion: v1
kind: Service
metadata:
name: list-service-test
spec:
ports:
- protocol: TCP
port: 80
selector:
app: list-deployment-test
- apiVersion: apps/v1
kind: Deployment
metadata:
name: list-deployment-test
labels:
app: list-deployment-test
spec:
replicas: 1
selector:
matchLabels:
app: list-deployment-test
template:
metadata:
labels:
app: list-deployment-test
spec:
containers:
- name: nginx
image: nginx`}
t2_expected := []string{`apiVersion: v1
kind: Service
metadata:
name: list-service-test
spec:
ports:
- protocol: TCP
port: 80
selector:
app: list-deployment-test`, `apiVersion: apps/v1
kind: Deployment
metadata:
name: list-deployment-test
labels:
app: list-deployment-test
spec:
replicas: 1
selector:
matchLabels:
app: list-deployment-test
template:
metadata:
labels:
app: list-deployment-test
spec:
containers:
- name: nginx
image: nginx`}

tests := []struct {
name string
args args
want []string
}{
{"t1_Does split into two parts", t1_input, t1_expected},
{"t2_Does split List into two parts", t2_input, t2_expected},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := splitStr(tt.args.s); !reflect.DeepEqual(got, tt.want) {
t.Errorf("splitStr() = %v, want %v", got, tt.want)
}
})
}
}

func TestGetYamlKindName(t *testing.T) {
type args struct {
y string
}
t1_input := `apiVersion: v1
kind: Service
metadata:
name: list-service-test
spec:
ports:
- protocol: TCP
port: 80
selector:
app: list-deployment-test`
t1_expected_kind := "service"
t1_expected_name := "list-service-test"

tests := []struct {
name string
args args
want string
want1 string
wantErr bool
}{
{"returns kind list and name list-service-test", args{t1_input}, t1_expected_kind, t1_expected_name, false},
{"returns err when no yaml supplied", args{"bogus yaml string"}, "", "", true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, got1, err := GetYamlKindName(tt.args.y)
if (err != nil) != tt.wantErr {
t.Errorf("GetYamlKindName() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got != tt.want {
t.Errorf("GetYamlKindName() got = %v, want %v", got, tt.want)
}
if got1 != tt.want1 {
t.Errorf("GetYamlKindName() got1 = %v, want %v", got1, tt.want1)
}
})
}
}
7 changes: 4 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func main() {

if tO == "" {
fmt.Printf("Output dir not supplied, specify with `-o path/to/dir`, quitting.\n")
os.Exit(2)
os.Exit(1)
} else {
//read os-specific path to os-agnostic path
o = filepath.ToSlash(tO)
Expand All @@ -47,7 +47,7 @@ func main() {
c, err := helpers.ReadYamlFileToStringArr(fn)
if err != nil {
fmt.Printf("unable to read file %v, quitting\n", fn)
os.Exit(2)
os.Exit(3)
}
y = c
}
Expand All @@ -62,7 +62,7 @@ func main() {

if len(y) < 2 {
fmt.Printf("no yaml supplied\n")
os.Exit(2)
os.Exit(4)
}

for _, str := range y {
Expand All @@ -84,5 +84,6 @@ func main() {
fmt.Printf("failed to write file %v, error: %v\n", file, werr)
}
}
os.Exit(0)

}

0 comments on commit e6e43fd

Please sign in to comment.