Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added gotest templates, changed unit test naming #1621

Merged
merged 2 commits into from
Aug 16, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions contrib/gotests_templates/call.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{define "call"}}{{with .Receiver}}{{if not .IsStruct}}tt.{{end}}{{Receiver .}}.{{end}}{{.Name}}({{range $i, $el := .Parameters}}{{if $i}}, {{end}}{{if not .IsWriter}}tt.args.{{end}}{{Param .}}{{if .Type.IsVariadic}}...{{end}}{{end}}){{end}}
107 changes: 107 additions & 0 deletions contrib/gotests_templates/function.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
{{define "function"}}
{{- $f := .}}

func Test_Unit{{.FullName}}(t *testing.T) {
{{- with .Receiver}}
{{- if .IsStruct}}
{{- if .Fields}}
type fields struct {
{{- range .Fields}}
{{Field .}} {{.Type}}
{{- end}}
}
{{- end}}
{{- end}}
{{- end}}
{{- if .TestParameters}}
type args struct {
{{- range .TestParameters}}
{{Param .}} {{.Type}}
{{- end}}
}
{{- end}}
tests := []struct {
name string
{{- with .Receiver}}
{{- if and .IsStruct .Fields}}
fields fields
{{- else}}
{{Receiver .}} {{.Type}}
{{- end}}
{{- end}}
{{- if .TestParameters}}
args args
{{- end}}
setup func() error // Optional, delete if unused
teardown func() error // Optional, delete if unused
{{- range .TestResults}}
{{Want .}} {{.Type}}
{{- end}}
{{- if .ReturnsError}}
wantErr bool
{{- end}}
}{
// TODO: Add test cases.
// {
// name: "Example Test",
// args: args {
// },
// {{- range .TestResults}}
// want: {{Want .}} {{.Type}}
// {{- end}}
// setup: func() error { return nil },
// teardown: func() error { return nil },
// },
}
for {{if (or .Subtests (not .IsNaked))}} _, tt := {{end}} range tests {
{{- if .Subtests}}
{{- if .Parallel}}tt := tt{{end}}
t.Run(tt.name, func(t *testing.T) {
{{- if .Parallel}}t.Parallel(){{end}}
{{- end}}
{{- with .Receiver}}
{{- if .IsStruct}}
{{Receiver .}} := {{if .Type.IsStar}}&{{end}}{{.Type.Value}}{
{{- range .Fields}}
{{.Name}}: tt.fields.{{Field .}},
{{- end}}
}
{{- end}}
{{- end}}
{{- range .Parameters}}
{{- if .IsWriter}}
{{Param .}} := &bytes.Buffer{}
{{- end}}
{{- end}}
defer tt.teardown()
if err := tt.setup(); err != nil {
t.Errorf("Setup for {{template "message" $f}} failed = %v", err)
return
}
{{- if and (not .OnlyReturnsError) (not .OnlyReturnsOneValue) }}
{{template "results" $f}} {{template "call" $f}}
{{- end}}
{{- if .ReturnsError}}
if {{if .OnlyReturnsError}} err := {{template "call" $f}}; {{end}} (err != nil) != tt.wantErr {
t.Errorf("{{template "message" $f}} error = %v, wantErr %v", {{template "inputs" $f}} err, tt.wantErr)
{{- if .TestResults}}
{{if .Subtests }}return{{else}}continue{{end}}
{{- end}}
}
{{- end}}
{{- range .TestResults}}
{{- if .IsWriter}}
if {{Got .}} := {{Param .}}.String(); {{Got .}} != tt.{{Want .}} {
{{- else if .IsBasicType}}
if {{if $f.OnlyReturnsOneValue}}{{Got .}} := {{template "inline" $f}}; {{end}} {{Got .}} != tt.{{Want .}} {
{{- else}}
if {{if $f.OnlyReturnsOneValue}}{{Got .}} := {{template "inline" $f}}; {{end}} !reflect.DeepEqual({{Got .}}, tt.{{Want .}}) {
{{- end}}
t.Errorf("{{template "message" $f}} {{if $f.ReturnsMultiple}}{{Got .}} {{end}}= %+v\nWant = %+v", {{template "inputs" $f}} {{Got .}}, tt.{{Want .}})
}
{{- end}}
{{- if .Subtests }} }) {{- end -}}
}
}

{{end}}
10 changes: 10 additions & 0 deletions contrib/gotests_templates/header.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{{define "header"}}
{{range .Comments}}{{.}}
{{end -}}
package {{.Package}}

import (
{{range .Imports}}{{.Name}} {{.Path}}
{{end}}
)
{{end}}
1 change: 1 addition & 0 deletions contrib/gotests_templates/inline.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{define "inline"}} {{template "call" .}} {{end}}
1 change: 1 addition & 0 deletions contrib/gotests_templates/inputs.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{define "inputs"}}{{$f := .}}{{if not .Subtests}}tt.name, {{end}}{{if $f.PrintInputs}}{{range $f.Parameters}}tt.args.{{Param .}}, {{end}}{{end}}{{end}}
3 changes: 3 additions & 0 deletions contrib/gotests_templates/message.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{{define "message" -}}
{{if not .Subtests}}%q. {{end}}{{with .Receiver}}{{.Type.Value}}.{{end}}{{.Name}}({{if .PrintInputs}}{{range $i, $el := .Parameters}}{{if $i}}, {{end}}%v{{end}}{{end}})
{{- end}}
1 change: 1 addition & 0 deletions contrib/gotests_templates/results.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{define "results"}} {{range $i, $el := .Results}}{{if $i}}, {{end}}{{Got .}}{{end}}{{if .ReturnsError}}, err{{end}} {{if or .Results .ReturnsError}} := {{end}} {{end}}
137 changes: 137 additions & 0 deletions pkg/images/images_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
package images

import (
"io/ioutil"
"os"
"testing"
)

var testDefaultImages Images
dereknola marked this conversation as resolved.
Show resolved Hide resolved

func Test_Unitoverride(t *testing.T) {
type args struct {
defaultValue string
overrideValue string
}
tests := []struct {
name string
args args
want string
}{
{
name: "override has only spaces",
args: args{
defaultValue: "defaultCase",
overrideValue: " ",
},

want: "defaultCase",
},
{
name: "override has extra spaces",
args: args{
defaultValue: "defaultCase",
overrideValue: " caseWithSpaces ",
},

want: "caseWithSpaces",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := override(tt.args.defaultValue, tt.args.overrideValue); got != tt.want {
t.Errorf("override() = %+v\nWant = %+v", got, tt.want)
}
})
}
}

func Test_UnitPull(t *testing.T) {
type args struct {
dir string
name string
image string
}
tests := []struct {
name string
args args
setup func(a *args) error
teardown func(a *args) error
wantTxtFile bool
wantErr bool
}{
{
name: "Pull with no directory",
args: args{
name: "kube-scheduler",
image: testDefaultImages.KubeScheduler,
},
setup: func(a *args) error { return nil },
teardown: func(a *args) error { return nil },
},
{
name: "Pull with nonexistent directory",
args: args{
dir: "/tmp/DEADBEEF",
},
setup: func(a *args) error { return nil },
teardown: func(a *args) error {
return os.RemoveAll(a.dir)
},
},
{
name: "Pull with no image in directory",
args: args{
name: "kube-scheduler",
image: testDefaultImages.KubeScheduler,
},
setup: func(a *args) error {
var err error
a.dir, err = os.MkdirTemp("", "*")
return err
},
teardown: func(a *args) error {
return os.RemoveAll(a.dir)
},

wantTxtFile: true,
},
{
name: "Pull with fake image in directory",
args: args{
name: "kube-scheduler",
image: testDefaultImages.KubeScheduler,
},
setup: func(a *args) error {
var err error
a.dir, err = os.MkdirTemp("", "*")
tempImage := a.dir + "/" + a.name + ".image"
ioutil.WriteFile(tempImage, []byte(a.image+"\n"), 0644)
return err
},
teardown: func(a *args) error {
return os.RemoveAll(a.dir)
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {

if err := tt.setup(&tt.args); err != nil {
t.Errorf("Setup for Pull() failed = %v", err)
}
if err := Pull(tt.args.dir, tt.args.name, tt.args.image); (err != nil) != tt.wantErr {
t.Errorf("Pull() error = %v, wantErr %v", err, tt.wantErr)
}
if tt.wantTxtFile {
fileName := tt.args.name + ".txt"
if _, err := os.Stat(tt.args.dir + "/" + fileName); os.IsNotExist(err) {
t.Errorf("File generate by Pull() %s, does not exists, wantFile %v", fileName, tt.wantTxtFile)
}
}
if err := tt.teardown(&tt.args); err != nil {
t.Errorf("Teardown for Pull() failed = %v", err)
}
})
}
}
8 changes: 4 additions & 4 deletions pkg/rke2/psp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ func fakeWithRetriableError(ro interface{}) *fake.Clientset {
return cs
}

func Test_deployPodSecurityPolicyFromYaml(t *testing.T) {
func Test_UnitdeployPodSecurityPolicyFromYaml(t *testing.T) {
pspYAML := fmt.Sprintf(globalRestrictedPSPTemplate, testPSPName)
type args struct {
ctx context.Context
Expand Down Expand Up @@ -207,7 +207,7 @@ func Test_deployPodSecurityPolicyFromYaml(t *testing.T) {
}
}

func Test_deployClusterRoleBindingFromYaml(t *testing.T) {
func Test_UnitdeployClusterRoleBindingFromYaml(t *testing.T) {
clusterRoleBindingYaml := fmt.Sprintf(kubeletAPIServerRoleBindingTemplate, testClusterRoleBindingName)
type args struct {
ctx context.Context
Expand Down Expand Up @@ -274,7 +274,7 @@ func Test_deployClusterRoleBindingFromYaml(t *testing.T) {
}
}

func Test_deployClusterRoleFromYaml(t *testing.T) {
func Test_UnitdeployClusterRoleFromYaml(t *testing.T) {
const testResourceName = "test-resource-name"
clusterRoleYaml := fmt.Sprintf(roleTemplate, "test-cluster-role", testResourceName)
type args struct {
Expand Down Expand Up @@ -342,7 +342,7 @@ func Test_deployClusterRoleFromYaml(t *testing.T) {
}
}

func Test_deployRoleBindingFromYaml(t *testing.T) {
func Test_UnitdeployRoleBindingFromYaml(t *testing.T) {
roleBindingYaml := fmt.Sprintf(tunnelControllerRoleTemplate, testRoleBindingName)
type args struct {
ctx context.Context
Expand Down
2 changes: 1 addition & 1 deletion pkg/rke2/serviceaccount_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func addClientReactors(cs *fake.Clientset, verb string, pass bool) *fake.Clients
return cs
}

func Test_restrictServiceAccount(t *testing.T) {
func Test_UnitrestrictServiceAccount(t *testing.T) {
type args struct {
ctx context.Context
namespace string
Expand Down