@ -290,8 +290,8 @@ func TestLegacyResourceResourceMigrator_Write(t *testing.T) {
t . Run ( "records error when create resource file fails" , func ( t * testing . T ) {
mockParser := resources . NewMockParser ( t )
obj := & unstructured . Unstructured {
Object : map [ string ] interface { } {
"metadata" : map [ string ] interface { } {
Object : map [ string ] any {
"metadata" : map [ string ] any {
"name" : "test" ,
} ,
} ,
@ -306,7 +306,7 @@ func TestLegacyResourceResourceMigrator_Write(t *testing.T) {
} , nil )
mockRepoResources := resources . NewMockRepositoryResources ( t )
mockRepoResources . On ( "Crea teResourceFileFromObject" , mock . Anything , mock . Anything , mock . Anything ) .
mockRepoResources . On ( "Wri teResourceFileFromObject" , mock . Anything , mock . Anything , mock . Anything ) .
Return ( "" , errors . New ( "create file error" ) )
progress := jobs . NewMockJobProgressRecorder ( t )
@ -340,8 +340,8 @@ func TestLegacyResourceResourceMigrator_Write(t *testing.T) {
t . Run ( "should fail when signer fails" , func ( t * testing . T ) {
mockParser := resources . NewMockParser ( t )
obj := & unstructured . Unstructured {
Object : map [ string ] interface { } {
"metadata" : map [ string ] interface { } {
Object : map [ string ] any {
"metadata" : map [ string ] any {
"name" : "test" ,
} ,
} ,
@ -383,8 +383,8 @@ func TestLegacyResourceResourceMigrator_Write(t *testing.T) {
t . Run ( "should successfully add author signature" , func ( t * testing . T ) {
mockParser := resources . NewMockParser ( t )
obj := & unstructured . Unstructured {
Object : map [ string ] interface { } {
"metadata" : map [ string ] interface { } {
Object : map [ string ] any {
"metadata" : map [ string ] any {
"name" : "test" ,
} ,
} ,
@ -407,7 +407,7 @@ func TestLegacyResourceResourceMigrator_Write(t *testing.T) {
Return ( signedCtx , nil )
mockRepoResources := resources . NewMockRepositoryResources ( t )
mockRepoResources . On ( "Crea teResourceFileFromObject" , signedCtx , mock . Anything , mock . Anything ) .
mockRepoResources . On ( "Wri teResourceFileFromObject" , signedCtx , mock . Anything , mock . Anything ) .
Return ( "test/path" , nil )
progress := jobs . NewMockJobProgressRecorder ( t )
@ -439,11 +439,66 @@ func TestLegacyResourceResourceMigrator_Write(t *testing.T) {
progress . AssertExpectations ( t )
} )
t . Run ( "should maintain history" , func ( t * testing . T ) {
progress := jobs . NewMockJobProgressRecorder ( t )
progress . On ( "Record" , mock . Anything , mock . Anything ) . Return ( )
progress . On ( "TooManyErrors" ) . Return ( nil )
mockParser := resources . NewMockParser ( t )
obj := & unstructured . Unstructured {
Object : map [ string ] any {
"metadata" : map [ string ] any {
"name" : "test" ,
} ,
} ,
}
meta , _ := utils . MetaAccessor ( obj )
mockParser . On ( "Parse" , mock . Anything , mock . Anything ) .
Return ( & resources . ParsedResource {
Meta : meta ,
Obj : obj ,
} , nil )
mockRepoResources := resources . NewMockRepositoryResources ( t )
writeResourceFileFromObject := mockRepoResources . On ( "WriteResourceFileFromObject" , mock . Anything , mock . Anything , mock . Anything )
migrator := NewLegacyResourceMigrator (
nil ,
mockParser ,
mockRepoResources ,
progress ,
provisioning . MigrateJobOptions {
History : true ,
} ,
"test-namespace" ,
schema . GroupResource { Group : "test.grafana.app" , Resource : "tests" } ,
signature . NewGrafanaSigner ( ) ,
)
writeResourceFileFromObject . Return ( "aaaa.json" , nil )
err := migrator . Write ( context . Background ( ) , & resource . ResourceKey { } , [ ] byte ( "" ) )
require . NoError ( t , err )
require . Equal ( t , "aaaa.json" , migrator . history [ "test" ] , "kept track of the old files" )
// Change the result file name
writeResourceFileFromObject . Return ( "bbbb.json" , nil )
mockRepoResources . On ( "RemoveResourceFromFile" , mock . Anything , "aaaa.json" , "" ) .
Return ( "" , schema . GroupVersionKind { } , nil ) . Once ( )
err = migrator . Write ( context . Background ( ) , & resource . ResourceKey { } , [ ] byte ( "" ) )
require . NoError ( t , err )
require . Equal ( t , "bbbb.json" , migrator . history [ "test" ] , "kept track of the old files" )
mockParser . AssertExpectations ( t )
mockRepoResources . AssertExpectations ( t )
progress . AssertExpectations ( t )
} )
t . Run ( "should successfully write resource" , func ( t * testing . T ) {
mockParser := resources . NewMockParser ( t )
obj := & unstructured . Unstructured {
Object : map [ string ] interface { } {
"metadata" : map [ string ] interface { } {
Object : map [ string ] any {
"metadata" : map [ string ] any {
"name" : "test" ,
} ,
} ,
@ -471,7 +526,7 @@ func TestLegacyResourceResourceMigrator_Write(t *testing.T) {
} , nil )
mockRepoResources := resources . NewMockRepositoryResources ( t )
mockRepoResources . On ( "Crea teResourceFileFromObject" , mock . Anything , mock . MatchedBy ( func ( obj * unstructured . Unstructured ) bool {
mockRepoResources . On ( "Wri teResourceFileFromObject" , mock . Anything , mock . MatchedBy ( func ( obj * unstructured . Unstructured ) bool {
if obj == nil {
return false
}
@ -524,8 +579,8 @@ func TestLegacyResourceResourceMigrator_Write(t *testing.T) {
t . Run ( "should fail when too many errors" , func ( t * testing . T ) {
mockParser := resources . NewMockParser ( t )
obj := & unstructured . Unstructured {
Object : map [ string ] interface { } {
"metadata" : map [ string ] interface { } {
Object : map [ string ] any {
"metadata" : map [ string ] any {
"name" : "test" ,
} ,
} ,
@ -540,7 +595,7 @@ func TestLegacyResourceResourceMigrator_Write(t *testing.T) {
} , nil )
mockRepoResources := resources . NewMockRepositoryResources ( t )
mockRepoResources . On ( "Crea teResourceFileFromObject" , mock . Anything , mock . Anything , resources . WriteOptions { } ) .
mockRepoResources . On ( "Wri teResourceFileFromObject" , mock . Anything , mock . Anything , resources . WriteOptions { } ) .
Return ( "test/path" , nil )
progress := jobs . NewMockJobProgressRecorder ( t )