Files
gvm/tasks_test.go
2026-09-08 15:50:23 +02:00

114 lines
3.7 KiB
Go

package main
import (
"testing"
"time"
"github.com/vmware/govmomi/vim25/types"
)
// The operation is named from vSphere's descriptionId and not from the task's
// own Description, which vCenter localises: a German vCenter would otherwise
// put German words in an English table.
func TestTaskVerb(t *testing.T) {
for _, c := range []struct{ id, want string }{
{"VirtualMachine.createSnapshot", "snapshot"},
{"VirtualMachine.removeAllSnapshots", "rm snaps"},
{"VirtualMachine.relocate", "migrate"},
{"VirtualMachine.reconfigure", "reconfig"},
{"Datacenter.somethingNobodyHasHeardOf", "somethingNobodyHasHeardOf"},
{"noDotAtAll", "noDotAtAll"},
{"", "busy"},
} {
if got := taskVerb(c.id); got != c.want {
t.Errorf("taskVerb(%q) = %q, want %q", c.id, got, c.want)
}
}
}
// Every verb fits the column, or the cell it is put in would be truncated with
// the percentage — the part that says whether anything is happening — cut off.
func TestEveryTaskVerbFitsItsColumn(t *testing.T) {
room := taskColumn.width - len(" 100%")
for id, verb := range taskVerbs {
if len(verb) > room {
t.Errorf("%s is called %q, which is %d characters of the %d there are",
id, verb, len(verb), room)
}
}
}
// A task at nought per cent has not reported any progress, which is not the
// same as having made none: printing 0 % makes a task that has just started
// look stuck.
func TestTaskCell(t *testing.T) {
for _, c := range []struct {
task runningTask
want string
}{
{runningTask{what: "clone", progress: 40}, "clone 40%"},
{runningTask{what: "clone"}, "clone"},
{runningTask{what: "clone", queued: true}, "clone q"},
{runningTask{what: "clone", queued: true, progress: 10}, "clone q"},
} {
if got := c.task.cell(); got != c.want {
t.Errorf("cell() = %q, want %q", got, c.want)
}
}
}
func TestTaskLineSpellsQueuedOut(t *testing.T) {
since := time.Date(2026, 9, 8, 11, 42, 0, 0, time.Local)
got := runningTask{what: "consolid", queued: true, since: since}.line()
if got != "consolid · queued, not started · since 11:42:00" {
t.Errorf("the sheet line reads %q", got)
}
got = runningTask{what: "clone", progress: 40, since: since}.line()
if got != "clone · 40 % · since 11:42:00" {
t.Errorf("the sheet line reads %q", got)
}
}
// Only what is still going on is a task. A task that has finished stays on
// vCenter's recentTask list for minutes afterwards, and a table that showed it
// would report a snapshot being taken long after it was taken.
func TestOnlyRunningTasksCount(t *testing.T) {
for _, c := range []struct {
state types.TaskInfoState
want bool
}{
{types.TaskInfoStateRunning, true},
{types.TaskInfoStateQueued, true},
{types.TaskInfoStateSuccess, false},
{types.TaskInfoStateError, false},
} {
_, ok := taskOf(types.TaskInfo{State: c.state, DescriptionId: "VirtualMachine.clone"})
if ok != c.want {
t.Errorf("a %s task counts = %v, want %v", c.state, ok, c.want)
}
}
}
// A running task is timed from when it started, a queued one from when it was
// accepted — there is nothing else to time it from.
func TestTaskTakesItsTimeFromTheRightEnd(t *testing.T) {
queued := time.Date(2026, 9, 8, 11, 0, 0, 0, time.UTC)
started := time.Date(2026, 9, 8, 11, 5, 0, 0, time.UTC)
got, _ := taskOf(types.TaskInfo{
State: types.TaskInfoStateRunning, QueueTime: queued, StartTime: &started,
DescriptionId: "VirtualMachine.clone",
})
if !got.since.Equal(started) {
t.Errorf("a running task is timed from %v", got.since)
}
got, _ = taskOf(types.TaskInfo{
State: types.TaskInfoStateQueued, QueueTime: queued,
DescriptionId: "VirtualMachine.clone",
})
if !got.since.Equal(queued) {
t.Errorf("a queued task is timed from %v", got.since)
}
}