github.com/cloudfoundry-attic/cli-with-i18n@v6.32.1-0.20171002233121-7401370d3b85+incompatible/integration/assets/service_broker/run_all_cases.rb (about)

     1  #!/usr/bin/env ruby
     2  
     3  require 'CSV'
     4  require 'json'
     5  require 'benchmark'
     6  require 'securerandom'
     7  require 'optparse'
     8  
     9  DEFAULT_BROKER_URL = 'http://async-broker.bosh-lite.com'
    10  
    11  def get_config
    12    raw_config = File.read('data.json')
    13    JSON.parse(raw_config)
    14  end
    15  
    16  def get_service
    17    config = get_config
    18    config['behaviors']['catalog']['body']['services'].first['name']
    19  end
    20  
    21  def get_plan
    22    config = get_config
    23    config['behaviors']['catalog']['body']['services'].first['plans'].first['name']
    24  end
    25  
    26  def get_second_plan
    27    config = get_config
    28    config['behaviors']['catalog']['body']['services'].first['plans'][1]['name']
    29  end
    30  
    31  def execute(cmd)
    32    `#{cmd}`
    33  end
    34  
    35  class ProvisionCommand
    36    def setup(instance_name)
    37    end
    38  
    39    def run(instance_name)
    40      execute "cf create-service #{get_service} #{get_plan} #{instance_name}"
    41    end
    42  
    43    def cleanup(instance_name)
    44    end
    45  end
    46  
    47  class UpdateCommand
    48    def setup(instance_name)
    49      execute "cf create-service #{get_service} #{get_plan} #{instance_name}"
    50    end
    51  
    52    def run(instance_name)
    53      execute "cf update-service #{instance_name} -p #{get_second_plan}"
    54    end
    55  
    56    def cleanup(instance_name)
    57    end
    58  end
    59  
    60  class DeprovisionCommand
    61    def setup(instance_name)
    62      execute "cf create-service #{get_service} #{get_plan} #{instance_name}"
    63    end
    64  
    65    def run(instance_name)
    66      execute "cf delete-service #{instance_name} -f"
    67    end
    68  
    69    def cleanup(instance_name)
    70    end
    71  end
    72  
    73  class CleanupCommandWrapper
    74    def initialize(command, broker_url)
    75      @command = command
    76      @broker_url = broker_url
    77    end
    78  
    79    def setup(instance_name)
    80      @command.setup(instance_name)
    81    end
    82  
    83    def run(instance_name)
    84      @command.run(instance_name)
    85    end
    86  
    87    def cleanup(instance_name)
    88      @command.cleanup(instance_name)
    89      -> {
    90        execute "curl -s #{@broker_url}/config/reset -X POST"
    91        until attempt_delete(instance_name)
    92        end
    93      }
    94    end
    95  
    96    private
    97  
    98    def attempt_delete(instance_name)
    99      output = execute "cf delete-service #{instance_name} -f"
   100      !output.include?('Another operation for this service instance is in progress')
   101    end
   102  end
   103  
   104  def write_output_file(output_file, rows)
   105    CSV.open(output_file, 'w') do |csv|
   106      csv << rows[0].headers
   107      rows.each do |row|
   108        csv << row
   109      end
   110    end
   111  end
   112  
   113  def delete_leftover_instances(deferred_deletions)
   114    count = deferred_deletions.compact.count
   115    STDOUT.write("Cleaning up service instances ... 0 / #{count}")
   116    STDOUT.flush
   117    i = 0
   118    deferred_deletions.compact.each do |callback|
   119      callback.call
   120      i += 1
   121      STDOUT.write("\rCleaning up service instances ... #{i} / #{count}")
   122      STDOUT.flush
   123    end
   124    puts
   125    puts "Done"
   126  end
   127  
   128  def parse_parameters
   129    options = { cleanup: true }
   130    OptionParser.new do |opts|
   131      opts.on("--no-cleanup", "Run script without cleanup") do |v|
   132        options[:cleanup] = v
   133      end
   134    end.parse!
   135  
   136    if ARGV.length < 1
   137      puts "Usage: #{$PROGRAM_NAME} CSV_FILE [BROKER_URL] [--no-cleanup]"
   138      puts
   139      puts "Broker URL defaults to #{DEFAULT_BROKER_URL}"
   140      exit(1)
   141    end
   142  
   143    input_file = ARGV[0]
   144  
   145    name = File.basename(input_file, '.*')
   146    extension = File.extname(input_file)
   147    output_file = name + "-out" + extension
   148  
   149    broker_url = ARGV.length > 1 ? ARGV[1] : DEFAULT_BROKER_URL
   150    return broker_url, input_file, output_file, options[:cleanup]
   151  end
   152  
   153  def configure_broker_endpoint(action, body, broker_url, row, status)
   154    json_config = {
   155      behaviors: {
   156        action => {
   157          default: {
   158            status: status,
   159            raw_body: body,
   160            sleep_seconds: row['sleep seconds'].to_f
   161          }
   162        }
   163      }
   164    }
   165  
   166    execute "curl -s #{broker_url}/config/reset -X POST"
   167    execute "curl -s #{broker_url}/config -d '#{json_config.to_json}'"
   168  end
   169  
   170  def run_command(command, deferred_deletions, cleanup, line_number)
   171    instance_name = "si-#{line_number}-#{SecureRandom.uuid}"
   172  
   173    command.setup(instance_name)
   174    output = command.run(instance_name)
   175    deferred_deletions << command.cleanup(instance_name) if cleanup
   176    output
   177  end
   178  
   179  deferred_deletions = []
   180  rows = []
   181  
   182  broker_url, input_file, output_file, cleanup = parse_parameters
   183  
   184  action_to_cmd_mapping = {
   185    provision: CleanupCommandWrapper.new(ProvisionCommand.new, broker_url),
   186    update: CleanupCommandWrapper.new(UpdateCommand.new, broker_url),
   187    deprovision: CleanupCommandWrapper.new(DeprovisionCommand.new, broker_url),
   188  }
   189  
   190  report = Benchmark.measure do
   191    i = 0
   192    CSV.foreach(input_file, headers: true) do |row|
   193      i += 1
   194      rows << row
   195  
   196      action, status, body = row['action'], row['status'], row['body']
   197      next unless action
   198  
   199      command = action_to_cmd_mapping[action.to_sym]
   200      next unless command
   201  
   202      configure_broker_endpoint(action, body, broker_url, row, status)
   203  
   204      output = run_command(command, deferred_deletions, cleanup, i)
   205      row['output'] = output
   206      STDOUT.write('.')
   207      STDOUT.flush
   208    end
   209  
   210    puts
   211  
   212    write_output_file(output_file, rows)
   213    delete_leftover_instances(deferred_deletions)
   214  end
   215  
   216  puts "Took #{report.real} seconds"