aptible-cli 0.26.4 → 0.26.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (31) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile.lock +1 -1
  3. data/lib/aptible/cli/helpers/app.rb +18 -16
  4. data/lib/aptible/cli/helpers/database.rb +20 -19
  5. data/lib/aptible/cli/helpers/environment.rb +5 -4
  6. data/lib/aptible/cli/helpers/vhost/option_set_builder.rb +154 -1
  7. data/lib/aptible/cli/helpers/vhost.rb +5 -2
  8. data/lib/aptible/cli/renderer/text.rb +3 -1
  9. data/lib/aptible/cli/resource_formatter.rb +6 -0
  10. data/lib/aptible/cli/subcommands/backup.rb +1 -1
  11. data/lib/aptible/cli/subcommands/endpoints.rb +27 -9
  12. data/lib/aptible/cli/version.rb +1 -1
  13. data/spec/aptible/cli/helpers/vhost/option_set_builder_spec.rb +94 -0
  14. data/spec/aptible/cli/resource_formatter_spec.rb +3 -0
  15. data/spec/aptible/cli/subcommands/apps_spec.rb +34 -21
  16. data/spec/aptible/cli/subcommands/backup_retention_policy_spec.rb +3 -1
  17. data/spec/aptible/cli/subcommands/backup_spec.rb +28 -3
  18. data/spec/aptible/cli/subcommands/config_spec.rb +3 -3
  19. data/spec/aptible/cli/subcommands/db_spec.rb +46 -44
  20. data/spec/aptible/cli/subcommands/deploy_spec.rb +8 -3
  21. data/spec/aptible/cli/subcommands/endpoints_spec.rb +198 -8
  22. data/spec/aptible/cli/subcommands/environment_spec.rb +4 -0
  23. data/spec/aptible/cli/subcommands/log_drain_spec.rb +9 -0
  24. data/spec/aptible/cli/subcommands/logs_spec.rb +16 -4
  25. data/spec/aptible/cli/subcommands/maintenance_spec.rb +7 -2
  26. data/spec/aptible/cli/subcommands/metric_drain_spec.rb +10 -2
  27. data/spec/aptible/cli/subcommands/services_spec.rb +3 -4
  28. data/spec/fabricators/setting_fabricator.rb +8 -0
  29. data/spec/fabricators/vhost_fabricator.rb +2 -0
  30. data/spec/spec_helper.rb +2 -0
  31. metadata +6 -2
@@ -20,13 +20,17 @@ describe Aptible::CLI::Agent do
20
20
  before do
21
21
  allow(subject).to receive(:save_token)
22
22
  allow(subject).to receive(:attach_to_operation_logs)
23
- allow(subject).to receive(:fetch_token) { double 'token' }
23
+ allow(subject).to receive(:fetch_token) { token }
24
+ allow(Aptible::Api::Account).to receive(:find_by_url)
25
+ .with("/find/account?handle=#{account.handle}", token: token)
26
+ .and_return(account)
24
27
  end
25
28
 
26
29
  let!(:account) { Fabricate(:account) }
27
30
  let!(:app) { Fabricate(:app, handle: 'hello', account: account) }
28
31
  let!(:service) { Fabricate(:service, app: app, process_type: 'web') }
29
32
  let(:op) { Fabricate(:operation, status: 'succeeded', resource: app) }
33
+ let(:token) { double 'token' }
30
34
 
31
35
  describe '#apps' do
32
36
  it 'lists an app in an account' do
@@ -83,13 +87,14 @@ describe Aptible::CLI::Agent do
83
87
 
84
88
  it 'lists filters down to one account' do
85
89
  account2 = Fabricate(:account, handle: 'account2')
90
+ allow(Aptible::Api::Account).to receive(:find_by_url)
91
+ .with("/find/account?handle=#{account2.handle}", token: token)
92
+ .and_return(account2)
86
93
  app2 = Fabricate(:app, account: account2, handle: 'app2')
87
94
  allow(subject).to receive(:options)
88
95
  .and_return(environment: account2.handle)
89
96
  allow(Aptible::Api::App).to receive(:all).and_return([app2])
90
97
 
91
- allow(Aptible::Api::Account).to receive(:all)
92
- .and_return([account, account2])
93
98
  subject.send('apps')
94
99
 
95
100
  expect(captured_output_text)
@@ -215,8 +220,11 @@ describe Aptible::CLI::Agent do
215
220
 
216
221
  describe '#apps:rename' do
217
222
  before do
218
- allow(Aptible::Api::App).to receive(:all) { [app] }
219
- allow(Aptible::Api::Account).to receive(:all) { [account] }
223
+ allow(Aptible::Api::App).to receive(:find_by_url)
224
+ .and_return(nil)
225
+ allow(Aptible::Api::App).to receive(:find_by_url)
226
+ .with("/find/app?handle=#{app.handle}&environment=#{account.handle}", token: token)
227
+ .and_return(app)
220
228
  end
221
229
 
222
230
  before(:each) do
@@ -256,7 +264,6 @@ describe Aptible::CLI::Agent do
256
264
 
257
265
  describe '#apps:scale' do
258
266
  before do
259
- allow(Aptible::Api::App).to receive(:all) { [app] }
260
267
  allow(Aptible::Api::Account).to receive(:all) { [account] }
261
268
  end
262
269
 
@@ -268,9 +275,9 @@ describe Aptible::CLI::Agent do
268
275
  .with('foobar')
269
276
  .and_return(account)
270
277
 
271
- expect(subject).to receive(:apps_from_handle)
278
+ expect(subject).to receive(:app_from_handle)
272
279
  .with('hello', account)
273
- .and_return([app])
280
+ .and_return(app)
274
281
  end
275
282
 
276
283
  def stub_options(**opts)
@@ -330,7 +337,7 @@ describe Aptible::CLI::Agent do
330
337
  allow(subject).to receive(:options) do
331
338
  { environment: 'foo', app: 'web', container_count: 2 }
332
339
  end
333
- allow(Aptible::Api::Account).to receive(:all) { [] }
340
+ allow(Aptible::Api::Account).to receive(:find_by_url).and_return(nil)
334
341
  allow(service).to receive(:create_operation!) { op }
335
342
 
336
343
  expect do
@@ -394,7 +401,14 @@ describe Aptible::CLI::Agent do
394
401
 
395
402
  before do
396
403
  account.apps = apps
397
- allow(Aptible::Api::App).to receive(:all).and_return(apps)
404
+ allow(Aptible::Api::App).to receive(:find_by_url)
405
+ .and_return(nil)
406
+ allow(Aptible::Api::App).to receive(:find_by_url)
407
+ .with("/find/app?handle=#{app.handle}&environment=#{account.handle}", token: token)
408
+ .and_return(app)
409
+ allow(Aptible::Api::App).to receive(:find_by_url)
410
+ .with("/find/app?handle=#{app.handle}", token: token)
411
+ .and_return(app)
398
412
  end
399
413
 
400
414
  it 'scopes the app search to an environment if provided' do
@@ -415,27 +429,21 @@ describe Aptible::CLI::Agent do
415
429
  end
416
430
 
417
431
  it 'fails if no app is found' do
418
- apps.pop
419
-
420
- strategies = [dummy_strategy_factory('hello', nil, true)]
432
+ strategies = [dummy_strategy_factory('goodbye', nil, true)]
421
433
  allow(subject).to receive(:handle_strategies) { strategies }
422
434
 
423
- expect { subject.ensure_app }.to raise_error(/not find app hello/)
435
+ expect { subject.ensure_app }.to raise_error(/not find app goodbye/)
424
436
  end
425
437
 
426
438
  it 'explains the strategy when it fails' do
427
- apps.pop
428
-
429
- strategies = [dummy_strategy_factory('hello', nil, true)]
439
+ strategies = [dummy_strategy_factory('goodbye', nil, true)]
430
440
  allow(subject).to receive(:handle_strategies) { strategies }
431
441
 
432
442
  expect { subject.ensure_app }.to raise_error(/from dummy/)
433
443
  end
434
444
 
435
445
  it 'indicates the environment when the app search was scoped' do
436
- apps.pop
437
-
438
- strategies = [dummy_strategy_factory('hello', 'aptible', true)]
446
+ strategies = [dummy_strategy_factory('goodbye', 'aptible', true)]
439
447
  allow(subject).to receive(:handle_strategies) { strategies }
440
448
 
441
449
  expect(subject).to receive(:environment_from_handle).with('aptible')
@@ -445,7 +453,12 @@ describe Aptible::CLI::Agent do
445
453
  end
446
454
 
447
455
  it 'fails if multiple apps are found' do
448
- apps << Fabricate(:app, handle: 'hello')
456
+ error = HyperResource::ClientError.new('multiple resources found')
457
+ allow(error).to receive(:body)
458
+ .and_return('error' => 'multiple_resources_found')
459
+ allow(Aptible::Api::App).to receive(:find_by_url)
460
+ .with('/find/app?handle=hello', token: token)
461
+ .and_raise(error)
449
462
 
450
463
  strategies = [dummy_strategy_factory('hello', nil, true)]
451
464
  allow(subject).to receive(:handle_strategies) { strategies }
@@ -13,7 +13,9 @@ describe Aptible::CLI::Agent do
13
13
 
14
14
  before do
15
15
  allow(subject).to receive(:fetch_token).and_return(token)
16
- allow(Aptible::Api::Account).to receive(:all) { [account] }
16
+ allow(Aptible::Api::Account).to receive(:find_by_url)
17
+ .with("/find/account?handle=#{account.handle}", token: token)
18
+ .and_return(account)
17
19
  end
18
20
 
19
21
  describe '#backup_retention_policy' do
@@ -20,7 +20,24 @@ describe Aptible::CLI::Agent do
20
20
 
21
21
  before do
22
22
  allow(subject).to receive(:fetch_token).and_return(token)
23
- allow(Aptible::Api::Account).to receive(:all) { [account, alt_account] }
23
+ allow(Aptible::Api::Account).to receive(:find_by_url)
24
+ .with("/find/account?handle=#{account.handle}", token: token)
25
+ .and_return(account)
26
+ allow(Aptible::Api::Account).to receive(:find_by_url)
27
+ .with("/find/account?handle=#{alt_account.handle}", token: token)
28
+ .and_return(alt_account)
29
+ allow(Aptible::Api::Database).to receive(:find_by_url)
30
+ .with("/find/database?handle=#{default_handle}", token: token)
31
+ .and_return(database)
32
+ allow(Aptible::Api::Database).to receive(:find_by_url)
33
+ .with("/find/database?handle=#{default_handle}&environment=#{account.handle}", token: token)
34
+ .and_return(database)
35
+ allow(Aptible::Api::Database).to receive(:find_by_url)
36
+ .with("/find/database?handle=#{database.handle}", token: token)
37
+ .and_return(database)
38
+ allow(Aptible::Api::Database).to receive(:find_by_url)
39
+ .with("/find/database?handle=#{database.handle}&environment=#{account.handle}", token: token)
40
+ .and_return(database)
24
41
  end
25
42
 
26
43
  describe '#backup:restore' do
@@ -60,6 +77,11 @@ describe Aptible::CLI::Agent do
60
77
 
61
78
  it 'accepts a handle' do
62
79
  h = 'some-handle'
80
+ restored = Fabricate(:database, account: account, handle: h)
81
+
82
+ allow(Aptible::Api::Database).to receive(:find_by_url)
83
+ .with("/find/database?handle=#{h}&environment=#{account.handle}", token: token)
84
+ .and_return(database)
63
85
 
64
86
  expect(backup).to receive(:create_operation!) do |options|
65
87
  expect(options[:handle]).to eq(h)
@@ -70,7 +92,7 @@ describe Aptible::CLI::Agent do
70
92
  end
71
93
 
72
94
  expect(subject).to receive(:attach_to_operation_logs).with(op) do
73
- Fabricate(:database, account: account, handle: h)
95
+ restored
74
96
  end
75
97
 
76
98
  subject.options = { handle: h }
@@ -143,6 +165,9 @@ describe Aptible::CLI::Agent do
143
165
  end
144
166
 
145
167
  subject.options = { environment: 'alt' }
168
+ allow(Aptible::Api::Database).to receive(:find_by_url)
169
+ .with("/find/database?handle=#{default_handle}&environment=alt", token: token)
170
+ .and_return(database)
146
171
  subject.send('backup:restore', 1)
147
172
  end
148
173
  end
@@ -150,7 +175,6 @@ describe Aptible::CLI::Agent do
150
175
 
151
176
  describe '#backup:list' do
152
177
  before { allow(Aptible::Api::Account).to receive(:all) { [account] } }
153
- before { allow(Aptible::Api::Database).to receive(:all) { [database] } }
154
178
 
155
179
  before do
156
180
  m = allow(database).to receive(:each_backup)
@@ -192,6 +216,7 @@ describe Aptible::CLI::Agent do
192
216
  end
193
217
 
194
218
  it 'fails if the DB is not found' do
219
+ allow(Aptible::Api::Database).to receive(:find_by_url).and_return(nil)
195
220
  expect { subject.send('backup:list', 'nope') }
196
221
  .to raise_error(Thor::Error, 'Could not find database nope')
197
222
  end
@@ -8,9 +8,9 @@ describe Aptible::CLI::Agent do
8
8
  before { allow(subject).to receive(:fetch_token).and_return(token) }
9
9
 
10
10
  before do
11
- allow(Aptible::Api::App).to receive(:all)
12
- .with(token: token, href: '/apps?per_page=5000&no_embed=true')
13
- .and_return([app])
11
+ allow(Aptible::Api::App).to receive(:find_by_url)
12
+ .with("/find/app?handle=#{app.handle}", token: token)
13
+ .and_return(app)
14
14
  allow(Aptible::Api::Account).to receive(:all)
15
15
  .with(token: token, href: '/apps?per_page=5000&no_embed=true')
16
16
  .and_return([account])
@@ -13,11 +13,17 @@ describe Aptible::CLI::Agent do
13
13
  allow(Aptible::Api::DatabaseCredential).to receive(:all) do
14
14
  database.database_credentials
15
15
  end
16
+ allow(Aptible::Api::Database).to receive(:find_by_url)
17
+ .with("/find/database?handle=#{handle}", token: token)
18
+ .and_return(database)
19
+ allow(Aptible::Api::Database).to receive(:find_by_url)
20
+ .with("/find/database?handle=#{handle}&environment=#{account.handle}", token: token)
21
+ .and_return(database)
16
22
  end
17
23
 
18
24
  let(:handle) { 'foobar' }
19
25
  let(:stack) { Fabricate(:stack, internal_domain: 'aptible.in') }
20
- let(:account) { Fabricate(:account, stack: stack) }
26
+ let(:account) { Fabricate(:account, stack: stack, handle: 'aptible') }
21
27
  let(:database) { Fabricate(:database, handle: handle, account: account) }
22
28
  let(:socat_helper) { SocatHelperMock.new(port: 4242) }
23
29
 
@@ -151,15 +157,15 @@ describe Aptible::CLI::Agent do
151
157
 
152
158
  describe '#db:tunnel' do
153
159
  it 'should fail if database is non-existent' do
154
- allow(Aptible::Api::Database).to receive(:all) { [] }
160
+ allow(Aptible::Api::Database).to receive(:find_by_url)
161
+ .with("/find/database?handle=#{handle}", token: token)
162
+ .and_return(nil)
155
163
  expect do
156
164
  subject.send('db:tunnel', handle)
157
165
  end.to raise_error("Could not find database #{handle}")
158
166
  end
159
167
 
160
168
  context 'valid database' do
161
- before { allow(Aptible::Api::Database).to receive(:all) { [database] } }
162
-
163
169
  it 'prints a message explaining how to connect' do
164
170
  cred = Fabricate(:database_credential, default: true, type: 'foo',
165
171
  database: database)
@@ -371,6 +377,9 @@ describe Aptible::CLI::Agent do
371
377
  allow(Aptible::Api::Account).to receive(:all)
372
378
  .with(token: token, href: '/accounts?per_page=5000&no_embed=true')
373
379
  .and_return([staging, prod])
380
+ allow(Aptible::Api::Account).to receive(:find_by_url)
381
+ .with("/find/account?handle=#{staging.handle}", token: token)
382
+ .and_return(staging)
374
383
  allow(Aptible::Api::ExternalAwsResource).to receive(:all)
375
384
  .with(token: token)
376
385
  .and_return([])
@@ -406,6 +415,7 @@ describe Aptible::CLI::Agent do
406
415
  end
407
416
 
408
417
  context 'when an invalid account is specified' do
418
+ before { allow(Aptible::Api::Account).to receive(:find_by_url).and_return(nil) }
409
419
  it 'prints out an error' do
410
420
  subject.options = { environment: 'foo' }
411
421
  expect { subject.send('db:list') }
@@ -544,6 +554,9 @@ describe Aptible::CLI::Agent do
544
554
  allow(Aptible::Api::Account).to receive(:all)
545
555
  .with(token: token, href: '/accounts?per_page=5000&no_embed=true')
546
556
  .and_return([staging, prod])
557
+ allow(Aptible::Api::Account).to receive(:find_by_url)
558
+ .with("/find/account?handle=#{staging.handle}", token: token)
559
+ .and_return(staging)
547
560
  allow(Aptible::Api::ExternalAwsResource).to receive(:all)
548
561
  .with(token: token)
549
562
  .and_return([staging_rds, prod_rds, unattached_rds])
@@ -627,7 +640,6 @@ describe Aptible::CLI::Agent do
627
640
 
628
641
  describe '#db:backup' do
629
642
  before { allow(Aptible::Api::Account).to receive(:all) { [account] } }
630
- before { allow(Aptible::Api::Database).to receive(:all) { [database] } }
631
643
 
632
644
  let(:op) { Fabricate(:operation) }
633
645
 
@@ -642,6 +654,7 @@ describe Aptible::CLI::Agent do
642
654
  end
643
655
 
644
656
  it 'fails if the DB is not found' do
657
+ allow(Aptible::Api::Database).to receive(:find_by_url).and_return(nil)
645
658
  expect { subject.send('db:backup', 'nope') }
646
659
  .to raise_error(Thor::Error, 'Could not find database nope')
647
660
  end
@@ -649,7 +662,6 @@ describe Aptible::CLI::Agent do
649
662
 
650
663
  describe '#db:reload' do
651
664
  before { allow(Aptible::Api::Account).to receive(:all) { [account] } }
652
- before { allow(Aptible::Api::Database).to receive(:all) { [database] } }
653
665
 
654
666
  let(:op) { Fabricate(:operation) }
655
667
 
@@ -664,6 +676,7 @@ describe Aptible::CLI::Agent do
664
676
  end
665
677
 
666
678
  it 'fails if the DB is not found' do
679
+ allow(Aptible::Api::Database).to receive(:find_by_url).and_return(nil)
667
680
  expect { subject.send('db:reload', 'nope') }
668
681
  .to raise_error(Thor::Error, 'Could not find database nope')
669
682
  end
@@ -671,7 +684,6 @@ describe Aptible::CLI::Agent do
671
684
 
672
685
  describe '#db:restart' do
673
686
  before { allow(Aptible::Api::Account).to receive(:all) { [account] } }
674
- before { allow(Aptible::Api::Database).to receive(:all) { [database] } }
675
687
 
676
688
  let(:op) { Fabricate(:operation) }
677
689
 
@@ -736,6 +748,7 @@ describe Aptible::CLI::Agent do
736
748
  end
737
749
 
738
750
  it 'fails if the DB is not found' do
751
+ allow(Aptible::Api::Database).to receive(:find_by_url).and_return(nil)
739
752
  expect { subject.send('db:restart', 'nope') }
740
753
  .to raise_error(Thor::Error, 'Could not find database nope')
741
754
  end
@@ -743,7 +756,6 @@ describe Aptible::CLI::Agent do
743
756
 
744
757
  describe '#db:modify' do
745
758
  before { allow(Aptible::Api::Account).to receive(:all) { [account] } }
746
- before { allow(Aptible::Api::Database).to receive(:all) { [database] } }
747
759
 
748
760
  let(:op) { Fabricate(:operation) }
749
761
 
@@ -783,16 +795,15 @@ describe Aptible::CLI::Agent do
783
795
  end
784
796
 
785
797
  it 'fails if the DB is not found' do
798
+ allow(Aptible::Api::Database).to receive(:find_by_url).and_return(nil)
786
799
  expect { subject.send('db:modify', 'nope') }
787
800
  .to raise_error(Thor::Error, 'Could not find database nope')
788
801
  end
789
802
  end
790
803
 
791
804
  describe '#db:url' do
792
- let(:databases) { [database] }
793
- before { expect(Aptible::Api::Database).to receive(:all) { databases } }
794
-
795
805
  it 'fails if the DB is not found' do
806
+ allow(Aptible::Api::Database).to receive(:find_by_url).and_return(nil)
796
807
  expect { subject.send('db:url', 'nope') }
797
808
  .to raise_error(Thor::Error, 'Could not find database nope')
798
809
  end
@@ -808,7 +819,12 @@ describe Aptible::CLI::Agent do
808
819
  end
809
820
 
810
821
  it 'fails if multiple DBs are found' do
811
- databases << database
822
+ error = HyperResource::ClientError.new('multiple resources found')
823
+ allow(error).to receive(:body)
824
+ .and_return('error' => 'multiple_resources_found')
825
+ allow(Aptible::Api::Database).to receive(:find_by_url)
826
+ .with("/find/database?handle=#{handle}", token: token)
827
+ .and_raise(error)
812
828
 
813
829
  expect { subject.send('db:url', handle) }
814
830
  .to raise_error(/Multiple databases/)
@@ -817,16 +833,18 @@ describe Aptible::CLI::Agent do
817
833
  end
818
834
 
819
835
  describe '#db:replicate' do
820
- let(:databases) { [] }
821
- before { allow(Aptible::Api::Database).to receive(:all) { databases } }
836
+ let(:master) { Fabricate(:database, handle: 'master') }
837
+ let(:replica) { Fabricate(:database, account: master.account, handle: 'replica') }
838
+ before do
839
+ allow(Aptible::Api::Database).to receive(:find_by_url)
840
+ .with("/find/database?handle=#{master.handle}", token: token)
841
+ .and_return(master)
842
+ allow(Aptible::Api::Database).to receive(:find_by_url)
843
+ .with("/find/database?handle=#{replica.handle}&environment=#{account.handle}", token: token)
844
+ .and_return(replica)
845
+ end
822
846
 
823
847
  def expect_replicate_database(opts = {})
824
- master = Fabricate(:database, handle: 'master')
825
- databases << master
826
- replica = Fabricate(:database,
827
- account: master.account,
828
- handle: 'replica')
829
-
830
848
  op = Fabricate(:operation)
831
849
 
832
850
  params = { type: 'replicate', handle: 'replica' }.merge(opts)
@@ -835,7 +853,6 @@ describe Aptible::CLI::Agent do
835
853
  .with(**params).and_return(op)
836
854
 
837
855
  expect(subject).to receive(:attach_to_operation_logs).with(op) do
838
- databases << replica
839
856
  replica
840
857
  end
841
858
 
@@ -867,17 +884,12 @@ describe Aptible::CLI::Agent do
867
884
  end
868
885
 
869
886
  it 'fails if the DB is not found' do
887
+ allow(Aptible::Api::Database).to receive(:find_by_url).and_return(nil)
870
888
  expect { subject.send('db:replicate', 'nope', 'replica') }
871
889
  .to raise_error(Thor::Error, 'Could not find database nope')
872
890
  end
873
891
 
874
892
  it 'allows logical replication of a database with --version set' do
875
- master = Fabricate(:database, handle: 'master')
876
- databases << master
877
- replica = Fabricate(:database,
878
- account: master.account,
879
- handle: 'replica')
880
-
881
893
  dbimg = Fabricate(:database_image,
882
894
  type: 'postgresql',
883
895
  version: 10,
@@ -894,7 +906,6 @@ describe Aptible::CLI::Agent do
894
906
  .with(**params).and_return(op)
895
907
 
896
908
  expect(subject).to receive(:attach_to_operation_logs).with(op) do
897
- databases << replica
898
909
  replica
899
910
  end
900
911
 
@@ -914,9 +925,6 @@ describe Aptible::CLI::Agent do
914
925
  end
915
926
 
916
927
  it 'fails if logical replication requested without --version' do
917
- master = Fabricate(:database, handle: 'master', type: 'postgresql')
918
- databases << master
919
-
920
928
  subject.options = { type: 'replicate', handle: 'replica', logical: true }
921
929
  expect { subject.send('db:replicate', 'master', 'replica') }
922
930
  .to raise_error(Thor::Error, '--version is required for logical ' \
@@ -924,9 +932,7 @@ describe Aptible::CLI::Agent do
924
932
  end
925
933
 
926
934
  it 'fails if logical replication requested for non-postgres db' do
927
- master = Fabricate(:database, handle: 'master', type: 'mysql')
928
- databases << master
929
-
935
+ master.type = 'mysql'
930
936
  subject.options = { type: 'replicate', handle: 'replica',
931
937
  logical: true, version: 10 }
932
938
  expect { subject.send('db:replicate', 'master', 'replica') }
@@ -937,17 +943,13 @@ describe Aptible::CLI::Agent do
937
943
 
938
944
  describe '#db:dump' do
939
945
  it 'should fail if database is non-existent' do
940
- allow(Aptible::Api::Database).to receive(:all) { [] }
946
+ allow(Aptible::Api::Database).to receive(:find_by_url).and_return(nil)
941
947
  expect do
942
948
  subject.send('db:dump', handle)
943
949
  end.to raise_error("Could not find database #{handle}")
944
950
  end
945
951
 
946
952
  context 'valid database' do
947
- before do
948
- allow(Aptible::Api::Database).to receive(:all) { [database] }
949
- end
950
-
951
953
  it 'exits with the same code as pg_dump' do
952
954
  exit_status = 123
953
955
  cred = Fabricate(:database_credential, default: true, type: 'foo',
@@ -1132,7 +1134,7 @@ describe Aptible::CLI::Agent do
1132
1134
  describe '#db:execute' do
1133
1135
  sql_path = 'file.sql'
1134
1136
  it 'should fail if database is non-existent' do
1135
- allow(Aptible::Api::Database).to receive(:all) { [] }
1137
+ allow(Aptible::Api::Database).to receive(:find_by_url).and_return(nil)
1136
1138
  expect do
1137
1139
  subject.send('db:execute', handle, sql_path)
1138
1140
  end.to raise_error("Could not find database #{handle}")
@@ -1140,7 +1142,6 @@ describe Aptible::CLI::Agent do
1140
1142
 
1141
1143
  context 'valid database' do
1142
1144
  before do
1143
- allow(Aptible::Api::Database).to receive(:all) { [database] }
1144
1145
  allow(subject).to receive(:`).with(/psql .*/) { `exit 0` }
1145
1146
  end
1146
1147
 
@@ -1338,8 +1339,6 @@ describe Aptible::CLI::Agent do
1338
1339
  end
1339
1340
 
1340
1341
  describe '#db:deprovision' do
1341
- before { expect(Aptible::Api::Database).to receive(:all) { [database] } }
1342
-
1343
1342
  let(:operation) { Fabricate(:operation, resource: database) }
1344
1343
 
1345
1344
  it 'deprovisions a database' do
@@ -1409,7 +1408,9 @@ describe Aptible::CLI::Agent do
1409
1408
  before do
1410
1409
  allow(subject).to receive(:options)
1411
1410
  .and_return(environment: account.handle)
1412
- allow(Aptible::Api::Account).to receive(:all) { [account] }
1411
+ allow(Aptible::Api::Account).to receive(:find_by_url)
1412
+ .with("/find/account?handle=#{account.handle}", token: token)
1413
+ .and_return(account)
1413
1414
  end
1414
1415
  context 'with environment and db' do
1415
1416
  it 'should rename properly' do
@@ -1422,6 +1423,7 @@ describe Aptible::CLI::Agent do
1422
1423
  )
1423
1424
  end
1424
1425
  it 'should fail if db does not exist' do
1426
+ allow(Aptible::Api::Database).to receive(:find_by_url).and_return(nil)
1425
1427
  expect { subject.send('db:rename', 'foo2', 'foo3') }
1426
1428
  .to raise_error(/Could not find database foo2/)
1427
1429
  end
@@ -4,12 +4,17 @@ describe Aptible::CLI::Agent do
4
4
  let!(:account) { Fabricate(:account, handle: 'foobar') }
5
5
  let!(:app) { Fabricate(:app, handle: 'hello', account: account) }
6
6
  let(:operation) { Fabricate(:operation) }
7
+ let(:token) { double 'token' }
7
8
 
8
9
  describe '#deploy' do
9
10
  before do
10
- allow(Aptible::Api::App).to receive(:all) { [app] }
11
- allow(Aptible::Api::Account).to receive(:all) { [account] }
12
- allow(subject).to receive(:fetch_token) { double'token' }
11
+ allow(Aptible::Api::App).to receive(:find_by_url)
12
+ .with("/find/app?handle=#{app.handle}&environment=#{account.handle}", token: token)
13
+ .and_return(app)
14
+ allow(subject).to receive(:fetch_token) { token }
15
+ allow(Aptible::Api::Account).to receive(:find_by_url)
16
+ .with("/find/account?handle=#{account.handle}", token: token)
17
+ .and_return(account)
13
18
  end
14
19
 
15
20
  context 'with app' do