Skip to content

Commit

Permalink
Use testdata to implement rudimentary testings.
Browse files Browse the repository at this point in the history
  • Loading branch information
Sven Rebhan committed Jun 13, 2021
1 parent 932bf12 commit 873f59c
Show file tree
Hide file tree
Showing 3 changed files with 220 additions and 38 deletions.
181 changes: 143 additions & 38 deletions plugins/inputs/sql/sql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func pwgen(n int) string {

var spinup = flag.Bool("spinup", false, "Spin-up the required test containers")

func TestMysql(t *testing.T) {
func TestMariaDB(t *testing.T) {
if testing.Short() {
t.Skip("Skipping integration test in short mode")
}
Expand All @@ -43,7 +43,7 @@ func TestMysql(t *testing.T) {
addr := "127.0.0.1"
port := "3306"
passwd := ""
database := "nation"
database := "foo"

if *spinup {
logger.Infof("Spinning up container...")
Expand All @@ -52,7 +52,7 @@ func TestMysql(t *testing.T) {
passwd = pwgen(32)

// Determine the test-data mountpoint
testdata, err := filepath.Abs("testdata")
testdata, err := filepath.Abs("testdata/mariadb")
require.NoError(t, err, "determining absolute path of test-data failed")

// Spin-up the container
Expand All @@ -62,6 +62,7 @@ func TestMysql(t *testing.T) {
Image: "mariadb",
Env: map[string]string{
"MYSQL_ROOT_PASSWORD": passwd,
"MYSQL_DATABASE": database,
},
BindMounts: map[string]string{
testdata: "/docker-entrypoint-initdb.d",
Expand All @@ -71,16 +72,16 @@ func TestMysql(t *testing.T) {
},
Started: true,
}
mariadbContainer, err := testcontainers.GenericContainer(ctx, req)
container, err := testcontainers.GenericContainer(ctx, req)
require.NoError(t, err, "starting container failed")
defer func() {
require.NoError(t, mariadbContainer.Terminate(ctx), "terminating container failed")
require.NoError(t, container.Terminate(ctx), "terminating container failed")
}()

// Get the connection details from the container
addr, err = mariadbContainer.Host(ctx)
addr, err = container.Host(ctx)
require.NoError(t, err, "getting container host address failed")
p, err := mariadbContainer.MappedPort(ctx, "3306/tcp")
p, err := container.MappedPort(ctx, "3306/tcp")
require.NoError(t, err, "getting container host port failed")
port = p.Port()
}
Expand All @@ -92,44 +93,147 @@ func TestMysql(t *testing.T) {
expected []telegraf.Metric
}{
{
name: "guests",
name: "metric_one",
queries: []Query{
{
Query: "SELECT * FROM guests",
TagColumnsInclude: []string{"name"},
FieldColumnsExclude: []string{"name"},
Query: "SELECT * FROM metric_one",
TagColumnsInclude: []string{"tag_*"},
FieldColumnsExclude: []string{"tag_*", "timestamp"},
TimeColumn: "timestamp",
TimeFormat: "2006-01-02 15:04:05",
},
},
expected: []telegraf.Metric{
testutil.MustMetric(
"sql",
map[string]string{"name": "John"},
map[string]interface{}{"guest_id": int64(1)},
time.Unix(0, 0),
),
testutil.MustMetric(
"sql",
map[string]string{"name": "Jane"},
map[string]interface{}{"guest_id": int64(2)},
time.Unix(0, 0),
),
testutil.MustMetric(
"sql",
map[string]string{"name": "Jean"},
map[string]interface{}{"guest_id": int64(3)},
time.Unix(0, 0),
),
testutil.MustMetric(
"sql",
map[string]string{"name": "Storm"},
map[string]interface{}{"guest_id": int64(4)},
time.Unix(0, 0),
map[string]string{
"tag_one": "tag1",
"tag_two": "tag2",
},
map[string]interface{}{
"int64_one": int64(1234),
"int64_two": int64(2345),
},
time.Date(2021, 5, 17, 22, 4, 45, 0, time.UTC),
),
},
},
}

for _, tt := range testset {
t.Run(tt.name, func(t *testing.T) {
// Setup the plugin-under-test
plugin := &SQL{
Driver: "maria",
Dsn: fmt.Sprintf("root:%s@tcp(%s:%s)/%s", passwd, addr, port, database),
Queries: tt.queries,
Log: logger,
}

var acc testutil.Accumulator

// Startup the plugin
err := plugin.Init()
require.NoError(t, err)
err = plugin.Start(&acc)
require.NoError(t, err)

// Gather
err = plugin.Gather(&acc)
require.NoError(t, err)
require.Len(t, acc.Errors, 0)

// Stopping the plugin
plugin.Stop()

// Do the comparison
testutil.RequireMetricsEqual(t, tt.expected, acc.GetTelegrafMetrics())
})
}
}

func TestPostgreSQL(t *testing.T) {
if testing.Short() {
t.Skip("Skipping integration test in short mode")
}

logger := testutil.Logger{}

addr := "127.0.0.1"
port := "5432"
passwd := ""
database := "foo"

if *spinup {
logger.Infof("Spinning up container...")

// Generate a random password
passwd = pwgen(32)

// Determine the test-data mountpoint
testdata, err := filepath.Abs("testdata/postgres")
require.NoError(t, err, "determining absolute path of test-data failed")

// Spin-up the container
ctx := context.Background()
req := testcontainers.GenericContainerRequest{
ContainerRequest: testcontainers.ContainerRequest{
Image: "postgres",
Env: map[string]string{
"POSTGRES_PASSWORD": passwd,
"POSTGRES_DB": database,
},
BindMounts: map[string]string{
testdata: "/docker-entrypoint-initdb.d",
},
ExposedPorts: []string{"5432/tcp"},
WaitingFor: wait.ForListeningPort("5432/tcp"),
},
Started: true,
}
container, err := testcontainers.GenericContainer(ctx, req)
require.NoError(t, err, "starting container failed")
defer func() {
require.NoError(t, container.Terminate(ctx), "terminating container failed")
}()

// Get the connection details from the container
addr, err = container.Host(ctx)
require.NoError(t, err, "getting container host address failed")
p, err := container.MappedPort(ctx, "5432/tcp")
require.NoError(t, err, "getting container host port failed")
port = p.Port()
}

// Define the testset
var testset = []struct {
name string
queries []Query
expected []telegraf.Metric
}{
{
name: "metric_one",
queries: []Query{
{
Query: "SELECT * FROM metric_one",
TagColumnsInclude: []string{"tag_*"},
FieldColumnsExclude: []string{"tag_*", "timestamp"},
TimeColumn: "timestamp",
TimeFormat: "2006-01-02 15:04:05",
},
},
expected: []telegraf.Metric{
testutil.MustMetric(
"sql",
map[string]string{"name": "Beast"},
map[string]interface{}{"guest_id": int64(5)},
time.Unix(0, 0),
map[string]string{
"tag_one": "tag1",
"tag_two": "tag2",
},
map[string]interface{}{
"int64_one": int64(1234),
"int64_two": int64(2345),
},
time.Date(2021, 5, 17, 22, 4, 45, 0, time.UTC),
),
},
},
Expand All @@ -139,8 +243,8 @@ func TestMysql(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
// Setup the plugin-under-test
plugin := &SQL{
Driver: "mysql",
Dsn: fmt.Sprintf("root:%s@tcp(%s:%s)/%s", passwd, addr, port, database),
Driver: "pgx",
Dsn: fmt.Sprintf("postgres://postgres:%v@%v:%v/%v", passwd, addr, port, database),
Queries: tt.queries,
Log: logger,
}
Expand All @@ -156,12 +260,13 @@ func TestMysql(t *testing.T) {
// Gather
err = plugin.Gather(&acc)
require.NoError(t, err)
require.Len(t, acc.Errors, 0)

// Stopping the plugin
plugin.Stop()

// Do the comparison
testutil.RequireMetricsEqual(t, tt.expected, acc.GetTelegrafMetrics(), testutil.IgnoreTime())
testutil.RequireMetricsEqual(t, tt.expected, acc.GetTelegrafMetrics())
})
}
}
36 changes: 36 additions & 0 deletions plugins/inputs/sql/testdata/mariadb/expected.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `bar` (
`baz` int(11) DEFAULT NULL
);
/*!40101 SET character_set_client = @saved_cs_client */;
INSERT INTO `bar` VALUES (1);
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `metric three` (
`timestamp` timestamp NOT NULL DEFAULT current_timestamp(),
`tag four` text DEFAULT NULL,
`string two` text DEFAULT NULL
);
/*!40101 SET character_set_client = @saved_cs_client */;
INSERT INTO `metric three` VALUES ('2021-05-17 22:04:45','tag4','string2');
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `metric_one` (
`timestamp` timestamp NOT NULL DEFAULT current_timestamp(),
`tag_one` text DEFAULT NULL,
`tag_two` text DEFAULT NULL,
`int64_one` int(11) DEFAULT NULL,
`int64_two` int(11) DEFAULT NULL
);
/*!40101 SET character_set_client = @saved_cs_client */;
INSERT INTO `metric_one` VALUES ('2021-05-17 22:04:45','tag1','tag2',1234,2345);
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `metric_two` (
`timestamp` timestamp NOT NULL DEFAULT current_timestamp(),
`tag_three` text DEFAULT NULL,
`string_one` text DEFAULT NULL
);
/*!40101 SET character_set_client = @saved_cs_client */;
INSERT INTO `metric_two` VALUES ('2021-05-17 22:04:45','tag3','string1');
41 changes: 41 additions & 0 deletions plugins/inputs/sql/testdata/postgres/expected.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
SET statement_timeout = 0;
SET lock_timeout = 0;
SET idle_in_transaction_session_timeout = 0;
SET client_encoding = 'UTF8';
SET standard_conforming_strings = on;
SELECT pg_catalog.set_config('search_path', '', false);
SET check_function_bodies = false;
SET xmloption = content;
SET client_min_messages = warning;
SET row_security = off;
SET default_tablespace = '';
SET default_table_access_method = heap;
CREATE TABLE public."metric three" (
"timestamp" timestamp without time zone,
"tag four" text,
"string two" text
);
ALTER TABLE public."metric three" OWNER TO postgres;
CREATE TABLE public.metric_one (
"timestamp" timestamp without time zone,
tag_one text,
tag_two text,
int64_one integer,
int64_two integer
);
ALTER TABLE public.metric_one OWNER TO postgres;
CREATE TABLE public.metric_two (
"timestamp" timestamp without time zone,
tag_three text,
string_one text
);
ALTER TABLE public.metric_two OWNER TO postgres;
COPY public."metric three" ("timestamp", "tag four", "string two") FROM stdin;
2021-05-17 22:04:45 tag4 string2
\.
COPY public.metric_one ("timestamp", tag_one, tag_two, int64_one, int64_two) FROM stdin;
2021-05-17 22:04:45 tag1 tag2 1234 2345
\.
COPY public.metric_two ("timestamp", tag_three, string_one) FROM stdin;
2021-05-17 22:04:45 tag3 string1
\.

0 comments on commit 873f59c

Please sign in to comment.