Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

first tests for provisioning localhost

  • Loading branch information...
commit 0f4c4cf4c5643e1d273d1b9fa30aa3441a88249d 1 parent 3b24d53
@csquared csquared authored
View
29 bin/addons-client
@@ -2,35 +2,10 @@
require 'bundler/setup'
require_relative '../lib/addons-client'
-include Addons::Client
begin
- CLI.run!
-rescue UserError => e
+ Addons::CLI.run!
+rescue Addons::UserError => e
STDERR.puts e.message
exit 1
end
-
-pre_token = [Settings[:api_salt], Settings[:api_password]].join(':')
-hashed_pw = Digest::SHA1.hexdigest(pre_token)
-addons_api_url = "http://heroku:#{hashed_pw}@localhost:3000"
-resource = RestClient::Resource.new(addons_api_url)['/api/1/resources']
-addon_name, plan = ARGV[1].split(':')
-p addon_name, plan
-
-RestClient.log = STDOUT
-case ARGV[0]
-when /list/i
-when /provision/i
- p resource.post({
- addon: addon_name,
- plan: plan,
- consumer_id: 'api-client@localhost'
- })
-else
- if ARGV[0]
- puts "#{ARGV[0]} is not a valid command"
- else
- puts "must specify: provision, deprovision, plan change"
- end
-end
View
40 lib/addons-client/cli.rb
@@ -1,25 +1,39 @@
-module Addons::Client::CLI
- def self.run!
+module Addons::CLI
+ extend self
+
+ def run!
define_settings
load_settings!
+ run_command!
end
- def self.run_command!
+ def run_command!
+ command = Settings.rest.first
+ case command
+ when /provision/i
+ slug = Settings.rest[1]
+ raise UserError, "Must supply add-on:plan as second argument" unless slug
+ client.provision!(slug)
+ else
+ if command
+ puts "#{command} is not a valid command"
+ else
+ puts "Command must be one of: provision, deprovision, planchange"
+ end
+ end
+ end
+ def client
+ @client ||= Addons::Client.new(:username => 'heroku',
+ :salt => Settings[:api_salt],
+ :password => Settings[:api_password])
end
- def self.define_settings
- Settings.define :api_salt,
- :env_var => "ADDONS_API_SALT",
- :description => "Salt used for hashing login",
- :required => true
- Settings.define :api_password,
- :env_var => "ADDONS_API_PASSWORD",
- :description => "Addons API password",
- :required => true
+ def define_settings
+ load File.expand_path('../settings.rb', __FILE__)
end
- def self.load_settings!
+ def load_settings!
Settings.use :commandline
Settings.resolve!
rescue RuntimeError => e
View
22 lib/addons-client/client.rb
@@ -1,3 +1,25 @@
class Addons::Client
+ def initialize(opts = {})
+ @username = opts[:username]
+ pre_token = [opts[:salt], opts[:password]].join(':')
+ @password = Digest::SHA1.hexdigest(pre_token)
+ @consumer_id = opts.fetch :consumer_id, 'api-client@localhost'
+ end
+ def provision!(slug)
+ addon_name, plan = slug.split(':')
+ raise UserError, "No add-on name given" unless addon_name
+ raise UserError, "No plan name given" unless plan
+ resource.post({
+ addon: addon_name,
+ plan: plan,
+ consumer_id: "api-client@localhost"
+ })
+ end
+
+ protected
+ def resource
+ addons_api_url = "http://#{@username}:#{@password}@localhost:3000"
+ RestClient::Resource.new(addons_api_url)['/api/1/resources']
+ end
end
View
8 lib/addons-client/settings.rb
@@ -0,0 +1,8 @@
+Settings.define :api_salt,
+ :env_var => "ADDONS_API_SALT",
+ :description => "Salt used for hashing login",
+ :required => true
+Settings.define :api_password,
+ :env_var => "ADDONS_API_PASSWORD",
+ :description => "Addons API password",
+ :required => true
View
31 test/client_settings_test.rb
@@ -1,13 +1,34 @@
require_relative 'test_helper'
class SettingsTest < Addons::Client::TestCase
- def test_requires_api_salt_and_api_password
+ def setup
+ super
+ stub_request(:any, /^http:\/\/heroku/)
+ end
+
+ def test_works_when_options_provided
+ assert_nothing_raised { Addons::CLI.run! }
+ end
+
+ def test_hashes_password
+ password = Digest::SHA1.hexdigest('salt:bacon')
+ addons_client! "provision memcache:5mb"
+ assert_requested :post, /heroku:#{password}/
+ end
+
+ def test_requires_api_password
+ Settings.delete :api_password
+ ENV['ADDONS_API_PASSWORD'] = nil
assert_raises Addons::UserError do
- Addons::Client::CLI.run!
+ addons_client! "provision memcache:5mb"
end
+ end
- ENV['ADDONS_API_SALT'] = 'salt'
- ENV['ADDONS_API_PASSWORD'] = 'bacon'
- assert_nothing_raised { Addons::Client::CLI.run! }
+ def test_requires_api_salt
+ Settings.delete :api_salt
+ ENV['ADDONS_API_SALT'] = nil
+ assert_raises Addons::UserError do
+ addons_client! "provision memcache:5mb"
+ end
end
end
View
39 test/provision_test.rb
@@ -0,0 +1,39 @@
+require_relative 'test_helper'
+
+class ProvisionTest < Addons::Client::TestCase
+ def setup
+ super
+ stub_request(:any, /resources$/)
+ @client = Addons::Client.new(:username => 'test',
+ :password => 'pass',
+ :salt => 'salt')
+ end
+
+ def test_provisions_from_cmd_line
+ password = Digest::SHA1.hexdigest('salt:bacon')
+ addons_client! "provision memcache:5mb"
+
+ target_url = "http://heroku:#{password}@localhost:3000/api/1/resources"
+ assert_requested(:post, target_url,
+ body: { addon: 'memcache', plan: '5mb',
+ consumer_id: 'api-client@localhost'})
+ end
+
+ def test_provisions_from_ruby
+ password = Digest::SHA1.hexdigest('salt:pass')
+ @client.provision! 'foo:plizzan'
+ target_url = "http://test:#{password}@localhost:3000/api/1/resources"
+ assert_requested(:post, target_url,
+ body: { addon: 'foo', plan: 'plizzan',
+ consumer_id: 'api-client@localhost'})
+ end
+
+ def test_sets_consumer_id
+ end
+
+ def test_sets_scheme
+ end
+
+ def test_accepts_options
+ end
+end
View
16 test/test_helper.rb
@@ -1,8 +1,22 @@
require 'test/unit'
Bundler.require :test
-
+require 'webmock/test_unit'
require_relative '../lib/addons-client'
class Addons::Client::TestCase < Test::Unit::TestCase
include RR::Adapters::TestUnit
+
+ def setup
+ ENV['ADDONS_API_SALT'] = 'salt'
+ ENV['ADDONS_API_PASSWORD'] = 'bacon'
+ end
+
+ def teardown
+ WebMock.reset!
+ end
+
+ def addons_client! cmd
+ stub(Settings).rest { cmd.split }
+ Addons::CLI.run!
+ end
end
Please sign in to comment.
Something went wrong with that request. Please try again.