Skip to content

Commit 9c4c8c9

Browse files
committed
test (e2e) : add more assertions to verify crc status output
Add stricter assertions to verify output of crc status Signed-off-by: Rohan Kumar <[email protected]>
1 parent b03610c commit 9c4c8c9

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

test/e2e/features/basic.feature

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ Feature: Basic test
4343
* stdout should match "(?s)(.*)https:\/\/console-openshift-console\.apps-crc\.testing(.*)$"
4444
# status
4545
When checking that CRC is running
46+
And checking the CRC status JSON output is valid
4647
# ip
4748
When executing crc ip command succeeds
4849
Then stdout should match "\d+\.\d+\.\d+\.\d+"

test/e2e/testsuite/testsuite.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package testsuite
33
import (
44
"context"
55
"crypto/tls"
6+
"encoding/json"
67
"fmt"
78
"net/http"
89
"os"
@@ -12,6 +13,9 @@ import (
1213
"strings"
1314
"time"
1415

16+
"github.com/containers/common/pkg/strongunits"
17+
"github.com/spf13/cast"
18+
1519
"github.com/crc-org/crc/v2/pkg/crc/constants"
1620
"github.com/crc-org/crc/v2/pkg/crc/machine"
1721
"github.com/crc-org/crc/v2/pkg/crc/preset"
@@ -516,6 +520,8 @@ func InitializeScenario(s *godog.ScenarioContext) {
516520
CheckOutputMatchWithRetry)
517521
s.Step(`^checking that CRC is (running|stopped)$`,
518522
CheckCRCStatus)
523+
s.Step(`^checking the CRC status JSON output is valid$`,
524+
CheckCRCStatusJSONOutput)
519525
s.Step(`^execut(?:e|ing) crc (.*) command$`,
520526
ExecuteCRCCommand)
521527
s.Step(`^execut(?:e|ing) crc (.*) command (.*)$`,
@@ -661,6 +667,52 @@ func CheckCRCStatus(state string) error {
661667
return crcCmd.CheckCRCStatus(state)
662668
}
663669

670+
func CheckCRCStatusJSONOutput() error {
671+
err := util.ExecuteCommand("crc status -ojson")
672+
if err != nil {
673+
return err
674+
}
675+
crcStatusJSONOutput := util.GetLastCommandOutput("stdout")
676+
var crcStatusJSONOutputObj map[string]interface{}
677+
if err := json.Unmarshal([]byte(crcStatusJSONOutput), &crcStatusJSONOutputObj); err != nil {
678+
return err
679+
}
680+
crcStatusSuccess := crcStatusJSONOutputObj["success"]
681+
if crcStatusSuccess != true {
682+
return fmt.Errorf("failure in asserting 'success' field of crc status json output, expectd : true, actual : %t", crcStatusSuccess)
683+
}
684+
crcStatus := crcStatusJSONOutputObj["crcStatus"]
685+
if crcStatus != "Running" {
686+
return fmt.Errorf("failure in asserting 'status' field of crc status json output, expectd : 'Running', actual : %s", crcStatus)
687+
}
688+
crcCacheDir := crcStatusJSONOutputObj["cacheDir"]
689+
if crcCacheDir != filepath.Join(util.CRCHome, "cache") {
690+
return fmt.Errorf("failure is asserting 'cacheDir' field of crc status json output, exected : %s, actual %s", filepath.Join(util.CRCHome, "cache"), crcCacheDir)
691+
}
692+
crcOpenShiftStatus := crcStatusJSONOutputObj["openshiftStatus"]
693+
if crcOpenShiftStatus != "Running" {
694+
return fmt.Errorf("failure in asserting 'openshiftStatus' field of crc status json output, expected : 'Running', actual : %s", crcOpenShiftStatus)
695+
}
696+
crcOpenShiftVersion := crcStatusJSONOutputObj["openshiftVersion"]
697+
if !strings.HasPrefix(cast.ToString(crcOpenShiftVersion), "4.") {
698+
return fmt.Errorf("failure in asserting 'openshiftVersion' field of crc status json output, expected with prefix: '4.', actual : %s", crcOpenShiftVersion)
699+
}
700+
crcPresetStr := crcStatusJSONOutputObj["preset"]
701+
crcPreset, err := preset.ParsePresetE(crcPresetStr.(string))
702+
if err != nil {
703+
return fmt.Errorf("failure in asserting 'preset' field of crc status json output, %v", err)
704+
}
705+
crcDiskSize := crcStatusJSONOutputObj["diskSize"]
706+
if strongunits.B(cast.ToUint64(crcDiskSize)) > strongunits.GiB(constants.DefaultDiskSize).ToBytes() {
707+
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)))
708+
}
709+
crcRAMSize := crcStatusJSONOutputObj["ramSize"]
710+
if strongunits.B(cast.ToUint64(crcRAMSize)) > constants.GetDefaultMemory(crcPreset).ToBytes() {
711+
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))
712+
}
713+
return nil
714+
}
715+
664716
func DeleteFileFromCRCHome(fileName string) error {
665717

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

0 commit comments

Comments
 (0)