open-nomad/client/pluginmanager/csimanager/usage_tracker_test.go
2020-03-23 13:58:30 -04:00

63 lines
1.2 KiB
Go

package csimanager
import (
"testing"
"github.com/hashicorp/nomad/nomad/mock"
"github.com/hashicorp/nomad/nomad/structs"
"github.com/stretchr/testify/require"
)
func TestUsageTracker(t *testing.T) {
mockAllocs := []*structs.Allocation{
mock.Alloc(),
mock.Alloc(),
mock.Alloc(),
mock.Alloc(),
mock.Alloc(),
}
cases := []struct {
Name string
RegisterAllocs []*structs.Allocation
FreeAllocs []*structs.Allocation
ExpectedResult bool
}{
{
Name: "Register and deregister all allocs",
RegisterAllocs: mockAllocs,
FreeAllocs: mockAllocs,
ExpectedResult: true,
},
{
Name: "Register all and deregister partial allocs",
RegisterAllocs: mockAllocs,
FreeAllocs: mockAllocs[0:3],
ExpectedResult: false,
},
}
for _, tc := range cases {
t.Run(tc.Name, func(t *testing.T) {
tracker := newVolumeUsageTracker()
volume := &structs.CSIVolume{
ID: "foo",
}
for _, alloc := range tc.RegisterAllocs {
tracker.Claim(alloc, volume, &UsageOptions{})
}
result := false
for _, alloc := range tc.FreeAllocs {
result = tracker.Free(alloc, volume, &UsageOptions{})
}
require.Equal(t, tc.ExpectedResult, result, "Tracker State: %#v", tracker.state)
})
}
}