Skip to content

Commit

Permalink
Make gen string from PyrscopeSettings testable
Browse files Browse the repository at this point in the history
And test it!
  • Loading branch information
agarciamontoro committed Oct 30, 2023
1 parent e8c6757 commit cadb710
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 9 deletions.
14 changes: 14 additions & 0 deletions deployment/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,20 @@ type PyroscopeSettings struct {
EnableAgentProfiling bool `default:"true"`
}

func (ps PyroscopeSettings) GenString(template string, mmTargets, ltTargets []string) string {
pyroscopeAppConfig := ""
if ps.EnableAppProfiling {
pyroscopeAppConfig = strings.Join(mmTargets, ",")
}
pyroscopeAgentsConfig := ""
if ps.EnableAgentProfiling {
pyroscopeAgentsConfig = strings.Join(ltTargets, ",")
}
pyroscopeConfigFile := fmt.Sprintf(template, pyroscopeAppConfig, pyroscopeAgentsConfig)

return pyroscopeConfigFile
}

// TerraformDBSettings contains the necessary data
// to configure an instance to be deployed
// and provisioned.
Expand Down
10 changes: 1 addition & 9 deletions deployment/terraform/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,16 +140,8 @@ func (t *Terraform) setupMetrics(extAgent *ssh.ExtAgent) error {
return fmt.Errorf("error upload prometheus config: output: %s, error: %w", out, err)
}

pyroscopeAppConfig := ""
if t.config.PyroscopeSettings.EnableAppProfiling {
pyroscopeAppConfig = strings.Join(mmTargets, ",")
}
pyroscopeAgentsConfig := ""
if t.config.PyroscopeSettings.EnableAppProfiling {
pyroscopeAgentsConfig = strings.Join(ltTargets, ",")
}
mlog.Info("Updating Pyroscope config", mlog.String("host", t.output.MetricsServer.PublicIP))
pyroscopeConfigFile := fmt.Sprintf(pyroscopeConfig, pyroscopeAppConfig, pyroscopeAgentsConfig)
pyroscopeConfigFile := t.config.PyroscopeSettings.GenString(pyroscopeConfig, mmTargets, ltTargets)

rdr = strings.NewReader(pyroscopeConfigFile)
if out, err := sshc.Upload(rdr, "/etc/pyroscope/server.yml", true); err != nil {
Expand Down
101 changes: 101 additions & 0 deletions deployment/terraform/strings_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package terraform

import (
"fmt"
"regexp"
"strings"
"testing"

"github.com/mattermost/mattermost-load-test-ng/deployment"
"github.com/stretchr/testify/require"
)

var reListElems = regexp.MustCompile(`\[(.*)\]`)

func TestPyroscopeSettingsGenString(t *testing.T) {
mmTarget := "app-0:8067"
ltTarget := "agent-0:4000"

// The Name represents the state of each setting, with
// 1 meaning true for bool and non-empty for []string,
// and 0 meaning false for bool and empty for []string
testCases := []struct {
Name string
EnableAppProfiling bool
EnableAgentProfiling bool
MMTargets []string
LTTargets []string
}{
{"0000", false, false, []string{}, []string{}},
{"0001", false, false, []string{}, []string{ltTarget}},
{"0010", false, false, []string{mmTarget}, []string{}},
{"0011", false, false, []string{mmTarget}, []string{ltTarget}},
{"0100", false, true, []string{}, []string{}},
{"0101", false, true, []string{}, []string{ltTarget}},
{"0110", false, true, []string{mmTarget}, []string{}},
{"0111", false, true, []string{mmTarget}, []string{ltTarget}},
{"1000", true, false, []string{}, []string{}},
{"1001", true, false, []string{}, []string{ltTarget}},
{"1010", true, false, []string{mmTarget}, []string{}},
{"1011", true, false, []string{mmTarget}, []string{ltTarget}},
{"1100", true, true, []string{}, []string{}},
{"1101", true, true, []string{}, []string{ltTarget}},
{"1110", true, true, []string{mmTarget}, []string{}},
{"1111", true, true, []string{mmTarget}, []string{ltTarget}},
}

for _, tc := range testCases {
t.Run(tc.Name, func(t *testing.T) {
settings := deployment.PyroscopeSettings{
EnableAppProfiling: tc.EnableAppProfiling,
EnableAgentProfiling: tc.EnableAgentProfiling,
}

generatedYaml := settings.GenString(pyroscopeConfig, tc.MMTargets, tc.LTTargets)

// We need to do some parsing here to get the part we're interested in, which is the targets
// for the app nodes (the first 'targets: [...]' line), and the targets for the agent nodes,
// (the second 'targets: [...]' line), and then we extract everything that is inside the
// square brackets
targets := []string{}
for _, line := range strings.Split(generatedYaml, "\n") {
if strings.Contains(line, "targets") {
matches := reListElems.FindStringSubmatch(line)
fmt.Println(matches)
require.Len(t, matches, 2)
targets = append(targets, matches[1])
}
}
require.Len(t, targets, 2)

// Now we need to reconstruct the slice of targets for the app nodes
actualMMTargets := []string{}
mmTargets := targets[0]
if mmTargets != "" {
actualMMTargets = strings.Split(mmTargets, ",")
}

// And the same for the agent nodes
actualLTTargets := []string{}
ltTargets := targets[1]
if ltTargets != "" {
actualLTTargets = strings.Split(ltTargets, ",")
}

// Now's time to check the targets for the app nodes
if tc.EnableAppProfiling {
require.ElementsMatch(t, tc.MMTargets, actualMMTargets)
} else {
require.Empty(t, actualMMTargets)
}

// And again for the agent nodes
if tc.EnableAgentProfiling {
require.ElementsMatch(t, tc.LTTargets, actualLTTargets)
} else {
require.Empty(t, actualLTTargets)
}
})
}

}

0 comments on commit cadb710

Please sign in to comment.