-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from celo-org/tests-readme
Tests readme
- Loading branch information
Showing
6 changed files
with
102 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,6 +21,9 @@ jobs: | |
- name: Build the project | ||
run: | | ||
make build | ||
- name: Run the test | ||
run: | | ||
make test | ||
socket-exporter-build-dev: | ||
uses: celo-org/reusable-workflows/.github/workflows/[email protected] | ||
|
@@ -30,7 +33,7 @@ jobs: | |
contents: read | ||
id-token: write | ||
security-events: write | ||
if: ${{ github.ref != 'refs/heads/main' && startsWith(github.ref, 'refs/heads/') }} | ||
if: ${{ github.ref != 'refs/heads/main' && startsWith(github.ref, 'refs/pull/') }} | ||
with: | ||
workload-id-provider: 'projects/1094498259535/locations/global/workloadIdentityPools/gh-socket-exporter/providers/github-by-repos' | ||
service-account: '[email protected]' | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# socket-dev-exporter | ||
|
||
A simple Prometheus exporter to expose [Socket.dev](https://socket.dev/) scores for the latest versions of `@celo` NPM packages written in go. | ||
|
||
This exporter exposes metrics in port `9101`, path `/metrics`, with the following format: | ||
|
||
```txt | ||
# HELP socket_score Shows socket.dev packages scores | ||
# TYPE socket_score gauge | ||
socket_score{package="@celo/0x-contracts",score="license",version="2.1.2-0.0"} 0.8629757195290285 | ||
socket_score{package="@celo/0x-contracts",score="maintenance",version="2.1.2-0.0"} 0.6968453019359488 | ||
socket_score{package="@celo/0x-contracts",score="miscellaneous",version="2.1.2-0.0"} 0 | ||
socket_score{package="@celo/0x-contracts",score="quality",version="2.1.2-0.0"} 0.6410426253533731 | ||
socket_score{package="@celo/0x-contracts",score="supplychainrisk",version="2.1.2-0.0"} 0.39592272547306173 | ||
socket_score{package="@celo/0x-contracts",score="vulnerability",version="2.1.2-0.0"} 0.25 | ||
... | ||
``` | ||
|
||
## Configuration | ||
|
||
3 environmental variables are available to configure this exporter: | ||
|
||
- `API_TOKEN` (REQUIRED): A [Socket.dev](https://socket.dev/) API token. | ||
- `LOG_LEVEL`: The [Logrus](https://github.com/sirupsen/logrus) log level. If not set, defaults to `info`. | ||
- `PERIOD`: The period to refresh the [Socket.dev](https://socket.dev/) scores, in hours. If not set, defaults to `24`. | ||
|
||
## Tests | ||
|
||
Tests can be found in [`main_tests.go`](./main_test.go). | ||
|
||
## CI/CD | ||
|
||
The CI/CD pipeline is defined as [GitHub Action workflow](.github/workflows/ci-cd.yaml) with the following jobs: | ||
|
||
- With each PR, commit to `main` or release the code will be built and tested. | ||
- With each PR, a Docker image will be pushed to `us-west1-docker.pkg.dev/devopsre/dev-images/socket-exporter` with tag `test`. | ||
- With each commit to `main`, a Docker image will be pushed to `us-west1-docker.pkg.dev/devopsre/socket-exporter/socket-exporter` with tag `latest`. | ||
- With each release, a Docker image will be pushed to `us-west1-docker.pkg.dev/devopsre/socket-exporter/socket-exporter` with the same tag as the release tag. | ||
|
||
The Dockerfile for building the Docker image can be found [here](./Dockerfile). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package main | ||
|
||
import ( | ||
"os" | ||
"os/exec" | ||
"testing" | ||
|
||
"github.com/prometheus/client_golang/prometheus" | ||
"github.com/prometheus/client_golang/prometheus/testutil" | ||
) | ||
|
||
// Source https://stackoverflow.com/a/33404435 | ||
// Exit with return code 1 if env. var. is not provided | ||
func TestApiTokenCrash(t *testing.T) { | ||
if os.Getenv("BE_CRASHER") == "1" { | ||
main() | ||
return | ||
} | ||
cmd := exec.Command(os.Args[0], "-test.run=TestApiTokenCrash") | ||
cmd.Env = append(os.Environ(), "BE_CRASHER=1") | ||
err := cmd.Run() | ||
if e, ok := err.(*exec.ExitError); ok && !e.Success() { | ||
return | ||
} | ||
t.Fatalf("process ran with err %v, want exit status 1", err) | ||
} | ||
|
||
var collector = newSocketCollector() | ||
var ch = make(chan prometheus.Metric) | ||
|
||
// Count that the number of metrics is greater than 1 | ||
func TestCollectAndCount(t *testing.T) { | ||
|
||
updateMetrics() | ||
|
||
number := testutil.CollectAndCount(collector, "socket_score") | ||
if number < 1 { | ||
t.Fatalf("Less than 1 metric was returned. Only %d metrics returned", number) | ||
} | ||
|
||
} | ||
|
||
// Check linter on metrics | ||
func TestCollectndLint(t *testing.T) { | ||
|
||
problem, err := testutil.CollectAndLint(collector, "socket_score") | ||
if err != nil { | ||
t.Errorf("%s", err.Error()) | ||
t.Errorf("%s", problem) | ||
} | ||
|
||
} |