Skip to content

Commit

Permalink
test (e2e) : add more assertions to verify crc status output
Browse files Browse the repository at this point in the history
Add stricter assertions to verify output of crc status

Signed-off-by: Rohan Kumar <[email protected]>
  • Loading branch information
rohanKanojia committed Feb 10, 2025
1 parent b03610c commit c54baee
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 2 deletions.
5 changes: 3 additions & 2 deletions test/e2e/features/basic.feature
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ Feature: Basic test
When executing crc status command fails
Then stderr should contain "crc does not seem to be setup correctly, have you run 'crc setup'?"

@darwin @linux @windows @cleanup
@darwin @linux @windows
Scenario: CRC start usecase
Given executing "crc setup --check-only" fails
# Request start with monitoring stack
Expand All @@ -43,6 +43,7 @@ Feature: Basic test
* stdout should match "(?s)(.*)https:\/\/console-openshift-console\.apps-crc\.testing(.*)$"
# status
When checking that CRC is running
And checking the CRC status JSON output is valid
# ip
When executing crc ip command succeeds
Then stdout should match "\d+\.\d+\.\d+\.\d+"
Expand Down Expand Up @@ -74,4 +75,4 @@ Feature: Basic test
And kubeconfig is cleaned up
# cleanup
When executing crc cleanup command succeeds
And executing "man -P cat crc" fails
# And executing "man -P cat crc" fails
51 changes: 51 additions & 0 deletions test/e2e/testsuite/testsuite.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ package testsuite
import (
"context"
"crypto/tls"
"encoding/json"
"fmt"
"github.com/containers/common/pkg/strongunits"

Check failure on line 8 in test/e2e/testsuite/testsuite.go

View workflow job for this annotation

GitHub Actions / build (macOS-13, 1.22)

File is not properly formatted (goimports)

Check failure on line 8 in test/e2e/testsuite/testsuite.go

View workflow job for this annotation

GitHub Actions / build (macOS-14, 1.22)

File is not properly formatted (goimports)

Check failure on line 8 in test/e2e/testsuite/testsuite.go

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 1.22)

File is not properly formatted (goimports)

Check failure on line 8 in test/e2e/testsuite/testsuite.go

View workflow job for this annotation

GitHub Actions / build (ubuntu-20.04, 1.22)

File is not properly formatted (goimports)
"github.com/spf13/cast"
"net/http"
"os"
"os/user"
Expand Down Expand Up @@ -516,6 +519,8 @@ func InitializeScenario(s *godog.ScenarioContext) {
CheckOutputMatchWithRetry)
s.Step(`^checking that CRC is (running|stopped)$`,
CheckCRCStatus)
s.Step(`^checking the CRC status JSON output is valid$`,
CheckCRCStatusJSONOutput)
s.Step(`^execut(?:e|ing) crc (.*) command$`,
ExecuteCRCCommand)
s.Step(`^execut(?:e|ing) crc (.*) command (.*)$`,
Expand Down Expand Up @@ -661,6 +666,52 @@ func CheckCRCStatus(state string) error {
return crcCmd.CheckCRCStatus(state)
}

func CheckCRCStatusJSONOutput() error {
err := util.ExecuteCommand("crc status -ojson")
if err != nil {
return err
}
crcStatusJSONOutput := util.GetLastCommandOutput("stdout")
var crcStatusJSONOutputObj map[string]interface{}
if err := json.Unmarshal([]byte(crcStatusJSONOutput), &crcStatusJSONOutputObj); err != nil {
return err
}
crcStatusSuccess := crcStatusJSONOutputObj["success"]
if crcStatusSuccess != true {
return fmt.Errorf("failure in asserting 'success' field of crc status json output, expectd : true, actual : %t", crcStatusSuccess)
}
crcStatus := crcStatusJSONOutputObj["crcStatus"]
if crcStatus != "Running" {
return fmt.Errorf("failure in asserting 'status' field of crc status json output, expectd : 'Running', actual : %s", crcStatus)
}
crcCacheDir := crcStatusJSONOutputObj["cacheDir"]
if crcCacheDir != filepath.Join(util.CRCHome, "cache") {
return fmt.Errorf("failure is asserting 'cacheDir' field of crc status json output, exected : %s, actual %s", filepath.Join(util.CRCHome, "cache"), crcCacheDir)
}
crcOpenShiftStatus := crcStatusJSONOutputObj["openshiftStatus"]
if crcOpenShiftStatus != "Running" {
return fmt.Errorf("failure in asserting 'openshiftStatus' field of crc status json output, expected : 'Running', actual : %s", crcOpenShiftStatus)
}
crcOpenShiftVersion := crcStatusJSONOutputObj["openshiftVersion"]
if !strings.HasPrefix(cast.ToString(crcOpenShiftVersion), "4.") {
return fmt.Errorf("failure in asserting 'openshiftVersion' field of crc status json output, expected with prefix: '4.', actual : %s", crcOpenShiftVersion)
}
crcPresetStr := crcStatusJSONOutputObj["preset"]
crcPreset, err := preset.ParsePresetE(crcPresetStr.(string))
if err != nil {
return fmt.Errorf("failure in asserting 'preset' field of crc status json output, %v", err)
}
crcDiskSize := crcStatusJSONOutputObj["diskSize"]
if strongunits.B(cast.ToUint64(crcDiskSize)) > strongunits.GiB(constants.DefaultDiskSize).ToBytes() {
return fmt.Errorf("failure in asserting 'diskSize' field of crc status json output, expected less than or equal to %d bytes, actual : %d bytes", strongunits.GiB(constants.DefaultDiskSize).ToBytes(), strongunits.B(cast.ToUint64(crcDiskSize)))
}
crcRAMSize := crcStatusJSONOutputObj["ramSize"]
if strongunits.B(cast.ToUint64(crcRAMSize)) > constants.GetDefaultMemory(crcPreset).ToBytes() {
return fmt.Errorf("failure in asserting 'ramSize' field of crc status json output, expected less than or equal to %d bytes, actual : %d bytes", constants.GetDefaultMemory(crcPreset).ToBytes(), cast.ToUint64(crcRAMSize))
}
return nil
}

func DeleteFileFromCRCHome(fileName string) error {

theFile := filepath.Join(util.CRCHome, fileName)
Expand Down

0 comments on commit c54baee

Please sign in to comment.