kontena-cli 0.15.5 → 0.16.0.pre1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (207) hide show
  1. checksums.yaml +4 -4
  2. data/Dockerfile +0 -3
  3. data/Gemfile +3 -0
  4. data/LOGO +8 -0
  5. data/VERSION +1 -1
  6. data/kontena-cli.gemspec +2 -3
  7. data/lib/kontena/callback.rb +57 -0
  8. data/lib/kontena/callbacks/.gitkeep +0 -0
  9. data/lib/kontena/callbacks/auth/01_list_and_select_grid_after_master_auth.rb +27 -0
  10. data/lib/kontena/callbacks/master/01_clear_current_master_after_terminate.rb +20 -0
  11. data/lib/kontena/callbacks/master/deploy/01_show_logo_before_deploy.rb +15 -0
  12. data/lib/kontena/callbacks/master/deploy/05_before_deploy_configuration_wizard.rb +124 -0
  13. data/lib/kontena/callbacks/master/deploy/50_authenticate_after_deploy.rb +53 -0
  14. data/lib/kontena/callbacks/master/deploy/55_create_initial_grid_after_deploy.rb +32 -0
  15. data/lib/kontena/callbacks/master/deploy/60_configure_auth_provider_after_deploy.rb +49 -0
  16. data/lib/kontena/callbacks/master/deploy/90_suggest_inviting_yourself_after_deploy.rb +24 -0
  17. data/lib/kontena/cli/app_command.rb +2 -1
  18. data/lib/kontena/cli/apps/build_command.rb +1 -1
  19. data/lib/kontena/cli/apps/common.rb +6 -1
  20. data/lib/kontena/cli/apps/config_command.rb +1 -1
  21. data/lib/kontena/cli/apps/deploy_command.rb +1 -1
  22. data/lib/kontena/cli/apps/init_command.rb +3 -5
  23. data/lib/kontena/cli/apps/list_command.rb +1 -1
  24. data/lib/kontena/cli/apps/logs_command.rb +1 -1
  25. data/lib/kontena/cli/apps/monitor_command.rb +1 -1
  26. data/lib/kontena/cli/apps/remove_command.rb +2 -3
  27. data/lib/kontena/cli/apps/restart_command.rb +1 -1
  28. data/lib/kontena/cli/apps/scale_command.rb +1 -1
  29. data/lib/kontena/cli/apps/show_command.rb +1 -1
  30. data/lib/kontena/cli/apps/start_command.rb +1 -1
  31. data/lib/kontena/cli/apps/stop_command.rb +1 -1
  32. data/lib/kontena/cli/apps/yaml/reader.rb +3 -13
  33. data/lib/kontena/cli/apps/yaml/validator.rb +0 -4
  34. data/lib/kontena/cli/apps/yaml/validator_v2.rb +1 -5
  35. data/lib/kontena/cli/certificate/authorize_command.rb +1 -1
  36. data/lib/kontena/cli/certificate/get_command.rb +1 -1
  37. data/lib/kontena/cli/certificate/register_command.rb +1 -1
  38. data/lib/kontena/cli/certificate_command.rb +1 -1
  39. data/lib/kontena/cli/cloud/login_command.rb +128 -0
  40. data/lib/kontena/cli/cloud/master/add_command.rb +54 -0
  41. data/lib/kontena/cli/cloud/master/delete_command.rb +20 -0
  42. data/lib/kontena/cli/cloud/master/list_command.rb +29 -0
  43. data/lib/kontena/cli/cloud/master/show_command.rb +23 -0
  44. data/lib/kontena/cli/cloud/master/update_command.rb +58 -0
  45. data/lib/kontena/cli/cloud/master_command.rb +21 -0
  46. data/lib/kontena/cli/cloud_command.rb +10 -0
  47. data/lib/kontena/cli/common.rb +230 -88
  48. data/lib/kontena/cli/config.rb +537 -0
  49. data/lib/kontena/cli/container_command.rb +1 -1
  50. data/lib/kontena/cli/containers/exec_command.rb +1 -1
  51. data/lib/kontena/cli/containers/inspect_command.rb +1 -1
  52. data/lib/kontena/cli/etcd/get_command.rb +1 -1
  53. data/lib/kontena/cli/etcd/list_command.rb +1 -1
  54. data/lib/kontena/cli/etcd/mkdir_command.rb +1 -1
  55. data/lib/kontena/cli/etcd/remove_command.rb +1 -1
  56. data/lib/kontena/cli/etcd/set_command.rb +1 -1
  57. data/lib/kontena/cli/etcd_command.rb +1 -1
  58. data/lib/kontena/cli/external_registries/add_command.rb +1 -1
  59. data/lib/kontena/cli/external_registries/delete_command.rb +1 -1
  60. data/lib/kontena/cli/external_registries/list_command.rb +1 -1
  61. data/lib/kontena/cli/external_registries/remove_command.rb +1 -1
  62. data/lib/kontena/cli/external_registry_command.rb +1 -1
  63. data/lib/kontena/cli/grid_command.rb +1 -1
  64. data/lib/kontena/cli/grids/audit_log_command.rb +6 -5
  65. data/lib/kontena/cli/grids/cloud_config_command.rb +1 -1
  66. data/lib/kontena/cli/grids/common.rb +1 -1
  67. data/lib/kontena/cli/grids/create_command.rb +8 -4
  68. data/lib/kontena/cli/grids/current_command.rb +1 -1
  69. data/lib/kontena/cli/grids/env_command.rb +1 -1
  70. data/lib/kontena/cli/grids/list_command.rb +35 -10
  71. data/lib/kontena/cli/grids/logs_command.rb +1 -1
  72. data/lib/kontena/cli/grids/remove_command.rb +2 -2
  73. data/lib/kontena/cli/grids/show_command.rb +1 -1
  74. data/lib/kontena/cli/grids/trusted_subnet_command.rb +1 -1
  75. data/lib/kontena/cli/grids/trusted_subnets/add_command.rb +1 -1
  76. data/lib/kontena/cli/grids/trusted_subnets/list_command.rb +1 -1
  77. data/lib/kontena/cli/grids/trusted_subnets/remove_command.rb +1 -1
  78. data/lib/kontena/cli/grids/update_command.rb +1 -1
  79. data/lib/kontena/cli/grids/use_command.rb +11 -6
  80. data/lib/kontena/cli/grids/user_command.rb +1 -1
  81. data/lib/kontena/cli/grids/users/add_command.rb +1 -1
  82. data/lib/kontena/cli/grids/users/list_command.rb +1 -1
  83. data/lib/kontena/cli/grids/users/remove_command.rb +1 -1
  84. data/lib/kontena/cli/localhost_web_server.rb +93 -0
  85. data/lib/kontena/cli/login_command.rb +5 -118
  86. data/lib/kontena/cli/logout_command.rb +33 -2
  87. data/lib/kontena/cli/master/audit_log_command.rb +19 -0
  88. data/lib/kontena/cli/master/config/export_command.rb +47 -0
  89. data/lib/kontena/cli/master/config/get_command.rb +24 -0
  90. data/lib/kontena/cli/master/config/import_command.rb +69 -0
  91. data/lib/kontena/cli/master/config/set_command.rb +19 -0
  92. data/lib/kontena/cli/master/config/unset_command.rb +20 -0
  93. data/lib/kontena/cli/master/config_command.rb +24 -0
  94. data/lib/kontena/cli/master/create_command.rb +76 -0
  95. data/lib/kontena/cli/master/current_command.rb +10 -2
  96. data/lib/kontena/cli/master/join_command.rb +20 -0
  97. data/lib/kontena/cli/master/list_command.rb +4 -4
  98. data/lib/kontena/cli/master/login_command.rb +274 -0
  99. data/lib/kontena/cli/master/use_command.rb +8 -19
  100. data/lib/kontena/cli/master/users/invite_command.rb +33 -6
  101. data/lib/kontena/cli/master/users/list_command.rb +2 -2
  102. data/lib/kontena/cli/master/users/remove_command.rb +1 -1
  103. data/lib/kontena/cli/master/users/role_command.rb +1 -1
  104. data/lib/kontena/cli/master/users/roles/add_command.rb +18 -16
  105. data/lib/kontena/cli/master/users/roles/remove_command.rb +1 -1
  106. data/lib/kontena/cli/master/users_command.rb +1 -1
  107. data/lib/kontena/cli/master_command.rb +21 -1
  108. data/lib/kontena/cli/node_command.rb +1 -1
  109. data/lib/kontena/cli/nodes/label_command.rb +1 -1
  110. data/lib/kontena/cli/nodes/labels/add_command.rb +1 -1
  111. data/lib/kontena/cli/nodes/labels/remove_command.rb +1 -1
  112. data/lib/kontena/cli/nodes/list_command.rb +1 -1
  113. data/lib/kontena/cli/nodes/remove_command.rb +1 -1
  114. data/lib/kontena/cli/nodes/show_command.rb +1 -1
  115. data/lib/kontena/cli/nodes/ssh_command.rb +1 -1
  116. data/lib/kontena/cli/nodes/update_command.rb +1 -1
  117. data/lib/kontena/cli/plugin_command.rb +1 -1
  118. data/lib/kontena/cli/plugins/install_command.rb +2 -2
  119. data/lib/kontena/cli/plugins/list_command.rb +2 -2
  120. data/lib/kontena/cli/plugins/search_command.rb +1 -1
  121. data/lib/kontena/cli/plugins/uninstall_command.rb +2 -2
  122. data/lib/kontena/cli/registry/create_command.rb +2 -4
  123. data/lib/kontena/cli/registry/delete_command.rb +1 -1
  124. data/lib/kontena/cli/registry/remove_command.rb +1 -1
  125. data/lib/kontena/cli/registry_command.rb +1 -1
  126. data/lib/kontena/cli/service_command.rb +1 -1
  127. data/lib/kontena/cli/services/container_command.rb +1 -1
  128. data/lib/kontena/cli/services/containers_command.rb +1 -1
  129. data/lib/kontena/cli/services/create_command.rb +1 -1
  130. data/lib/kontena/cli/services/delete_command.rb +1 -1
  131. data/lib/kontena/cli/services/deploy_command.rb +1 -1
  132. data/lib/kontena/cli/services/env_command.rb +1 -1
  133. data/lib/kontena/cli/services/envs/add_command.rb +1 -1
  134. data/lib/kontena/cli/services/envs/list_command.rb +1 -1
  135. data/lib/kontena/cli/services/envs/remove_command.rb +1 -1
  136. data/lib/kontena/cli/services/link_command.rb +1 -1
  137. data/lib/kontena/cli/services/list_command.rb +1 -1
  138. data/lib/kontena/cli/services/logs_command.rb +1 -1
  139. data/lib/kontena/cli/services/monitor_command.rb +1 -1
  140. data/lib/kontena/cli/services/remove_command.rb +1 -1
  141. data/lib/kontena/cli/services/restart_command.rb +1 -1
  142. data/lib/kontena/cli/services/scale_command.rb +1 -1
  143. data/lib/kontena/cli/services/secret_command.rb +1 -1
  144. data/lib/kontena/cli/services/secrets/link_command.rb +1 -1
  145. data/lib/kontena/cli/services/secrets/unlink_command.rb +1 -1
  146. data/lib/kontena/cli/services/services_helper.rb +6 -3
  147. data/lib/kontena/cli/services/show_command.rb +1 -1
  148. data/lib/kontena/cli/services/start_command.rb +1 -1
  149. data/lib/kontena/cli/services/stats_command.rb +1 -1
  150. data/lib/kontena/cli/services/stop_command.rb +1 -1
  151. data/lib/kontena/cli/services/unlink_command.rb +1 -1
  152. data/lib/kontena/cli/services/update_command.rb +1 -1
  153. data/lib/kontena/cli/spinner.rb +122 -0
  154. data/lib/kontena/cli/stack_command.rb +1 -1
  155. data/lib/kontena/cli/stacks/create_command.rb +1 -1
  156. data/lib/kontena/cli/stacks/deploy_command.rb +1 -1
  157. data/lib/kontena/cli/stacks/list_command.rb +1 -1
  158. data/lib/kontena/cli/stacks/remove_command.rb +1 -1
  159. data/lib/kontena/cli/stacks/show_command.rb +1 -1
  160. data/lib/kontena/cli/stacks/update_command.rb +1 -1
  161. data/lib/kontena/cli/vault/list_command.rb +1 -1
  162. data/lib/kontena/cli/vault/read_command.rb +1 -1
  163. data/lib/kontena/cli/vault/remove_command.rb +1 -1
  164. data/lib/kontena/cli/vault/update_command.rb +1 -1
  165. data/lib/kontena/cli/vault/write_command.rb +1 -1
  166. data/lib/kontena/cli/vault_command.rb +1 -1
  167. data/lib/kontena/cli/version.rb +1 -1
  168. data/lib/kontena/cli/version_command.rb +1 -1
  169. data/lib/kontena/cli/vpn/config_command.rb +1 -1
  170. data/lib/kontena/cli/vpn/create_command.rb +2 -4
  171. data/lib/kontena/cli/vpn/delete_command.rb +1 -1
  172. data/lib/kontena/cli/vpn/remove_command.rb +1 -1
  173. data/lib/kontena/cli/vpn_command.rb +1 -1
  174. data/lib/kontena/cli/whoami_command.rb +16 -13
  175. data/lib/kontena/client.rb +410 -90
  176. data/lib/kontena/command.rb +172 -0
  177. data/lib/kontena/main_command.rb +7 -8
  178. data/lib/kontena/presets/github_auth_provider.yml +11 -0
  179. data/lib/kontena/presets/kontena_auth_provider.yml +11 -0
  180. data/lib/kontena_cli.rb +51 -1
  181. data/spec/kontena/cli/app/deploy_command_spec.rb +14 -44
  182. data/spec/kontena/cli/app/scale_spec.rb +1 -1
  183. data/spec/kontena/cli/app/yaml/reader_spec.rb +0 -48
  184. data/spec/kontena/cli/common_spec.rb +63 -59
  185. data/spec/kontena/cli/grids/use_command_spec.rb +43 -0
  186. data/spec/kontena/cli/master/current_command_spec.rb +3 -24
  187. data/spec/kontena/cli/master/use_command_spec.rb +2 -27
  188. data/spec/kontena/cli/master/users/invite_command_spec.rb +4 -18
  189. data/spec/kontena/cli/master/users/roles/add_command_spec.rb +2 -16
  190. data/spec/kontena/cli/master/users/roles/remove_command_spec.rb +2 -13
  191. data/spec/kontena/cli/services/restart_command_spec.rb +1 -1
  192. data/spec/kontena/cli/services/update_command_spec.rb +5 -5
  193. data/spec/kontena/client_spec.rb +104 -35
  194. data/spec/kontena/config_spec.rb +65 -0
  195. data/spec/spec_helper.rb +25 -3
  196. data/spec/support/client_helpers.rb +10 -3
  197. data/spec/support/requirements_helper.rb +32 -0
  198. metadata +61 -48
  199. data/lib/kontena/cli/register_command.rb +0 -23
  200. data/lib/kontena/cli/user/forgot_password_command.rb +0 -16
  201. data/lib/kontena/cli/user/reset_password_command.rb +0 -23
  202. data/lib/kontena/cli/user/verify_command.rb +0 -20
  203. data/lib/kontena/cli/user_command.rb +0 -13
  204. data/spec/fixtures/kontena-malformed-yaml.yml +0 -6
  205. data/spec/fixtures/kontena-not-hash-service-config.yml +0 -3
  206. data/spec/kontena/cli/login_command_spec.rb +0 -32
  207. data/spec/kontena/cli/register_command_spec.rb +0 -57
@@ -4,25 +4,14 @@ require 'kontena/cli/master/users/roles/remove_command'
4
4
 
5
5
  describe Kontena::Cli::Master::Users::Roles::RemoveCommand do
6
6
 
7
+ include ClientHelpers
8
+
7
9
  let(:subject) do
8
10
  described_class.new(File.basename($0))
9
11
  end
10
12
 
11
- let(:valid_settings) do
12
- {'current_server' => 'alias',
13
- 'servers' => [
14
- {'name' => 'some_master', 'url' => 'some_master'},
15
- {'name' => 'alias', 'url' => 'someurl', 'token' => '123456'}
16
- ]
17
- }
18
- end
19
-
20
- let(:client) { spy(:client) }
21
-
22
13
  describe "#remove-role" do
23
14
  before(:each) do
24
- allow(subject).to receive(:client).and_return(client)
25
- allow(subject).to receive(:settings).and_return(valid_settings)
26
15
  allow(subject).to receive(:confirm).and_return(true)
27
16
  end
28
17
 
@@ -22,7 +22,7 @@ describe Kontena::Cli::Services::RestartCommand do
22
22
  end
23
23
 
24
24
  it 'triggers restart command' do
25
- expect(subject).to receive(:restart_service).with(token, 'service')
25
+ expect(subject).to receive(:restart_service)
26
26
  subject.run(['service'])
27
27
  end
28
28
  end
@@ -23,28 +23,28 @@ describe Kontena::Cli::Services::UpdateCommand do
23
23
  end
24
24
 
25
25
  it 'sends update command' do
26
- expect(subject).to receive(:update_service).with(token, 'service', {privileged: false})
26
+ expect(subject).to receive(:update_service).with(duck_type(:access_token), 'service', {privileged: false})
27
27
  subject.run(['service'])
28
28
  end
29
29
 
30
30
  it 'sends --cap-add' do
31
- expect(subject).to receive(:update_service).with(token, 'service', hash_including(cap_add: ['NET_ADMIN']))
31
+ expect(subject).to receive(:update_service).with(duck_type(:access_token), 'service', hash_including(cap_add: ['NET_ADMIN']))
32
32
  subject.run(['--cap-add', 'NET_ADMIN', 'service'])
33
33
  end
34
34
 
35
35
  it 'sends --cap-drop' do
36
- expect(subject).to receive(:update_service).with(token, 'service', hash_including(cap_drop: ['MKNOD']))
36
+ expect(subject).to receive(:update_service).with(duck_type(:access_token), 'service', hash_including(cap_drop: ['MKNOD']))
37
37
  subject.run(['--cap-drop', 'MKNOD', 'service'])
38
38
  end
39
39
 
40
40
  it 'sends --log-driver' do
41
- expect(subject).to receive(:update_service).with(token, 'service', hash_including(log_driver: 'syslog'))
41
+ expect(subject).to receive(:update_service).with(duck_type(:access_token), 'service', hash_including(log_driver: 'syslog'))
42
42
  subject.run(['--log-driver', 'syslog', 'service'])
43
43
  end
44
44
 
45
45
  it 'sends --log-opt' do
46
46
  expect(subject).to receive(:update_service).with(
47
- token, 'service', hash_including(log_opts: {
47
+ duck_type(:access_token), 'service', hash_including(log_opts: {
48
48
  'gelf-address' => 'udp://log_forwarder-logstash_internal:12201'
49
49
  })
50
50
  )
@@ -1,38 +1,95 @@
1
1
  require_relative '../spec_helper'
2
2
  require 'kontena_cli'
3
+ require 'ostruct'
3
4
 
4
5
  describe Kontena::Client do
5
6
 
6
7
  let(:subject) { described_class.new('https://localhost/v1/') }
7
8
  let(:http_client) { double(:http_client) }
8
9
 
10
+ # This trickery is here for making the tests work with or without the new configuration handler.
11
+ # The client itself will work with any kind of token that acts like a hash or ostruct.
12
+ let(:server_class) { Kontena::Cli.const_defined?('Config', false) ? Kontena::Cli::Config::Server : OpenStruct }
13
+ let(:token_class) { Kontena::Cli.const_defined?('Config', false) ? Kontena::Cli::Config::Token : OpenStruct }
14
+ let(:account_class) { Kontena::Cli.const_defined?('Config', false) ? Kontena::Cli::Config::Account : OpenStruct }
15
+
16
+ let(:master) { server_class.new(url: 'https://localhost', name: 'master') }
17
+ let(:token) { token_class.new(access_token: '1234', refresh_token: '5678', expires_at: nil, parent_type: :master, parent_name: 'master') }
18
+ let(:expiring_token) { token_class.new(access_token: '1234', refresh_token: '5678', expires_at: Time.now.utc + 1000, parent_type: :master, parent_name: 'master') }
19
+ let(:expired_token) { token_class.new(access_token: '1234', refresh_token: '5678', expires_at: Time.now.utc - 1000, parent_type: :master, parent_name: 'master') }
20
+
9
21
  before(:each) do
10
22
  allow(subject).to receive(:http_client).and_return(http_client)
23
+ if Kontena::Cli.const_defined?('Config', false)
24
+ config = Kontena::Cli::Config
25
+ else
26
+ config = Class.new { include Kontena::Cli::Common}.new
27
+ end
28
+ config.servers << master
29
+ allow(config).to receive(:find_server).and_return(master)
30
+ allow(config).to receive(:current_master).and_return(master)
31
+ allow(config).to receive(:account).and_return(account_class.new(token_verify_path: '/v1/user', token_endpoint: '/oauth2/token', authorization_endpoint: '/oauth2/authorize'))
32
+ allow(config).to receive(:write).and_return(true)
33
+ end
34
+
35
+ context 'token authentication' do
36
+
37
+ it 'takes a token' do
38
+ client = Kontena::Client.new('https://localhost/v1/', token)
39
+ expect(client.token).to eq token
40
+ end
41
+
42
+ it 'uses the access token as a bearer token' do
43
+ client = Kontena::Client.new('https://localhost/v1/', token)
44
+ expect(client.http_client).to receive(:request) do |opts|
45
+ expect(opts[:headers]['Authorization']).to eq "Bearer #{token.access_token}"
46
+ end.and_return(spy(:response, status: 200))
47
+ client.get('/v1/foo')
48
+ end
49
+
50
+ it 'does not try to refresh an expiring token that is still valid' do
51
+ client = Kontena::Client.new('https://localhost/v1/', expiring_token)
52
+ expect(client.http_client).to receive(:request) do |opts|
53
+ expect(opts[:headers]['Authorization']).to eq "Bearer #{token.access_token}"
54
+ end.and_return(spy(:response, status: 200))
55
+ client.get('/v1/foo')
56
+ end
57
+
58
+ it 'tries to refresh an expired token' do
59
+ master.token = expired_token
60
+ client = Kontena::Client.new(master.url, master.token)
61
+ allow(client).to receive(:token_refresh_path).and_return('/oauth2/token')
62
+ expect(client.http_client).to receive(:request).with(hash_including(path: '/oauth2/token', method: :post)).and_return(OpenStruct.new(status: 201, headers: {'Content-Type' => 'application/json'}, body: '{"access_token": "abcd"}'))
63
+ expect(client.http_client).to receive(:request).with(hash_including(path: '/v1/foo')) do |args|
64
+ expect(args[:headers]['Authorization']).to eq "Bearer abcd"
65
+ end.and_return(spy(:response, status: 200))
66
+ client.get('/v1/foo')
67
+ end
11
68
  end
12
69
 
13
70
  describe '#get' do
14
71
  it 'passes path to client' do
15
72
  allow(subject).to receive(:http_client).and_return(http_client)
16
- expect(http_client).to receive(:get).with(
17
- hash_including(path: '/v1/foo')
73
+ expect(http_client).to receive(:request).with(
74
+ hash_including(path: '/v1/foo', method: :get)
18
75
  ).and_return(spy(:response, status: 200))
19
76
  subject.get('foo')
20
77
  end
21
78
 
22
79
  it 'passes params to client' do
23
80
  allow(subject).to receive(:http_client).and_return(http_client)
24
- expect(http_client).to receive(:get).with(
25
- hash_including(query: {bar: 'baz'})
81
+ expect(http_client).to receive(:request).with(
82
+ hash_including(query: {bar: 'baz'}, method: :get)
26
83
  ).and_return(spy(:response, status: 200))
27
84
  subject.get('foo', {bar: 'baz'})
28
85
  end
29
86
 
30
87
  it 'passes params to client' do
31
88
  allow(subject).to receive(:http_client).and_return(http_client)
32
- expect(http_client).to receive(:get).with(
33
- hash_including(headers: hash_including(:'Some-Header' => 'value'))
89
+ expect(http_client).to receive(:request).with(
90
+ hash_including(headers: hash_including('Some-Header' => 'value'), method: :get)
34
91
  ).and_return(spy(:response, status: 200))
35
- subject.get('foo', nil, {:'Some-Header' => 'value'})
92
+ subject.get('foo', nil, {'Some-Header' => 'value'})
36
93
  end
37
94
  end
38
95
 
@@ -40,24 +97,27 @@ describe Kontena::Client do
40
97
  let(:response_block) { Proc.new{ } }
41
98
 
42
99
  it 'passes path & response_block to client' do
43
- expect(http_client).to receive(:get).with(
44
- hash_including(path: '/v1/foo', response_block: response_block)
100
+ allow(subject).to receive(:http_client).and_return(http_client)
101
+ expect(http_client).to receive(:request).with(
102
+ hash_including(path: '/v1/foo', response_block: response_block, method: :get)
45
103
  ).and_return(spy(:response, status: 200))
46
104
  subject.get_stream('foo', response_block)
47
105
  end
48
106
 
49
107
  it 'passes params to client' do
50
- expect(http_client).to receive(:get).with(
51
- hash_including(query: {bar: 'baz'})
108
+ allow(subject).to receive(:http_client).and_return(http_client)
109
+ expect(http_client).to receive(:request).with(
110
+ hash_including(query: {bar: 'baz'}, method: :get)
52
111
  ).and_return(spy(:response, status: 200))
53
112
  subject.get_stream('foo', response_block, {bar: 'baz'})
54
113
  end
55
114
 
56
115
  it 'passes params to client' do
57
- expect(http_client).to receive(:get).with(
58
- hash_including(headers: hash_including(:'Some-Header' => 'value'))
116
+ allow(subject).to receive(:http_client).and_return(http_client)
117
+ expect(http_client).to receive(:request).with(
118
+ hash_including(headers: hash_including('Some-Header' => 'value'), method: :get)
59
119
  ).and_return(spy(:response, status: 200))
60
- subject.get_stream('foo', response_block, nil, {:'Some-Header' => 'value'})
120
+ subject.get_stream('foo', response_block, nil, {'Some-Header' => 'value'})
61
121
  end
62
122
  end
63
123
 
@@ -67,24 +127,27 @@ describe Kontena::Client do
67
127
  end
68
128
 
69
129
  it 'passes path and object to client' do
70
- expect(http_client).to receive(:post).with(
71
- hash_including(path: '/v1/foo', body: kind_of(String))
130
+ allow(subject).to receive(:http_client).and_return(http_client)
131
+ expect(http_client).to receive(:request).with(
132
+ hash_including(path: '/v1/foo', body: kind_of(String), method: :post)
72
133
  ).and_return(spy(:response, status: 200))
73
134
  subject.post('foo', data)
74
135
  end
75
136
 
76
137
  it 'passes params to client' do
77
- expect(http_client).to receive(:post).with(
78
- hash_including(query: {bar: 'baz'})
138
+ allow(subject).to receive(:http_client).and_return(http_client)
139
+ expect(http_client).to receive(:request).with(
140
+ hash_including(query: {bar: 'baz'}, method: :post)
79
141
  ).and_return(spy(:response, status: 200))
80
142
  subject.post('foo', data, {bar: 'baz'})
81
143
  end
82
144
 
83
145
  it 'passes params to client' do
84
- expect(http_client).to receive(:post).with(
85
- hash_including(headers: hash_including(:'Some-Header' => 'value'))
146
+ allow(subject).to receive(:http_client).and_return(http_client)
147
+ expect(http_client).to receive(:request).with(
148
+ hash_including(headers: hash_including('Some-Header' => 'value'), method: :post)
86
149
  ).and_return(spy(:response, status: 200))
87
- subject.post('foo', data, nil, {:'Some-Header' => 'value'})
150
+ subject.post('foo', data, nil, {'Some-Header' => 'value'})
88
151
  end
89
152
  end
90
153
 
@@ -94,24 +157,27 @@ describe Kontena::Client do
94
157
  end
95
158
 
96
159
  it 'passes path and object to client' do
97
- expect(http_client).to receive(:put).with(
98
- hash_including(path: '/v1/foo', body: kind_of(String))
160
+ allow(subject).to receive(:http_client).and_return(http_client)
161
+ expect(http_client).to receive(:request).with(
162
+ hash_including(path: '/v1/foo', body: kind_of(String), method: :put)
99
163
  ).and_return(spy(:response, status: 200))
100
164
  subject.put('foo', data)
101
165
  end
102
166
 
103
167
  it 'passes params to client' do
104
- expect(http_client).to receive(:put).with(
105
- hash_including(query: {bar: 'baz'})
168
+ allow(subject).to receive(:http_client).and_return(http_client)
169
+ expect(http_client).to receive(:request).with(
170
+ hash_including(query: {bar: 'baz'}, method: :put)
106
171
  ).and_return(spy(:response, status: 200))
107
172
  subject.put('foo', data, {bar: 'baz'})
108
173
  end
109
174
 
110
175
  it 'passes params to client' do
111
- expect(http_client).to receive(:put).with(
112
- hash_including(headers: hash_including(:'Some-Header' => 'value'))
176
+ allow(subject).to receive(:http_client).and_return(http_client)
177
+ expect(http_client).to receive(:request).with(
178
+ hash_including(headers: hash_including('Some-Header' => 'value'), method: :put)
113
179
  ).and_return(spy(:response, status: 200))
114
- subject.put('foo', data, nil, {:'Some-Header' => 'value'})
180
+ subject.put('foo', data, nil, {'Some-Header' => 'value'})
115
181
  end
116
182
  end
117
183
 
@@ -121,24 +187,27 @@ describe Kontena::Client do
121
187
  end
122
188
 
123
189
  it 'passes path to client' do
124
- expect(http_client).to receive(:delete).with(
125
- hash_including(path: '/v1/foo')
190
+ allow(subject).to receive(:http_client).and_return(http_client)
191
+ expect(http_client).to receive(:request).with(
192
+ hash_including(path: '/v1/foo', method: :delete)
126
193
  ).and_return(spy(:response, status: 200))
127
194
  subject.delete('foo')
128
195
  end
129
196
 
130
197
  it 'passes params to client' do
131
- expect(http_client).to receive(:delete).with(
132
- hash_including(query: {bar: 'baz'})
198
+ allow(subject).to receive(:http_client).and_return(http_client)
199
+ expect(http_client).to receive(:request).with(
200
+ hash_including(query: {bar: 'baz'}, method: :delete)
133
201
  ).and_return(spy(:response, status: 200))
134
202
  subject.delete('foo', nil, {bar: 'baz'})
135
203
  end
136
204
 
137
205
  it 'passes params to client' do
138
- expect(http_client).to receive(:delete).with(
139
- hash_including(headers: hash_including(:'Some-Header' => 'value'))
206
+ allow(subject).to receive(:http_client).and_return(http_client)
207
+ expect(http_client).to receive(:request).with(
208
+ hash_including(headers: hash_including('Some-Header' => 'value'), method: :delete)
140
209
  ).and_return(spy(:response, status: 200))
141
- subject.delete('foo', nil, nil, {:'Some-Header' => 'value'})
210
+ subject.delete('foo', nil, nil, {'Some-Header' => 'value'})
142
211
  end
143
212
  end
144
213
  end
@@ -0,0 +1,65 @@
1
+ require_relative '../spec_helper'
2
+
3
+ # Lots of coverage already in Common spec
4
+ describe Kontena::Cli::Config do
5
+
6
+ context 'base' do
7
+ let(:subject) { described_class.instance }
8
+
9
+ before(:each) do
10
+ allow(File).to receive(:exist?).and_return(false)
11
+ allow(File).to receive(:write).and_return(true)
12
+ subject.class.reset_instance
13
+ subject.servers << Kontena::Cli::Config::Server.new(
14
+ url: 'http://localhost',
15
+ name: 'test',
16
+ token: Kontena::Cli::Config::Token.new(access_token: 'abcd')
17
+ )
18
+ end
19
+
20
+ it 'finds a server by name' do
21
+ expect(subject.find_server('test').url).to eq 'http://localhost'
22
+ end
23
+
24
+ it 'finds a server by url' do
25
+ expect(subject.find_server_by(url: 'http://localhost').name).to eq 'test'
26
+ end
27
+
28
+ it 'returns current master' do
29
+ subject.current_master = 'test'
30
+ expect(subject.current_master.name).to eq 'test'
31
+ end
32
+
33
+ it 'returns an array of servers' do
34
+ expect(subject.servers).to be_kind_of(Array)
35
+ expect(subject.servers.first.url).to match /^http/
36
+ end
37
+
38
+ it 'returns an array of accounts' do
39
+ expect(subject.accounts).to be_kind_of(Array)
40
+ end
41
+
42
+ it 'adds default accounts' do
43
+ expect(subject.find_account('kontena').name).to eq 'kontena'
44
+ expect(subject.find_account('master').name).to eq 'master'
45
+ end
46
+
47
+ it 'sets and returns current grid' do
48
+ subject.current_master = 'test'
49
+ subject.current_grid = 'foo'
50
+ expect(subject.current_master.grid).to eq 'foo'
51
+ expect(subject.current_grid).to eq 'foo'
52
+ end
53
+ end
54
+
55
+ describe 'Token' do
56
+ let(:subject) { Kontena::Cli::Config::Token.new(access_token: 'abcd', expires_at: Time.now.utc - 100) }
57
+
58
+ it 'knows when a token is expired' do
59
+ expect(subject.expired?).to be_truthy
60
+ subject.expires_at = Time.now.utc + 100
61
+ expect(subject.expired?).to be_falsey
62
+ end
63
+ end
64
+ end
65
+
@@ -5,11 +5,15 @@
5
5
  #
6
6
  # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
7
7
 
8
+ begin
9
+ require 'simplecov'
10
+ SimpleCov.start
11
+ rescue LoadError
12
+ end
13
+
8
14
  require 'clamp'
9
15
  require 'ruby_dig'
10
- require 'kontena/cli/common'
11
- require 'kontena/cli/grid_options'
12
- require 'kontena/client'
16
+ require 'kontena_cli'
13
17
 
14
18
  RSpec.configure do |config|
15
19
  config.run_all_when_everything_filtered = true
@@ -22,8 +26,26 @@ RSpec.configure do |config|
22
26
  config.order = 'random'
23
27
  config.before(:each) do
24
28
  allow(Dir).to receive(:home).and_return('/tmp/')
29
+ allow(ENV).to receive(:[]).with(anything).and_call_original
30
+ allow(ENV).to receive(:[]).with('DEBUG').and_call_original
31
+ Kontena::Cli::Config.reset_instance
32
+ end
33
+
34
+ config.after(:each) do
35
+ RSpec::Mocks.space.proxy_for(File).reset
36
+ RSpec::Mocks.space.proxy_for(Kontena::Cli::Config).reset
37
+ File.unlink(Kontena::Cli::Config.default_config_filename) if File.exist?(Kontena::Cli::Config.default_config_filename)
38
+ end
39
+
40
+ config.around(:each) do |example|
41
+ begin
42
+ example.run
43
+ rescue SystemExit
44
+ puts "Got SystemExit: #{$!.message} - Exit code: #{$!.status}"
45
+ end
25
46
  end
26
47
  end
27
48
 
28
49
  require_relative 'support/client_helpers'
50
+ require_relative 'support/requirements_helper'
29
51
  require_relative 'support/fixtures_helpers'
@@ -15,17 +15,24 @@ module ClientHelpers
15
15
 
16
16
  base.let(:settings) do
17
17
  {'current_server' => 'alias',
18
+ 'current_account' => 'kontena',
18
19
  'servers' => [
19
20
  {'name' => 'some_master', 'url' => 'some_master'},
20
- {'name' => 'alias', 'url' => 'someurl', 'token' => token}
21
+ {'name' => 'alias', 'url' => 'someurl', 'token' => token, 'account' => 'master'}
21
22
  ]
22
23
  }
23
24
  end
24
25
 
25
26
  base.before(:each) do
26
- allow(subject).to receive(:client).with(token).and_return(client)
27
+ RSpec::Mocks.space.proxy_for(File).reset
28
+ allow(subject).to receive(:client).and_return(client)
27
29
  allow(subject).to receive(:current_grid).and_return('test-grid')
28
- allow(subject).to receive(:settings).and_return(settings)
30
+ allow(File).to receive(:exist?).with(File.join(Dir.home, '.kontena_client.json')).and_return(true)
31
+ allow(File).to receive(:readable?).with(File.join(Dir.home, '.kontena_client.json')).and_return(true)
32
+ allow(File).to receive(:read).and_call_original
33
+ allow(File).to receive(:read).with(File.join(Dir.home, '.kontena_client.json')).and_return(JSON.dump(settings))
34
+ Kontena::Cli::Config.reset_instance
29
35
  end
30
36
  end
37
+
31
38
  end