@ -1,6 +1,5 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: BUSL-1.1
package server
import (
@ -14,6 +13,10 @@ import (
"github.com/stretchr/testify/require"
)
const (
removeControllerSql = ` delete from server_controller where private_id = $1 `
)
func TestRepository_UpsertController ( t * testing . T ) {
ctx := context . Background ( )
conn , _ := db . TestSetup ( t , "postgres" )
@ -22,10 +25,8 @@ func TestRepository_UpsertController(t *testing.T) {
testKms := kms . TestKms ( t , conn , wrapper )
testRepo , err := NewRepository ( ctx , rw , rw , testKms )
require . NoError ( t , err )
iamRepo := iam . TestRepo ( t , conn , wrapper )
iam . TestScopes ( t , iamRepo )
tests := [ ] struct {
name string
controller * Controller
@ -81,3 +82,147 @@ func TestRepository_UpsertController(t *testing.T) {
} )
}
}
func TestRepository_UpdateControllerStatus ( t * testing . T ) {
ctx := context . Background ( )
conn , _ := db . TestSetup ( t , "postgres" )
rw := db . New ( conn )
wrapper := db . TestWrapper ( t )
testKms := kms . TestKms ( t , conn , wrapper )
testRepo , err := NewRepository ( ctx , rw , rw , testKms )
require . NoError ( t , err )
iamRepo := iam . TestRepo ( t , conn , wrapper )
iam . TestScopes ( t , iamRepo )
tests := map [ string ] struct {
originalController * Controller
updatedController * Controller
wantCount int
wantErr bool
cleanUpFunc func ( t * testing . T , rw * db . Db , privateId string )
} {
"nil-controller" : {
wantErr : true ,
} ,
"empty-id" : {
updatedController : NewController ( "" , WithAddress ( "127.0.0.1" ) ) ,
wantErr : true ,
} ,
"empty-address" : {
updatedController : NewController ( "test-controller" ) ,
wantErr : true ,
} ,
"controller-not-found" : {
updatedController : NewController ( "test-controller" , WithAddress ( "127.0.0.1" ) , WithDescription ( "new ipv4 description" ) ) ,
wantErr : true ,
} ,
"valid-ipv4-controller" : {
originalController : NewController ( "ipv4-controller" , WithAddress ( "127.0.0.1" ) , WithDescription ( "ipv4 description" ) ) ,
updatedController : NewController ( "ipv4-controller" , WithAddress ( "127.0.0.2" ) , WithDescription ( "new ipv4 description" ) ) ,
wantCount : 1 ,
cleanUpFunc : func ( t * testing . T , rw * db . Db , privateId string ) {
t . Helper ( )
c , err := rw . Exec ( ctx , removeControllerSql , [ ] any { privateId } )
require . NoError ( t , err )
require . Equal ( t , 1 , c )
} ,
} ,
"valid-ipv6-controller" : {
originalController : NewController ( "test-ipv6-controller" , WithAddress ( "[2001:4860:4860:0:0:0:0:8888]" ) , WithDescription ( "ipv6 description" ) ) ,
updatedController : NewController ( "test-ipv6-controller" , WithAddress ( "[2001:4860:4860:0:0:0:0:9999]" ) , WithDescription ( "new ipv6 description" ) ) ,
wantCount : 1 ,
cleanUpFunc : func ( t * testing . T , rw * db . Db , privateId string ) {
t . Helper ( )
c , err := rw . Exec ( ctx , removeControllerSql , [ ] any { privateId } )
require . NoError ( t , err )
require . Equal ( t , 1 , c )
} ,
} ,
"valid-abbreviated-ipv6-controller" : {
originalController : NewController ( "test-abbreviated-ipv6-controller" , WithAddress ( "[2001:4860:4860::8888]" ) , WithDescription ( "abbreviated ipv6 description" ) ) ,
updatedController : NewController ( "test-abbreviated-ipv6-controller" , WithAddress ( "[2001:4860:4860::9999]" ) , WithDescription ( "new abbreviated ipv6 description" ) ) ,
wantCount : 1 ,
cleanUpFunc : func ( t * testing . T , rw * db . Db , privateId string ) {
t . Helper ( )
c , err := rw . Exec ( ctx , removeControllerSql , [ ] any { privateId } )
require . NoError ( t , err )
require . Equal ( t , 1 , c )
} ,
} ,
"valid-controller-short-name" : {
originalController : NewController ( "test" , WithAddress ( "127.0.0.1" ) , WithDescription ( "short name description" ) ) ,
updatedController : NewController ( "test" , WithAddress ( "127.0.0.2" ) , WithDescription ( "new short name description" ) ) ,
wantCount : 1 ,
cleanUpFunc : func ( t * testing . T , rw * db . Db , privateId string ) {
t . Helper ( )
c , err := rw . Exec ( ctx , removeControllerSql , [ ] any { privateId } )
require . NoError ( t , err )
require . Equal ( t , 1 , c )
} ,
} ,
// Test case for updating a controller with the same attributes validating update_time is updated
"duplicate-ipv4-controller-update" : {
originalController : NewController ( "ipv4-controller" , WithAddress ( "127.0.0.1" ) , WithDescription ( "new ipv4 description" ) ) ,
updatedController : NewController ( "ipv4-controller" , WithAddress ( "127.0.0.1" ) , WithDescription ( "new ipv4 description" ) ) ,
wantCount : 1 ,
cleanUpFunc : func ( t * testing . T , rw * db . Db , privateId string ) {
t . Helper ( )
c , err := rw . Exec ( ctx , removeControllerSql , [ ] any { privateId } )
require . NoError ( t , err )
require . Equal ( t , 1 , c )
} ,
} ,
"duplicate-ipv6-controller-update" : {
originalController : NewController ( "test-ipv6-controller" , WithAddress ( "[2001:4860:4860:0:0:0:0:8888]" ) , WithDescription ( "ipv6 description" ) ) ,
updatedController : NewController ( "test-ipv6-controller" , WithAddress ( "[2001:4860:4860:0:0:0:0:8888]" ) , WithDescription ( "ipv6 description" ) ) ,
wantCount : 1 ,
cleanUpFunc : func ( t * testing . T , rw * db . Db , privateId string ) {
t . Helper ( )
c , err := rw . Exec ( ctx , removeControllerSql , [ ] any { privateId } )
require . NoError ( t , err )
require . Equal ( t , 1 , c )
} ,
} ,
}
for name , tt := range tests {
t . Run ( name , func ( t * testing . T ) {
assert , require := assert . New ( t ) , require . New ( t )
var originalControllerEntry * Controller
if tt . originalController != nil {
_ , err := testRepo . UpsertController ( ctx , tt . originalController )
require . NoError ( err )
controllerList , err := testRepo . ListControllers ( ctx , [ ] Option { } ... )
require . NoError ( err )
originalControllerEntry = controllerList [ 0 ]
}
got , err := testRepo . UpdateControllerStatus ( ctx , tt . updatedController )
if tt . wantErr {
require . Error ( err )
assert . Equal ( 0 , got )
if tt . cleanUpFunc != nil {
tt . cleanUpFunc ( t , rw , tt . updatedController . PrivateId )
}
return
}
require . NoError ( err )
assert . Equal ( tt . wantCount , got )
controllerList , err := testRepo . ListControllers ( ctx , [ ] Option { } ... )
require . NoError ( err )
require . Len ( controllerList , 1 )
updatedControllerEntry := controllerList [ 0 ]
assert . Equal ( tt . updatedController . PrivateId , updatedControllerEntry . PrivateId )
assert . Equal ( tt . updatedController . Address , updatedControllerEntry . Address )
assert . Equal ( tt . updatedController . Description , updatedControllerEntry . Description )
assert . True ( updatedControllerEntry . UpdateTime . AsTime ( ) . After ( originalControllerEntry . UpdateTime . AsTime ( ) ) )
tt . cleanUpFunc ( t , rw , tt . updatedController . PrivateId )
} )
}
}