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.

198 lines
4.6 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 // 期望的结果
wantErr bool // 我们进行测试时候函数是否需要出错
}{
{
name: "测试使用ingress mode 时候生成Deployment资源。",
args: args{
md: newMsbDeployment("msb-ingress-cr.yaml"),
},
want: newDeployment("msb-ingress-deployment-expect.yaml"),
wantErr: false,
},
{
name: "测试使用nodeport mode 时候生成Deployment资源",
args: args{
md: newMsbDeployment("msb-nodeport-cr.yaml"),
},
want: newDeployment("msb-nodeport-deployment-expect.yaml"),
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := NewDeployment(tt.args.md)
if (err != nil) != tt.wantErr {
t.Errorf("NewDeployment() error = %v, wantErr %v", err, tt.wantErr)
return
}
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
wantErr bool
}{
{
name: "测试使用ingress mode 时候生成ingress资源",
args: args{
md: newMsbDeployment("msb-ingress-cr.yaml"),
},
want: newIngress("msb-ingress-ingress-expect.yaml"),
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := NewIngress(tt.args.md)
if (err != nil) != tt.wantErr {
t.Errorf("NewIngress() error = %v, wantErr %v", err, tt.wantErr)
return
}
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
wantErr bool
}{
{
name: "测试使用ingress mode 时候,生成 service 资源",
args: args{
md: newMsbDeployment("msb-ingress-cr.yaml"),
},
want: newService("msb-ingress-service-expect.yaml"),
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := NewService(tt.args.md)
if (err != nil) != tt.wantErr {
t.Errorf("NewService() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("NewService() got = %v, want %v", got, tt.want)
}
})
}
}
func TestNewServiceNP(t *testing.T) {
type args struct {
md *myAppsv1.MsbDeployment
}
tests := []struct {
name string
args args
want *corev1.Service
wantErr bool
}{
{
name: "测试使用 nodeport mode时候生成 nodeport 类型的 service 资源",
args: args{
md: newMsbDeployment("msb-nodeport-cr.yaml"),
},
want: newService("msb-nodeport-service-expect.yaml"),
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := NewServiceNP(tt.args.md)
if (err != nil) != tt.wantErr {
t.Errorf("NewServiceNP() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("NewServiceNP() got = %v, want %v", got, tt.want)
}
})
}
}