Skip to content

Commit

Permalink
🧹 restructure test logs for e2e tests (#1034)
Browse files Browse the repository at this point in the history
Signed-off-by: Ivan Milchev <[email protected]>
  • Loading branch information
imilchev authored Feb 27, 2024
1 parent 460d8b6 commit 2e812d0
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 18 deletions.
9 changes: 5 additions & 4 deletions tests/framework/installer/installer.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,8 @@ func (i *MondooInstaller) UninstallOperator() error {
zap.S().Warn("Operator not installed. Skip gathering logs...")
return nil
}
i.K8sHelper.GetLogsFromNamespace(i.Settings.Namespace, i.T().Name())

i.K8sHelper.GetLogsFromNamespace(i.Settings.Namespace, i.Settings.SuiteName, i.T().Name())

if err := i.CleanupAuditConfigs(); err != nil {
return err
Expand Down Expand Up @@ -210,9 +211,9 @@ func (i *MondooInstaller) CreateClientSecret(ns string) error {
func (i *MondooInstaller) GatherAllMondooLogs(testName string, namespaces ...string) {
zap.S().Infof("gathering all logs from the test")
for _, namespace := range namespaces {
i.K8sHelper.GetLogsFromNamespace(namespace, testName)
i.K8sHelper.GetDescribeFromNamespace(namespace, testName)
i.K8sHelper.GetEventsFromNamespace(namespace, testName)
i.K8sHelper.GetLogsFromNamespace(namespace, i.Settings.SuiteName, testName)
i.K8sHelper.GetDescribeFromNamespace(namespace, i.Settings.SuiteName, testName)
i.K8sHelper.GetEventsFromNamespace(namespace, i.Settings.SuiteName, testName)
}
}

Expand Down
1 change: 1 addition & 0 deletions tests/framework/installer/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
const MondooNamespace = "mondoo-operator"

type Settings struct {
SuiteName string
Namespace string
token string
installRelease bool
Expand Down
46 changes: 32 additions & 14 deletions tests/framework/utils/k8s_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,18 +251,18 @@ func LabelSelectorListOptions(labelSelector string) (*client.ListOptions, error)
}

// GetLogsFromNamespace collects logs for all containers in all pods in the namespace
func (k8sh *K8sHelper) GetLogsFromNamespace(namespace, testName string) {
func (k8sh *K8sHelper) GetLogsFromNamespace(namespace, suiteName, testName string) {
ctx := context.TODO()
zap.S().Infof("Gathering logs for all pods in namespace %s", namespace)
pods, err := k8sh.kubeClient.CoreV1().Pods(namespace).List(ctx, metav1.ListOptions{})
if err != nil {
zap.S().Errorf("failed to list pods in namespace %s. %+v", namespace, err)
return
}
k8sh.getPodsLogs(pods, namespace, testName)
k8sh.getPodsLogs(pods, namespace, suiteName, testName)
}

func (k8sh *K8sHelper) GetDescribeFromNamespace(namespace, testName string) {
func (k8sh *K8sHelper) GetDescribeFromNamespace(namespace, suiteName, testName string) {
ctx := context.TODO()
zap.S().Infof("Gathering pod describe for all pods in namespace %s", namespace)
pods, err := k8sh.kubeClient.CoreV1().Pods(namespace).List(ctx, metav1.ListOptions{})
Expand All @@ -271,7 +271,7 @@ func (k8sh *K8sHelper) GetDescribeFromNamespace(namespace, testName string) {
return
}

file, err := k8sh.createTestLogFile("", "describe", namespace, testName)
file, err := k8sh.createTestLogFile("", "describe", suiteName, testName, namespace)
if err != nil {
return
}
Expand Down Expand Up @@ -314,10 +314,10 @@ func (k8sh *K8sHelper) GetDescribeFromNamespace(namespace, testName string) {
}
}

func (k8sh *K8sHelper) GetEventsFromNamespace(namespace, testName string) {
func (k8sh *K8sHelper) GetEventsFromNamespace(namespace, suiteName, testName string) {
zap.S().Infof("Gathering events in namespace %q", namespace)

file, err := k8sh.createTestLogFile("", "events", namespace, testName)
file, err := k8sh.createTestLogFile("", "events", suiteName, testName, namespace)
if err != nil {
zap.S().Errorf("failed to create event file. %v", err)
return
Expand Down Expand Up @@ -474,17 +474,17 @@ func (k8sh *K8sHelper) getAuditConfigDescribe(namespace string, args ...string)
return description
}

func (k8sh *K8sHelper) getPodsLogs(pods *v1.PodList, namespace, testName string) {
func (k8sh *K8sHelper) getPodsLogs(pods *v1.PodList, namespace, suiteName, testName string) {
for _, p := range pods.Items {
k8sh.getPodLogs(p, namespace, testName, false)
k8sh.getPodLogs(p, namespace, suiteName, testName, false)
if strings.Contains(p.Name, "operator") {
// get the previous logs for the operator
k8sh.getPodLogs(p, namespace, testName, true)
k8sh.getPodLogs(p, namespace, suiteName, testName, true)
}
}
}

func (k8sh *K8sHelper) createTestLogFile(name, namespace, testName, suffix string) (*os.File, error) {
func (k8sh *K8sHelper) createTestLogFile(name, namespace, suiteName, testName, suffix string) (*os.File, error) {
dir, _ := os.Getwd()
logDir := path.Join(dir, "_output/tests/")
if _, err := os.Stat(logDir); os.IsNotExist(err) {
Expand All @@ -495,9 +495,27 @@ func (k8sh *K8sHelper) createTestLogFile(name, namespace, testName, suffix strin
}
}

fileName := fmt.Sprintf("%s_%s_%s%s_%d.log", testName, namespace, name, suffix, time.Now().Unix())
suiteDir := path.Join(logDir, suiteName)
if _, err := os.Stat(suiteDir); os.IsNotExist(err) {
err := os.MkdirAll(suiteDir, 0o777)
if err != nil {
zap.S().Errorf("Cannot get suite files dir for app : %v in namespace %v, err: %v", name, namespace, err)
return nil, err
}
}

testDir := path.Join(suiteDir, testName)
if _, err := os.Stat(testDir); os.IsNotExist(err) {
err := os.MkdirAll(testDir, 0o777)
if err != nil {
zap.S().Errorf("Cannot get test files dir for app : %v in namespace %v, err: %v", name, namespace, err)
return nil, err
}
}

fileName := fmt.Sprintf("%s_%s%s_%d.log", namespace, name, suffix, time.Now().Unix())
fileName = strings.ReplaceAll(fileName, "/", "")
filePath := path.Join(logDir, fileName)
filePath := path.Join(testDir, fileName)
file, err := os.Create(filePath)
if err != nil {
zap.S().Errorf("Cannot create file %s. %v", filePath, err)
Expand All @@ -508,12 +526,12 @@ func (k8sh *K8sHelper) createTestLogFile(name, namespace, testName, suffix strin
return file, nil
}

func (k8sh *K8sHelper) getPodLogs(pod v1.Pod, namespace, testName string, previousLog bool) {
func (k8sh *K8sHelper) getPodLogs(pod v1.Pod, namespace, suiteName, testName string, previousLog bool) {
suffix := ""
if previousLog {
suffix = "_previous"
}
file, err := k8sh.createTestLogFile(pod.Name, namespace, testName, suffix)
file, err := k8sh.createTestLogFile(pod.Name, namespace, suiteName, testName, suffix)
if err != nil {
return
}
Expand Down
1 change: 1 addition & 0 deletions tests/integration/audit_config_namespace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ type AuditConfigCustomNamespaceSuite struct {

func (s *AuditConfigCustomNamespaceSuite) SetupSuite() {
s.AuditConfigBaseSuite.SetupSuite()
s.testCluster.MondooInstaller.Settings.SuiteName = "AuditConfigCustomNamespaceSuite"

s.ns = &corev1.Namespace{}
s.ns.Name = "some-namespace"
Expand Down
5 changes: 5 additions & 0 deletions tests/integration/audit_config_oom_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ type AuditConfigOOMSuite struct {
AuditConfigBaseSuite
}

func (s *AuditConfigOOMSuite) SetupSuite() {
s.AuditConfigBaseSuite.SetupSuite()
s.testCluster.MondooInstaller.Settings.SuiteName = "AuditConfigOOMSuite"
}

func (s *AuditConfigOOMSuite) TestOOMControllerReporting() {
auditConfig := utils.DefaultAuditConfigMinimal(s.testCluster.Settings.Namespace, false, false, false, false)
s.auditConfig = auditConfig
Expand Down
5 changes: 5 additions & 0 deletions tests/integration/audit_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ type AuditConfigSuite struct {
AuditConfigBaseSuite
}

func (s *AuditConfigSuite) SetupSuite() {
s.AuditConfigBaseSuite.SetupSuite()
s.testCluster.MondooInstaller.Settings.SuiteName = "AuditConfigSuite"
}

func (s *AuditConfigSuite) TestReconcile_AllDisabled() {
auditConfig := utils.DefaultAuditConfigMinimal(s.testCluster.Settings.Namespace, false, false, false, false)
s.testMondooAuditConfigAllDisabled(auditConfig)
Expand Down
5 changes: 5 additions & 0 deletions tests/integration/audit_config_upgrade_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ type AuditConfigUpgradeSuite struct {
AuditConfigBaseSuite
}

func (s *AuditConfigUpgradeSuite) SetupSuite() {
s.AuditConfigBaseSuite.SetupSuite()
s.testCluster.MondooInstaller.Settings.SuiteName = "AuditConfigUpgradeSuite"
}

func (s *AuditConfigUpgradeSuite) AfterTest(suiteName, testName string) {
if s.testCluster != nil {
s.testCluster.GatherAllMondooLogs(testName, installer.MondooNamespace)
Expand Down

0 comments on commit 2e812d0

Please sign in to comment.