From e9a9eda9729643887d2166f9cb4b69ad092a63e3 Mon Sep 17 00:00:00 2001 From: Enrico Stahn Date: Thu, 12 Mar 2020 10:48:34 +1100 Subject: [PATCH] chore: add e2e tests --- Makefile | 4 +- README.md | 70 +++++++++++++++++++++++++++-------- test/docker-compose-e2e.yml | 32 ++++++++++++++++ test/docker-compose-local.yml | 31 ++++++++++++++++ test/docker-compose.yml | 4 +- test/e2e.bats | 34 +++++++++++++++++ 6 files changed, 156 insertions(+), 19 deletions(-) create mode 100644 test/docker-compose-e2e.yml create mode 100644 test/docker-compose-local.yml create mode 100644 test/e2e.bats diff --git a/Makefile b/Makefile index d06c434..d8a1932 100644 --- a/Makefile +++ b/Makefile @@ -19,9 +19,11 @@ test-coverage-html: ## Create a code coverage report in HTML go test -coverprofile .cover/cover.out ./... go tool cover -html .cover/cover.out +test-e2e: + bats tests/e2e.bats + lint: ## Run linters golangci-lint run fmt: ## Fix formatting issues goimports -w . - diff --git a/README.md b/README.md index 8ce8474..0e356a0 100644 --- a/README.md +++ b/README.md @@ -1,10 +1,9 @@ # php-fpm_exporter -[![CircleCI](https://circleci.com/gh/hipages/php-fpm_exporter.svg?style=shield)](https://circleci.com/gh/hipages/php-fpm_exporter) +![Test](https://github.com/hipages/php-fpm_exporter/workflows/Test/badge.svg) [![Go Report Card](https://goreportcard.com/badge/github.com/hipages/php-fpm_exporter)](https://goreportcard.com/report/github.com/hipages/php-fpm_exporter) [![GoDoc](https://godoc.org/github.com/hipages/php-fpm_exporter?status.svg)](https://godoc.org/github.com/hipages/php-fpm_exporter) -[![Maintainability](https://api.codeclimate.com/v1/badges/52f9e1f0388e8aa38bfe/maintainability)](https://codeclimate.com/github/hipages/php-fpm_exporter/maintainability) -[![Test Coverage](https://api.codeclimate.com/v1/badges/52f9e1f0388e8aa38bfe/test_coverage)](https://codeclimate.com/github/hipages/php-fpm_exporter/test_coverage) +[![Quality Gate Status](https://sonarcloud.io/api/project_badges/measure?project=hipages_php-fpm_exporter&metric=alert_status)](https://sonarcloud.io/dashboard?id=hipages_php-fpm_exporter) [![Docker Pulls](https://img.shields.io/docker/pulls/hipages/php-fpm_exporter.svg)](https://hub.docker.com/r/hipages/php-fpm_exporter/) [![Average time to resolve an issue](http://isitmaintained.com/badge/resolution/hipages/php-fpm_exporter.svg)](http://isitmaintained.com/project/hipages/php-fpm_exporter "Average time to resolve an issue") [![Percentage of issues still open](http://isitmaintained.com/badge/open/hipages/php-fpm_exporter.svg)](http://isitmaintained.com/project/hipages/php-fpm_exporter "Percentage of issues still open") @@ -16,6 +15,28 @@ The exporter connects directly to PHP-FPM and exports the metrics via HTTP. A webserver such as NGINX or Apache is **NOT** needed! +## Table of Contents + + + +- [Features](#features) +- [Usage](#usage) + * [Options and defaults](#options-and-defaults) + * [Why `--phpfpm.fix-process-count`?](#why---phpfpmfix-process-count) + * [CLI Examples](#cli-examples) + * [Docker Examples](#docker-examples) + * [Kubernetes Example](#kubernetes-example) +- [Metrics collected](#metrics-collected) +- [Grafana Dasbhoard for Kubernetes](#grafana-dasbhoard-for-kubernetes) +- [FAQ](#faq) +- [Development](#development) + * [E2E Tests](#e2e-tests) +- [Contributing](#contributing) +- [Contributors](#contributors) +- [Alternatives](#alternatives) + + + ## Features * Export single or multiple pools @@ -106,6 +127,8 @@ If you like to have a more granular reporting please use `phpfpm_process_state`. ### Kubernetes Example +TBD + ## Metrics collected ``` @@ -153,14 +176,6 @@ The Grafana dashboard can be found [here](https://grafana.com/dashboards/4912). -## Contributing - -Contributions are greatly appreciated. -The maintainers actively manage the issues list, and try to highlight issues suitable for newcomers. -The project follows the typical GitHub pull request model. -See " [How to Contribute to Open Source](https://opensource.guide/how-to-contribute/) " for more details. -Before starting any work, please either comment on an existing issue, or file a new one. - ## FAQ * **How to update "Metrics collected"?** @@ -170,11 +185,30 @@ Before starting any work, please either comment on an existing issue, or file a curl http://127.0.0.1:12345/metrics | grep phpfpm | grep "#" ``` -## Alternatives +## Development -* [bakins/php-fpm-exporter](https://github.com/bakins/php-fpm-exporter) -* [peakgames/php-fpm-prometheus](https://github.com/peakgames/php-fpm-prometheus) -* [craigmj/phpfpm_exporter](https://github.com/craigmj/phpfpm_exporter) +### E2E Tests + +The E2E tests are based on docker-compose and bats-core. Install the required components, e.g. via brew on MacOS: + +```bash +brew tap kaos/shell +brew install docker-compose bats-core kaos/shell/bats-assert kaos/shell/bats-support +``` + +After the components are installed run the E2E tests: + +```bash +make tests-e2e +``` + +## Contributing + +Contributions are greatly appreciated. +The maintainers actively manage the issues list, and try to highlight issues suitable for newcomers. +The project follows the typical GitHub pull request model. +See " [How to Contribute to Open Source](https://opensource.guide/how-to-contribute/) " for more details. +Before starting any work, please either comment on an existing issue, or file a new one. ## Contributors @@ -198,3 +232,9 @@ Thanks goes to these wonderful people ([emoji key](https://github.com/all-contri This project follows the [all-contributors](https://github.com/all-contributors/all-contributors) specification. Contributions of any kind welcome! + +## Alternatives + +* [bakins/php-fpm-exporter](https://github.com/bakins/php-fpm-exporter) +* [peakgames/php-fpm-prometheus](https://github.com/peakgames/php-fpm-prometheus) +* [craigmj/phpfpm_exporter](https://github.com/craigmj/phpfpm_exporter) diff --git a/test/docker-compose-e2e.yml b/test/docker-compose-e2e.yml new file mode 100644 index 0000000..b62ee28 --- /dev/null +++ b/test/docker-compose-e2e.yml @@ -0,0 +1,32 @@ +version: "3" + +services: + + phpfpm1: + image: hipages/php + ports: + - "9031:9000" + environment: + PHP_FPM_PM_STATUS_PATH: "/status" + + phpfpm2: + image: hipages/php + ports: + - "9032:9000" + environment: + PHP_FPM_PM_STATUS_PATH: "/status" + + phpfpm3: + image: hipages/php + ports: + - "9033:9000" + environment: + PHP_FPM_PM_STATUS_PATH: "/status" + + exporter: + image: hipages/php-fpm_exporter:latest + ports: + - "9253:9253" + environment: + PHP_FPM_SCRAPE_URI: "tcp://phpfpm1:9000/status,tcp://phpfpm2:9000/status,tcp://phpfpm3:9000/status" + PHP_FPM_LOG_LEVEL: "debug" diff --git a/test/docker-compose-local.yml b/test/docker-compose-local.yml new file mode 100644 index 0000000..ca99c38 --- /dev/null +++ b/test/docker-compose-local.yml @@ -0,0 +1,31 @@ +version: "3" + +services: + + prometheus: + image: quay.io/prometheus/prometheus:latest + ports: + - 9090:9090 + volumes: + - "./prometheus.yml:/etc/prometheus/prometheus.yml" + + phpfpm: + image: hipages/php + ports: + - "9000:9000" + environment: + PHP_FPM_PM_STATUS_PATH: "/status" + + phpfpm1: + image: hipages/php + ports: + - "9001:9001" + environment: + PHP_FPM_PM_STATUS_PATH: "/status" + + phpfpm2: + image: hipages/php + ports: + - "9002:9002" + environment: + PHP_FPM_PM_STATUS_PATH: "/status" diff --git a/test/docker-compose.yml b/test/docker-compose.yml index 2f158e0..4bb8e2e 100644 --- a/test/docker-compose.yml +++ b/test/docker-compose.yml @@ -11,13 +11,11 @@ services: phpfpm: image: hipages/php - ports: - - "9000:9000" environment: PHP_FPM_PM_STATUS_PATH: "/status" exporter: - image: hipages/php-fpm_exporter:0.0.4 + image: hipages/php-fpm_exporter:latest ports: - "9253:9253" environment: diff --git a/test/e2e.bats b/test/e2e.bats new file mode 100644 index 0000000..4be208c --- /dev/null +++ b/test/e2e.bats @@ -0,0 +1,34 @@ +#!/usr/bin/env bats + +load '/usr/local/lib/bats-support/load.bash' +load '/usr/local/lib/bats-assert/load.bash' + +setup () { + docker-compose -f ./test/docker-compose-e2e.yml up -d + # Workaround to wait for docker to start containers + sleep 5 + #go run main.go server --phpfpm.scrape-uri tcp://127.0.0.1:9031/status,tcp://127.0.0.1:9032/status 3>&- & +} + +teardown() { + docker-compose -f ./test/docker-compose-e2e.yml stop + docker-compose -f ./test/docker-compose-e2e.yml rm -f + docker-compose -f ./test/docker-compose-e2e.yml down --volumes +} + +@test "Should have metrics endpoint" { + run curl -sSL http://localhost:9253/metrics + [ "$status" -eq 0 ] +} + +@test "Should have metric phpfpm_up" { + run curl -sSL http://localhost:9253/metrics + assert_output --partial '# TYPE phpfpm_up gauge' +} + +@test "Should have scraped multiple PHP-FPM pools" { + run curl -sSL http://localhost:9253/metrics + assert_output --partial 'phpfpm_up{pool="www",scrape_uri="tcp://phpfpm1:9000/status"} 1' + assert_output --partial 'phpfpm_up{pool="www",scrape_uri="tcp://phpfpm2:9000/status"} 1' + assert_output --partial 'phpfpm_up{pool="www",scrape_uri="tcp://phpfpm3:9000/status"} 1' +}