Skip to content

Commit

Permalink
update test
Browse files Browse the repository at this point in the history
  • Loading branch information
leon-inf committed Oct 24, 2023
1 parent 6ef9e69 commit 827f62d
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 16 deletions.
4 changes: 2 additions & 2 deletions pkg/controller/component/component_definition_convertor.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ func (c *compDefRuntimeConvertor) convert(args ...any) (any, error) {
return nil, fmt.Errorf("no pod spec")
}

podSpec := *clusterCompDef.PodSpec
podSpec := clusterCompDef.PodSpec.DeepCopy()
if clusterCompVer != nil {
for _, container := range clusterCompVer.VersionsCtx.InitContainers {
podSpec.InitContainers = appendOrOverrideContainerAttr(podSpec.InitContainers, container)
Expand All @@ -164,7 +164,7 @@ func (c *compDefRuntimeConvertor) convert(args ...any) (any, error) {
podSpec.Containers = appendOrOverrideContainerAttr(podSpec.Containers, container)
}
}
return podSpec, nil
return *podSpec, nil
}

func (c *compDefVolumesConvertor) convert(args ...any) (any, error) {
Expand Down
109 changes: 95 additions & 14 deletions pkg/controller/component/component_definition_convertor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ var _ = Describe("Component Definition Convertor", func() {
logVolumeName = "log"

defaultVolumeMode = int32(0555)

runAsUser = int64(0)
runAsNonRoot = false
)

BeforeEach(func() {
Expand Down Expand Up @@ -102,23 +105,68 @@ var _ = Describe("Component Definition Convertor", func() {
FilePathPattern: "/data/mysql/log/mysqld.log",
},
},
PodSpec: &corev1.PodSpec{},
PodSpec: &corev1.PodSpec{
Volumes: []corev1.Volume{},
Containers: []corev1.Container{
corev1.Container{
Name: "mysql",
Command: []string{"/entrypoint.sh"},
Env: []corev1.EnvVar{
corev1.EnvVar{
Name: "port",
Value: "3306",
},
},
VolumeMounts: []corev1.VolumeMount{
corev1.VolumeMount{
Name: dataVolumeName,
MountPath: "/data/mysql",
},
corev1.VolumeMount{
Name: logVolumeName,
MountPath: "/data/log",
},
},
Ports: []corev1.ContainerPort{
corev1.ContainerPort{
Name: "mysql",
ContainerPort: 3306,
},
corev1.ContainerPort{
Name: "paxos",
ContainerPort: 13306,
},
},
SecurityContext: &corev1.SecurityContext{
RunAsUser: &runAsUser,
RunAsNonRoot: &runAsNonRoot,
},
Lifecycle: &corev1.Lifecycle{
PreStop: &corev1.LifecycleHandler{
Exec: &corev1.ExecAction{
Command: []string{"/pre-stop.sh"},
},
},
},
},
},
},
Service: &appsv1alpha1.ServiceSpec{
Ports: []appsv1alpha1.ServicePort{
appsv1alpha1.ServicePort{
Name: "data",
Port: 3306,
TargetPort: intstr.IntOrString{
Type: intstr.Int,
IntVal: 3306,
Type: intstr.String,
StrVal: "mysql",
},
},
appsv1alpha1.ServicePort{
Name: "paxos",
Port: 13306,
TargetPort: intstr.IntOrString{
Type: intstr.Int,
IntVal: 13306,
Type: intstr.String,
StrVal: "paxos",
},
},
},
Expand Down Expand Up @@ -279,13 +327,33 @@ var _ = Describe("Component Definition Convertor", func() {
})

It("w/ comp version", func() {
// TODO(component)
clusterCompVer := &appsv1alpha1.ClusterComponentVersion{}
clusterCompVer := &appsv1alpha1.ClusterComponentVersion{
VersionsCtx: appsv1alpha1.VersionsContext{
InitContainers: []corev1.Container{
corev1.Container{
Name: "init",
Image: "init",
},
},
Containers: []corev1.Container{
corev1.Container{
Name: "mysql",
Image: "image",
},
},
},
}

convertor := &compDefRuntimeConvertor{}
res, err := convertor.convert(clusterCompDef, clusterCompVer)
Expect(err).Should(Succeed())
Expect(res).Should(BeEquivalentTo(*clusterCompDef.PodSpec))

expectedPodSpec := clusterCompDef.PodSpec
Expect(expectedPodSpec.Containers[0].Image).Should(BeEmpty())
Expect(expectedPodSpec.InitContainers).Should(HaveLen(0))
expectedPodSpec.Containers[0].Image = clusterCompVer.VersionsCtx.Containers[0].Image
expectedPodSpec.InitContainers = clusterCompVer.VersionsCtx.InitContainers
Expect(res).Should(BeEquivalentTo(*expectedPodSpec))
})
})

Expand Down Expand Up @@ -370,9 +438,8 @@ var _ = Describe("Component Definition Convertor", func() {

// headless service
Expect(services[1].ServiceName).Should(BeEquivalentTo(svcName + "-headless"))
// TODO(component): container ports
Expect(services[1].ServiceSpec.Ports).Should(HaveLen(len(clusterCompDef.Service.Ports)))
for i := range services[1].ServiceSpec.Ports {
Expect(len(services[1].ServiceSpec.Ports)).Should(Equal(len(clusterCompDef.Service.Ports) + len(clusterCompDef.PodSpec.Containers[0].Ports)))
for i := range clusterCompDef.Service.Ports {
Expect(services[1].ServiceSpec.Ports[i].Name).Should(Equal(clusterCompDef.Service.Ports[i].Name))
Expect(services[1].ServiceSpec.Ports[i].Port).Should(Equal(clusterCompDef.Service.Ports[i].Port))
Expect(services[1].ServiceSpec.Ports[i].TargetPort).Should(Equal(clusterCompDef.Service.Ports[i].TargetPort))
Expand All @@ -393,13 +460,27 @@ var _ = Describe("Component Definition Convertor", func() {
})

It("w/ comp version", func() {
// TODO(component)
clusterCompVer := &appsv1alpha1.ClusterComponentVersion{}
clusterCompVer := &appsv1alpha1.ClusterComponentVersion{
ConfigSpecs: []appsv1alpha1.ComponentConfigSpec{
appsv1alpha1.ComponentConfigSpec{
ComponentTemplateSpec: appsv1alpha1.ComponentTemplateSpec{
Name: "agamotto-config",
TemplateRef: "agamotto-config-template",
VolumeName: "agamotto-config",
DefaultMode: &defaultVolumeMode,
},
},
},
}

convertor := &compDefConfigsConvertor{}
res, err := convertor.convert(clusterCompDef, clusterCompVer)
Expect(err).Should(Succeed())
Expect(res).Should(BeEquivalentTo(clusterCompDef.ConfigSpecs))

expectedConfigs := make([]appsv1alpha1.ComponentConfigSpec, 0)
expectedConfigs = append(expectedConfigs, clusterCompVer.ConfigSpecs...)
expectedConfigs = append(expectedConfigs, clusterCompDef.ConfigSpecs...)
Expect(res).Should(BeEquivalentTo(expectedConfigs))
})
})

Expand Down

0 comments on commit 827f62d

Please sign in to comment.