Skip to content

Commit

Permalink
fix (machine) : crc daemon /status api should return correct preset…
Browse files Browse the repository at this point in the history
… value for `okd` cluster (#4478)

+ Refactor `Status()` method to move ClusterStatusResult object creation
  logic to a separate method for easier testing
+ Add case for OKD preset while setting preset value in cluster preset

Signed-off-by: Rohan Kumar <[email protected]>
  • Loading branch information
rohanKanojia committed Nov 26, 2024
1 parent 8a1d173 commit 157b1a0
Show file tree
Hide file tree
Showing 2 changed files with 283 additions and 23 deletions.
54 changes: 31 additions & 23 deletions pkg/crc/machine/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,45 +33,53 @@ func (client *client) Status() (*types.ClusterStatusResult, error) {
return nil, errors.Wrap(err, "Cannot get machine state")
}

ip, err := vm.IP()
if err != nil {
return nil, errors.Wrap(err, "Error getting ip")
}
ramSize, ramUse := client.getRAMStatus(vm)
diskSize, diskUse := client.getDiskDetails(vm)
pvSize, pvUse := client.getPVCSize(vm)
var openShiftStatusSupplier = getOpenShiftStatus
if vm.bundle.IsMicroshift() {
openShiftStatusSupplier = getMicroShiftStatus
}

return createClusterStatusResult(vmStatus, vm.bundle.GetBundleType(), vm.bundle.GetVersion(), ip, ramSize, ramUse, diskSize, diskUse, pvSize, pvUse, openShiftStatusSupplier)
}

func createClusterStatusResult(vmStatus state.State, bundleType preset.Preset, vmBundleVersion string, vmIP string,
diskSize int64, diskUse int64, ramSize int64, ramUse int64, pvUse int, pvSize int,
openShiftStatusSupplier func(context.Context, string) types.OpenshiftStatus) (*types.ClusterStatusResult, error) {
clusterStatusResult := &types.ClusterStatusResult{
CrcStatus: vmStatus,
CrcStatus: vmStatus,
OpenshiftVersion: vmBundleVersion,
OpenshiftStatus: types.OpenshiftStopped,
}
switch {
case vm.bundle.IsMicroshift():
clusterStatusResult.OpenshiftStatus = types.OpenshiftStopped
clusterStatusResult.OpenshiftVersion = vm.bundle.GetVersion()
switch bundleType {
case preset.Microshift:
clusterStatusResult.Preset = preset.Microshift
case preset.OKD:
clusterStatusResult.Preset = preset.OKD
default:
clusterStatusResult.OpenshiftStatus = types.OpenshiftStopped
clusterStatusResult.OpenshiftVersion = vm.bundle.GetVersion()
clusterStatusResult.Preset = preset.OpenShift
}

if vmStatus != state.Running {
return clusterStatusResult, nil
}

ip, err := vm.IP()
if err != nil {
return nil, errors.Wrap(err, "Error getting ip")
}

diskSize, diskUse := client.getDiskDetails(vm)
clusterStatusResult.CrcStatus = state.Running
clusterStatusResult.DiskUse = diskUse
clusterStatusResult.DiskSize = diskSize

switch {
case vm.bundle.IsMicroshift():
clusterStatusResult.OpenshiftStatus = getMicroShiftStatus(context.Background(), ip)
clusterStatusResult.PersistentVolumeUse, clusterStatusResult.PersistentVolumeSize = client.getPVCSize(vm)
case vm.bundle.IsOpenShift():
clusterStatusResult.OpenshiftStatus = getOpenShiftStatus(context.Background(), ip)
}

ramSize, ramUse := client.getRAMStatus(vm)
clusterStatusResult.RAMSize = ramSize
clusterStatusResult.RAMUse = ramUse
clusterStatusResult.OpenshiftStatus = openShiftStatusSupplier(context.Background(), vmIP)

if bundleType == preset.Microshift {
clusterStatusResult.PersistentVolumeUse = pvUse
clusterStatusResult.PersistentVolumeSize = pvSize
}

return clusterStatusResult, nil
}
Expand Down
252 changes: 252 additions & 0 deletions pkg/crc/machine/status_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,252 @@
package machine

import (
"context"
"testing"

"github.com/crc-org/crc/v2/pkg/crc/machine/state"
"github.com/crc-org/crc/v2/pkg/crc/machine/types"
"github.com/crc-org/crc/v2/pkg/crc/preset"
"github.com/stretchr/testify/assert"
)

func TestCreateClusterStatusResultShouldSetOpenShiftStatusAsExpected(t *testing.T) {
tests := []struct {
name string
vmStatus state.State
vmBundleType preset.Preset
expectedClusterStatus types.ClusterStatusResult
}{
{
"MicroShift cluster running", state.Running, preset.Microshift, types.ClusterStatusResult{
CrcStatus: "Running",
OpenshiftStatus: "Running",
OpenshiftVersion: "v4.5.1",
DiskUse: int64(16),
DiskSize: int64(32),
RAMUse: int64(8),
RAMSize: int64(12),
PersistentVolumeUse: 16,
PersistentVolumeSize: 32,
Preset: preset.Microshift,
},
},
{
"MicroShift cluster stopped", state.Stopped, preset.Microshift, types.ClusterStatusResult{
CrcStatus: "Stopped",
OpenshiftStatus: "Stopped",
OpenshiftVersion: "v4.5.1",
Preset: preset.Microshift,
DiskUse: int64(0),
DiskSize: int64(0),
RAMUse: int64(0),
RAMSize: int64(0),
PersistentVolumeUse: 0,
PersistentVolumeSize: 0,
},
},
{
"MicroShift cluster error state", state.Error, preset.Microshift, types.ClusterStatusResult{
CrcStatus: "Error",
OpenshiftStatus: "Stopped",
OpenshiftVersion: "v4.5.1",
Preset: preset.Microshift,
DiskUse: int64(0),
DiskSize: int64(0),
RAMUse: int64(0),
RAMSize: int64(0),
PersistentVolumeUse: 0,
PersistentVolumeSize: 0,
},
},
{
"MicroShift cluster stopping state", state.Stopping, preset.Microshift, types.ClusterStatusResult{
CrcStatus: "Stopping",
OpenshiftStatus: "Stopped",
OpenshiftVersion: "v4.5.1",
Preset: preset.Microshift,
DiskUse: int64(0),
DiskSize: int64(0),
RAMUse: int64(0),
RAMSize: int64(0),
PersistentVolumeUse: 0,
PersistentVolumeSize: 0,
},
},
{
"MicroShift cluster starting state", state.Starting, preset.Microshift, types.ClusterStatusResult{
CrcStatus: "Starting",
OpenshiftStatus: "Stopped",
OpenshiftVersion: "v4.5.1",
Preset: preset.Microshift,
DiskUse: int64(0),
DiskSize: int64(0),
RAMUse: int64(0),
RAMSize: int64(0),
PersistentVolumeUse: 0,
PersistentVolumeSize: 0,
},
},
{
"OpenShift cluster running", state.Running, preset.OpenShift, types.ClusterStatusResult{
CrcStatus: "Running",
OpenshiftStatus: "Running",
OpenshiftVersion: "v4.5.1",
Preset: preset.OpenShift,
DiskUse: int64(16),
DiskSize: int64(32),
RAMUse: int64(8),
RAMSize: int64(12),
PersistentVolumeUse: 0,
PersistentVolumeSize: 0,
},
},
{
"OpenShift cluster stopped", state.Stopped, preset.OpenShift, types.ClusterStatusResult{
CrcStatus: "Stopped",
OpenshiftStatus: "Stopped",
OpenshiftVersion: "v4.5.1",
Preset: preset.OpenShift,
DiskUse: int64(0),
DiskSize: int64(0),
RAMUse: int64(0),
RAMSize: int64(0),
PersistentVolumeUse: 0,
PersistentVolumeSize: 0,
},
},
{
"OpenShift cluster errored", state.Error, preset.OpenShift, types.ClusterStatusResult{
CrcStatus: "Error",
OpenshiftStatus: "Stopped",
OpenshiftVersion: "v4.5.1",
Preset: preset.OpenShift,
DiskUse: int64(0),
DiskSize: int64(0),
RAMUse: int64(0),
RAMSize: int64(0),
PersistentVolumeUse: 0,
PersistentVolumeSize: 0,
},
},
{
"OpenShift cluster stopping state", state.Stopping, preset.OpenShift, types.ClusterStatusResult{
CrcStatus: "Stopping",
OpenshiftStatus: "Stopped",
OpenshiftVersion: "v4.5.1",
Preset: preset.OpenShift,
DiskUse: int64(0),
DiskSize: int64(0),
RAMUse: int64(0),
RAMSize: int64(0),
PersistentVolumeUse: 0,
PersistentVolumeSize: 0,
},
},
{
"OpenShift cluster starting state", state.Starting, preset.OpenShift, types.ClusterStatusResult{
CrcStatus: "Starting",
OpenshiftStatus: "Stopped",
OpenshiftVersion: "v4.5.1",
Preset: preset.OpenShift,
DiskUse: int64(0),
DiskSize: int64(0),
RAMUse: int64(0),
RAMSize: int64(0),
PersistentVolumeUse: 0,
PersistentVolumeSize: 0,
},
},
{
"OpenShift/OKD cluster running", state.Running, preset.OKD, types.ClusterStatusResult{
CrcStatus: "Running",
OpenshiftStatus: "Running",
OpenshiftVersion: "v4.5.1",
Preset: preset.OKD,
DiskUse: int64(16),
DiskSize: int64(32),
RAMUse: int64(8),
RAMSize: int64(12),
PersistentVolumeUse: 0,
PersistentVolumeSize: 0,
},
},
{
"OpenShift/OKD cluster stopped", state.Stopped, preset.OKD, types.ClusterStatusResult{
CrcStatus: "Stopped",
OpenshiftStatus: "Stopped",
OpenshiftVersion: "v4.5.1",
Preset: preset.OKD,
DiskUse: int64(0),
DiskSize: int64(0),
RAMUse: int64(0),
RAMSize: int64(0),
PersistentVolumeUse: 0,
PersistentVolumeSize: 0,
},
},
{
"OpenShift/OKD cluster errored", state.Error, preset.OKD, types.ClusterStatusResult{
CrcStatus: "Error",
OpenshiftStatus: "Stopped",
OpenshiftVersion: "v4.5.1",
Preset: preset.OKD,
DiskUse: int64(0),
DiskSize: int64(0),
RAMUse: int64(0),
RAMSize: int64(0),
PersistentVolumeUse: 0,
PersistentVolumeSize: 0,
},
},
{
"OpenShift/OKD cluster stopping state", state.Stopping, preset.OKD, types.ClusterStatusResult{
CrcStatus: "Stopping",
OpenshiftStatus: "Stopped",
OpenshiftVersion: "v4.5.1",
Preset: preset.OKD,
DiskUse: int64(0),
DiskSize: int64(0),
RAMUse: int64(0),
RAMSize: int64(0),
PersistentVolumeUse: 0,
PersistentVolumeSize: 0,
},
},
{
"OpenShift/OKD cluster starting state", state.Starting, preset.OKD, types.ClusterStatusResult{
CrcStatus: "Starting",
OpenshiftStatus: "Stopped",
OpenshiftVersion: "v4.5.1",
Preset: preset.OKD,
DiskUse: int64(0),
DiskSize: int64(0),
RAMUse: int64(0),
RAMSize: int64(0),
PersistentVolumeUse: 0,
PersistentVolumeSize: 0,
},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Given
// When
actualClusterStatusResult, err := createClusterStatusResult(tt.vmStatus, tt.vmBundleType, "v4.5.1", "127.0.0.1", 32, 16, 12, 8, 16, 32, func(context.Context, string) types.OpenshiftStatus { return types.OpenshiftRunning })

// Then
assert.NoError(t, err)
assert.Equal(t, tt.expectedClusterStatus.CrcStatus, actualClusterStatusResult.CrcStatus)
assert.Equal(t, tt.expectedClusterStatus.OpenshiftStatus, actualClusterStatusResult.OpenshiftStatus)
assert.Equal(t, tt.expectedClusterStatus.Preset, actualClusterStatusResult.Preset)
assert.Equal(t, tt.expectedClusterStatus.OpenshiftVersion, actualClusterStatusResult.OpenshiftVersion)
assert.Equal(t, tt.expectedClusterStatus.RAMSize, actualClusterStatusResult.RAMSize)
assert.Equal(t, tt.expectedClusterStatus.RAMUse, actualClusterStatusResult.RAMUse)
assert.Equal(t, tt.expectedClusterStatus.DiskSize, actualClusterStatusResult.DiskSize)
assert.Equal(t, tt.expectedClusterStatus.DiskUse, actualClusterStatusResult.DiskUse)
assert.Equal(t, tt.expectedClusterStatus.PersistentVolumeSize, actualClusterStatusResult.PersistentVolumeSize)
assert.Equal(t, tt.expectedClusterStatus.PersistentVolumeUse, actualClusterStatusResult.PersistentVolumeUse)
})
}
}

0 comments on commit 157b1a0

Please sign in to comment.