|
| 1 | +package ginkgo_util |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "regexp" |
| 6 | + "time" |
| 7 | + |
| 8 | + ginkgo "github.com/onsi/ginkgo" |
| 9 | + . "github.com/onsi/gomega" |
| 10 | + |
| 11 | + "github.com/riptano/dse-operator/mage/kubectl" |
| 12 | + mageutil "github.com/riptano/dse-operator/mage/util" |
| 13 | +) |
| 14 | + |
| 15 | +// Wrapper type to make it simpler to |
| 16 | +// set a namespace one time and execute all of your |
| 17 | +// KCmd objects inside of it, and then use Gomega |
| 18 | +// assertions on panic |
| 19 | +type NsWrapper struct { |
| 20 | + Namespace string |
| 21 | + TestSuiteName string |
| 22 | + LogDir string |
| 23 | + stepCounter int |
| 24 | +} |
| 25 | + |
| 26 | +func NewWrapper(suiteName string, namespace string) NsWrapper { |
| 27 | + return NsWrapper{ |
| 28 | + Namespace: namespace, |
| 29 | + TestSuiteName: suiteName, |
| 30 | + LogDir: genSuiteLogDir(suiteName), |
| 31 | + stepCounter: 1, |
| 32 | + } |
| 33 | + |
| 34 | +} |
| 35 | + |
| 36 | +func (k NsWrapper) ExecV(kcmd kubectl.KCmd) error { |
| 37 | + err := kcmd.InNamespace(k.Namespace).ExecV() |
| 38 | + return err |
| 39 | +} |
| 40 | + |
| 41 | +func (k NsWrapper) ExecVPanic(kcmd kubectl.KCmd) { |
| 42 | + err := kcmd.InNamespace(k.Namespace).ExecV() |
| 43 | + Expect(err).ToNot(HaveOccurred()) |
| 44 | +} |
| 45 | + |
| 46 | +func (k NsWrapper) Output(kcmd kubectl.KCmd) (string, error) { |
| 47 | + out, err := kcmd.InNamespace(k.Namespace).Output() |
| 48 | + return out, err |
| 49 | +} |
| 50 | + |
| 51 | +func (k NsWrapper) OutputPanic(kcmd kubectl.KCmd) string { |
| 52 | + out, err := kcmd.InNamespace(k.Namespace).Output() |
| 53 | + Expect(err).ToNot(HaveOccurred()) |
| 54 | + return out |
| 55 | +} |
| 56 | + |
| 57 | +func (k NsWrapper) WaitForOutput(kcmd kubectl.KCmd, expected string, seconds int) error { |
| 58 | + return kubectl.WaitForOutput(kcmd.InNamespace(k.Namespace), expected, seconds) |
| 59 | +} |
| 60 | + |
| 61 | +func (k NsWrapper) WaitForOutputPanic(kcmd kubectl.KCmd, expected string, seconds int) { |
| 62 | + err := kubectl.WaitForOutput(kcmd.InNamespace(k.Namespace), expected, seconds) |
| 63 | + Expect(err).ToNot(HaveOccurred()) |
| 64 | +} |
| 65 | + |
| 66 | +func (k *NsWrapper) countStep() int { |
| 67 | + n := k.stepCounter |
| 68 | + k.stepCounter++ |
| 69 | + return n |
| 70 | +} |
| 71 | + |
| 72 | +//=================================== |
| 73 | +// Logging functions for the NsWrapper |
| 74 | +// that execute the Kcmd and then dump |
| 75 | +// k8s logs for that namespace |
| 76 | +//==================================== |
| 77 | +func sanitizeForLogDirs(s string) string { |
| 78 | + reg, err := regexp.Compile(`[\s\\\/\-\.,]`) |
| 79 | + mageutil.PanicOnError(err) |
| 80 | + return reg.ReplaceAllLiteralString(s, "_") |
| 81 | +} |
| 82 | + |
| 83 | +func genSuiteLogDir(suiteName string) string { |
| 84 | + datetime := time.Now().Format("2006.01.02_15:04:05") |
| 85 | + return fmt.Sprintf("../../build/kubectl_dump/%s/%s", |
| 86 | + sanitizeForLogDirs(suiteName), datetime) |
| 87 | +} |
| 88 | + |
| 89 | +func (ns *NsWrapper) genTestLogDir(description string) string { |
| 90 | + sanitizedDesc := sanitizeForLogDirs(description) |
| 91 | + return fmt.Sprintf("%s/%02d_%s", ns.LogDir, ns.countStep(), sanitizedDesc) |
| 92 | +} |
| 93 | + |
| 94 | +func (ns *NsWrapper) ExecAndLog(description string, kcmd kubectl.KCmd) { |
| 95 | + ginkgo.By(description) |
| 96 | + defer kubectl.DumpLogs(ns.genTestLogDir(description), ns.Namespace).ExecVPanic() |
| 97 | + execErr := ns.ExecV(kcmd) |
| 98 | + Expect(execErr).ToNot(HaveOccurred()) |
| 99 | +} |
| 100 | + |
| 101 | +func (ns *NsWrapper) OutputAndLog(description string, kcmd kubectl.KCmd) string { |
| 102 | + ginkgo.By(description) |
| 103 | + defer kubectl.DumpLogs(ns.genTestLogDir(description), ns.Namespace).ExecVPanic() |
| 104 | + output, execErr := ns.Output(kcmd) |
| 105 | + Expect(execErr).ToNot(HaveOccurred()) |
| 106 | + return output |
| 107 | +} |
| 108 | + |
| 109 | +func (ns *NsWrapper) WaitForOutputAndLog(description string, kcmd kubectl.KCmd, expected string, seconds int) { |
| 110 | + ginkgo.By(description) |
| 111 | + defer kubectl.DumpLogs(ns.genTestLogDir(description), ns.Namespace).ExecVPanic() |
| 112 | + execErr := ns.WaitForOutput(kcmd, expected, seconds) |
| 113 | + Expect(execErr).ToNot(HaveOccurred()) |
| 114 | +} |
0 commit comments