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

Initial version of daemon mode #103

Merged
merged 17 commits into from
Mar 28, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
/3rdparty
/bin
/coverage.html
/dist/
/logstash-filter-verifier
/.vscode/
*.coverprofile
*.swp
logstash-filter-verifier.yml
30 changes: 24 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,14 @@ TARGETS := darwin_amd64 linux_386 linux_amd64 windows_386 windows_amd64

VERSION := $(shell git describe --tags --always)

GOCOV := $(GOBIN)/gocov$(EXEC_SUFFIX)
GOCOV_HTML := $(GOBIN)/gocov-html$(EXEC_SUFFIX)
GOLANGCI_LINT := $(GOBIN)/golangci-lint$(EXEC_SUFFIX)
GOVVV := $(GOBIN)/govvv$(EXEC_SUFFIX)
OVERALLS := $(GOBIN)/overalls$(EXEC_SUFFIX)
GOCOV := $(GOBIN)/gocov$(EXEC_SUFFIX)
GOCOV_HTML := $(GOBIN)/gocov-html$(EXEC_SUFFIX)
GOLANGCI_LINT := $(GOBIN)/golangci-lint$(EXEC_SUFFIX)
GOVVV := $(GOBIN)/govvv$(EXEC_SUFFIX)
OVERALLS := $(GOBIN)/overalls$(EXEC_SUFFIX)
PROTOC_GEN_GO := $(GOBIN)/protoc-gen-go$(EXEC_SUFFIX)
PROTOC_GEN_GO_GRPC := $(GOBIN)/protoc-gen-go-grpc$(EXEC_SUFFIX)
MOQ := $(GOBIN)/moq$(EXEC_SUFFIX)

GOLANGCI_LINT_VERSION := v1.32.2

Expand All @@ -66,11 +69,26 @@ $(GOVVV):
$(OVERALLS):
go get github.com/go-playground/overalls

# TODO: For protoc to find this dependency, I suppose, they must reside in the PATH
breml marked this conversation as resolved.
Show resolved Hide resolved
$(PROTOC_GEN_GO):
go get google.golang.org/protobuf/cmd/protoc-gen-go

# TODO: For protoc to find this dependency, I suppose, they must reside in the PATH
$(PROTOC_GEN_GO_GRPC):
go get google.golang.org/grpc/cmd/protoc-gen-go-grpc

$(MOQ):
go get github.com/matryer/moq

# The Go compiler is fast and pretty good about figuring out what to
# build so we don't try to to outsmart it.
$(PROGRAM)$(EXEC_SUFFIX): .FORCE $(GOVVV)
$(PROGRAM)$(EXEC_SUFFIX): gogenerate .FORCE $(GOVVV)
govvv build -o $@

.PHONY: gogenerate
generate: $(MOQ) # TODO: go generate also depends on protobuf-compiler, which needs to be installed as well.
breml marked this conversation as resolved.
Show resolved Hide resolved
go generate ./...

.PHONY: check
check: $(GOLANGCI_LINT)
golangci-lint run
Expand Down
19 changes: 15 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,29 @@ require (
github.com/Masterminds/semver/v3 v3.0.1
github.com/ahmetb/govvv v0.3.0
github.com/axw/gocov v1.0.0
github.com/breml/logstash-config v0.1.0
github.com/bmatcuk/doublestar/v2 v2.0.4
github.com/breml/logstash-config v0.4.0
github.com/go-playground/overalls v0.0.0-20191218162659-7df9f728c018
github.com/golang/protobuf v1.4.2
github.com/hashicorp/packer v1.4.4
github.com/hpcloud/tail v1.0.0
github.com/imkira/go-observer v1.0.3
github.com/magnusbaeck/logstash-filter-verifier v0.0.0-20201128205846-61a579889997
github.com/matm/gocov-html v0.0.0-20200509184451-71874e2e203b
github.com/matoous/go-nanoid v1.5.0
github.com/matryer/is v1.4.0
github.com/matryer/moq v0.2.1
github.com/mattn/go-shellwords v1.0.6
github.com/mikefarah/yaml/v2 v2.4.0
github.com/op/go-logging v0.0.0-20160315200505-970db520ece7
github.com/pkg/errors v0.9.1
github.com/spf13/cobra v1.1.1
github.com/spf13/viper v1.7.1
github.com/stretchr/testify v1.4.0
github.com/yookoala/realpath v1.0.0 // indirect
github.com/stretchr/testify v1.5.1
github.com/tidwall/gjson v1.6.8
golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c // indirect
gopkg.in/go-playground/assert.v1 v1.2.1 // indirect
google.golang.org/grpc v1.34.0
google.golang.org/grpc/cmd/protoc-gen-go-grpc v1.0.1
google.golang.org/protobuf v1.25.0
gopkg.in/yaml.v2 v2.2.8
)
91 changes: 91 additions & 0 deletions go.sum

Large diffs are not rendered by default.

111 changes: 111 additions & 0 deletions integration_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
package main_test

import (
"context"
"os"
"path"
"testing"
"time"

"github.com/matryer/is"

"github.com/magnusbaeck/logstash-filter-verifier/v2/internal/app/daemon"
"github.com/magnusbaeck/logstash-filter-verifier/v2/internal/app/daemon/test"
"github.com/magnusbaeck/logstash-filter-verifier/v2/internal/daemon/api/grpc"
"github.com/magnusbaeck/logstash-filter-verifier/v2/internal/daemon/file"
"github.com/magnusbaeck/logstash-filter-verifier/v2/internal/logging"
)

func TestIntegration(t *testing.T) {
if os.Getenv("INTEGRATION_TEST") != "1" {
t.Skip("integration test skipped, enable with env var `INTEGRATION_TEST=1`")
}

is := is.New(t)

// FIXME: use test logger
testLogger := &logging.LoggerMock{
DebugFunc: func(args ...interface{}) { t.Log(args...) },
DebugfFunc: func(format string, args ...interface{}) { t.Logf(format, args...) },
ErrorFunc: func(args ...interface{}) { t.Log(args...) },
ErrorfFunc: func(format string, args ...interface{}) { t.Logf(format, args...) },
FatalFunc: func(args ...interface{}) { t.Log(args...) },
FatalfFunc: func(format string, args ...interface{}) { t.Logf(format, args...) },
InfoFunc: func(args ...interface{}) { t.Log(args...) },
InfofFunc: func(format string, args ...interface{}) { t.Logf(format, args...) },
WarningFunc: func(args ...interface{}) { t.Log(args...) },
WarningfFunc: func(format string, args ...interface{}) { t.Logf(format, args...) },
}

tempdir := t.TempDir()
// Start Daemon
socket := path.Join(tempdir, "integration_test.socket")
logstashPath := path.Join("3rdparty/logstash-7.10.0/bin/logstash")
if !file.Exists(logstashPath) {
t.Fatalf("Logstash needs to be present in %q for the integration tests to work", logstashPath)
}

// FIXME: use test logger
log := testLogger
server := daemon.New(socket, logstashPath, log)

go func() {
is := is.New(t)

defer server.Cleanup()

err := server.Run()
is.NoErr(err)
}()

i := 0
for {
if file.Exists(socket) {
break
}
time.Sleep(100 * time.Millisecond)
i++
if i >= 20 {
t.Fatalf("wait for socket file failed")
}
}

// Run tests
cases := []struct {
name string
pipeline string
basePath string
testcases string
}{
{
name: "basic_pipeline",
},
{
name: "conditional_output",
},
{
name: "pipeline_to_pipeline",
},
}

for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
client, err := test.New(
path.Join(tempdir, "integration_test.socket"),
log,
"testdata/"+tc.name+".yml",
"testdata/"+tc.name,
"testdata/testcases/"+tc.name,
)
is.NoErr(err)

err = client.Run()
is.NoErr(err)
})
}

_, err := server.Shutdown(context.Background(), &grpc.ShutdownRequest{})
is.NoErr(err)

time.Sleep(3 * time.Second)
breml marked this conversation as resolved.
Show resolved Hide resolved
}
1 change: 1 addition & 0 deletions internal/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ func makeRootCmd(version string) *cobra.Command {
_ = viper.BindPFlag("loglevel", rootCmd.PersistentFlags().Lookup("loglevel"))

rootCmd.AddCommand(makeStandaloneCmd())
rootCmd.AddCommand(makeDaemonCmd())

return rootCmd
}
Expand Down
22 changes: 22 additions & 0 deletions internal/app/daemon.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package app

import (
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

func makeDaemonCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "daemon",
Short: "Control logstash-filter-verifier daemon mode",
}

cmd.PersistentFlags().StringP("socket", "s", "", "location of the control socket")
_ = viper.BindPFlag("socket", cmd.PersistentFlags().Lookup("socket"))

cmd.AddCommand(makeDaemonStartCmd())
cmd.AddCommand(makeDaemonShutdownCmd())
cmd.AddCommand(makeDaemonRunCmd())

return cmd
}
Loading