diff --git a/lib/facter/es_facts.rb b/lib/facter/es_facts.rb index 0114751..eb587f4 100644 --- a/lib/facter/es_facts.rb +++ b/lib/facter/es_facts.rb @@ -1,147 +1,137 @@ require 'net/http' require 'json' require 'yaml' # Helper module to encapsulate custom fact injection -# rubocop:disable Metrics/ModuleLength module EsFacts # Add a fact to the catalog of host facts def self.add_fact(prefix, key, value) key = "#{prefix}_#{key}".to_sym ::Facter.add(key) do setcode { value } end end def self.ssl?(config) tls_keys = [ - 'xpack.security.http.ssl.enabled', - 'shield.http.ssl', - 'searchguard.ssl.http.enabled' + 'xpack.security.http.ssl.enabled' ] tls_keys.any? { |key| (config.key? key) && (config[key] == true) } end # Helper to determine the instance http.port number def self.get_httpport(config) enabled = 'http.enabled' httpport = 'http.port' - if !config[enabled].nil? && config[enabled] == 'false' - false - elsif !config[httpport].nil? - { config[httpport] => ssl?(config) } - else - { '9200' => ssl?(config) } - end + return false, false if !config[enabled].nil? && config[enabled] == 'false' + return config[httpport], ssl?(config) unless config[httpport].nil? + ['9200', ssl?(config)] end # Entrypoint for custom fact populator # # This is a super old function but works; disable a bunch of checks. # rubocop:disable Lint/HandleExceptions # rubocop:disable Metrics/CyclomaticComplexity # rubocop:disable Metrics/PerceivedComplexity def self.run dir_prefix = '/etc/elasticsearch' # httpports is a hash of port_number => ssl? - httpports = {} transportports = [] http_bound_addresses = [] transport_bound_addresses = [] transport_publish_addresses = [] nodes = {} # only when the directory exists we need to process the stuff return unless File.directory?(dir_prefix) - Dir.foreach(dir_prefix) do |dir| - next if dir == '.' - - if File.readable?("#{dir_prefix}/#{dir}/elasticsearch.yml") - config_data = YAML.load_file("#{dir_prefix}/#{dir}/elasticsearch.yml") - httpport = get_httpport(config_data) - httpports.merge! httpport if httpport - end + if File.readable?("#{dir_prefix}/elasticsearch.yml") + config_data = YAML.load_file("#{dir_prefix}/elasticsearch.yml") + httpport, ssl = get_httpport(config_data) end begin - if httpports.keys.count > 0 - - add_fact('elasticsearch', 'ports', httpports.keys.join(',')) - - httpports.each_pair do |httpport, ssl| - next if ssl + add_fact('elasticsearch', 'port', httpport) - key_prefix = "elasticsearch_#{httpport}" + unless ssl + key_prefix = 'elasticsearch' + # key_prefix = "elasticsearch_#{httpport}" - uri = URI("http://localhost:#{httpport}") - http = Net::HTTP.new(uri.host, uri.port) - http.read_timeout = 10 - http.open_timeout = 2 - response = http.get('/') - json_data = JSON.parse(response.body) - next if json_data['status'] && json_data['status'] != 200 + uri = URI("http://localhost:#{httpport}") + http = Net::HTTP.new(uri.host, uri.port) + http.read_timeout = 10 + http.open_timeout = 2 + response = http.get('/') + json_data = JSON.parse(response.body) + if json_data['status'] && json_data['status'] == 200 add_fact(key_prefix, 'name', json_data['name']) add_fact(key_prefix, 'version', json_data['version']['number']) uri2 = URI("http://localhost:#{httpport}/_nodes/#{json_data['name']}") http2 = Net::HTTP.new(uri2.host, uri2.port) http2.read_timeout = 10 http2.open_timeout = 2 response2 = http2.get(uri2.path) json_data_node = JSON.parse(response2.body) add_fact(key_prefix, 'cluster_name', json_data_node['cluster_name']) node_data = json_data_node['nodes'].first add_fact(key_prefix, 'node_id', node_data[0]) nodes_data = json_data_node['nodes'][node_data[0]] process = nodes_data['process'] add_fact(key_prefix, 'mlockall', process['mlockall']) plugins = nodes_data['plugins'] plugin_names = [] plugins.each do |plugin| plugin_names << plugin['name'] plugin.each do |key, value| prefix = "#{key_prefix}_plugin_#{plugin['name']}" add_fact(prefix, key, value) unless key == 'name' end end add_fact(key_prefix, 'plugins', plugin_names.join(',')) nodes_data['http']['bound_address'].each { |i| http_bound_addresses << i } nodes_data['transport']['bound_address'].each { |i| transport_bound_addresses << i } transport_publish_addresses << nodes_data['transport']['publish_address'] unless nodes_data['transport']['publish_address'].nil? transportports << nodes_data['settings']['transport']['tcp']['port'] unless nodes_data['settings']['transport']['tcp'].nil? or nodes_data['settings']['transport']['tcp']['port'].nil? - node = { 'http_ports' => httpports.keys, - 'transport_ports' => transportports, - 'http_bound_addresses' => http_bound_addresses, - 'transport_bound_addresses' => transport_bound_addresses, - 'transport_publish_addresses' => transport_publish_addresses, - json_data['name'] => { 'settings' => nodes_data['settings'], 'http' => nodes_data['http'], 'transport' => nodes_data['transport'] } } + node = { + 'http_ports' => httpports.keys, + 'transport_ports' => transportports, + 'http_bound_addresses' => http_bound_addresses, + 'transport_bound_addresses' => transport_bound_addresses, + 'transport_publish_addresses' => transport_publish_addresses, + json_data['name'] => { + 'settings' => nodes_data['settings'], + 'http' => nodes_data['http'], + 'transport' => nodes_data['transport'] + } + } nodes.merge! node end end rescue end Facter.add(:elasticsearch) do setcode do nodes end nodes unless nodes.empty? end end # rubocop:enable Metrics/CyclomaticComplexity # rubocop:enable Metrics/PerceivedComplexity end EsFacts.run diff --git a/spec/classes/000_elasticsearch_init_spec.rb b/spec/classes/000_elasticsearch_init_spec.rb index a7fc102..1bd73cc 100644 --- a/spec/classes/000_elasticsearch_init_spec.rb +++ b/spec/classes/000_elasticsearch_init_spec.rb @@ -1,438 +1,440 @@ require 'spec_helper' describe 'elasticsearch', :type => 'class' do default_params = { :config => { 'node.name' => 'foo' } } on_supported_os.each do |os, facts| context "on #{os}" do case facts[:os]['family'] when 'Debian' let(:defaults_path) { '/etc/default' } let(:system_service_folder) { '/lib/systemd/system' } let(:pkg_ext) { 'deb' } let(:pkg_prov) { 'dpkg' } let(:version_add) { '' } if (facts[:os]['name'] == 'Debian' and \ facts[:os]['release']['major'].to_i >= 8) or \ (facts[:os]['name'] == 'Ubuntu' and \ facts[:os]['release']['major'].to_i >= 15) let(:systemd_service_path) { '/lib/systemd/system' } test_pid = true else test_pid = false end when 'RedHat' let(:defaults_path) { '/etc/sysconfig' } let(:system_service_folder) { '/lib/systemd/system' } let(:pkg_ext) { 'rpm' } let(:pkg_prov) { 'rpm' } let(:version_add) { '-1' } if facts[:os]['release']['major'].to_i >= 7 let(:systemd_service_path) { '/lib/systemd/system' } test_pid = true else test_pid = false end when 'Suse' let(:defaults_path) { '/etc/sysconfig' } let(:pkg_ext) { 'rpm' } let(:pkg_prov) { 'rpm' } let(:version_add) { '-1' } if facts[:os]['name'] == 'OpenSuSE' and facts[:os]['release']['major'].to_i <= 12 let(:systemd_service_path) { '/lib/systemd/system' } else let(:systemd_service_path) { '/usr/lib/systemd/system' } end end let(:facts) do - facts.merge('scenario' => '', 'common' => '') + facts.merge('scenario' => '', 'common' => '', 'elasticsearch' => {}) end let(:params) do default_params.merge({}) end + it { should compile.with_all_deps } + # Varies depending on distro it { should contain_augeas("#{defaults_path}/elasticsearch") } # Systemd-specific files if test_pid == true it { should contain_service('elasticsearch').with( :ensure => 'running', :enable => true ) } end context 'java installation' do let(:pre_condition) do <<-MANIFEST include ::java MANIFEST end it { should contain_class('elasticsearch::config') .that_requires('Class[java]') } end context 'package installation' do context 'via repository' do context 'with specified version' do let(:params) do default_params.merge( :version => '1.0' ) end it { should contain_package('elasticsearch') .with(:ensure => "1.0#{version_add}") } end if facts[:os]['family'] == 'RedHat' context 'Handle special CentOS/RHEL package versioning' do let(:params) do default_params.merge( :version => '1.1-2' ) end it { should contain_package('elasticsearch') .with(:ensure => '1.1-2') } end end end context 'when setting package version and package_url' do let(:params) do default_params.merge( :version => '0.90.10', :package_url => "puppet:///path/to/some/es-0.90.10.#{pkg_ext}" ) end it { expect { should raise_error(Puppet::Error) } } end context 'via package_url setting' do ['file:/', 'ftp://', 'http://', 'https://', 'puppet:///'].each do |schema| context "using #{schema} schema" do let(:params) do default_params.merge( :package_url => "#{schema}domain-or-path/pkg.#{pkg_ext}" ) end unless schema.start_with? 'puppet' it { should contain_exec('create_package_dir_elasticsearch') .with(:command => 'mkdir -p /opt/elasticsearch/swdl') } it { should contain_file('/opt/elasticsearch/swdl') .with( :purge => false, :force => false, :require => 'Exec[create_package_dir_elasticsearch]' ) } end case schema when 'file:/' it { should contain_file( "/opt/elasticsearch/swdl/pkg.#{pkg_ext}" ).with( :source => "/domain-or-path/pkg.#{pkg_ext}", :backup => false ) } when 'puppet:///' it { should contain_file( "/opt/elasticsearch/swdl/pkg.#{pkg_ext}" ).with( :source => "#{schema}domain-or-path/pkg.#{pkg_ext}", :backup => false ) } else [true, false].each do |verify_certificates| context "with download_tool_verify_certificates '#{verify_certificates}'" do let(:params) do default_params.merge( :package_url => "#{schema}domain-or-path/pkg.#{pkg_ext}", :download_tool_verify_certificates => verify_certificates ) end flag = (not verify_certificates) ? ' --no-check-certificate' : '' it { should contain_exec('download_package_elasticsearch') .with( :command => "wget#{flag} -O /opt/elasticsearch/swdl/pkg.#{pkg_ext} #{schema}domain-or-path/pkg.#{pkg_ext} 2> /dev/null", :require => 'File[/opt/elasticsearch/swdl]' ) } end end end it { should contain_package('elasticsearch') .with( :ensure => 'present', :source => "/opt/elasticsearch/swdl/pkg.#{pkg_ext}", :provider => pkg_prov ) } end end context 'using http:// schema with proxy_url' do let(:params) do default_params.merge( :package_url => "http://www.domain.com/package.#{pkg_ext}", :proxy_url => 'http://proxy.example.com:12345/' ) end it { should contain_exec('download_package_elasticsearch') .with( :environment => [ 'use_proxy=yes', 'http_proxy=http://proxy.example.com:12345/', 'https_proxy=http://proxy.example.com:12345/' ] ) } end end end # package context 'when setting the module to absent' do let(:params) do default_params.merge( :ensure => 'absent' ) end case facts[:os]['family'] when 'Suse' it { should contain_package('elasticsearch') .with(:ensure => 'absent') } else it { should contain_package('elasticsearch') .with(:ensure => 'purged') } end it { should contain_service('elasticsearch') .with( :ensure => 'stopped', :enable => 'false' ) } it { should contain_file('/usr/share/elasticsearch/plugins') .with(:ensure => 'absent') } it { should contain_file("#{defaults_path}/elasticsearch") .with(:ensure => 'absent') } end context 'When managing the repository' do let(:params) do default_params.merge( :manage_repo => true ) end it { should contain_class('elastic_stack::repo') } end context 'When not managing the repository' do let(:params) do default_params.merge( :manage_repo => false ) end it { should compile.with_all_deps } end end end on_supported_os( :hardwaremodels => ['x86_64'], :supported_os => [ { 'operatingsystem' => 'CentOS', 'operatingsystemrelease' => ['7'] } ] ).each do |os, facts| context "on #{os}" do let(:facts) { facts.merge( :scenario => '', :common => '' ) } describe 'main class tests' do # init.pp it { should compile.with_all_deps } it { should contain_class('elasticsearch') } it { should contain_class('elasticsearch::package') } it { should contain_class('elasticsearch::config') .that_requires('Class[elasticsearch::package]') } it { should contain_class('elasticsearch::service') .that_requires('Class[elasticsearch::config]') } # Base directories it { should contain_file('/etc/elasticsearch') } it { should contain_file('/usr/share/elasticsearch') } it { should contain_file('/usr/share/elasticsearch/lib') } it { should contain_file('/var/lib/elasticsearch') } it { should contain_exec('remove_plugin_dir') } end context 'package installation' do describe 'with default package' do it { should contain_package('elasticsearch') .with(:ensure => 'present') } it { should_not contain_package('my-elasticsearch') .with(:ensure => 'present') } end describe 'with specified package name' do let(:params) do default_params.merge( :package_name => 'my-elasticsearch' ) end it { should contain_package('elasticsearch') .with(:ensure => 'present', :name => 'my-elasticsearch') } it { should_not contain_package('elasticsearch') .with(:ensure => 'present', :name => 'elasticsearch') } end describe 'with auto upgrade enabled' do let(:params) do default_params.merge( :autoupgrade => true ) end it { should contain_package('elasticsearch') .with(:ensure => 'latest') } end end describe 'running a a different user' do let(:params) do default_params.merge( :elasticsearch_user => 'myesuser', :elasticsearch_group => 'myesgroup' ) end it { should contain_file('/etc/elasticsearch') .with(:owner => 'myesuser', :group => 'myesgroup') } it { should contain_file('/var/log/elasticsearch') .with(:owner => 'myesuser') } it { should contain_file('/usr/share/elasticsearch') .with(:owner => 'myesuser', :group => 'myesgroup') } it { should contain_file('/var/lib/elasticsearch') .with(:owner => 'myesuser', :group => 'myesgroup') } end describe 'setting jvm_options' do jvm_options = [ '-Xms16g', '-Xmx16g' ] let(:params) do default_params.merge( :jvm_options => jvm_options ) end jvm_options.each do |jvm_option| it { should contain_file_line("jvm_option_#{jvm_option}") .with( :ensure => 'present', :path => '/etc/elasticsearch/jvm.options', :line => jvm_option )} end end context 'with restart_on_change => true' do let(:params) do default_params.merge( :restart_on_change => true ) end describe 'should restart elasticsearch' do it { should contain_file('/etc/elasticsearch/elasticsearch.yml') .that_notifies('Service[elasticsearch]')} end describe 'setting jvm_options triggers restart' do let(:params) do super().merge( :jvm_options => ['-Xmx16g'] ) end it { should contain_file_line('jvm_option_-Xmx16g') .that_notifies('Service[elasticsearch]')} end end # This check helps catch dependency cycles. context 'create_resource' do # Helper for these tests def singular(s) case s when 'indices' 'index' when 'snapshot_repositories' 'snapshot_repository' else s[0..-2] end end { 'indices' => { 'test-index' => {} }, # 'instances' => { 'es-instance' => {} }, 'pipelines' => { 'testpipeline' => { 'content' => {} } }, 'plugins' => { 'head' => {} }, 'roles' => { 'elastic_role' => {} }, 'scripts' => { 'foo' => { 'source' => 'puppet:///path/to/foo.groovy' } }, 'snapshot_repositories' => { 'backup' => { 'location' => '/backups' } }, 'templates' => { 'foo' => { 'content' => {} } }, 'users' => { 'elastic' => { 'password' => 'foobar' } } }.each_pair do |deftype, params| describe deftype do let(:params) do default_params.merge( deftype => params ) end it { should compile } it { should send( "contain_elasticsearch__#{singular(deftype)}", params.keys.first ) } end end end describe 'oss' do let(:params) do default_params.merge(:oss => true) end it do should contain_package('elasticsearch').with( :name => 'elasticsearch-oss' ) end end end end end diff --git a/spec/fixtures/facts/v5-nodes.json b/spec/fixtures/facts/v5-nodes.json deleted file mode 100644 index 7e6dc16..0000000 --- a/spec/fixtures/facts/v5-nodes.json +++ /dev/null @@ -1,371 +0,0 @@ -{ - "_nodes" : { - "total" : 1, - "successful" : 1, - "failed" : 0 - }, - "cluster_name" : "elasticsearch", - "nodes" : { - "9lRSXfREQnqIgBWP0FBi0Q" : { - "name" : "v5", - "transport_address" : "127.0.0.1:9300", - "host" : "127.0.0.1", - "ip" : "127.0.0.1", - "version" : "5.5.0", - "build_hash" : "260387d", - "total_indexing_buffer" : 211261849, - "roles" : [ - "master", - "data", - "ingest" - ], - "settings" : { - "client" : { - "type" : "node" - }, - "cluster" : { - "name" : "elasticsearch" - }, - "http" : { - "type" : { - "default" : "netty4" - }, - "port" : "9202" - }, - "node" : { - "name" : "v5" - }, - "path" : { - "logs" : "/Users/tylerjl/Work/elasticsearch-5.5.0/logs", - "home" : "/Users/tylerjl/Work/elasticsearch-5.5.0" - }, - "transport" : { - "type" : { - "default" : "netty4" - } - } - }, - "os" : { - "refresh_interval_in_millis" : 1000, - "name" : "Mac OS X", - "arch" : "x86_64", - "version" : "10.12.5", - "available_processors" : 4, - "allocated_processors" : 4 - }, - "process" : { - "refresh_interval_in_millis" : 1000, - "id" : 16828, - "mlockall" : false - }, - "jvm" : { - "pid" : 16828, - "version" : "1.8.0_121", - "vm_name" : "Java HotSpot(TM) 64-Bit Server VM", - "vm_version" : "25.121-b13", - "vm_vendor" : "Oracle Corporation", - "start_time_in_millis" : 1502814708699, - "mem" : { - "heap_init_in_bytes" : 2147483648, - "heap_max_in_bytes" : 2112618496, - "non_heap_init_in_bytes" : 2555904, - "non_heap_max_in_bytes" : 0, - "direct_max_in_bytes" : 2112618496 - }, - "gc_collectors" : [ - "ParNew", - "ConcurrentMarkSweep" - ], - "memory_pools" : [ - "Code Cache", - "Metaspace", - "Compressed Class Space", - "Par Eden Space", - "Par Survivor Space", - "CMS Old Gen" - ], - "using_compressed_ordinary_object_pointers" : "true", - "input_arguments" : [ - "-Xms2g", - "-Xmx2g", - "-XX:+UseConcMarkSweepGC", - "-XX:CMSInitiatingOccupancyFraction=75", - "-XX:+UseCMSInitiatingOccupancyOnly", - "-XX:+AlwaysPreTouch", - "-Xss1m", - "-Djava.awt.headless=true", - "-Dfile.encoding=UTF-8", - "-Djna.nosys=true", - "-Djdk.io.permissionsUseCanonicalPath=true", - "-Dio.netty.noUnsafe=true", - "-Dio.netty.noKeySetOptimization=true", - "-Dio.netty.recycler.maxCapacityPerThread=0", - "-Dlog4j.shutdownHookEnabled=false", - "-Dlog4j2.disable.jmx=true", - "-Dlog4j.skipJansi=true", - "-XX:+HeapDumpOnOutOfMemoryError", - "-Des.path.home=/Users/tylerjl/Work/elasticsearch-5.5.0" - ] - }, - "thread_pool" : { - "force_merge" : { - "type" : "fixed", - "min" : 1, - "max" : 1, - "queue_size" : -1 - }, - "fetch_shard_started" : { - "type" : "scaling", - "min" : 1, - "max" : 8, - "keep_alive" : "5m", - "queue_size" : -1 - }, - "listener" : { - "type" : "fixed", - "min" : 2, - "max" : 2, - "queue_size" : -1 - }, - "index" : { - "type" : "fixed", - "min" : 4, - "max" : 4, - "queue_size" : 200 - }, - "refresh" : { - "type" : "scaling", - "min" : 1, - "max" : 2, - "keep_alive" : "5m", - "queue_size" : -1 - }, - "generic" : { - "type" : "scaling", - "min" : 4, - "max" : 128, - "keep_alive" : "30s", - "queue_size" : -1 - }, - "warmer" : { - "type" : "scaling", - "min" : 1, - "max" : 2, - "keep_alive" : "5m", - "queue_size" : -1 - }, - "search" : { - "type" : "fixed", - "min" : 7, - "max" : 7, - "queue_size" : 1000 - }, - "flush" : { - "type" : "scaling", - "min" : 1, - "max" : 2, - "keep_alive" : "5m", - "queue_size" : -1 - }, - "fetch_shard_store" : { - "type" : "scaling", - "min" : 1, - "max" : 8, - "keep_alive" : "5m", - "queue_size" : -1 - }, - "management" : { - "type" : "scaling", - "min" : 1, - "max" : 5, - "keep_alive" : "5m", - "queue_size" : -1 - }, - "get" : { - "type" : "fixed", - "min" : 4, - "max" : 4, - "queue_size" : 1000 - }, - "bulk" : { - "type" : "fixed", - "min" : 4, - "max" : 4, - "queue_size" : 200 - }, - "snapshot" : { - "type" : "scaling", - "min" : 1, - "max" : 2, - "keep_alive" : "5m", - "queue_size" : -1 - } - }, - "transport" : { - "bound_address" : [ - "[fe80::1]:9300", - "[::1]:9300", - "127.0.0.1:9300" - ], - "publish_address" : "127.0.0.1:9300", - "profiles" : { } - }, - "http" : { - "bound_address" : [ - "[fe80::1]:9202", - "[::1]:9202", - "127.0.0.1:9202" - ], - "publish_address" : "127.0.0.1:9202", - "max_content_length_in_bytes" : 104857600 - }, - "plugins" : [ ], - "modules" : [ - { - "name" : "aggs-matrix-stats", - "version" : "5.5.0", - "description" : "Adds aggregations whose input are a list of numeric fields and output includes a matrix.", - "classname" : "org.elasticsearch.search.aggregations.matrix.MatrixAggregationPlugin", - "has_native_controller" : false - }, - { - "name" : "ingest-common", - "version" : "5.5.0", - "description" : "Module for ingest processors that do not require additional security permissions or have large dependencies and resources", - "classname" : "org.elasticsearch.ingest.common.IngestCommonPlugin", - "has_native_controller" : false - }, - { - "name" : "lang-expression", - "version" : "5.5.0", - "description" : "Lucene expressions integration for Elasticsearch", - "classname" : "org.elasticsearch.script.expression.ExpressionPlugin", - "has_native_controller" : false - }, - { - "name" : "lang-groovy", - "version" : "5.5.0", - "description" : "Groovy scripting integration for Elasticsearch", - "classname" : "org.elasticsearch.script.groovy.GroovyPlugin", - "has_native_controller" : false - }, - { - "name" : "lang-mustache", - "version" : "5.5.0", - "description" : "Mustache scripting integration for Elasticsearch", - "classname" : "org.elasticsearch.script.mustache.MustachePlugin", - "has_native_controller" : false - }, - { - "name" : "lang-painless", - "version" : "5.5.0", - "description" : "An easy, safe and fast scripting language for Elasticsearch", - "classname" : "org.elasticsearch.painless.PainlessPlugin", - "has_native_controller" : false - }, - { - "name" : "parent-join", - "version" : "5.5.0", - "description" : "This module adds the support parent-child queries and aggregations", - "classname" : "org.elasticsearch.join.ParentJoinPlugin", - "has_native_controller" : false - }, - { - "name" : "percolator", - "version" : "5.5.0", - "description" : "Percolator module adds capability to index queries and query these queries by specifying documents", - "classname" : "org.elasticsearch.percolator.PercolatorPlugin", - "has_native_controller" : false - }, - { - "name" : "reindex", - "version" : "5.5.0", - "description" : "The Reindex module adds APIs to reindex from one index to another or update documents in place.", - "classname" : "org.elasticsearch.index.reindex.ReindexPlugin", - "has_native_controller" : false - }, - { - "name" : "transport-netty3", - "version" : "5.5.0", - "description" : "Netty 3 based transport implementation", - "classname" : "org.elasticsearch.transport.Netty3Plugin", - "has_native_controller" : false - }, - { - "name" : "transport-netty4", - "version" : "5.5.0", - "description" : "Netty 4 based transport implementation", - "classname" : "org.elasticsearch.transport.Netty4Plugin", - "has_native_controller" : false - } - ], - "ingest" : { - "processors" : [ - { - "type" : "append" - }, - { - "type" : "convert" - }, - { - "type" : "date" - }, - { - "type" : "date_index_name" - }, - { - "type" : "dot_expander" - }, - { - "type" : "fail" - }, - { - "type" : "foreach" - }, - { - "type" : "grok" - }, - { - "type" : "gsub" - }, - { - "type" : "join" - }, - { - "type" : "json" - }, - { - "type" : "kv" - }, - { - "type" : "lowercase" - }, - { - "type" : "remove" - }, - { - "type" : "rename" - }, - { - "type" : "script" - }, - { - "type" : "set" - }, - { - "type" : "sort" - }, - { - "type" : "split" - }, - { - "type" : "trim" - }, - { - "type" : "uppercase" - } - ] - } - } - } -} diff --git a/spec/fixtures/facts/v5-root.json b/spec/fixtures/facts/v5-root.json deleted file mode 100644 index 62ac5ee..0000000 --- a/spec/fixtures/facts/v5-root.json +++ /dev/null @@ -1,13 +0,0 @@ -{ - "name" : "v5", - "cluster_name" : "elasticsearch", - "cluster_uuid" : "1ENUnFlUQcmF6qko-PjpHw", - "version" : { - "number" : "5.5.0", - "build_hash" : "260387d", - "build_date" : "2017-06-30T23:16:05.735Z", - "build_snapshot" : false, - "lucene_version" : "6.6.0" - }, - "tagline" : "You Know, for Search" -} diff --git a/spec/unit/facter/es_facts_spec.rb b/spec/unit/facter/es_facts_spec.rb index 4aae002..d6bfe9e 100644 --- a/spec/unit/facter/es_facts_spec.rb +++ b/spec/unit/facter/es_facts_spec.rb @@ -1,137 +1,107 @@ require 'spec_helper' require 'webmock/rspec' describe 'elasticsearch facts' do before(:each) do - Dir[File.join(RSpec.configuration.fixture_path, 'facts', '*.json')].map do |json| - File.basename(json).split('.').first.split('-').first - end.uniq.sort.each_with_index do |instance, n| - stub_request(:get, "http://localhost:920#{n}/") - .with(:headers => { 'Accept' => '*/*', 'User-Agent' => 'Ruby' }) - .to_return( - :status => 200, - :body => File.read( - File.join( - fixture_path, - "facts/#{instance}-root.json" - ) + stub_request(:get, 'http://localhost:9200/') + .with(:headers => { 'Accept' => '*/*', 'User-Agent' => 'Ruby' }) + .to_return( + :status => 200, + :body => File.read( + File.join( + fixture_path, + 'facts/Warlock-root.json' ) ) + ) - stub_request(:get, "http://localhost:920#{n}/_nodes/#{instance}") - .with(:headers => { 'Accept' => '*/*', 'User-Agent' => 'Ruby' }) - .to_return( - :status => 200, - :body => File.read( - File.join( - fixture_path, - "facts/#{instance}-nodes.json" - ) + stub_request(:get, 'http://localhost:9200/_nodes/Warlock') + .with(:headers => { 'Accept' => '*/*', 'User-Agent' => 'Ruby' }) + .to_return( + :status => 200, + :body => File.read( + File.join( + fixture_path, + 'facts/Warlock-nodes.json' ) ) - end + ) allow(File) .to receive(:directory?) .and_return(true) - allow(Dir) - .to receive(:foreach) - .and_yield('es01').and_yield('es02').and_yield('es03').and_yield('es-ssl') - - %w[es01 es02 es03 es-ssl].each do |instance| - allow(File) - .to receive(:readable?) - .with("/etc/elasticsearch/#{instance}/elasticsearch.yml") - .and_return(true) - end + allow(File) + .to receive(:readable?) + .and_return(true) allow(YAML) .to receive(:load_file) - .with('/etc/elasticsearch/es01/elasticsearch.yml', any_args) + .with('/etc/elasticsearch/elasticsearch.yml', any_args) .and_return({}) - allow(YAML) - .to receive(:load_file) - .with('/etc/elasticsearch/es02/elasticsearch.yml', any_args) - .and_return('http.port' => '9201') - - allow(YAML) - .to receive(:load_file) - .with('/etc/elasticsearch/es03/elasticsearch.yml', any_args) - .and_return('http.port' => '9202') - - allow(YAML) - .to receive(:load_file) - .with('/etc/elasticsearch/es-ssl/elasticsearch.yml', any_args) - .and_return( - 'xpack.security.http.ssl.enabled' => true, - 'shield.http.ssl' => true, - 'http.port' => '9443' - ) - require 'lib/facter/es_facts' end - describe 'elasticsearch_ports' do - it 'finds listening ports' do - expect(Facter.fact(:elasticsearch_ports).value.split(',')) - .to contain_exactly('9200', '9201', '9202', '9443') + describe 'elasticsearch_port' do + it 'finds listening port' do + expect(Facter.fact(:elasticsearch_port).value) + .to eq('9200') end end describe 'instance' do it 'returns the node name' do - expect(Facter.fact(:elasticsearch_9200_name).value).to eq('Warlock') + expect(Facter.fact(:elasticsearch_name).value).to eq('Warlock') end it 'returns the node version' do - expect(Facter.fact(:elasticsearch_9200_version).value).to eq('1.4.2') + expect(Facter.fact(:elasticsearch_version).value).to eq('1.4.2') end it 'returns the cluster name' do - expect(Facter.fact(:elasticsearch_9200_cluster_name).value) + expect(Facter.fact(:elasticsearch_cluster_name).value) .to eq('elasticsearch') end it 'returns the node ID' do - expect(Facter.fact(:elasticsearch_9200_node_id).value) + expect(Facter.fact(:elasticsearch_node_id).value) .to eq('yQAWBO3FS8CupZnSvAVziQ') end it 'returns the mlockall boolean' do - expect(Facter.fact(:elasticsearch_9200_mlockall).value).to be_falsy + expect(Facter.fact(:elasticsearch_mlockall).value).to be_falsy end it 'returns installed plugins' do - expect(Facter.fact(:elasticsearch_9200_plugins).value).to eq('kopf') + expect(Facter.fact(:elasticsearch_plugins).value).to eq('kopf') end describe 'kopf plugin' do it 'returns the correct version' do - expect(Facter.fact(:elasticsearch_9200_plugin_kopf_version).value) + expect(Facter.fact(:elasticsearch_plugin_kopf_version).value) .to eq('1.4.3') end it 'returns the correct description' do - expect(Facter.fact(:elasticsearch_9200_plugin_kopf_description).value) + expect(Facter.fact(:elasticsearch_plugin_kopf_description).value) .to eq('kopf - simple web administration tool for ElasticSearch') end it 'returns the plugin URL' do - expect(Facter.fact(:elasticsearch_9200_plugin_kopf_url).value) + expect(Facter.fact(:elasticsearch_plugin_kopf_url).value) .to eq('/_plugin/kopf/') end it 'returns the plugin JVM boolean' do - expect(Facter.fact(:elasticsearch_9200_plugin_kopf_jvm).value) + expect(Facter.fact(:elasticsearch_plugin_kopf_jvm).value) .to be_falsy end it 'returns the plugin _site boolean' do - expect(Facter.fact(:elasticsearch_9200_plugin_kopf_site).value) + expect(Facter.fact(:elasticsearch_plugin_kopf_site).value) .to be_truthy end end # of describe plugin end # of describe instance end # of describe elasticsearch facts