Skip to content

Commit

Permalink
feat(scheme/plugin): make sure that plugin records within 1 hours
Browse files Browse the repository at this point in the history
  • Loading branch information
macrat committed Dec 23, 2022
1 parent eb8459e commit dd58373
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 8 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,7 @@ For example, if the target URL has the scheme `xxx-yyy:`, Ayd will search these
The scheme names that supported by Ayd, `ayd`, and `alert`, are reserved and cannot be used by plugins.

The plugin prints result to stdout, in the same format as [log file](#log-file).
Plugins should not report results that are more than 1 hour old.
Plugins should not report future results, or old results more than 1 hour.

Ayd expects the output of the plugin to be in UTF-8.
However, in Windows, the system's default character encoding can be used.
Expand Down
15 changes: 15 additions & 0 deletions internal/scheme/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ import (
api "github.com/macrat/ayd/lib-ayd"
)

var (
// currentTime returns the current time.
// This function can override for testing.
currentTime = time.Now
)

// PluginScheme is the plugin handler. This implements both of Prober interface and Alerter interface.
type PluginScheme struct {
target *api.URL
Expand Down Expand Up @@ -141,6 +147,15 @@ func (p PluginScheme) execute(ctx context.Context, r Reporter, scope string, arg

rec.Time = rec.Time.Local()

current := currentTime()
if rec.Time.After(current) {
rec.Time = current
}
min := current.Add(-1 * time.Hour)
if rec.Time.Before(min) {
rec.Time = min
}

count++
r.Report(p.target, rec)
}
Expand Down
10 changes: 10 additions & 0 deletions internal/scheme/plugin_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,20 @@ import (
"os"
"path/filepath"
"testing"
"time"

"github.com/google/go-cmp/cmp"
)

func SetCurrentTime(t *testing.T, ct time.Time) {
currentTime = func() time.Time {
return ct
}
t.Cleanup(func() {
currentTime = time.Now
})
}

// PreparePluginPath set PATH to ./testdata/ directory.
func PreparePluginPath(t *testing.T) {
t.Helper()
Expand Down
48 changes: 47 additions & 1 deletion internal/scheme/plugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,12 +127,13 @@ func TestPluginScheme_Probe(t *testing.T) {
func TestPluginScheme_Probe_timezone(t *testing.T) {
PreparePluginPath(t)
t.Setenv("TZ", "UTC")
scheme.SetCurrentTime(t, time.Date(2001, 2, 3, 16, 10, 0, 0, time.UTC))

tests := []struct {
URL string
Time time.Time
}{
{"plug:+0900", time.Date(2001, 2, 3, 16-9, 5, 6, 0, time.UTC)},
{"plug:+0900", time.Date(2001, 2, 3, 16, 5, 6, 0, time.UTC)},
{"plug-plus:utc", time.Date(2001, 2, 3, 16, 5, 6, 0, time.UTC)},
}

Expand Down Expand Up @@ -161,6 +162,51 @@ func TestPluginScheme_Probe_timezone(t *testing.T) {
}
}

func TestPluginScheme_Probe_trimTime(t *testing.T) {
PreparePluginPath(t)
t.Setenv("TZ", "UTC")

p, err := scheme.NewProber("plug:")
if err != nil {
t.Fatalf("failed to create plugin: %s", err)
}

base := time.Date(2001, 2, 3, 16, 5, 6, 0, time.UTC)

tests := []struct {
Cur time.Time
Out time.Time
}{
{base, base},
{base.Add(1 * time.Minute), base},
{base.Add(50 * time.Minute), base},
{base.Add(60 * time.Minute), base},
{base.Add(61 * time.Minute), base.Add(1 * time.Minute)},
{base.Add(-1 * time.Minute), base.Add(-1 * time.Minute)},
}

for i, tt := range tests {
scheme.SetCurrentTime(t, tt.Cur)

ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()

rs := testutil.RunProbe(ctx, p)

if len(rs) != 1 {
t.Fatalf("%d: %s: unexpected number of results: %d", i, tt.Cur, len(rs))
}

if rs[0].Target.String() != "plug:" {
t.Errorf("%d: %s: unexpected target: %s", i, tt.Cur, rs[0].Target)
}

if !rs[0].Time.Equal(tt.Out) {
t.Errorf("%d: %s: unexpected time: %s", i, tt.Cur, rs[0].Time)
}
}
}

func TestPluginScheme_Alert(t *testing.T) {
t.Parallel()
PreparePluginPath(t)
Expand Down
6 changes: 3 additions & 3 deletions internal/scheme/testdata/ayd-plug-probe
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#!/bin/sh

if [ "$1" = "plug:change" ]; then
echo '{"time":"2001-02-03T16:05:06+09:00","status":"HEALTHY","latency":123.456,"target":"changed:plug","message":"check changed:plug"}'
echo '{"time":"2001-02-03T17:05:06+01:00","status":"HEALTHY","latency":123.456,"target":"changed:plug","message":"check changed:plug"}'
elif [ "$1" = "plug:extra" ]; then
echo '{"time":"2001-02-03T16:05:06+09:00","status":"HEALTHY","latency":123.456,"target":"'${1}'","message":"with extra","hello":"world"}'
echo '{"time":"2001-02-03T17:05:06+01:00","status":"HEALTHY","latency":123.456,"target":"'${1}'","message":"with extra","hello":"world"}'
elif [ "$1" != "plug:empty" ]; then
echo '{"time":"2001-02-03T16:05:06+09:00","status":"HEALTHY","latency":123.456,"target":"'${1}'","message":"check '${1}'"}'
echo '{"time":"2001-02-03T17:05:06+01:00","status":"HEALTHY","latency":123.456,"target":"'${1}'","message":"check '${1}'"}'
fi

if [ "$1" = "plug:invalid-record" ]; then
Expand Down
6 changes: 3 additions & 3 deletions internal/scheme/testdata/ayd-plug-probe.bat
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
@echo off

if "%1" == "plug:change" (
echo {"time":"2001-02-03T16:05:06+09:00","status":"HEALTHY","latency":123.456,"target":"changed:plug","message":"check changed:plug"}
echo {"time":"2001-02-03T17:05:06+01:00","status":"HEALTHY","latency":123.456,"target":"changed:plug","message":"check changed:plug"}
) else if "%1" == "plug:extra" (
echo {"time":"2001-02-03T16:05:06+09:00","status":"HEALTHY","latency":123.456,"target":"%1","message":"with extra","hello":"world"}
echo {"time":"2001-02-03T17:05:06+01:00","status":"HEALTHY","latency":123.456,"target":"%1","message":"with extra","hello":"world"}
) else if not "%1" == "plug:empty" (
echo {"time":"2001-02-03T16:05:06+09:00","status":"HEALTHY","latency":123.456,"target":"%1","message":"check %1"}
echo {"time":"2001-02-03T17:05:06+01:00","status":"HEALTHY","latency":123.456,"target":"%1","message":"check %1"}
)

if "%1" == "plug:invalid-record" (
Expand Down

0 comments on commit dd58373

Please sign in to comment.