Skip to content

Commit

Permalink
Dump CassandraDatacenter and CassandraTask yaml to the build output a…
Browse files Browse the repository at this point in the history
…lso, Fixes k8ssandra#312
  • Loading branch information
burmanm committed Apr 1, 2022
1 parent 3296abd commit fded188
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 6 deletions.
12 changes: 6 additions & 6 deletions tests/util/ginkgo/lib.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,44 +153,44 @@ func (ns *NsWrapper) genTestLogDir(description string) string {

func (ns *NsWrapper) ExecAndLog(description string, kcmd kubectl.KCmd) {
ginkgo.By(description)
defer kubectl.DumpLogs(ns.genTestLogDir(description), ns.Namespace).ExecVPanic()
defer kubectl.DumpClusterInfo(ns.genTestLogDir(description), ns.Namespace)
execErr := ns.ExecV(kcmd)
Expect(execErr).ToNot(HaveOccurred())
}

func (ns *NsWrapper) ExecAndLogAndExpectErrorString(description string, kcmd kubectl.KCmd, expectedError string) {
ginkgo.By(description)
defer kubectl.DumpLogs(ns.genTestLogDir(description), ns.Namespace).ExecVPanic()
defer kubectl.DumpClusterInfo(ns.genTestLogDir(description), ns.Namespace)
_, captureErr, execErr := ns.ExecVCapture(kcmd)
Expect(execErr).To(HaveOccurred())
Expect(captureErr).Should(ContainSubstring(expectedError))
}

func (ns *NsWrapper) OutputAndLog(description string, kcmd kubectl.KCmd) string {
ginkgo.By(description)
defer kubectl.DumpLogs(ns.genTestLogDir(description), ns.Namespace).ExecVPanic()
defer kubectl.DumpClusterInfo(ns.genTestLogDir(description), ns.Namespace)
output, execErr := ns.Output(kcmd)
Expect(execErr).ToNot(HaveOccurred())
return output
}

func (ns *NsWrapper) WaitForOutputAndLog(description string, kcmd kubectl.KCmd, expected string, seconds int) {
ginkgo.By(description)
defer kubectl.DumpLogs(ns.genTestLogDir(description), ns.Namespace).ExecVPanic()
defer kubectl.DumpClusterInfo(ns.genTestLogDir(description), ns.Namespace)
execErr := ns.WaitForOutput(kcmd, expected, seconds)
Expect(execErr).ToNot(HaveOccurred())
}

func (ns *NsWrapper) WaitForOutputPatternAndLog(description string, kcmd kubectl.KCmd, expected string, seconds int) {
ginkgo.By(description)
defer kubectl.DumpLogs(ns.genTestLogDir(description), ns.Namespace).ExecVPanic()
defer kubectl.DumpClusterInfo(ns.genTestLogDir(description), ns.Namespace)
execErr := ns.WaitForOutputPattern(kcmd, expected, seconds)
Expect(execErr).ToNot(HaveOccurred())
}

func (ns *NsWrapper) WaitForOutputContainsAndLog(description string, kcmd kubectl.KCmd, expected string, seconds int) {
ginkgo.By(description)
defer kubectl.DumpLogs(ns.genTestLogDir(description), ns.Namespace).ExecVPanic()
defer kubectl.DumpClusterInfo(ns.genTestLogDir(description), ns.Namespace)
execErr := ns.WaitForOutputContains(kcmd, expected, seconds)
Expect(execErr).ToNot(HaveOccurred())
}
Expand Down
37 changes: 37 additions & 0 deletions tests/util/kubectl/kubectl.go
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,43 @@ func DumpLogs(path string, namespace string) KCmd {
return KCmd{Command: "cluster-info", Args: args, Flags: flags}
}

// DumpClusterInfo Executes `kubectl cluster-info dump -o yaml` on each cluster. The output
// is stored under <project-root>/build/test.
func DumpClusterInfo(path string, namespace string) error {
_ = os.MkdirAll(path, os.ModePerm)

dumpCmd := DumpLogs(path, namespace)
dumpCmd.ExecVPanic()

// Store the list of pods in an easy to read format.
podWide := Get("pods", "-o", "wide").OutputPanic()
storeOutput(path, "pods", "out", podWide)

// Dump all objects that we need to investigate failures as a flat list and as yaml manifests
for _, objectType := range []string{"CassandraDatacenter", "CassandraTask"} {
// Get the list of objects
output := Get(objectType, "-o", "out").OutputPanic()
storeOutput(path, objectType, "out", output)

// Get the yamls for each object
output = Get(objectType, "-o", "yaml").OutputPanic()
storeOutput(path, objectType, "yaml", output)
}

return nil
}

func storeOutput(path, objectType, ext, output string) {
filePath := fmt.Sprintf("%s/%s.%s", path, objectType, ext)
outputFile, err := os.Create(filePath)
if err != nil {
panic("Failed to create log file")
}
defer outputFile.Close()
outputFile.WriteString(output)
outputFile.Sync()
}

func ExecOnPod(podName string, args ...string) KCmd {
execArgs := []string{podName}
execArgs = append(execArgs, args...)
Expand Down

0 comments on commit fded188

Please sign in to comment.