Skip to content

Commit

Permalink
Merge pull request #13 from caas-team/test/entsoe-tests-and-actions
Browse files Browse the repository at this point in the history
Test/entsoe tests and actions
  • Loading branch information
puffitos authored Nov 16, 2023
2 parents 6d7f569 + 8ac20b2 commit 755df0f
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 12 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Docker Image CI
name: build

on: push

Expand Down
17 changes: 17 additions & 0 deletions .github/workflows/gotest.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
name: test
on: [push]
jobs:
build:
name: Test Go package
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Set up Go
uses: actions/setup-go@v3
with:
go-version: 1.21
- name: Mod Tidy
run: go mod tidy
- name: Test
run: make test

19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,25 @@ prometheus-auth --log.debug --proxy-url http://localhost:9090 --listen-address :

`GET` - `/_/metrics` [sample](METRICS)

## Developement

### Testing

The `agent/test` folder contains the various `Scenario` structs for each Hijack method (/federate, /query and so on). Each "case" is split into three sub-cases:

- NoneNamespacesToken: the token used to query the API is not associated with any namespace
- SomeNamespacesToken: the token used to query the API is associated with some namespaces
- MyToken: the token used to query the API is associated with all namespaces (i.e. the token is a cluster-admin)

The `http_test.go` file contains the tests themselves, as well as the initialization of the TSDB with the metrics
present.

To run the tests, you can use the following command:

```bash
make test
```

# License

Copyright (c) 2014-2018 [Rancher Labs, Inc.](http://rancher.com)
Expand Down
4 changes: 2 additions & 2 deletions pkg/agent/http_hijack.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,10 @@ func hijackFederate(apiCtx *apiContext) error {
log.Debugf("raw federate[%s - %d] => %s", apiCtx.tag, idx, rawValue)
hjkValue := modifyExpression(expr, apiCtx.namespaceSet)

// introduce a new label namespace="caasglobal",
// introduce a new label namespace="caasglobal",
// all metrics with this label will pass the auth gate
caasNs := "|caasglobal\"}"
hjkValue = strings.Replace(hjkValue, "\"}", caasNs, -1)
hjkValue = strings.ReplaceAll(hjkValue, "\"}", caasNs)

log.Debugf("hjk federate[%s - %d] => %s", apiCtx.tag, idx, hjkValue)

Expand Down
2 changes: 2 additions & 0 deletions pkg/agent/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,11 +209,13 @@ func getTestCases(t *testing.T) []httpTestCase {
}

func Test_accessControl(t *testing.T) {
// the contents of the prom TSDB
input := `
load 1m
test_metric1{namespace="ns-a",foo="bar"} 0+100x100
test_metric1{namespace="ns-c",foo="boo"} 1+0x100
test_metric2{foo="boo"} 1+0x100
test_cluster_wide_metric{namespace="caasglobal"} 1+0x100
test_metric_without_labels 1+10x100
test_metric_stale 1+10x99 stale
test_metric_old 1+10x98
Expand Down
3 changes: 1 addition & 2 deletions pkg/agent/listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package agent
import (
"fmt"
"io"
"io/ioutil"
"net"
"strings"

Expand Down Expand Up @@ -46,7 +45,7 @@ func http2HeaderFieldEqual(nameValuePairs map[string]string) cmux.Matcher {
return false
}

framer := http2.NewFramer(ioutil.Discard, r)
framer := http2.NewFramer(io.Discard, r)
hdec := hpack.NewDecoder(uint32(4<<10), func(hf hpack.HeaderField) {
for name, value := range nameValuePairs {
matched = strings.EqualFold(hf.Name, name) && hf.Value == value
Expand Down
26 changes: 19 additions & 7 deletions pkg/agent/test/federate.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ var NoneNamespacesTokenFederateScenarios = map[string]Scenario{
RespCode: http.StatusOK,
RespBody: ``,
},
"empty none-existing label value matches everything": {
"empty non-existing label value matches everything": {
Queries: url.Values{
"match[]": []string{"{bar='',__name__=~'.+'}"},
},
Expand Down Expand Up @@ -214,7 +214,9 @@ test_metric1{foo="bar",namespace="ns-a",instance="",prometheus="cluster-level/te
"match[]": []string{"{__name__=~'.+'}"},
},
RespCode: http.StatusOK,
RespBody: `# TYPE test_metric1 untyped
RespBody: `# TYPE test_cluster_wide_metric untyped
test_cluster_wide_metric{namespace="caasglobal",instance="",prometheus="cluster-level/test"} 1 6000000
# TYPE test_metric1 untyped
test_metric1{foo="bar",namespace="ns-a",instance="",prometheus="cluster-level/test"} 10000 6000000
`,
},
Expand All @@ -223,14 +225,18 @@ test_metric1{foo="bar",namespace="ns-a",instance="",prometheus="cluster-level/te
"match[]": []string{"{foo='',__name__=~'.+'}"},
},
RespCode: http.StatusOK,
RespBody: ``,
RespBody: `# TYPE test_cluster_wide_metric untyped
test_cluster_wide_metric{namespace="caasglobal",instance="",prometheus="cluster-level/test"} 1 6000000
`,
},
"empty none-existing label value matches everything": {
Queries: url.Values{
"match[]": []string{"{bar='',__name__=~'.+'}"},
},
RespCode: http.StatusOK,
RespBody: `# TYPE test_metric1 untyped
RespBody: `# TYPE test_cluster_wide_metric untyped
test_cluster_wide_metric{namespace="caasglobal",instance="",prometheus="cluster-level/test"} 1 6000000
# TYPE test_metric1 untyped
test_metric1{foo="bar",namespace="ns-a",instance="",prometheus="cluster-level/test"} 10000 6000000
`,
},
Expand Down Expand Up @@ -353,7 +359,9 @@ test_metric2{foo="boo",instance="",prometheus="cluster-level/test"} 1 6000000
"match[]": []string{"{__name__=~'.+'}"},
},
RespCode: http.StatusOK,
RespBody: `# TYPE test_metric1 untyped
RespBody: `# TYPE test_cluster_wide_metric untyped
test_cluster_wide_metric{namespace="caasglobal",instance="",prometheus="cluster-level/test"} 1 6000000
# TYPE test_metric1 untyped
test_metric1{foo="bar",namespace="ns-a",instance="",prometheus="cluster-level/test"} 10000 6000000
test_metric1{foo="boo",namespace="ns-c",instance="",prometheus="cluster-level/test"} 1 6000000
# TYPE test_metric2 untyped
Expand All @@ -369,7 +377,9 @@ test_metric_without_labels{instance="",prometheus="cluster-level/test"} 1001 600
"match[]": []string{"{foo='',__name__=~'.+'}"},
},
RespCode: http.StatusOK,
RespBody: `# TYPE test_metric_old untyped
RespBody: `# TYPE test_cluster_wide_metric untyped
test_cluster_wide_metric{namespace="caasglobal",instance="",prometheus="cluster-level/test"} 1 6000000
# TYPE test_metric_old untyped
test_metric_old{instance="",prometheus="cluster-level/test"} 981 5880000
# TYPE test_metric_without_labels untyped
test_metric_without_labels{instance="",prometheus="cluster-level/test"} 1001 6000000
Expand All @@ -380,7 +390,9 @@ test_metric_without_labels{instance="",prometheus="cluster-level/test"} 1001 600
"match[]": []string{"{bar='',__name__=~'.+'}"},
},
RespCode: http.StatusOK,
RespBody: `# TYPE test_metric1 untyped
RespBody: `# TYPE test_cluster_wide_metric untyped
test_cluster_wide_metric{namespace="caasglobal",instance="",prometheus="cluster-level/test"} 1 6000000
# TYPE test_metric1 untyped
test_metric1{foo="bar",namespace="ns-a",instance="",prometheus="cluster-level/test"} 10000 6000000
test_metric1{foo="boo",namespace="ns-c",instance="",prometheus="cluster-level/test"} 1 6000000
# TYPE test_metric2 untyped
Expand Down
2 changes: 2 additions & 0 deletions pkg/agent/test/label.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ var MyTokenLabelScenarios = map[string]Scenario{
RespBody: &jsonResponseData{
Status: "success",
Data: []string{
"test_cluster_wide_metric",
"test_metric1",
"test_metric2",
"test_metric_old",
Expand All @@ -179,6 +180,7 @@ var MyTokenLabelScenarios = map[string]Scenario{
RespBody: &jsonResponseData{
Status: "success",
Data: []string{
"caasglobal",
"ns-a",
"ns-c",
},
Expand Down

0 comments on commit 755df0f

Please sign in to comment.