2020-08-20 08:20:26 -07:00
package hosts_test
import (
"context"
2020-09-23 08:10:27 -07:00
"errors"
2020-08-20 08:20:26 -07:00
"fmt"
"strings"
"testing"
"github.com/golang/protobuf/ptypes/wrappers"
"github.com/google/go-cmp/cmp"
"github.com/hashicorp/boundary/internal/db"
pb "github.com/hashicorp/boundary/internal/gen/controller/api/resources/hosts"
"github.com/hashicorp/boundary/internal/gen/controller/api/resources/scopes"
pbs "github.com/hashicorp/boundary/internal/gen/controller/api/services"
"github.com/hashicorp/boundary/internal/host/static"
"github.com/hashicorp/boundary/internal/iam"
"github.com/hashicorp/boundary/internal/kms"
2021-07-27 09:59:52 -07:00
"github.com/hashicorp/boundary/internal/servers/controller/auth"
2020-09-23 08:10:27 -07:00
"github.com/hashicorp/boundary/internal/servers/controller/handlers"
2020-08-20 08:20:26 -07:00
"github.com/hashicorp/boundary/internal/servers/controller/handlers/hosts"
"github.com/hashicorp/boundary/internal/types/scope"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"google.golang.org/genproto/protobuf/field_mask"
"google.golang.org/grpc/codes"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/testing/protocmp"
"google.golang.org/protobuf/types/known/structpb"
2021-04-23 09:13:37 -05:00
"google.golang.org/protobuf/types/known/timestamppb"
2020-08-20 08:20:26 -07:00
)
2021-04-23 09:13:37 -05:00
var testAuthorizedActions = [] string { "no-op" , "read" , "update" , "delete" }
2020-08-20 08:20:26 -07:00
func TestGet ( t * testing . T ) {
t . Parallel ()
conn , _ := db . TestSetup ( t , "postgres" )
wrapper := db . TestWrapper ( t )
kms := kms . TestKms ( t , conn , wrapper )
2021-03-25 03:15:43 -04:00
iamRepo := iam . TestRepo ( t , conn , wrapper )
iamRepoFn := func () ( * iam . Repository , error ) {
return iamRepo , nil
}
org , proj := iam . TestScopes ( t , iamRepo )
2020-08-20 08:20:26 -07:00
rw := db . New ( conn )
repoFn := func () ( * static . Repository , error ) {
return static . NewRepository ( rw , rw , kms )
}
hc := static . TestCatalogs ( t , conn , proj . GetPublicId (), 1 )[ 0 ]
h := static . TestHosts ( t , conn , hc . GetPublicId (), 1 )[ 0 ]
pHost := & pb . Host {
HostCatalogId : hc . GetPublicId (),
Id : h . GetPublicId (),
CreatedTime : h . CreateTime . GetTimestamp (),
UpdatedTime : h . UpdateTime . GetTimestamp (),
2021-03-25 03:15:43 -04:00
Scope : & scopes . ScopeInfo { Id : proj . GetPublicId (), Type : scope . Project . String (), ParentScopeId : org . GetPublicId ()},
2020-08-20 08:20:26 -07:00
Type : "static" ,
Attributes : & structpb . Struct { Fields : map [ string ] * structpb . Value {
"address" : structpb . NewStringValue ( h . GetAddress ()),
}},
2021-04-23 09:13:37 -05:00
AuthorizedActions : testAuthorizedActions ,
2020-08-20 08:20:26 -07:00
}
cases := [] struct {
2020-09-23 08:10:27 -07:00
name string
req * pbs . GetHostRequest
res * pbs . GetHostResponse
err error
2020-08-20 08:20:26 -07:00
}{
{
2020-09-23 08:10:27 -07:00
name : "Get an Existing Host" ,
req : & pbs . GetHostRequest { Id : h . GetPublicId ()},
res : & pbs . GetHostResponse { Item : pHost },
2020-08-20 08:20:26 -07:00
},
{
2020-09-23 08:10:27 -07:00
name : "Get a non existing Host Set" ,
req : & pbs . GetHostRequest { Id : static . HostPrefix + "_DoesntExis" },
res : nil ,
err : handlers . ApiErrorWithCode ( codes . NotFound ),
2020-08-20 08:20:26 -07:00
},
{
2020-09-23 08:10:27 -07:00
name : "Wrong id prefix" ,
req : & pbs . GetHostRequest { Id : "j_1234567890" },
res : nil ,
err : handlers . ApiErrorWithCode ( codes . InvalidArgument ),
2020-08-20 08:20:26 -07:00
},
{
2020-09-23 08:10:27 -07:00
name : "space in id" ,
req : & pbs . GetHostRequest { Id : static . HostPrefix + "_1 23456789" },
res : nil ,
err : handlers . ApiErrorWithCode ( codes . InvalidArgument ),
2020-08-20 08:20:26 -07:00
},
}
for _ , tc := range cases {
t . Run ( tc . name , func ( t * testing . T ) {
2020-09-23 08:10:27 -07:00
assert , require := assert . New ( t ), require . New ( t )
2020-08-20 08:20:26 -07:00
s , err := hosts . NewService ( repoFn )
2020-09-23 08:10:27 -07:00
require . NoError ( err , "Couldn't create a new host set service." )
2020-08-20 08:20:26 -07:00
2021-03-25 03:15:43 -04:00
got , gErr := s . GetHost ( auth . DisabledAuthTestContext ( iamRepoFn , proj . GetPublicId ()), tc . req )
2020-09-23 08:10:27 -07:00
if tc . err != nil {
require . Error ( gErr )
assert . True ( errors . Is ( gErr , tc . err ), "GetHost(%+v) got error %v, wanted %v" , tc . req , gErr , tc . err )
}
2020-08-20 08:20:26 -07:00
if tc . res != nil {
tc . res . Item . Version = 1
}
2020-09-02 19:43:41 -07:00
assert . Empty ( cmp . Diff ( got , tc . res , protocmp . Transform ()), "GetHost(%q) got response %q, wanted %q" , tc . req , got , tc . res )
2020-08-20 08:20:26 -07:00
})
}
}
func TestList ( t * testing . T ) {
conn , _ := db . TestSetup ( t , "postgres" )
wrapper := db . TestWrapper ( t )
kms := kms . TestKms ( t , conn , wrapper )
2021-03-25 03:15:43 -04:00
iamRepo := iam . TestRepo ( t , conn , wrapper )
iamRepoFn := func () ( * iam . Repository , error ) {
return iamRepo , nil
}
org , proj := iam . TestScopes ( t , iamRepo )
2020-08-20 08:20:26 -07:00
rw := db . New ( conn )
repoFn := func () ( * static . Repository , error ) {
return static . NewRepository ( rw , rw , kms )
}
hcs := static . TestCatalogs ( t , conn , proj . GetPublicId (), 2 )
hc , hcNoHosts := hcs [ 0 ], hcs [ 1 ]
var wantHs [] * pb . Host
for _ , h := range static . TestHosts ( t , conn , hc . GetPublicId (), 10 ) {
wantHs = append ( wantHs , & pb . Host {
Id : h . GetPublicId (),
HostCatalogId : h . GetCatalogId (),
2021-03-25 03:15:43 -04:00
Scope : & scopes . ScopeInfo { Id : proj . GetPublicId (), Type : scope . Project . String (), ParentScopeId : org . GetPublicId ()},
2020-08-20 08:20:26 -07:00
CreatedTime : h . GetCreateTime (). GetTimestamp (),
UpdatedTime : h . GetUpdateTime (). GetTimestamp (),
Version : h . GetVersion (),
2021-07-27 09:59:52 -07:00
Type : static . Subtype . String (), Attributes : & structpb . Struct { Fields : map [ string ] * structpb . Value {
2020-08-20 08:20:26 -07:00
"address" : structpb . NewStringValue ( h . GetAddress ()),
}},
2021-04-23 09:13:37 -05:00
AuthorizedActions : testAuthorizedActions ,
2020-08-20 08:20:26 -07:00
})
}
cases := [] struct {
2021-02-22 10:09:25 -07:00
name string
req * pbs . ListHostsRequest
res * pbs . ListHostsResponse
err error
2020-08-20 08:20:26 -07:00
}{
{
2021-02-22 10:09:25 -07:00
name : "List Many Hosts" ,
req : & pbs . ListHostsRequest { HostCatalogId : hc . GetPublicId ()},
res : & pbs . ListHostsResponse { Items : wantHs },
},
{
name : "List No Hosts" ,
req : & pbs . ListHostsRequest { HostCatalogId : hcNoHosts . GetPublicId ()},
res : & pbs . ListHostsResponse {},
},
{
name : "Filter to One Host" ,
req : & pbs . ListHostsRequest { HostCatalogId : hc . GetPublicId (), Filter : fmt . Sprintf ( `"/item/id"==%q` , wantHs [ 1 ]. GetId ())},
res : & pbs . ListHostsResponse { Items : wantHs [ 1 : 2 ]},
},
{
name : "Filter to No Hosts" ,
req : & pbs . ListHostsRequest { HostCatalogId : hc . GetPublicId (), Filter : `"/item/id"=="doesnt match"` },
res : & pbs . ListHostsResponse {},
2020-08-20 08:20:26 -07:00
},
{
2021-02-22 10:09:25 -07:00
name : "Filter Bad Format" ,
req : & pbs . ListHostsRequest { HostCatalogId : hc . GetPublicId (), Filter : `"//id/"=="bad"` },
err : handlers . InvalidArgumentErrorf ( "bad format" , nil ),
2020-08-20 08:20:26 -07:00
},
}
for _ , tc := range cases {
t . Run ( tc . name , func ( t * testing . T ) {
assert , require := assert . New ( t ), require . New ( t )
s , err := hosts . NewService ( repoFn )
require . NoError ( err , "Couldn't create new host set service." )
2021-05-04 19:57:35 -05:00
// Test non-anonymous listing
2021-03-25 03:15:43 -04:00
got , gErr := s . ListHosts ( auth . DisabledAuthTestContext ( iamRepoFn , proj . GetPublicId ()), tc . req )
2020-09-23 08:10:27 -07:00
if tc . err != nil {
require . Error ( gErr )
2021-02-22 10:09:25 -07:00
assert . True ( errors . Is ( gErr , tc . err ), "ListHosts(%q) got error %v, wanted %v" , tc . req , gErr , tc . err )
2021-05-04 19:57:35 -05:00
return
2020-09-23 08:10:27 -07:00
}
2021-05-04 19:57:35 -05:00
require . NoError ( gErr )
2021-02-22 10:09:25 -07:00
assert . Empty ( cmp . Diff ( got , tc . res , protocmp . Transform ()), "ListHosts(%q) got response %q, wanted %q" , tc . req , got , tc . res )
2021-05-04 19:57:35 -05:00
// Test anonymous listing
got , gErr = s . ListHosts ( auth . DisabledAuthTestContext ( iamRepoFn , proj . GetPublicId (), auth . WithUserId ( auth . AnonymousUserId )), tc . req )
require . NoError ( gErr )
assert . Len ( got . Items , len ( tc . res . Items ))
for _ , item := range got . GetItems () {
require . Nil ( item . CreatedTime )
require . Nil ( item . UpdatedTime )
require . Zero ( item . Version )
}
2020-08-20 08:20:26 -07:00
})
}
}
func TestDelete ( t * testing . T ) {
t . Parallel ()
conn , _ := db . TestSetup ( t , "postgres" )
wrapper := db . TestWrapper ( t )
kms := kms . TestKms ( t , conn , wrapper )
2021-03-25 03:15:43 -04:00
iamRepo := iam . TestRepo ( t , conn , wrapper )
iamRepoFn := func () ( * iam . Repository , error ) {
return iamRepo , nil
}
_ , proj := iam . TestScopes ( t , iamRepo )
2020-08-20 08:20:26 -07:00
rw := db . New ( conn )
repoFn := func () ( * static . Repository , error ) {
return static . NewRepository ( rw , rw , kms )
}
hc := static . TestCatalogs ( t , conn , proj . GetPublicId (), 1 )[ 0 ]
h := static . TestHosts ( t , conn , hc . GetPublicId (), 1 )[ 0 ]
s , err := hosts . NewService ( repoFn )
require . NoError ( t , err , "Couldn't create a new host set service." )
cases := [] struct {
name string
scopeId string
req * pbs . DeleteHostRequest
res * pbs . DeleteHostResponse
2020-09-23 08:10:27 -07:00
err error
2020-08-20 08:20:26 -07:00
}{
{
name : "Delete an Existing Host" ,
scopeId : proj . GetPublicId (),
req : & pbs . DeleteHostRequest {
2020-09-02 19:43:41 -07:00
Id : h . GetPublicId (),
2020-08-20 08:20:26 -07:00
},
},
{
name : "Delete bad id Host" ,
scopeId : proj . GetPublicId (),
req : & pbs . DeleteHostRequest {
2020-09-02 19:43:41 -07:00
Id : static . HostPrefix + "_doesntexis" ,
2020-08-20 08:20:26 -07:00
},
2020-09-23 08:10:27 -07:00
err : handlers . ApiErrorWithCode ( codes . NotFound ),
2020-08-20 08:20:26 -07:00
},
{
name : "Bad Host Id formatting" ,
scopeId : proj . GetPublicId (),
req : & pbs . DeleteHostRequest {
2020-09-02 19:43:41 -07:00
Id : static . HostPrefix + "_bad_format" ,
2020-08-20 08:20:26 -07:00
},
2020-09-23 08:10:27 -07:00
err : handlers . ApiErrorWithCode ( codes . InvalidArgument ),
2020-08-20 08:20:26 -07:00
},
}
for _ , tc := range cases {
t . Run ( tc . name , func ( t * testing . T ) {
2020-09-23 08:10:27 -07:00
assert , require := assert . New ( t ), require . New ( t )
2021-03-25 03:15:43 -04:00
got , gErr := s . DeleteHost ( auth . DisabledAuthTestContext ( iamRepoFn , tc . scopeId ), tc . req )
2020-09-23 08:10:27 -07:00
if tc . err != nil {
require . Error ( gErr )
assert . True ( errors . Is ( gErr , tc . err ), "DeleteHost(%+v) got error %v, wanted %v" , tc . req , gErr , tc . err )
}
2020-08-20 08:20:26 -07:00
assert . Empty ( cmp . Diff ( tc . res , got , protocmp . Transform ()), "DeleteHost(%q) got response %q, wanted %q" , tc . req , got , tc . res )
})
}
}
func TestDelete_twice ( t * testing . T ) {
t . Parallel ()
2020-09-23 08:10:27 -07:00
assert , require := assert . New ( t ), require . New ( t )
2020-08-20 08:20:26 -07:00
conn , _ := db . TestSetup ( t , "postgres" )
wrapper := db . TestWrapper ( t )
kms := kms . TestKms ( t , conn , wrapper )
2021-03-25 03:15:43 -04:00
iamRepo := iam . TestRepo ( t , conn , wrapper )
iamRepoFn := func () ( * iam . Repository , error ) {
return iamRepo , nil
}
_ , proj := iam . TestScopes ( t , iamRepo )
2020-08-20 08:20:26 -07:00
rw := db . New ( conn )
repoFn := func () ( * static . Repository , error ) {
return static . NewRepository ( rw , rw , kms )
}
hc := static . TestCatalogs ( t , conn , proj . GetPublicId (), 1 )[ 0 ]
h := static . TestHosts ( t , conn , hc . GetPublicId (), 1 )[ 0 ]
s , err := hosts . NewService ( repoFn )
2020-09-23 08:10:27 -07:00
require . NoError ( err , "Couldn't create a new host set service." )
2020-08-20 08:20:26 -07:00
req := & pbs . DeleteHostRequest {
2020-09-02 19:43:41 -07:00
Id : h . GetPublicId (),
2020-08-20 08:20:26 -07:00
}
2021-03-25 03:15:43 -04:00
ctx := auth . DisabledAuthTestContext ( iamRepoFn , proj . GetPublicId ())
2020-09-08 23:57:26 -04:00
_ , gErr := s . DeleteHost ( ctx , req )
2020-08-20 08:20:26 -07:00
assert . NoError ( gErr , "First attempt" )
2020-09-08 23:57:26 -04:00
_ , gErr = s . DeleteHost ( ctx , req )
2020-09-04 14:48:11 -07:00
assert . Error ( gErr , "Second attempt" )
2020-09-23 08:10:27 -07:00
assert . True ( errors . Is ( gErr , handlers . ApiErrorWithCode ( codes . NotFound )), "Expected permission denied for the second delete." )
2020-08-20 08:20:26 -07:00
}
func TestCreate ( t * testing . T ) {
t . Parallel ()
conn , _ := db . TestSetup ( t , "postgres" )
wrapper := db . TestWrapper ( t )
kms := kms . TestKms ( t , conn , wrapper )
2021-03-25 03:15:43 -04:00
iamRepo := iam . TestRepo ( t , conn , wrapper )
iamRepoFn := func () ( * iam . Repository , error ) {
return iamRepo , nil
}
org , proj := iam . TestScopes ( t , iamRepo )
2020-08-20 08:20:26 -07:00
rw := db . New ( conn )
repoFn := func () ( * static . Repository , error ) {
return static . NewRepository ( rw , rw , kms )
}
hc := static . TestCatalogs ( t , conn , proj . GetPublicId (), 1 )[ 0 ]
2021-04-23 09:13:37 -05:00
defaultHcCreated := hc . GetCreateTime (). GetTimestamp (). AsTime ()
2020-08-20 08:20:26 -07:00
cases := [] struct {
2020-09-23 08:10:27 -07:00
name string
req * pbs . CreateHostRequest
res * pbs . CreateHostResponse
err error
2020-08-20 08:20:26 -07:00
}{
{
name : "Create a valid Host" ,
req : & pbs . CreateHostRequest { Item : & pb . Host {
2020-09-02 19:43:41 -07:00
HostCatalogId : hc . GetPublicId (),
Name : & wrappers . StringValue { Value : "name" },
Description : & wrappers . StringValue { Value : "desc" },
Type : "static" ,
2020-08-20 08:20:26 -07:00
Attributes : & structpb . Struct { Fields : map [ string ] * structpb . Value {
"address" : structpb . NewStringValue ( "123.456.789" ),
}},
}},
res : & pbs . CreateHostResponse {
2020-09-02 19:43:41 -07:00
Uri : fmt . Sprintf ( "hosts/%s_" , static . HostPrefix ),
2020-08-20 08:20:26 -07:00
Item : & pb . Host {
HostCatalogId : hc . GetPublicId (),
2021-03-25 03:15:43 -04:00
Scope : & scopes . ScopeInfo { Id : proj . GetPublicId (), Type : scope . Project . String (), ParentScopeId : org . GetPublicId ()},
2020-08-20 08:20:26 -07:00
Name : & wrappers . StringValue { Value : "name" },
Description : & wrappers . StringValue { Value : "desc" },
Type : "static" ,
Attributes : & structpb . Struct { Fields : map [ string ] * structpb . Value {
"address" : structpb . NewStringValue ( "123.456.789" ),
}},
2021-04-23 09:13:37 -05:00
AuthorizedActions : testAuthorizedActions ,
2020-08-20 08:20:26 -07:00
},
},
},
2020-09-12 22:07:56 -07:00
{
name : "Create with empty address" ,
req : & pbs . CreateHostRequest { Item : & pb . Host {
HostCatalogId : hc . GetPublicId (),
Name : & wrappers . StringValue { Value : "name" },
Description : & wrappers . StringValue { Value : "desc" },
Type : "static" ,
Attributes : & structpb . Struct { Fields : map [ string ] * structpb . Value {
"address" : structpb . NewStringValue ( "" ),
}},
}},
2020-09-23 08:10:27 -07:00
err : handlers . ApiErrorWithCode ( codes . InvalidArgument ),
2020-09-12 22:07:56 -07:00
},
2020-08-20 08:20:26 -07:00
{
name : "Create without address" ,
req : & pbs . CreateHostRequest { Item : & pb . Host {
2020-09-02 19:43:41 -07:00
HostCatalogId : hc . GetPublicId (),
Name : & wrappers . StringValue { Value : "name" },
Description : & wrappers . StringValue { Value : "desc" },
Type : "static" ,
Attributes : & structpb . Struct { Fields : map [ string ] * structpb . Value {}},
2020-08-20 08:20:26 -07:00
}},
2020-09-23 08:10:27 -07:00
err : handlers . ApiErrorWithCode ( codes . InvalidArgument ),
2020-08-20 08:20:26 -07:00
},
{
name : "Create with unknown type" ,
req : & pbs . CreateHostRequest { Item : & pb . Host {
2020-09-02 19:43:41 -07:00
HostCatalogId : hc . GetPublicId (),
Name : & wrappers . StringValue { Value : "name" },
Description : & wrappers . StringValue { Value : "desc" },
Type : "ThisIsMadeUp" ,
2020-08-20 08:20:26 -07:00
}},
2020-09-23 08:10:27 -07:00
err : handlers . ApiErrorWithCode ( codes . InvalidArgument ),
2020-08-20 08:20:26 -07:00
},
{
name : "Create with no type" ,
req : & pbs . CreateHostRequest { Item : & pb . Host {
2020-09-02 19:43:41 -07:00
HostCatalogId : hc . GetPublicId (),
Name : & wrappers . StringValue { Value : "no type name" },
Description : & wrappers . StringValue { Value : "no type desc" },
2020-08-21 10:57:30 -07:00
Attributes : & structpb . Struct { Fields : map [ string ] * structpb . Value {
"address" : structpb . NewStringValue ( "123.456.789" ),
}},
2020-08-20 08:20:26 -07:00
}},
2020-08-21 10:57:30 -07:00
res : & pbs . CreateHostResponse {
2020-09-02 19:43:41 -07:00
Uri : fmt . Sprintf ( "hosts/%s_" , static . HostPrefix ),
2020-08-21 10:57:30 -07:00
Item : & pb . Host {
HostCatalogId : hc . GetPublicId (),
2021-03-25 03:15:43 -04:00
Scope : & scopes . ScopeInfo { Id : proj . GetPublicId (), Type : scope . Project . String (), ParentScopeId : org . GetPublicId ()},
2020-08-21 10:57:30 -07:00
Name : & wrappers . StringValue { Value : "no type name" },
Description : & wrappers . StringValue { Value : "no type desc" },
Type : "static" ,
Attributes : & structpb . Struct { Fields : map [ string ] * structpb . Value {
"address" : structpb . NewStringValue ( "123.456.789" ),
}},
2021-04-23 09:13:37 -05:00
AuthorizedActions : testAuthorizedActions ,
2020-08-21 10:57:30 -07:00
},
},
2020-08-20 08:20:26 -07:00
},
{
name : "Can't specify Id" ,
req : & pbs . CreateHostRequest { Item : & pb . Host {
2020-09-02 19:43:41 -07:00
HostCatalogId : hc . GetPublicId (),
Id : "not allowed to be set" ,
2020-08-20 08:20:26 -07:00
}},
2020-09-23 08:10:27 -07:00
res : nil ,
err : handlers . ApiErrorWithCode ( codes . InvalidArgument ),
2020-08-20 08:20:26 -07:00
},
{
name : "Can't specify Created Time" ,
req : & pbs . CreateHostRequest { Item : & pb . Host {
2020-09-02 19:43:41 -07:00
HostCatalogId : hc . GetPublicId (),
2021-04-23 09:13:37 -05:00
CreatedTime : timestamppb . Now (),
2020-08-20 08:20:26 -07:00
}},
2020-09-23 08:10:27 -07:00
res : nil ,
err : handlers . ApiErrorWithCode ( codes . InvalidArgument ),
2020-08-20 08:20:26 -07:00
},
{
name : "Can't specify Update Time" ,
req : & pbs . CreateHostRequest { Item : & pb . Host {
2021-04-23 09:13:37 -05:00
UpdatedTime : timestamppb . Now (),
2020-08-20 08:20:26 -07:00
}},
2020-09-23 08:10:27 -07:00
res : nil ,
err : handlers . ApiErrorWithCode ( codes . InvalidArgument ),
2020-08-20 08:20:26 -07:00
},
}
for _ , tc := range cases {
t . Run ( tc . name , func ( t * testing . T ) {
assert , require := assert . New ( t ), require . New ( t )
s , err := hosts . NewService ( repoFn )
require . NoError ( err , "Failed to create a new host set service." )
2021-03-25 03:15:43 -04:00
got , gErr := s . CreateHost ( auth . DisabledAuthTestContext ( iamRepoFn , proj . GetPublicId ()), tc . req )
2020-09-23 08:10:27 -07:00
if tc . err != nil {
require . Error ( gErr )
assert . True ( errors . Is ( gErr , tc . err ), "CreateHost(%+v) got error %v, wanted %v" , tc . req , gErr , tc . err )
}
2020-08-20 08:20:26 -07:00
if got != nil {
assert . Contains ( got . GetUri (), tc . res . GetUri ())
assert . True ( strings . HasPrefix ( got . GetItem (). GetId (), static . HostPrefix ))
2021-04-23 09:13:37 -05:00
gotCreateTime := got . GetItem (). GetCreatedTime (). AsTime ()
gotUpdateTime := got . GetItem (). GetUpdatedTime (). AsTime ()
2020-08-20 08:20:26 -07:00
// Verify it is a set created after the test setup's default set
2020-08-21 10:57:30 -07:00
assert . True ( gotCreateTime . After ( defaultHcCreated ), "New host should have been created after default host. Was created %v, which is after %v" , gotCreateTime , defaultHcCreated )
assert . True ( gotUpdateTime . After ( defaultHcCreated ), "New host should have been updated after default host. Was updated %v, which is after %v" , gotUpdateTime , defaultHcCreated )
2020-08-20 08:20:26 -07:00
// Clear all values which are hard to compare against.
got . Uri , tc . res . Uri = "" , ""
got . Item . Id , tc . res . Item . Id = "" , ""
got . Item . CreatedTime , got . Item . UpdatedTime , tc . res . Item . CreatedTime , tc . res . Item . UpdatedTime = nil , nil , nil , nil
}
if tc . res != nil {
tc . res . Item . Version = 1
}
2020-09-02 19:43:41 -07:00
assert . Empty ( cmp . Diff ( got , tc . res , protocmp . Transform ()), "CreateHost(%q) got response %q, wanted %q" , tc . req , got , tc . res )
2020-08-20 08:20:26 -07:00
})
}
}
func TestUpdate ( t * testing . T ) {
t . Parallel ()
conn , _ := db . TestSetup ( t , "postgres" )
wrapper := db . TestWrapper ( t )
kms := kms . TestKms ( t , conn , wrapper )
2021-03-25 03:15:43 -04:00
iamRepo := iam . TestRepo ( t , conn , wrapper )
iamRepoFn := func () ( * iam . Repository , error ) {
return iamRepo , nil
}
org , proj := iam . TestScopes ( t , iamRepo )
2020-08-20 08:20:26 -07:00
rw := db . New ( conn )
repoFn := func () ( * static . Repository , error ) {
return static . NewRepository ( rw , rw , kms )
}
repo , err := repoFn ()
require . NoError ( t , err , "Couldn't create new static repo." )
hc := static . TestCatalogs ( t , conn , proj . GetPublicId (), 1 )[ 0 ]
h , err := static . NewHost ( hc . GetPublicId (), static . WithName ( "default" ), static . WithDescription ( "default" ), static . WithAddress ( "defaultaddress" ))
require . NoError ( t , err )
h , err = repo . CreateHost ( context . Background (), proj . GetPublicId (), h )
require . NoError ( t , err )
var version uint32 = 1
resetHost := func () {
version ++
_ , _ , err = repo . UpdateHost ( context . Background (), proj . GetPublicId (), h , version , [] string { "Name" , "Description" , "Address" })
require . NoError ( t , err , "Failed to reset host." )
version ++
}
2021-04-23 09:13:37 -05:00
hCreated := h . GetCreateTime (). GetTimestamp (). AsTime ()
2020-08-20 08:20:26 -07:00
toMerge := & pbs . UpdateHostRequest {
2020-09-02 19:43:41 -07:00
Id : h . GetPublicId (),
2020-08-20 08:20:26 -07:00
}
tested , err := hosts . NewService ( repoFn )
require . NoError ( t , err , "Failed to create a new host set service." )
cases := [] struct {
2020-09-23 08:10:27 -07:00
name string
req * pbs . UpdateHostRequest
res * pbs . UpdateHostResponse
err error
2020-08-20 08:20:26 -07:00
}{
{
name : "Update an Existing Host" ,
req : & pbs . UpdateHostRequest {
UpdateMask : & field_mask . FieldMask {
2020-09-08 16:04:19 -07:00
Paths : [] string { "name" , "description" , "type" },
2020-08-20 08:20:26 -07:00
},
Item : & pb . Host {
Name : & wrappers . StringValue { Value : "new" },
Description : & wrappers . StringValue { Value : "desc" },
2020-09-08 16:04:19 -07:00
Type : "static" ,
2020-08-20 08:20:26 -07:00
},
},
res : & pbs . UpdateHostResponse {
Item : & pb . Host {
HostCatalogId : hc . GetPublicId (),
Id : h . GetPublicId (),
2021-03-25 03:15:43 -04:00
Scope : & scopes . ScopeInfo { Id : proj . GetPublicId (), Type : scope . Project . String (), ParentScopeId : org . GetPublicId ()},
2020-08-20 08:20:26 -07:00
Name : & wrappers . StringValue { Value : "new" },
Description : & wrappers . StringValue { Value : "desc" },
CreatedTime : h . GetCreateTime (). GetTimestamp (),
Type : "static" ,
Attributes : & structpb . Struct { Fields : map [ string ] * structpb . Value {
"address" : structpb . NewStringValue ( "defaultaddress" ),
}},
2021-04-23 09:13:37 -05:00
AuthorizedActions : testAuthorizedActions ,
2020-08-20 08:20:26 -07:00
},
},
},
{
name : "Multiple Paths in single string" ,
req : & pbs . UpdateHostRequest {
UpdateMask : & field_mask . FieldMask {
2020-09-08 16:04:19 -07:00
Paths : [] string { "name,description,type" },
2020-08-20 08:20:26 -07:00
},
Item : & pb . Host {
Name : & wrappers . StringValue { Value : "new" },
Description : & wrappers . StringValue { Value : "desc" },
2020-09-08 16:04:19 -07:00
Type : "static" ,
2020-08-20 08:20:26 -07:00
},
},
res : & pbs . UpdateHostResponse {
Item : & pb . Host {
HostCatalogId : hc . GetPublicId (),
Id : h . GetPublicId (),
2021-03-25 03:15:43 -04:00
Scope : & scopes . ScopeInfo { Id : proj . GetPublicId (), Type : scope . Project . String (), ParentScopeId : org . GetPublicId ()},
2020-08-20 08:20:26 -07:00
Name : & wrappers . StringValue { Value : "new" },
Description : & wrappers . StringValue { Value : "desc" },
CreatedTime : h . GetCreateTime (). GetTimestamp (),
Type : "static" ,
Attributes : & structpb . Struct { Fields : map [ string ] * structpb . Value {
"address" : structpb . NewStringValue ( "defaultaddress" ),
}},
2021-04-23 09:13:37 -05:00
AuthorizedActions : testAuthorizedActions ,
2020-08-20 08:20:26 -07:00
},
},
},
{
name : "No Update Mask" ,
req : & pbs . UpdateHostRequest {
Item : & pb . Host {
Name : & wrappers . StringValue { Value : "updated name" },
Description : & wrappers . StringValue { Value : "updated desc" },
},
},
2020-09-23 08:10:27 -07:00
err : handlers . ApiErrorWithCode ( codes . InvalidArgument ),
2020-08-20 08:20:26 -07:00
},
2020-09-08 16:04:19 -07:00
{
name : "No Update Mask" ,
req : & pbs . UpdateHostRequest {
UpdateMask : & field_mask . FieldMask {
Paths : [] string { "name,type" },
},
Item : & pb . Host {
Name : & wrappers . StringValue { Value : "updated name" },
Type : "ec2" ,
},
},
2020-09-23 08:10:27 -07:00
err : handlers . ApiErrorWithCode ( codes . InvalidArgument ),
2020-09-08 16:04:19 -07:00
},
2020-08-20 08:20:26 -07:00
{
name : "Empty Path" ,
req : & pbs . UpdateHostRequest {
UpdateMask : & field_mask . FieldMask { Paths : [] string {}},
Item : & pb . Host {
Name : & wrappers . StringValue { Value : "updated name" },
Description : & wrappers . StringValue { Value : "updated desc" },
},
},
2020-09-23 08:10:27 -07:00
err : handlers . ApiErrorWithCode ( codes . InvalidArgument ),
2020-08-20 08:20:26 -07:00
},
{
name : "Only non-existant paths in Mask" ,
req : & pbs . UpdateHostRequest {
UpdateMask : & field_mask . FieldMask { Paths : [] string { "nonexistant_field" }},
Item : & pb . Host {
Name : & wrappers . StringValue { Value : "updated name" },
Description : & wrappers . StringValue { Value : "updated desc" },
},
},
2020-09-23 08:10:27 -07:00
err : handlers . ApiErrorWithCode ( codes . InvalidArgument ),
2020-08-20 08:20:26 -07:00
},
{
name : "Unset Name" ,
req : & pbs . UpdateHostRequest {
UpdateMask : & field_mask . FieldMask {
Paths : [] string { "name" },
},
Item : & pb . Host {
Description : & wrappers . StringValue { Value : "ignored" },
},
},
res : & pbs . UpdateHostResponse {
Item : & pb . Host {
HostCatalogId : hc . GetPublicId (),
Id : h . GetPublicId (),
2021-03-25 03:15:43 -04:00
Scope : & scopes . ScopeInfo { Id : proj . GetPublicId (), Type : scope . Project . String (), ParentScopeId : org . GetPublicId ()},
2020-08-20 08:20:26 -07:00
Description : & wrappers . StringValue { Value : "default" },
CreatedTime : h . GetCreateTime (). GetTimestamp (),
Type : "static" ,
Attributes : & structpb . Struct { Fields : map [ string ] * structpb . Value {
"address" : structpb . NewStringValue ( "defaultaddress" ),
}},
2021-04-23 09:13:37 -05:00
AuthorizedActions : testAuthorizedActions ,
2020-08-20 08:20:26 -07:00
},
},
},
{
name : "Unset Description" ,
req : & pbs . UpdateHostRequest {
UpdateMask : & field_mask . FieldMask {
Paths : [] string { "description" },
},
Item : & pb . Host {
Name : & wrappers . StringValue { Value : "ignored" },
},
},
res : & pbs . UpdateHostResponse {
Item : & pb . Host {
HostCatalogId : hc . GetPublicId (),
Id : h . GetPublicId (),
2021-03-25 03:15:43 -04:00
Scope : & scopes . ScopeInfo { Id : proj . GetPublicId (), Type : scope . Project . String (), ParentScopeId : org . GetPublicId ()},
2020-08-20 08:20:26 -07:00
Name : & wrappers . StringValue { Value : "default" },
CreatedTime : h . GetCreateTime (). GetTimestamp (),
Type : "static" ,
Attributes : & structpb . Struct { Fields : map [ string ] * structpb . Value {
"address" : structpb . NewStringValue ( "defaultaddress" ),
}},
2021-04-23 09:13:37 -05:00
AuthorizedActions : testAuthorizedActions ,
2020-08-20 08:20:26 -07:00
},
},
},
{
name : "Update Only Name" ,
req : & pbs . UpdateHostRequest {
UpdateMask : & field_mask . FieldMask {
Paths : [] string { "name" },
},
Item : & pb . Host {
Name : & wrappers . StringValue { Value : "updated" },
Description : & wrappers . StringValue { Value : "ignored" },
},
},
res : & pbs . UpdateHostResponse {
Item : & pb . Host {
HostCatalogId : hc . GetPublicId (),
Id : h . GetPublicId (),
2021-03-25 03:15:43 -04:00
Scope : & scopes . ScopeInfo { Id : proj . GetPublicId (), Type : scope . Project . String (), ParentScopeId : org . GetPublicId ()},
2020-08-20 08:20:26 -07:00
Name : & wrappers . StringValue { Value : "updated" },
Description : & wrappers . StringValue { Value : "default" },
CreatedTime : h . GetCreateTime (). GetTimestamp (),
Type : "static" ,
Attributes : & structpb . Struct { Fields : map [ string ] * structpb . Value {
"address" : structpb . NewStringValue ( "defaultaddress" ),
}},
2021-04-23 09:13:37 -05:00
AuthorizedActions : testAuthorizedActions ,
2020-08-20 08:20:26 -07:00
},
},
},
{
name : "Update Only Description" ,
req : & pbs . UpdateHostRequest {
UpdateMask : & field_mask . FieldMask {
Paths : [] string { "description" },
},
Item : & pb . Host {
Name : & wrappers . StringValue { Value : "ignored" },
Description : & wrappers . StringValue { Value : "notignored" },
},
},
res : & pbs . UpdateHostResponse {
Item : & pb . Host {
HostCatalogId : hc . GetPublicId (),
Id : h . GetPublicId (),
2021-03-25 03:15:43 -04:00
Scope : & scopes . ScopeInfo { Id : proj . GetPublicId (), Type : scope . Project . String (), ParentScopeId : org . GetPublicId ()},
2020-08-20 08:20:26 -07:00
Name : & wrappers . StringValue { Value : "default" },
Description : & wrappers . StringValue { Value : "notignored" },
CreatedTime : h . GetCreateTime (). GetTimestamp (),
Type : "static" ,
Attributes : & structpb . Struct { Fields : map [ string ] * structpb . Value {
"address" : structpb . NewStringValue ( "defaultaddress" ),
}},
2021-04-23 09:13:37 -05:00
AuthorizedActions : testAuthorizedActions ,
2020-08-20 08:20:26 -07:00
},
},
},
{
name : "Update a Non Existing Host" ,
req : & pbs . UpdateHostRequest {
Id : static . HostPrefix + "_DoesntExis" ,
UpdateMask : & field_mask . FieldMask {
Paths : [] string { "description" },
},
Item : & pb . Host {
Name : & wrappers . StringValue { Value : "new" },
2021-03-25 03:15:43 -04:00
Scope : & scopes . ScopeInfo { Id : proj . GetPublicId (), Type : scope . Project . String (), ParentScopeId : org . GetPublicId ()},
2020-08-20 08:20:26 -07:00
Description : & wrappers . StringValue { Value : "desc" },
},
},
2020-09-23 08:10:27 -07:00
err : handlers . ApiErrorWithCode ( codes . NotFound ),
2020-08-20 08:20:26 -07:00
},
{
name : "Cant change Id" ,
req : & pbs . UpdateHostRequest {
Id : hc . GetPublicId (),
UpdateMask : & field_mask . FieldMask {
Paths : [] string { "id" },
},
Item : & pb . Host {
Id : "p_somethinge" ,
2021-03-25 03:15:43 -04:00
Scope : & scopes . ScopeInfo { Id : proj . GetPublicId (), Type : scope . Project . String (), ParentScopeId : org . GetPublicId ()},
2020-08-20 08:20:26 -07:00
Name : & wrappers . StringValue { Value : "new" },
Description : & wrappers . StringValue { Value : "new desc" },
2021-01-07 10:02:32 -05:00
},
},
2020-09-23 08:10:27 -07:00
res : nil ,
err : handlers . ApiErrorWithCode ( codes . InvalidArgument ),
2020-08-20 08:20:26 -07:00
},
2020-09-12 22:07:56 -07:00
{
name : "Cant unset address" ,
req : & pbs . UpdateHostRequest {
Id : hc . GetPublicId (),
UpdateMask : & field_mask . FieldMask {
Paths : [] string { "attributes.address" },
},
Item : & pb . Host {
Attributes : & structpb . Struct { Fields : map [ string ] * structpb . Value {
"address" : structpb . NewNullValue (),
}},
2021-01-07 10:02:32 -05:00
},
},
2020-09-23 08:10:27 -07:00
res : nil ,
err : handlers . ApiErrorWithCode ( codes . InvalidArgument ),
2020-09-12 22:07:56 -07:00
},
{
name : "Cant set address to empty string" ,
req : & pbs . UpdateHostRequest {
Id : hc . GetPublicId (),
UpdateMask : & field_mask . FieldMask {
Paths : [] string { "attributes.address" },
},
Item : & pb . Host {
Attributes : & structpb . Struct { Fields : map [ string ] * structpb . Value {
"address" : structpb . NewStringValue ( "" ),
}},
2021-01-07 10:02:32 -05:00
},
},
2020-09-23 08:10:27 -07:00
res : nil ,
err : handlers . ApiErrorWithCode ( codes . InvalidArgument ),
2020-09-12 22:07:56 -07:00
},
2020-08-20 08:20:26 -07:00
{
name : "Cant specify Created Time" ,
req : & pbs . UpdateHostRequest {
UpdateMask : & field_mask . FieldMask {
Paths : [] string { "created_time" },
},
Item : & pb . Host {
2021-04-23 09:13:37 -05:00
CreatedTime : timestamppb . Now (),
2020-08-20 08:20:26 -07:00
},
},
2020-09-23 08:10:27 -07:00
res : nil ,
err : handlers . ApiErrorWithCode ( codes . InvalidArgument ),
2020-08-20 08:20:26 -07:00
},
{
name : "Cant specify Updated Time" ,
req : & pbs . UpdateHostRequest {
UpdateMask : & field_mask . FieldMask {
Paths : [] string { "updated_time" },
},
Item : & pb . Host {
2021-04-23 09:13:37 -05:00
UpdatedTime : timestamppb . Now (),
2020-08-20 08:20:26 -07:00
},
},
2020-09-23 08:10:27 -07:00
res : nil ,
err : handlers . ApiErrorWithCode ( codes . InvalidArgument ),
2020-08-20 08:20:26 -07:00
},
{
name : "Valid mask, cant specify type" ,
req : & pbs . UpdateHostRequest {
UpdateMask : & field_mask . FieldMask {
Paths : [] string { "name" },
},
Item : & pb . Host {
Type : "Unknown" ,
},
},
2020-09-23 08:10:27 -07:00
res : nil ,
err : handlers . ApiErrorWithCode ( codes . InvalidArgument ),
2020-08-20 08:20:26 -07:00
},
}
for _ , tc := range cases {
t . Run ( tc . name , func ( t * testing . T ) {
assert , require := assert . New ( t ), require . New ( t )
tc . req . Item . Version = version
req := proto . Clone ( toMerge ).( * pbs . UpdateHostRequest )
proto . Merge ( req , tc . req )
// Test some bad versions
req . Item . Version = version + 2
2021-03-25 03:15:43 -04:00
_ , gErr := tested . UpdateHost ( auth . DisabledAuthTestContext ( iamRepoFn , proj . GetPublicId ()), req )
2020-08-20 08:20:26 -07:00
require . Error ( gErr )
req . Item . Version = version - 1
2021-03-25 03:15:43 -04:00
_ , gErr = tested . UpdateHost ( auth . DisabledAuthTestContext ( iamRepoFn , proj . GetPublicId ()), req )
2020-08-20 08:20:26 -07:00
require . Error ( gErr )
req . Item . Version = version
2021-03-25 03:15:43 -04:00
got , gErr := tested . UpdateHost ( auth . DisabledAuthTestContext ( iamRepoFn , proj . GetPublicId ()), req )
2020-09-23 08:10:27 -07:00
if tc . err != nil {
require . Error ( gErr )
assert . True ( errors . Is ( gErr , tc . err ), "UpdateHost(%+v) got error %v, wanted %v" , req , gErr , tc . err )
}
2020-08-20 08:20:26 -07:00
2020-09-23 08:10:27 -07:00
if tc . err == nil {
2020-08-20 08:20:26 -07:00
defer resetHost ()
}
if got != nil {
assert . NotNilf ( tc . res , "Expected UpdateHost response to be nil, but was %v" , got )
2021-04-23 09:13:37 -05:00
gotUpdateTime := got . GetItem (). GetUpdatedTime (). AsTime ()
2020-08-20 08:20:26 -07:00
// Verify it is a set updated after it was created
// TODO: This is currently failing.
assert . True ( gotUpdateTime . After ( hCreated ), "Updated set should have been updated after it's creation. Was updated %v, which is after %v" , gotUpdateTime , hCreated )
// Clear all values which are hard to compare against.
got . Item . UpdatedTime , tc . res . Item . UpdatedTime = nil , nil
}
if tc . res != nil {
tc . res . Item . Version = version + 1
}
assert . Empty ( cmp . Diff ( got , tc . res , protocmp . Transform ()), "UpdateHost(%q) got response %q, wanted %q" , req , got , tc . res )
})
}
}