test: P1.6 — table-driven tests for classifyStatus, isAdoptable, isTerminalError (34 cases)
This commit is contained in:
@@ -3,6 +3,8 @@ package resources_core
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"terraform-provider-nubes/internal/core"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestIsStatusSuspended(t *testing.T) {
|
func TestIsStatusSuspended(t *testing.T) {
|
||||||
@@ -54,3 +56,144 @@ func TestDeleteBehaviorDefault(t *testing.T) {
|
|||||||
t.Errorf("expected nil error for default (state_only) destroy behavior, got: %v", err)
|
t.Errorf("expected nil error for default (state_only) destroy behavior, got: %v", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ── P1.6: Табличные тесты classifyStatus ────────────────────────────────────
|
||||||
|
|
||||||
|
func TestClassifyStatus(t *testing.T) {
|
||||||
|
cases := []struct {
|
||||||
|
name string
|
||||||
|
inst *core.InstanceStateResponse
|
||||||
|
expected InstanceState
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "NOT_CREATED",
|
||||||
|
inst: &core.InstanceStateResponse{ExplainedStatus: "not created"},
|
||||||
|
expected: StateNotCreated,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "CREATING",
|
||||||
|
inst: &core.InstanceStateResponse{
|
||||||
|
ExplainedStatus: "creating",
|
||||||
|
OperationIsInProgress: true,
|
||||||
|
},
|
||||||
|
expected: StateCreating,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "RUNNING",
|
||||||
|
inst: &core.InstanceStateResponse{
|
||||||
|
ExplainedStatus: "running",
|
||||||
|
},
|
||||||
|
expected: StateRunning,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "RUNNING_PENDING",
|
||||||
|
inst: &core.InstanceStateResponse{
|
||||||
|
ExplainedStatus: "running",
|
||||||
|
OperationIsPending: true,
|
||||||
|
},
|
||||||
|
expected: StateRunningPending,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "MODIFYING",
|
||||||
|
inst: &core.InstanceStateResponse{
|
||||||
|
ExplainedStatus: "modifying",
|
||||||
|
OperationIsInProgress: true,
|
||||||
|
},
|
||||||
|
expected: StateModifying,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "SUSPENDED",
|
||||||
|
inst: &core.InstanceStateResponse{
|
||||||
|
ExplainedStatus: "suspended",
|
||||||
|
},
|
||||||
|
expected: StateSuspended,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "SUSPENDING",
|
||||||
|
inst: &core.InstanceStateResponse{
|
||||||
|
ExplainedStatus: "suspending",
|
||||||
|
OperationIsInProgress: true,
|
||||||
|
},
|
||||||
|
expected: StateSuspending,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "DELETED",
|
||||||
|
inst: &core.InstanceStateResponse{
|
||||||
|
ExplainedStatus: "deleted",
|
||||||
|
IsDeleted: true,
|
||||||
|
},
|
||||||
|
expected: StateDeleted,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "DELETING",
|
||||||
|
inst: &core.InstanceStateResponse{
|
||||||
|
ExplainedStatus: "deleting",
|
||||||
|
OperationIsInProgress: true,
|
||||||
|
},
|
||||||
|
expected: StateDeleting,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "CREATION_FAILED",
|
||||||
|
inst: &core.InstanceStateResponse{
|
||||||
|
ExplainedStatus: "creation failed",
|
||||||
|
},
|
||||||
|
expected: StateCreationFailed,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "UNKNOWN (empty)",
|
||||||
|
inst: &core.InstanceStateResponse{
|
||||||
|
ExplainedStatus: "",
|
||||||
|
},
|
||||||
|
expected: StateUnknown,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, c := range cases {
|
||||||
|
t.Run(c.name, func(t *testing.T) {
|
||||||
|
result := classifyStatus(c.inst)
|
||||||
|
if result != c.expected {
|
||||||
|
t.Errorf("classifyStatus(%q): expected %d, got %d",
|
||||||
|
c.inst.ExplainedStatus, c.expected, result)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestIsAdoptable(t *testing.T) {
|
||||||
|
adoptable := []InstanceState{StateRunning, StateRunningPending, StateSuspended}
|
||||||
|
notAdoptable := []InstanceState{StateNotCreated, StateCreating, StateCreationFailed,
|
||||||
|
StateModifying, StateModificationFailed, StateSuspending, StateSuspendFailed,
|
||||||
|
StateResuming, StateResumeFailed, StateDeleting, StateDeletionFailed,
|
||||||
|
StateDeleted, StateOrphaned, StateUnknown}
|
||||||
|
|
||||||
|
for _, s := range adoptable {
|
||||||
|
if !isAdoptable(s) {
|
||||||
|
t.Errorf("state %d should be adoptable", s)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for _, s := range notAdoptable {
|
||||||
|
if isAdoptable(s) {
|
||||||
|
t.Errorf("state %d should NOT be adoptable", s)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestIsTerminalError(t *testing.T) {
|
||||||
|
errors := []InstanceState{StateCreationFailed, StateModificationFailed,
|
||||||
|
StateSuspendFailed, StateResumeFailed, StateDeletionFailed,
|
||||||
|
StateOrphaned, StateDeleted}
|
||||||
|
nonErrors := []InstanceState{StateNotCreated, StateCreating, StateRunning,
|
||||||
|
StateRunningPending, StateModifying, StateSuspending, StateSuspended,
|
||||||
|
StateResuming, StateDeleting, StateUnknown}
|
||||||
|
|
||||||
|
for _, s := range errors {
|
||||||
|
if !isTerminalError(s) {
|
||||||
|
t.Errorf("state %d should be terminal error", s)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for _, s := range nonErrors {
|
||||||
|
if isTerminalError(s) {
|
||||||
|
t.Errorf("state %d should NOT be terminal error", s)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user