Skip to content

Commit

Permalink
Merge a87bc12 into b5c422b
Browse files Browse the repository at this point in the history
  • Loading branch information
ne-sachirou committed May 28, 2020
2 parents b5c422b + a87bc12 commit a0e8a4a
Show file tree
Hide file tree
Showing 11 changed files with 122 additions and 63 deletions.
42 changes: 42 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
---
name: test

on:
push:
branches:
- master
pull_request:

jobs:
test:
strategy:
matrix:
go: ["1.14.x"]
os: [ubuntu-latest]
runs-on: ${{ matrix.os }}
steps:
- run: sudo apt-get install memcached
- uses: actions/setup-go@v2
with:
go-version: ${{ matrix.go }}
- uses: actions/checkout@v2
- run: |
make lint
make cover
test-windows:
strategy:
matrix:
go: ["1.14.x"]
runs-on: windows-latest
steps:
- uses: actions/setup-go@v2
with:
go-version: ${{ matrix.go }}
- uses: actions/checkout@v2
- run: |
go build -o check-log/check-log.exe ./check-log
go build -o check-procs/check-procs.exe ./check-procs
go build -o check-windows-eventlog/check-windows-evenglog.exe ./check-windows-eventlog
go vet -all ./check-log/... ./check-procs/... ./check-ntservice/... ./check-windows-eventlog/...
go test ./check-log/... ./check-procs/... ./check-ntservice/... ./check-windows-eventlog/...
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
.*
!/.dependabot
!/.github
!.gitignore
!.goxc.json
!.travis.yml
Expand Down
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
language: go
go:
- 1.12.x
- 1.14.x
env:
global:
- GO111MODULE=on
Expand Down
3 changes: 1 addition & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ test: lint
.PHONY: devel-deps
devel-deps:
cd && go get golang.org/x/lint/golint \
github.com/pierrre/gotestcover \
github.com/mattn/goveralls

.PHONY: check-release-deps
Expand All @@ -44,7 +43,7 @@ testconvention:

.PHONY: cover
cover: devel-deps
gotestcover -v -short -covermode=count -coverprofile=.profile.cov -parallelpackages=4 ./...
go test -race -covermode atomic -coverprofile=.profile.cov ./...

.PHONY: build
build:
Expand Down
3 changes: 2 additions & 1 deletion appveyor.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
version: 0.9.4-{build}
image: Previous Visual Studio 2019
clone_folder: c:\gopath\src\github.com\mackerelio\go-check-plugins
environment:
GOPATH: c:\gopath
GOROOT: c:\go112-x86
GOROOT: c:\go114
install:
- set PATH=%GOROOT%\bin;%GOPATH%\bin;%PATH%;C:\MinGW\bin
- echo %Path%
Expand Down
24 changes: 12 additions & 12 deletions check-http/lib/check_http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ import (

func TestRun(t *testing.T) {
ckr := Run([]string{"-u", "hoge"})
assert.Equal(t, ckr.Status, checkers.CRITICAL, "chr.Status should be CRITICAL")
assert.Equal(t, ckr.Message, `Get hoge: unsupported protocol scheme ""`, "something went wrong")
assert.Equal(t, checkers.CRITICAL, ckr.Status, "chr.Status should be CRITICAL")
assert.Equal(t, `Get "hoge": unsupported protocol scheme ""`, ckr.Message, "something went wrong")
}

func TestNoCheckCertificate(t *testing.T) {
ckr := Run([]string{"--no-check-certificate", "-u", "hoge"})
assert.Equal(t, ckr.Status, checkers.CRITICAL, "chr.Status should be CRITICAL")
assert.Equal(t, ckr.Message, `Get hoge: unsupported protocol scheme ""`, "something went wrong")
assert.Equal(t, checkers.CRITICAL, ckr.Status, "chr.Status should be CRITICAL")
assert.Equal(t, `Get "hoge": unsupported protocol scheme ""`, ckr.Message, "something went wrong")
}

func TestStatusRange(t *testing.T) {
Expand Down Expand Up @@ -70,13 +70,13 @@ func TestStatusRange(t *testing.T) {
}
for _, tt := range tests {
ckr := Run(tt.args)
assert.Equal(t, ckr.Status, tt.want, fmt.Sprintf("chr.Status wrong: %v", ckr.Status))
assert.Equal(t, tt.want, ckr.Status, fmt.Sprintf("chr.Status wrong: %v", ckr.Status))
}
}

func TestSourceIP(t *testing.T) {
ckr := Run([]string{"-u", "hoge", "-i", "1.2.3"})
assert.Equal(t, ckr.Status, checkers.UNKNOWN, "chr.Status should be UNKNOWN")
assert.Equal(t, checkers.UNKNOWN, ckr.Status, "chr.Status should be UNKNOWN")
}

func TestHost(t *testing.T) {
Expand All @@ -96,7 +96,7 @@ func TestHost(t *testing.T) {
"-u", ts.URL,
})

assert.Equal(t, ckr.Status, checkers.OK, "ckr.Status should be OK")
assert.Equal(t, checkers.OK, ckr.Status, "ckr.Status should be OK")
}

func TestExpectedContent(t *testing.T) {
Expand Down Expand Up @@ -133,7 +133,7 @@ func TestExpectedContent(t *testing.T) {

for i, tc := range testCases {
ckr := Run([]string{"-u", ts.URL, "-p", tc.regexp})
assert.Equal(t, ckr.Status, tc.status, "#%d: Status should be %s", i, tc.status)
assert.Equal(t, tc.status, ckr.Status, "#%d: Status should be %s", i, tc.status)
}
}

Expand Down Expand Up @@ -169,7 +169,7 @@ func TestMaxRedirects(t *testing.T) {

for i, tc := range testCases {
ckr := Run(tc.args)
assert.Equal(t, ckr.Status, tc.want, "#%d: Status should be %s", i, tc.want)
assert.Equal(t, tc.want, ckr.Status, "#%d: Status should be %s", i, tc.want)
}
}

Expand Down Expand Up @@ -282,7 +282,7 @@ func TestConnectTos(t *testing.T) {

for i, tc := range testCases {
ckr := Run(tc.args)
assert.Equal(t, ckr.Status, tc.want, "#%d: Status should be %s, %s", i, tc.want, ckr.Message)
assert.Equal(t, tc.want, ckr.Status, "#%d: Status should be %s, %s", i, tc.want, ckr.Message)
}
}

Expand Down Expand Up @@ -334,7 +334,7 @@ func TestProxy(t *testing.T) {

for i, tc := range testCases {
ckr := Run(tc.args)
assert.Equal(t, ckr.Status, tc.want, "#%d: Status should be %s, %s", i, tc.want, ckr.Message)
assert.Equal(t, tc.want, ckr.Status, "#%d: Status should be %s, %s", i, tc.want, ckr.Message)
}
}

Expand Down Expand Up @@ -383,6 +383,6 @@ func TestProxy_Auth(t *testing.T) {

for i, tc := range testCases {
ckr := Run(tc.args)
assert.Equal(t, ckr.Status, tc.want, "#%d: Status should be %s, %s", i, tc.want, ckr.Message)
assert.Equal(t, tc.want, ckr.Status, "#%d: Status should be %s, %s", i, tc.want, ckr.Message)
}
}
12 changes: 4 additions & 8 deletions check-log/lib/check-log_state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,11 @@ func TestLoadStateIfFileNotExist(t *testing.T) {
}

func TestLoadStateIfAccessDenied(t *testing.T) {
var file string
switch runtime.GOOS {
case "windows":
file = `C:\pagefile.sys`
default:
file = "testdata/plain.txt/file"
t.Skip()
}
file := "testdata/plain.txt/file"
s, err := loadState(file)
if err == nil {
t.Errorf("loadState(%q) = %v; want an error", file, s)
Expand Down Expand Up @@ -192,13 +190,11 @@ func TestGetBytesToSkipOldIfFileNotExist(t *testing.T) {
}

func TestGetBytesToSkipOldErr(t *testing.T) {
var file string
switch runtime.GOOS {
case "windows":
file = `C:\pagefile.sys`
default:
file = "testdata/plain.txt/file"
t.Skip()
}
file := "testdata/plain.txt/file"
n, err := getBytesToSkipOld(file)
if err == nil {
t.Errorf("getBytesToSkip(%s) = %d; but want an error", file, n)
Expand Down
12 changes: 6 additions & 6 deletions check-log/lib/check-log_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,16 +245,16 @@ func TestRun(t *testing.T) {
}, 1)
}
testCancel := func() {
ctx, cancel := context.WithCancel(context.Background())
go func() {
<-time.After(10 * time.Millisecond)
cancel()
}()
// This test checks searchLog keeps reading until EOL even if ctx is cancelled.
// To guarantee to read at least once, a timeout sec
// should be choice it is greater than reading the state file.
fh.WriteString("OK\nFATAL\nFATAL\n")
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Millisecond)
defer cancel()

expected := time.Now().Add(30 * time.Millisecond)
w, c, errLines, err := opts.searchLog(ctx, logf)
assert.WithinDuration(t, expected, time.Now(), 15*time.Millisecond, "searching time exceeded")
assert.WithinDuration(t, expected, time.Now(), 18*time.Millisecond, "searching time exceeded")

assert.Equal(t, err, nil, "err should be nil")
assert.Equal(t, int64(0), w, "something went wrong")
Expand Down
68 changes: 52 additions & 16 deletions check-memcached/lib/check-memcached_test.go
Original file line number Diff line number Diff line change
@@ -1,42 +1,78 @@
package checkmemcached

import (
"fmt"
"log"
"context"
"net"
"os/exec"
"syscall"
"testing"
"time"

"github.com/lestrrat/go-tcptest"
)

func TestMemd(t *testing.T) {
var cmd *exec.Cmd
memd := func(t *tcptest.TCPTest) {
cmd = exec.Command("memcached", "-p", fmt.Sprintf("%d", t.Port()))
cmd.SysProcAttr = &syscall.SysProcAttr{
Setpgid: true,
func allocUnusedPort() (string, error) {
l, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
return "", err
}
_, port, err := net.SplitHostPort(l.Addr().String())
if err != nil {
return "", err
}
l.Close()
return port, nil
}

func waitForPort(ctx context.Context, port string) error {
addr := net.JoinHostPort("127.0.0.1", port)
d, _ := ctx.Deadline()
dialer := net.Dialer{
Deadline: d,
}
for ctx.Err() == nil {
c, err := dialer.Dial("tcp", addr)
if err != nil {
time.Sleep(100 * time.Millisecond)
continue
}
cmd.Run()
c.Close()
return nil
}
return ctx.Err()
}

server, err := tcptest.Start2(memd, 30*time.Second)
func TestMemd(t *testing.T) {
if _, err := exec.LookPath("memcached"); err != nil {
t.Skip("memcached is not installed")
}
port, err := allocUnusedPort()
if err != nil {
log.Fatalf("Failed to start memcached: %s", err)
t.Fatal("allocUnusedPort:", err)
}
t.Logf("memcached started on port %d", server.Port())
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
cmd := exec.CommandContext(ctx, "memcached", "-p", port)
cmd.SysProcAttr = &syscall.SysProcAttr{
Setpgid: true,
}
if err := cmd.Start(); err != nil {
t.Fatal("Start:", err)
}
if err := waitForPort(ctx, port); err != nil {
t.Fatal("waitForPort:", err)
}

t.Logf("memcached started on port %s", port)
defer func() {
if cmd != nil && cmd.Process != nil {
cmd.Process.Signal(syscall.SIGTERM)
}
}()

argv := []string{"-p", fmt.Sprintf("%d", server.Port()), "-k", "test"}
argv := []string{"-p", port, "-k", "test"}
ckr := run(argv)
if ckr.Status.String() != "OK" {
t.Errorf("failed to check memcache:%s", ckr)
}
cmd.Process.Signal(syscall.SIGTERM)
server.Wait()
cmd.Wait()
}
6 changes: 1 addition & 5 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/mackerelio/go-check-plugins

go 1.13
go 1.14

require (
github.com/AdRoll/goamz v0.0.0-20170825154802-2731d20f46f4
Expand All @@ -15,8 +15,6 @@ require (
github.com/go-ldap/ldap/v3 v3.1.10
github.com/go-ole/go-ole v1.2.4
github.com/jessevdk/go-flags v1.4.0
github.com/lestrrat/go-tcptest v0.0.0-20180223004105-0f9f9980aecf
github.com/lestrrat/go-tcputil v0.0.0-20180223002529-67ef49c1ebf3 // indirect
github.com/lib/pq v1.5.2
github.com/mackerelio/checkers v0.0.0-20190411030116-60cbd7b55456
github.com/mackerelio/go-osstat v0.1.0
Expand All @@ -31,6 +29,4 @@ require (
golang.org/x/crypto v0.0.0-20200220183623-bac4c82f6975
golang.org/x/sys v0.0.0-20191218084908-4a24b4065292
golang.org/x/text v0.3.2
golang.org/x/tools v0.0.0-20190524140312-2c0ae7006135 // indirect
honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc // indirect
)
12 changes: 0 additions & 12 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ github.com/Microsoft/hcsshim v0.8.7/go.mod h1:OHd7sQqRFrYd3RmSgbgji+ctCwkbq2wbEY
github.com/Songmu/retry v0.1.0/go.mod h1:7sXIW7eseB9fq0FUvigRcQMVLR9tuHI0Scok+rkpAuA=
github.com/StackExchange/wmi v0.0.0-20190523213315-cbe66965904d h1:G0m3OIz70MZUWq3EgK3CesDbo8upS2Vm9/P3FtgI+Jk=
github.com/StackExchange/wmi v0.0.0-20190523213315-cbe66965904d/go.mod h1:3eOhrUMpNV+6aFIbp5/iudMxNCF27Vw2OZgy4xEx0Fg=
github.com/aws/aws-sdk-go v1.30.9 h1:DntpBUKkchINPDbhEzDRin1eEn1TG9TZFlzWPf0i8to=
github.com/aws/aws-sdk-go v1.30.9/go.mod h1:5zCpMtNQVjRREroY7sYe8lOMRSxkhG6MZveU8YkpAk0=
github.com/aws/aws-sdk-go v1.30.26 h1:wP0N6DBb/3EyHTtWNz4jzgGVi1l290zoFGfu4HFGeM0=
github.com/aws/aws-sdk-go v1.30.26/go.mod h1:5zCpMtNQVjRREroY7sYe8lOMRSxkhG6MZveU8YkpAk0=
github.com/beevik/ntp v0.3.0 h1:xzVrPrE4ziasFXgBVBZJDP0Wg/KpMwk2KHJ4Ba8GrDw=
Expand Down Expand Up @@ -66,8 +64,6 @@ github.com/fzzy/radix v0.5.6/go.mod h1:KhtJfdbo4PD2LEOYO7QCVSIH0pOcZEZ/SpNsXgwQt
github.com/github/hub v2.11.2+incompatible/go.mod h1:zQrzJEdze2hfWJDgktd/L6sROjAdCThFrzjbxw4keTs=
github.com/go-asn1-ber/asn1-ber v1.3.1 h1:gvPdv/Hr++TRFCl0UbPFHC54P9N9jgsRPnmnr419Uck=
github.com/go-asn1-ber/asn1-ber v1.3.1/go.mod h1:hEBeB/ic+5LoWskz+yKT7vGhhPYkProFKoKdwZRWMe0=
github.com/go-ldap/ldap/v3 v3.1.8 h1:5vU/2jOh9HqprwXp8aF915s9p6Z8wmbSEVF7/gdTFhM=
github.com/go-ldap/ldap/v3 v3.1.8/go.mod h1:5Zun81jBTabRaI8lzN7E1JjyEl1g6zI6u9pd8luAK4Q=
github.com/go-ldap/ldap/v3 v3.1.10 h1:7WsKqasmPThNvdl0Q5GPpbTDD/ZD98CfuawrMIuh7qQ=
github.com/go-ldap/ldap/v3 v3.1.10/go.mod h1:5Zun81jBTabRaI8lzN7E1JjyEl1g6zI6u9pd8luAK4Q=
github.com/go-ole/go-ole v1.2.4 h1:nNBDSCOigTSiarFpYE9J/KtEA1IOW4CNeqT9TQDqCxI=
Expand Down Expand Up @@ -112,12 +108,6 @@ github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/pty v1.1.4/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/lestrrat/go-tcptest v0.0.0-20180223004105-0f9f9980aecf h1:pk0QTs43Yvo7UwxqakaEYGpeaMqB7XX3bsM0zZBHmSw=
github.com/lestrrat/go-tcptest v0.0.0-20180223004105-0f9f9980aecf/go.mod h1:mr9p8Z+w+nwRmW0hZt8mM7/DTklvcrLTzCXdgb9aY8M=
github.com/lestrrat/go-tcputil v0.0.0-20180223002529-67ef49c1ebf3 h1:qbEBRH3oeH2wOK6dvB6ug/L1ypurkhbD+1HTrrIEI4Q=
github.com/lestrrat/go-tcputil v0.0.0-20180223002529-67ef49c1ebf3/go.mod h1:B+/nnJuKL4WcXSYfHy1SqQeHbl58EeOVNOJvXmtgi+k=
github.com/lib/pq v1.3.0 h1:/qkRGz8zljWiDcFvgpwUpwIAPu3r07TDvs3Rws+o/pU=
github.com/lib/pq v1.3.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
github.com/lib/pq v1.5.2 h1:yTSXVswvWUOQ3k1sd7vJfDrbSl8lKuscqFJRqjC0ifw=
github.com/lib/pq v1.5.2/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
github.com/mackerelio/checkers v0.0.0-20190411030116-60cbd7b55456 h1:TN6fEPX3sQRkg4nvztTBYD0UTNEY5x52XMmBwT9HqLU=
Expand Down Expand Up @@ -161,8 +151,6 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
github.com/prometheus/procfs v0.0.5/go.mod h1:4A/X28fw3Fc593LaREMrKMqOKvUAntwMDaekg4FpcdQ=
github.com/rogpeppe/go-charset v0.0.0-20180617210344-2471d30d28b4/go.mod h1:qgYeAmZ5ZIpBWTGllZSQnw97Dj+woV0toclVaRGI8pc=
github.com/shirou/gopsutil v2.20.3+incompatible h1:0JVooMPsT7A7HqEYdydp/OfjSOYSjhXV7w1hkKj/NPQ=
github.com/shirou/gopsutil v2.20.3+incompatible/go.mod h1:5b4v6he4MtMOwMlS0TUMTu2PcXUg8+E1lC7eC3UO/RA=
github.com/shirou/gopsutil v2.20.4+incompatible h1:cMT4rxS55zx9NVUnCkrmXCsEB/RNfG9SwHY9evtX8Ng=
github.com/shirou/gopsutil v2.20.4+incompatible/go.mod h1:5b4v6he4MtMOwMlS0TUMTu2PcXUg8+E1lC7eC3UO/RA=
github.com/sirupsen/logrus v1.0.4-0.20170822132746-89742aefa4b2/go.mod h1:pMByvHTf9Beacp5x1UXfOR9xyW/9antXMhjMPG0dEzc=
Expand Down

0 comments on commit a0e8a4a

Please sign in to comment.