From 157b1a05e56db9c7ab596f76c4126103fa5f8014 Mon Sep 17 00:00:00 2001 From: Rohan Kumar Date: Fri, 22 Nov 2024 22:31:26 +0530 Subject: [PATCH] fix (machine) : crc daemon `/status` api should return correct preset 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 --- pkg/crc/machine/status.go | 54 ++++--- pkg/crc/machine/status_test.go | 252 +++++++++++++++++++++++++++++++++ 2 files changed, 283 insertions(+), 23 deletions(-) create mode 100644 pkg/crc/machine/status_test.go diff --git a/pkg/crc/machine/status.go b/pkg/crc/machine/status.go index 7dbad231a1..a9df2fb6c2 100644 --- a/pkg/crc/machine/status.go +++ b/pkg/crc/machine/status.go @@ -33,17 +33,35 @@ 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 } @@ -51,27 +69,17 @@ func (client *client) Status() (*types.ClusterStatusResult, error) { 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 } diff --git a/pkg/crc/machine/status_test.go b/pkg/crc/machine/status_test.go new file mode 100644 index 0000000000..8bf205cb09 --- /dev/null +++ b/pkg/crc/machine/status_test.go @@ -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) + }) + } +}