2023-04-10 15:36:59 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2017-09-07 23:56:15 +00:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2022-03-17 13:34:57 +00:00
|
|
|
"github.com/hashicorp/nomad/api/internal/testutil"
|
2022-12-16 18:25:28 +00:00
|
|
|
"github.com/shoenig/test/must"
|
2017-09-07 23:56:15 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestNamespaces_Register(t *testing.T) {
|
2022-03-17 13:34:57 +00:00
|
|
|
testutil.Parallel(t)
|
2022-12-16 18:25:28 +00:00
|
|
|
|
2017-09-07 23:56:15 +00:00
|
|
|
c, s := makeClient(t, nil, nil)
|
|
|
|
defer s.Stop()
|
|
|
|
namespaces := c.Namespaces()
|
|
|
|
|
|
|
|
// Create a namespace and register it
|
|
|
|
ns := testNamespace()
|
|
|
|
wm, err := namespaces.Register(ns, nil)
|
2022-12-16 18:25:28 +00:00
|
|
|
must.NoError(t, err)
|
2017-09-07 23:56:15 +00:00
|
|
|
assertWriteMeta(t, wm)
|
|
|
|
|
|
|
|
// Query the jobs back out again
|
|
|
|
resp, qm, err := namespaces.List(nil)
|
2022-12-16 18:25:28 +00:00
|
|
|
must.NoError(t, err)
|
2017-09-07 23:56:15 +00:00
|
|
|
assertQueryMeta(t, qm)
|
2022-12-16 18:25:28 +00:00
|
|
|
must.Len(t, 2, resp)
|
|
|
|
must.Eq(t, ns.Name, resp[0].Name)
|
|
|
|
must.Eq(t, "default", resp[1].Name)
|
2017-09-07 23:56:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestNamespaces_Register_Invalid(t *testing.T) {
|
2022-03-17 13:34:57 +00:00
|
|
|
testutil.Parallel(t)
|
2022-12-16 18:25:28 +00:00
|
|
|
|
2017-09-07 23:56:15 +00:00
|
|
|
c, s := makeClient(t, nil, nil)
|
|
|
|
defer s.Stop()
|
|
|
|
namespaces := c.Namespaces()
|
|
|
|
|
|
|
|
// Create an invalid namespace and register it
|
|
|
|
ns := testNamespace()
|
|
|
|
ns.Name = "*"
|
|
|
|
_, err := namespaces.Register(ns, nil)
|
2022-12-16 18:25:28 +00:00
|
|
|
must.ErrorContains(t, err, `invalid name "*".`)
|
2017-09-07 23:56:15 +00:00
|
|
|
}
|
|
|
|
|
2022-12-16 18:25:28 +00:00
|
|
|
func TestNamespaces_Info(t *testing.T) {
|
2022-03-17 13:34:57 +00:00
|
|
|
testutil.Parallel(t)
|
2022-12-16 18:25:28 +00:00
|
|
|
|
2017-09-07 23:56:15 +00:00
|
|
|
c, s := makeClient(t, nil, nil)
|
|
|
|
defer s.Stop()
|
|
|
|
namespaces := c.Namespaces()
|
|
|
|
|
|
|
|
// Trying to retrieve a namespace before it exists returns an error
|
|
|
|
_, _, err := namespaces.Info("foo", nil)
|
2022-12-16 18:25:28 +00:00
|
|
|
must.NotNil(t, err)
|
|
|
|
must.ErrorContains(t, err, "not found")
|
2017-09-07 23:56:15 +00:00
|
|
|
|
|
|
|
// Register the namespace
|
|
|
|
ns := testNamespace()
|
|
|
|
wm, err := namespaces.Register(ns, nil)
|
2022-12-16 18:25:28 +00:00
|
|
|
must.NoError(t, err)
|
2017-09-07 23:56:15 +00:00
|
|
|
assertWriteMeta(t, wm)
|
|
|
|
|
|
|
|
// Query the namespace again and ensure it exists
|
|
|
|
result, qm, err := namespaces.Info(ns.Name, nil)
|
2022-12-16 18:25:28 +00:00
|
|
|
must.NoError(t, err)
|
2017-09-07 23:56:15 +00:00
|
|
|
assertQueryMeta(t, qm)
|
2022-12-16 18:25:28 +00:00
|
|
|
must.NotNil(t, result)
|
|
|
|
must.Eq(t, ns.Name, result.Name)
|
2017-09-07 23:56:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestNamespaces_Delete(t *testing.T) {
|
2022-03-17 13:34:57 +00:00
|
|
|
testutil.Parallel(t)
|
2022-12-16 18:25:28 +00:00
|
|
|
|
2017-09-07 23:56:15 +00:00
|
|
|
c, s := makeClient(t, nil, nil)
|
|
|
|
defer s.Stop()
|
|
|
|
namespaces := c.Namespaces()
|
|
|
|
|
|
|
|
// Create a namespace and register it
|
|
|
|
ns := testNamespace()
|
|
|
|
wm, err := namespaces.Register(ns, nil)
|
2022-12-16 18:25:28 +00:00
|
|
|
must.NoError(t, err)
|
2017-09-07 23:56:15 +00:00
|
|
|
assertWriteMeta(t, wm)
|
|
|
|
|
|
|
|
// Query the namespace back out again
|
|
|
|
resp, qm, err := namespaces.List(nil)
|
2022-12-16 18:25:28 +00:00
|
|
|
must.NoError(t, err)
|
2017-09-07 23:56:15 +00:00
|
|
|
assertQueryMeta(t, qm)
|
2022-12-16 18:25:28 +00:00
|
|
|
must.Len(t, 2, resp)
|
|
|
|
must.Eq(t, ns.Name, resp[0].Name)
|
|
|
|
must.Eq(t, "default", resp[1].Name)
|
2017-09-07 23:56:15 +00:00
|
|
|
|
|
|
|
// Delete the namespace
|
|
|
|
wm, err = namespaces.Delete(ns.Name, nil)
|
2022-12-16 18:25:28 +00:00
|
|
|
must.NoError(t, err)
|
2017-09-07 23:56:15 +00:00
|
|
|
assertWriteMeta(t, wm)
|
|
|
|
|
|
|
|
// Query the namespaces back out again
|
|
|
|
resp, qm, err = namespaces.List(nil)
|
2022-12-16 18:25:28 +00:00
|
|
|
must.NoError(t, err)
|
2017-09-07 23:56:15 +00:00
|
|
|
assertQueryMeta(t, qm)
|
2022-12-16 18:25:28 +00:00
|
|
|
must.Len(t, 1, resp)
|
|
|
|
must.Eq(t, "default", resp[0].Name)
|
2017-09-07 23:56:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestNamespaces_List(t *testing.T) {
|
2022-03-17 13:34:57 +00:00
|
|
|
testutil.Parallel(t)
|
2022-12-16 18:25:28 +00:00
|
|
|
|
2017-09-07 23:56:15 +00:00
|
|
|
c, s := makeClient(t, nil, nil)
|
|
|
|
defer s.Stop()
|
|
|
|
namespaces := c.Namespaces()
|
|
|
|
|
|
|
|
// Create two namespaces and register them
|
|
|
|
ns1 := testNamespace()
|
|
|
|
ns2 := testNamespace()
|
|
|
|
ns1.Name = "fooaaa"
|
|
|
|
ns2.Name = "foobbb"
|
|
|
|
wm, err := namespaces.Register(ns1, nil)
|
2022-12-16 18:25:28 +00:00
|
|
|
must.NoError(t, err)
|
2017-09-07 23:56:15 +00:00
|
|
|
assertWriteMeta(t, wm)
|
|
|
|
|
|
|
|
wm, err = namespaces.Register(ns2, nil)
|
2022-12-16 18:25:28 +00:00
|
|
|
must.NoError(t, err)
|
2017-09-07 23:56:15 +00:00
|
|
|
assertWriteMeta(t, wm)
|
|
|
|
|
|
|
|
// Query the namespaces
|
|
|
|
resp, qm, err := namespaces.List(nil)
|
2022-12-16 18:25:28 +00:00
|
|
|
must.NoError(t, err)
|
2017-09-07 23:56:15 +00:00
|
|
|
assertQueryMeta(t, qm)
|
2022-12-16 18:25:28 +00:00
|
|
|
must.Len(t, 3, resp)
|
2017-09-07 23:56:15 +00:00
|
|
|
|
|
|
|
// Query the namespaces using a prefix
|
|
|
|
resp, qm, err = namespaces.PrefixList("foo", nil)
|
2022-12-16 18:25:28 +00:00
|
|
|
must.NoError(t, err)
|
2017-09-07 23:56:15 +00:00
|
|
|
assertQueryMeta(t, qm)
|
2022-12-16 18:25:28 +00:00
|
|
|
must.Len(t, 2, resp)
|
2017-09-07 23:56:15 +00:00
|
|
|
|
|
|
|
// Query the namespaces using a prefix
|
|
|
|
resp, qm, err = namespaces.PrefixList("foob", nil)
|
2022-12-16 18:25:28 +00:00
|
|
|
must.NoError(t, err)
|
2017-09-07 23:56:15 +00:00
|
|
|
assertQueryMeta(t, qm)
|
2022-12-16 18:25:28 +00:00
|
|
|
must.Len(t, 1, resp)
|
|
|
|
must.Eq(t, ns2.Name, resp[0].Name)
|
2017-09-07 23:56:15 +00:00
|
|
|
}
|