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 9c4c8c9
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
1 change: 1 addition & 0 deletions test/e2e/features/basic.feature
Original file line number Diff line number Diff line change
Expand Up @@ -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
52 changes: 52 additions & 0 deletions test/e2e/testsuite/testsuite.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package testsuite
import (
"context"
"crypto/tls"
"encoding/json"
"fmt"
"net/http"
"os"
Expand All @@ -12,6 +13,9 @@ import (
"strings"
"time"

"github.com/containers/common/pkg/strongunits"
"github.com/spf13/cast"

"github.com/crc-org/crc/v2/pkg/crc/constants"
"github.com/crc-org/crc/v2/pkg/crc/machine"
"github.com/crc-org/crc/v2/pkg/crc/preset"
Expand Down Expand Up @@ -516,6 +520,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 +667,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 9c4c8c9

Please sign in to comment.