Skip to content

Commit

Permalink
Updated test for the new metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
Ygnas committed Oct 17, 2023
1 parent ea3f33a commit a93c0f2
Show file tree
Hide file tree
Showing 5 changed files with 168 additions and 0 deletions.
3 changes: 3 additions & 0 deletions tests/e2e.sh
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ trap finish EXIT

# create Gateway API CRDs
kubectl create -f ./config/gateway-api/crd/standard/
kubectl create -f ./config/kuadrant/crd/

# create gateway-api customresourcestatemetrics configmap
kubectl create configmap custom-resource-state --from-file=./config/default/custom-resource-state.yaml --dry-run=client -o yaml | kubectl -n kube-system apply -f -
Expand All @@ -144,6 +145,8 @@ kubectl create -f ./config/examples/kube-state-metrics/deployment.yaml

kubectl create -f ./config/examples/kube-state-metrics/service.yaml

kubectl patch clusterrole kube-state-metrics --type=json -p "$(cat ./config/kuadrant/clusterrole-patch.yaml)"

# Create test Gateway API resources
kubectl create -f ./tests/manifests/
# Set statuses as well to mock different controller behaviour
Expand Down
107 changes: 107 additions & 0 deletions tests/e2e/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ func TestGatewayMetricsAvailable(t *testing.T) {
testTCPRoute(t, gatewayapiMetrics)
testUDPRoute(t, gatewayapiMetrics)
testTLSRoute(t, gatewayapiMetrics)
testBackendTLSPolicy(t, gatewayapiMetrics)
testRateLimitPolicy(t, gatewayapiMetrics)
testTLSPolicy(t, gatewayapiMetrics)
}

func testGatewayClasses(t *testing.T, metrics map[string][][]string) {
Expand Down Expand Up @@ -481,6 +484,110 @@ func testUDPRoute(t *testing.T, metrics map[string][][]string) {
expectEqual(t, udproute1ParentStatusInfo1Labels["parent_name"], "testgateway1", "gatewayapi_udproute_status_parent_info__1 parent_name")
}

func testBackendTLSPolicy(t *testing.T, metrics map[string][][]string) {
// gatewayapi_backendtlspolicy_created
backendtlspolicyCreated := metrics["gatewayapi_backendtlspolicy_created"]
backendtlspolicy1Created := backendtlspolicyCreated[0]
expectValidTimestampInPast(t, backendtlspolicy1Created[3], "gatewayapi_backendtlspolicy_created__1 value")
backendtlspolicy1CreatedLabels := parseLabels(string(backendtlspolicy1Created[2]))
expectEqual(t, backendtlspolicy1CreatedLabels["customresource_group"], "gateway.networking.k8s.io", "gatewayapi_backendtlspolicy_created__1 customresource_group")
expectEqual(t, backendtlspolicy1CreatedLabels["customresource_kind"], "BackendTLSPolicy", "gatewayapi_backendtlspolicy_created__1 customresource_kind")
expectEqual(t, backendtlspolicy1CreatedLabels["customresource_version"], "v1alpha2", "gatewayapi_backendtlspolicy_created__1 customresource_version")
expectEqual(t, backendtlspolicy1CreatedLabels["name"], "testbackendtlspolicy1", "gatewayapi_backendtlspolicy_created__1 name")
expectEqual(t, backendtlspolicy1CreatedLabels["namespace"], "default", "gatewayapi_backendtlspolicy_created__1 namespace")

//gatewayapi_backendtlspolicy_target_info
backendtlspolicyParentInfo := metrics["gatewayapi_backendtlspolicy_target_info"]
backendtlspolicy1ParentInfo1 := backendtlspolicyParentInfo[0]
expectEqual(t, backendtlspolicy1ParentInfo1[3], "1", "gatewayapi_backendtlspolicy_target_info__1 value")
backendtlspolicy1ParentInfo1Labels := parseLabels(string(backendtlspolicy1ParentInfo1[2]))
expectEqual(t, backendtlspolicy1ParentInfo1Labels["customresource_group"], "gateway.networking.k8s.io", "gatewayapi_backendtlspolicy_target_info__1 customresource_group")
expectEqual(t, backendtlspolicy1ParentInfo1Labels["customresource_kind"], "BackendTLSPolicy", "gatewayapi_backendtlspolicy_target_info__1 customresource_kind")
expectEqual(t, backendtlspolicy1ParentInfo1Labels["customresource_version"], "v1alpha2", "gatewayapi_backendtlspolicy_target_info__1 customresource_version")
expectEqual(t, backendtlspolicy1ParentInfo1Labels["name"], "testbackendtlspolicy1", "gatewayapi_backendtlspolicy_target_info__1 name")
expectEqual(t, backendtlspolicy1ParentInfo1Labels["namespace"], "default", "gatewayapi_backendtlspolicy_target_info__1 namespace")
expectEqual(t, backendtlspolicy1ParentInfo1Labels["target_group"], "", "gatewayapi_backendtlspolicy_target_info__1 target_group")
expectEqual(t, backendtlspolicy1ParentInfo1Labels["target_kind"], "Service", "gatewayapi_backendtlspolicy_target_info__1 target_kind")
}

func testRateLimitPolicy(t *testing.T, metrics map[string][][]string) {
// gatewayapi_ratelimitpolicy_created
ratelimitpolicyCreated := metrics["gatewayapi_ratelimitpolicy_created"]
ratelimitpolicy1Created := ratelimitpolicyCreated[0]
expectValidTimestampInPast(t, ratelimitpolicy1Created[3], "gatewayapi_ratelimitpolicy_created__1 value")
ratelimitpolicy1CreatedLabels := parseLabels(string(ratelimitpolicy1Created[2]))
expectEqual(t, ratelimitpolicy1CreatedLabels["customresource_group"], "kuadrant.io", "gatewayapi_ratelimitpolicy_created__1 customresource_group")
expectEqual(t, ratelimitpolicy1CreatedLabels["customresource_kind"], "RateLimitPolicy", "gatewayapi_ratelimitpolicy_created__1 customresource_kind")
expectEqual(t, ratelimitpolicy1CreatedLabels["customresource_version"], "v1beta2", "gatewayapi_ratelimitpolicy_created__1 customresource_version")
expectEqual(t, ratelimitpolicy1CreatedLabels["name"], "testratelimitpolicy1", "gatewayapi_ratelimitpolicy_created__1 name")
expectEqual(t, ratelimitpolicy1CreatedLabels["namespace"], "default", "gatewayapi_ratelimitpolicy_created__1 namespace")

//gatewayapi_ratelimitpolicy_target_info
ratelimitpolicyParentInfo := metrics["gatewayapi_ratelimitpolicy_target_info"]
ratelimitpolicy1ParentInfo1 := ratelimitpolicyParentInfo[0]
expectEqual(t, ratelimitpolicy1ParentInfo1[3], "1", "gatewayapi_ratelimitpolicy_target_info__1 value")
ratelimitpolicy1ParentInfo1Labels := parseLabels(string(ratelimitpolicy1ParentInfo1[2]))
expectEqual(t, ratelimitpolicy1ParentInfo1Labels["customresource_group"], "kuadrant.io", "gatewayapi_ratelimitpolicy_target_info__1 customresource_group")
expectEqual(t, ratelimitpolicy1ParentInfo1Labels["customresource_kind"], "RateLimitPolicy", "gatewayapi_ratelimitpolicy_target_info__1 customresource_kind")
expectEqual(t, ratelimitpolicy1ParentInfo1Labels["customresource_version"], "v1beta2", "gatewayapi_ratelimitpolicy_target_info__1 customresource_version")
expectEqual(t, ratelimitpolicy1ParentInfo1Labels["name"], "testratelimitpolicy1", "gatewayapi_ratelimitpolicy_target_info__1 name")
expectEqual(t, ratelimitpolicy1ParentInfo1Labels["namespace"], "default", "gatewayapi_ratelimitpolicy_target_info__1 namespace")
expectEqual(t, ratelimitpolicy1ParentInfo1Labels["target_group"], "gateway.networking.k8s.io", "gatewayapi_ratelimitpolicy_target_info__1 target_group")
expectEqual(t, ratelimitpolicy1ParentInfo1Labels["target_kind"], "HTTPRoute", "gatewayapi_ratelimitpolicy_target_info__1 target_kind")
expectEqual(t, ratelimitpolicy1ParentInfo1Labels["target_name"], "testname1", "gatewayapi_ratelimitpolicy_target_info__1 target_name")

//gatewayapi_ratelimitpolicy_status
ratelimitpolicyStatus := metrics["gatewayapi_ratelimitpolicy_status"]
ratelimitpolicy1Status1 := ratelimitpolicyStatus[0]
expectEqual(t, ratelimitpolicy1Status1[3], "1", "gatewayapi_ratelimitpolicy_status__1 value")
ratelimitpolicy1Status1Labels := parseLabels(string(ratelimitpolicy1Status1[2]))
expectEqual(t, ratelimitpolicy1Status1Labels["customresource_group"], "kuadrant.io", "gatewayapi_ratelimitpolicy_status__1 customresource_group")
expectEqual(t, ratelimitpolicy1Status1Labels["customresource_kind"], "RateLimitPolicy", "gatewayapi_ratelimitpolicy_status__1 customresource_kind")
expectEqual(t, ratelimitpolicy1Status1Labels["customresource_version"], "v1beta2", "gatewayapi_ratelimitpolicy_status__1 customresource_version")
expectEqual(t, ratelimitpolicy1Status1Labels["name"], "testratelimitpolicy1", "gatewayapi_ratelimitpolicy_status__1 name")
expectEqual(t, ratelimitpolicy1Status1Labels["namespace"], "default", "gatewayapi_ratelimitpolicy_status__1 namespace")
expectEqual(t, ratelimitpolicy1Status1Labels["type"], "Available", "gatewayapi_ratelimitpolicy_status__1 type")
}

func testTLSPolicy(t *testing.T, metrics map[string][][]string) {
// gatewayapi_tlspolicy_created
tlspolicyCreated := metrics["gatewayapi_tlspolicy_created"]
tlspolicy1Created := tlspolicyCreated[0]
expectValidTimestampInPast(t, tlspolicy1Created[3], "gatewayapi_tlspolicy_created__1 value")
tlspolicy1CreatedLabels := parseLabels(string(tlspolicy1Created[2]))
expectEqual(t, tlspolicy1CreatedLabels["customresource_group"], "kuadrant.io", "gatewayapi_tlspolicy_created__1 customresource_group")
expectEqual(t, tlspolicy1CreatedLabels["customresource_kind"], "TLSPolicy", "gatewayapi_tlspolicy_created__1 customresource_kind")
expectEqual(t, tlspolicy1CreatedLabels["customresource_version"], "v1alpha1", "gatewayapi_tlspolicy_created__1 customresource_version")
expectEqual(t, tlspolicy1CreatedLabels["name"], "testtlspolicy1", "gatewayapi_tlspolicy_created__1 name")
expectEqual(t, tlspolicy1CreatedLabels["namespace"], "default", "gatewayapi_tlspolicy_created__1 namespace")

//gatewayapi_tlspolicy_target_info
tlspolicyParentInfo := metrics["gatewayapi_tlspolicy_target_info"]
tlspolicy1ParentInfo1 := tlspolicyParentInfo[0]
expectEqual(t, tlspolicy1ParentInfo1[3], "1", "gatewayapi_tlspolicy_target_info__1 value")
tlspolicy1ParentInfo1Labels := parseLabels(string(tlspolicy1ParentInfo1[2]))
expectEqual(t, tlspolicy1ParentInfo1Labels["customresource_group"], "kuadrant.io", "gatewayapi_tlspolicy_target_info__1 customresource_group")
expectEqual(t, tlspolicy1ParentInfo1Labels["customresource_kind"], "TLSPolicy", "gatewayapi_tlspolicy_target_info__1 customresource_kind")
expectEqual(t, tlspolicy1ParentInfo1Labels["customresource_version"], "v1alpha1", "gatewayapi_tlspolicy_target_info__1 customresource_version")
expectEqual(t, tlspolicy1ParentInfo1Labels["name"], "testtlspolicy1", "gatewayapi_tlspolicy_target_info__1 name")
expectEqual(t, tlspolicy1ParentInfo1Labels["namespace"], "default", "gatewayapi_tlspolicy_target_info__1 namespace")
expectEqual(t, tlspolicy1ParentInfo1Labels["target_group"], "gateway.networking.k8s.io", "gatewayapi_tlspolicy_target_info__1 target_group")
expectEqual(t, tlspolicy1ParentInfo1Labels["target_kind"], "Gateway", "gatewayapi_tlspolicy_target_info__1 target_kind")
expectEqual(t, tlspolicy1ParentInfo1Labels["target_name"], "testgateway1", "gatewayapi_tlspolicy_target_info__1 target_name")

//gatewayapi_tlspolicy_status
tlspolicyStatus := metrics["gatewayapi_tlspolicy_status"]
tlspolicy1Status1 := tlspolicyStatus[0]
expectEqual(t, tlspolicy1Status1[3], "1", "gatewayapi_tlspolicy_status__1 value")
tlspolicy1Status1Labels := parseLabels(string(tlspolicy1Status1[2]))
expectEqual(t, tlspolicy1Status1Labels["customresource_group"], "kuadrant.io", "gatewayapi_tlspolicy_status__1 customresource_group")
expectEqual(t, tlspolicy1Status1Labels["customresource_kind"], "TLSPolicy", "gatewayapi_tlspolicy_status__1 customresource_kind")
expectEqual(t, tlspolicy1Status1Labels["customresource_version"], "v1alpha1", "gatewayapi_tlspolicy_status__1 customresource_version")
expectEqual(t, tlspolicy1Status1Labels["name"], "testtlspolicy1", "gatewayapi_tlspolicy_status__1 name")
expectEqual(t, tlspolicy1Status1Labels["namespace"], "default", "gatewayapi_tlspolicy_status__1 namespace")
expectEqual(t, tlspolicy1Status1Labels["type"], "Ready", "gatewayapi_tlspolicy_status__1 type")
}

func parseLabels(labelsRaw string) map[string]string {
// simple label parsing assuming no special chars/escaping
// fmt.Printf("labelsRaw=%s\n", labelsRaw)
Expand Down
15 changes: 15 additions & 0 deletions tests/manifests/testbackendtlspolicy1.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
apiVersion: gateway.networking.k8s.io/v1alpha2
kind: BackendTLSPolicy
metadata:
name: testbackendtlspolicy1
spec:
targetRef:
group: ""
kind: Service
name: testname1
tls:
caCertRefs:
- group: ""
name: "grafana"
kind: "ConfigMap"
hostname: grafana.example.com
22 changes: 22 additions & 0 deletions tests/manifests/testratelimitpolicy.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
apiVersion: kuadrant.io/v1beta2
kind: RateLimitPolicy
metadata:
name: testratelimitpolicy1
spec:
targetRef:
group: gateway.networking.k8s.io
kind: HTTPRoute
name: testname1
limits:
"global":
rates:
- limit: 5
duration: 10
unit: second
status:
conditions:
- lastTransitionTime: "2023-08-21T22:53:08Z"
message: HTTPRoute is ratelimited
reason: HTTPRouteProtected
status: "True"
type: Available
21 changes: 21 additions & 0 deletions tests/manifests/testtlspolicy.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
apiVersion: kuadrant.io/v1alpha1
kind: TLSPolicy
metadata:
name: testtlspolicy1
namespace: default
spec:
targetRef:
name: testgateway1
group: gateway.networking.k8s.io
kind: Gateway
issuerRef:
group: cert-manager.io
kind: ClusterIssuer
name: selfsigned-cluster-issuer
status:
conditions:
- lastTransitionTime: "2023-08-21T22:53:08Z"
message: Gateway is TLS Enabled
reason: GatewayTLSEnabled
status: "True"
type: Ready

0 comments on commit a93c0f2

Please sign in to comment.