Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Agent] Fixed test to pass on windows #16922

Merged
merged 6 commits into from
Mar 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions x-pack/agent/CHANGELOG.asciidoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Use these for links to issue and pulls. Note issues and pulls redirect one to
// each other on Github, so don't worry too much on using the right prefix.
:issue: https://github.com/elastic/beats/issues/
:pull: https://github.com/elastic/beats/pull/


[[release-notes-8.0.0]]
=== Agent version 8.0.0


==== Breaking changes

==== Bugfixes

- Fixed tests on windows {pull}16922[16922]

==== New features
103 changes: 103 additions & 0 deletions x-pack/agent/pkg/agent/operation/common_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
// or more contributor license agreements. Licensed under the Elastic License;
// you may not use this file except in compliance with the Elastic License.

package operation

import (
"context"
"testing"
"time"

operatorCfg "github.com/elastic/beats/v7/x-pack/agent/pkg/agent/operation/config"
"github.com/elastic/beats/v7/x-pack/agent/pkg/agent/stateresolver"
"github.com/elastic/beats/v7/x-pack/agent/pkg/artifact"
"github.com/elastic/beats/v7/x-pack/agent/pkg/artifact/download"
"github.com/elastic/beats/v7/x-pack/agent/pkg/artifact/install"
"github.com/elastic/beats/v7/x-pack/agent/pkg/config"
"github.com/elastic/beats/v7/x-pack/agent/pkg/core/logger"
"github.com/elastic/beats/v7/x-pack/agent/pkg/core/plugin/app"
"github.com/elastic/beats/v7/x-pack/agent/pkg/core/plugin/app/monitoring"
"github.com/elastic/beats/v7/x-pack/agent/pkg/core/plugin/process"
"github.com/elastic/beats/v7/x-pack/agent/pkg/core/plugin/retry"
)

var installPath = "tests/scripts"

func getTestOperator(t *testing.T, installPath string) (*Operator, *operatorCfg.Config) {
operatorConfig := &operatorCfg.Config{
RetryConfig: &retry.Config{
Enabled: true,
RetriesCount: 2,
Delay: 3 * time.Second,
MaxDelay: 10 * time.Second,
},
ProcessConfig: &process.Config{},
DownloadConfig: &artifact.Config{
InstallPath: installPath,
},
MonitoringConfig: &monitoring.Config{
MonitorMetrics: false,
},
}

cfg, err := config.NewConfigFrom(operatorConfig)
if err != nil {
t.Fatal(err)
}

l := getLogger()

fetcher := &DummyDownloader{}
installer := &DummyInstaller{}

stateResolver, err := stateresolver.NewStateResolver(l)
if err != nil {
t.Fatal(err)
}

operator, err := NewOperator(context.Background(), l, "p1", cfg, fetcher, installer, stateResolver, nil)
if err != nil {
t.Fatal(err)
}

operator.config.DownloadConfig.OperatingSystem = "darwin"
operator.config.DownloadConfig.Architecture = "32"

return operator, operatorConfig
}

func getLogger() *logger.Logger {
l, _ := logger.New()
return l
}

func getProgram(binary, version string) *app.Descriptor {
downloadCfg := &artifact.Config{
InstallPath: installPath,
OperatingSystem: "darwin",
}
return app.NewDescriptor(binary, version, downloadCfg, nil)
}

type TestConfig struct {
TestFile string
}

type DummyDownloader struct {
}

func (*DummyDownloader) Download(_ context.Context, p, v string) (string, error) {
return "", nil
}

var _ download.Downloader = &DummyDownloader{}

type DummyInstaller struct {
}

func (*DummyInstaller) Install(p, v, _ string) error {
return nil
}

var _ install.Installer = &DummyInstaller{}
2 changes: 0 additions & 2 deletions x-pack/agent/pkg/agent/operation/monitoring_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
// or more contributor license agreements. Licensed under the Elastic License;
// you may not use this file except in compliance with the Elastic License.

// +build linux darwin

package operation

import (
Expand Down
90 changes: 0 additions & 90 deletions x-pack/agent/pkg/agent/operation/operator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,31 +7,19 @@
package operation

import (
"context"
"fmt"
"math/rand"
"os"
"path/filepath"
"testing"
"time"

operatorCfg "github.com/elastic/beats/v7/x-pack/agent/pkg/agent/operation/config"
"github.com/elastic/beats/v7/x-pack/agent/pkg/agent/program"
"github.com/elastic/beats/v7/x-pack/agent/pkg/agent/stateresolver"
"github.com/elastic/beats/v7/x-pack/agent/pkg/artifact"
"github.com/elastic/beats/v7/x-pack/agent/pkg/artifact/download"
"github.com/elastic/beats/v7/x-pack/agent/pkg/artifact/install"
"github.com/elastic/beats/v7/x-pack/agent/pkg/config"
"github.com/elastic/beats/v7/x-pack/agent/pkg/core/logger"
"github.com/elastic/beats/v7/x-pack/agent/pkg/core/plugin/app"
"github.com/elastic/beats/v7/x-pack/agent/pkg/core/plugin/app/monitoring"
"github.com/elastic/beats/v7/x-pack/agent/pkg/core/plugin/process"
"github.com/elastic/beats/v7/x-pack/agent/pkg/core/plugin/retry"
"github.com/elastic/beats/v7/x-pack/agent/pkg/core/plugin/state"
)

var installPath = "tests/scripts"

func TestMain(m *testing.M) {
// init supported with test cases
shortSpec := program.Spec{
Expand Down Expand Up @@ -374,81 +362,3 @@ func TestConfigurableByFileRun(t *testing.T) {
t.Fatal("Process found")
}
}

func getTestOperator(t *testing.T, installPath string) (*Operator, *operatorCfg.Config) {
operatorConfig := &operatorCfg.Config{
RetryConfig: &retry.Config{
Enabled: true,
RetriesCount: 2,
Delay: 3 * time.Second,
MaxDelay: 10 * time.Second,
},
ProcessConfig: &process.Config{},
DownloadConfig: &artifact.Config{
InstallPath: installPath,
},
MonitoringConfig: &monitoring.Config{
MonitorMetrics: false,
},
}

cfg, err := config.NewConfigFrom(operatorConfig)
if err != nil {
t.Fatal(err)
}

l := getLogger()

fetcher := &DummyDownloader{}
installer := &DummyInstaller{}

stateResolver, err := stateresolver.NewStateResolver(l)
if err != nil {
t.Fatal(err)
}

operator, err := NewOperator(context.Background(), l, "p1", cfg, fetcher, installer, stateResolver, nil)
if err != nil {
t.Fatal(err)
}

operator.config.DownloadConfig.OperatingSystem = "darwin"
operator.config.DownloadConfig.Architecture = "32"

return operator, operatorConfig
}

func getLogger() *logger.Logger {
l, _ := logger.New()
return l
}

func getProgram(binary, version string) *app.Descriptor {
downloadCfg := &artifact.Config{
InstallPath: installPath,
OperatingSystem: "darwin",
}
return app.NewDescriptor(binary, version, downloadCfg, nil)
}

type TestConfig struct {
TestFile string
}

type DummyDownloader struct {
}

func (*DummyDownloader) Download(_ context.Context, p, v string) (string, error) {
return "", nil
}

var _ download.Downloader = &DummyDownloader{}

type DummyInstaller struct {
}

func (*DummyInstaller) Install(p, v, _ string) error {
return nil
}

var _ install.Installer = &DummyInstaller{}
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,10 @@ func (b *Monitor) monitoringDrop() string {
func monitoringDrop(path string) (drop string) {
defer func() {
if drop != "" {
drop = filepath.Dir(drop)
// Dir call changes separator to the one used in OS
// '/var/lib' -> '\var\lib\' on windows
baseLen := len(filepath.Dir(drop))
drop = drop[:baseLen]
}
}()

Expand Down
42 changes: 18 additions & 24 deletions x-pack/agent/pkg/core/plugin/app/monitoring/beats/drop_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,42 +5,36 @@
package beats

import (
"runtime"
"testing"
)

type testCase struct {
Endpoint string
Drop string
SkipWindows bool
Endpoint string
Drop string
}

func TestMonitoringDrops(t *testing.T) {
cases := []testCase{
testCase{`/var/lib/drop/abc.sock`, "/var/lib/drop", true},
testCase{`npipe://drop`, "", false},
testCase{`http+npipe://drop`, "", false},
testCase{`\\.\pipe\drop`, "", false},
testCase{`unix:///var/lib/drop/abc.sock`, "/var/lib/drop", true},
testCase{`http+unix:///var/lib/drop/abc.sock`, "/var/lib/drop", true},
testCase{`file:///var/lib/drop/abc.sock`, "/var/lib/drop", true},
testCase{`http://localhost/stats`, "", false},
testCase{`localhost/stats`, "", false},
testCase{`http://localhost:8080/stats`, "", false},
testCase{`localhost:8080/stats`, "", false},
testCase{`http://1.2.3.4/stats`, "", false},
testCase{`http://1.2.3.4:5678/stats`, "", false},
testCase{`1.2.3.4:5678/stats`, "", false},
testCase{`http://hithere.com:5678/stats`, "", false},
testCase{`hithere.com:5678/stats`, "", false},
testCase{`/var/lib/drop/abc.sock`, "/var/lib/drop"},
testCase{`npipe://drop`, ""},
testCase{`http+npipe://drop`, ""},
testCase{`\\.\pipe\drop`, ""},
testCase{`unix:///var/lib/drop/abc.sock`, "/var/lib/drop"},
testCase{`http+unix:///var/lib/drop/abc.sock`, "/var/lib/drop"},
testCase{`file:///var/lib/drop/abc.sock`, "/var/lib/drop"},
testCase{`http://localhost/stats`, ""},
testCase{`localhost/stats`, ""},
testCase{`http://localhost:8080/stats`, ""},
testCase{`localhost:8080/stats`, ""},
testCase{`http://1.2.3.4/stats`, ""},
testCase{`http://1.2.3.4:5678/stats`, ""},
testCase{`1.2.3.4:5678/stats`, ""},
testCase{`http://hithere.com:5678/stats`, ""},
testCase{`hithere.com:5678/stats`, ""},
}

for _, c := range cases {
t.Run(c.Endpoint, func(t *testing.T) {
if runtime.GOOS == "windows" && c.SkipWindows {
t.Skip("Skipped under windows")
}

drop := monitoringDrop(c.Endpoint)
if drop != c.Drop {
t.Errorf("Case[%s]: Expected '%s', got '%s'", c.Endpoint, c.Drop, drop)
Expand Down