Skip to content

Commit

Permalink
Fix tests for Windows - part 1 (#8414)
Browse files Browse the repository at this point in the history
  • Loading branch information
zak-pawel authored Nov 23, 2020
1 parent d64c722 commit 0fcfee0
Show file tree
Hide file tree
Showing 51 changed files with 416 additions and 253 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ README.md merge=union
go.sum merge=union
plugins/inputs/all/all.go merge=union
plugins/outputs/all/all.go merge=union
**/testdata/** test eol=lf
7 changes: 1 addition & 6 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -114,12 +114,7 @@ fmtcheck:

.PHONY: test-windows
test-windows:
go test -short $(race_detector) ./plugins/inputs/ping/...
go test -short $(race_detector) ./plugins/inputs/win_perf_counters/...
go test -short $(race_detector) ./plugins/inputs/win_services/...
go test -short $(race_detector) ./plugins/inputs/procstat/...
go test -short $(race_detector) ./plugins/inputs/ntpq/...
go test -short $(race_detector) ./plugins/processors/port_name/...
go test -short ./...

.PHONY: vet
vet:
Expand Down
2 changes: 1 addition & 1 deletion appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ clone_folder: C:\gopath\src\github.com\influxdata\telegraf
environment:
GOPATH: C:\gopath

stack: go 1.14
stack: go 1.15

platform: x64

Expand Down
3 changes: 2 additions & 1 deletion config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package config

import (
"os"
"strings"
"testing"
"time"

Expand Down Expand Up @@ -201,7 +202,7 @@ func TestConfig_LoadSpecialTypes(t *testing.T) {
// Tests telegraf size parsing.
assert.Equal(t, internal.Size{Size: 1024 * 1024}, inputHTTPListener.MaxBodySize)
// Tests toml multiline basic strings.
assert.Equal(t, "/path/to/my/cert\n", inputHTTPListener.TLSCert)
assert.Equal(t, "/path/to/my/cert", strings.TrimRight(inputHTTPListener.TLSCert, "\r\n"))
}

func TestConfig_FieldNotDefined(t *testing.T) {
Expand Down
8 changes: 0 additions & 8 deletions config/testdata/telegraf-agent.toml
Original file line number Diff line number Diff line change
Expand Up @@ -176,14 +176,6 @@
# If no servers are specified, then 127.0.0.1 is used as the host and 4020 as the port.
servers = ["127.0.0.1:4021"]

# Read metrics from local Lustre service on OST, MDS
[[inputs.lustre2]]
# An array of /proc globs to search for Lustre stats
# If not specified, the default will work on Lustre 2.5.x
#
# ost_procfiles = ["/proc/fs/lustre/obdfilter/*/stats", "/proc/fs/lustre/osd-ldiskfs/*/stats"]
# mds_procfiles = ["/proc/fs/lustre/mdt/*/md_stats"]

# Read metrics about memory usage
[[inputs.mem]]
# no configuration
Expand Down
54 changes: 35 additions & 19 deletions internal/globpath/globpath_test.go
Original file line number Diff line number Diff line change
@@ -1,29 +1,38 @@
// +build !windows

// TODO: Windows - should be enabled for Windows when super asterisk is fixed on Windows
// https://github.com/influxdata/telegraf/issues/6248

package globpath

import (
"os"
"path/filepath"
"runtime"
"strings"
"testing"

"github.com/stretchr/testify/require"
)

var (
testdataDir = getTestdataDir()
)

func TestCompileAndMatch(t *testing.T) {
dir := getTestdataDir()
// test super asterisk
g1, err := Compile(dir + "/**")
g1, err := Compile(filepath.Join(testdataDir, "**"))
require.NoError(t, err)
// test single asterisk
g2, err := Compile(dir + "/*.log")
g2, err := Compile(filepath.Join(testdataDir, "*.log"))
require.NoError(t, err)
// test no meta characters (file exists)
g3, err := Compile(dir + "/log1.log")
g3, err := Compile(filepath.Join(testdataDir, "log1.log"))
require.NoError(t, err)
// test file that doesn't exist
g4, err := Compile(dir + "/i_dont_exist.log")
g4, err := Compile(filepath.Join(testdataDir, "i_dont_exist.log"))
require.NoError(t, err)
// test super asterisk that doesn't exist
g5, err := Compile(dir + "/dir_doesnt_exist/**")
g5, err := Compile(filepath.Join(testdataDir, "dir_doesnt_exist", "**"))
require.NoError(t, err)

matches := g1.Match()
Expand All @@ -39,15 +48,14 @@ func TestCompileAndMatch(t *testing.T) {
}

func TestRootGlob(t *testing.T) {
dir := getTestdataDir()
tests := []struct {
input string
output string
}{
{dir + "/**", dir + "/*"},
{dir + "/nested?/**", dir + "/nested?/*"},
{dir + "/ne**/nest*", dir + "/ne*"},
{dir + "/nested?/*", ""},
{filepath.Join(testdataDir, "**"), filepath.Join(testdataDir, "*")},
{filepath.Join(testdataDir, "nested?", "**"), filepath.Join(testdataDir, "nested?", "*")},
{filepath.Join(testdataDir, "ne**", "nest*"), filepath.Join(testdataDir, "ne*")},
{filepath.Join(testdataDir, "nested?", "*"), ""},
}

for _, test := range tests {
Expand All @@ -57,21 +65,19 @@ func TestRootGlob(t *testing.T) {
}

func TestFindNestedTextFile(t *testing.T) {
dir := getTestdataDir()
// test super asterisk
g1, err := Compile(dir + "/**.txt")
g1, err := Compile(filepath.Join(testdataDir, "**.txt"))
require.NoError(t, err)

matches := g1.Match()
require.Len(t, matches, 1)
}

func getTestdataDir() string {
_, filename, _, _ := runtime.Caller(1)
return strings.Replace(filename, "globpath_test.go", "testdata", 1)
}

func TestMatch_ErrPermission(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("Skipping Unix only test")
}

tests := []struct {
input string
expected []string
Expand All @@ -98,3 +104,13 @@ func TestWindowsSeparator(t *testing.T) {
ok := glob.MatchString("testdata\\nested1")
require.True(t, ok)
}

func getTestdataDir() string {
dir, err := os.Getwd()
if err != nil {
// if we cannot even establish the test directory, further progress is meaningless
panic(err)
}

return filepath.Join(dir, "testdata")
}
18 changes: 9 additions & 9 deletions plugins/inputs/bcache/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,15 @@ cache_readaheads
Using this configuration:

```toml
[bcache]
# Bcache sets path
# If not specified, then default is:
# bcachePath = "/sys/fs/bcache"
#
# By default, telegraf gather stats for all bcache devices
# Setting devices will restrict the stats to the specified
# bcache devices.
# bcacheDevs = ["bcache0", ...]
[[inputs.bcache]]
## Bcache sets path
## If not specified, then default is:
bcachePath = "/sys/fs/bcache"

## By default, Telegraf gather stats for all bcache devices
## Setting devices will restrict the stats to the specified
## bcache devices.
bcacheDevs = ["bcache0"]
```

When run with:
Expand Down
6 changes: 5 additions & 1 deletion plugins/inputs/bcache/bcache.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
// +build !windows

// bcache doesn't aim for Windows

package bcache

import (
Expand All @@ -22,7 +26,7 @@ var sampleConfig = `
## If not specified, then default is:
bcachePath = "/sys/fs/bcache"
## By default, telegraf gather stats for all bcache devices
## By default, Telegraf gather stats for all bcache devices
## Setting devices will restrict the stats to the specified
## bcache devices.
bcacheDevs = ["bcache0"]
Expand Down
2 changes: 2 additions & 0 deletions plugins/inputs/bcache/bcache_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// +build !windows

package bcache

import (
Expand Down
3 changes: 3 additions & 0 deletions plugins/inputs/bcache/bcache_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// +build windows

package bcache
8 changes: 4 additions & 4 deletions plugins/inputs/ceph/ceph_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"fmt"
"io/ioutil"
"os"
"path"
"path/filepath"
"strconv"
"strings"
"testing"
Expand Down Expand Up @@ -163,7 +163,7 @@ func assertFoundSocket(t *testing.T, dir, sockType string, i int, sockets []*soc
} else {
prefix = monPrefix
}
expected := path.Join(dir, sockFile(prefix, i))
expected := filepath.Join(dir, sockFile(prefix, i))
found := false
for _, s := range sockets {
fmt.Printf("Checking %s\n", s.socket)
Expand All @@ -183,7 +183,7 @@ func sockFile(prefix string, i int) string {
func createTestFiles(dir string, st *SockTest) {
writeFile := func(prefix string, i int) {
f := sockFile(prefix, i)
fpath := path.Join(dir, f)
fpath := filepath.Join(dir, f)
ioutil.WriteFile(fpath, []byte(""), 0777)
}
tstFileApply(st, writeFile)
Expand All @@ -192,7 +192,7 @@ func createTestFiles(dir string, st *SockTest) {
func cleanupTestFiles(dir string, st *SockTest) {
rmFile := func(prefix string, i int) {
f := sockFile(prefix, i)
fpath := path.Join(dir, f)
fpath := filepath.Join(dir, f)
err := os.Remove(fpath)
if err != nil {
fmt.Printf("Error removing test file %s: %v\n", fpath, err)
Expand Down
11 changes: 6 additions & 5 deletions plugins/inputs/disk/disk_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package disk

import (
"fmt"
"os"
"testing"

Expand Down Expand Up @@ -74,13 +75,13 @@ func TestDiskUsage(t *testing.T) {
assert.Equal(t, expectedAllDiskMetrics, numDiskMetrics)

tags1 := map[string]string{
"path": "/",
"path": string(os.PathSeparator),
"fstype": "ext4",
"device": "sda",
"mode": "ro",
}
tags2 := map[string]string{
"path": "/home",
"path": fmt.Sprintf("%chome", os.PathSeparator),
"fstype": "ext4",
"device": "sdb",
"mode": "rw",
Expand Down Expand Up @@ -144,7 +145,7 @@ func TestDiskUsageHostMountPrefix(t *testing.T) {
},
},
expectedTags: map[string]string{
"path": "/",
"path": string(os.PathSeparator),
"device": "sda",
"fstype": "ext4",
"mode": "ro",
Expand Down Expand Up @@ -177,7 +178,7 @@ func TestDiskUsageHostMountPrefix(t *testing.T) {
},
hostMountPrefix: "/hostfs",
expectedTags: map[string]string{
"path": "/var",
"path": fmt.Sprintf("%cvar", os.PathSeparator),
"device": "sda",
"fstype": "ext4",
"mode": "ro",
Expand Down Expand Up @@ -210,7 +211,7 @@ func TestDiskUsageHostMountPrefix(t *testing.T) {
},
hostMountPrefix: "/hostfs",
expectedTags: map[string]string{
"path": "/",
"path": string(os.PathSeparator),
"device": "sda",
"fstype": "ext4",
"mode": "ro",
Expand Down
5 changes: 5 additions & 0 deletions plugins/inputs/exec/exec_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
// +build !windows

// TODO: Windows - should be enabled for Windows when super asterisk is fixed on Windows
// https://github.com/influxdata/telegraf/issues/6248

package exec

import (
Expand Down
11 changes: 4 additions & 7 deletions plugins/inputs/execd/execd_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
// +build !windows

package execd

import (
Expand All @@ -11,17 +9,16 @@ import (
"testing"
"time"

"github.com/stretchr/testify/require"

"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/agent"
"github.com/influxdata/telegraf/config"
"github.com/influxdata/telegraf/metric"
"github.com/influxdata/telegraf/models"
"github.com/influxdata/telegraf/testutil"
"github.com/stretchr/testify/require"

"github.com/influxdata/telegraf/plugins/parsers"
"github.com/influxdata/telegraf/plugins/serializers"

"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/testutil"
)

func TestSettingConfigWorks(t *testing.T) {
Expand Down
5 changes: 0 additions & 5 deletions plugins/inputs/execd/shim/shim_posix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"context"
"io"
"os"
"runtime"
"syscall"
"testing"
"time"
Expand All @@ -16,10 +15,6 @@ import (
)

func TestShimUSR1SignalingWorks(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip()
return
}
stdinReader, stdinWriter := io.Pipe()
stdoutReader, stdoutWriter := io.Pipe()

Expand Down
5 changes: 5 additions & 0 deletions plugins/inputs/file/file_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
// +build !windows

// TODO: Windows - should be enabled for Windows when super asterisk is fixed on Windows
// https://github.com/influxdata/telegraf/issues/6248

package file

import (
Expand Down
5 changes: 5 additions & 0 deletions plugins/inputs/filecount/filecount_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
// +build !windows

// TODO: Windows - should be enabled for Windows when super asterisk is fixed on Windows
// https://github.com/influxdata/telegraf/issues/6248

package filecount

import (
Expand Down
5 changes: 5 additions & 0 deletions plugins/inputs/filecount/filesystem_helpers_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
// +build !windows

// TODO: Windows - should be enabled for Windows when super asterisk is fixed on Windows
// https://github.com/influxdata/telegraf/issues/6248

package filecount

import (
Expand Down
Loading

0 comments on commit 0fcfee0

Please sign in to comment.