diff --git a/test/e2e/features/story_openshift.feature b/test/e2e/features/story_openshift.feature index 37ecd0b78e..e11f4c6952 100644 --- a/test/e2e/features/story_openshift.feature +++ b/test/e2e/features/story_openshift.feature @@ -12,6 +12,8 @@ Feature: 4 Openshift stories @darwin @linux @windows @testdata @story_health @needs_namespace Scenario: Overall cluster health Given executing "oc new-project testproj" succeeds + And get cpu data "After start" + And get memory data "After start" When executing "oc apply -f httpd-example.yaml" succeeds And executing "oc rollout status deployment httpd-example" succeeds Then stdout should contain "successfully rolled out" @@ -23,10 +25,14 @@ Feature: 4 Openshift stories Then stdout should contain "httpd-example exposed" When executing "oc expose svc httpd-example" succeeds Then stdout should contain "httpd-example exposed" + And get cpu data "After deployment" + And get memory data "After deployment" When with up to "20" retries with wait period of "5s" http response from "http://httpd-example-testproj.apps-crc.testing" has status code "200" Then executing "curl -s http://httpd-example-testproj.apps-crc.testing" succeeds And stdout should contain "Hello CRC!" When executing "crc stop" succeeds + And get cpu data "After stop" + And get memory data "After stop" And starting CRC with default bundle succeeds And checking that CRC is running And with up to "4" retries with wait period of "1m" http response from "http://httpd-example-testproj.apps-crc.testing" has status code "200" diff --git a/test/e2e/testsuite/testsuite.go b/test/e2e/testsuite/testsuite.go index 84d203ebce..ca27790fc1 100644 --- a/test/e2e/testsuite/testsuite.go +++ b/test/e2e/testsuite/testsuite.go @@ -28,6 +28,8 @@ import ( crcCmd "github.com/crc-org/crc/v2/test/extended/crc/cmd" "github.com/crc-org/crc/v2/test/extended/util" "github.com/cucumber/godog" + "github.com/shirou/gopsutil/v4/cpu" + "github.com/shirou/gopsutil/v4/mem" "github.com/spf13/pflag" ) @@ -261,6 +263,15 @@ func InitializeScenario(s *godog.ScenarioContext) { os.Exit(1) } } + + if tag.Name == "@story_health" { + if err := getCPUdata("Before start"); err != nil { + fmt.Printf("Failed to collect CPU data: %v\n", err) + } + if err := getMemoryData("Before start"); err != nil { + fmt.Printf("Failed to collect memory data: %v\n", err) + } + } } return ctx, nil @@ -568,6 +579,10 @@ func InitializeScenario(s *godog.ScenarioContext) { EnsureApplicationIsAccessibleViaNodePort) s.Step(`^persistent volume of size "([^"]*)"GB exists$`, EnsureVMPartitionSizeCorrect) + s.Step(`^get cpu data "([^"]*)"`, + getCPUdata) + s.Step(`^get memory data "([^"]*)"`, + getMemoryData) s.After(func(ctx context.Context, _ *godog.Scenario, err error) (context.Context, error) { @@ -1304,3 +1319,35 @@ func deserializeListBlockDeviceCommandOutputToExtractPVSize(lsblkOutput string) } return diskSize - (lvmSize + 1), nil } + +func getCPUdata(content string) error { + cpuData, err := cpu.Percent(0, false) + if err != nil { + return fmt.Errorf("failed to get CPU data: %v", err) + } + if len(cpuData) == 0 { + return fmt.Errorf("no CPU data available") + } + data := fmt.Sprintf("%s: %.2f%%\n", content, cpuData) + wd, _ := os.Getwd() + file := filepath.Join(wd, "../test-results/cpu-consume.txt") + return util.WriteToFile(data, file) +} + +func getMemoryData(content string) error { + v, err := mem.VirtualMemory() + if err != nil { + return fmt.Errorf("failed to get memory data: %v", err) + } + if v == nil { + return fmt.Errorf("no memory data available") + } + data := fmt.Sprintf("%s, UsedPercent: %.2f%%, Free: %d MiB\n", content, v.UsedPercent, v.Free/1024/1024) + + wd, err := os.Getwd() + if err != nil { + return fmt.Errorf("failed to get working directory: %v", err) + } + file := filepath.Join(wd, "../test-results/memory-consume.txt") + return util.WriteToFile(data, file) +} diff --git a/test/extended/util/fileops.go b/test/extended/util/fileops.go index 864493e2de..7d675e4ada 100644 --- a/test/extended/util/fileops.go +++ b/test/extended/util/fileops.go @@ -91,7 +91,7 @@ func CreateFile(fileName string) error { } func WriteToFile(text string, fileName string) error { - file, err := os.OpenFile(fileName, os.O_RDWR, 0644) + file, err := os.OpenFile(fileName, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0644) if err != nil { return err }