Files
threshold/api/hosts/host_catalog_test.go
T

122 lines
3.9 KiB
Go
Raw Normal View History

2020-06-05 13:02:58 -07:00
package hosts_test
import (
"net/http"
"testing"
"github.com/hashicorp/boundary/api"
"github.com/hashicorp/boundary/api/hosts"
"github.com/hashicorp/boundary/api/scopes"
"github.com/hashicorp/boundary/internal/host/static"
"github.com/hashicorp/boundary/internal/servers/controller"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
2020-06-05 13:02:58 -07:00
func TestCatalogs_Crud(t *testing.T) {
assert, require := assert.New(t), require.New(t)
orgId := "o_1234567890"
amId := "paum_1234567890"
tc := controller.NewTestController(t, &controller.TestControllerOpts{
DisableAuthorizationFailures: true,
DefaultOrgId: orgId,
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()
proj, apiErr, err := scopes.NewScopesClient(client).Create(tc.Context(), orgId)
require.NoError(err)
require.Nil(apiErr)
projClient := client.Clone()
projClient.SetScopeId(proj.Id)
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
hc, apiErr, err := hcClient.Create(tc.Context(), hosts.WithName("foo"), hosts.WithType("static"))
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)
orgId := "o_1234567890"
amId := "paum_1234567890"
tc := controller.NewTestController(t, &controller.TestControllerOpts{
DisableAuthorizationFailures: true,
DefaultOrgId: orgId,
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()
proj, apiErr, err := scopes.NewScopesClient(client).Create(tc.Context(), orgId)
require.NoError(err)
require.Nil(apiErr)
client.SetScopeId(proj.Id)
pc := hosts.NewHostCatalogsClient(client)
hc, apiErr, err := pc.Create(tc.Context(), hosts.WithType("static"))
require.NoError(err)
2020-06-05 13:02:58 -07:00
assert.Nil(apiErr)
assert.NotNil(hc)
_, apiErr, err = pc.Create(tc.Context())
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
_, apiErr, err = pc.Update(tc.Context(), hc.Id, hc.Version, hosts.WithType("Cant Update"))
require.NoError(err)
2020-06-05 13:02:58 -07:00
assert.NotNil(apiErr)
assert.EqualValues(http.StatusBadRequest, apiErr.Status)
2020-06-05 13:02:58 -07:00
}