2023-04-10 15:36:59 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2021-10-01 13:59:55 +00:00
|
|
|
//go:build ent
|
2017-10-13 21:36:02 +00:00
|
|
|
|
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2022-03-17 13:34:57 +00:00
|
|
|
"github.com/hashicorp/nomad/api/internal/testutil"
|
2023-01-01 18:57:26 +00:00
|
|
|
"github.com/shoenig/test/must"
|
2017-10-13 21:36:02 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestQuotas_Register(t *testing.T) {
|
2022-03-17 13:34:57 +00:00
|
|
|
testutil.Parallel(t)
|
2023-01-01 18:57:26 +00:00
|
|
|
|
2017-10-13 21:36:02 +00:00
|
|
|
c, s := makeClient(t, nil, nil)
|
|
|
|
defer s.Stop()
|
|
|
|
quotas := c.Quotas()
|
|
|
|
|
|
|
|
// Create a quota spec and register it
|
|
|
|
qs := testQuotaSpec()
|
|
|
|
wm, err := quotas.Register(qs, nil)
|
2023-01-01 18:57:26 +00:00
|
|
|
must.NoError(t, err)
|
2017-10-13 21:36:02 +00:00
|
|
|
assertWriteMeta(t, wm)
|
|
|
|
|
|
|
|
// Query the specs back out again
|
|
|
|
resp, qm, err := quotas.List(nil)
|
2023-01-01 18:57:26 +00:00
|
|
|
must.NoError(t, err)
|
2017-10-13 21:36:02 +00:00
|
|
|
assertQueryMeta(t, qm)
|
2023-01-01 18:57:26 +00:00
|
|
|
must.Len(t, 1, resp)
|
|
|
|
must.Eq(t, qs.Name, resp[0].Name)
|
2017-10-13 21:36:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestQuotas_Register_Invalid(t *testing.T) {
|
2022-03-17 13:34:57 +00:00
|
|
|
testutil.Parallel(t)
|
2023-01-01 18:57:26 +00:00
|
|
|
|
2017-10-13 21:36:02 +00:00
|
|
|
c, s := makeClient(t, nil, nil)
|
|
|
|
defer s.Stop()
|
|
|
|
quotas := c.Quotas()
|
|
|
|
|
|
|
|
// Create an invalid namespace and register it
|
|
|
|
qs := testQuotaSpec()
|
|
|
|
qs.Name = "*"
|
|
|
|
_, err := quotas.Register(qs, nil)
|
2023-01-01 18:57:26 +00:00
|
|
|
must.Error(t, err)
|
2017-10-13 21:36:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestQuotas_Info(t *testing.T) {
|
2022-03-17 13:34:57 +00:00
|
|
|
testutil.Parallel(t)
|
2023-01-01 18:57:26 +00:00
|
|
|
|
2017-10-13 21:36:02 +00:00
|
|
|
c, s := makeClient(t, nil, nil)
|
|
|
|
defer s.Stop()
|
|
|
|
quotas := c.Quotas()
|
|
|
|
|
|
|
|
// Trying to retrieve a quota spec before it exists returns an error
|
|
|
|
_, _, err := quotas.Info("foo", nil)
|
2023-01-01 18:57:26 +00:00
|
|
|
must.ErrorContains(t, err, "not found")
|
2017-10-13 21:36:02 +00:00
|
|
|
|
|
|
|
// Register the quota
|
|
|
|
qs := testQuotaSpec()
|
|
|
|
wm, err := quotas.Register(qs, nil)
|
2023-01-01 18:57:26 +00:00
|
|
|
must.NoError(t, err)
|
2017-10-13 21:36:02 +00:00
|
|
|
assertWriteMeta(t, wm)
|
|
|
|
|
|
|
|
// Query the quota again and ensure it exists
|
|
|
|
result, qm, err := quotas.Info(qs.Name, nil)
|
2023-01-01 18:57:26 +00:00
|
|
|
must.NoError(t, err)
|
2017-10-13 21:36:02 +00:00
|
|
|
assertQueryMeta(t, qm)
|
2023-01-01 18:57:26 +00:00
|
|
|
must.NotNil(t, result)
|
|
|
|
must.Eq(t, qs.Name, result.Name)
|
2017-10-13 21:36:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestQuotas_Usage(t *testing.T) {
|
2022-03-17 13:34:57 +00:00
|
|
|
testutil.Parallel(t)
|
2023-01-01 18:57:26 +00:00
|
|
|
|
2017-10-13 21:36:02 +00:00
|
|
|
c, s := makeClient(t, nil, nil)
|
|
|
|
defer s.Stop()
|
|
|
|
quotas := c.Quotas()
|
|
|
|
|
|
|
|
// Trying to retrieve a quota spec before it exists returns an error
|
|
|
|
_, _, err := quotas.Usage("foo", nil)
|
2023-01-01 18:57:26 +00:00
|
|
|
must.ErrorContains(t, err, "not found")
|
2017-10-13 21:36:02 +00:00
|
|
|
|
|
|
|
// Register the quota
|
|
|
|
qs := testQuotaSpec()
|
|
|
|
wm, err := quotas.Register(qs, nil)
|
2023-01-01 18:57:26 +00:00
|
|
|
must.NoError(t, err)
|
2017-10-13 21:36:02 +00:00
|
|
|
assertWriteMeta(t, wm)
|
|
|
|
|
|
|
|
// Query the quota usage and ensure it exists
|
|
|
|
result, qm, err := quotas.Usage(qs.Name, nil)
|
2023-01-01 18:57:26 +00:00
|
|
|
must.NoError(t, err)
|
2017-10-13 21:36:02 +00:00
|
|
|
assertQueryMeta(t, qm)
|
2023-01-01 18:57:26 +00:00
|
|
|
must.NotNil(t, result)
|
|
|
|
must.Eq(t, qs.Name, result.Name)
|
2017-10-13 21:36:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestQuotas_Delete(t *testing.T) {
|
2022-03-17 13:34:57 +00:00
|
|
|
testutil.Parallel(t)
|
2023-01-01 18:57:26 +00:00
|
|
|
|
2017-10-13 21:36:02 +00:00
|
|
|
c, s := makeClient(t, nil, nil)
|
|
|
|
defer s.Stop()
|
|
|
|
quotas := c.Quotas()
|
|
|
|
|
|
|
|
// Create a quota and register it
|
|
|
|
qs := testQuotaSpec()
|
|
|
|
wm, err := quotas.Register(qs, nil)
|
2023-01-01 18:57:26 +00:00
|
|
|
must.NoError(t, err)
|
2017-10-13 21:36:02 +00:00
|
|
|
assertWriteMeta(t, wm)
|
|
|
|
|
|
|
|
// Query the quota back out again
|
|
|
|
resp, qm, err := quotas.List(nil)
|
2023-01-01 18:57:26 +00:00
|
|
|
must.NoError(t, err)
|
2017-10-13 21:36:02 +00:00
|
|
|
assertQueryMeta(t, qm)
|
2023-01-01 18:57:26 +00:00
|
|
|
must.Len(t, 1, resp)
|
|
|
|
must.Eq(t, qs.Name, resp[0].Name)
|
2017-10-13 21:36:02 +00:00
|
|
|
|
|
|
|
// Delete the quota
|
|
|
|
wm, err = quotas.Delete(qs.Name, nil)
|
2023-01-01 18:57:26 +00:00
|
|
|
must.NoError(t, err)
|
2017-10-13 21:36:02 +00:00
|
|
|
assertWriteMeta(t, wm)
|
|
|
|
|
|
|
|
// Query the quotas back out again
|
|
|
|
resp, qm, err = quotas.List(nil)
|
2023-01-01 18:57:26 +00:00
|
|
|
must.NoError(t, err)
|
2017-10-13 21:36:02 +00:00
|
|
|
assertQueryMeta(t, qm)
|
2023-01-01 18:57:26 +00:00
|
|
|
must.SliceEmpty(t, resp)
|
2017-10-13 21:36:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestQuotas_List(t *testing.T) {
|
2022-03-17 13:34:57 +00:00
|
|
|
testutil.Parallel(t)
|
2023-01-01 18:57:26 +00:00
|
|
|
|
2017-10-13 21:36:02 +00:00
|
|
|
c, s := makeClient(t, nil, nil)
|
|
|
|
defer s.Stop()
|
|
|
|
quotas := c.Quotas()
|
|
|
|
|
|
|
|
// Create two quotas and register them
|
|
|
|
qs1 := testQuotaSpec()
|
|
|
|
qs2 := testQuotaSpec()
|
|
|
|
qs1.Name = "fooaaa"
|
|
|
|
qs2.Name = "foobbb"
|
|
|
|
wm, err := quotas.Register(qs1, nil)
|
2023-01-01 18:57:26 +00:00
|
|
|
must.NoError(t, err)
|
2017-10-13 21:36:02 +00:00
|
|
|
assertWriteMeta(t, wm)
|
|
|
|
|
|
|
|
wm, err = quotas.Register(qs2, nil)
|
2023-01-01 18:57:26 +00:00
|
|
|
must.NoError(t, err)
|
2017-10-13 21:36:02 +00:00
|
|
|
assertWriteMeta(t, wm)
|
|
|
|
|
|
|
|
// Query the quotas
|
|
|
|
resp, qm, err := quotas.List(nil)
|
2023-01-01 18:57:26 +00:00
|
|
|
must.NoError(t, err)
|
2017-10-13 21:36:02 +00:00
|
|
|
assertQueryMeta(t, qm)
|
2023-01-01 18:57:26 +00:00
|
|
|
must.Len(t, 2, resp)
|
2017-10-13 21:36:02 +00:00
|
|
|
|
|
|
|
// Query the quotas using a prefix
|
|
|
|
resp, qm, err = quotas.PrefixList("foo", nil)
|
2023-01-01 18:57:26 +00:00
|
|
|
must.NoError(t, err)
|
2017-10-13 21:36:02 +00:00
|
|
|
assertQueryMeta(t, qm)
|
2023-01-01 18:57:26 +00:00
|
|
|
must.Len(t, 2, resp)
|
2017-10-13 21:36:02 +00:00
|
|
|
|
|
|
|
// Query the quotas using a prefix
|
|
|
|
resp, qm, err = quotas.PrefixList("foob", nil)
|
2023-01-01 18:57:26 +00:00
|
|
|
must.NoError(t, err)
|
2017-10-13 21:36:02 +00:00
|
|
|
assertQueryMeta(t, qm)
|
2023-01-01 18:57:26 +00:00
|
|
|
must.Len(t, 1, resp)
|
|
|
|
must.Eq(t, qs2.Name, resp[0].Name)
|
2017-10-13 21:36:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestQuotas_ListUsages(t *testing.T) {
|
2022-03-17 13:34:57 +00:00
|
|
|
testutil.Parallel(t)
|
2023-01-01 18:57:26 +00:00
|
|
|
|
2017-10-13 21:36:02 +00:00
|
|
|
c, s := makeClient(t, nil, nil)
|
|
|
|
defer s.Stop()
|
|
|
|
quotas := c.Quotas()
|
|
|
|
|
|
|
|
// Create two quotas and register them
|
|
|
|
qs1 := testQuotaSpec()
|
|
|
|
qs2 := testQuotaSpec()
|
|
|
|
qs1.Name = "fooaaa"
|
|
|
|
qs2.Name = "foobbb"
|
|
|
|
wm, err := quotas.Register(qs1, nil)
|
2023-01-01 18:57:26 +00:00
|
|
|
must.NoError(t, err)
|
2017-10-13 21:36:02 +00:00
|
|
|
assertWriteMeta(t, wm)
|
|
|
|
|
|
|
|
wm, err = quotas.Register(qs2, nil)
|
2023-01-01 18:57:26 +00:00
|
|
|
must.NoError(t, err)
|
2017-10-13 21:36:02 +00:00
|
|
|
assertWriteMeta(t, wm)
|
|
|
|
|
|
|
|
// Query the quotas
|
|
|
|
resp, qm, err := quotas.ListUsage(nil)
|
2023-01-01 18:57:26 +00:00
|
|
|
must.NoError(t, err)
|
2017-10-13 21:36:02 +00:00
|
|
|
assertQueryMeta(t, qm)
|
2023-01-01 18:57:26 +00:00
|
|
|
must.Len(t, 2, resp)
|
2017-10-13 21:36:02 +00:00
|
|
|
|
|
|
|
// Query the quotas using a prefix
|
|
|
|
resp, qm, err = quotas.PrefixListUsage("foo", nil)
|
2023-01-01 18:57:26 +00:00
|
|
|
must.NoError(t, err)
|
2017-10-13 21:36:02 +00:00
|
|
|
assertQueryMeta(t, qm)
|
2023-01-01 18:57:26 +00:00
|
|
|
must.Len(t, 2, resp)
|
2017-10-13 21:36:02 +00:00
|
|
|
|
|
|
|
// Query the quotas using a prefix
|
|
|
|
resp, qm, err = quotas.PrefixListUsage("foob", nil)
|
2023-01-01 18:57:26 +00:00
|
|
|
must.NoError(t, err)
|
2017-10-13 21:36:02 +00:00
|
|
|
assertQueryMeta(t, qm)
|
2023-01-01 18:57:26 +00:00
|
|
|
must.Len(t, 1, resp)
|
|
|
|
must.Eq(t, qs2.Name, resp[0].Name)
|
2017-10-13 21:36:02 +00:00
|
|
|
}
|