Ver Fonte

Deep integration tests with docker

Nick Babcock há 6 anos atrás
pai
commit
14358a87ff

+ 5 - 0
.travis.yml

@@ -0,0 +1,5 @@
+language: rust
+sudo: required
+services:
+  - docker
+script: sudo docker-compose up --abort-on-container-exit --renew-anon-volumes --build --exit-code-from app

+ 6 - 0
Dockerfile.tests

@@ -0,0 +1,6 @@
+FROM mono:5
+COPY . /tmp/
+WORKDIR /tmp
+RUN msbuild /t:restore /p:TargetFrameworks=net461 && \
+  msbuild /t:build /p:TargetFrameworks=net461
+CMD mono ~/.nuget/packages/xunit.runner.console/2.4.0/tools/net461/xunit.console.exe OhmGraphite.Test/bin/Debug/net461/OhmGraphite.Test.dll

+ 3 - 0
Dockerfile.timescale

@@ -0,0 +1,3 @@
+FROM timescale/timescaledb:0.12.1-pg10
+COPY assets/setup-docker.sh /docker-entrypoint-initdb.d/.
+COPY assets/schema.sql /sql/schema.sql

+ 25 - 0
OhmGraphite.Test/GraphiteTest.cs

@@ -0,0 +1,25 @@
+using System;
+using System.Net.Http;
+using System.Threading;
+using Xunit;
+
+namespace OhmGraphite.Test
+{
+    public class GraphiteTest
+    {
+        [Fact, Trait("Category", "integration")]
+        public async void InsertGraphiteTest()
+        {
+            var writer = new GraphiteWriter("graphite", 2003, "my-pc", tags: false);
+            await writer.ReportMetrics(DateTime.Now, TestSensorCreator.Values());
+
+            // wait for carbon to sync to disk
+            Thread.Sleep(TimeSpan.FromSeconds(1));
+            var client = new HttpClient();
+            var resp = await client.GetAsync("http://graphite/render?format=csv&target=ohm.my-pc.intelcpu.0.temperature.cpucore.1");
+            Assert.True(resp.IsSuccessStatusCode);
+            var content = await resp.Content.ReadAsStringAsync();
+            Assert.True(content.Contains("ohm.my-pc.intelcpu.0.temperature.cpucore.1"));
+        }
+    }
+}

+ 20 - 1
OhmGraphite.Test/TimescaleTest.cs

@@ -9,7 +9,7 @@ namespace OhmGraphite.Test
         [Fact, Trait("Category", "integration")]
         public async void CanSetupTimescale()
         {
-            const string connStr = "Host=localhost;Username=postgres;Password=123456";
+            const string connStr = "Host=timescale;Username=postgres;Password=123456";
             var writer = new TimescaleWriter(connStr, true, "my-pc");
             var epoch = new DateTime(2001, 1, 13);
             await writer.ReportMetrics(epoch, TestSensorCreator.Values());
@@ -22,5 +22,24 @@ namespace OhmGraphite.Test
                 }
             }
         }
+
+        [Fact, Trait("Category", "integration")]
+        public async void InsertOnlyTimescale()
+        {
+            const string selectStr = "Host=timescale;Username=postgres;Password=123456;Database=timescale_built";
+
+            const string connStr = "Host=timescale;Username=ohm;Password=itsohm;Database=timescale_built";
+            var writer = new TimescaleWriter(connStr, false, "my-pc");
+            var epoch = new DateTime(2001, 1, 13);
+            await writer.ReportMetrics(epoch, TestSensorCreator.Values());
+            using (var conn = new NpgsqlConnection(selectStr))
+            {
+                conn.Open();
+                using (var cmd = new NpgsqlCommand("SELECT COUNT(*) FROM ohm_stats", conn))
+                {
+                    Assert.Equal(3, Convert.ToInt32(cmd.ExecuteScalar()));
+                }
+            }
+        }
     }
 }

+ 15 - 0
assets/schema.sql

@@ -0,0 +1,15 @@
+CREATE TABLE IF NOT EXISTS ohm_stats (
+   time TIMESTAMPTZ NOT NULL,
+   host TEXT,
+   hardware TEXT,
+   hardware_type TEXT,
+   identifier TEXT,
+   sensor TEXT,
+   sensor_type TEXT,
+   sensor_index INT,
+   value REAL
+);
+
+SELECT create_hypertable('ohm_stats', 'time', if_not_exists => TRUE);
+CREATE INDEX IF NOT EXISTS idx_ohm_host ON ohm_stats (host);
+CREATE INDEX IF NOT EXISTS idx_ohm_identifier ON ohm_stats (identifier);

+ 16 - 0
assets/setup-docker.sh

@@ -0,0 +1,16 @@
+#!/bin/bash
+set -e
+
+DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null && pwd )"
+
+SCHEMA=$(cat "$DIR/../sql/schema.sql")
+
+psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --dbname "$POSTGRES_DB" <<EOF
+	CREATE DATABASE timescale_built;
+EOF
+
+psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --dbname "timescale_built" <<EOF
+    CREATE USER ohm WITH PASSWORD 'itsohm';
+    $SCHEMA
+    GRANT INSERT ON ohm_stats TO ohm;
+EOF

+ 177 - 0
assets/wait-for-it.sh

@@ -0,0 +1,177 @@
+#!/usr/bin/env bash
+#   Use this script to test if a given TCP host/port are available
+
+cmdname=$(basename $0)
+
+echoerr() { if [[ $QUIET -ne 1 ]]; then echo "$@" 1>&2; fi }
+
+usage()
+{
+    cat << USAGE >&2
+Usage:
+    $cmdname host:port [-s] [-t timeout] [-- command args]
+    -h HOST | --host=HOST       Host or IP under test
+    -p PORT | --port=PORT       TCP port under test
+                                Alternatively, you specify the host and port as host:port
+    -s | --strict               Only execute subcommand if the test succeeds
+    -q | --quiet                Don't output any status messages
+    -t TIMEOUT | --timeout=TIMEOUT
+                                Timeout in seconds, zero for no timeout
+    -- COMMAND ARGS             Execute command with args after the test finishes
+USAGE
+    exit 1
+}
+
+wait_for()
+{
+    if [[ $TIMEOUT -gt 0 ]]; then
+        echoerr "$cmdname: waiting $TIMEOUT seconds for $HOST:$PORT"
+    else
+        echoerr "$cmdname: waiting for $HOST:$PORT without a timeout"
+    fi
+    start_ts=$(date +%s)
+    while :
+    do
+        if [[ $ISBUSY -eq 1 ]]; then
+            nc -z $HOST $PORT
+            result=$?
+        else
+            (echo > /dev/tcp/$HOST/$PORT) >/dev/null 2>&1
+            result=$?
+        fi
+        if [[ $result -eq 0 ]]; then
+            end_ts=$(date +%s)
+            echoerr "$cmdname: $HOST:$PORT is available after $((end_ts - start_ts)) seconds"
+            break
+        fi
+        sleep 1
+    done
+    return $result
+}
+
+wait_for_wrapper()
+{
+    # In order to support SIGINT during timeout: http://unix.stackexchange.com/a/57692
+    if [[ $QUIET -eq 1 ]]; then
+        timeout $BUSYTIMEFLAG $TIMEOUT $0 --quiet --child --host=$HOST --port=$PORT --timeout=$TIMEOUT &
+    else
+        timeout $BUSYTIMEFLAG $TIMEOUT $0 --child --host=$HOST --port=$PORT --timeout=$TIMEOUT &
+    fi
+    PID=$!
+    trap "kill -INT -$PID" INT
+    wait $PID
+    RESULT=$?
+    if [[ $RESULT -ne 0 ]]; then
+        echoerr "$cmdname: timeout occurred after waiting $TIMEOUT seconds for $HOST:$PORT"
+    fi
+    return $RESULT
+}
+
+# process arguments
+while [[ $# -gt 0 ]]
+do
+    case "$1" in
+        *:* )
+        hostport=(${1//:/ })
+        HOST=${hostport[0]}
+        PORT=${hostport[1]}
+        shift 1
+        ;;
+        --child)
+        CHILD=1
+        shift 1
+        ;;
+        -q | --quiet)
+        QUIET=1
+        shift 1
+        ;;
+        -s | --strict)
+        STRICT=1
+        shift 1
+        ;;
+        -h)
+        HOST="$2"
+        if [[ $HOST == "" ]]; then break; fi
+        shift 2
+        ;;
+        --host=*)
+        HOST="${1#*=}"
+        shift 1
+        ;;
+        -p)
+        PORT="$2"
+        if [[ $PORT == "" ]]; then break; fi
+        shift 2
+        ;;
+        --port=*)
+        PORT="${1#*=}"
+        shift 1
+        ;;
+        -t)
+        TIMEOUT="$2"
+        if [[ $TIMEOUT == "" ]]; then break; fi
+        shift 2
+        ;;
+        --timeout=*)
+        TIMEOUT="${1#*=}"
+        shift 1
+        ;;
+        --)
+        shift
+        CLI=("$@")
+        break
+        ;;
+        --help)
+        usage
+        ;;
+        *)
+        echoerr "Unknown argument: $1"
+        usage
+        ;;
+    esac
+done
+
+if [[ "$HOST" == "" || "$PORT" == "" ]]; then
+    echoerr "Error: you need to provide a host and port to test."
+    usage
+fi
+
+TIMEOUT=${TIMEOUT:-15}
+STRICT=${STRICT:-0}
+CHILD=${CHILD:-0}
+QUIET=${QUIET:-0}
+
+# check to see if timeout is from busybox?
+# check to see if timeout is from busybox?
+TIMEOUT_PATH=$(realpath $(which timeout))
+if [[ $TIMEOUT_PATH =~ "busybox" ]]; then
+        ISBUSY=1
+        BUSYTIMEFLAG="-t"
+else
+        ISBUSY=0
+        BUSYTIMEFLAG=""
+fi
+
+if [[ $CHILD -gt 0 ]]; then
+    wait_for
+    RESULT=$?
+    exit $RESULT
+else
+    if [[ $TIMEOUT -gt 0 ]]; then
+        wait_for_wrapper
+        RESULT=$?
+    else
+        wait_for
+        RESULT=$?
+    fi
+fi
+
+if [[ $CLI != "" ]]; then
+    if [[ $RESULT -ne 0 && $STRICT -eq 1 ]]; then
+        echoerr "$cmdname: strict mode, refusing to execute subprocess"
+        exit $RESULT
+    fi
+    exec "${CLI[@]}"
+else
+    exit $RESULT
+fi

+ 15 - 0
docker-compose.yml

@@ -0,0 +1,15 @@
+version: '3'
+services:
+  graphite:
+    image: graphiteapp/graphite-statsd
+  timescale:
+    build:
+      dockerfile: Dockerfile.timescale
+      context: .
+    environment:
+      POSTGRES_PASSWORD: 123456
+  app:
+    build:
+      dockerfile: Dockerfile.tests
+      context: .
+    command: ./assets/wait-for-it.sh timescale:5432 -- ./assets/wait-for-it.sh graphite:80 -- mono /root/.nuget/packages/xunit.runner.console/2.4.0/tools/net461/xunit.console.exe OhmGraphite.Test/bin/Debug/net461/OhmGraphite.Test.dll