vscripts 0.1.0 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (40) hide show
  1. checksums.yaml +13 -5
  2. data/.travis.yml +8 -1
  3. data/CHANGELOG.md +23 -0
  4. data/README.md +3 -1
  5. data/Rakefile +1 -3
  6. data/VERSION +1 -1
  7. data/lib/vscripts.rb +1 -1
  8. data/lib/vscripts/aws.rb +5 -0
  9. data/lib/vscripts/aws/ec2.rb +11 -16
  10. data/lib/vscripts/command_line.rb +3 -3
  11. data/lib/vscripts/{command.rb → commands.rb} +1 -1
  12. data/lib/vscripts/commands/identify.rb +9 -19
  13. data/lib/vscripts/commands/tags2facts.rb +7 -25
  14. data/lib/vscripts/util.rb +12 -0
  15. data/lib/vscripts/util/local_system.rb +1 -29
  16. data/spec/aws_spec_helper.rb +10 -0
  17. data/spec/spec_helper.rb +27 -2
  18. data/spec/unit/vscripts/aws/ec2_spec.rb +98 -0
  19. data/spec/unit/vscripts/aws/metadata_spec.rb +62 -0
  20. data/spec/{vscripts → unit/vscripts}/aws_spec.rb +3 -1
  21. data/spec/unit/vscripts/command_line_spec.rb +43 -0
  22. data/spec/unit/vscripts/commands/identify_spec.rb +178 -0
  23. data/spec/unit/vscripts/commands/tags2facts_spec.rb +52 -0
  24. data/spec/unit/vscripts/commands_spec.rb +10 -0
  25. data/spec/unit/vscripts/util/local_system_spec.rb +82 -0
  26. data/spec/{vscripts → unit/vscripts}/version_spec.rb +1 -0
  27. data/spec/unit/vscripts_spec.rb +27 -0
  28. data/tasks/deploy.rake +6 -1
  29. data/tasks/lint.rake +1 -1
  30. data/vscripts.gemspec +3 -1
  31. metadata +86 -56
  32. data/.rspec +0 -2
  33. data/spec/vscripts/aws/ec2_spec.rb +0 -90
  34. data/spec/vscripts/aws/metadata_spec.rb +0 -48
  35. data/spec/vscripts/command_line_spec.rb +0 -34
  36. data/spec/vscripts/command_spec.rb +0 -9
  37. data/spec/vscripts/commands/identify_spec.rb +0 -111
  38. data/spec/vscripts/commands/tags2facts_spec.rb +0 -35
  39. data/spec/vscripts/util/local_system_spec.rb +0 -80
  40. data/spec/vscripts_spec.rb +0 -4
@@ -0,0 +1,98 @@
1
+ require 'spec_helper'
2
+ require 'aws_spec_helper'
3
+ require 'vscripts/aws'
4
+
5
+ describe VScripts::AWS::EC2 do
6
+ subject { Object.new.extend VScripts::AWS }
7
+
8
+ let(:tags) { {'Name' => 'test'} }
9
+ let(:fake_instances) {::AWS::Core::Data.new([
10
+ {id: '1111', status: :running},
11
+ {id: '2222', status: :running, tags: tags},
12
+ {id: '3333', status: :terminated}
13
+ ])}
14
+
15
+ before :each do
16
+ allow(subject).to receive(:check_instance).and_return(true)
17
+ allow(subject).to receive(:instance_id).and_return(fake_instances[0].id)
18
+ allow(subject).to receive(:region).and_return('us-east-1')
19
+ end
20
+
21
+ describe '#ec2' do
22
+ it 'returns AWS::EC2' do
23
+ expect(subject.ec2).to be_an_instance_of ::AWS::EC2
24
+ end
25
+ end
26
+
27
+ describe '#instance' do
28
+ it 'returns AWS::EC2' do
29
+ expect(subject.instance).to be_an_instance_of ::AWS::EC2::Instance
30
+ expect(subject.instance.id).to eq(fake_instances[0].id)
31
+ end
32
+ end
33
+
34
+ describe '#all_tags' do
35
+ it 'returns AWS::EC2::ResourceTagCollection' do
36
+ expect(subject.all_tags)
37
+ .to be_an_instance_of ::AWS::EC2::ResourceTagCollection
38
+ end
39
+ end
40
+
41
+ describe '#tag' do
42
+ it 'returns the tag value' do
43
+ resource = ::AWS.ec2.instances['i-12345678']
44
+ expect(subject.create_tag(resource, 'TestTag', value: 'TestValue'))
45
+ .to be_an_instance_of ::AWS::EC2::Tag
46
+ end
47
+ end
48
+
49
+ describe '#create_tag' do
50
+ it 'creates tag' do
51
+ allow(subject).to receive_message_chain('instance.tags')
52
+ .and_return(tags)
53
+ expect(subject.tag('Name'))
54
+ .to eq('test')
55
+ end
56
+ end
57
+
58
+ describe '#tags_without' do
59
+ it 'returns a filtered Hash' do
60
+ allow(subject).to receive('all_tags')
61
+ .and_return(tags)
62
+ expect(subject.tags_without).to be_an_instance_of Hash
63
+ expect(subject.tags_without(['Name'])).to be_empty
64
+ end
65
+ end
66
+
67
+ describe '#name' do
68
+ it 'returns Hash value' do
69
+ allow(subject).to receive(:all_tags_hash).and_return(tags)
70
+ expect(subject.name).to eq(tags['Name'])
71
+ end
72
+ end
73
+
74
+ describe '#named_instances' do
75
+ it 'returns AWS::EC2::InstanceCollection' do
76
+ expect(subject.named_instances)
77
+ .to be_an_instance_of ::AWS::EC2::InstanceCollection
78
+ end
79
+ end
80
+
81
+ describe '#functional_instances' do
82
+ it 'returns an Array' do
83
+ allow(subject).to receive(:named_instances)
84
+ .and_return(fake_instances)
85
+ expect(subject.functional_instances).to be_an Array
86
+ expect(subject.functional_instances)
87
+ .to include(fake_instances[0], fake_instances[1])
88
+ end
89
+ end
90
+
91
+ describe '#similar_instances' do
92
+ it 'returns an Array' do
93
+ allow(subject).to receive(:functional_instances)
94
+ .and_return([fake_instances[0], fake_instances[1]])
95
+ expect(subject.similar_instances).to include(tags['Name'])
96
+ end
97
+ end
98
+ end
@@ -0,0 +1,62 @@
1
+ require 'spec_helper'
2
+ require 'vscripts/aws'
3
+
4
+ describe VScripts::AWS::Metadata do
5
+ subject { Class.new { extend VScripts::AWS } }
6
+
7
+ before(:each) do
8
+ allow(subject).to receive_message_chain('open.read')
9
+ .and_return('Remote server response')
10
+ end
11
+
12
+ describe '#metadata_url' do
13
+ it 'returns metadata url string' do
14
+ expect(subject.metadata_url).to be_a String
15
+ end
16
+ end
17
+
18
+ describe '#zone' do
19
+ it 'returns zone string' do
20
+ expect(subject.zone).to eq('Remote server response')
21
+ end
22
+ end
23
+
24
+ describe '#region' do
25
+ it 'returns region string' do
26
+ allow(subject).to receive(:zone).and_return('us-test-1a')
27
+ expect(subject.region).to eq('us-test-1')
28
+ end
29
+ end
30
+
31
+ describe '#instance_id' do
32
+ it 'returns instance id string' do
33
+ expect(subject.instance_id).to eq('Remote server response')
34
+ end
35
+ end
36
+
37
+ describe '#public_hostname' do
38
+ it 'returns public hostname string' do
39
+ expect(subject.public_hostname).to eq('Remote server response')
40
+ end
41
+ end
42
+
43
+ describe '#ec2_instance?' do
44
+ it 'returns true if metadata accessible' do
45
+ allow(Net::HTTP).to receive(:get_response).and_return(true)
46
+ expect(subject.ec2_instance?).to be true
47
+ end
48
+ it 'returns false if metadata not accessible' do
49
+ allow(Net::HTTP).to receive(:get_response).and_raise(StandardError)
50
+ expect(subject.ec2_instance?).to be false
51
+ end
52
+ end
53
+
54
+ describe '#check_instance' do
55
+ it 'exits if not ec2 instance' do
56
+ $stderr = StringIO.new
57
+ allow(subject).to receive(:ec2_instance?).and_return(false)
58
+ expect { subject.check_instance }.to raise_error(SystemExit)
59
+ $stderr = STDERR
60
+ end
61
+ end
62
+ end
@@ -1,7 +1,9 @@
1
+ require 'spec_helper'
2
+ require 'aws_spec_helper'
1
3
  require 'vscripts/aws'
2
4
 
3
5
  describe VScripts::AWS do
4
6
  it 'Should load default configuration for AWS SDK' do
5
- expect(::AWS.config.stub_requests).to be_true
7
+ expect(::AWS.config.stub_requests).to be true
6
8
  end
7
9
  end
@@ -0,0 +1,43 @@
1
+ require 'spec_helper'
2
+ require 'vscripts/command_line'
3
+
4
+ describe VScripts::CommandLine do
5
+ subject { VScripts::CommandLine.new([cmd, 'extra_args']) }
6
+ let(:cmd) { VScripts::Commands.list.first.to_s }
7
+
8
+ describe '#new' do
9
+ it 'returns the command line arguments' do
10
+ expect(subject).to be_an_instance_of VScripts::CommandLine
11
+ end
12
+ end
13
+
14
+ describe '#global' do
15
+ it 'returns the global options as a Hash' do
16
+ expect(subject.global).to be_an_instance_of Hash
17
+ end
18
+ end
19
+
20
+ describe '#command' do
21
+ it 'returns the command name' do
22
+ expect(subject.command).to be_a Symbol
23
+ expect(subject.command).to eql cmd.capitalize.to_sym
24
+ end
25
+ end
26
+
27
+ describe '#extra' do
28
+ it 'returns the rest of the arguments as an Array' do
29
+ expect(subject.arguments).to be_an_instance_of Array
30
+ expect(subject.arguments).to eql ['extra_args']
31
+ end
32
+ end
33
+
34
+ describe '#verify_command' do
35
+ it 'fails if command not present' do
36
+ $stderr = StringIO.new
37
+ expect {
38
+ VScripts::CommandLine.new(['TestCommand', 'TestArguments']).command
39
+ }.to raise_error(SystemExit)
40
+ $stderr = STDERR
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,178 @@
1
+ require 'spec_helper'
2
+ require 'vscripts/commands/identify'
3
+
4
+ describe VScripts::Commands::Identify do
5
+ before :each do
6
+ allow(subject).to receive(:tag).and_return('TestTag')
7
+ allow(subject).to receive(:similar_instances)
8
+ .and_return(['TestTag-TestTag-1.'])
9
+ end
10
+
11
+ describe 'USAGE' do
12
+ it 'returns a String' do
13
+ expect(VScripts::Commands::Identify::USAGE).to be_a String
14
+ end
15
+ end
16
+
17
+ describe '#new' do
18
+ context 'when theme is not passed' do
19
+ it 'returns the default theme' do
20
+ expect(subject.theme).to eq('Group-Role-#')
21
+ end
22
+ end
23
+
24
+ context 'when theme is passed' do
25
+ it 'returns the theme' do
26
+ subject1 = VScripts::Commands::Identify.new(['--ec2-tag-theme=test'])
27
+ expect(subject1.theme).to eq('test')
28
+ end
29
+ end
30
+
31
+ context 'when host is not passed' do
32
+ it 'host is nil' do
33
+ expect(subject.host).to be_nil
34
+ end
35
+ end
36
+
37
+ context 'when host is passed' do
38
+ it 'returns the host' do
39
+ subject2 = VScripts::Commands::Identify.new(['--host=test'])
40
+ expect(subject2.host).to eq('test')
41
+ end
42
+ end
43
+
44
+ context 'when domain is not passed' do
45
+ it 'domain is nil' do
46
+ expect(subject.domain).to be_nil
47
+ end
48
+ end
49
+
50
+ context 'when domain is passed' do
51
+ it 'returns the domain' do
52
+ subject3 = VScripts::Commands::Identify.new(['--domain=test'])
53
+ expect(subject3.domain).to eq('test')
54
+ end
55
+ end
56
+ end
57
+
58
+ describe '#parser' do
59
+ it 'returns Trollop' do
60
+ expect(subject.parser).to be_a Trollop::Parser
61
+ end
62
+ end
63
+
64
+ describe '#cli' do
65
+ it 'returns cli arguments as a Hash' do
66
+ expect(subject.cli).to be_a Hash
67
+ end
68
+ end
69
+
70
+ describe '#theme_elements' do
71
+ it 'returns an array' do
72
+ expect(subject.theme_elements).to eq(['Group', 'Role', '#'])
73
+ end
74
+ end
75
+
76
+ describe '#map2tags' do
77
+ it 'returns an array' do
78
+ expect(subject.map2tags).to eq(['TestTag', 'TestTag', '#'])
79
+ end
80
+ end
81
+
82
+ describe '#themed_host_name' do
83
+ it 'returns a string' do
84
+ expect(subject.themed_host_name).to eq('TestTag-TestTag-#')
85
+ end
86
+ end
87
+
88
+ describe '#incremented_hostname' do
89
+ it 'returns a string' do
90
+ expect(subject.incremented_hostname).to eq('TestTag-TestTag-2')
91
+ end
92
+ end
93
+
94
+ describe '#new_hostname' do
95
+ context 'when host is not passed' do
96
+ it 'hostname is incremented' do
97
+ expect(subject.new_hostname).to eq('TestTag-TestTag-2')
98
+ end
99
+ end
100
+
101
+ context 'when host is passed' do
102
+ it 'returns a string' do
103
+ subject4 = VScripts::Commands::Identify.new(['--host=test'])
104
+ expect(subject4.new_hostname).to eq('test')
105
+ end
106
+ end
107
+ end
108
+
109
+ describe '#new_domain' do
110
+ context 'when domain is not passed' do
111
+ context 'when tag is not present' do
112
+ it 'returns a String' do
113
+ allow(subject).to receive(:tag)
114
+ allow(subject).to receive(:local_domain_name).and_return('local')
115
+ expect(subject.new_domain).to eq('local')
116
+ end
117
+ end
118
+
119
+ context 'when tag is present' do
120
+ it 'returns a String' do
121
+ allow(subject).to receive(:tag)
122
+ .and_return('Test')
123
+ expect(subject.new_domain).to eq('Test')
124
+ end
125
+ end
126
+ end
127
+
128
+ context 'when domain is passed' do
129
+ it 'returns a string' do
130
+ subject5 = VScripts::Commands::Identify.new(['--domain=test'])
131
+ expect(subject5.new_domain).to eq('test')
132
+ end
133
+ end
134
+ end
135
+
136
+ describe '#new_fqdn' do
137
+ it 'returns a string' do
138
+ expect(subject.new_fqdn).to eq('TestTag-TestTag-2.TestTag')
139
+ end
140
+ end
141
+
142
+ describe '#set_name_tag' do
143
+ it 'returns a string' do
144
+ allow(subject).to receive(:create_tag).and_return(true)
145
+ allow(subject).to receive(:instance).and_return(true)
146
+ allow(subject).to receive(:puts)
147
+ expect(subject.set_name_tag).to be true
148
+ end
149
+ end
150
+
151
+ describe '#set_hostname' do
152
+ it 'returns nil' do
153
+ allow(subject).to receive('`')
154
+ allow(subject).to receive(:write_file)
155
+ allow(subject).to receive(:puts)
156
+ allow(File).to receive('read').and_return('test')
157
+ expect(subject.set_hostname).to be_nil
158
+ end
159
+ end
160
+
161
+ describe '#update_hosts' do
162
+ it 'returns nil' do
163
+ allow(subject).to receive(:write_file)
164
+ allow(subject).to receive(:puts)
165
+ expect(subject.update_hosts).to be_nil
166
+ end
167
+ end
168
+
169
+ describe '#execute' do
170
+ it 'returns nil' do
171
+ allow(subject).to receive(:set_name_tag)
172
+ allow(subject).to receive(:set_hostname)
173
+ allow(subject).to receive(:update_hosts)
174
+ expect(STDOUT).to receive(:puts).with('Done.')
175
+ subject.execute
176
+ end
177
+ end
178
+ end
@@ -0,0 +1,52 @@
1
+ require 'spec_helper'
2
+ require 'vscripts/commands/tags2facts'
3
+
4
+ describe VScripts::Commands::Tags2facts do
5
+ subject { VScripts::Commands::Tags2facts.new(['extra_args']) }
6
+ before :each do
7
+ allow_any_instance_of(VScripts::Commands::Tags2facts).to receive(:check_instance)
8
+ .and_return(true)
9
+ allow_any_instance_of(VScripts::Commands::Tags2facts).to receive(:region)
10
+ .and_return('us-east-1')
11
+ end
12
+
13
+ describe '#new' do
14
+ it 'returns cli arguments' do
15
+ expect(subject.cli).to be_a Hash
16
+ end
17
+ end
18
+
19
+ describe '#parser' do
20
+ it 'returns parser' do
21
+ expect(subject.parser).to be_an_instance_of Trollop::Parser
22
+ end
23
+ end
24
+
25
+ describe '#exclude_list' do
26
+ it 'returns exclude list' do
27
+ expect(subject.exclude_list).to eq(['Name', 'Domain'])
28
+ end
29
+ end
30
+
31
+ describe '#tags_json' do
32
+ it 'returns JSON formatted string' do
33
+ $stderr = StringIO.new
34
+ allow(subject).to receive(:tags_without)
35
+ .and_return({}, { key: 'value' })
36
+ expect{subject.tags_json}.to raise_error(SystemExit)
37
+ expect(subject.tags_json).to eq("{\n \"key\": \"value\"\n}")
38
+ $stderr = STDERR
39
+ end
40
+ end
41
+
42
+ describe '#execute' do
43
+ it 'executes the command' do
44
+ $stdout = StringIO.new
45
+ allow(subject).to receive(:tags_json)
46
+ allow(subject).to receive(:ensure_file_content)
47
+ subject.execute
48
+ expect($stdout.string).to match(/Writing tags to/)
49
+ $stdout = STDOUT
50
+ end
51
+ end
52
+ end