cf-perm-test-helpers 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 62093132d462287506bd69bd9fd9a9b4b3ab179b979bb5b74763ada01546fe65
4
- data.tar.gz: 514cb428c64d9e5e05a84949abbec1b265cf5a6cd98f6f09bde93136b8af0aab
3
+ metadata.gz: a6b5cca332b35e11b69d4b989cdd7edea56369d6cd7f851e625bf69941a3aecf
4
+ data.tar.gz: fb66cd2f69de1894f4cd58145bd39a3eb18645d890529be7fefb74134845d180
5
5
  SHA512:
6
- metadata.gz: c35fb55913ec901a53f5769feaa719c9b540e6cadc4ff98f1657e2af958833015335e6da20c3ce2e669cb681dc3e8f386f14c67538f05e2cdcd6f489ca29b557
7
- data.tar.gz: d292683a0e8102c177dbc448119a7f695892aa70a8409589e013983880596debeca74ae7a1469e7f8db4940dde382d7166b13fb54ff1446bbb5076a7f40b0926
6
+ metadata.gz: f2afae297bf88523aeb02b39373c3c75195d7a77aa6cb0390730c428ca4fb917ae785317390b84c6ef3060c287aec513ceb13107c3e364d2612bbd30fc93e8b2
7
+ data.tar.gz: 93beb280def100489f506b2528c4f7a52689f125f40f5500852e5f2f6ca95e6be4b3d0d25a06739bd9aa87606a9c2e5158bc4dd8de57de784a44635ef0ded015
@@ -7,114 +7,76 @@ require 'subprocess'
7
7
 
8
8
  module CloudFoundry
9
9
  module PermTestHelpers
10
- # rubocop:disable Metrics/ClassLength
11
10
  class ServerRunner
12
11
  attr_reader :hostname, :port, :tls_ca, :tls_ca_path
13
12
 
14
- # rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity, Metrics/AbcSize, Metrics/MethodLength
13
+ # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
15
14
  def initialize(opts = {})
16
15
  cwd = File.dirname(__FILE__)
17
16
  cert_path = File.join(cwd, '..', 'fixtures', 'certs')
18
17
 
19
- @hostname = opts[:hostname] || ENV['PERM_TEST_HOSTNAME'] || 'localhost'
20
- @port = opts[:port] || ENV['PERM_TEST_PORT'] || random_port
21
- @perm_path = opts[:perm_path] || ENV['PERM_TEST_PATH'] || 'perm'
22
- @log_level = opts[:log_level] || ENV['PERM_TEST_LOG_LEVEL'] || 'fatal'
23
- @tls_cert = opts[:tls_cert_path] || ENV['PERM_TEST_TLS_CERT_PATH'] || File.join(cert_path, 'tls.crt')
24
- @tls_key = opts[:tls_key_path] || ENV['PERM_TEST_TLS_KEY_PATH'] || File.join(cert_path, 'tls.key')
25
- @tls_ca_path = opts[:tls_ca_path] || ENV['PERM_TEST_TLS_CA_PATH'] || File.join(cert_path, 'tls_ca.crt')
26
- @audit_file_path = opts[:audit_file_path] || ENV['PERM_TEST_AUDIT_FILE_PATH'] || '/dev/null'
18
+ options = Options.new(opts)
19
+
20
+ @hostname = options.attr(:hostname, 'PERM_TEST_HOSTNAME', 'localhost')
21
+ @port = options.attr(:port, 'PERM_TEST_PORT', random_port)
22
+ @perm_path = options.attr(:perm_path, 'PERM_TEST_PATH', 'perm')
23
+ @log_level = options.attr(:log_level, 'PERM_TEST_LOG_LEVEL', 'fatal')
24
+ @tls_cert = options.attr(:tls_cert_path, 'PERM_TEST_TLS_CERT_PATH', File.join(cert_path, 'tls.crt'))
25
+ @tls_key = options.attr(:tls_key_path, 'PERM_TEST_TLS_KEY_PATH', File.join(cert_path, 'tls.key'))
26
+ @tls_ca_path = options.attr(:tls_ca_path, 'PERM_TEST_TLS_CA_PATH', File.join(cert_path, 'tls_ca.crt'))
27
+ @audit_file_path = options.attr(:audit_file_path, 'PERM_TEST_AUDIT_FILE_PATH', '/dev/null')
27
28
  @tls_ca = File.open(tls_ca_path).read
28
29
 
29
- opts[:db] ||= {}
30
- @db_driver = opts[:db][:driver] || ENV['PERM_TEST_SQL_DB_DRIVER'] || 'mysql'
31
- @db_schema = opts[:db][:schema] || ENV['PERM_TEST_SQL_DB_SCHEMA'] || random_schema
32
- @db_host = opts[:db][:host] || ENV['PERM_TEST_SQL_DB_HOST'] || 'localhost'
33
- @db_port = opts[:db][:port] || ENV['PERM_TEST_SQL_DB_PORT'] || '3306'
34
- @db_socket = opts[:db][:socket] || ENV['PERM_TEST_SQL_DB_SOCKET'] || Mysql::MYSQL_UNIX_PORT
35
- @db_username = opts[:db][:username] || ENV['PERM_TEST_SQL_DB_USERNAME'] || 'perm'
36
- @db_password = opts[:db][:password] || ENV['PERM_TEST_SQL_DB_PASSWORD'] || ''
37
-
38
- @stdout = opts[:stdout] || ENV['PERM_TEST_STDOUT_PATH'] || STDOUT
39
- @stderr = opts[:stderr] || ENV['PERM_TEST_STDERR_PATH'] || STDERR
30
+ @stdout = options.attr(:stdout, 'PERM_TEST_STDOUT_PATH', STDOUT)
31
+ @stderr = options.attr(:stderr, 'PERM_TEST_STDERR_PATH', STDERR)
40
32
  end
41
- # rubocop:enable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity, Metrics/AbcSize
33
+ # rubocop:enable Metrics/AbcSize, Metrics/MethodLength
42
34
 
43
35
  def start
44
- create_db
45
- migrate_db
46
36
  @process ||= start_perm
47
37
  end
48
38
 
49
39
  def stop
50
- drop_db
51
40
  @process&.terminate
52
41
  @process = nil
53
42
  end
54
43
 
55
44
  private
56
45
 
57
- attr_writer :port
58
- attr_reader :perm_path, :log_level, :process, :tls_cert, :tls_key, :audit_file_path
59
- attr_reader :db_connection, :db_driver, :db_schema, :db_host, :db_port, :db_socket, :db_username, :db_password
60
- attr_reader :stdout, :stderr
61
-
62
- def create_db
63
- @db_connection = Mysql.connect(db_host, db_username, db_password, nil, db_port, db_socket)
64
-
65
- stmt = @db_connection.prepare("create database #{db_schema}")
66
- stmt.execute
67
- end
46
+ class Options
47
+ def initialize(opts)
48
+ @opts = opts
49
+ end
68
50
 
69
- def migrate_db
70
- cmd = [
71
- perm_path,
72
- 'migrate',
73
- 'up',
74
- '--log-level', log_level,
75
- '--sql-db-driver', db_driver,
76
- '--sql-db-schema', db_schema,
77
- '--sql-db-host', db_host,
78
- '--sql-db-port', db_port,
79
- '--sql-db-username', db_username,
80
- '--sql-db-password', db_password
81
- ]
82
-
83
- Subprocess.check_call(cmd, stdout: stdout, stderr: stderr)
51
+ def attr(key, env, default)
52
+ @opts[key] || ENV[env] || default
53
+ end
84
54
  end
85
55
 
86
- def drop_db
87
- stmt = @db_connection.prepare("drop database #{db_schema}")
88
- stmt.execute
89
- end
56
+ attr_writer :port
57
+ attr_reader :perm_path, :log_level, :process, :tls_cert, :tls_key, :audit_file_path
58
+ attr_reader :stdout, :stderr
90
59
 
91
- # rubocop:disable Metrics/AbcSize
60
+ # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
92
61
  def start_perm
93
62
  retries = 0
94
63
  process = nil
95
64
 
96
65
  begin
97
- listen_port = port || random_port
98
66
  cmd = [
99
67
  perm_path,
100
68
  'serve',
101
69
  '--listen-hostname', hostname,
102
- '--listen-port', listen_port.to_s,
70
+ '--listen-port', port.to_s,
103
71
  '--log-level', log_level,
104
72
  '--tls-certificate', tls_cert,
105
73
  '--tls-key', tls_key,
106
- '--sql-db-driver', db_driver,
107
- '--sql-db-schema', db_schema,
108
- '--sql-db-host', db_host,
109
- '--sql-db-port', db_port,
110
- '--sql-db-username', db_username,
111
- '--sql-db-password', db_password,
74
+ '--db-driver', 'in-memory',
112
75
  '--audit-file-path', audit_file_path
113
76
  ]
114
77
 
115
78
  process = Subprocess.popen(cmd, stdout: stdout, stderr: stderr)
116
79
  wait_for_server(process.pid)
117
- @port = listen_port
118
80
 
119
81
  process
120
82
  rescue Errno::ESRCH => e
@@ -130,11 +92,6 @@ module CloudFoundry
130
92
  end
131
93
  # rubocop:enable Metrics/MethodLength, Metrics/AbcSize
132
94
 
133
- def random_schema
134
- # Dashes are invalid in DB names
135
- "perm-#{SecureRandom.uuid}".tr('-', '_')
136
- end
137
-
138
95
  def random_port
139
96
  rand(65_000 - 1024) + 1024
140
97
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module CloudFoundry
4
4
  module PermTestHelpers
5
- VERSION = '0.0.4'
5
+ VERSION = '0.0.5'
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cf-perm-test-helpers
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - CloudFoundry Permissions Team
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-04-26 00:00:00.000000000 Z
11
+ date: 2018-05-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: subprocess