From cadb7103fc09a85b29b314ba81a8e645d67cae00 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alejandro=20Garc=C3=ADa=20Montoro?= Date: Mon, 30 Oct 2023 21:00:49 +0100 Subject: [PATCH] Make gen string from PyrscopeSettings testable And test it! --- deployment/config.go | 14 ++++ deployment/terraform/metrics.go | 10 +-- deployment/terraform/strings_test.go | 101 +++++++++++++++++++++++++++ 3 files changed, 116 insertions(+), 9 deletions(-) create mode 100644 deployment/terraform/strings_test.go diff --git a/deployment/config.go b/deployment/config.go index 58a9c4239..4da45a4ea 100644 --- a/deployment/config.go +++ b/deployment/config.go @@ -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. diff --git a/deployment/terraform/metrics.go b/deployment/terraform/metrics.go index 9cec92afd..0fed97bea 100644 --- a/deployment/terraform/metrics.go +++ b/deployment/terraform/metrics.go @@ -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 { diff --git a/deployment/terraform/strings_test.go b/deployment/terraform/strings_test.go new file mode 100644 index 000000000..d6005868f --- /dev/null +++ b/deployment/terraform/strings_test.go @@ -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) + } + }) + } + +}