build_randomization.rb 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. #!/usr/bin/env ruby
  2. # Licensed to Elasticsearch under one or more contributor
  3. # license agreements. See the NOTICE file distributed with
  4. # this work for additional information regarding copyright
  5. # ownership. Elasticsearch licenses this file to you under
  6. # the Apache License, Version 2.0 (the "License"); you may
  7. # not use this file except in compliance with the License.
  8. # You may obtain a copy of the License at
  9. #
  10. # http://www.apache.org/licenses/LICENSE-2.0
  11. #
  12. # Unless required by applicable law or agreed to in writing,
  13. # software distributed under the License is distributed on
  14. # an 'AS IS' BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
  15. # either express or implied. See the License for the specific
  16. # language governing permissions and limitations under the License
  17. #
  18. # generate property file for the jdk randomization test
  19. #
  20. #
  21. require 'yaml'
  22. class JDKSelector
  23. attr_reader :directory, :jdk_list
  24. def initialize(directory)
  25. @directory = directory
  26. end
  27. # get selection of available jdks from jenkins automatic install directory
  28. def get_jdk
  29. @jdk_list = Dir.entries(directory).select do |x|
  30. x.chars.first == 'J'
  31. end.map do |y|
  32. File.join(directory, y)
  33. end
  34. self
  35. end
  36. # do ranomize selection from a given array
  37. def select_one(selection_array = nil)
  38. selection_array = filter_java_6(selection_array || @jdk_list)
  39. selection_array[rand(selection_array.size)]
  40. get_random_one(selection_array)
  41. end
  42. end
  43. def get_random_one(data_array)
  44. data_array[rand(data_array.size)]
  45. end
  46. def filter_java_6(files)
  47. files.select{ |i| File.basename(i).split(/[^0-9]/)[-1].to_i > 6 }
  48. end
  49. # given a jdk directory selection, generate relevant environment variables
  50. def get_env_matrix(data_array)
  51. #refactoring target
  52. es_test_jvm_option1 = get_random_one(['-server']) #only server for now get_random_one(['-client', '-server'])
  53. es_test_jvm_option2 = get_random_one(['-XX:+UseConcMarkSweepGC', '-XX:+UseParallelGC', '-XX:+UseSerialGC', '-XX:+UseG1GC'])
  54. es_test_jvm_option3 = get_random_one(['-XX:+UseCompressedOops', '-XX:-UseCompressedOops'])
  55. es_node_mode = get_random_one(['local', 'network'])
  56. tests_nightly = get_random_one([true, false])
  57. tests_nightly = get_random_one([false]) #bug
  58. test_assert_off = (rand(10) == 9) #10 percent chance turning it off
  59. tests_security_manager = (rand(10) != 9) #10 percent chance running without security manager
  60. arg_line = [es_test_jvm_option1, es_test_jvm_option2, es_test_jvm_option3]
  61. [*data_array].map do |x|
  62. data_hash = {
  63. 'PATH' => File.join(x,'bin') + ':' + ENV['PATH'],
  64. 'JAVA_HOME' => x,
  65. 'BUILD_DESC' => "%s,%s,%s%s,%s %s%s%s"%[File.basename(x), es_node_mode, tests_nightly ? 'nightly,':'',
  66. es_test_jvm_option1[1..-1], es_test_jvm_option2[4..-1], es_test_jvm_option3[4..-1],
  67. test_assert_off ? ',assert off' : '', tests_security_manager ? ', security manager enabled' : ''],
  68. 'es.node.mode' => es_node_mode,
  69. 'tests.nightly' => tests_nightly,
  70. 'tests.security.manager' => tests_security_manager,
  71. 'tests.jvm.argline' => arg_line.join(" "),
  72. }
  73. data_hash['tests.assertion.disabled'] = 'org.elasticsearch' if test_assert_off
  74. data_hash
  75. end
  76. end
  77. # pick first element out of array of hashes, generate write java property file
  78. def generate_property_file(directory, data)
  79. #array transformation
  80. content = data.first.map do |key, value|
  81. "%s=%s"%[key, value]
  82. end
  83. file_name = (ENV['BUILD_ID'] + ENV['BUILD_NUMBER']) || 'prop' rescue 'prop'
  84. file_name = file_name.split(File::SEPARATOR).first + '.txt'
  85. File.open(File.join(directory, file_name), 'w') do |file|
  86. file.write(content.join("\n"))
  87. end
  88. end
  89. working_directory = ENV['WORKSPACE'] || '/var/tmp'
  90. unless(ENV['BUILD_ID'])
  91. #local mode set up fake environment
  92. test_directory = 'tools/hudson.model.JDK/'
  93. unless(File.exist?(test_directory))
  94. puts "running local mode, setting up running environment"
  95. puts "properties are written to file prop.txt"
  96. system("mkdir -p %sJDK{6,7}"%test_directory)
  97. end
  98. working_directory = ENV['PWD']
  99. end
  100. # jenkins sets pwd prior to execution
  101. jdk_selector = JDKSelector.new(File.join(ENV['PWD'],'tools','hudson.model.JDK'))
  102. environment_matrix = get_env_matrix(jdk_selector.get_jdk.select_one)
  103. generate_property_file(working_directory, environment_matrix)