hieracles 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (54) hide show
  1. checksums.yaml +7 -0
  2. data/.coveralls.yml +2 -0
  3. data/.gitignore +15 -0
  4. data/.rspec +2 -0
  5. data/.ruby-version +1 -0
  6. data/.travis.yml +5 -0
  7. data/CHANGELOG.md +8 -0
  8. data/Gemfile +4 -0
  9. data/LICENSE +22 -0
  10. data/README.md +30 -0
  11. data/Rakefile +14 -0
  12. data/bin/hc +32 -0
  13. data/bin/nodeinfo +41 -0
  14. data/hieracles.gemspec +30 -0
  15. data/lib/hieracles.rb +15 -0
  16. data/lib/hieracles/config.rb +56 -0
  17. data/lib/hieracles/format.rb +58 -0
  18. data/lib/hieracles/formats/console.rb +84 -0
  19. data/lib/hieracles/formats/csv.rb +66 -0
  20. data/lib/hieracles/formats/plain.rb +61 -0
  21. data/lib/hieracles/formats/yaml.rb +36 -0
  22. data/lib/hieracles/help.rb +27 -0
  23. data/lib/hieracles/hiera.rb +27 -0
  24. data/lib/hieracles/node.rb +108 -0
  25. data/lib/hieracles/optparse.rb +63 -0
  26. data/lib/hieracles/utils.rb +59 -0
  27. data/spec/files/config.yml +5 -0
  28. data/spec/files/enc/server.example.com.yaml +7 -0
  29. data/spec/files/enc/server2.example.com.yaml +7 -0
  30. data/spec/files/enc/server3.example.com.yaml +7 -0
  31. data/spec/files/farm_modules/dev.pp +5 -0
  32. data/spec/files/farm_modules/dev2.pp +5 -0
  33. data/spec/files/hiera.yaml +16 -0
  34. data/spec/files/hiera_no_yamlbackend.yaml +16 -0
  35. data/spec/files/hiera_yamlbackend_notfound.yaml +16 -0
  36. data/spec/files/modules/fake_module/manifests/init.pp +3 -0
  37. data/spec/files/modules/fake_module2/manifests/init.pp +3 -0
  38. data/spec/files/modules/fake_module3/manifests/init.pp +3 -0
  39. data/spec/files/params/common/common.yml +2 -0
  40. data/spec/files/params/farm/dev.yaml +1 -0
  41. data/spec/files/params/nodes/server.example.com.yaml +5 -0
  42. data/spec/lib/config_spec.rb +63 -0
  43. data/spec/lib/format_spec.rb +91 -0
  44. data/spec/lib/formats/console_spec.rb +107 -0
  45. data/spec/lib/formats/csv_spec.rb +89 -0
  46. data/spec/lib/formats/plain_spec.rb +94 -0
  47. data/spec/lib/formats/yaml_spec.rb +80 -0
  48. data/spec/lib/help_spec.rb +8 -0
  49. data/spec/lib/hiera_spec.rb +111 -0
  50. data/spec/lib/node_spec.rb +158 -0
  51. data/spec/lib/optparse_spec.rb +65 -0
  52. data/spec/lib/utils_spec.rb +61 -0
  53. data/spec/spec_helper.rb +26 -0
  54. metadata +223 -0
@@ -0,0 +1,94 @@
1
+ require 'spec_helper'
2
+
3
+ describe Hieracles::Formats::Plain do
4
+ let(:node) { double("node") }
5
+ let(:plain_format) { Hieracles::Formats::Plain.new node }
6
+
7
+ describe ".info" do
8
+ let(:expected) {
9
+ "Node fqdn\n" +
10
+ "Farm farm\n"
11
+ }
12
+ before {
13
+ allow(node).to receive(:info).and_return(
14
+ {
15
+ 'Node' => 'fqdn',
16
+ 'Farm' => 'farm'
17
+ }
18
+ )
19
+ }
20
+ it "outputs proper text" do
21
+ expect(plain_format.info nil).to eq expected
22
+ end
23
+ end
24
+
25
+ describe ".files" do
26
+ let(:expected) { "path1\npath2\n" }
27
+ before {
28
+ allow(node).to receive(:files).and_return(['path1', 'path2'])
29
+ }
30
+ it "outputs proper text" do
31
+ expect(plain_format.files nil).to eq expected
32
+ end
33
+ end
34
+
35
+ describe ".paths" do
36
+ let(:expected) { "path1\npath2\n" }
37
+ before {
38
+ allow(node).to receive(:paths).and_return(['path1', 'path2'])
39
+ }
40
+ it "outputs proper text" do
41
+ expect(plain_format.paths nil).to eq expected
42
+ end
43
+ end
44
+
45
+ describe ".build_head" do
46
+ let(:expected) { "[0] path1\n[1] path2\n\n" }
47
+ before {
48
+ allow(node).to receive(:files).and_return(['path1', 'path2'])
49
+ }
50
+ it "outputs proper text" do
51
+ expect(plain_format.send :build_head, true).to eq expected
52
+ end
53
+ end
54
+
55
+ describe ".build_params_line" do
56
+ let(:expected) {
57
+ "[0] params.this.var value1\n"+
58
+ " [1] params.this.var value2\n"
59
+ }
60
+ let(:params) {
61
+ [
62
+ { file: 'path1', value: 'value1'},
63
+ { file: 'path2', value: 'value2'},
64
+ ]
65
+ }
66
+ before {
67
+ plain_format.instance_variable_set(:@index,
68
+ {'path1' => 0, 'path2' => 1}
69
+ )
70
+ }
71
+ it "outputs proper text" do
72
+ expect(plain_format.send :build_params_line,
73
+ "params.this.var",
74
+ params,
75
+ nil).to eq expected
76
+ end
77
+ end
78
+
79
+ describe ".build_modules_line" do
80
+ before {
81
+ allow(node).to receive(:modules).and_return(
82
+ {
83
+ 'module1' => nil,
84
+ 'longmodule2' => nil
85
+ }
86
+ )
87
+ }
88
+ let(:expected) { "module1 value\n" }
89
+ it "outputs proper text" do
90
+ expect(plain_format.send :build_modules_line, "module1", "value").to eq expected
91
+ end
92
+ end
93
+
94
+ end
@@ -0,0 +1,80 @@
1
+ require 'spec_helper'
2
+
3
+ describe Hieracles::Formats::Yaml do
4
+ let(:node) { double("node") }
5
+ let(:yaml_format) { Hieracles::Formats::Yaml.new node }
6
+
7
+ describe ".info" do
8
+ let(:expected) {"---\nNode: fqdn\nFarm: farm\n"}
9
+ before {
10
+ allow(node).to receive(:info).and_return(
11
+ {
12
+ 'Node' => 'fqdn',
13
+ 'Farm' => 'farm'
14
+ }
15
+ )
16
+ }
17
+ it "outputs proper text" do
18
+ expect(yaml_format.info nil).to eq expected
19
+ end
20
+ end
21
+
22
+ describe ".files" do
23
+ let(:expected) { "---\n- path1\n- path2\n" }
24
+ before {
25
+ allow(node).to receive(:files).and_return(['path1', 'path2'])
26
+ }
27
+ it "outputs proper text" do
28
+ expect(yaml_format.files nil).to eq expected
29
+ end
30
+ end
31
+
32
+ describe ".paths" do
33
+ let(:expected) { "---\n- path1\n- path2\n" }
34
+ before {
35
+ allow(node).to receive(:paths).and_return(['path1', 'path2'])
36
+ }
37
+ it "outputs proper text" do
38
+ expect(yaml_format.paths nil).to eq expected
39
+ end
40
+ end
41
+
42
+ describe ".modules" do
43
+ before {
44
+ allow(node).to receive(:modules).and_return(
45
+ {
46
+ 'module1' => "value",
47
+ 'longmodule2' => "not found"
48
+ }
49
+ )
50
+ }
51
+ let(:expected) { "---\nmodule1: value\nlongmodule2: not found\n" }
52
+ it "outputs proper text" do
53
+ expect(yaml_format.modules nil).to eq expected
54
+ end
55
+ end
56
+
57
+ describe ".params" do
58
+ let(:expected) {
59
+ "---\n"+
60
+ "params:\n" +
61
+ " this:\n" +
62
+ " var: value1\n"
63
+ }
64
+ before {
65
+ allow(node).to receive(:params_tree).and_return(
66
+ {
67
+ 'params' => {
68
+ 'this' => {
69
+ 'var' => 'value1'
70
+ }
71
+ }
72
+ }
73
+ )
74
+ }
75
+ it "outputs proper text" do
76
+ expect(yaml_format.params nil).to eq expected
77
+ end
78
+ end
79
+
80
+ end
@@ -0,0 +1,8 @@
1
+ require 'spec_helper'
2
+
3
+ describe Hieracles::Help do
4
+
5
+ describe '.usage' do
6
+ it { expect(subject.usage).to be_a String }
7
+ end
8
+ end
@@ -0,0 +1,111 @@
1
+ require 'spec_helper'
2
+
3
+ describe Hieracles::Hiera do
4
+ before { Hieracles::Config.load(options) }
5
+
6
+ describe '.new' do
7
+
8
+ context 'hiera file not found' do
9
+ let(:options) { {
10
+ basepath: 'spec/files',
11
+ hierafile: 'hiera_no.yaml'
12
+ } }
13
+ it 'raises an error' do
14
+ expect { Hieracles::Hiera.new }.to raise_error(IOError)
15
+ end
16
+ end
17
+
18
+ context 'hiera file found' do
19
+ let(:options) { {
20
+ basepath: 'spec/files',
21
+ hierafile: 'hiera.yaml'
22
+ } }
23
+ let(:expected){
24
+ File.expand_path(File.join(options[:basepath], options[:hierafile]))
25
+ }
26
+ let(:hiera) { Hieracles::Hiera.new }
27
+ it 'load the file' do
28
+ expect(hiera.instance_variable_get :@loaded).to be_a Hash
29
+ expect(hiera.instance_variable_get :@hierafile).to eq expected
30
+ end
31
+ end
32
+
33
+ end
34
+
35
+ describe '.datadir' do
36
+ context 'hiera file do not have a yaml backend' do
37
+ let(:options) { {
38
+ basepath: 'spec/files',
39
+ hierafile: 'hiera_no_yamlbackend.yaml'
40
+ } }
41
+ let(:hiera) { Hieracles::Hiera.new }
42
+ it 'raises an error' do
43
+ expect { hiera.datadir }.to raise_error(TypeError)
44
+ end
45
+ end
46
+ context 'hiera file has a yaml backend but dir not found' do
47
+ let(:options) { {
48
+ basepath: 'spec/files',
49
+ hierafile: 'hiera_yamlbackend_notfound.yaml'
50
+ } }
51
+ let(:hiera) { Hieracles::Hiera.new }
52
+ it 'raises an error' do
53
+ expect { hiera.datadir }.to raise_error(IOError)
54
+ end
55
+ end
56
+ context 'hiera file has a yaml backend' do
57
+ let(:options) {
58
+ {
59
+ config: 'spec/files/config.yml',
60
+ hierafile: 'hiera.yaml',
61
+ basepath: 'spec/files'
62
+ }
63
+ }
64
+ let(:hiera) { Hieracles::Hiera.new }
65
+ let(:expected) { File.expand_path(File.join(Hieracles::Config.basepath, 'params')) }
66
+ it 'returns params path' do
67
+ expect(hiera.datadir).to eq expected
68
+ end
69
+ end
70
+ end
71
+
72
+ context "with proper params" do
73
+ let(:options) { {
74
+ basepath: 'spec/files',
75
+ hierafile: 'hiera.yaml'
76
+ } }
77
+ let(:hiera) { Hieracles::Hiera.new }
78
+
79
+ describe '.hierarchy' do
80
+ let(:expected) { [
81
+ 'nodes/%{fqdn}',
82
+ 'farm_datacenter/%{farm}_%{datacenter}',
83
+ 'farm/%{farm}',
84
+ 'room/%{room}',
85
+ 'datacenter/%{datacenter}',
86
+ 'country/%{country}',
87
+ 'os/%{operatingsystem}-%{lsbdistcodename}',
88
+ 'common/common'
89
+ ]}
90
+ it "extracts the hierarchy accoding to the hierfile" do
91
+ expect(hiera.hierarchy).to eq expected
92
+ end
93
+ end
94
+
95
+ describe '.params' do
96
+ let(:expected) { [
97
+ 'fqdn',
98
+ 'farm',
99
+ 'datacenter',
100
+ 'room',
101
+ 'country',
102
+ 'operatingsystem',
103
+ 'lsbdistcodename'
104
+ ]}
105
+ it 'extracts hiera parameters from the hierarchy' do
106
+ expect(hiera.params).to eq expected
107
+ end
108
+ end
109
+ end
110
+
111
+ end
@@ -0,0 +1,158 @@
1
+ require 'spec_helper'
2
+
3
+ describe Hieracles::Node do
4
+ let(:options) {
5
+ {
6
+ config: 'spec/files/config.yml',
7
+ hierafile: 'hiera.yaml',
8
+ encpath: 'enc',
9
+ basepath: 'spec/files'
10
+ }
11
+ }
12
+
13
+ context "when extra parameters are specified" do
14
+ describe '.new' do
15
+ let(:extraoptions) {
16
+ options.merge({ params: 'key1=value1;key2=value2' })
17
+ }
18
+ let(:node) { Hieracles::Node.new 'server.example.com', extraoptions }
19
+ let(:expected) {
20
+ {
21
+ fqdn: 'server.example.com',
22
+ country: 'fr',
23
+ datacenter: 'equinix',
24
+ farm: 'dev',
25
+ key1: 'value1',
26
+ key2: 'value2'
27
+ }
28
+ }
29
+ it { expect(node).to be_a Hieracles::Node }
30
+ it { expect(node.hiera_params).to eq expected }
31
+ end
32
+ end
33
+
34
+ context "when parameters are not valid" do
35
+ let(:node) { Hieracles::Node.new 'server_not_there.example.com', options }
36
+ it { expect{ node }.to raise_error(RuntimeError) }
37
+ end
38
+
39
+ context "when parameters are valid" do
40
+ let(:node) { Hieracles::Node.new 'server.example.com', options }
41
+
42
+ describe '.new' do
43
+ let(:expected) {
44
+ {
45
+ fqdn: 'server.example.com',
46
+ country: 'fr',
47
+ datacenter: 'equinix',
48
+ farm: 'dev'
49
+ }
50
+ }
51
+ it { expect(node).to be_a Hieracles::Node }
52
+ it { expect(node.hiera_params).to eq expected }
53
+ end
54
+
55
+ describe '.files' do
56
+ let(:expected) {
57
+ [
58
+ 'nodes/server.example.com.yaml',
59
+ 'farm/dev.yaml'
60
+ ]
61
+ }
62
+ it { expect(node.files).to eq expected }
63
+ end
64
+
65
+ describe '.paths' do
66
+ let(:expected) {
67
+ [
68
+ File.join(node.hiera.datadir, 'nodes/server.example.com.yaml'),
69
+ File.join(node.hiera.datadir, 'farm/dev.yaml')
70
+ ]
71
+ }
72
+ it { expect(node.paths).to eq expected }
73
+ end
74
+
75
+ describe '.params' do
76
+ let(:expected) {
77
+ [
78
+ [ "another.sublevel.thing",
79
+ [{
80
+ value: "always",
81
+ file: 'nodes/server.example.com.yaml'
82
+ }]
83
+ ],
84
+ [ "common_param.subparam",
85
+ [{
86
+ value: "overriden",
87
+ file: 'nodes/server.example.com.yaml'
88
+ }]
89
+ ],
90
+ [ "somefarmparam",
91
+ [{
92
+ value: false,
93
+ file: 'farm/dev.yaml'
94
+ }]
95
+ ]
96
+ ]
97
+ }
98
+ it { expect(node.params).to eq expected }
99
+ end
100
+
101
+ describe '.params_tree' do
102
+ let(:expected) {
103
+ {
104
+ "another" => {
105
+ "sublevel" => {
106
+ "thing" => "always"
107
+ }
108
+ },
109
+ "common_param" => {
110
+ "subparam" => "overriden"
111
+ },
112
+ "somefarmparam" => false
113
+ }
114
+ }
115
+ it { expect(node.params_tree).to eq expected }
116
+ end
117
+
118
+
119
+ describe '.modules' do
120
+ context "no unfound modules" do
121
+ let(:expected) {
122
+ {
123
+ "fake_module" => "modules/fake_module",
124
+ "fake_module2" => "modules/fake_module2",
125
+ "fake_module3" => "modules/fake_module3"
126
+ }
127
+ }
128
+ it { expect(node.modules).to eq expected }
129
+ end
130
+ context "one unfound modules" do
131
+ let(:node) { Hieracles::Node.new 'server2.example.com', options }
132
+ let(:expected) {
133
+ {
134
+ "fake_module" => "modules/fake_module",
135
+ "fake_module2" => "modules/fake_module2",
136
+ "fake_module4" => nil
137
+ }
138
+ }
139
+ it { expect(node.modules).to eq expected }
140
+ end
141
+ context "no farm file found" do
142
+ let(:node) { Hieracles::Node.new 'server3.example.com', options }
143
+ it { expect { node.modules }.to raise_error(RuntimeError) }
144
+ end
145
+ end
146
+
147
+ describe '.info' do
148
+ let(:expected) { {
149
+ fqdn: 'server.example.com',
150
+ datacenter: 'equinix',
151
+ country: 'fr',
152
+ farm: 'dev'
153
+ } }
154
+ it { expect(node.info).to eq expected }
155
+ end
156
+ end
157
+
158
+ end