@@ -381,3 +381,168 @@ def test_relations_validation_fails_if_schemas_is_wrong_type():
381381def test_relations_validation_fails_if_relkind_is_wrong_type ():
382382 with pytest .raises (ConfigurationError ):
383383 RelationsManager .validate_relations_config ([{"relation_name" : "person" , "relkind" : "foo" }])
384+
385+
386+ def test_autodiscovery_dbname_defaults_to_global_view_db (mock_check ):
387+ # When autodiscovery is enabled and dbname is not explicitly set,
388+ # dbname should default to global_view_db
389+ instance = {
390+ 'host' : 'localhost' ,
391+ 'port' : 5432 ,
392+ 'username' : 'testuser' ,
393+ 'password' : 'testpass' ,
394+ 'database_autodiscovery' : {
395+ 'enabled' : True ,
396+ 'global_view_db' : 'main' ,
397+ },
398+ }
399+ mock_check .instance = instance
400+ mock_check .init_config = {}
401+ config , result = build_config (check = mock_check )
402+ assert result .valid
403+ assert config .dbname == 'main'
404+
405+
406+ def test_autodiscovery_dbname_respects_explicit_value (mock_check ):
407+ # When autodiscovery is enabled and dbname IS explicitly set,
408+ # the explicit value should be used, even if it's in the exclude list
409+ # This allows users to connect to a database for global operations
410+ # while excluding it from per-database metric collection
411+ instance = {
412+ 'host' : 'localhost' ,
413+ 'port' : 5432 ,
414+ 'username' : 'testuser' ,
415+ 'password' : 'testpass' ,
416+ 'dbname' : 'postgres' ,
417+ 'database_autodiscovery' : {
418+ 'enabled' : True ,
419+ 'global_view_db' : 'postgres' ,
420+ 'exclude' : ['postgres' ],
421+ },
422+ }
423+ mock_check .instance = instance
424+ mock_check .init_config = {}
425+ config , result = build_config (check = mock_check )
426+ # Should be valid - user explicitly set dbname, so they know what they're doing
427+ assert result .valid
428+ assert config .dbname == 'postgres'
429+
430+
431+ def test_autodiscovery_excluded_default_dbname_fails (mock_check ):
432+ # When autodiscovery is enabled, default dbname postgres is used,
433+ # and postgres is in the exclude list, should fail validation
434+ instance = {
435+ 'host' : 'localhost' ,
436+ 'port' : 5432 ,
437+ 'username' : 'testuser' ,
438+ 'password' : 'testpass' ,
439+ 'database_autodiscovery' : {
440+ 'enabled' : True ,
441+ 'exclude' : ['postgres' ],
442+ },
443+ }
444+ mock_check .instance = instance
445+ mock_check .init_config = {}
446+ config , result = build_config (check = mock_check )
447+ assert not result .valid
448+ assert any ('is excluded by autodiscovery pattern' in str (e ) for e in result .errors )
449+ assert any ('global_view_db' in str (e ) for e in result .errors )
450+
451+
452+ def test_autodiscovery_with_regex_exclude_pattern (mock_check ):
453+ # Test that regex patterns work correctly in exclude validation
454+ instance = {
455+ 'host' : 'localhost' ,
456+ 'port' : 5432 ,
457+ 'username' : 'testuser' ,
458+ 'password' : 'testpass' ,
459+ 'database_autodiscovery' : {
460+ 'enabled' : True ,
461+ 'global_view_db' : 'test_main' ,
462+ 'exclude' : ['test_.*' ],
463+ },
464+ }
465+ mock_check .instance = instance
466+ mock_check .init_config = {}
467+ config , result = build_config (check = mock_check )
468+ assert not result .valid
469+ assert any ('is excluded by autodiscovery pattern' in str (e ) for e in result .errors )
470+
471+
472+ def test_autodiscovery_with_non_excluded_global_view_db_succeeds (mock_check ):
473+ # When autodiscovery is enabled with exclude patterns,
474+ # but global_view_db doesn't match any of them, should succeed
475+ instance = {
476+ 'host' : 'localhost' ,
477+ 'port' : 5432 ,
478+ 'username' : 'testuser' ,
479+ 'password' : 'testpass' ,
480+ 'database_autodiscovery' : {
481+ 'enabled' : True ,
482+ 'global_view_db' : 'production' ,
483+ 'exclude' : ['postgres' , 'test_.*' ],
484+ },
485+ }
486+ mock_check .instance = instance
487+ mock_check .init_config = {}
488+ config , result = build_config (check = mock_check )
489+ assert result .valid
490+ assert config .dbname == 'production'
491+
492+
493+ def test_autodiscovery_case_sensitive_exclude_matching (mock_check ):
494+ instance = {
495+ 'host' : 'localhost' ,
496+ 'port' : 5432 ,
497+ 'username' : 'testuser' ,
498+ 'password' : 'testpass' ,
499+ 'database_autodiscovery' : {
500+ 'enabled' : True ,
501+ 'global_view_db' : 'POSTGRES' ,
502+ 'exclude' : ['postgres' ],
503+ },
504+ }
505+ mock_check .instance = instance
506+ mock_check .init_config = {}
507+ config , result = build_config (check = mock_check )
508+ # Should be valid - case-sensitive matching means POSTGRES != postgres
509+ assert result .valid
510+ assert config .dbname == 'POSTGRES'
511+
512+
513+ def test_autodiscovery_invalid_regex_pattern_warns (mock_check ):
514+ # Invalid regex patterns should generate warnings
515+ instance = {
516+ 'host' : 'localhost' ,
517+ 'port' : 5432 ,
518+ 'username' : 'testuser' ,
519+ 'password' : 'testpass' ,
520+ 'database_autodiscovery' : {
521+ 'enabled' : True ,
522+ 'global_view_db' : 'main' ,
523+ 'exclude' : ['[invalid' ], # Invalid regex
524+ },
525+ }
526+ mock_check .instance = instance
527+ mock_check .init_config = {}
528+ config , result = build_config (check = mock_check )
529+ # Should have a warning about invalid regex
530+ assert any ('Invalid regex pattern' in w for w in result .warnings )
531+
532+
533+ def test_autodiscovery_exclude_none_does_not_error (mock_check ):
534+ instance = {
535+ 'host' : 'localhost' ,
536+ 'port' : 5432 ,
537+ 'username' : 'testuser' ,
538+ 'password' : 'testpass' ,
539+ 'database_autodiscovery' : {
540+ 'enabled' : True ,
541+ 'global_view_db' : 'main' ,
542+ },
543+ }
544+ mock_check .instance = instance
545+ mock_check .init_config = {}
546+ config , result = build_config (check = mock_check )
547+ assert result .valid
548+ assert config .dbname == 'main'
0 commit comments