Add the Loki query UI, access policies, ingress insights, alert and registry settings, and their backend APIs. Include the related Kubernetes deployment and collector updates required by the integrated application. Validated with go test ./..., npm run build, and npm run lint.
39 lines
1.8 KiB
Go
39 lines
1.8 KiB
Go
package services
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"opsxloki/models"
|
|
"testing"
|
|
)
|
|
|
|
func TestClusterWorkloadSelectorsUseSelectedNamespace(t *testing.T) {
|
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
w.Header().Set("Content-Type", "application/json")
|
|
switch r.URL.Path {
|
|
case "/api/v1/namespaces":
|
|
fmt.Fprint(w, `{"apiVersion":"v1","kind":"NamespaceList","items":[{"metadata":{"name":"prod"}},{"metadata":{"name":"default"}}]}`)
|
|
case "/apis/apps/v1/namespaces/prod/deployments":
|
|
fmt.Fprint(w, `{"apiVersion":"apps/v1","kind":"DeploymentList","items":[{"metadata":{"name":"api"},"spec":{"replicas":3},"status":{"readyReplicas":2}}]}`)
|
|
default:
|
|
t.Errorf("unexpected Kubernetes API path %s", r.URL.Path)
|
|
http.NotFound(w, r)
|
|
}
|
|
}))
|
|
defer server.Close()
|
|
cluster := &models.Cluster{KubeConfig: fmt.Sprintf("apiVersion: v1\nkind: Config\nclusters:\n- name: test\n cluster:\n server: %s\ncontexts:\n- name: test\n context:\n cluster: test\n user: test\ncurrent-context: test\nusers:\n- name: test\n user: {}\n", server.URL)}
|
|
namespaces, err := ListClusterNamespaces(context.Background(), cluster)
|
|
if err != nil || len(namespaces) != 2 || namespaces[0] != "default" || namespaces[1] != "prod" {
|
|
t.Fatalf("unexpected namespaces: %v, %v", namespaces, err)
|
|
}
|
|
deployments, err := ListClusterDeployments(context.Background(), cluster, "prod")
|
|
if err != nil || len(deployments) != 1 || deployments[0].Name != "api" || deployments[0].Replicas != 3 || deployments[0].ReadyReplicas != 2 {
|
|
t.Fatalf("unexpected deployments: %v, %v", deployments, err)
|
|
}
|
|
if _, err := ListClusterDeployments(context.Background(), cluster, "-bad"); err == nil {
|
|
t.Fatal("expected invalid namespace to be rejected")
|
|
}
|
|
}
|