@@ -916,4 +916,112 @@ var _ = Describe("PostgresUser Controller", func() {
916916 Expect (secret .Name ).To (Equal ("mysecret3" ))
917917 })
918918 })
919+
920+ Describe ("Cross-resource watching" , func () {
921+ var (
922+ postgresDB * dbv1alpha1.Postgres
923+ postgresUser1 * dbv1alpha1.PostgresUser
924+ postgresUser2 * dbv1alpha1.PostgresUser
925+ )
926+
927+ BeforeEach (func () {
928+ postgresDB = & dbv1alpha1.Postgres {
929+ ObjectMeta : metav1.ObjectMeta {
930+ Name : databaseName ,
931+ Namespace : namespace ,
932+ },
933+ Spec : dbv1alpha1.PostgresSpec {
934+ Database : databaseName ,
935+ },
936+ Status : dbv1alpha1.PostgresStatus {
937+ Succeeded : true ,
938+ Roles : dbv1alpha1.PostgresRoles {
939+ Owner : databaseName + "-group" ,
940+ Reader : databaseName + "-reader" ,
941+ Writer : databaseName + "-writer" ,
942+ },
943+ },
944+ }
945+
946+ postgresUser1 = & dbv1alpha1.PostgresUser {
947+ ObjectMeta : metav1.ObjectMeta {
948+ Name : "user-for-db" ,
949+ Namespace : namespace ,
950+ },
951+ Spec : dbv1alpha1.PostgresUserSpec {
952+ Database : databaseName ,
953+ SecretName : secretName ,
954+ Role : roleName ,
955+ Privileges : "WRITE" ,
956+ },
957+ }
958+
959+ postgresUser2 = & dbv1alpha1.PostgresUser {
960+ ObjectMeta : metav1.ObjectMeta {
961+ Name : "user-for-other-db" ,
962+ Namespace : namespace ,
963+ },
964+ Spec : dbv1alpha1.PostgresUserSpec {
965+ Database : "other-db" ,
966+ SecretName : secretName ,
967+ Role : roleName ,
968+ Privileges : "READ" ,
969+ },
970+ }
971+ })
972+
973+ Context ("findPostgresUsersForPostgres mapping function" , func () {
974+ It ("should return reconcile requests for PostgresUsers referencing the Postgres CR" , func () {
975+ // Create users first (without the Postgres CR)
976+ Expect (cl .Create (ctx , postgresUser1 )).To (Succeed ())
977+ Expect (cl .Create (ctx , postgresUser2 )).To (Succeed ())
978+
979+ // Call the mapping function
980+ requests := rp .findPostgresUsersForPostgres (ctx , postgresDB )
981+
982+ // Should only return the user that references our database
983+ Expect (requests ).To (HaveLen (1 ))
984+ Expect (requests [0 ].Name ).To (Equal ("user-for-db" ))
985+ Expect (requests [0 ].Namespace ).To (Equal (namespace ))
986+ })
987+
988+ It ("should return empty list when no PostgresUsers reference the Postgres CR" , func () {
989+ // Create a user that references a different database
990+ Expect (cl .Create (ctx , postgresUser2 )).To (Succeed ())
991+
992+ // Call the mapping function
993+ requests := rp .findPostgresUsersForPostgres (ctx , postgresDB )
994+
995+ // Should return empty list
996+ Expect (requests ).To (BeEmpty ())
997+ })
998+
999+ It ("should return multiple requests when multiple PostgresUsers reference the same Postgres CR" , func () {
1000+ // Create two users that reference the same database
1001+ Expect (cl .Create (ctx , postgresUser1 )).To (Succeed ())
1002+
1003+ anotherUser := & dbv1alpha1.PostgresUser {
1004+ ObjectMeta : metav1.ObjectMeta {
1005+ Name : "another-user-for-db" ,
1006+ Namespace : namespace ,
1007+ },
1008+ Spec : dbv1alpha1.PostgresUserSpec {
1009+ Database : databaseName ,
1010+ SecretName : "another-secret" ,
1011+ Role : "another-role" ,
1012+ Privileges : "READ" ,
1013+ },
1014+ }
1015+ Expect (cl .Create (ctx , anotherUser )).To (Succeed ())
1016+
1017+ // Call the mapping function
1018+ requests := rp .findPostgresUsersForPostgres (ctx , postgresDB )
1019+
1020+ // Should return both users
1021+ Expect (requests ).To (HaveLen (2 ))
1022+ names := []string {requests [0 ].Name , requests [1 ].Name }
1023+ Expect (names ).To (ContainElements ("user-for-db" , "another-user-for-db" ))
1024+ })
1025+ })
1026+ })
9191027})
0 commit comments