Skip to content

Commit

Permalink
Add logs tests
Browse files Browse the repository at this point in the history
  • Loading branch information
micafer committed Oct 15, 2024
1 parent 7fc3cfa commit 7287a23
Show file tree
Hide file tree
Showing 2 changed files with 128 additions and 5 deletions.
10 changes: 5 additions & 5 deletions pkg/handlers/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import (
// TODO Try using cookies to avoid excesive calls to the k8s API //

// MakeJobsInfoHandler makes a handler for listing all existing jobs from a service and show their JobInfo
func MakeJobsInfoHandler(back types.ServerlessBackend, kubeClientset *kubernetes.Clientset, namespace string) gin.HandlerFunc {
func MakeJobsInfoHandler(back types.ServerlessBackend, kubeClientset kubernetes.Interface, namespace string) gin.HandlerFunc {
return func(c *gin.Context) {
jobsInfo := make(map[string]*types.JobInfo)
// Get serviceName
Expand Down Expand Up @@ -103,7 +103,7 @@ func MakeJobsInfoHandler(back types.ServerlessBackend, kubeClientset *kubernetes

// MakeDeleteJobsHandler makes a handler for deleting all jobs created by the provided service.
// If 'all' querystring is set to 'true' pending, running and failed jobs will also be deleted
func MakeDeleteJobsHandler(back types.ServerlessBackend, kubeClientset *kubernetes.Clientset, namespace string) gin.HandlerFunc {
func MakeDeleteJobsHandler(back types.ServerlessBackend, kubeClientset kubernetes.Interface, namespace string) gin.HandlerFunc {
return func(c *gin.Context) {
// Get serviceName and jobName
serviceName := c.Param("serviceName")
Expand Down Expand Up @@ -147,7 +147,7 @@ func MakeDeleteJobsHandler(back types.ServerlessBackend, kubeClientset *kubernet
}

// MakeGetLogsHandler makes a handler for getting logs from the 'oscar-container' inside the pod created by the specified job
func MakeGetLogsHandler(back types.ServerlessBackend, kubeClientset *kubernetes.Clientset, namespace string) gin.HandlerFunc {
func MakeGetLogsHandler(back types.ServerlessBackend, kubeClientset kubernetes.Interface, namespace string) gin.HandlerFunc {
return func(c *gin.Context) {
// Get serviceName and jobName
serviceName := c.Param("serviceName")
Expand Down Expand Up @@ -200,7 +200,7 @@ func MakeGetLogsHandler(back types.ServerlessBackend, kubeClientset *kubernetes.
}

// MakeDeleteJobHandler makes a handler for removing a job
func MakeDeleteJobHandler(back types.ServerlessBackend, kubeClientset *kubernetes.Clientset, namespace string) gin.HandlerFunc {
func MakeDeleteJobHandler(back types.ServerlessBackend, kubeClientset kubernetes.Interface, namespace string) gin.HandlerFunc {
return func(c *gin.Context) {
// Get serviceName and jobName
serviceName := c.Param("serviceName")
Expand Down Expand Up @@ -251,7 +251,7 @@ func isOIDCAuthorised(c *gin.Context, back types.ServerlessBackend, serviceName
// If is oidc auth get service and check on allowed users
authHeader := c.GetHeader("Authorization")
if len(strings.Split(authHeader, "Bearer")) > 1 {
service, _ := back.ReadService(c.Param("serviceName"))
service, _ := back.ReadService(serviceName)
uid, err := auth.GetUIDFromContext(c)
if err != nil {
c.String(http.StatusInternalServerError, fmt.Sprintln(err))
Expand Down
123 changes: 123 additions & 0 deletions pkg/handlers/logs_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
package handlers

import (
"fmt"
"net/http"
"net/http/httptest"
"testing"

"github.com/gin-gonic/gin"
"github.com/grycap/oscar/v3/pkg/backends"
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"
)

func TestMakeJobsInfoHandler(t *testing.T) {
back := backends.MakeFakeBackend()
kubeClientset := testclient.NewSimpleClientset()

r := gin.Default()
r.GET("/system/logs/:serviceName", MakeJobsInfoHandler(back, kubeClientset, "namespace"))

w := httptest.NewRecorder()
serviceName := "test"
req, _ := http.NewRequest("GET", "/system/logs/"+serviceName, nil)
r.ServeHTTP(w, req)

if w.Code != http.StatusOK {
fmt.Println(w.Body)
t.Errorf("expecting code %d, got %d", http.StatusOK, w.Code)
}

actions := kubeClientset.Actions()
if len(actions) != 2 {
t.Errorf("expecting 2 actions, got %d", len(actions))
}

if actions[0].GetVerb() != "list" || actions[0].GetResource().Resource != "jobs" {
t.Errorf("expecting list jobs, got %s %s", actions[0].GetVerb(), actions[0].GetResource().Resource)
}
if actions[1].GetVerb() != "list" || actions[1].GetResource().Resource != "pods" {
t.Errorf("expecting list pods, got %s %s", actions[1].GetVerb(), actions[1].GetResource().Resource)
}
}

func TestMakeDeleteJobsHandler(t *testing.T) {
back := backends.MakeFakeBackend()
kubeClientset := testclient.NewSimpleClientset()

r := gin.Default()
r.DELETE("/system/logs/:serviceName", MakeDeleteJobsHandler(back, kubeClientset, "namespace"))

w := httptest.NewRecorder()
serviceName := "test"
req, _ := http.NewRequest("DELETE", "/system/logs/"+serviceName, nil)
r.ServeHTTP(w, req)

if w.Code != http.StatusNoContent {
fmt.Println(w.Body)
t.Errorf("expecting code %d, got %d", http.StatusNoContent, w.Code)
}

actions := kubeClientset.Actions()
if len(actions) != 1 {
t.Errorf("expecting 1 actions, got %d", len(actions))
}

if actions[0].GetVerb() != "delete-collection" || actions[0].GetResource().Resource != "jobs" {
t.Errorf("expecting list jobs, got %s %s", actions[0].GetVerb(), actions[0].GetResource().Resource)
}
}

func TestMakeGetLogsHandler(t *testing.T) {
back := backends.MakeFakeBackend()

K8sObjects := []runtime.Object{
&corev1.PodList{
Items: []corev1.Pod{
{
ObjectMeta: metav1.ObjectMeta{
Name: "pod",
Namespace: "namespace",
Labels: map[string]string{
"oscar_service": "test",
"job-name": "job"},
},
},
},
},
}
kubeClientset := testclient.NewSimpleClientset(K8sObjects...)

r := gin.Default()
r.GET("/system/logs/:serviceName/:jobName", MakeGetLogsHandler(back, kubeClientset, "namespace"))

w := httptest.NewRecorder()
serviceName := "test"
jobName := "job"
req, _ := http.NewRequest("GET", "/system/logs/"+serviceName+"/"+jobName, nil)
r.ServeHTTP(w, req)

if w.Code != http.StatusOK {
fmt.Println(w.Body)
t.Errorf("expecting code %d, got %d", http.StatusOK, w.Code)
}
if w.Body.String() != "fake logs" {
t.Errorf("expecting 'fake logs', got %s", w.Body.String())
}

actions := kubeClientset.Actions()
if len(actions) != 2 {
t.Errorf("expecting 2 actions, got %d", len(actions))
}

if actions[0].GetVerb() != "list" || actions[0].GetResource().Resource != "pods" {
t.Errorf("expecting list pods, got %s %s", actions[0].GetVerb(), actions[0].GetResource().Resource)
}
if actions[1].GetVerb() != "get" || actions[1].GetResource().Resource != "pods" {
t.Errorf("expecting get pods, got %s %s", actions[1].GetVerb(), actions[1].GetResource().Resource)
}

}

0 comments on commit 7287a23

Please sign in to comment.