noms-client 1.9.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,90 @@
1
+ #!/usr/bin/env rspec
2
+
3
+ require 'ncc/client'
4
+ require 'noms/client/version'
5
+ require 'spec_helper'
6
+
7
+
8
+ describe NCC::Client do
9
+ before(:all) do
10
+ init_test
11
+ NCC::Client.mock! nil
12
+ end
13
+
14
+ context "initializing" do
15
+
16
+ it "creates an NCC::Client object" do
17
+ ncc = NCC::Client.new $opt
18
+ expect(ncc).to be_an_instance_of NCC::Client
19
+ end
20
+
21
+ end
22
+
23
+ describe '#clouds' do
24
+
25
+ before(:each) do
26
+ @ncc = NCC::Client.new $opt
27
+ # Necessary to exercise mock--should be abstracted?
28
+ @ncc.do_request :PUT => '/clouds/os0', :body => {
29
+ 'name' => 'os0',
30
+ 'status' => 'ok',
31
+ 'provider' => 'openstack',
32
+ 'service' => 'Fog::Compute::OpenStack::Mock'
33
+ }
34
+ end
35
+
36
+ it "returns a list of cloud objects" do
37
+ result = @ncc.clouds
38
+ expect(result.size).to eq(1)
39
+ expect(result.first['name']).to eq('os0')
40
+ end
41
+
42
+ it "returns a specific cloud object" do
43
+ result = @ncc.clouds('os0')
44
+ expect(result).to have_key 'provider'
45
+ expect(result['provider']).to eq 'openstack'
46
+ end
47
+
48
+ end
49
+
50
+ describe '#info' do
51
+ before(:each) do
52
+ @ncc = NCC::Client.new $opt
53
+ @ncc.do_request :PUT => '', :body => {
54
+ 'version' => NOMS::Client::VERSION
55
+ }
56
+ end
57
+
58
+ it "returns version info" do
59
+ info = @ncc.info
60
+ expect(info).to have_key 'version'
61
+ expect(info['version']).to eq NOMS::Client::VERSION
62
+ end
63
+ end
64
+
65
+ describe '#console' do
66
+ before(:each) do
67
+ @ncc = NCC::Client.new $opt
68
+ @ncc.do_request :PUT => 'clouds/os0/instances/1', :body => {
69
+ 'name' => 'testinst1.local',
70
+ 'id' => '1',
71
+ 'role' => [ ],
72
+ 'status' => 'active',
73
+ 'size' => 'm1.small',
74
+ 'image' => 'deb6',
75
+ 'host' => 'ostack01.local',
76
+ 'ip_address' => '127.0.0.1'
77
+ }
78
+ @ncc.do_request :PUT => 'clouds/os0/instances/1/console', :body => {
79
+ 'url' => 'vnc://ostack01:5959'
80
+ }
81
+ end
82
+
83
+ it "returns a console URL" do
84
+ console = @ncc.console('os0', '1')
85
+ expect(console).to have_key 'url'
86
+ expect(console['url']).to eq 'vnc://ostack01:5959'
87
+ end
88
+ end
89
+
90
+ end
@@ -0,0 +1,214 @@
1
+ #!/usr/bin/env rspec
2
+
3
+ require 'noms/cmdb'
4
+ require 'spec_helper'
5
+
6
+ describe NOMS::CMDB::RestMock do
7
+
8
+ before(:all) do
9
+ init_test
10
+ NOMS::CMDB.mock! nil
11
+ end
12
+
13
+ context 'initializing' do
14
+
15
+ describe '#new' do
16
+
17
+ it 'creates a new instance' do
18
+
19
+ cmdb = NOMS::CMDB.new $opt
20
+ expect(cmdb).to be_an_instance_of NOMS::CMDB
21
+
22
+ end
23
+
24
+ end
25
+
26
+ end
27
+
28
+ describe '#do_query' do
29
+
30
+ before(:each) do
31
+ @cmdb = NOMS::CMDB.new $opt
32
+ end
33
+
34
+ context :POST do
35
+
36
+ it 'creates a new entry' do
37
+ result = @cmdb.do_request :POST => '/environments', :body => {
38
+ 'id' => 'production', 'name' => 'production',
39
+ 'environment_name' => 'production' }
40
+ expect(result).to have_key 'name'
41
+ expect(result['name']).to be == 'production'
42
+ expect(@cmdb.all_data).to have_key $server
43
+ expect(@cmdb.all_data[$server]).to have_key "#{$cmdbapi}/environments"
44
+ expect(@cmdb.all_data[$server]["#{$cmdbapi}/environments"]).to be_an Array
45
+ expect(@cmdb.all_data[$server]["#{$cmdbapi}/environments"]).to include { |x| x['name'] == 'production' }
46
+ end
47
+
48
+ it 'creates several new entries' do
49
+ @cmdb.do_request :POST => '/environments', :body => {
50
+ 'id' => 'production', 'name' => 'production',
51
+ 'environment_name' => 'production' }
52
+ 10.times do |i|
53
+ env = "environment-#{i}"
54
+ @cmdb.do_request :POST => '/environments', :body => {
55
+ 'id' => env, 'name' => env, 'environment_name' => 'production'
56
+ }
57
+ end
58
+ envs = @cmdb.all_data[$server]["#{$cmdbapi}/environments"]
59
+ expect(envs).to have(11).items
60
+ end
61
+
62
+ it 'synthesizes an id' do
63
+ result = @cmdb.do_request :POST => '/environments', :body => { 'name' => 'production' }
64
+ expect(result).to have_key 'id'
65
+ expect(@cmdb.all_data[$server][$cmdbapi + '/environments']).to include { |o| o['id'] = result['id'] }
66
+ end
67
+
68
+ end
69
+
70
+ context :PUT do
71
+
72
+ it 'creates a new entry' do
73
+ @cmdb.do_request :PUT => '/environments/1', :body => {
74
+ 'name' => 'production', 'environment_name' => 'production' }
75
+ expect(@cmdb.all_data[$server][$cmdbapi + '/environments']).to include { |o| o['name'] == 'production' }
76
+ end
77
+
78
+ it 'creates several new entries' do
79
+ @cmdb.do_request :PUT => '/environments/1', :body => {
80
+ 'name' => 'production', 'environment_name' => 'production' }
81
+ 10.times do |i|
82
+ @cmdb.do_request :PUT => "/environments/#{100 + i}", :body => {
83
+ 'name' => "environment-#{i}", 'environment_name' => 'production' }
84
+ end
85
+ expect(@cmdb.all_data[$server][$cmdbapi + '/environments']).to have(11).items
86
+ expect(@cmdb.all_data[$server][$cmdbapi + '/environments']).to include { |o| o['name'] == 'production' }
87
+ end
88
+
89
+ it 'infers the id' do
90
+ result = @cmdb.do_request :PUT => '/environments/1', :body => { 'name' => 'production' }
91
+ expect(result).to have_key 'id'
92
+ expect(@cmdb.all_data[$server][$cmdbapi + '/environments'][0]).to have_key 'id'
93
+ expect(@cmdb.all_data[$server][$cmdbapi + '/environments'][0]['id']).to eq '1'
94
+ end
95
+
96
+ it 'replaces an existing object' do
97
+ @cmdb.do_request :PUT => '/environments/1', :body => {
98
+ 'name' => 'production', 'environment_name' => 'production' }
99
+ @cmdb.do_request :PUT => '/environments/1', :body => {
100
+ 'name' => 'testing', 'note' => 'testing environment' }
101
+ data = @cmdb.all_data[$server][$cmdbapi + '/environments']
102
+ expect(data[0]).to have_key 'name'
103
+ expect(data[0]).to have_key 'id'
104
+ expect(data[0]).to have_key 'note'
105
+ expect(data[0]).not_to have_key 'environment_name'
106
+ expect(data[0]['name']).to eq 'testing'
107
+ end
108
+
109
+ it 'updates an existing object' do
110
+ def @cmdb.allow_partial_updates
111
+ true
112
+ end
113
+ @cmdb.do_request :PUT => '/environments/1', :body => {
114
+ 'name' => 'production', 'environment_name' => 'production' }
115
+ @cmdb.do_request :PUT => '/environments/1', :body => {
116
+ 'name' => 'testing', 'note' => 'testing environment' }
117
+ object = @cmdb.all_data[$server][$cmdbapi + '/environments'][0]
118
+ expect(object['id']).to eq '1'
119
+ expect(object['name']).to eq 'testing'
120
+ expect(object['note']).to eq 'testing environment'
121
+ expect(object['environment_name']).to eq 'production'
122
+ end
123
+
124
+ end
125
+
126
+ context :GET do
127
+
128
+ it 'finds an existing entry' do
129
+ @cmdb.do_request :POST => '/environments', :body => {
130
+ 'id' => 'production', 'name' => 'production',
131
+ 'environment_name' => 'production' }
132
+ result = @cmdb.do_request :GET => '/environments/production'
133
+ expect(result).to be_a Hash
134
+ expect(result).to include 'name' => 'production'
135
+ end
136
+
137
+ it 'retrieves all entries' do
138
+ @cmdb.do_request :POST => '/environments', :body => {
139
+ 'name' => 'production', 'environment_name' => 'production'
140
+ }
141
+ 10.times do |i|
142
+ @cmdb.do_request :POST => '/environments', :body => {
143
+ 'name' => "environment-#{i}", 'environment_name' => 'production'
144
+ }
145
+ end
146
+ result = @cmdb.do_request :GET => '/environments'
147
+ expect(result).to be_an Array
148
+ expect(result).to have(11).items
149
+ expect(result).to include { |o| o['name'] == 'production' }
150
+ end
151
+
152
+ it 'raises an exception for missing entries' do
153
+ expect { @cmdb.do_request :GET => '/environments/1' }.to raise_error
154
+ @cmdb.do_request :PUT => '/environments/1', :body => { 'name' => 'production' }
155
+ expect { @cmdb.do_request :GET => '/chorizo' }.to raise_error
156
+ expect { @cmdb.do_request :GET => '/environments/2' }.to raise_error
157
+ end
158
+
159
+ end
160
+
161
+ context :DELETE do
162
+
163
+ it 'deletes an existing entry' do
164
+ @cmdb.do_request :PUT => '/environments/1', :body => { 'name' => 'production' }
165
+ result = @cmdb.do_request :DELETE => '/environments/1'
166
+ expect(result).to be true
167
+ expect(@cmdb.all_data[$server][$cmdbapi + '/environments']).to have(0).items
168
+ end
169
+
170
+ it 'deletes an existing entry among many' do
171
+ @cmdb.do_request :PUT => '/environments/1', :body => { 'name' => 'production' }
172
+ 10.times do |i|
173
+ @cmdb.do_request :PUT => "/environments/#{100 + i}", :body => {
174
+ 'name' => "environment-#{i}", 'environment_name' => 'production' }
175
+ end
176
+ @cmdb.do_request :DELETE => '/environments/103'
177
+ data = @cmdb.all_data[$server][$cmdbapi + '/environments']
178
+ expect(data).to have(10).items
179
+ expect(data).not_to include { |o| o['name'] == 'environment-3' }
180
+ end
181
+
182
+ it 'deletes a whole collection' do
183
+ @cmdb.do_request :PUT => '/environments/1', :body => {'name' => 'production' }
184
+ @cmdb.do_request :DELETE => '/environments'
185
+ expect(@cmdb.all_data[$server]).not_to have_key '/environments'
186
+ end
187
+
188
+ it 'raises an exception for nonexistent entries' do
189
+ expect { @cmdb.do_request :DELETE => '/environments/2' }.to raise_error
190
+ @cmdb.do_request :PUT => '/environments/1', :body => { 'name' => 'production' }
191
+ expect { @cmdb.do_request :DELETE => '/environments/2' }.to raise_error
192
+ expect { @cmdb.do_request :DELETE => '/chorizo' }.to raise_error
193
+ end
194
+
195
+ end
196
+
197
+ end
198
+
199
+ describe "#get_or_assign_system_name" do
200
+
201
+ before(:each) do
202
+ @cmdb = NOMS::CMDB.new $opt
203
+ end
204
+
205
+ it 'creates an entry with a new system name' do
206
+ response = @cmdb.get_or_assign_system_name('MCN0021')
207
+ expect(response).to have_key 'fqdn'
208
+ expect(response['fqdn']).to include 'm-0'
209
+ expect(response['serial']).to eq 'MCN0021'
210
+ end
211
+
212
+ end
213
+
214
+ end
@@ -0,0 +1,83 @@
1
+ #!/usr/bin/env rspec
2
+
3
+ require 'noms/cmdb'
4
+ require 'spec_helper'
5
+ require 'fileutils'
6
+
7
+
8
+ describe NOMS::CMDB::RestMock do
9
+
10
+ before(:all) do
11
+ FileUtils.mkdir 'test' unless Dir.exists? 'test'
12
+ $datafile = 'test/data.json'
13
+ NOMS::CMDB.mock! $datafile
14
+ File.unlink($datafile) if File.exist? $datafile
15
+ end
16
+
17
+ context 'initializing' do
18
+
19
+ describe '#new' do
20
+
21
+ it 'creates a new instance' do
22
+
23
+ cmdb = NOMS::CMDB.new $opt
24
+ expect(cmdb).to be_an_instance_of NOMS::CMDB
25
+
26
+ end
27
+
28
+ end
29
+
30
+ end
31
+
32
+ describe '#do_query' do
33
+
34
+ before(:each) do
35
+ @cmdb = NOMS::CMDB.new $opt
36
+ end
37
+
38
+ after(:all) do
39
+ unless $opt['debug'] > 0
40
+ File.unlink $datafile
41
+ FileUtils.rmdir 'test'
42
+ end
43
+ end
44
+
45
+ context :PUT do
46
+
47
+ it 'creates a new entry' do
48
+ @cmdb.do_request :PUT => '/environments/production', :body => {
49
+ 'name' => 'production', 'environment_name' => 'production' }
50
+ expect(File.exist? $datafile).to be true
51
+ end
52
+
53
+ end
54
+
55
+ context :GET do
56
+
57
+ it 'finds an existing entry' do
58
+ result = @cmdb.do_request :GET => '/environments/production'
59
+ expect(result).to be_a Hash
60
+ expect(result).to include 'name' => 'production'
61
+ end
62
+
63
+ it 'raises an exception for missing entries' do
64
+ expect { @cmdb.do_request :GET => '/environments/1' }.to raise_error
65
+ expect { @cmdb.do_request :GET => '/chorizo' }.to raise_error
66
+ expect { @cmdb.do_request :GET => '/environments/2' }.to raise_error
67
+ end
68
+
69
+ end
70
+
71
+ context :DELETE do
72
+
73
+ it 'deletes an existing entry' do
74
+ result = @cmdb.do_request :DELETE => '/environments/production'
75
+ expect(result).to be true
76
+ expect(@cmdb.all_data[$server][$cmdbapi + '/environments']).to have(0).items
77
+ end
78
+
79
+ end
80
+
81
+ end
82
+
83
+ end
@@ -0,0 +1,35 @@
1
+ #!/usr/bin/env bats
2
+
3
+ # @test "noms-mock instance" {
4
+ # noms --mock=/dev/null cloud list
5
+ # }
6
+
7
+ setup() {
8
+ mkdir -p test/etc
9
+ cat >test/etc/noms.conf <<EOF
10
+ { "cmdb": { "url": "http://cmdb/cmdb_api/v1" },
11
+ "ncc": { "url": "http://ncc-api/ncc_api/v2" }
12
+ }
13
+ EOF
14
+ }
15
+
16
+ teardown() {
17
+ rm -rf test/etc
18
+ }
19
+
20
+ @test "noms-mock cmdb" {
21
+ cat >test/data.json <<EOF
22
+ { "cmdb": {
23
+ "/cmdb_api/v1/system": [
24
+ { "id": "test1.example.com",
25
+ "fqdn": "test1.example.com",
26
+ "status": "idle",
27
+ "environment": "testing",
28
+ "data_center_code": "DC1",
29
+ "ip_address": "10.0.0.1" }
30
+ ]
31
+ }
32
+ }
33
+ EOF
34
+ noms --config=test/etc/noms.conf --mock=test/data.json cmdb show test1.example.com
35
+ }
@@ -0,0 +1,91 @@
1
+ #!/usr/bin/env bats
2
+
3
+ # @test "noms-mock instance" {
4
+ # noms --mock=/dev/null cloud list
5
+ # }
6
+
7
+ setup() {
8
+ mkdir -p test/etc
9
+ cat >test/etc/noms.conf <<EOF
10
+ { "cmdb": { "url": "http://cmdb/cmdb_api/v1" },
11
+ "ncc": { "url": "http://ncc-api/ncc_api/v2" }
12
+ }
13
+ EOF
14
+ cat >test/data.json <<EOF
15
+ { "ncc-api": {
16
+ "/ncc_api/v2": {
17
+ "version": "0.6.9"
18
+ },
19
+ "/ncc_api/v2/clouds": ["os0", "aws"],
20
+ "/ncc_api/v2/clouds/os0": {
21
+ "provider": "openstack",
22
+ "status": "ok"
23
+ },
24
+ "/ncc_api/v2/clouds/os0/instances/1": {
25
+ "name": "testist1.local",
26
+ "id": "1",
27
+ "status": "active",
28
+ "role": [ ],
29
+ "size": "m1.small",
30
+ "image": "deb6",
31
+ "host": "ostack01.local"
32
+ },
33
+ "/ncc_api/v2/clouds/os0/instances/1/console": {
34
+ "url": "vnc://ostack01.local:5959"
35
+ },
36
+ "/ncc_api/v2/sizes": [
37
+ {
38
+ "name": "m1.large",
39
+ "description": "8GB RAM 80GB disk",
40
+ "cores": 4,
41
+ "ram": 8000,
42
+ "disk": 80
43
+ },
44
+ {
45
+ "name": "m1.small",
46
+ "description": "2GB RAM 10GB disk",
47
+ "cores": 1,
48
+ "ram": 2000,
49
+ "disk": 10
50
+ }
51
+ ],
52
+ "/ncc_api/v2/images": [
53
+ {
54
+ "operatingsystemrelease": "6",
55
+ "osfamily": "debian",
56
+ "description": "Debian6 Image",
57
+ "operatingsystem": "Debian"
58
+ },
59
+ {
60
+ "operatingsystemrelease": "7",
61
+ "osfamily": "debian",
62
+ "description": "Debian7 Image",
63
+ "operatingsystem": "Debian"
64
+ }
65
+ ]
66
+ }
67
+ }
68
+ EOF
69
+ }
70
+
71
+ teardown() {
72
+ rm -rf test/etc
73
+ }
74
+
75
+ @test "noms-admin server-info" {
76
+ noms --config=test/etc/noms.conf --mock=test/data.json describe ncc | grep "version: 0.6.9"
77
+ }
78
+
79
+ @test "noms-admin describe cloud os0" {
80
+ noms --config=test/etc/noms.conf --mock=test/data.json describe cloud os0 | grep 'provider: openstack'
81
+ }
82
+
83
+ @test "noms-admin get console (negative)" {
84
+ noms --config=test/etc/noms.conf --mock=test/data.json instance show os0 1 | grep -v 'console_url'
85
+ }
86
+
87
+ @test "noms-admin get console (positive)" {
88
+ noms --config=test/etc/noms.conf --mock=test/data.json --fields fqdn,console instance show os0 1 |\
89
+ grep 'console: vnc://ostack01.local:5959'
90
+ }
91
+
metadata ADDED
@@ -0,0 +1,148 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: noms-client
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.9.0
5
+ platform: ruby
6
+ authors:
7
+ - Jeremy Brinkley
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-01-27 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.7'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec-collection_matchers
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: optconfig
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description:
84
+ email:
85
+ - jbrinkley@evernote.com
86
+ executables:
87
+ - ansible-cmdb
88
+ - noms
89
+ extensions: []
90
+ extra_rdoc_files: []
91
+ files:
92
+ - .gitignore
93
+ - Gemfile
94
+ - LICENSE
95
+ - README.rst
96
+ - Rakefile
97
+ - TODO
98
+ - bin/ansible-cmdb
99
+ - bin/noms
100
+ - control.m4
101
+ - etc/noms.conf
102
+ - lib/ncc/client.rb
103
+ - lib/noms/client/version.rb
104
+ - lib/noms/cmdb.rb
105
+ - lib/noms/errors.rb
106
+ - lib/noms/httpclient.rb
107
+ - lib/noms/nagui.rb
108
+ - lib/spec_helper.rb
109
+ - noms-client.gemspec
110
+ - spec/01nomscmdb_spec.rb
111
+ - spec/02noms.sh
112
+ - spec/03ncc_spec.rb
113
+ - spec/04cmdb-mock_spec.rb
114
+ - spec/05restmock-persist_spec.rb
115
+ - spec/06noms-mock.sh
116
+ - spec/07noms-admin.sh
117
+ homepage: http://github.com/evernote/noms-client
118
+ licenses:
119
+ - Apache-2
120
+ metadata: {}
121
+ post_install_message:
122
+ rdoc_options: []
123
+ require_paths:
124
+ - lib
125
+ required_ruby_version: !ruby/object:Gem::Requirement
126
+ requirements:
127
+ - - '>='
128
+ - !ruby/object:Gem::Version
129
+ version: '0'
130
+ required_rubygems_version: !ruby/object:Gem::Requirement
131
+ requirements:
132
+ - - '>='
133
+ - !ruby/object:Gem::Version
134
+ version: '0'
135
+ requirements: []
136
+ rubyforge_project:
137
+ rubygems_version: 2.0.14
138
+ signing_key:
139
+ specification_version: 4
140
+ summary: Client libraries and command-line tool for NOMS components
141
+ test_files:
142
+ - spec/01nomscmdb_spec.rb
143
+ - spec/02noms.sh
144
+ - spec/03ncc_spec.rb
145
+ - spec/04cmdb-mock_spec.rb
146
+ - spec/05restmock-persist_spec.rb
147
+ - spec/06noms-mock.sh
148
+ - spec/07noms-admin.sh