From ac7bfc88f73fce61d6f42d6887dec4d07a4f6cc4 Mon Sep 17 00:00:00 2001 From: SuperQ Date: Sat, 5 Mar 2022 14:09:27 +0100 Subject: [PATCH] Add exporter GRANT to integration tests Add a test of the GRANT statement to the exporter integration test. Fixes: https://github.com/prometheus/mysqld_exporter/issues/622 Signed-off-by: SuperQ --- .circleci/config.yml | 3 +++ collector/exporter_test.go | 13 ++++++++++--- scripts/test_grant.sql | 5 +++++ 3 files changed, 18 insertions(+), 3 deletions(-) create mode 100644 scripts/test_grant.sql diff --git a/.circleci/config.yml b/.circleci/config.yml index 69c12a79..3c69b678 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -23,6 +23,7 @@ jobs: environment: MYSQL_ALLOW_EMPTY_PASSWORD: yes MYSQL_ROOT_HOST: '%' + DATA_SOURCE_NAME: 'exporter:integration-test@/' parameters: mysql_image: type: string @@ -31,6 +32,8 @@ jobs: - setup_remote_docker - run: docker version - run: docker-compose --version + - run: sudo apt-get install -y mysql-client + - run: mysql < scripts/test_grant.sql - run: make build - run: make test codespell: diff --git a/collector/exporter_test.go b/collector/exporter_test.go index 07ceea78..dbd0f0bd 100644 --- a/collector/exporter_test.go +++ b/collector/exporter_test.go @@ -26,7 +26,14 @@ import ( "github.com/smartystreets/goconvey/convey" ) -const dsn = "root@/mysql" +var testDSN = "root@/mysql" + +func init() { + testDSNEnv := os.Getenv("DATA_SOURCE_NAME") + if testDSNEnv != "" + testDSN = testDSNEnv + } +} func TestExporter(t *testing.T) { if testing.Short() { @@ -35,7 +42,7 @@ func TestExporter(t *testing.T) { exporter := New( context.Background(), - dsn, + testDSN, NewMetrics(), []Scraper{ ScrapeGlobalStatus{}, @@ -79,7 +86,7 @@ func TestGetMySQLVersion(t *testing.T) { logger = level.NewFilter(logger, level.AllowDebug()) convey.Convey("Version parsing", t, func() { - db, err := sql.Open("mysql", dsn) + db, err := sql.Open("mysql", testDSN) convey.So(err, convey.ShouldBeNil) defer db.Close() diff --git a/scripts/test_grant.sql b/scripts/test_grant.sql new file mode 100644 index 00000000..7543bda5 --- /dev/null +++ b/scripts/test_grant.sql @@ -0,0 +1,5 @@ +CREATE USER 'exporter'@'localhost' IDENTIFIED BY 'integration-test' WITH MAX_USER_CONNECTIONS 3; +GRANT PROCESS, REPLICATION CLIENT TO 'exporter'@'localhost'; +GRANT SELECT ON performance_schema.* TO 'exporter'@'localhost'; +GRANT SELECT ON information_schema.* TO 'exporter'@'localhost'; +