Skip to content

Commit

Permalink
add test cases
Browse files Browse the repository at this point in the history
Signed-off-by: Frank Jogeleit <[email protected]>
  • Loading branch information
Frank Jogeleit committed Apr 18, 2024
1 parent 50ec5db commit 85a0f07
Show file tree
Hide file tree
Showing 3 changed files with 247 additions and 3 deletions.
4 changes: 2 additions & 2 deletions charts/policy-reporter/templates/ui/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,11 @@ spec:
protocol: TCP
livenessProbe:
httpGet:
path: /
path: /healthz
port: http
readinessProbe:
httpGet:
path: /
path: /healthz
port: http
resources:
{{- toYaml .Values.ui.resources | nindent 12 }}
Expand Down
2 changes: 1 addition & 1 deletion charts/policy-reporter/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -720,7 +720,7 @@ ui:
pullPolicy: IfNotPresent
# -- (string) Image tag
# Defaults to `Chart.AppVersion` if omitted
tag: "2.0.0-alpha.37"
tag: "2.0.0-alpha.47"

# -- Deployment replica count
replicaCount: 1
Expand Down
244 changes: 244 additions & 0 deletions pkg/api/v2/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,4 +196,248 @@ func TestV2(t *testing.T) {
assert.Contains(t, resp, v2.SourceDetails{Name: "test", Categories: []*v2.Category{{Name: "Other", Fail: 1}, {Name: "test", Fail: 2}}})
}
})

t.Run("ListResourceCategories", func(t *testing.T) {
req, _ := http.NewRequest("GET", "/v2/resource/17962226559046503697/source-categories", nil)
w := httptest.NewRecorder()

server.Serve(w, req)

if ok := assert.Equal(t, http.StatusOK, w.Code); ok {
resp := make([]v2.SourceDetails, 0)

json.NewDecoder(w.Body).Decode(&resp)

assert.Equal(t, resp[0], v2.SourceDetails{Name: "test", Categories: []*v2.Category{{Name: "test", Fail: 1}}})
}
})

t.Run("GetResource", func(t *testing.T) {
req, _ := http.NewRequest("GET", "/v2/resource/17962226559046503697", nil)
w := httptest.NewRecorder()

server.Serve(w, req)

if ok := assert.Equal(t, http.StatusOK, w.Code); ok {
resp := v2.Resource{}

json.NewDecoder(w.Body).Decode(&resp)

assert.Equal(t, resp, v2.Resource{ID: "17962226559046503697", UID: "dfd57c50-f30c-4729-b63f-b1954d8988d1", Namespace: "test", Name: "nginx", Kind: "Deployment", APIVersion: "v1"})
}
})

t.Run("GetResourceStatusCounts", func(t *testing.T) {
req, _ := http.NewRequest("GET", "/v2/resource/17962226559046503697/status-counts", nil)
w := httptest.NewRecorder()

server.Serve(w, req)

if ok := assert.Equal(t, http.StatusOK, w.Code); ok {
resp := make([]v2.ResourceStatusCount, 0)

json.NewDecoder(w.Body).Decode(&resp)

assert.Contains(t, resp, v2.ResourceStatusCount{Source: "test", Fail: 1})
}
})

t.Run("ListNamespaceResourceResults", func(t *testing.T) {
req, _ := http.NewRequest("GET", "/v2/namespace-scoped/resource-results?namespaces=kyverno", nil)
w := httptest.NewRecorder()

server.Serve(w, req)

if ok := assert.Equal(t, http.StatusOK, w.Code); ok {
resp := v2.Paginated[v2.ResourceResult]{}

json.NewDecoder(w.Body).Decode(&resp)

assert.Equal(t, resp.Count, 2)
assert.Contains(t, resp.Items, v2.ResourceResult{ID: "6274512523942114905", UID: "dfd57c50-f30c-4729-b63f-b1954d8988d1", Name: "nginx", Kind: "Deployment", APIVersion: "v1", Namespace: "kyverno", Pass: 1})
assert.Contains(t, resp.Items, v2.ResourceResult{ID: "8277600851619588241", UID: "dfd57c50-f30c-4729-b63f-b1954d8988d2", Name: "nginx2", Kind: "Deployment", APIVersion: "v1", Namespace: "kyverno", Warn: 1})
}
})

t.Run("ListClusterResourceResults", func(t *testing.T) {
req, _ := http.NewRequest("GET", "/v2/cluster-scoped/resource-results", nil)
w := httptest.NewRecorder()

server.Serve(w, req)

if ok := assert.Equal(t, http.StatusOK, w.Code); ok {
resp := v2.Paginated[v2.ResourceResult]{}

json.NewDecoder(w.Body).Decode(&resp)

assert.Equal(t, resp.Count, 1)
assert.Equal(t, resp.Items[0], v2.ResourceResult{ID: "6274512523942114905", UID: "dfd57c50-f30c-4729-b63f-b1954d8988d1", Name: "kyverno", Kind: "Namespace", APIVersion: "v1", Source: "", Fail: 1})
}
})

t.Run("GetClusterStatusCounts", func(t *testing.T) {
req, _ := http.NewRequest("GET", "/v2/cluster-scoped/Kyverno/status-counts", nil)
w := httptest.NewRecorder()

server.Serve(w, req)

if ok := assert.Equal(t, http.StatusOK, w.Code); ok {
resp := make(map[string]int, 0)

json.NewDecoder(w.Body).Decode(&resp)

assert.Equal(t, 5, len(resp))
assert.Equal(t, 1, resp["fail"])
}
})

t.Run("GetNamespaceStatusCounts", func(t *testing.T) {
req, _ := http.NewRequest("GET", "/v2/namespace-scoped/Kyverno/status-counts", nil)
w := httptest.NewRecorder()

server.Serve(w, req)

if ok := assert.Equal(t, http.StatusOK, w.Code); ok {
resp := make(map[string]map[string]int, 0)

json.NewDecoder(w.Body).Decode(&resp)

assert.Equal(t, 1, len(resp))
assert.Equal(t, 5, len(resp["kyverno"]))
assert.Equal(t, 1, resp["kyverno"]["pass"])
assert.Equal(t, 1, resp["kyverno"]["warn"])
assert.Equal(t, 0, resp["kyverno"]["fail"])
assert.Equal(t, 0, resp["kyverno"]["error"])
assert.Equal(t, 0, resp["kyverno"]["skip"])
}
})

t.Run("ListClusterKinds", func(t *testing.T) {
req, _ := http.NewRequest("GET", "/v2/cluster-scoped/kinds", nil)
w := httptest.NewRecorder()

server.Serve(w, req)

if ok := assert.Equal(t, http.StatusOK, w.Code); ok {
resp := make([]string, 0)

json.NewDecoder(w.Body).Decode(&resp)

assert.Equal(t, 1, len(resp))
assert.Equal(t, "Namespace", resp[0])
}
})

t.Run("ListNamespaceKinds", func(t *testing.T) {
req, _ := http.NewRequest("GET", "/v2/namespace-scoped/kinds", nil)
w := httptest.NewRecorder()

server.Serve(w, req)

if ok := assert.Equal(t, http.StatusOK, w.Code); ok {
resp := make([]string, 0)

json.NewDecoder(w.Body).Decode(&resp)

assert.Equal(t, 1, len(resp))
assert.Equal(t, "Deployment", resp[0])
}
})

t.Run("ListResourceResults", func(t *testing.T) {
req, _ := http.NewRequest("GET", "/v2/resource/6274512523942114905/resource-results", nil)
w := httptest.NewRecorder()

server.Serve(w, req)

if ok := assert.Equal(t, http.StatusOK, w.Code); ok {
resp := make([]v2.ResourceResult, 0)

json.NewDecoder(w.Body).Decode(&resp)

assert.Equal(t, 1, len(resp))
assert.Equal(t, resp[0], v2.ResourceResult{ID: "6274512523942114905", UID: "dfd57c50-f30c-4729-b63f-b1954d8988d1", Name: "nginx", Kind: "Deployment", APIVersion: "v1", Namespace: "kyverno", Source: "Kyverno", Pass: 1})
}
})

t.Run("ListResourcePolilcyResults", func(t *testing.T) {
req, _ := http.NewRequest("GET", "/v2/resource/6274512523942114905/results", nil)
w := httptest.NewRecorder()

server.Serve(w, req)

if ok := assert.Equal(t, http.StatusOK, w.Code); ok {
resp := v2.Paginated[v2.PolicyResult]{}

json.NewDecoder(w.Body).Decode(&resp)

assert.Equal(t, 1, resp.Count)
assert.Equal(t, resp.Items[0], v2.PolicyResult{ID: "14158407137220160684", ResourceID: "6274512523942114905", Severity: "high", Name: "nginx", Kind: "Deployment", APIVersion: "v1", Namespace: "kyverno", Message: "message", Category: "test", Policy: "required-limit", Rule: "resource-limit-required", Status: "pass", Timestamp: 1614093003})
}
})

t.Run("ListPolicyResults Namespaced", func(t *testing.T) {
req, _ := http.NewRequest("GET", "/v2/namespace-scoped/results?namespaces=kyverno", nil)
w := httptest.NewRecorder()

server.Serve(w, req)

if ok := assert.Equal(t, http.StatusOK, w.Code); ok {
resp := v2.Paginated[v2.PolicyResult]{}

json.NewDecoder(w.Body).Decode(&resp)

assert.Equal(t, 2, resp.Count)
assert.Equal(t, resp.Items[0], v2.PolicyResult{ID: "14158407137220160684", ResourceID: "6274512523942114905", Severity: "high", Name: "nginx", Kind: "Deployment", APIVersion: "v1", Namespace: "kyverno", Message: "message", Category: "test", Policy: "required-limit", Rule: "resource-limit-required", Status: "pass", Timestamp: 1614093003})
assert.Equal(t, resp.Items[1], v2.PolicyResult{ID: "2079631062832497014", ResourceID: "8277600851619588241", Severity: "high", Name: "nginx2", Kind: "Deployment", APIVersion: "v1", Namespace: "kyverno", Message: "message", Category: "test", Policy: "required-limit", Rule: "resource-limit-required", Status: "warn", Timestamp: 1614093003})
}
})

t.Run("ListPolicyResults", func(t *testing.T) {
req, _ := http.NewRequest("GET", "/v2/cluster-scoped/results", nil)
w := httptest.NewRecorder()

server.Serve(w, req)

if ok := assert.Equal(t, http.StatusOK, w.Code); ok {
resp := v2.Paginated[v2.PolicyResult]{}

json.NewDecoder(w.Body).Decode(&resp)

assert.Equal(t, 1, resp.Count)
assert.Equal(t, resp.Items[0], v2.PolicyResult{ID: "16800058481201255747", ResourceID: "11786270724827677857", Severity: "high", Name: "kyverno", Kind: "Namespace", APIVersion: "v1", Namespace: "", Message: "message", Category: "test", Policy: "cluster-required-quota", Rule: "ns-quota-required", Status: "fail", Timestamp: 1614093000})
}
})

t.Run("ListResultsWithoutResource", func(t *testing.T) {
req, _ := http.NewRequest("GET", "/v2/results-without-resources", nil)
w := httptest.NewRecorder()

server.Serve(w, req)

if ok := assert.Equal(t, http.StatusOK, w.Code); ok {
resp := v2.Paginated[v2.PolicyResult]{}

json.NewDecoder(w.Body).Decode(&resp)

assert.Equal(t, 1, resp.Count)
assert.Equal(t, resp.Items[0], v2.PolicyResult{ID: "8115731892871392633", ResourceID: "18007334074686647077", Severity: "", Name: "", Kind: "", APIVersion: "", Namespace: "test", Message: "message 2", Category: "", Policy: "priority-test", Rule: "", Status: "fail", Timestamp: 1614093000})
}
})

t.Run("UseResources", func(t *testing.T) {
req, _ := http.NewRequest("GET", "/v2/sources/Kyverno/use-resources", nil)
w := httptest.NewRecorder()

server.Serve(w, req)

if ok := assert.Equal(t, http.StatusOK, w.Code); ok {
resp := make(map[string]bool, 0)

json.NewDecoder(w.Body).Decode(&resp)

assert.Equal(t, 1, len(resp))
assert.True(t, resp["resources"])
}
})
}

0 comments on commit 85a0f07

Please sign in to comment.