j-enc 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (48) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +15 -0
  3. data/Gemfile +4 -0
  4. data/LICENSE.txt +22 -0
  5. data/README.md +31 -0
  6. data/Rakefile +2 -0
  7. data/bin/j-enc +12 -0
  8. data/j-enc.gemspec +28 -0
  9. data/j-enc.iml +24 -0
  10. data/lib/enc/builder.rb +28 -0
  11. data/lib/enc/cache/exceptions.rb +6 -0
  12. data/lib/enc/cache/node_cache.rb +57 -0
  13. data/lib/enc/cache.rb +2 -0
  14. data/lib/enc/collins_helper/api.rb +43 -0
  15. data/lib/enc/collins_helper/connection.rb +26 -0
  16. data/lib/enc/collins_helper/node/exceptions.rb +9 -0
  17. data/lib/enc/collins_helper/node/node_asset.rb +135 -0
  18. data/lib/enc/collins_helper/node.rb +2 -0
  19. data/lib/enc/collins_helper.rb +3 -0
  20. data/lib/enc/config.rb +63 -0
  21. data/lib/enc/runner.rb +58 -0
  22. data/lib/enc/utils/logging.rb +68 -0
  23. data/lib/enc/utils.rb +1 -0
  24. data/lib/enc/version.rb +3 -0
  25. data/lib/enc.rb +38 -0
  26. data/spec/api_spec.rb +42 -0
  27. data/spec/builder_spec.rb +43 -0
  28. data/spec/cli_helper.rb +22 -0
  29. data/spec/config_spec.rb +15 -0
  30. data/spec/connection_spec.rb +9 -0
  31. data/spec/enc_spec.rb +25 -0
  32. data/spec/factories/api.rb +60 -0
  33. data/spec/factories/builder.rb +27 -0
  34. data/spec/factories/cache.rb +9 -0
  35. data/spec/factories/config.rb +21 -0
  36. data/spec/factories/connection.rb +11 -0
  37. data/spec/factories/node_asset.rb +20 -0
  38. data/spec/factories/response.rb +49 -0
  39. data/spec/factories/runner.rb +19 -0
  40. data/spec/factories.rb +21 -0
  41. data/spec/features/external_request_spec.rb +9 -0
  42. data/spec/node_asset_spec.rb +54 -0
  43. data/spec/node_cache_spec.rb +25 -0
  44. data/spec/runner_spec.rb +41 -0
  45. data/spec/spec_helper.rb +52 -0
  46. data/spec/support/factory_girl.rb +8 -0
  47. data/spec/support/utils.rb +37 -0
  48. metadata +197 -0
data/lib/enc.rb ADDED
@@ -0,0 +1,38 @@
1
+ require 'enc/version'
2
+ require 'enc/utils'
3
+ require 'enc/cache'
4
+ require 'enc/config'
5
+ require 'enc/collins_helper'
6
+ require 'enc/runner'
7
+ require 'enc/builder'
8
+
9
+ module Enc
10
+ class << self
11
+ HELP_FLAGS = %w(-h --help)
12
+
13
+ include Enc::Utils::Logging
14
+
15
+ def run(args)
16
+ node = args[0]
17
+ config_file = args[1]
18
+ if not node
19
+ logger.fatal('ENC called with no arguments!')
20
+ usage
21
+ elsif HELP_FLAGS.include?(node)
22
+ usage
23
+ else
24
+ logger.info("Starting external node classifier for node #{node}")
25
+ runner = Runner.new(Enc::Config.new(config_file))
26
+ node_builder = runner.build(node)
27
+ puts node_builder.to_yaml
28
+ logger.info("Completed external node classifier for node #{node}")
29
+ end
30
+ end
31
+
32
+ protected
33
+
34
+ def usage
35
+ puts 'Usage: j-enc <fqdn> [config_file]'
36
+ end
37
+ end
38
+ end
data/spec/api_spec.rb ADDED
@@ -0,0 +1,42 @@
1
+ require 'spec_helper'
2
+ require 'collins_client'
3
+
4
+ describe 'Collins API' do
5
+ it 'gets an asset' do
6
+ api = FactoryGirl.build(:api)
7
+ asset = api.safe_find(:hostname => FactoryDefaults::MOCK_ASSET_HOSTNAME,
8
+ :details => true)
9
+ expect(asset).to be_an_instance_of(Enc::CollinsHelper::Node::NodeAsset)
10
+ expect(asset.get_attribute('HOSTNAME')).to eq(FactoryDefaults::MOCK_ASSET_HOSTNAME)
11
+ end
12
+
13
+ it 'searches for missing asset' do
14
+ api = FactoryGirl.build(:api)
15
+ expect{api.safe_find(:hostname => 'missing', :details => true)}.
16
+ to raise_exception(Enc::CollinsHelper::Api::NoAssets, 'No assets found')
17
+ end
18
+
19
+ it 'seaches asset using invalid collins host' do
20
+ api = FactoryGirl.build(:api_timeout)
21
+ expect{api.safe_find(:hostname => FactoryDefaults::MOCK_ASSET_HOSTNAME, :details => true)}.
22
+ to raise_exception(Enc::CollinsHelper::Api::CannotConnect)
23
+ end
24
+
25
+ it 'seaches asset using invalid credentials' do
26
+ api = FactoryGirl.build(:api_unauthorized)
27
+ expect{api.safe_find(:hostname => FactoryDefaults::MOCK_ASSET_HOSTNAME, :details => true)}.
28
+ to raise_exception(Collins::AuthenticationError)
29
+ end
30
+
31
+ it 'seaches asset using invalid role' do
32
+ api = FactoryGirl.build(:api_forbidden)
33
+ expect{api.safe_find(:hostname => FactoryDefaults::MOCK_ASSET_HOSTNAME, :details => true)}.
34
+ to raise_exception(Collins::RequestError)
35
+ end
36
+
37
+ it 'seaches asset and gets an unknown error' do
38
+ api = FactoryGirl.build(:api_error)
39
+ expect{api.safe_find(:hostname => FactoryDefaults::MOCK_ASSET_HOSTNAME, :details => true)}.
40
+ to raise_exception(Collins::RequestError)
41
+ end
42
+ end
@@ -0,0 +1,43 @@
1
+ require 'spec_helper'
2
+ require 'yaml'
3
+
4
+ describe 'ENC Builder' do
5
+ it 'builds the asset in yaml' do
6
+ node = FactoryGirl.build(:node_asset)
7
+ builder = FactoryGirl.build(:builder)
8
+ expect(builder.to_yaml).to be_an_instance_of(String)
9
+ expect(YAML.load(builder.to_yaml)).to be_an_instance_of(Hash)
10
+ expect(YAML.load(builder.to_yaml)['parameters']['deployment_environment']).to eq(node.get_deployment_environment)
11
+ end
12
+
13
+ it 'loads the asset from yaml' do
14
+ node = FactoryGirl.build(:node_asset)
15
+ builder = FactoryGirl.build(:builder)
16
+ expect(YAML.load(builder.to_yaml)).to be_an_instance_of(Hash)
17
+ expect(YAML.load(builder.to_yaml)['parameters']['deployment_environment']).to eq(node.get_deployment_environment)
18
+ end
19
+
20
+ it 'builds the required enc output when the enc is enabled' do
21
+ node = FactoryGirl.build(:node_asset)
22
+ builder = FactoryGirl.build(:builder)
23
+ expect(YAML.load(builder.to_yaml)).to be_an_instance_of(Hash)
24
+ expect(YAML.load(builder.to_yaml)['parameters']['deployment_environment']).to eq(node.get_deployment_environment)
25
+ expect(YAML.load(builder.to_yaml)['parameters']['datacenter']).to eq(node.get_datacenter)
26
+ expect(YAML.load(builder.to_yaml)['parameters']['roles']).to eq(node.get_roles)
27
+ expect(YAML.load(builder.to_yaml)['parameters']['role1']).to eq(node.get_roles_by_index[0])
28
+ expect(YAML.load(builder.to_yaml)['parameters']['role2']).to eq(node.get_roles_by_index[1])
29
+ expect(YAML.load(builder.to_yaml)['parameters']['role3']).to eq(node.get_roles_by_index[2])
30
+ expect(YAML.load(builder.to_yaml)['parameters']['collins']).to eq(node.get_flattened_attributes)
31
+ expect(YAML.load(builder.to_yaml)['parameters']['collins']['HOSTNAME']).to eq(node.get_hostname)
32
+ end
33
+
34
+ it 'builds the required enc output when the enc is not enabled' do
35
+ builder = FactoryGirl.build(:builder_enc_disabled)
36
+ expect(builder.to_yaml).to eq("--- ''\n")
37
+ end
38
+
39
+ it 'builds the required enc output when the asset does not exist' do
40
+ builder = FactoryGirl.build(:builder_blank_asset)
41
+ expect(builder.to_yaml).to eq("--- ''\n")
42
+ end
43
+ end
@@ -0,0 +1,22 @@
1
+ RSpec.configure do |config|
2
+ config.before do
3
+ ARGV.replace []
4
+ end
5
+
6
+ config.expect_with :rspec do |c|
7
+ c.syntax = :expect
8
+ end
9
+
10
+ def capture(stream)
11
+ begin
12
+ stream = stream.to_s
13
+ eval "$#{stream} = StringIO.new"
14
+ yield
15
+ result = eval("$#{stream}").string
16
+ ensure
17
+ eval("$#{stream} = #{stream.upcase}")
18
+ end
19
+ result
20
+ end
21
+
22
+ end
@@ -0,0 +1,15 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'ENC Config' do
4
+ before :all do
5
+ @config = FactoryGirl.build(:config)
6
+ end
7
+
8
+ it 'sets a valid configuration' do
9
+ expect(@config.get('host')).to eq(FactoryDefaults::MOCK_COLLINS_URL)
10
+ expect(@config.get('username')).to eq(FactoryDefaults::MOCK_COLLINS_USERNAME)
11
+ expect(@config.get('password')).to eq(FactoryDefaults::MOCK_COLLINS_PASSWORD)
12
+ expect(@config.get('timeout')).to eq(FactoryDefaults::MOCK_COLLINS_TIMEOUT)
13
+ expect(@config.get('jive_enc_cache_dir')).to eq(FactoryDefaults::MOCK_ENC_CACHE_DIR)
14
+ end
15
+ end
@@ -0,0 +1,9 @@
1
+ require 'spec_helper'
2
+ require 'collins_client'
3
+
4
+ describe 'Collins Connection' do
5
+ it 'gets a client' do
6
+ connection = FactoryGirl.build(:connection)
7
+ expect(connection.api).to be_an_instance_of(Enc::CollinsHelper::Api)
8
+ end
9
+ end
data/spec/enc_spec.rb ADDED
@@ -0,0 +1,25 @@
1
+ require 'rspec'
2
+ require 'spec_helper'
3
+ require 'cli_helper'
4
+
5
+ describe 'CLI' do
6
+ it 'runs with no arguments' do
7
+ expect(capture(:stdout) { Enc.run([]) }).to match("Usage: j-enc <fqdn> [config_file]\n")
8
+ end
9
+
10
+ it 'runs with -h argument' do
11
+ expect(capture(:stdout) { Enc.run(['-h']) }).to match("Usage: j-enc <fqdn> [config_file]\n")
12
+ end
13
+
14
+ it 'runs with --help argument' do
15
+ expect(capture(:stdout) { Enc.run(['--help']) }).to eq("Usage: j-enc <fqdn> [config_file]\n")
16
+ end
17
+
18
+ it 'runs with a valid argument' do
19
+ config = FactoryGirl.build(:config)
20
+ config.create!
21
+ expect(YAML.load((capture(:stdout) { Enc.run([FactoryDefaults::MOCK_ASSET_HOSTNAME,
22
+ config.config_file]) }))).to be_an_instance_of(Hash)
23
+ config.delete!
24
+ end
25
+ end
@@ -0,0 +1,60 @@
1
+ require 'factories/config'
2
+
3
+ FactoryGirl.define do
4
+ factory :api, class: Enc::CollinsHelper::Api do
5
+ skip_create
6
+
7
+ config FactoryGirl.build(:config)
8
+
9
+ initialize_with { new(:host => config.get('host'),
10
+ :username => config.get('username'),
11
+ :password => config.get('password'),
12
+ :timeout => config.get('timeout'),
13
+ :strict => true) }
14
+ end
15
+
16
+ factory :api_timeout, class: Enc::CollinsHelper::Api do
17
+ skip_create
18
+
19
+ config FactoryGirl.build(:config, host: 'http://shouldneverexist99.jivehostedblah.com')
20
+
21
+ initialize_with { new(:host => config.get('host'),
22
+ :username => config.get('username'),
23
+ :password => config.get('password'),
24
+ :timeout => config.get('timeout'),
25
+ :strict => true) }
26
+ end
27
+ factory :api_unauthorized, class: Enc::CollinsHelper::Api do
28
+ skip_create
29
+
30
+ config FactoryGirl.build(:config, host: 'http://willreturn401.jivehostedblah.com')
31
+
32
+ initialize_with { new(:host => config.get('host'),
33
+ :username => config.get('username'),
34
+ :password => config.get('password'),
35
+ :timeout => config.get('timeout'),
36
+ :strict => true) }
37
+ end
38
+ factory :api_forbidden, class: Enc::CollinsHelper::Api do
39
+ skip_create
40
+
41
+ config FactoryGirl.build(:config, host: 'http://willreturn403.jivehostedblah.com')
42
+
43
+ initialize_with { new(:host => config.get('host'),
44
+ :username => config.get('username'),
45
+ :password => config.get('password'),
46
+ :timeout => config.get('timeout'),
47
+ :strict => true) }
48
+ end
49
+ factory :api_error, class: Enc::CollinsHelper::Api do
50
+ skip_create
51
+
52
+ config FactoryGirl.build(:config, host: 'http://willreturn500.jivehostedblah.com')
53
+
54
+ initialize_with { new(:host => config.get('host'),
55
+ :username => config.get('username'),
56
+ :password => config.get('password'),
57
+ :timeout => config.get('timeout'),
58
+ :strict => true) }
59
+ end
60
+ end
@@ -0,0 +1,27 @@
1
+ require 'factories/node_asset'
2
+
3
+ FactoryGirl.define do
4
+ factory :builder, class: Enc::Builder do
5
+ skip_create
6
+
7
+ node_asset FactoryGirl.build(:node_asset)
8
+
9
+ initialize_with { new(node_asset) }
10
+ end
11
+
12
+ factory :builder_enc_disabled, class: Enc::Builder do
13
+ skip_create
14
+
15
+ node_asset FactoryGirl.build(:node_asset, puppet_enc: false)
16
+
17
+ initialize_with { new(node_asset) }
18
+ end
19
+
20
+ factory :builder_blank_asset, class: Enc::Builder do
21
+ skip_create
22
+
23
+ node_asset FactoryGirl.build(:node_asset_blank)
24
+
25
+ initialize_with { new(node_asset) }
26
+ end
27
+ end
@@ -0,0 +1,9 @@
1
+ FactoryGirl.define do
2
+ factory :node_cache, class: Enc::Cache::NodeCache do
3
+ skip_create
4
+
5
+ config FactoryGirl.build(:config)
6
+
7
+ initialize_with { new(config) }
8
+ end
9
+ end
@@ -0,0 +1,21 @@
1
+ FactoryGirl.define do
2
+ factory :config, class: Enc::Config do
3
+ skip_create
4
+
5
+ config_file FactoryDefaults::MOCK_ENC_CONF_DIR + '/collins.yaml'
6
+ host FactoryDefaults::MOCK_COLLINS_URL
7
+ username FactoryDefaults::MOCK_COLLINS_USERNAME
8
+ password FactoryDefaults::MOCK_COLLINS_PASSWORD
9
+ log_file FactoryDefaults::MOCK_ENC_LOG_DIR + '/enc.log'
10
+ log_level 'debug'
11
+ cache_dir FactoryDefaults::MOCK_ENC_CACHE_DIR
12
+
13
+ initialize_with { new(config_file, {:host => host,
14
+ :username => username,
15
+ :password => password,
16
+ :jive_enc_log_file => log_file,
17
+ :jive_enc_log_level => log_level,
18
+ :jive_enc_cache_dir => cache_dir}, false) }
19
+
20
+ end
21
+ end
@@ -0,0 +1,11 @@
1
+ require 'factories/config'
2
+
3
+ FactoryGirl.define do
4
+ factory :connection, class: Enc::CollinsHelper::Connection do
5
+ skip_create
6
+
7
+ config FactoryGirl.build(:config)
8
+
9
+ initialize_with { new(config) }
10
+ end
11
+ end
@@ -0,0 +1,20 @@
1
+ FactoryGirl.define do
2
+ factory :node_asset, class: Enc::CollinsHelper::Node::NodeAsset do
3
+ skip_create
4
+
5
+ tag FactoryDefaults::MOCK_ASSET_TAG
6
+ hostname FactoryDefaults::MOCK_ASSET_HOSTNAME
7
+ datacenter FactoryDefaults::MOCK_ASSET_DATACENTER
8
+ environment FactoryDefaults::MOCK_ASSET_ENVIRONMENT
9
+ puppet_environment FactoryDefaults::MOCK_ASSET_ENVIRONMENT
10
+ puppet_enc true
11
+
12
+ initialize_with { new.class.from_json(Utils.get_asset_data(tag, hostname, datacenter, environment,
13
+ puppet_environment, puppet_enc)
14
+ ) }
15
+ end
16
+
17
+ factory :node_asset_blank, class: Enc::CollinsHelper::Node::NodeAsset do
18
+ skip_create
19
+ end
20
+ end
@@ -0,0 +1,49 @@
1
+ require 'factories/node_asset'
2
+
3
+ FactoryGirl.define do
4
+ factory :collins_response, class: Hash do
5
+ skip_create
6
+
7
+ tag FactoryDefaults::MOCK_ASSET_TAG
8
+ hostname FactoryDefaults::MOCK_ASSET_HOSTNAME
9
+ datacenter FactoryDefaults::MOCK_ASSET_DATACENTER
10
+ environment FactoryDefaults::MOCK_ASSET_ENVIRONMENT
11
+ puppet_environment FactoryDefaults::MOCK_ASSET_ENVIRONMENT
12
+ puppet_enc true
13
+
14
+ initialize_with { ({ 'status' => 'success:ok',
15
+ 'data' => { 'Pagination' => { 'PreviousPage' => 0,
16
+ 'CurrentPage' => 0,
17
+ 'NextPage' => 0,
18
+ 'TotalResults' => 1},
19
+ 'Data' => [Utils.get_asset_data(tag, hostname, datacenter, environment,
20
+ puppet_environment, puppet_enc)]}}
21
+ ) }
22
+ end
23
+
24
+ factory :collins_response_headers, class: Hash do
25
+ skip_create
26
+
27
+ x_pagination_nextpage '0'
28
+ content_language 'en'
29
+ x_pagination_totalresults '1'
30
+ x_pagination_currentpage '0'
31
+ content_type 'application/json; charset=utf-8'
32
+ x_pagination_previouspage '0'
33
+ content_length '883'
34
+ via '1.1 collins.dev.jivehosted.com'
35
+ connection 'close'
36
+
37
+ initialize_with { ({
38
+ 'x-pagination-nextpage' => [x_pagination_nextpage],
39
+ 'content-language' => [content_language],
40
+ 'x-pagination-totalresults' => [x_pagination_totalresults],
41
+ 'x-pagination-currentpage' => [x_pagination_currentpage],
42
+ 'content-type' => [content_type],
43
+ 'x-pagination-previouspage' => [x_pagination_previouspage],
44
+ 'content-length' => [content_length],
45
+ 'via' => [via],
46
+ 'connection' => [connection]}
47
+ ) }
48
+ end
49
+ end
@@ -0,0 +1,19 @@
1
+ require 'factories/config'
2
+
3
+ FactoryGirl.define do
4
+ factory :runner, class: Enc::Runner do
5
+ skip_create
6
+
7
+ config FactoryGirl.build(:config)
8
+
9
+ initialize_with { new(config) }
10
+ end
11
+
12
+ factory :runner_fallback, class: Enc::Runner do
13
+ skip_create
14
+
15
+ config FactoryGirl.build(:config, host: 'http://shouldneverexist99.jivehostedblah.com')
16
+
17
+ initialize_with { new(config) }
18
+ end
19
+ end
data/spec/factories.rb ADDED
@@ -0,0 +1,21 @@
1
+ require 'factory_girl'
2
+ require 'support/factory_girl'
3
+
4
+ module FactoryDefaults
5
+ MOCK_ASSET_TAG = 'jive-00001460'.freeze
6
+ MOCK_ASSET_HOSTNAME = 'cfgmgmt1.test1.jivehosted.com'.freeze
7
+ MOCK_ASSET_DATACENTER = 'jcadev'.freeze
8
+ MOCK_ASSET_ENVIRONMENT = 'dev'.freeze
9
+ MOCK_ASSET_PUPPET_ENVIRONMENT = 'production'.freeze
10
+ MOCK_COLLINS_HOST = 'collins.dev.jivehosted.com'.freeze
11
+ MOCK_COLLINS_URL = "http://#{MOCK_COLLINS_HOST}".freeze
12
+ MOCK_COLLINS_USERNAME = 'admin'.freeze
13
+ MOCK_COLLINS_PASSWORD = 'admin'.freeze
14
+ MOCK_COLLINS_TIMEOUT = 30.freeze
15
+ ROOT_SUPPORT_DIR = File.expand_path(File.dirname(__FILE__)).freeze
16
+ MOCK_ENC_CACHE_DIR = ROOT_SUPPORT_DIR + '/support/cache'.freeze
17
+ MOCK_ENC_CONF_DIR = ROOT_SUPPORT_DIR + '/support/conf'.freeze
18
+ MOCK_ENC_LOG_DIR = ROOT_SUPPORT_DIR + '/support/log'.freeze
19
+ end
20
+
21
+ Dir[File.dirname(__FILE__) + '/factories/*.rb'].each {|file| require file }
@@ -0,0 +1,9 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'External request' do
4
+ it 'queries collins_helper' do
5
+ uri = URI(FactoryDefaults::MOCK_COLLINS_URL)
6
+ response = Net::HTTP.get(uri)
7
+ expect(response).to be_an_instance_of(String)
8
+ end
9
+ end
@@ -0,0 +1,54 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'Collins Node Asset' do
4
+ it 'should have hostname set' do
5
+ node = FactoryGirl.build(:node_asset)
6
+ expect(node.get_hostname).to eq(FactoryDefaults::MOCK_ASSET_HOSTNAME)
7
+ end
8
+
9
+ it 'should have a datacenter set' do
10
+ node = FactoryGirl.build(:node_asset)
11
+ expect(node.get_datacenter).to eq('jcadev')
12
+ end
13
+
14
+ it 'should have a deployment_environmnet' do
15
+ node = FactoryGirl.build(:node_asset)
16
+ expect(node.get_deployment_environment).to eq('dev')
17
+ end
18
+
19
+ it 'should have a default puppet environment' do
20
+ node = FactoryGirl.build(:node_asset, puppet_environment: nil)
21
+ expect(node.get_puppet_environment).to eq('production')
22
+ end
23
+
24
+ it 'should have a variable puppet environment' do
25
+ node = FactoryGirl.build(:node_asset, puppet_environment: 'fake_env')
26
+ expect(node.get_puppet_environment).to eq('fake_env')
27
+ end
28
+
29
+ it 'is a valid asset' do
30
+ node = FactoryGirl.build(:node_asset)
31
+ expect(node.is_valid?).to eq(true)
32
+ end
33
+
34
+ it 'is not a valid asset due to missing datacenter' do
35
+ node = FactoryGirl.build(:node_asset, datacenter: nil)
36
+ expect(node.is_valid?).to eq(false)
37
+ expect{node.get_datacenter}.to raise_exception(Enc::CollinsHelper::Node::NoDatacenter,
38
+ 'The node does not have a data_center tag set')
39
+ end
40
+
41
+ it 'is not a valid asset due to missing deployment environment' do
42
+ node = FactoryGirl.build(:node_asset, environment: nil)
43
+ expect(node.is_valid?).to eq(false)
44
+ expect{node.get_deployment_environment}.to raise_exception(Enc::CollinsHelper::Node::NoDeploymentEnvironment,
45
+ 'The node does not have an environment tag set')
46
+ end
47
+
48
+ it 'is not a valid asset due to missing hostname' do
49
+ node = FactoryGirl.build(:node_asset, hostname: nil)
50
+ expect(node.is_valid?).to eq(false)
51
+ expect{node.get_hostname}.to raise_exception(Enc::CollinsHelper::Node::NoHostname,
52
+ 'The node does not have a hostname tag set')
53
+ end
54
+ end
@@ -0,0 +1,25 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'Node Asset Cache' do
4
+ before :all do
5
+ @cache = FactoryGirl.build(:node_cache)
6
+ @node = FactoryGirl.build(:node_asset)
7
+ end
8
+
9
+ it 'write cache' do
10
+ @cache.write(@node.get_hostname, @node)
11
+ expect(@cache.exists?(@node.get_hostname)).to eq(true)
12
+ @cache.delete!(@node.get_hostname)
13
+ end
14
+
15
+ it 'reads cache' do
16
+ @cache.write(@node.get_hostname, @node)
17
+ expect(@cache.read(@node.get_hostname).attributes).to eq(@node.attributes)
18
+ @cache.delete!(@node.get_hostname)
19
+ end
20
+
21
+ it 'delete cache' do
22
+ @cache.delete!(@node.get_hostname)
23
+ expect(@cache.exists?(@node.get_hostname)).to eq(false)
24
+ end
25
+ end
@@ -0,0 +1,41 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'ENC Runner' do
4
+ before :all do
5
+ @config = FactoryGirl.build(:config)
6
+ @cache = FactoryGirl.build(:node_cache)
7
+ end
8
+
9
+ it 'gets an instance of the builder' do
10
+ node = FactoryGirl.build(:node_asset)
11
+ runner = FactoryGirl.build(:runner)
12
+ expect(runner.build(node.get_hostname)).to be_an_instance_of(Enc::Builder)
13
+ end
14
+
15
+ it 'gets an instance of the builder even if the asset does not have the puppet_enc tag set' do
16
+ runner = FactoryGirl.build(:runner)
17
+ node = FactoryGirl.build(:node_asset, puppet_enc: false)
18
+ expect(runner.build(node.get_hostname)).to be_an_instance_of(Enc::Builder)
19
+ end
20
+
21
+ it 'falls back to use cache when collins is unavailable' do
22
+ node = FactoryGirl.build(:node_asset)
23
+ runner = FactoryGirl.build(:runner_fallback)
24
+ @cache.write(node.get_hostname, node)
25
+ expect(@cache.exists?(node.get_hostname)).to eq(true)
26
+ expect(runner.build(node.get_hostname)).to be_an_instance_of(Enc::Builder)
27
+ end
28
+
29
+ it 'falls back to use cache when the asset does not exist' do
30
+ runner = FactoryGirl.build(:runner)
31
+ expect(runner.build('missing')).to be_an_instance_of(Enc::Builder)
32
+ expect(@cache.exists?('missing')).to eq(true)
33
+ end
34
+
35
+ it 'exits when neither collins nor the local cache is available' do
36
+ node = FactoryGirl.build(:node_asset)
37
+ runner = FactoryGirl.build(:runner_fallback)
38
+ expect{runner.build(node.get_hostname)}.to raise_exception(SystemExit)
39
+ expect(@cache.exists?(node.get_hostname)).to eq(false)
40
+ end
41
+ end
@@ -0,0 +1,52 @@
1
+ require 'webmock/rspec'
2
+ require 'enc'
3
+ require 'json'
4
+ require 'factories'
5
+
6
+ WebMock.disable_net_connect!(allow_localhost: true)
7
+
8
+ RSpec.configure do |config|
9
+ good_response = FactoryGirl.build(:collins_response)
10
+ missing_asset_response = FactoryGirl.build(:collins_response, hostname: 'missing')
11
+ good_response_headers = FactoryGirl.build(:collins_response_headers)
12
+
13
+ config.before(:each) do
14
+ stub_request(:get, "#{FactoryDefaults::MOCK_COLLINS_USERNAME}:#{FactoryDefaults::MOCK_COLLINS_PASSWORD}@
15
+ #{FactoryDefaults::MOCK_COLLINS_HOST}
16
+ /api/assets?attribute=hostname%3B#{FactoryDefaults::MOCK_ASSET_HOSTNAME}&details=true").
17
+ to_return(status: 200,
18
+ body: good_response.to_json,
19
+ headers: good_response_headers
20
+ )
21
+ stub_request(:get, "#{FactoryDefaults::MOCK_COLLINS_USERNAME}:#{FactoryDefaults::MOCK_COLLINS_PASSWORD}@
22
+ #{FactoryDefaults::MOCK_COLLINS_HOST}
23
+ /api/assets?attribute=hostname%3Bmissing&details=true").
24
+ to_return(status: 200,
25
+ body: missing_asset_response.to_json,
26
+ headers: good_response_headers
27
+ )
28
+ stub_request(:get, FactoryDefaults::MOCK_COLLINS_URL).
29
+ to_return(status: 200,
30
+ body: 'response body',
31
+ headers: {}
32
+ )
33
+ stub_request(:any, /shouldneverexist99.jivehostedblah.com/).to_timeout
34
+ stub_request(:get, /willreturn401.jivehostedblah.com/).
35
+ to_return(status: 401)
36
+ stub_request(:get, /willreturn403.jivehostedblah.com/).
37
+ to_return(status: 403)
38
+ stub_request(:get, /willreturn500.jivehostedblah.com/).
39
+ to_return(status: 500)
40
+
41
+ FileUtils::mkdir_p(FactoryDefaults::MOCK_ENC_CACHE_DIR)
42
+ FileUtils::mkdir_p(FactoryDefaults::MOCK_ENC_CONF_DIR)
43
+ FileUtils::mkdir_p(FactoryDefaults::MOCK_ENC_LOG_DIR)
44
+ end
45
+
46
+ config.after(:each) do
47
+ FileUtils.rm_rf(FactoryDefaults::MOCK_ENC_CACHE_DIR)
48
+ FileUtils.rm_rf(FactoryDefaults::MOCK_ENC_CONF_DIR)
49
+ FileUtils.rm_rf(FactoryDefaults::MOCK_ENC_LOG_DIR)
50
+ end
51
+ end
52
+
@@ -0,0 +1,8 @@
1
+ require 'support/utils'
2
+
3
+ RSpec.configure do |config|
4
+ config.include FactoryGirl::Syntax::Methods
5
+ config.before(:suite) do
6
+ FactoryGirl.lint
7
+ end
8
+ end