This is an automated email from the ASF dual-hosted git repository.
tuichenchuxin pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/shardingsphere-on-cloud.git
The following commit(s) were added to refs/heads/main by this push:
new ac0343f chore: add unit test for reconcile ConstructCascadingService
new 8407647 Merge pull request #123 from mlycore/add-ut
ac0343f is described below
commit ac0343fc6757ea857f5b7b426051b2dc9223fcc5
Author: mlycore <[email protected]>
AuthorDate: Wed Nov 30 14:10:00 2022 +0800
chore: add unit test for reconcile ConstructCascadingService
Signed-off-by: mlycore <[email protected]>
---
.../pkg/reconcile/reconcile_test.go | 61 ++++++++++++++++++++++
shardingsphere-operator/pkg/reconcile/resource.go | 3 ++
2 files changed, 64 insertions(+)
diff --git a/shardingsphere-operator/pkg/reconcile/reconcile_test.go
b/shardingsphere-operator/pkg/reconcile/reconcile_test.go
index ab6dd55..6dbad0d 100644
--- a/shardingsphere-operator/pkg/reconcile/reconcile_test.go
+++ b/shardingsphere-operator/pkg/reconcile/reconcile_test.go
@@ -463,7 +463,68 @@ func Test_ConstructCascadingDeployment(t *testing.T) {
}
func Test_ConstructCascadingService(t *testing.T) {
+ cases := []struct {
+ proxy *v1alpha1.ShardingSphereProxy
+ exp *v1.Service
+ message string
+ }{
+ {
+ exp: &v1.Service{},
+ message: "Nil ShardingSphereProxy definition should
lead to empty Service",
+ },
+ {
+ proxy: &v1alpha1.ShardingSphereProxy{},
+ exp: &v1.Service{},
+ message: "Empty ShardingSphereProxy definition should
lead to empty Service",
+ },
+ {
+ proxy: &v1alpha1.ShardingSphereProxy{
+ ObjectMeta: metav1.ObjectMeta{
+ Name: "testname",
+ Namespace: "testnamespace",
+ },
+ Spec: v1alpha1.ProxySpec{
+ ServiceType: v1alpha1.ServiceType{
+ Type:
v1.ServiceTypeNodePort,
+ NodePort: 33007,
+ },
+ Port: 3307,
+ },
+ },
+ exp: &v1.Service{
+ ObjectMeta: metav1.ObjectMeta{
+ Name: "testname",
+ Namespace: "testnamespace",
+ },
+ Spec: v1.ServiceSpec{
+ Selector: map[string]string{
+ "apps": "testname",
+ },
+ Type: v1.ServiceTypeNodePort,
+ Ports: []v1.ServicePort{
+ {
+ Name:
"proxy-port",
+ TargetPort:
fromInt32(3307),
+ Port: 3307,
+ NodePort: 33007,
+ },
+ },
+ },
+ },
+ message: "Normal ShardingSphereProxy definition should
lead to normal Service",
+ },
+ }
+ for _, c := range cases {
+ act := ConstructCascadingService(c.proxy)
+ assert.Equal(t, c.exp.ObjectMeta.Name, act.ObjectMeta.Name,
c.message)
+ assert.Equal(t, c.exp.ObjectMeta.Namespace,
act.ObjectMeta.Namespace, c.message)
+ if c.proxy != nil {
+ assert.EqualValues(t, c.exp.Spec.Selector,
act.Spec.Selector, c.message)
+ assert.Equal(t, c.exp.Spec.Type, act.Spec.Type,
c.message)
+ assert.Equal(t, c.exp.Spec.Ports, act.Spec.Ports,
c.message)
+ }
+ }
}
func Test_addInitContaienr(t *testing.T) {
diff --git a/shardingsphere-operator/pkg/reconcile/resource.go
b/shardingsphere-operator/pkg/reconcile/resource.go
index 66f0be0..3ace3d5 100644
--- a/shardingsphere-operator/pkg/reconcile/resource.go
+++ b/shardingsphere-operator/pkg/reconcile/resource.go
@@ -159,6 +159,9 @@ func ConstructCascadingDeployment(proxy
*v1alpha1.ShardingSphereProxy) *appsv1.D
}
func ConstructCascadingService(proxy *v1alpha1.ShardingSphereProxy)
*v1.Service {
+ if proxy == nil || reflect.DeepEqual(proxy,
&v1alpha1.ShardingSphereProxy{}) {
+ return &v1.Service{}
+ }
svc := v1.Service{
ObjectMeta: metav1.ObjectMeta{