From 6103228ee951c7c8bf5d425471b7c440980c6044 Mon Sep 17 00:00:00 2001 From: Valery Piashchynski Date: Wed, 9 Jun 2021 02:31:33 +0300 Subject: - Update CI (add new tests) - Add path scan with regex (find .php/.ph/.sh scripts). When found, try to stat path for potential fix for the [file not exists problem] Signed-off-by: Valery Piashchynski --- .github/workflows/linux.yml | 1 + .github/workflows/windows.yml | 1 + Makefile | 2 ++ go.mod | 1 - plugins/server/command.go | 33 ++++++++++++++++++++++ plugins/server/command_test.go | 14 +++++++++ plugins/server/plugin.go | 19 +++++++++---- .../gzip/configs/.rr-http-middlewareNotExist.yaml | 6 +--- tests/plugins/gzip/plugin_test.go | 1 + tests/plugins/reload/reload_plugin_test.go | 7 ++++- tests/plugins/server/configs/.rr.yaml | 2 +- tests/plugins/server/plugin_pipes.go | 2 +- tests/plugins/server/server_plugin_test.go | 21 ++++---------- 13 files changed, 80 insertions(+), 30 deletions(-) create mode 100644 plugins/server/command.go create mode 100644 plugins/server/command_test.go diff --git a/.github/workflows/linux.yml b/.github/workflows/linux.yml index 041e6b6e..670919a5 100644 --- a/.github/workflows/linux.yml +++ b/.github/workflows/linux.yml @@ -63,6 +63,7 @@ jobs: go test -v -race -cover -tags=debug -coverpkg=./... -coverprofile=./coverage-ci/bst.txt -covermode=atomic ./pkg/bst go test -v -race -cover -tags=debug -coverpkg=./... -coverprofile=./coverage-ci/worker_stack.txt -covermode=atomic ./pkg/worker_watcher go test -v -race -cover -tags=debug -coverpkg=./... -coverprofile=./coverage-ci/http_config.txt -covermode=atomic ./plugins/http/config + go test -v -race -cover -tags=debug -coverpkg=./... -coverprofile=./coverage/server_cmd.out -covermode=atomic ./plugins/server go test -v -race -cover -tags=debug -coverpkg=./... -coverprofile=./coverage-ci/http.txt -covermode=atomic ./tests/plugins/http go test -v -race -cover -tags=debug -coverpkg=./... -coverprofile=./coverage-ci/informer.txt -covermode=atomic ./tests/plugins/informer go test -v -race -cover -tags=debug -coverpkg=./... -coverprofile=./coverage-ci/reload.txt -covermode=atomic ./tests/plugins/reload diff --git a/.github/workflows/windows.yml b/.github/workflows/windows.yml index 759137b5..f662b0b1 100644 --- a/.github/workflows/windows.yml +++ b/.github/workflows/windows.yml @@ -63,6 +63,7 @@ jobs: go test -v -race ./pkg/bst go test -v -race ./pkg/worker_watcher go test -v -race ./plugins/http/config + go test -v -race ./plugins/server go test -v -race ./tests/plugins/http go test -v -race ./tests/plugins/informer go test -v -race ./tests/plugins/reload diff --git a/Makefile b/Makefile index 9cd0531d..431cb017 100755 --- a/Makefile +++ b/Makefile @@ -16,6 +16,7 @@ test_coverage: go test -v -race -cover -tags=debug -coverpkg=./... -coverprofile=./coverage/bst.out -covermode=atomic ./pkg/bst go test -v -race -cover -tags=debug -coverpkg=./... -coverprofile=./coverage/http.out -covermode=atomic ./tests/plugins/http go test -v -race -cover -tags=debug -coverpkg=./... -coverprofile=./coverage/http_config.out -covermode=atomic ./plugins/http/config + go test -v -race -cover -tags=debug -coverpkg=./... -coverprofile=./coverage/server_cmd.out -covermode=atomic ./plugins/server go test -v -race -cover -tags=debug -coverpkg=./... -coverprofile=./coverage/informer.out -covermode=atomic ./tests/plugins/informer go test -v -race -cover -tags=debug -coverpkg=./... -coverprofile=./coverage/reload.out -covermode=atomic ./tests/plugins/reload go test -v -race -cover -tags=debug -coverpkg=./... -coverprofile=./coverage/server.out -covermode=atomic ./tests/plugins/server @@ -44,6 +45,7 @@ test: ## Run application tests go test -v -race -tags=debug ./pkg/bst go test -v -race -tags=debug ./tests/plugins/http go test -v -race -tags=debug ./plugins/http/config + go test -v -race -tags=debug ./plugins/server go test -v -race -tags=debug ./tests/plugins/informer go test -v -race -tags=debug ./tests/plugins/reload go test -v -race -tags=debug ./tests/plugins/server diff --git a/go.mod b/go.mod index 0b8661c0..cbde0875 100644 --- a/go.mod +++ b/go.mod @@ -12,7 +12,6 @@ require ( github.com/go-redis/redis/v8 v8.9.0 github.com/gofiber/fiber/v2 v2.10.0 github.com/golang/mock v1.4.4 - github.com/golang/protobuf v1.4.3 github.com/google/uuid v1.2.0 github.com/json-iterator/go v1.1.11 github.com/klauspost/compress v1.13.0 diff --git a/plugins/server/command.go b/plugins/server/command.go new file mode 100644 index 00000000..527cc224 --- /dev/null +++ b/plugins/server/command.go @@ -0,0 +1,33 @@ +package server + +import ( + "os" + "regexp" + + "github.com/spiral/errors" +) + +// pattern for the path finding +const pattern string = `^\/*([A-z0-9/.:-]+\.(php|sh|ph))$` + +func (server *Plugin) scanCommand(cmd []string) error { + const op = errors.Op("server_command_scan") + r, err := regexp.Compile(pattern) + if err != nil { + return err + } + + for i := 0; i < len(cmd); i++ { + if r.MatchString(cmd[i]) { + // try to stat + _, err := os.Stat(cmd[i]) + if err != nil { + return errors.E(op, err) + } + + // stat successful + return nil + } + } + return errors.E(errors.Str("scan failed, possible path not found"), op) +} diff --git a/plugins/server/command_test.go b/plugins/server/command_test.go new file mode 100644 index 00000000..24051fba --- /dev/null +++ b/plugins/server/command_test.go @@ -0,0 +1,14 @@ +package server + +import ( + "strings" + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestServerCommandChecker(t *testing.T) { + s := &Plugin{} + cmd1 := "php ../../tests/client.php" + assert.NoError(t, s.scanCommand(strings.Split(cmd1, " "))) +} diff --git a/plugins/server/plugin.go b/plugins/server/plugin.go index 7f694f3c..00639f43 100644 --- a/plugins/server/plugin.go +++ b/plugins/server/plugin.go @@ -24,10 +24,11 @@ import ( // PluginName for the server const PluginName = "server" -// RR_RELAY env variable key (internal) -const RR_RELAY = "RR_RELAY" //nolint:stylecheck -// RR_RPC env variable key (internal) if the RPC presents -const RR_RPC = "RR_RPC" //nolint:stylecheck +// RrRelay env variable key (internal) +const RrRelay = "RR_RELAY" + +// RrRPC env variable key (internal) if the RPC presents +const RrRPC = "RR_RPC" // Plugin manages worker type Plugin struct { @@ -93,6 +94,12 @@ func (server *Plugin) CmdFactory(env Env) (func() *exec.Cmd, error) { return nil, errors.E(op, errors.Str("minimum command should be `