Skip to content

Commit

Permalink
MM-46253: Comparison collect command (#653)
Browse files Browse the repository at this point in the history
* Add comparison collect command

* Test comparison.GetDeploymentIds
  • Loading branch information
agarciamontoro authored Oct 12, 2023
1 parent 03c212e commit 30a3c75
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 5 deletions.
13 changes: 9 additions & 4 deletions cmd/ltctl/collect.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"sync"
"time"

"github.com/mattermost/mattermost-load-test-ng/deployment"
"github.com/mattermost/mattermost-load-test-ng/deployment/terraform"
"github.com/mattermost/mattermost-load-test-ng/deployment/terraform/ssh"

Expand All @@ -33,7 +34,7 @@ type file struct {
data []byte
}

func saveCollection(files []file) error {
func saveCollection(namePrefix string, files []file) error {
u, err := user.Current()
if err != nil {
return err
Expand All @@ -49,7 +50,7 @@ func saveCollection(files []file) error {
return err
}

name := fmt.Sprintf("collection_%d", time.Now().Unix())
name := fmt.Sprintf("%scollection_%d", namePrefix, time.Now().Unix())
filename := fmt.Sprintf("./%s.tar", name)
f, err := os.Create(filename)
if err != nil {
Expand Down Expand Up @@ -144,7 +145,11 @@ func RunCollectCmdF(cmd *cobra.Command, args []string) error {
return err
}

t, err := terraform.New("", config)
return collect(config, "", "")
}

func collect(config deployment.Config, deploymentId string, outputName string) error {
t, err := terraform.New(deploymentId, config)
if err != nil {
return fmt.Errorf("failed to create terraform engine: %w", err)
}
Expand Down Expand Up @@ -256,7 +261,7 @@ func RunCollectCmdF(cmd *cobra.Command, args []string) error {
files[i] = <-filesChan
}

if err := saveCollection(files); err != nil {
if err := saveCollection(outputName, files); err != nil {
return fmt.Errorf("failed to save collection: %w", err)
}

Expand Down
28 changes: 28 additions & 0 deletions cmd/ltctl/comparison.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"time"

"github.com/mattermost/mattermost-load-test-ng/comparison"
"github.com/wiggin77/merror"

"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -172,6 +173,33 @@ func RunComparisonCmdF(cmd *cobra.Command, args []string) error {
return nil
}

func CollectComparisonCmdF(cmd *cobra.Command, args []string) error {
deployerConfig, err := getConfig(cmd)
if err != nil {
return err
}

configFilePath, _ := cmd.Flags().GetString("comparison-config")
cfg, err := comparison.ReadConfig(configFilePath)
if err != nil {
return fmt.Errorf("failed to read comparison config: %w", err)
}

cmp, err := comparison.New(cfg, &deployerConfig)
if err != nil {
return fmt.Errorf("failed to initialize comparison object: %w", err)
}

merr := merror.New()
for _, id := range cmp.GetDeploymentIds() {
if err := collect(deployerConfig, id, id+"_"); err != nil {
merr.Append(err)
}
}

return merr.ErrorOrNil()
}

func DestroyComparisonCmdF(cmd *cobra.Command, args []string) error {
deployerConfig, err := getConfig(cmd)
if err != nil {
Expand Down
9 changes: 8 additions & 1 deletion cmd/ltctl/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -329,12 +329,19 @@ func main() {
runComparisonCmd.Flags().StringP("output-dir", "d", "", "path to output directory")
runComparisonCmd.Flags().StringP("format", "f", "plain", "output format [plain, json]")

collectComparisonCmd := &cobra.Command{
Use: "collect",
Short: "Collect logs and configurations from all deployments",
RunE: CollectComparisonCmdF,
}

destroyComparisonCmd := &cobra.Command{
Use: "destroy",
Short: "Destroy the current load-test comparison environment",
RunE: DestroyComparisonCmdF,
}
comparisonCmd.AddCommand(runComparisonCmd, destroyComparisonCmd)

comparisonCmd.AddCommand(runComparisonCmd, destroyComparisonCmd, collectComparisonCmd)
rootCmd.AddCommand(comparisonCmd)

if err := rootCmd.Execute(); err != nil {
Expand Down
10 changes: 10 additions & 0 deletions comparison/comparison.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package comparison

import (
"fmt"
"sort"
"sync"

"github.com/mattermost/mattermost-load-test-ng/defaults"
Expand Down Expand Up @@ -153,3 +154,12 @@ func (c *Comparison) Destroy() error {
return t.Destroy()
})
}

func (c *Comparison) GetDeploymentIds() []string {
ids := []string{}
for k := range c.deployments {
ids = append(ids, k)
}
sort.Strings(ids)
return ids
}
50 changes: 50 additions & 0 deletions comparison/comparison_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package comparison

import (
"sort"
"testing"

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

func TestGetDeploymentIds(t *testing.T) {
testCases := []struct {
Name string
Ids []string
}{
{
Name: "Empty comparison",
Ids: []string{},
},
{
Name: "One element",
Ids: []string{"a"},
},
{
Name: "Elements already ordered",
Ids: []string{"a", "b"},
},
{
Name: "Unordered elements",
Ids: []string{"b", "a"},
},
}

emptyCfg := deploymentConfig{}
for _, tc := range testCases {
t.Run(tc.Name, func(t *testing.T) {
cmp := Comparison{}
cmp.deployments = make(map[string]*deploymentConfig)
for _, id := range tc.Ids {
cmp.deployments[id] = &emptyCfg
}

expectedIds := make([]string, len(tc.Ids))
copy(expectedIds, tc.Ids)
sort.Strings(expectedIds)

actualIds := cmp.GetDeploymentIds()
require.EqualValues(t, expectedIds, actualIds)
})
}
}

0 comments on commit 30a3c75

Please sign in to comment.