Skip to content

Commit

Permalink
changed nil health outcome and added nil unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
nikohobart committed Sep 10, 2024
1 parent 72f368d commit de539a0
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 9 deletions.
16 changes: 7 additions & 9 deletions pkg/status/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,7 @@ func SetError(s *common.Status, err error) {

// SetHealth
func SetHealth(s *common.Status, hState common.HealthState, err ...error) {
if s.Health.CurrentState != hState {
s.Health.PreviousState = s.Health.CurrentState
}
s.Health.PreviousState = s.Health.CurrentState
s.Health.CurrentState = hState
if len(err) > 0 {
SetError(s, err[0])
Expand Down Expand Up @@ -149,16 +147,16 @@ func GetFromStatuses(statuses map[string]*string) (status *common.Status) {
// HealthState requires custom parsing as it is a proto enum. Otherwise enum 0 (NOT_KNOWN) will be an empty string.
func parseHealth(hstate *common.Health) string {
if hstate == nil {
return ""
return fmt.Sprintf("currentState:%s", common.HealthState_name[0])
}

prevHealth, ok := common.HealthState_name[int32(hstate.GetPreviousState())]
if !ok {
prevHealth = ""
prevHealth = common.HealthState_name[0]
}
currHealth, ok := common.HealthState_name[int32(hstate.GetCurrentState())]
if !ok {
currHealth = ""
currHealth = common.HealthState_name[0]
}

return fmt.Sprintf("currentState:%s previousState:%s", currHealth, prevHealth)
Expand All @@ -167,16 +165,16 @@ func parseHealth(hstate *common.Health) string {
// ProvisionState requires custom parsing as it is a proto enum. Otherwise enum 0 (UNKNOWN) will be an empty string.
func parseProvisioning(pstate *common.ProvisionStatus) string {
if pstate == nil {
return ""
return fmt.Sprintf("currentState:%s", common.ProvisionState_name[0])
}

prevProv, ok := common.ProvisionState_name[int32(pstate.GetPreviousState())]
if !ok {
prevProv = ""
prevProv = common.ProvisionState_name[0]
}
currProv, ok := common.ProvisionState_name[int32(pstate.GetCurrentState())]
if !ok {
currProv = ""
currProv = common.ProvisionState_name[0]
}

return fmt.Sprintf("currentState:%s previousState:%s", currProv, prevProv)
Expand Down
28 changes: 28 additions & 0 deletions pkg/status/status_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,31 @@ func TestProvisionStatusConversion(t *testing.T) {
t.Errorf("CurrentState mismatch: got %v, want %v", convertedStatus.ProvisioningStatus.CurrentState, originalStatus.ProvisioningStatus.CurrentState)
}
}

func TestHealthAndProvisionStateNilConversion(t *testing.T) {
// Create a sample status with nil health state
originalStatus := &common.Status{
Health: nil,
ProvisioningStatus: nil,
LastError: &common.Error{},
Version: &common.Version{},
}

// Convert the status to a map using GetStatuses
statusMap := GetStatuses(originalStatus)

// Convert the map back to a status using GetFromStatuses
convertedStatus := GetFromStatuses(statusMap)

// Check that the health state in the converted status is NOT_KNOWN
expectedHealthState := common.HealthState_NOTKNOWN
if convertedStatus.Health == nil || convertedStatus.Health.CurrentState != expectedHealthState {
t.Errorf("HealthState mismatch: got %v, want %v", convertedStatus.Health.GetCurrentState(), expectedHealthState)
}

// Check that the provision state in the converted status is UNKNOWN
expectedProvisionState := common.ProvisionState_UNKNOWN
if convertedStatus.ProvisioningStatus == nil || convertedStatus.ProvisioningStatus.CurrentState != expectedProvisionState {
t.Errorf("ProvisionState mismatch: got %v, want %v", convertedStatus.ProvisioningStatus.GetCurrentState(), expectedProvisionState)
}
}

0 comments on commit de539a0

Please sign in to comment.