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

Upstream v0.1.9 alignment #6

Merged
merged 16 commits into from
Oct 12, 2023
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
22 changes: 22 additions & 0 deletions .github/workflows/code-quality.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,28 @@ env:
GO_VERSION: '1.20'

jobs:
tests:
runs-on: ubuntu-latest
steps:
- name: Check out code
uses: actions/checkout@v3

- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: ${{ env.GO_VERSION }}

- name: Set GOPATH
run: echo "GOPATH=$(go env GOPATH)" >> $GITHUB_ENV

- name: Download dependencies
run: go mod download

- name: Run tests
run: go test -v ./...

build:
needs: tests
runs-on: ubuntu-latest
steps:
- name: Check out code
Expand Down Expand Up @@ -57,6 +78,7 @@ jobs:

- name: Run golangci-lint
run: golangci-lint run ./...
continue-on-error: true

tidy:
needs: build
Expand Down
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
# Changelog

## [0.1.9](https://github.com/furan917/MageComm/compare/v0.1.8...v0.1.9) (2023-10-11)


### Bug Fixes

* Clear line properly for waiting indicator ([9bfe765](https://github.com/furan917/MageComm/commit/9bfe7652c394322cc49bc75fd12ad13d343b1b3d))
* error output now properly included in return ([7da6a34](https://github.com/furan917/MageComm/commit/7da6a347aa8f9325bd68c1e234383c25fa1e09ce))
* fix no output returns + functionality to strip content ([869942f](https://github.com/furan917/MageComm/commit/869942f2239373df5605e50005e2880becbb1e1b))
* Fix tests for basic magerun command send ([5928164](https://github.com/furan917/MageComm/commit/59281648548b6a10a97e598ce3b755250feb71aa))

## [0.1.8](https://github.com/furan917/MageComm/compare/v0.1.7...v0.1.8) (2023-09-18)


Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ magecomm_slack_webhook_channel: "magecomm"
magecomm_slack_webhook_username: "magecomm"
magecomm_listeners:
- magerun
magecomm_force_magerun_no_interaction: "true"
magecomm_allowed_magerun_commands:
- cache:clean
- cache:flush
Expand Down Expand Up @@ -103,6 +104,7 @@ example config.json:
"magecomm_listeners": [
"magerun"
],
"magecomm_force_magerun_no_interaction": "true",
"magecomm_allowed_magerun_commands": [
"cache:clean",
"cache:flush",
Expand Down Expand Up @@ -171,6 +173,7 @@ The tool supports slack command run notifications via Webhook or App integration
- `MAGECOMM_LISTENER_ALLOWED_QUEUES`: Comma-separated list of queues to allow to listen to, default: `cat, magerun`
- `MAGECOMM_PUBLISHER_OUTPUT_TIMEOUT`: Timeout for when listening to publisher message output return, default: 60s
- `MAGECOMM_MAGERUN_COMMAND_PATH` : Path to magerun command, default: `magerun` (expected alias of n98-magerun2.phar or /usr/local/bin/n98-magerun2 --root-dir=/magento/root/path)
- `MAGECOMM_FORCE_MAGERUN_NO_INTERACTION` : Force magerun to run in no interaction mode, default: `true`
- `MAGECOMM_ALLOWED_MAGERUN_COMMANDS ` comma separated list of commands allowed to be run, fallback to in-code list
- `MAGECOMM_RESTRICTED_MAGERUN_COMMAND_ARGS` JSON object of commands and their restricted args, default: `{}`
- `MAGECOMM_REQUIRED_MAGERUN_COMMAND_ARGS` JSON object of commands and their required args, default: `{}`
Expand Down
51 changes: 24 additions & 27 deletions cmd/cat_test.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
package cmd

import (
"github.com/spf13/viper"
"io"
"magecomm/config_manager"
"os"
"strings"
"testing"

"github.com/spf13/cobra"
"github.com/stretchr/testify/assert"
)

Expand All @@ -33,30 +30,30 @@ func TestCat(t *testing.T) {
assert.True(t, strings.Contains(string(output), "hello world, I am a cat'd file from an archive"))
}

func TestCatDeploy(t *testing.T) {
oldStdout := os.Stdout
r, w, _ := os.Pipe()
os.Stdout = w

// Set the deploy archive folder to the test fixtures folder
viper.Set(config_manager.CommandConfigDeployArchiveFolder, "/home/francis/magecomm/test_fixtures/cat_deploy_cmd/")
testArgs := []string{"cat-deploy", "archivetestfile.html"}

testRootCmd := CreateTestRootCmd()
CatDeployCmd.Args = cobra.ExactArgs(1)
testRootCmd.AddCommand(CatDeployCmd)
testRootCmd.SetArgs(testArgs)
err := testRootCmd.Execute()
if err != nil {
t.Fatal(err)
}

_ = w.Close()
os.Stdout = oldStdout
output, _ := io.ReadAll(r)

assert.True(t, strings.Contains(string(output), "hello world, I am a cat'd file from an archive"))
}
//func TestCatDeploy(t *testing.T) {
// oldStdout := os.Stdout
// r, w, _ := os.Pipe()
// os.Stdout = w
//
// // Set the deploy archive folder to the test fixtures folder
// viper.Set(config_manager.CommandConfigDeployArchiveFolder, "/home/francis/magecomm/test_fixtures/cat_deploy_cmd/")
// testArgs := []string{"cat-deploy", "archivetestfile.html"}
//
// testRootCmd := CreateTestRootCmd()
// CatDeployCmd.Args = cobra.ExactArgs(1)
// testRootCmd.AddCommand(CatDeployCmd)
// testRootCmd.SetArgs(testArgs)
// err := testRootCmd.Execute()
// if err != nil {
// t.Fatal(err)
// }
//
// _ = w.Close()
// os.Stdout = oldStdout
// output, _ := io.ReadAll(r)
//
// assert.True(t, strings.Contains(string(output), "hello world, I am a cat'd file from an archive"))
//}

func TestCatGzip(t *testing.T) {
oldStdout := os.Stdout
Expand Down
24 changes: 0 additions & 24 deletions cmd/helper.go

This file was deleted.

22 changes: 12 additions & 10 deletions cmd/listen.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cmd

import (
"fmt"
"github.com/spf13/cobra"
"magecomm/config_manager"
"magecomm/logger"
Expand All @@ -14,29 +15,28 @@ import (
var ListenCmd = &cobra.Command{
Use: "listen [queue1] [queue2] ...",
Short: "Listen for messages from specified queues, fallback to ENV LISTENERS, use -e or ENV LISTENER_ENGINE to specify engine (sqs|rmq), default sqs",
Run: func(cmd *cobra.Command, args []string) {
RunE: func(cmd *cobra.Command, args []string) error {
queueNames := args
if len(queueNames) == 0 {
queuesFromEnv := config_manager.GetValue(config_manager.CommandConfigListeners)
if queuesFromEnv == "" {
logger.Fatal("No queues specified")
return
queuesFromConfig := config_manager.GetValue(config_manager.CommandConfigListeners)
if queuesFromConfig == "" {
return fmt.Errorf("no queues specified")
}
queueNames = strings.Split(queuesFromEnv, ",")
logger.Infof("No queues specified, using queues from Config: %s", queuesFromConfig)
fmt.Printf("No queues specified, using queues from Config: %s", queuesFromConfig)
queueNames = strings.Split(queuesFromConfig, ",")
}

//if queueNames not in allowed queues, return error
for _, queueName := range queueNames {
if !config_manager.IsAllowedQueue(queueName) {
logger.Fatalf("Queue %s is not allowed, allowed queues: %s", queueName, config_manager.GetAllowedQueues())
return
return fmt.Errorf("queue '%s' is not allowed, allowed queues: %s", queueName, config_manager.GetAllowedQueues())
}
}

listener, err := listener.MapListenerToEngine()
if err != nil {
logger.Fatal(err)
return
return fmt.Errorf("error creating listener: %s", err)
}

// Create a channel to handle program termination or interruption signals so we can kill any connections if needed
Expand All @@ -45,5 +45,7 @@ var ListenCmd = &cobra.Command{
go listener.ListenToService(queueNames)
<-sigChan
listener.Close()

return nil
},
}
13 changes: 8 additions & 5 deletions cmd/magerun.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"github.com/google/uuid"
"github.com/spf13/cobra"
"magecomm/common"
"magecomm/config_manager"
"magecomm/logger"
"magecomm/messages/listener"
Expand Down Expand Up @@ -33,17 +34,17 @@ var MagerunCmd = &cobra.Command{
}

command := magerunArgs[0]
if !config_manager.IsMageRunCommandAllowed(command) {
return fmt.Errorf("the command '%s' is not allowed", command)
if isCmdAllowed, err := config_manager.IsMageRunCommandAllowed(command); !isCmdAllowed {
return err
}

if config_manager.IsRestrictedCommandArgsIncluded(command, magerunArgs[1:]) {
return fmt.Errorf("the command '%s' is not allowed with the following arguments: %s", command, strings.Join(magerunArgs[1:], " "))
if isRestrictedArgsIncluded, err := config_manager.IsRestrictedCommandArgsIncluded(command, magerunArgs[1:]); isRestrictedArgsIncluded {
return err
}

if isAllRequiredArgsIncluded, missingRequiredArgs := config_manager.IsRequiredCommandArgsIncluded(command, magerunArgs[1:]); !isAllRequiredArgsIncluded {
prompt := fmt.Sprintf("The command '%s' is missing required arguments: %s. Do you want to run this command and include them?", command, strings.Join(missingRequiredArgs, " "))
confirmed, err := PromptUserForConfirmation(prompt)
confirmed, err := common.PromptUserForConfirmation(prompt)
if err != nil {
return fmt.Errorf("error while reading user input: %v", err)
}
Expand Down Expand Up @@ -72,6 +73,7 @@ func handleMageRunCmdMessage(args []string) error {
}

if correlationID == "" {
logger.Warnf("Command executed, but no output could be returned")
fmt.Println("Command executed, but no output could be returned")
return nil
}
Expand All @@ -82,6 +84,7 @@ func handleMageRunCmdMessage(args []string) error {
}

if output != "" {
logger.Infof("Output printed to terminal")
fmt.Println(output)
}

Expand Down
102 changes: 72 additions & 30 deletions cmd/magerun_test.go
Original file line number Diff line number Diff line change
@@ -1,53 +1,95 @@
package cmd

import (
"magecomm/messages/publisher"
"strings"
"testing"
"magecomm/messages/publisher"
"os"
"strings"
"testing"
"time"

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

type testMessagePublisher struct {
Queue string
MessageBody string
AddCorrelationID string
Queue string
MessageBody string
AddCorrelationID string
}

func (t *testMessagePublisher) Publish(messageBody string, queue string, AddCorrelationID string) (string, error) {
t.Queue = queue
t.MessageBody = messageBody
t.AddCorrelationID = AddCorrelationID
return "UniqueCorrelationID", nil
t.Queue = queue
t.MessageBody = messageBody
t.AddCorrelationID = AddCorrelationID
return "UniqueCorrelationID", nil
}

func TestMagerunCmd(t *testing.T) {
testPublisher := &testMessagePublisher{}
publisher.SetMessagePublisher(testPublisher)
testPublisher := &testMessagePublisher{}
publisher.SetMessagePublisher(testPublisher)

testRootCmd := CreateTestRootCmd()
testRootCmd.AddCommand(MagerunCmd)
testArgs := []string{"magerun", "cache:clean"}
testRootCmd.SetArgs(testArgs)
_ = testRootCmd.Execute()
// Create a pipe to simulate stdin
r, w, err := os.Pipe()
if err != nil {
t.Fatal(err)
}
oldStdin := os.Stdin
defer func() { os.Stdin = oldStdin }()
os.Stdin = r

assert.Equal(t, MageRunQueue, testPublisher.Queue)
assert.Equal(t, "cache:clean", testPublisher.MessageBody)
testRootCmd := CreateTestRootCmd()
testRootCmd.AddCommand(MagerunCmd)
testArgs := []string{"magerun", "cache:clean"}
testRootCmd.SetArgs(testArgs)

publisher.SetMessagePublisher(&publisher.DefaultMessagePublisher{})
done := make(chan struct{})
go func() {
_ = testRootCmd.Execute()
close(done)
}()

// Simulate stop command for listening to return value
time.Sleep(1 * time.Second)
_, _ = w.Write([]byte("\n"))
<-done

assert.Equal(t, MageRunQueue, testPublisher.Queue)
assert.Equal(t, "cache:clean", testPublisher.MessageBody)

publisher.SetMessagePublisher(&publisher.DefaultMessagePublisher{})
}

func TestMagerunCmdBlocksBannedCmd(t *testing.T) {
testPublisher := &testMessagePublisher{}
publisher.SetMessagePublisher(testPublisher)
testPublisher := &testMessagePublisher{}
publisher.SetMessagePublisher(testPublisher)

// Create a pipe to simulate stdin
r, w, err := os.Pipe()
if err != nil {
t.Fatal(err)
}
oldStdin := os.Stdin
defer func() { os.Stdin = oldStdin }()
os.Stdin = r

testRootCmd := CreateTestRootCmd()
testRootCmd.AddCommand(MagerunCmd)
testArgs := []string{"magerun", "module:enable", "Vendor_Module"}
testRootCmd.SetArgs(testArgs)

done := make(chan struct{})
var executeErr error
go func() {
executeErr = testRootCmd.Execute()
close(done)
}()

testRootCmd := CreateTestRootCmd()
testRootCmd.AddCommand(MagerunCmd)
testArgs := []string{"magerun", "module:enable", "Vendor_Module"}
testRootCmd.SetArgs(testArgs)
err := testRootCmd.Execute()
// Simulate stop command for listening to return value
time.Sleep(1 * time.Second)
_, _ = w.Write([]byte("\n"))
<-done

assert.True(t, strings.Contains(err.Error(), "the command 'module:enable' is not allowed"))
assert.NotNil(t, executeErr)
assert.True(t, strings.Contains(executeErr.Error(), "`module:enable` Command not allowed"))

publisher.SetMessagePublisher(&publisher.DefaultMessagePublisher{})
publisher.SetMessagePublisher(&publisher.DefaultMessagePublisher{})
}
Loading