Files
percona-toolkit/src/go/pt-k8s-debug-collector/main_test.go
Sveta Smirnova e8ebc855d0 PT-2134 - support for PostgreSQL and MySQL operators (#567)
* PT-2134 - Support for PostgreSQL and PS MySQL operators

For PS MySQL operator: added support of pt-mysql-summary
For PostgreSQL operator: added support for pg_gather
Options added:
  - kubeconfig - path to kubeconfig
  - forwardport - port to use when calling pt-*-summary tools ad pt_gather
Options changed:
  - resource - now default value is  (was pxc). New values added:
    psql - for PostgreSQL operator
    ps - for PS MySQL operator
    if default value () is used: only K8 information is collected
    Otherwise resource-specific summary is collected

* PT-2134_support_for_PostgreSQL_and_MySQL_operators

Fixed summary collection when connecting to PostgreSQL opertor and no resource specified

* PT-2134 - support for PostgreSQL and MySQL operators

Added test case for full supported set of values for option --resource,
added --kubeconfig and --forwardport options for test cases,
added support for environment variables KUBECONFIG_PXC, KUBECONFIG_PS, KUBECONFIG_PSMDB, KUBECONFIG_PSQL, FORWARDPORT for test cases

* PT-2134 - support for PostgreSQL and MySQL operators

updated docs/pt-k8s-debug-collector.rst,
replaced README.md in src/go/pt-k8s-debug-collector wih symbolic link to docs/pt-k8s-debug-collector.rst
to avoid having inconsistent documentation

* PT-2134 - support for PostgreSQL and MySQL operators

Removed curl STDERR from the command output

* PT-2134 - support for PostgreSQL and MySQL operators

typo in pt-k8s-debug-collector.rst

* PT-2134 - support for PostgreSQL and MySQL operators

Renamed --resource=psql to --resource=pg after review suggestion by Ege
and collecting feedback from potential users

Co-authored-by: EvgeniyPatlan <evgeniy.patlan@percona.com>
Co-authored-by: Carlos Salguero <carlos.salguero@percona.com>
2022-12-19 18:48:51 +03:00

161 lines
4.9 KiB
Go

package main
import (
"bytes"
"os"
"os/exec"
"path"
"strings"
"testing"
"golang.org/x/exp/slices"
)
/*
This test requires:
- Running K8 Operator installation
- kubectl configuration files, one for each supported operator
-- KUBECONFIG_PXC for K8SPXC
-- KUBECONFIG_PS for K8SPS
-- KUBECONFIG_PSMDB for K8SPSMDB
-- KUBECONFIG_PG for K8SPG
You can additionally set option FORWARDPORT if you want to use custom port when testing summaries.
pt-mysql-summary and pt-mongodb-summary must be in the PATH.
Since running pt-k8s-debug-collector may take long time run go test with increase timeout:
go test -timeout 6000s
We do not explicitly test --kubeconfig and --forwardport options, because they are used in other tests.
*/
/*
Tests collection of the individual files by pt-k8s-debug-collector.
Requires running K8SPXC instance and kubectl, configured to access that instance by default.
*/
func TestIndividualFiles(t *testing.T) {
if os.Getenv("KUBECONFIG_PXC") == "" {
t.Skip("TestIndividualFiles requires K8SPXC")
}
tests := []struct {
name string
cmd []string
want []string
preprocesor func(string) string
}{
{
// If the tool collects required log files
name: "pxc_logs_list",
// tar -tf cluster-dump-test.tar.gz --wildcards 'cluster-dump/*/var/lib/mysql/*'
cmd: []string{"tar", "-tf", "cluster-dump.tar.gz", "--wildcards", "cluster-dump/*/var/lib/mysql/*"},
want: []string{"auto.cnf", "grastate.dat", "gvwstate.dat", "innobackup.backup.log", "innobackup.move.log", "innobackup.prepare.log", "mysqld-error.log", "mysqld.post.processing.log"},
preprocesor: func(in string) string {
files := strings.Split(in, "\n")
var result []string
for _, f := range files {
b := path.Base(f)
if !slices.Contains(result, b) && b != "." && b != "" {
result = append(result, b)
}
}
slices.Sort(result)
return strings.Join(result, "\n")
},
},
{
// If MySQL error log is not empty
name: "pxc_mysqld_error_log",
// tar --to-command="grep -m 1 -o Version:" -xzf cluster-dump-test.tar.gz --wildcards 'cluster-dump/*/var/lib/mysql/mysqld-error.log'
cmd: []string{"tar", "--to-command", "grep -m 1 -o Version:", "-xzf", "cluster-dump.tar.gz", "--wildcards", "cluster-dump/*/var/lib/mysql/mysqld-error.log"},
want: []string{"Version:"},
preprocesor: func(in string) string {
nl := strings.Index(in, "\n")
if nl == -1 {
return ""
}
return in[:nl]
},
},
}
cmd := exec.Command("../../../bin/pt-k8s-debug-collector", "--kubeconfig", os.Getenv("KUBECONFIG_PXC"), "--forwardport", os.Getenv("FORWARDPORT"), "--resource", "pxc")
if err := cmd.Run(); err != nil {
t.Errorf("error executing pt-k8s-debug-collector: %s", err.Error())
}
defer func() {
cmd = exec.Command("rm", "-f", "cluster-dump.tar.gz")
if err := cmd.Run(); err != nil {
t.Errorf("error cleaning up test data: %s", err.Error())
}
}()
for _, test := range tests {
out, err := exec.Command(test.cmd[0], test.cmd[1:]...).CombinedOutput()
if err != nil {
t.Errorf("test %s, error running command %s:\n%s\n\nCommand output:\n%s", test.name, test.cmd[0], err.Error(), out)
}
if test.preprocesor(bytes.NewBuffer(out).String()) != strings.Join(test.want, "\n") {
t.Errorf("test %s, output is not as expected\nOutput: %s\nWanted: %s", test.name, test.preprocesor(bytes.NewBuffer(out).String()), test.want)
}
}
}
/*
Tests for supported values of the --resource option
*/
func TestResourceOption(t *testing.T) {
testcmd := []string{"sh", "-c", "tar -tf cluster-dump.tar.gz --wildcards '*/summary.txt' 2>/dev/null | wc -l"}
tests := []struct {
name string
want string
kubeconfig string
}{
{
name: "none",
want: "0",
kubeconfig: "",
},
{
name: "pxc",
want: "3",
kubeconfig: os.Getenv("KUBECONFIG_PXC"),
},
{
name: "ps",
want: "3",
kubeconfig: os.Getenv("KUBECONFIG_PS"),
},
{
name: "psmdb",
want: "3",
kubeconfig: os.Getenv("KUBECONFIG_PSMDB"),
},
{
name: "pg",
want: "3",
kubeconfig: os.Getenv("KUBECONFIG_PG"),
},
}
for _, test := range tests {
cmd := exec.Command("../../../bin/pt-k8s-debug-collector", "--kubeconfig", test.kubeconfig, "--forwardport", os.Getenv("FORWARDPORT"), "--resource", test.name)
if err := cmd.Run(); err != nil {
t.Errorf("error executing pt-k8s-debug-collector: %s", err.Error())
}
defer func() {
cmd = exec.Command("rm", "-f", "cluster-dump.tar.gz")
if err := cmd.Run(); err != nil {
t.Errorf("error cleaning up test data: %s", err.Error())
}
}()
out, err := exec.Command(testcmd[0], testcmd[1:]...).Output()
if err != nil {
t.Errorf("test %s, error running command %s:\n%s\n\nCommand output:\n%s", test.name, testcmd, err.Error(), out)
}
if strings.TrimRight(bytes.NewBuffer(out).String(), "\n") != test.want {
t.Errorf("test %s, output is not as expected\nOutput: %s\nWanted: %s", test.name, out, test.want)
}
}
}