| 1 | package Test::DBIx::Class::SchemaManager::Trait::Testmysqld; | ||
| 2 | |||
| 3 | use Moose::Role; | ||
| 4 | use MooseX::Attribute::ENV; | ||
| 5 | use Test::mysqld; | ||
| 6 | use Test::More (); | ||
| 7 | use Path::Class qw(dir); | ||
| 8 | use Test::DBIx::Class::Types qw(ReplicantsConnectInfo); | ||
| 9 | use Socket; | ||
| 10 | use File::Temp qw(tempdir); | ||
| 11 | use File::Path qw(make_path); | ||
| 12 | |||
| 13 | requires 'setup', 'cleanup'; | ||
| 14 | |||
| 15 | ## has '+force_drop_table' => (is=>'rw',default=>1); | ||
| 16 | |||
| 17 | has [qw/base_dir mysql_install_db mysqld/] => ( | ||
| 18 | is=>'ro', | ||
| 19 | traits=>['ENV'], | ||
| 20 | ); | ||
| 21 | |||
| 22 | has test_db_manager => ( | ||
| 23 | is=>'ro', | ||
| 24 | init_arg=>undef, | ||
| 25 | lazy_build=>1, | ||
| 26 | ); | ||
| 27 | |||
| 28 | sub _build_test_db_manager { | ||
| 29 | my $self = shift @_; | ||
| 30 | my %config = $self->prepare_config(@_); | ||
| 31 | |||
| 32 | if($self->keep_db) { | ||
| 33 | $ENV{TEST_MYSQLD_PRESERVE} = 1; | ||
| 34 | } | ||
| 35 | |||
| 36 | if(my $testdb = $self->deploy_testdb(%config)) { | ||
| 37 | return $testdb; | ||
| 38 | } else { | ||
| 39 | die $Test::mysqld::errstr; | ||
| 40 | } | ||
| 41 | } | ||
| 42 | |||
| 43 | has default_cnf => ( | ||
| 44 | is=>'ro', | ||
| 45 | init_arg=>undef, | ||
| 46 | isa=>'HashRef', | ||
| 47 | auto_deref=>1, | ||
| 48 | lazy_build=>1, | ||
| 49 | ); | ||
| 50 | |||
| 51 | sub _build_default_cnf { | ||
| 52 | my $port = $_[0]->find_next_unused_port(); | ||
| 53 | return { | ||
| 54 | 'server-id'=>1, | ||
| 55 | 'log-bin'=>'mysql-bin', | ||
| 56 | 'binlog-do-db'=>'test', | ||
| 57 | 'port'=>$port, | ||
| 58 | }; | ||
| 59 | } | ||
| 60 | |||
| 61 | has port_to_try_first => ( | ||
| 62 | is=>'rw', | ||
| 63 | default=> sub { 8000 + int(rand(2000)) }, | ||
| 64 | ); | ||
| 65 | |||
| 66 | has my_cnf => ( | ||
| 67 | is=>'ro', | ||
| 68 | isa=>'HashRef', | ||
| 69 | auto_deref=>1, | ||
| 70 | ); | ||
| 71 | |||
| 72 | ## Replicant stuff... probably should be a delegate | ||
| 73 | |||
| 74 | has deployed_replicants => (is=>'rw', isa=>'ArrayRef', auto_deref=>1); | ||
| 75 | |||
| 76 | has replicants => ( | ||
| 77 | is=>'rw', | ||
| 78 | isa=>ReplicantsConnectInfo, | ||
| 79 | coerce=>1, | ||
| 80 | auto_deref=>1, | ||
| 81 | predicate=>'has_replicants', | ||
| 82 | ); | ||
| 83 | |||
| 84 | has pool_args => ( | ||
| 85 | is=>'ro', | ||
| 86 | isa=>'HashRef', | ||
| 87 | required=>0, | ||
| 88 | predicate=>'has_pool_args', | ||
| 89 | ); | ||
| 90 | |||
| 91 | has balancer_type => ( | ||
| 92 | is=>'ro', | ||
| 93 | isa=>'Str', | ||
| 94 | required=>1, | ||
| 95 | predicate=>'has_balancer_type', | ||
| 96 | default=>'::Random', | ||
| 97 | ); | ||
| 98 | |||
| 99 | has balancer_args => ( | ||
| 100 | is=>'ro', | ||
| 101 | isa=>'HashRef', | ||
| 102 | required=>1, | ||
| 103 | predicate=>'has_balancer_args', | ||
| 104 | default=> sub { | ||
| 105 | return { | ||
| 106 | auto_validate_every=>10, | ||
| 107 | master_read_weight => 1, | ||
| 108 | }, | ||
| 109 | }, | ||
| 110 | ); | ||
| 111 | |||
| 112 | has default_replicant_cnf => ( | ||
| 113 | is=>'ro', | ||
| 114 | init_arg=>undef, | ||
| 115 | isa=>'HashRef', | ||
| 116 | auto_deref=>1, | ||
| 117 | required=>1, | ||
| 118 | default=> sub { +{} }, | ||
| 119 | ); | ||
| 120 | |||
| 121 | has my_replicant_cnf => ( | ||
| 122 | is=>'ro', | ||
| 123 | isa=>'HashRef', | ||
| 124 | auto_deref=>1, | ||
| 125 | ); | ||
| 126 | |||
| 127 | sub prepare_replicant_config { | ||
| 128 | my ($self, $replicant, @replicants,%extra) = @_; | ||
| 129 | my %my_cnf_extra = $extra{my_cnf} ? delete $extra{my_cnf} : (); | ||
| 130 | my $port = $self->find_next_unused_port(); | ||
| 131 | my %config = ( | ||
| 132 | my_cnf => { | ||
| 133 | 'port'=>$port, | ||
| 134 | 'server-id'=>($replicant->{name}+2), | ||
| 135 | $self->default_replicant_cnf, | ||
| 136 | $self->my_replicant_cnf, | ||
| 137 | %my_cnf_extra, | ||
| 138 | }, | ||
| 139 | %extra, | ||
| 140 | ); | ||
| 141 | |||
| 142 | my $replicant_name =$replicant->{name}; | ||
| 143 | my $base_dir = $self->test_db_manager->base_dir . "/$port" .'_'. "$replicant_name"; | ||
| 144 | |||
| 145 | $config{base_dir} = $base_dir; | ||
| 146 | $config{mysql_install_db} = $self->mysql_install_db if $self->mysql_install_db; | ||
| 147 | $config{mysqld} = $self->mysqld if $self->mysqld; | ||
| 148 | |||
| 149 | return %config; | ||
| 150 | } | ||
| 151 | |||
| 152 | around 'prepare_schema_class' => sub { | ||
| 153 | my ($prepare_schema_class, $self, @args) = @_; | ||
| 154 | my $schema_class = $self->$prepare_schema_class(@args); | ||
| 155 | if($self->has_replicants) { | ||
| 156 | $schema_class->storage_type({ | ||
| 157 | '::DBI::Replicated' => { | ||
| 158 | pool_args => $self->has_pool_args ? $self->pool_args : {}, | ||
| 159 | balancer_type => $self->has_balancer_type ? $self->balancer_type : '', | ||
| 160 | balancer_args => $self->has_balancer_args ? $self->balancer_args : {}, | ||
| 161 | }, | ||
| 162 | }); | ||
| 163 | } | ||
| 164 | |||
| 165 | return $schema_class; | ||
| 166 | }; | ||
| 167 | |||
| 168 | around 'setup' => sub { | ||
| 169 | my ($setup, $self, @args) = @_; | ||
| 170 | |||
| 171 | return $self->$setup(@args) unless $self->has_replicants; | ||
| 172 | |||
| 173 | ## Do we need to invent replicants? | ||
| 174 | my @replicants = (); | ||
| 175 | my @deployed_replicants = (); | ||
| 176 | foreach my $replicant ($self->replicants) { | ||
| 177 | if($replicant->{dsn}) { | ||
| 178 | push @replicants, $replicant; | ||
| 179 | } else { | ||
| 180 | ## If there is no 'dsn' key, that means we should auto generate | ||
| 181 | ## a test db and request its connect info. | ||
| 182 | $replicant->{name} = defined $replicant->{name} ? $replicant->{name} : ($#replicants+1); | ||
| 183 | my %config = $self->prepare_replicant_config($replicant, @replicants); | ||
| 184 | my $deployed = $self->deploy_testdb(%config); | ||
| 185 | my $replicant_base_dir = $deployed->base_dir; | ||
| 186 | |||
| 187 | if ($self->tdbic_debug || ($self->keep_db && !$self->base_dir)){ | ||
| 188 | Test::More::diag( | ||
| 189 | "Starting replicant mysqld with: ". | ||
| 190 | $deployed->mysqld. | ||
| 191 | " --defaults-file=".$replicant_base_dir . '/etc/my.cnf'. | ||
| 192 | " --user=root" | ||
| 193 | ); | ||
| 194 | Test::More::diag("DBI->connect('DBI:mysql:test;mysql_socket=$replicant_base_dir/tmp/mysql.sock','root','')"); | ||
| 195 | } | ||
| 196 | |||
| 197 | push @deployed_replicants, $deployed; | ||
| 198 | push @replicants, | ||
| 199 | ["DBI:mysql:test;mysql_socket=$replicant_base_dir/tmp/mysql.sock",'root','']; | ||
| 200 | |||
| 201 | } | ||
| 202 | } | ||
| 203 | |||
| 204 | |||
| 205 | $self->deployed_replicants(\@deployed_replicants); | ||
| 206 | $self->replicants(\@replicants); | ||
| 207 | $self->schema->storage->connect_replicants($self->replicants); | ||
| 208 | $self->schema->storage->ensure_connected; | ||
| 209 | my $port = $self->default_cnf->{port}; ## TODO I doubt this is correct..... | ||
| 210 | |||
| 211 | foreach my $storage ($self->schema->storage->pool->all_replicant_storages) { | ||
| 212 | ## TODO, need to change this to dbh_do | ||
| 213 | my $dbh = $storage->_get_dbh; | ||
| 214 | $dbh->do("STOP SLAVE") || die $dbh->errst; | ||
| 215 | $dbh->do("CHANGE MASTER TO master_host='127.0.0.1', master_port=$port, master_user='root', master_password=''") | ||
| 216 | || die $dbh->errstr; | ||
| 217 | $dbh->do("START SLAVE") | ||
| 218 | || die $dbh->errstr; | ||
| 219 | } | ||
| 220 | |||
| 221 | return $self->$setup(@args); | ||
| 222 | }; | ||
| 223 | |||
| 224 | sub prepare_config { | ||
| 225 | my ($self, %extra) = @_; | ||
| 226 | my %my_cnf_extra = $extra{my_cnf} ? delete $extra{my_cnf} : (); | ||
| 227 | my %config = ( | ||
| 228 | my_cnf => { | ||
| 229 | $self->default_cnf, | ||
| 230 | $self->my_cnf, | ||
| 231 | %my_cnf_extra, | ||
| 232 | }, | ||
| 233 | %extra, | ||
| 234 | ); | ||
| 235 | my $port = $self->default_cnf->{port}; | ||
| 236 | my $cleanup = ($ENV{TEST_MYSQLD_PRESERVE} || $self->keep_db) ? undef : 1; | ||
| 237 | $config{base_dir} = $self->base_dir ? $self->base_dir ."/$port" : tempdir(CLEANUP => $cleanup) . "/$port"; | ||
| 238 | make_path($config{base_dir}) unless -e $config{base_dir}; | ||
| 239 | $config{mysql_install_db} = $self->mysql_install_db if $self->mysql_install_db; | ||
| 240 | $config{mysqld} = $self->mysqld if $self->mysqld; | ||
| 241 | |||
| 242 | return %config; | ||
| 243 | } | ||
| 244 | |||
| 245 | sub deploy_testdb { | ||
| 246 | my ($self, %config) = @_; | ||
| 247 | return Test::mysqld->new(%config); | ||
| 248 | } | ||
| 249 | |||
| 250 | sub get_default_connect_info { | ||
| 251 | my $self = shift @_; | ||
| 252 | my $deployed_db = shift(@_) || $self->test_db_manager; | ||
| 253 | my $base_dir = $deployed_db->base_dir; | ||
| 254 | |||
| 255 | if ($self->tdbic_debug || ($self->keep_db && !$self->base_dir)){ | ||
| 256 | Test::More::diag( | ||
| 257 | "Starting mysqld with: ". | ||
| 258 | $deployed_db->mysqld. | ||
| 259 | " --defaults-file=".$base_dir . '/etc/my.cnf'. | ||
| 260 | " --user=root" | ||
| 261 | ); | ||
| 262 | |||
| 263 | Test::More::diag("DBI->connect('DBI:mysql:test;mysql_socket=$base_dir/tmp/mysql.sock','root','')"); | ||
| 264 | } | ||
| 265 | |||
| 266 | return ["DBI:mysql:test;mysql_socket=$base_dir/tmp/mysql.sock",'root','']; | ||
| 267 | } | ||
| 268 | |||
| 269 | after 'cleanup' => sub { | ||
| 270 | my ($self) = @_; | ||
| 271 | unless($self->keep_db) { | ||
| 272 | if($self->base_dir) { | ||
| 273 | my $dir = dir($self->base_dir); | ||
| 274 | $dir->rmtree; | ||
| 275 | } | ||
| 276 | } | ||
| 277 | }; | ||
| 278 | |||
| 279 | sub is_port_open { | ||
| 280 | my ($port) = @_; | ||
| 281 | my ($host, $iaddr, $paddr, $proto); | ||
| 282 | |||
| 283 | $host = '127.0.0.1'; | ||
| 284 | $iaddr = inet_aton($host) | ||
| 285 | or die "no host: $host"; | ||
| 286 | $paddr = sockaddr_in($port, $iaddr); | ||
| 287 | |||
| 288 | $proto = getprotobyname('tcp'); | ||
| 289 | socket(SOCK, PF_INET, SOCK_STREAM, $proto) | ||
| 290 | or die "error creating test socket for port $port: $!"; | ||
| 291 | if (connect(SOCK, $paddr)) { | ||
| 292 | close (SOCK) | ||
| 293 | or die "error closing test socket: $!"; | ||
| 294 | return 1; | ||
| 295 | } | ||
| 296 | return 0; | ||
| 297 | } | ||
| 298 | |||
| 299 | our $next_port; | ||
| 300 | sub find_next_unused_port { | ||
| 301 | $next_port ||= $_[0]->port_to_try_first; | ||
| 302 | my $port = $next_port; | ||
| 303 | while (is_port_open($port)) { | ||
| 304 | $port++; | ||
| 305 | if ($port > 0xFFF0) { | ||
| 306 | die "no ports available\n"; | ||
| 307 | } | ||
| 308 | } | ||
| 309 | ++$next_port; | ||
| 310 | return $port; | ||
| 311 | } | ||
| 312 | |||
| 313 | 1; | ||
| 314 | |||
| 315 | __END__ |