cucumber-chef 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (51) hide show
  1. data/.document +5 -0
  2. data/.gitignore +45 -0
  3. data/Gemfile +21 -0
  4. data/LICENSE +201 -0
  5. data/README.md +83 -0
  6. data/Rakefile +53 -0
  7. data/VERSION +1 -0
  8. data/bin/cucumber-chef +162 -0
  9. data/cookbooks/cucumber-chef/README.rdoc +8 -0
  10. data/cookbooks/cucumber-chef/files/default/add-git-identity +2 -0
  11. data/cookbooks/cucumber-chef/files/default/controller-first-boot +1 -0
  12. data/cookbooks/cucumber-chef/files/default/cucumber-net +5 -0
  13. data/cookbooks/cucumber-chef/files/default/cucumber-private-key +27 -0
  14. data/cookbooks/cucumber-chef/files/default/cucumber-run_list +1 -0
  15. data/cookbooks/cucumber-chef/files/default/git-private-key +27 -0
  16. data/cookbooks/cucumber-chef/files/default/install-chef +1 -0
  17. data/cookbooks/cucumber-chef/files/default/lxc-controller-network-config +5 -0
  18. data/cookbooks/cucumber-chef/files/default/lxc-lucid-chef +377 -0
  19. data/cookbooks/cucumber-chef/files/default/permissive-ssh-config +3 -0
  20. data/cookbooks/cucumber-chef/metadata.rb +6 -0
  21. data/cookbooks/cucumber-chef/recipes/controller.rb +50 -0
  22. data/cookbooks/cucumber-chef/recipes/lxc.rb +35 -0
  23. data/cookbooks/cucumber-chef/recipes/test_lab.rb +23 -0
  24. data/cookbooks/cucumber-chef/recipes/testrunner.rb +46 -0
  25. data/cookbooks/cucumber-chef/roles/controller.rb +7 -0
  26. data/cookbooks/cucumber-chef/roles/test_lab_test.rb +9 -0
  27. data/cookbooks/cucumber-chef/templates/default/controller-client.erb +5 -0
  28. data/cookbooks/cucumber-chef/templates/default/lxc-lucid-chef +385 -0
  29. data/cucumber-chef.gemspec +118 -0
  30. data/features/installing.feature +10 -0
  31. data/features/steps/installing_steps.rb +34 -0
  32. data/features/steps/setup_steps.rb +32 -0
  33. data/features/steps/upload_steps.rb +11 -0
  34. data/features/steps/usage_steps.rb +62 -0
  35. data/features/support/env.rb +25 -0
  36. data/features/support/filetools.rb +9 -0
  37. data/features/support/silent_system.rb +4 -0
  38. data/features/usage.feature +26 -0
  39. data/lib/cucumber-chef.rb +1 -0
  40. data/lib/cucumber/chef.rb +195 -0
  41. data/lib/cucumber/chef/handy.rb +87 -0
  42. data/lib/cucumber/chef/templates/controller.erb +35 -0
  43. data/lib/cucumber/chef/templates/env.rb +16 -0
  44. data/lib/cucumber/chef/templates/example_feature.erb +11 -0
  45. data/lib/cucumber/chef/templates/example_step.erb +19 -0
  46. data/lib/cucumber/chef/templates/readme.erb +28 -0
  47. data/lib/cucumber/chef/templates/ubuntu10.04-gems.erb +43 -0
  48. data/lib/cucumber/chef/version.rb +5 -0
  49. data/lib/cucumber/ec2_server_create.rb +99 -0
  50. data/spec/unit/cucumber_chef_spec.rb +270 -0
  51. metadata +213 -0
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
@@ -0,0 +1,45 @@
1
+ # rcov generated
2
+ coverage
3
+
4
+ # rdoc generated
5
+ rdoc
6
+
7
+ # yard generated
8
+ doc
9
+ .yardoc
10
+
11
+ # bundler
12
+ .bundle
13
+ Gemfile.lock
14
+
15
+ # jeweler generated
16
+ pkg
17
+ *.gem
18
+
19
+
20
+ # Have editor/IDE/OS specific files you need to ignore? Consider using a global gitignore:
21
+ #
22
+ # * Create a file at ~/.gitignore
23
+ # * Include files you want ignored
24
+ # * Run: git config --global core.excludesfile ~/.gitignore
25
+ #
26
+ # After doing this, these files will be ignored in all your git projects,
27
+ # saving you from having to 'pollute' every project you touch with them
28
+ #
29
+ # Not sure what to needs to be ignored for particular editors/OSes? Here's some ideas to get you started. (Remember, remove the leading # of the line)
30
+ #
31
+ # For MacOS:
32
+ #
33
+ #.DS_Store
34
+ #
35
+ # For TextMate
36
+ #*.tmproj
37
+ #tmtags
38
+ #
39
+ # For emacs:
40
+ *~
41
+ \#*
42
+ .\#*
43
+
44
+ # For vim:
45
+ *.swp
data/Gemfile ADDED
@@ -0,0 +1,21 @@
1
+ source "http://rubygems.org"
2
+ # Add dependencies required to use your gem here.
3
+ # Example:
4
+ # gem "activesupport", ">= 2.3.5"
5
+
6
+ # Add dependencies to develop your gem here.
7
+ # Include everything needed to run rake, tests, features, etc.
8
+
9
+ gem "chef", ">= 0.10.0"
10
+ gem "cucumber", ">= 0"
11
+ gem "cucumber-nagios", ">= 0"
12
+ gem "rspec", ">= 0"
13
+ gem "fog", ">= 0"
14
+ gem "thor", ">= 0"
15
+ gem "awesome_print", ">= 0"
16
+
17
+ group :development do
18
+ gem "bundler", "~> 1.0.0"
19
+ gem "jeweler", "~> 1.6.2"
20
+ gem "rcov", ">= 0"
21
+ end
data/LICENSE ADDED
@@ -0,0 +1,201 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "[]"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright [yyyy] [name of copyright owner]
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
@@ -0,0 +1,83 @@
1
+ Cucumber-chef is a library of tools to enable the emerging discipline of infrastructure as code to practice test driven development. It provides a testing platform within which cucumber tests can be run which provision lightweight virtual machines, configure them by applying the appriporaite Chef roles to them, and then run acceptance and integration tests against the environment.
2
+
3
+ ## Installation
4
+
5
+ Cucumber-chef is distributed as a gem. Until it's published on Rubygems, you'll need to build the gem yourself. Check the project out, and then build and install the gem.
6
+
7
+ First ensure you have bundler available, and then run:
8
+
9
+ $ bundle install
10
+
11
+ Cucumber-chef uses Jeweler (see https://github.com/technicalpickles/jeweler) for managing gem builds , publishing and dependencies. To build the gem simply run:
12
+
13
+ $ rake build
14
+
15
+ This will result in the gem appearing in pkg/. Install it:
16
+
17
+ $ gem install pkg/cucumber-chef-0.0.4.gem
18
+
19
+ ## Overview
20
+
21
+ Cucumber-chef begins with a very simple premise. If we are framing our infratructure as code - if we're writing cookbooks, recipes and other pieces of automation in a high level programming language, such as Ruby, then it makes sense to follow the current wisdom across the software development world to maximise the quality, maintainability and reusability of our code, providing maximum chance that we'll deliver value with it. One area which has been shown to have a very positive effect is the practive of 'test-driven' development. In this paradigm, the developer begins by writing a test that captures the intended behaviour of the code they are going to write. This test will start out by failing. The developer then writes code to make the test pass, and iterates thereafter.
22
+
23
+ Cucumber-chef provides a framework to make it easier to do test-driven development for infrastructure.
24
+
25
+ ### Vocabulary
26
+
27
+ Throughout this documentation, a few terms will crop up regularly. It makes sense to define these up front, as they're just terms we've been using since we started writing cucumber-chef. They may even change, but in the meantime, so we're all n the same page, here are some of the terms we use:
28
+
29
+ * Test Lab: An environment made up (at present) of an EC2 instance, configured to be an LXC host. This machine does nothing other than provide space in which Linux containers can be created and destroyed.
30
+ * Controller: One special Linux container which acts as the central point from which tests are run. This machine is where the tests run, and has connectivity and credentials to connect to the machines that are created as part of a test run.
31
+ * Container: A container is a lightweight virtual machine - it is entirely self-contained, with its own process tree, resource constraints, filesystem and network stack. It shares a kernel with the Test Lab host server.
32
+
33
+ ## Getting started
34
+
35
+ Cucumber-chef is tightly integrated with Chef - it uses your knife.rb for credentials, and any cucumber-chef-specific configuration goes in knife.rb under the cucumber-chef namespace.
36
+
37
+ On installation, the first thing you should do is run:
38
+
39
+ $ cucumber-chef displayconfig
40
+
41
+ This will look for your knife.rb, and extract the relevant sections, check them, and display them on the screen. If any entries are missing, it will alert you.
42
+
43
+ The current recommendation is to keep your knife.rb inside your organisation's chef repository, in .chef, and use environment variables to specify username, organisation name and cloud provider credentials. Cucumber-chef supports and encourages this approach. It will search for a directory called .chef in your current directory, and then carry on going up the directory tree until it finds one. In practice this means that if you stay within the chef-repo directory for the organisation on which you're working, cucumber-chef will use the knife.rb; if your elsewhere in the filesystem rooted in your home directory, and have .chef in your home directory, cucumber-chef will use that. Otherwise you'll need to either change into a directory where a .chef can be found, or copy, creatre or link accordingly. In most cases we anticipate that you'll be inside the chef-repo of your organisation, and the documentation is written from this perspective.
44
+
45
+ In its current incarnation, cucumber-chef makes two important assumptions. Firstly, we assume you're using the Opscode platform rather than your own Chef server. Secondly, we assume that you are comfortable with using Amazon's EC2 service for providing the 'bare metal' on which we set up the test lab. Future releases may well allow you to use your own Chef server, and will definitely look at offering alternative cloud providers. In the meantime, we welcome patches and pull requests!
46
+
47
+ Set your environment variables:
48
+
49
+ $ export OPSCODE_USER=platform_user_name
50
+ $ export ORGNAME=platform_organisation
51
+ $ export AWS_ACCESS_KEY_ID=SEKRITKEY
52
+ $ export AWS_SECRET_ACCESS_KEY=REELYSEKRITKEY
53
+
54
+ Now check your config again, with cucumber-chef display config. If you get no complaints, you're ready to set up a test lab.
55
+
56
+ $ cucumber-chef setup
57
+
58
+ This command will set up a complete test lab environment, including a controller. As long as you've provided valid AWS and Opscode credentials, it will do this automatically. The process takes about 12 minutes, after which you'll have a fully cuntioning platform available for you to use.
59
+
60
+ The next stage is to set up a project. A project is simply a directory structure for containing your cucumber features and steps, already set up with an appropriate environment to make use of the step definitions provided with cucumber-chef. We think it makes most sense to have this in your organisation's chef repo. Cucumber-chef provides a task which will create a the directory for you, and populate it with a README and an example feature and step.
61
+
62
+ $ cucumber-chef project someproject
63
+
64
+ **TO DO: fix this....**
65
+
66
+ ## Writing tests
67
+
68
+ Once you've got your test lab set up, and you've generated a project, it's time to crack on with writing a test. The basic idea is this:
69
+
70
+ 1) An infrastructure requirement is established
71
+ 2) Write a cucumber feature that expresses the required behaviour of the infrastructure requirement
72
+ 3) Write steps that will build this infrastructure environment on the test lab, using the step definitions provided - these include the ability to create a container, apply roles to it, and destroy it again.
73
+ 4) Write cookbooks and recipes and supporting code to make the test pass
74
+
75
+ ## Running tests
76
+
77
+ You can write the tests and Chef code wherever you like. We're assuming you prefer working on your local machine, and checking into version control. But we don't really care. When it's time to run tests, cucumber-chef provides a task which handles this:
78
+
79
+ $ cucumber-chef test myproject
80
+
81
+ At the moment cucumber-chef doesn't pass though clever filtering and tagging options that cucumber supports - you run all te tests. We're going to improve that soon, again, patches and pull requests very welcome.
82
+
83
+ Running the test task will upload your current project to the controller, and run the tests.
@@ -0,0 +1,53 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+ require 'rake'
11
+
12
+ require 'jeweler'
13
+ Jeweler::Tasks.new do |gem|
14
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
15
+ gem.name = "cucumber-chef"
16
+ gem.homepage = "http://github.com/atalanta/cucumber-chef"
17
+ gem.license = "MIT"
18
+ gem.summary = "Tests Chef-built infrastructure"
19
+ gem.description = "Framework for behaviour-drive infrastructure development."
20
+ gem.email = "stephen@atalanta-systems.com"
21
+ gem.authors = ["Stephen Nelson-Smith"]
22
+ gem.has_rdoc = false
23
+ gem.bindir = "bin"
24
+ gem.files = `git ls-files`.split("\n")
25
+ gem.executables = %w(cucumber-chef)
26
+ end
27
+ Jeweler::RubygemsDotOrgTasks.new
28
+
29
+ require 'rake/testtask'
30
+ Rake::TestTask.new(:test) do |test|
31
+ test.libs << 'lib' << 'test'
32
+ test.pattern = 'test/**/test_*.rb'
33
+ test.verbose = true
34
+ end
35
+
36
+ require 'rcov/rcovtask'
37
+ Rcov::RcovTask.new do |test|
38
+ test.libs << 'test'
39
+ test.pattern = 'test/**/test_*.rb'
40
+ test.verbose = true
41
+ end
42
+
43
+ task :default => :test
44
+
45
+ require 'rdoc/task'
46
+ Rake::RDocTask.new do |rdoc|
47
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
48
+
49
+ rdoc.rdoc_dir = 'rdoc'
50
+ rdoc.title = "cucumber-chef #{version}"
51
+ rdoc.rdoc_files.include('README*')
52
+ rdoc.rdoc_files.include('lib/**/*.rb')
53
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.4.0
@@ -0,0 +1,162 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'rubygems'
4
+ require 'thor'
5
+ require 'pathname'
6
+ require 'fileutils'
7
+ require 'yaml'
8
+ require 'awesome_print'
9
+ require 'chef'
10
+ require 'chef/knife'
11
+ require 'chef/knife/bootstrap'
12
+ require 'chef/knife/ssh'
13
+ require 'socket'
14
+ require 'net/ssh/multi'
15
+ require 'readline'
16
+ require 'stringio'
17
+
18
+ $LOAD_PATH << File.join(File.dirname(__FILE__), '../lib')
19
+ require 'cucumber-chef'
20
+
21
+ GEM_DIR = Gem.default_dir
22
+
23
+ class CucumberChef < Thor
24
+ include Thor::Actions
25
+
26
+ def self.source_root
27
+ File.dirname(__FILE__)
28
+ end
29
+
30
+ def initialize(args = [], options = {}, config = {})
31
+ @provisioner = ::Cucumber::Chef::Provisioner.new
32
+ super(args, options, config)
33
+ end
34
+
35
+ no_tasks do
36
+ def tcp_test_ssh(hostname)
37
+ tcp_socket = TCPSocket.new(hostname, 22)
38
+ IO.select([tcp_socket], nil, nil, 5)
39
+ rescue Errno::ETIMEDOUT
40
+ false
41
+ rescue Errno::EPERM
42
+ false
43
+ rescue Errno::ECONNREFUSED
44
+ sleep 2
45
+ false
46
+ # This happens on EC2 quite often
47
+ rescue Errno::EHOSTUNREACH
48
+ sleep 2
49
+ false
50
+ ensure
51
+ tcp_socket && tcp_socket.close
52
+ end
53
+
54
+ def get_latest_version
55
+ installed_versions = []
56
+ Gem::source_index.find_name('cucumber-chef').map do |g|
57
+ version = g.version.version
58
+ if version == ""
59
+ version = "0"
60
+ end
61
+ installed_versions << version
62
+ end
63
+ version = installed_versions.sort { |a, b| a.to_i <=> b.to_i }.last
64
+ if version == "0"
65
+ version = ""
66
+ end
67
+ return "cucumber-chef-" + version
68
+ end
69
+
70
+ def create_directory_structure(project_dir)
71
+ %w{step_definitions support}.each do |dir|
72
+ FileUtils.mkdir_p(project_dir + "features" + dir)
73
+ end
74
+ end
75
+
76
+ def generate_project_skeleton(project_dir)
77
+ template_dir = self.gem_root + 'lib' + 'cucumber' + 'chef' + 'templates'
78
+ templates = {
79
+ "readme.erb" => 'README',
80
+ "example_feature.erb" => 'features/example.feature',
81
+ "example_step.erb" => 'features/step_definitions/example_step.rb',
82
+ "env.rb" => "features/support/env.rb"
83
+ }
84
+ templates.each do |filename, destination|
85
+ template((template_dir + filename).realpath,
86
+ project_dir + destination)
87
+ end
88
+ end
89
+
90
+ def gem_root
91
+ Pathname.new(__FILE__).parent.parent
92
+ end
93
+
94
+ def error(message)
95
+ warn message
96
+ exit 255
97
+ end
98
+
99
+ def find_knife_config
100
+ @provisioner.config
101
+ rescue ::Cucumber::Chef::ProvisionerError => err
102
+ error(err.message)
103
+ end
104
+ end
105
+
106
+ desc "project <project name>" , "Create a project template for testing an infrastructure"
107
+ def project(project_name)
108
+ @project = project_name
109
+ project_dir = Pathname.new(".") + "cucumber-chef" + @project
110
+ create_directory_structure(project_dir)
111
+ generate_project_skeleton(project_dir)
112
+ end
113
+
114
+ desc "setup", "Set up a cucumber-chef test lab in Amazon EC2"
115
+ method_option :test, :type => :boolean
116
+ def setup
117
+ begin
118
+ config = find_knife_config
119
+ if options.test?
120
+ config[:mode] = "test"
121
+ else
122
+ config[:mode] = "user"
123
+ end
124
+ @provisioner.verify_opscode_platform_credentials(config)
125
+ @provisioner.verify_aws_credentials(config)
126
+ $stdout.sync
127
+ server = @provisioner.build_test_lab(config, $stdout)
128
+ sleep(10)
129
+ @provisioner.upload_cookbook(config)
130
+ @provisioner.upload_role(config)
131
+ @provisioner.bootstrap_node(server.dns_name, config).run
132
+ rescue ::Cucumber::Chef::ProvisionerError => err
133
+ error(err.message)
134
+ end
135
+ end
136
+
137
+ desc "displayconfig", "Display the current config from knife.rb"
138
+ def displayconfig
139
+ values, missing_keys = Cucumber::Chef::Config.new.display
140
+ puts values.join("\n")
141
+ if missing_keys.size > 0
142
+ error("Incomplete config file, please specify: #{missing_keys.join(", ")}")
143
+ end
144
+ rescue ::Cucumber::Chef::ProvisionerError => err
145
+ error(err.message)
146
+ end
147
+
148
+ desc "upload", "Upload a cucumber-chef test suite to the test lab platform"
149
+ def upload
150
+ puts "Project uploaded to the test-lab."
151
+ end
152
+
153
+ desc "test", "Run a cucumber-chef test suite from a workstation."
154
+ def test
155
+ puts "Running test..."
156
+ puts
157
+ puts "Test results will appear here."
158
+ end
159
+
160
+
161
+ end
162
+ CucumberChef.start