2022-05-09 12:45:10 -07:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
2022-04-27 08:13:46 -07:00
|
|
|
// SPDX-License-Identifier: BUSL-1.1
|
2023-02-02 17:15:17 -08:00
|
|
|
|
2020-08-27 20:43:33 -04:00
|
|
|
package hostcatalogs_test
|
2020-06-05 13:02:58 -07:00
|
|
|
|
2020-08-11 11:20:06 -04:00
|
|
|
import (
|
2020-08-19 21:49:52 -07:00
|
|
|
"fmt"
|
2020-08-11 11:20:06 -04:00
|
|
|
"net/http"
|
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
|
|
"github.com/hashicorp/boundary/api"
|
2020-08-27 20:43:33 -04:00
|
|
|
"github.com/hashicorp/boundary/api/hostcatalogs"
|
2023-03-10 15:06:39 -05:00
|
|
|
"github.com/hashicorp/boundary/globals"
|
2022-05-09 12:45:10 -07:00
|
|
|
"github.com/hashicorp/boundary/internal/daemon/controller"
|
2020-08-17 15:38:41 -04:00
|
|
|
"github.com/hashicorp/boundary/internal/iam"
|
2020-08-11 11:20:06 -04:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
)
|
|
|
|
|
|
2020-09-08 13:27:51 -07:00
|
|
|
func TestList(t *testing.T) {
|
2020-08-19 21:49:52 -07:00
|
|
|
assert, require := assert.New(t), require.New(t)
|
2020-09-08 13:27:51 -07:00
|
|
|
tc := controller.NewTestController(t, nil)
|
2020-08-19 21:49:52 -07:00
|
|
|
defer tc.Shutdown()
|
|
|
|
|
|
|
|
|
|
client := tc.Client()
|
2020-09-08 13:27:51 -07:00
|
|
|
token := tc.Token()
|
|
|
|
|
client.SetToken(token.Token)
|
|
|
|
|
_, proj := iam.TestScopes(t, tc.IamRepo(), iam.WithUserId(token.UserId))
|
2020-08-27 20:43:33 -04:00
|
|
|
catalogClient := hostcatalogs.NewClient(client)
|
2020-08-19 21:49:52 -07:00
|
|
|
|
2020-09-24 14:34:58 -07:00
|
|
|
ul, err := catalogClient.List(tc.Context(), proj.GetPublicId())
|
2020-09-15 01:08:21 -04:00
|
|
|
require.NoError(err)
|
|
|
|
|
assert.Empty(ul.Items)
|
2020-08-19 21:49:52 -07:00
|
|
|
|
2020-08-27 20:43:33 -04:00
|
|
|
var expected []*hostcatalogs.HostCatalog
|
2020-08-19 21:49:52 -07:00
|
|
|
for i := 0; i < 10; i++ {
|
2020-08-27 20:43:33 -04:00
|
|
|
expected = append(expected, &hostcatalogs.HostCatalog{Name: fmt.Sprint(i)})
|
2020-08-19 21:49:52 -07:00
|
|
|
}
|
|
|
|
|
|
2020-09-24 14:34:58 -07:00
|
|
|
cr, err := catalogClient.Create(tc.Context(), "static", proj.GetPublicId(), hostcatalogs.WithName(expected[0].Name))
|
2020-09-15 01:08:21 -04:00
|
|
|
require.NoError(err)
|
|
|
|
|
expected[0] = cr.Item
|
2020-08-19 21:49:52 -07:00
|
|
|
|
2020-09-24 14:34:58 -07:00
|
|
|
ul, err = catalogClient.List(tc.Context(), proj.GetPublicId())
|
2020-09-15 01:08:21 -04:00
|
|
|
require.NoError(err)
|
|
|
|
|
assert.ElementsMatch(comparableCatalogSlice(expected[:1]), comparableCatalogSlice(ul.Items))
|
2020-08-19 21:49:52 -07:00
|
|
|
|
|
|
|
|
for i := 1; i < 10; i++ {
|
2020-09-24 14:34:58 -07:00
|
|
|
cr, err = catalogClient.Create(tc.Context(), "static", proj.GetPublicId(), hostcatalogs.WithName(expected[i].Name))
|
2020-09-15 01:08:21 -04:00
|
|
|
require.NoError(err)
|
|
|
|
|
expected[i] = cr.Item
|
2020-08-19 21:49:52 -07:00
|
|
|
}
|
2020-09-24 14:34:58 -07:00
|
|
|
ul, err = catalogClient.List(tc.Context(), proj.GetPublicId())
|
2020-08-19 21:49:52 -07:00
|
|
|
require.NoError(err)
|
2020-09-15 01:08:21 -04:00
|
|
|
assert.ElementsMatch(comparableCatalogSlice(expected), comparableCatalogSlice(ul.Items))
|
2021-02-22 14:37:14 -07:00
|
|
|
|
|
|
|
|
filterItem := ul.Items[3]
|
|
|
|
|
ul, err = catalogClient.List(tc.Context(), proj.GetPublicId(),
|
2021-11-02 14:06:20 -07:00
|
|
|
hostcatalogs.WithFilter(fmt.Sprintf(`"/item/id"==%q`, filterItem.Id)))
|
|
|
|
|
require.NoError(err)
|
|
|
|
|
assert.Len(ul.Items, 1)
|
|
|
|
|
assert.Equal(filterItem.Id, ul.Items[0].Id)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestList_Plugin(t *testing.T) {
|
|
|
|
|
assert, require := assert.New(t), require.New(t)
|
|
|
|
|
tc := controller.NewTestController(t, nil)
|
|
|
|
|
defer tc.Shutdown()
|
|
|
|
|
|
|
|
|
|
client := tc.Client()
|
|
|
|
|
token := tc.Token()
|
|
|
|
|
client.SetToken(token.Token)
|
|
|
|
|
_, proj := iam.TestScopes(t, tc.IamRepo(), iam.WithUserId(token.UserId))
|
|
|
|
|
catalogClient := hostcatalogs.NewClient(client)
|
|
|
|
|
|
|
|
|
|
ul, err := catalogClient.List(tc.Context(), proj.GetPublicId())
|
|
|
|
|
require.NoError(err)
|
|
|
|
|
assert.Empty(ul.Items)
|
|
|
|
|
|
|
|
|
|
var expected []*hostcatalogs.HostCatalog
|
|
|
|
|
for i := 0; i < 10; i++ {
|
|
|
|
|
expected = append(expected, &hostcatalogs.HostCatalog{Name: fmt.Sprint(i)})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cr, err := catalogClient.Create(tc.Context(), "plugin", proj.GetPublicId(),
|
|
|
|
|
hostcatalogs.WithName(expected[0].Name), hostcatalogs.WithPluginId("pl_1234567890"))
|
|
|
|
|
require.NoError(err)
|
|
|
|
|
expected[0] = cr.Item
|
|
|
|
|
|
|
|
|
|
ul, err = catalogClient.List(tc.Context(), proj.GetPublicId())
|
|
|
|
|
require.NoError(err)
|
|
|
|
|
assert.ElementsMatch(comparableCatalogSlice(expected[:1]), comparableCatalogSlice(ul.Items))
|
|
|
|
|
|
|
|
|
|
for i := 1; i < 10; i++ {
|
|
|
|
|
cr, err = catalogClient.Create(tc.Context(), "static", proj.GetPublicId(), hostcatalogs.WithName(expected[i].Name))
|
|
|
|
|
require.NoError(err)
|
|
|
|
|
expected[i] = cr.Item
|
|
|
|
|
}
|
|
|
|
|
ul, err = catalogClient.List(tc.Context(), proj.GetPublicId())
|
|
|
|
|
require.NoError(err)
|
|
|
|
|
assert.ElementsMatch(comparableCatalogSlice(expected), comparableCatalogSlice(ul.Items))
|
|
|
|
|
|
|
|
|
|
filterItem := ul.Items[3]
|
|
|
|
|
ul, err = catalogClient.List(tc.Context(), proj.GetPublicId(),
|
2021-02-22 14:37:14 -07:00
|
|
|
hostcatalogs.WithFilter(fmt.Sprintf(`"/item/id"==%q`, filterItem.Id)))
|
|
|
|
|
require.NoError(err)
|
|
|
|
|
assert.Len(ul.Items, 1)
|
|
|
|
|
assert.Equal(filterItem.Id, ul.Items[0].Id)
|
2020-08-19 21:49:52 -07:00
|
|
|
}
|
|
|
|
|
|
2020-08-27 20:43:33 -04:00
|
|
|
func comparableCatalogSlice(in []*hostcatalogs.HostCatalog) []hostcatalogs.HostCatalog {
|
|
|
|
|
var filtered []hostcatalogs.HostCatalog
|
2020-08-19 21:49:52 -07:00
|
|
|
for _, i := range in {
|
2020-08-27 20:43:33 -04:00
|
|
|
p := hostcatalogs.HostCatalog{
|
2020-08-19 21:49:52 -07:00
|
|
|
Id: i.Id,
|
|
|
|
|
Name: i.Name,
|
|
|
|
|
Description: i.Description,
|
|
|
|
|
CreatedTime: i.CreatedTime,
|
|
|
|
|
UpdatedTime: i.UpdatedTime,
|
|
|
|
|
}
|
|
|
|
|
filtered = append(filtered, p)
|
|
|
|
|
}
|
|
|
|
|
return filtered
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-08 13:27:51 -07:00
|
|
|
func TestCrud(t *testing.T) {
|
2020-08-11 11:20:06 -04:00
|
|
|
assert, require := assert.New(t), require.New(t)
|
2020-09-08 13:27:51 -07:00
|
|
|
tc := controller.NewTestController(t, nil)
|
2020-06-05 13:02:58 -07:00
|
|
|
defer tc.Shutdown()
|
|
|
|
|
|
|
|
|
|
client := tc.Client()
|
2020-09-08 13:27:51 -07:00
|
|
|
token := tc.Token()
|
|
|
|
|
client.SetToken(token.Token)
|
|
|
|
|
_, proj := iam.TestScopes(t, tc.IamRepo(), iam.WithUserId(token.UserId))
|
2020-08-11 11:20:06 -04:00
|
|
|
|
2020-09-24 14:34:58 -07:00
|
|
|
checkCatalog := func(step string, hc *hostcatalogs.HostCatalog, err error, wantedName string, wantVersion uint32) {
|
2020-08-11 11:20:06 -04:00
|
|
|
require.NoError(err, step)
|
2020-06-15 15:22:41 -07:00
|
|
|
assert.NotNil(hc, "returned no resource", step)
|
2020-06-05 13:02:58 -07:00
|
|
|
gotName := ""
|
2020-08-11 11:20:06 -04:00
|
|
|
if hc.Name != "" {
|
|
|
|
|
gotName = hc.Name
|
2020-06-05 13:02:58 -07:00
|
|
|
}
|
|
|
|
|
assert.Equal(wantedName, gotName, step)
|
2020-08-11 11:20:06 -04:00
|
|
|
assert.Equal(wantVersion, hc.Version)
|
2020-06-05 13:02:58 -07:00
|
|
|
}
|
|
|
|
|
|
2020-09-08 13:27:51 -07:00
|
|
|
hcClient := hostcatalogs.NewClient(client)
|
2020-06-05 13:02:58 -07:00
|
|
|
|
2020-09-24 14:34:58 -07:00
|
|
|
hc, err := hcClient.Create(tc.Context(), "static", proj.GetPublicId(), hostcatalogs.WithName("foo"))
|
2024-01-23 14:33:09 -05:00
|
|
|
require.NoError(err)
|
2020-09-24 14:34:58 -07:00
|
|
|
checkCatalog("create", hc.Item, err, "foo", 1)
|
2020-06-05 13:02:58 -07:00
|
|
|
|
2020-09-24 14:34:58 -07:00
|
|
|
hc, err = hcClient.Read(tc.Context(), hc.Item.Id)
|
2024-01-23 14:33:09 -05:00
|
|
|
require.NoError(err)
|
2020-09-24 14:34:58 -07:00
|
|
|
checkCatalog("read", hc.Item, err, "foo", 1)
|
2020-06-05 13:02:58 -07:00
|
|
|
|
2021-11-12 09:46:40 -07:00
|
|
|
hc, err = hcClient.Update(tc.Context(), hc.Item.Id, hc.Item.Version, hostcatalogs.WithName("foo"))
|
2024-01-23 14:33:09 -05:00
|
|
|
require.NoError(err)
|
2021-11-12 09:46:40 -07:00
|
|
|
checkCatalog("update", hc.Item, err, "foo", 1)
|
|
|
|
|
|
2020-09-24 14:34:58 -07:00
|
|
|
hc, err = hcClient.Update(tc.Context(), hc.Item.Id, hc.Item.Version, hostcatalogs.WithName("bar"))
|
2024-01-23 14:33:09 -05:00
|
|
|
require.NoError(err)
|
2020-09-24 14:34:58 -07:00
|
|
|
checkCatalog("update", hc.Item, err, "bar", 2)
|
2020-06-05 13:02:58 -07:00
|
|
|
|
2020-09-24 14:34:58 -07:00
|
|
|
hc, err = hcClient.Update(tc.Context(), hc.Item.Id, hc.Item.Version, hostcatalogs.DefaultName())
|
2024-01-23 14:33:09 -05:00
|
|
|
require.NoError(err)
|
2020-09-24 14:34:58 -07:00
|
|
|
checkCatalog("update", hc.Item, err, "", 3)
|
2020-06-05 13:02:58 -07:00
|
|
|
|
2020-09-24 14:34:58 -07:00
|
|
|
_, err = hcClient.Delete(tc.Context(), hc.Item.Id)
|
2020-08-11 11:20:06 -04:00
|
|
|
assert.NoError(err)
|
|
|
|
|
|
2020-09-24 14:34:58 -07:00
|
|
|
_, err = hcClient.Delete(tc.Context(), hc.Item.Id)
|
|
|
|
|
require.Error(err)
|
|
|
|
|
apiErr := api.AsServerError(err)
|
2020-09-15 01:08:21 -04:00
|
|
|
assert.NotNil(apiErr)
|
2021-02-25 12:43:17 -05:00
|
|
|
assert.EqualValues(http.StatusNotFound, apiErr.Response().StatusCode())
|
2021-10-25 09:37:05 -07:00
|
|
|
|
|
|
|
|
// Plugin catalogs
|
|
|
|
|
c, err := hcClient.Create(tc.Context(), "plugin", proj.GetPublicId(), hostcatalogs.WithName("pluginfoo"), hostcatalogs.WithPluginId("pl_1234567890"),
|
2022-10-25 18:29:38 -05:00
|
|
|
hostcatalogs.WithAttributes(map[string]any{"foo": "bar"}))
|
2021-10-25 09:37:05 -07:00
|
|
|
require.NoError(err)
|
|
|
|
|
|
2021-11-09 14:42:48 -07:00
|
|
|
c, err = hcClient.Update(tc.Context(), c.Item.Id, c.Item.Version, hostcatalogs.WithName("bar"),
|
2022-10-25 18:29:38 -05:00
|
|
|
hostcatalogs.WithAttributes(map[string]any{"key": "val", "foo": nil}),
|
|
|
|
|
hostcatalogs.WithSecrets(map[string]any{"secretkey": "secretval"}))
|
2021-11-09 14:42:48 -07:00
|
|
|
checkCatalog("update", c.Item, err, "bar", 2)
|
|
|
|
|
assert.Contains(c.Item.Attributes, "key")
|
|
|
|
|
assert.NotContains(c.Item.Attributes, "foo")
|
|
|
|
|
assert.Empty(c.Item.Secrets)
|
|
|
|
|
|
|
|
|
|
c, err = hcClient.Update(tc.Context(), c.Item.Id, c.Item.Version, hostcatalogs.DefaultName())
|
|
|
|
|
checkCatalog("update", c.Item, err, "", 3)
|
|
|
|
|
|
2021-11-12 09:46:40 -07:00
|
|
|
c, err = hcClient.Update(tc.Context(), c.Item.Id, 0, hostcatalogs.WithAutomaticVersioning(true),
|
2022-10-25 18:29:38 -05:00
|
|
|
hostcatalogs.WithSecrets(map[string]any{
|
2021-11-12 09:46:40 -07:00
|
|
|
"key1": "val1",
|
|
|
|
|
"key2": "val2",
|
|
|
|
|
}))
|
|
|
|
|
checkCatalog("update", c.Item, err, "", 4)
|
|
|
|
|
|
2021-10-25 09:37:05 -07:00
|
|
|
c, err = hcClient.Read(tc.Context(), c.Item.Id)
|
|
|
|
|
assert.NoError(err)
|
|
|
|
|
|
|
|
|
|
_, err = hcClient.Delete(tc.Context(), c.Item.Id)
|
|
|
|
|
assert.NoError(err)
|
|
|
|
|
|
|
|
|
|
_, err = hcClient.Delete(tc.Context(), c.Item.Id)
|
|
|
|
|
require.Error(err)
|
|
|
|
|
apiErr = api.AsServerError(err)
|
|
|
|
|
assert.NotNil(apiErr)
|
|
|
|
|
assert.EqualValues(http.StatusNotFound, apiErr.Response().StatusCode())
|
2020-06-05 13:02:58 -07:00
|
|
|
}
|
|
|
|
|
|
2020-09-08 13:27:51 -07:00
|
|
|
func TestErrors(t *testing.T) {
|
2020-08-11 11:20:06 -04:00
|
|
|
assert, require := assert.New(t), require.New(t)
|
2020-09-08 13:27:51 -07:00
|
|
|
tc := controller.NewTestController(t, nil)
|
2020-06-05 13:02:58 -07:00
|
|
|
defer tc.Shutdown()
|
|
|
|
|
|
|
|
|
|
client := tc.Client()
|
2020-09-08 13:27:51 -07:00
|
|
|
token := tc.Token()
|
|
|
|
|
client.SetToken(token.Token)
|
|
|
|
|
_, proj := iam.TestScopes(t, tc.IamRepo(), iam.WithUserId(token.UserId))
|
2020-08-27 20:43:33 -04:00
|
|
|
pc := hostcatalogs.NewClient(client)
|
2020-08-11 11:20:06 -04:00
|
|
|
|
2020-09-24 14:34:58 -07:00
|
|
|
hc, err := pc.Create(tc.Context(), "static", proj.GetPublicId(), hostcatalogs.WithName("foo"))
|
2020-08-11 11:20:06 -04:00
|
|
|
require.NoError(err)
|
2020-06-05 13:02:58 -07:00
|
|
|
assert.NotNil(hc)
|
|
|
|
|
|
2022-04-27 08:13:46 -07:00
|
|
|
// A malformed id is processed as the id and not a different path to the api.
|
|
|
|
|
_, err = pc.Read(tc.Context(), fmt.Sprintf("%s/../", hc.Item.Id))
|
|
|
|
|
require.Error(err)
|
|
|
|
|
apiErr := api.AsServerError(err)
|
|
|
|
|
require.NotNil(apiErr)
|
|
|
|
|
assert.EqualValues(http.StatusBadRequest, apiErr.Response().StatusCode())
|
|
|
|
|
require.Len(apiErr.Details.RequestFields, 1)
|
|
|
|
|
assert.Equal(apiErr.Details.RequestFields[0].Name, "id")
|
|
|
|
|
|
2020-09-21 12:09:30 -07:00
|
|
|
// Updating the wrong version should fail.
|
2020-09-24 14:34:58 -07:00
|
|
|
_, err = pc.Update(tc.Context(), hc.Item.Id, 73, hostcatalogs.WithName("anything"))
|
|
|
|
|
require.Error(err)
|
|
|
|
|
apiErr = api.AsServerError(err)
|
2020-09-21 12:09:30 -07:00
|
|
|
assert.NotNil(apiErr)
|
2021-02-25 12:43:17 -05:00
|
|
|
assert.EqualValues(http.StatusNotFound, apiErr.Response().StatusCode())
|
2020-09-21 12:09:30 -07:00
|
|
|
|
2020-09-24 14:34:58 -07:00
|
|
|
_, err = pc.Create(tc.Context(), "static", proj.GetPublicId(), hostcatalogs.WithName("foo"))
|
|
|
|
|
require.Error(err)
|
|
|
|
|
apiErr = api.AsServerError(err)
|
2020-06-05 13:02:58 -07:00
|
|
|
assert.NotNil(apiErr)
|
|
|
|
|
|
2023-03-10 15:06:39 -05:00
|
|
|
_, err = pc.Read(tc.Context(), globals.StaticHostCatalogPrefix+"_doesntexis")
|
2020-09-24 14:34:58 -07:00
|
|
|
require.Error(err)
|
|
|
|
|
apiErr = api.AsServerError(err)
|
2020-06-05 13:02:58 -07:00
|
|
|
assert.NotNil(apiErr)
|
2021-02-25 12:43:17 -05:00
|
|
|
assert.EqualValues(http.StatusNotFound, apiErr.Response().StatusCode())
|
2020-06-05 13:02:58 -07:00
|
|
|
|
2020-09-24 14:34:58 -07:00
|
|
|
_, err = pc.Read(tc.Context(), "invalid id")
|
|
|
|
|
require.Error(err)
|
|
|
|
|
apiErr = api.AsServerError(err)
|
2020-06-05 13:02:58 -07:00
|
|
|
assert.NotNil(apiErr)
|
2021-02-25 12:43:17 -05:00
|
|
|
assert.EqualValues(http.StatusBadRequest, apiErr.Response().StatusCode())
|
2020-06-05 13:02:58 -07:00
|
|
|
}
|