Files
threshold/api/hosts/host_catalog_test.go
T

168 lines
5.3 KiB
Go
Raw Normal View History

2020-06-05 13:02:58 -07:00
package hosts_test
import (
"fmt"
"net/http"
"testing"
"github.com/hashicorp/boundary/api"
"github.com/hashicorp/boundary/api/hosts"
"github.com/hashicorp/boundary/internal/host/static"
2020-08-17 15:38:41 -04:00
"github.com/hashicorp/boundary/internal/iam"
"github.com/hashicorp/boundary/internal/servers/controller"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestCatalog_List(t *testing.T) {
assert, require := assert.New(t), require.New(t)
amId := "paum_1234567890"
tc := controller.NewTestController(t, &controller.TestControllerOpts{
DisableAuthorizationFailures: true,
DefaultAuthMethodId: amId,
DefaultLoginName: "user",
DefaultPassword: "passpass",
})
defer tc.Shutdown()
client := tc.Client()
_, proj := iam.TestScopes(t, tc.IamRepo())
client.SetScopeId(proj.GetPublicId())
catalogClient := hosts.NewHostCatalogsClient(client)
ul, apiErr, err := catalogClient.List(tc.Context())
assert.NoError(err)
assert.Nil(apiErr)
assert.Empty(ul)
var expected []*hosts.HostCatalog
for i := 0; i < 10; i++ {
expected = append(expected, &hosts.HostCatalog{Name: fmt.Sprint(i)})
}
expected[0], apiErr, err = catalogClient.Create(tc.Context(), "static", hosts.WithName(expected[0].Name))
assert.NoError(err)
assert.Nil(apiErr)
ul, apiErr, err = catalogClient.List(tc.Context())
assert.NoError(err)
assert.Nil(apiErr)
assert.ElementsMatch(comparableSlice(expected[:1]), comparableSlice(ul))
for i := 1; i < 10; i++ {
expected[i], apiErr, err = catalogClient.Create(tc.Context(), "static", hosts.WithName(expected[i].Name))
assert.NoError(err)
assert.Nil(apiErr)
}
ul, apiErr, err = catalogClient.List(tc.Context())
require.NoError(err)
assert.Nil(apiErr)
assert.ElementsMatch(comparableSlice(expected), comparableSlice(ul))
}
func comparableSlice(in []*hosts.HostCatalog) []hosts.HostCatalog {
var filtered []hosts.HostCatalog
for _, i := range in {
p := hosts.HostCatalog{
Id: i.Id,
Name: i.Name,
Description: i.Description,
CreatedTime: i.CreatedTime,
UpdatedTime: i.UpdatedTime,
}
filtered = append(filtered, p)
}
return filtered
}
2020-06-05 13:02:58 -07:00
func TestCatalogs_Crud(t *testing.T) {
assert, require := assert.New(t), require.New(t)
amId := "paum_1234567890"
tc := controller.NewTestController(t, &controller.TestControllerOpts{
DisableAuthorizationFailures: true,
DefaultAuthMethodId: amId,
2020-08-11 17:25:00 -04:00
DefaultLoginName: "user",
DefaultPassword: "passpass",
})
2020-06-05 13:02:58 -07:00
defer tc.Shutdown()
client := tc.Client()
2020-08-19 16:47:18 -04:00
org, proj := iam.TestScopes(t, tc.IamRepo())
2020-08-17 15:38:41 -04:00
client.SetScopeId(org.GetPublicId())
projClient := client.Clone()
2020-08-17 15:38:41 -04:00
projClient.SetScopeId(proj.GetPublicId())
checkCatalog := func(step string, hc *hosts.HostCatalog, apiErr *api.Error, err error, wantedName string, wantVersion uint32) {
require.NoError(err, step)
if !assert.Nil(apiErr, step) && apiErr.Message != "" {
t.Errorf("ApiError message: %q", apiErr.Message)
2020-06-05 13:02:58 -07:00
}
2020-06-15 15:22:41 -07:00
assert.NotNil(hc, "returned no resource", step)
2020-06-05 13:02:58 -07:00
gotName := ""
if hc.Name != "" {
gotName = hc.Name
2020-06-05 13:02:58 -07:00
}
assert.Equal(wantedName, gotName, step)
assert.Equal(wantVersion, hc.Version)
2020-06-05 13:02:58 -07:00
}
hcClient := hosts.NewHostCatalogsClient(projClient)
2020-06-05 13:02:58 -07:00
2020-08-18 19:16:52 -04:00
hc, apiErr, err := hcClient.Create(tc.Context(), "static", hosts.WithName("foo"))
checkCatalog("create", hc, apiErr, err, "foo", 1)
2020-06-05 13:02:58 -07:00
hc, apiErr, err = hcClient.Read(tc.Context(), hc.Id)
checkCatalog("read", hc, apiErr, err, "foo", 1)
2020-06-05 13:02:58 -07:00
hc, apiErr, err = hcClient.Update(tc.Context(), hc.Id, hc.Version, hosts.WithName("bar"))
checkCatalog("update", hc, apiErr, err, "bar", 2)
2020-06-05 13:02:58 -07:00
hc, apiErr, err = hcClient.Update(tc.Context(), hc.Id, hc.Version, hosts.DefaultName())
checkCatalog("update", hc, apiErr, err, "", 3)
2020-06-05 13:02:58 -07:00
existed, apiErr, err := hcClient.Delete(tc.Context(), hc.Id)
assert.NoError(err)
assert.True(existed, "Expected existing catalog when deleted, but it wasn't.")
existed, apiErr, err = hcClient.Delete(tc.Context(), hc.Id)
assert.NoError(err)
assert.False(existed, "Expected catalog to not exist when deleted, but it did.")
2020-06-05 13:02:58 -07:00
}
// TODO: Get better coverage for expected errors and error formats.
2020-06-09 11:20:59 -07:00
func TestCatalogs_Errors(t *testing.T) {
assert, require := assert.New(t), require.New(t)
amId := "paum_1234567890"
tc := controller.NewTestController(t, &controller.TestControllerOpts{
DisableAuthorizationFailures: true,
DefaultAuthMethodId: amId,
2020-08-11 17:25:00 -04:00
DefaultLoginName: "user",
DefaultPassword: "passpass",
})
2020-06-05 13:02:58 -07:00
defer tc.Shutdown()
client := tc.Client()
2020-08-19 16:47:18 -04:00
_, proj := iam.TestScopes(t, tc.IamRepo())
2020-08-17 15:38:41 -04:00
client.SetScopeId(proj.GetPublicId())
pc := hosts.NewHostCatalogsClient(client)
2020-08-18 19:16:52 -04:00
hc, apiErr, err := pc.Create(tc.Context(), "static", hosts.WithName("foo"))
require.NoError(err)
2020-06-05 13:02:58 -07:00
assert.Nil(apiErr)
assert.NotNil(hc)
2020-08-18 19:16:52 -04:00
_, apiErr, err = pc.Create(tc.Context(), "static", hosts.WithName("foo"))
require.NoError(err)
2020-06-05 13:02:58 -07:00
assert.NotNil(apiErr)
_, apiErr, err = pc.Read(tc.Context(), static.HostCatalogPrefix+"_doesntexis")
require.NoError(err)
2020-06-05 13:02:58 -07:00
// TODO: Should this be nil instead of just a catalog that has no values set
assert.NotNil(apiErr)
assert.EqualValues(apiErr.Status, http.StatusNotFound)
2020-06-05 13:02:58 -07:00
_, apiErr, err = pc.Read(tc.Context(), "invalid id")
require.NoError(err)
2020-06-05 13:02:58 -07:00
assert.NotNil(apiErr)
assert.EqualValues(http.StatusForbidden, apiErr.Status)
2020-06-05 13:02:58 -07:00
}