Debian Codegraph @

src/test/modules/test_extensions/t/001_extension_control_path.pl

raw
1 # Copyright (c) 2024-2025, PostgreSQL Global Development Group
2
3 use strict;
4 use warnings FATAL => 'all';
5 use File::Path qw(mkpath);
6 use PostgreSQL::Test::Cluster;
7 use PostgreSQL::Test::Utils;
8 use Test::More;
9
10 my $node = PostgreSQL::Test::Cluster->new('node');
11
12 $node->init;
13
14 # Create temporary directories for the extension control files
15 my $ext_dir = PostgreSQL::Test::Utils::tempdir();
16 mkpath("$ext_dir/extension");
17 my $ext_dir2 = PostgreSQL::Test::Utils::tempdir();
18 mkpath("$ext_dir2/extension");
19
20 my $ext_name = "test_custom_ext_paths";
21 create_extension($ext_name, $ext_dir);
22 create_extension($ext_name, $ext_dir2);
23
24 my $ext_name2 = "test_custom_ext_paths_using_directory";
25 mkpath("$ext_dir/$ext_name2");
26 create_extension($ext_name2, $ext_dir, $ext_name2);
27
28 # Use the correct separator and escape \ when running on Windows.
29 my $sep = $windows_os ? ";" : ":";
30 $node->append_conf(
31 'postgresql.conf', qq{
32 extension_control_path = '\$system$sep@{[ $windows_os ? ($ext_dir =~ s/\\/\\\\/gr) : $ext_dir ]}$sep@{[ $windows_os ? ($ext_dir2 =~ s/\\/\\\\/gr) : $ext_dir2 ]}'
33 });
34
35 # Start node
36 $node->start;
37
38 my $ecp = $node->safe_psql('postgres', 'show extension_control_path;');
39
40 is($ecp, "\$system$sep$ext_dir$sep$ext_dir2",
41 "custom extension control directory path configured");
42
43 $node->safe_psql('postgres', "CREATE EXTENSION $ext_name");
44 $node->safe_psql('postgres', "CREATE EXTENSION $ext_name2");
45
46 my $ret = $node->safe_psql('postgres',
47 "select * from pg_available_extensions where name = '$ext_name'");
48 is( $ret,
49 "test_custom_ext_paths|1.0|1.0|Test extension_control_path",
50 "extension is installed correctly on pg_available_extensions");
51
52 $ret = $node->safe_psql('postgres',
53 "select * from pg_available_extension_versions where name = '$ext_name'");
54 is( $ret,
55 "test_custom_ext_paths|1.0|t|t|f|t|||Test extension_control_path",
56 "extension is installed correctly on pg_available_extension_versions");
57
58 $ret = $node->safe_psql('postgres',
59 "select * from pg_available_extensions where name = '$ext_name2'");
60 is( $ret,
61 "test_custom_ext_paths_using_directory|1.0|1.0|Test extension_control_path",
62 "extension is installed correctly on pg_available_extensions");
63
64 $ret = $node->safe_psql('postgres',
65 "select * from pg_available_extension_versions where name = '$ext_name2'"
66 );
67 is( $ret,
68 "test_custom_ext_paths_using_directory|1.0|t|t|f|t|||Test extension_control_path",
69 "extension is installed correctly on pg_available_extension_versions");
70
71 # Ensure that extensions installed on $system is still visible when using with
72 # custom extension control path.
73 $ret = $node->safe_psql('postgres',
74 "select count(*) > 0 as ok from pg_available_extensions where name = 'plpgsql'"
75 );
76 is($ret, "t",
77 "\$system extension is installed correctly on pg_available_extensions");
78
79
80 $ret = $node->safe_psql('postgres',
81 "set extension_control_path = ''; select count(*) > 0 as ok from pg_available_extensions where name = 'plpgsql'"
82 );
83 is($ret, "t",
84 "\$system extension is installed correctly on pg_available_extensions with empty extension_control_path"
85 );
86
87 # Test with an extension that does not exists
88 my ($code, $stdout, $stderr) =
89 $node->psql('postgres', "CREATE EXTENSION invalid");
90 is($code, 3, 'error to create an extension that does not exists');
91 like($stderr, qr/ERROR: extension "invalid" is not available/);
92
93 sub create_extension
94 {
95 my ($ext_name, $ext_dir, $directory) = @_;
96
97 my $control_file = "$ext_dir/extension/$ext_name.control";
98 my $sql_file;
99
100 if (defined $directory)
101 {
102 $sql_file = "$ext_dir/$directory/$ext_name--1.0.sql";
103 }
104 else
105 {
106 $sql_file = "$ext_dir/extension/$ext_name--1.0.sql";
107 }
108
109 # Create .control .sql file
110 open my $cf, '>', $control_file
111 or die "Could not create control file: $!";
112 print $cf "comment = 'Test extension_control_path'\n";
113 print $cf "default_version = '1.0'\n";
114 print $cf "relocatable = true\n";
115 print $cf "directory = $directory" if defined $directory;
116 close $cf;
117
118 # Create --1.0.sql file
119 open my $sqlf, '>', $sql_file or die "Could not create sql file: $!";
120 print $sqlf "/* $sql_file */\n";
121 print $sqlf
122 "-- complain if script is sourced in psql, rather than via CREATE EXTENSION\n";
123 print $sqlf
124 qq'\\echo Use "CREATE EXTENSION $ext_name" to load this file. \\quit\n';
125 close $sqlf;
126 }
127
128 done_testing();