Skip to content

Commit

Permalink
Merge pull request #261 from grycap/micafer_dev
Browse files Browse the repository at this point in the history
Add tests
  • Loading branch information
catttam authored Oct 14, 2024
2 parents 4427db9 + a640afd commit ee0f66f
Show file tree
Hide file tree
Showing 4 changed files with 364 additions and 23 deletions.
111 changes: 111 additions & 0 deletions pkg/handlers/create_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
23 changes: 10 additions & 13 deletions pkg/types/expose.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
Loading

0 comments on commit ee0f66f

Please sign in to comment.