You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

146 lines
3.2 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package controllers
import (
"fmt"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
networkv1 "k8s.io/api/networking/v1"
"k8s.io/apimachinery/pkg/util/yaml"
myAppsv1 "mashibing.com/pkg/mashibing-deployment/api/v1"
"os"
"reflect"
"testing"
)
func readFile(fileName string) []byte {
content, err := os.ReadFile(fmt.Sprintf("testdata/%s", fileName))
if err != nil {
panic(err)
}
return content
}
func newMsbDeployment(fileName string) *myAppsv1.MsbDeployment {
content := readFile(fileName)
md := new(myAppsv1.MsbDeployment)
if err := yaml.Unmarshal(content, md); err != nil {
panic(err)
}
return md
}
func newDeployment(fileName string) *appsv1.Deployment {
content := readFile(fileName)
md := new(appsv1.Deployment)
if err := yaml.Unmarshal(content, md); err != nil {
panic(err)
}
return md
}
func newService(fileName string) *corev1.Service {
content := readFile(fileName)
md := new(corev1.Service)
if err := yaml.Unmarshal(content, md); err != nil {
panic(err)
}
return md
}
func newIngress(fileName string) *networkv1.Ingress {
content := readFile(fileName)
md := new(networkv1.Ingress)
if err := yaml.Unmarshal(content, md); err != nil {
panic(err)
}
return md
}
func TestNewDeployment(t *testing.T) {
type args struct {
md *myAppsv1.MsbDeployment
}
tests := []struct {
name string // 测试用例的名称
args args // 测试函数的参数
want *appsv1.Deployment // 期望的结果
}{
{
name: "测试使用ingress mode 时候生成Deployment资源。",
args: args{
md: newMsbDeployment("msb-ingress-cr.yaml"),
},
want: newDeployment("msb-ingress-deployment-expect.yaml"),
},
{
name: "测试使用nodeport mode 时候生成Deployment资源",
args: args{
md: newMsbDeployment("msb-nodeport-cr.yaml"),
},
want: newDeployment("msb-nodeport-deployment-expect.yaml"),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := NewDeployment(tt.args.md)
if !reflect.DeepEqual(got, *tt.want) {
t.Errorf("NewDeployment() got = %v, want %v", got, *tt.want)
}
})
}
}
func TestNewIngress(t *testing.T) {
type args struct {
md *myAppsv1.MsbDeployment
}
tests := []struct {
name string
args args
want *networkv1.Ingress
}{
{
name: "测试使用ingress mode 时候生成ingress资源",
args: args{
md: newMsbDeployment("msb-ingress-cr.yaml"),
},
want: newIngress("msb-ingress-ingress-expect.yaml"),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := NewIngress(tt.args.md)
if !reflect.DeepEqual(got, *tt.want) {
t.Errorf("NewIngress() got = %v, want %v", got, *tt.want)
}
})
}
}
func TestNewService(t *testing.T) {
type args struct {
md *myAppsv1.MsbDeployment
}
tests := []struct {
name string
args args
want *corev1.Service
}{
{
name: "测试使用ingress mode 时候,生成 service 资源",
args: args{
md: newMsbDeployment("msb-ingress-cr.yaml"),
},
want: newService("msb-ingress-service-expect.yaml"),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := NewService(tt.args.md)
if !reflect.DeepEqual(got, *tt.want) {
t.Errorf("NewService() got = %v, want %v", got, *tt.want)
}
})
}
}