From 3b814ac7a06e7fd9f8a835e87a99668f3db776ff Mon Sep 17 00:00:00 2001 From: Miguel Caballer Date: Mon, 7 Oct 2024 13:15:43 +0200 Subject: [PATCH 1/7] Add tests for expose --- pkg/types/expose.go | 21 ++++---- pkg/types/expose_test.go | 106 +++++++++++++++++++++++++++++++++++++++ pkg/types/service.go | 22 ++++---- 3 files changed, 127 insertions(+), 22 deletions(-) create mode 100644 pkg/types/expose_test.go diff --git a/pkg/types/expose.go b/pkg/types/expose.go index 0be38895..cb7be89c 100644 --- a/pkg/types/expose.go +++ b/pkg/types/expose.go @@ -246,7 +246,7 @@ func getHortizontalAutoScaleSpec(service Service, cfg *Config) *autos.Horizontal func getPodTemplateSpec(service Service, cfg *Config) v1.PodTemplateSpec { podSpec, _ := service.ToPodSpec(cfg) - for i, _ := range podSpec.Containers { + for i := range podSpec.Containers { podSpec.Containers[i].Ports = []v1.ContainerPort{ { Name: podPortName, @@ -414,7 +414,7 @@ func deleteService(name string, kubeClientset kubernetes.Interface, cfg *Config) func createIngress(service Service, kubeClientset kubernetes.Interface, cfg *Config) error { // Create Secret - ingress := getIngressSpec(service, kubeClientset, cfg) + ingress := getIngressSpec(service, cfg) _, err := kubeClientset.NetworkingV1().Ingresses(cfg.ServicesNamespace).Create(context.TODO(), ingress, metav1.CreateOptions{}) if err != nil { return err @@ -432,7 +432,7 @@ func updateIngress(service Service, kubeClientset kubernetes.Interface, cfg *Con //if exist continue and need -> Update //if exist and not need -> delete //if not exist create - kube_ingress := getIngressSpec(service, kubeClientset, cfg) + kube_ingress := getIngressSpec(service, cfg) _, err := kubeClientset.NetworkingV1().Ingresses(cfg.ServicesNamespace).Update(context.TODO(), kube_ingress, metav1.UpdateOptions{}) if err != nil { return err @@ -455,7 +455,7 @@ func updateIngress(service Service, kubeClientset kubernetes.Interface, cfg *Con } // Return a kubernetes ingress component, ready to deploy or update -func getIngressSpec(service Service, kubeClientset kubernetes.Interface, cfg *Config) *net.Ingress { +func getIngressSpec(service Service, cfg *Config) *net.Ingress { name_ingress := getIngressName(service.Name) pathofapi := getAPIPath(service.Name) name_service := getServiceName(service.Name) @@ -554,7 +554,7 @@ func deleteIngress(name string, kubeClientset kubernetes.Interface, cfg *Config) // Secret func createSecret(service Service, kubeClientset kubernetes.Interface, cfg *Config) error { - secret := getSecretSpec(service, kubeClientset, cfg) + secret := getSecretSpec(service, cfg) _, err := kubeClientset.CoreV1().Secrets(cfg.ServicesNamespace).Create(context.TODO(), secret, metav1.CreateOptions{}) if err != nil { return err @@ -563,7 +563,7 @@ func createSecret(service Service, kubeClientset kubernetes.Interface, cfg *Conf } func updateSecret(service Service, kubeClientset kubernetes.Interface, cfg *Config) error { - secret := getSecretSpec(service, kubeClientset, cfg) + secret := getSecretSpec(service, cfg) _, err := kubeClientset.CoreV1().Secrets(cfg.ServicesNamespace).Update(context.TODO(), secret, metav1.UpdateOptions{}) if err != nil { return err @@ -579,12 +579,12 @@ func deleteSecret(name string, kubeClientset kubernetes.Interface, cfg *Config) } return nil } -func getSecretSpec(service Service, kubeClientset kubernetes.Interface, cfg *Config) *v1.Secret { +func getSecretSpec(service Service, cfg *Config) *v1.Secret { //setPassword hash := make(htpasswd.HashedPasswords) err := hash.SetPassword(service.Name, service.Token, htpasswd.HashAPR1) if err != nil { - ExposeLogger.Printf(err.Error()) + ExposeLogger.Print(err.Error()) } //Create Secret inmutable := false @@ -620,10 +620,7 @@ func existsSecret(serviceName string, kubeClientset kubernetes.Interface, cfg *C func existsIngress(serviceName string, namespace string, kubeClientset kubernetes.Interface) bool { _, err := kubeClientset.NetworkingV1().Ingresses(namespace).Get(context.TODO(), getIngressName(serviceName), metav1.GetOptions{}) - if err == nil { - return true - } - return false + return err == nil } /// These are auxiliary functions diff --git a/pkg/types/expose_test.go b/pkg/types/expose_test.go new file mode 100644 index 00000000..3121a2a2 --- /dev/null +++ b/pkg/types/expose_test.go @@ -0,0 +1,106 @@ +/* +Copyright (C) GRyCAP - I3M - UPV + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + +http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package types + +import ( + "testing" + + testclient "k8s.io/client-go/kubernetes/fake" +) + +func TestCreateExpose(t *testing.T) { + + kubeClientset := testclient.NewSimpleClientset() + + service := Service{ + Name: "test-service", + Expose: Expose{ + MinScale: 1, + MaxScale: 3, + CpuThreshold: 80, + }, + } + cfg := &Config{Namespace: "namespace"} + + err := CreateExpose(service, kubeClientset, cfg) + + if err != nil { + t.Errorf("Error creating expose: %v", err) + } + + actions := kubeClientset.Actions() + if len(actions) != 4 { + t.Errorf("Expected 4 actions but got %d", len(actions)) + } + + if actions[0].GetVerb() != "create" || actions[0].GetResource().Resource != "deployments" { + t.Errorf("Expected create deployment action but got %v", actions[0]) + } + + if actions[1].GetVerb() != "create" || actions[1].GetResource().Resource != "horizontalpodautoscalers" { + t.Errorf("Expected create horizontalpodautoscalers action but got %v", actions[1]) + } + + if actions[2].GetVerb() != "create" || actions[2].GetResource().Resource != "services" { + t.Errorf("Expected create service action but got %v", actions[2]) + } + + if actions[3].GetVerb() != "create" || actions[3].GetResource().Resource != "ingresses" { + t.Errorf("Expected create ingress action but got %v", actions[3]) + } +} + +func TestServiceSpec(t *testing.T) { + + service := Service{ + Name: "test-service", + Expose: Expose{ + MinScale: 1, + MaxScale: 3, + CpuThreshold: 40, + APIPort: 8080, + }, + } + cfg := &Config{Namespace: "namespace"} + res := getServiceSpec(service, cfg) + if res.Spec.Ports[0].TargetPort.IntVal != 8080 { + t.Errorf("Expected port 8080 but got %d", res.Spec.Ports[0].Port) + } +} + +func TestHortizontalAutoScaleSpec(t *testing.T) { + + service := Service{ + Name: "test-service", + Expose: Expose{ + MinScale: 1, + MaxScale: 3, + CpuThreshold: 40, + }, + } + cfg := &Config{Namespace: "namespace"} + res := getHortizontalAutoScaleSpec(service, cfg) + if *res.Spec.MinReplicas != 1 { + t.Errorf("Expected min replicas 1 but got %d", res.Spec.MinReplicas) + } + if res.Spec.MaxReplicas != 3 { + t.Errorf("Expected max replicas 3 but got %d", res.Spec.MaxReplicas) + } + if *res.Spec.TargetCPUUtilizationPercentage != 40 { + t.Errorf("Expected target cpu 40 but got %d", res.Spec.TargetCPUUtilizationPercentage) + } +} diff --git a/pkg/types/service.go b/pkg/types/service.go index 0fc8c157..c7d071f8 100644 --- a/pkg/types/service.go +++ b/pkg/types/service.go @@ -108,6 +108,17 @@ const ( // YAMLMarshal package-level yaml marshal function var YAMLMarshal = yaml.Marshal +type Expose struct { + MinScale int32 `json:"min_scale" default:"1"` + MaxScale int32 `json:"max_scale" default:"10"` + APIPort int `json:"api_port,omitempty" ` + CpuThreshold int32 `json:"cpu_threshold" default:"80" ` + RewriteTarget bool `json:"rewrite_target" default:"false" ` + NodePort int32 `json:"nodePort" default:"0" ` + DefaultCommand bool `json:"default_command" ` + SetAuth bool `json:"set_auth" ` +} + // Service represents an OSCAR service following the SCAR Function Definition Language type Service struct { // Name the name of the service @@ -206,16 +217,7 @@ type Service struct { // Optional ImagePullSecrets []string `json:"image_pull_secrets,omitempty"` - Expose struct { - MinScale int32 `json:"min_scale" default:"1"` - MaxScale int32 `json:"max_scale" default:"10"` - APIPort int `json:"api_port,omitempty" ` - CpuThreshold int32 `json:"cpu_threshold" default:"80" ` - RewriteTarget bool `json:"rewrite_target" default:"false" ` - NodePort int32 `json:"nodePort" default:"0" ` - DefaultCommand bool `json:"default_command" ` - SetAuth bool `json:"set_auth" ` - } `json:"expose"` + Expose Expose `json:"expose"` // The user-defined environment variables assigned to the service // Optional From 05a1a379b4fb1d8af4be723d7a33c87244f11301 Mon Sep 17 00:00:00 2001 From: Miguel Caballer Date: Mon, 7 Oct 2024 13:49:26 +0200 Subject: [PATCH 2/7] Add tests for expose --- pkg/types/expose_test.go | 65 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 64 insertions(+), 1 deletion(-) diff --git a/pkg/types/expose_test.go b/pkg/types/expose_test.go index 3121a2a2..12db9fdb 100644 --- a/pkg/types/expose_test.go +++ b/pkg/types/expose_test.go @@ -19,6 +19,11 @@ package types import ( "testing" + v1 "k8s.io/api/apps/v1" + autoscalingv1 "k8s.io/api/autoscaling/v1" + corev1 "k8s.io/api/core/v1" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/runtime" testclient "k8s.io/client-go/kubernetes/fake" ) @@ -34,7 +39,7 @@ func TestCreateExpose(t *testing.T) { CpuThreshold: 80, }, } - cfg := &Config{Namespace: "namespace"} + cfg := &Config{ServicesNamespace: "namespace"} err := CreateExpose(service, kubeClientset, cfg) @@ -64,6 +69,64 @@ func TestCreateExpose(t *testing.T) { } } +func TestDeleteExpose(t *testing.T) { + + K8sObjects := []runtime.Object{ + &autoscalingv1.HorizontalPodAutoscaler{ + ObjectMeta: metav1.ObjectMeta{ + Name: "service-hpa", + Namespace: "namespace", + }, + }, + &v1.Deployment{ + ObjectMeta: metav1.ObjectMeta{ + Name: "service-dlp", + Namespace: "namespace", + }, + }, + &corev1.Service{ + ObjectMeta: metav1.ObjectMeta{ + Name: "service-svc", + Namespace: "namespace", + }, + }, + } + + kubeClientset := testclient.NewSimpleClientset(K8sObjects...) + cfg := &Config{ServicesNamespace: "namespace"} + + err := DeleteExpose("service", kubeClientset, cfg) + + if err != nil { + t.Errorf("Error creating expose: %v", err) + } + + actions := kubeClientset.Actions() + if len(actions) != 5 { + t.Errorf("Expected 2 actions but got %d", len(actions)) + } + + if actions[0].GetVerb() != "delete" || actions[0].GetResource().Resource != "horizontalpodautoscalers" { + t.Errorf("Expected delete horizontalpodautoscalers action but got %v", actions[0]) + } + + if actions[1].GetVerb() != "delete" || actions[1].GetResource().Resource != "deployments" { + t.Errorf("Expected delete deployment action but got %v", actions[1]) + } + + if actions[2].GetVerb() != "delete" || actions[2].GetResource().Resource != "services" { + t.Errorf("Expected delete services action but got %v", actions[2]) + } + + if actions[3].GetVerb() != "get" || actions[3].GetResource().Resource != "ingresses" { + t.Errorf("Expected get ingresses action but got %v", actions[3]) + } + + if actions[4].GetVerb() != "delete-collection" || actions[4].GetResource().Resource != "pods" { + t.Errorf("Expected delete-collection pods action but got %v", actions[4]) + } +} + func TestServiceSpec(t *testing.T) { service := Service{ From 654715cb7eaa9d07de73fa3c3dc3cf4602ffa770 Mon Sep 17 00:00:00 2001 From: Miguel Caballer Date: Mon, 7 Oct 2024 14:02:05 +0200 Subject: [PATCH 3/7] Add tests for expose --- pkg/types/expose_test.go | 60 +++++++++++++++++++--------------------- 1 file changed, 29 insertions(+), 31 deletions(-) diff --git a/pkg/types/expose_test.go b/pkg/types/expose_test.go index 12db9fdb..62f3a250 100644 --- a/pkg/types/expose_test.go +++ b/pkg/types/expose_test.go @@ -27,6 +27,12 @@ import ( testclient "k8s.io/client-go/kubernetes/fake" ) +type Action struct { + Verb string + Resource string + ObjectName string +} + func TestCreateExpose(t *testing.T) { kubeClientset := testclient.NewSimpleClientset() @@ -48,24 +54,21 @@ func TestCreateExpose(t *testing.T) { } actions := kubeClientset.Actions() - if len(actions) != 4 { - t.Errorf("Expected 4 actions but got %d", len(actions)) - } - - if actions[0].GetVerb() != "create" || actions[0].GetResource().Resource != "deployments" { - t.Errorf("Expected create deployment action but got %v", actions[0]) - } - - if actions[1].GetVerb() != "create" || actions[1].GetResource().Resource != "horizontalpodautoscalers" { - t.Errorf("Expected create horizontalpodautoscalers action but got %v", actions[1]) + expected_actions := []Action{ + {Verb: "create", Resource: "deployments"}, + {Verb: "create", Resource: "horizontalpodautoscalers"}, + {Verb: "create", Resource: "services"}, + {Verb: "create", Resource: "ingresses"}, } - if actions[2].GetVerb() != "create" || actions[2].GetResource().Resource != "services" { - t.Errorf("Expected create service action but got %v", actions[2]) + if len(actions) != len(expected_actions) { + t.Errorf("Expected %d actions but got %d", (len(expected_actions)), len(actions)) } - if actions[3].GetVerb() != "create" || actions[3].GetResource().Resource != "ingresses" { - t.Errorf("Expected create ingress action but got %v", actions[3]) + for i, action := range actions { + if action.GetVerb() != expected_actions[i].Verb || action.GetResource().Resource != expected_actions[i].Resource { + t.Errorf("Expected %v action but got %v", expected_actions[i], action) + } } } @@ -102,28 +105,23 @@ func TestDeleteExpose(t *testing.T) { } actions := kubeClientset.Actions() - if len(actions) != 5 { - t.Errorf("Expected 2 actions but got %d", len(actions)) - } - - if actions[0].GetVerb() != "delete" || actions[0].GetResource().Resource != "horizontalpodautoscalers" { - t.Errorf("Expected delete horizontalpodautoscalers action but got %v", actions[0]) - } - - if actions[1].GetVerb() != "delete" || actions[1].GetResource().Resource != "deployments" { - t.Errorf("Expected delete deployment action but got %v", actions[1]) - } - if actions[2].GetVerb() != "delete" || actions[2].GetResource().Resource != "services" { - t.Errorf("Expected delete services action but got %v", actions[2]) + expected_actions := []Action{ + {Verb: "delete", Resource: "horizontalpodautoscalers", ObjectName: "service-hpa"}, + {Verb: "delete", Resource: "deployments", ObjectName: "service-dlp"}, + {Verb: "delete", Resource: "services", ObjectName: "service-svc"}, + {Verb: "get", Resource: "ingresses", ObjectName: "service-ing"}, + {Verb: "delete-collection", Resource: "pods", ObjectName: "service-pod"}, } - if actions[3].GetVerb() != "get" || actions[3].GetResource().Resource != "ingresses" { - t.Errorf("Expected get ingresses action but got %v", actions[3]) + if len(actions) != len(expected_actions) { + t.Errorf("Expected %d actions but got %d", (len(expected_actions)), len(actions)) } - if actions[4].GetVerb() != "delete-collection" || actions[4].GetResource().Resource != "pods" { - t.Errorf("Expected delete-collection pods action but got %v", actions[4]) + for i, action := range actions { + if action.GetVerb() != expected_actions[i].Verb || action.GetResource().Resource != expected_actions[i].Resource { + t.Errorf("Expected %v action but got %v", expected_actions[i], action) + } } } From 96b7f8d87e6d5b810e5fe934de83a3878769c533 Mon Sep 17 00:00:00 2001 From: Miguel Caballer Date: Tue, 8 Oct 2024 12:31:56 +0200 Subject: [PATCH 4/7] Improve tests --- pkg/types/expose.go | 2 +- pkg/types/expose_test.go | 103 ++++++++++++++++++++++++++++++--------- 2 files changed, 82 insertions(+), 23 deletions(-) diff --git a/pkg/types/expose.go b/pkg/types/expose.go index cb7be89c..6ce2499c 100644 --- a/pkg/types/expose.go +++ b/pkg/types/expose.go @@ -153,7 +153,7 @@ func UpdateExpose(service Service, kubeClientset kubernetes.Interface, cfg *Conf // TODO check and refactor // Main function that list all the kubernetes components // This function is not used, in the future could be usefull -func ListExpose(service Service, kubeClientset kubernetes.Interface, cfg *Config) error { +func ListExpose(kubeClientset kubernetes.Interface, cfg *Config) error { deploy, hpa, err := listDeployments(kubeClientset, cfg) services, err2 := listServices(kubeClientset, cfg) diff --git a/pkg/types/expose_test.go b/pkg/types/expose_test.go index 62f3a250..d8d1614a 100644 --- a/pkg/types/expose_test.go +++ b/pkg/types/expose_test.go @@ -25,12 +25,25 @@ import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/runtime" testclient "k8s.io/client-go/kubernetes/fake" + k8stesting "k8s.io/client-go/testing" ) type Action struct { - Verb string - Resource string - ObjectName string + Verb string + Resource string +} + +func CompareActions(actions []k8stesting.Action, expected_actions []Action) bool { + if len(actions) != len(expected_actions) { + return false + } + + for i, action := range actions { + if action.GetVerb() != expected_actions[i].Verb || action.GetResource().Resource != expected_actions[i].Resource { + return false + } + } + return true } func TestCreateExpose(t *testing.T) { @@ -61,14 +74,8 @@ func TestCreateExpose(t *testing.T) { {Verb: "create", Resource: "ingresses"}, } - if len(actions) != len(expected_actions) { - t.Errorf("Expected %d actions but got %d", (len(expected_actions)), len(actions)) - } - - for i, action := range actions { - if action.GetVerb() != expected_actions[i].Verb || action.GetResource().Resource != expected_actions[i].Resource { - t.Errorf("Expected %v action but got %v", expected_actions[i], action) - } + if CompareActions(actions, expected_actions) == false { + t.Errorf("Expected %v actions but got %v", expected_actions, actions) } } @@ -107,21 +114,73 @@ func TestDeleteExpose(t *testing.T) { actions := kubeClientset.Actions() expected_actions := []Action{ - {Verb: "delete", Resource: "horizontalpodautoscalers", ObjectName: "service-hpa"}, - {Verb: "delete", Resource: "deployments", ObjectName: "service-dlp"}, - {Verb: "delete", Resource: "services", ObjectName: "service-svc"}, - {Verb: "get", Resource: "ingresses", ObjectName: "service-ing"}, - {Verb: "delete-collection", Resource: "pods", ObjectName: "service-pod"}, + {Verb: "delete", Resource: "horizontalpodautoscalers"}, + {Verb: "delete", Resource: "deployments"}, + {Verb: "delete", Resource: "services"}, + {Verb: "get", Resource: "ingresses"}, + {Verb: "delete-collection", Resource: "pods"}, } - if len(actions) != len(expected_actions) { - t.Errorf("Expected %d actions but got %d", (len(expected_actions)), len(actions)) + if CompareActions(actions, expected_actions) == false { + t.Errorf("Expected %v actions but got %v", expected_actions, actions) } +} - for i, action := range actions { - if action.GetVerb() != expected_actions[i].Verb || action.GetResource().Resource != expected_actions[i].Resource { - t.Errorf("Expected %v action but got %v", expected_actions[i], action) - } +func TestUpdateExpose(t *testing.T) { + + K8sObjects := []runtime.Object{ + &autoscalingv1.HorizontalPodAutoscaler{ + ObjectMeta: metav1.ObjectMeta{ + Name: "service-hpa", + Namespace: "namespace", + }, + }, + &v1.Deployment{ + ObjectMeta: metav1.ObjectMeta{ + Name: "service-dlp", + Namespace: "namespace", + }, + }, + &corev1.Service{ + ObjectMeta: metav1.ObjectMeta{ + Name: "service-svc", + Namespace: "namespace", + }, + }, + } + + kubeClientset := testclient.NewSimpleClientset(K8sObjects...) + cfg := &Config{ServicesNamespace: "namespace"} + + service := Service{ + Name: "service", + Expose: Expose{ + MinScale: 1, + MaxScale: 3, + CpuThreshold: 80, + }, + } + + err := UpdateExpose(service, kubeClientset, cfg) + + if err != nil { + t.Errorf("Error creating expose: %v", err) + } + + actions := kubeClientset.Actions() + + expected_actions := []Action{ + {Verb: "get", Resource: "deployments"}, + {Verb: "update", Resource: "deployments"}, + {Verb: "get", Resource: "horizontalpodautoscalers"}, + {Verb: "update", Resource: "horizontalpodautoscalers"}, + {Verb: "update", Resource: "services"}, + {Verb: "get", Resource: "ingresses"}, + {Verb: "create", Resource: "ingresses"}, + } + + if CompareActions(actions, expected_actions) == false { + t.Errorf("Expected %v actions but got %v", expected_actions, actions) } } From 10f4b52f7ad4ffd0f58df1614267c088f0a29757 Mon Sep 17 00:00:00 2001 From: Miguel Caballer Date: Tue, 8 Oct 2024 12:33:39 +0200 Subject: [PATCH 5/7] Improve tests --- pkg/types/expose_test.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pkg/types/expose_test.go b/pkg/types/expose_test.go index d8d1614a..6097ac40 100644 --- a/pkg/types/expose_test.go +++ b/pkg/types/expose_test.go @@ -19,7 +19,7 @@ package types import ( "testing" - v1 "k8s.io/api/apps/v1" + appsv1 "k8s.io/api/apps/v1" autoscalingv1 "k8s.io/api/autoscaling/v1" corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -88,7 +88,7 @@ func TestDeleteExpose(t *testing.T) { Namespace: "namespace", }, }, - &v1.Deployment{ + &appsv1.Deployment{ ObjectMeta: metav1.ObjectMeta{ Name: "service-dlp", Namespace: "namespace", @@ -135,7 +135,7 @@ func TestUpdateExpose(t *testing.T) { Namespace: "namespace", }, }, - &v1.Deployment{ + &appsv1.Deployment{ ObjectMeta: metav1.ObjectMeta{ Name: "service-dlp", Namespace: "namespace", From 2909bf1730bda5a480126e29fb15ea731bd4026f Mon Sep 17 00:00:00 2001 From: Miguel Caballer Date: Tue, 8 Oct 2024 12:48:53 +0200 Subject: [PATCH 6/7] Improve tests --- pkg/types/expose_test.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/pkg/types/expose_test.go b/pkg/types/expose_test.go index 6097ac40..e0185f09 100644 --- a/pkg/types/expose_test.go +++ b/pkg/types/expose_test.go @@ -56,6 +56,7 @@ func TestCreateExpose(t *testing.T) { MinScale: 1, MaxScale: 3, CpuThreshold: 80, + SetAuth: true, }, } cfg := &Config{ServicesNamespace: "namespace"} @@ -72,6 +73,7 @@ func TestCreateExpose(t *testing.T) { {Verb: "create", Resource: "horizontalpodautoscalers"}, {Verb: "create", Resource: "services"}, {Verb: "create", Resource: "ingresses"}, + {Verb: "create", Resource: "secrets"}, } if CompareActions(actions, expected_actions) == false { @@ -158,6 +160,7 @@ func TestUpdateExpose(t *testing.T) { MinScale: 1, MaxScale: 3, CpuThreshold: 80, + SetAuth: true, }, } @@ -177,6 +180,7 @@ func TestUpdateExpose(t *testing.T) { {Verb: "update", Resource: "services"}, {Verb: "get", Resource: "ingresses"}, {Verb: "create", Resource: "ingresses"}, + {Verb: "create", Resource: "secrets"}, } if CompareActions(actions, expected_actions) == false { @@ -193,6 +197,7 @@ func TestServiceSpec(t *testing.T) { MaxScale: 3, CpuThreshold: 40, APIPort: 8080, + SetAuth: true, }, } cfg := &Config{Namespace: "namespace"} From a640afdf951f93cf183f66254878c73f320d86f8 Mon Sep 17 00:00:00 2001 From: Miguel Caballer Date: Thu, 10 Oct 2024 16:52:32 +0200 Subject: [PATCH 7/7] Add create tests --- pkg/handlers/create_test.go | 111 ++++++++++++++++++++++++++++++++++++ 1 file changed, 111 insertions(+) create mode 100644 pkg/handlers/create_test.go diff --git a/pkg/handlers/create_test.go b/pkg/handlers/create_test.go new file mode 100644 index 00000000..a3f3d68b --- /dev/null +++ b/pkg/handlers/create_test.go @@ -0,0 +1,111 @@ +/* +Copyright (C) GRyCAP - I3M - UPV + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + +http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package handlers + +import ( + "fmt" + "strings" + "testing" + + "net/http" + "net/http/httptest" + + "github.com/gin-gonic/gin" + "github.com/grycap/oscar/v3/pkg/backends" + "github.com/grycap/oscar/v3/pkg/types" +) + +func TestMakeCreateHandler(t *testing.T) { + back := backends.MakeFakeBackend() + + // Create a fake MinIO server + server := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, hreq *http.Request) { + + if hreq.URL.Path != "/input" && hreq.URL.Path != "/output" && !strings.HasPrefix(hreq.URL.Path, "/minio/admin/v3/") { + t.Errorf("Unexpected path in request, got: %s", hreq.URL.Path) + } + + fmt.Println(hreq.URL.Path) + + if hreq.URL.Path == "/minio/admin/v3/info" { + rw.WriteHeader(http.StatusOK) + rw.Write([]byte(`{"Mode": "local", "Region": "us-east-1"}`)) + } else { + rw.WriteHeader(http.StatusOK) + rw.Write([]byte(`{"status": "success"}`)) + } + })) + + // and set the MinIO endpoint to the fake server + cfg := types.Config{ + MinIOProvider: &types.MinIOProvider{ + Endpoint: server.URL, + Region: "us-east-1", + AccessKey: "minioadmin", + SecretKey: "minioadmin", + Verify: false, + }, + } + r := gin.Default() + r.POST("/system/services", MakeCreateHandler(&cfg, back)) + + w := httptest.NewRecorder() + body := strings.NewReader(` + { + "name": "cowsay", + "cluster_id": "oscar", + "memory": "1Gi", + "cpu": "1.0", + "log_level": "CRITICAL", + "image": "ghcr.io/grycap/cowsay", + "alpine": false, + "script": "test", + "input": [ + { + "storage_provider": "minio", + "path": "/input" + } + ], + "output": [ + { + "storage_provider": "webdav.id", + "path": "/output" + } + ], + "storage_providers": { + "webdav": { + "id": { + "hostname": "` + server.URL + `", + "login": "user", + "password": "pass" + } + } + } + } + `) + + req, _ := http.NewRequest("POST", "/system/services", body) + r.ServeHTTP(w, req) + + // Close the fake MinIO server + defer server.Close() + + if w.Code != http.StatusCreated { + fmt.Println(w.Body) + t.Errorf("expecting code %d, got %d", http.StatusCreated, w.Code) + } +}