Skip to content

Commit

Permalink
Sprucing up some stubbed spec data.
Browse files Browse the repository at this point in the history
  • Loading branch information
nz committed Feb 1, 2010
1 parent a1d7741 commit bae6890
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 37 deletions.
2 changes: 1 addition & 1 deletion lib/sous/cluster.rb
Expand Up @@ -54,7 +54,7 @@ def connection
def servers
connection.servers.select do |server|
server.state =~ /running|pending/
end
end unless connection.servers.nil? || connection.servers.empty?
end

# TODO: add support for Array, Hash, Proc, whatever
Expand Down
26 changes: 18 additions & 8 deletions lib/sous/environment.rb
Expand Up @@ -31,20 +31,30 @@ def aws_access_key_id(aws_access_key_id=nil)
self.attributes[:aws_access_key_id]
end

def aws_secret_access_key(aws_secret_access_key=nil)
self.attributes[:aws_secret_access_key] = aws_secret_access_key unless aws_secret_access_key.nil?
self.attributes[:aws_secret_access_key]
end

def handle
[cluster.name, name].join("-")
end

def servers
# TODO: check to see if credentials are provided here for a separate cloud provider
# Otherwise, defer upward to the cluster
@servers ||= cluster.servers
def connection
@connection ||= if aws_access_key_id && aws_secret_access_key
Fog::AWS::EC2.new(
:aws_access_key_id => aws_access_key_id,
:aws_secret_access_key => aws_secret_access_key
)
else
cluster.connection
end
end

def connection
# TODO: check to see if credentials are provided here for a separate cloud provider
# Otherwise, defer upward to the cluster
@connection ||= cluster.connection
def servers
connection.servers.select do |server|
server.state =~ /running|pending/
end unless connection.servers.nil? || connection.servers.empty?
end

def image_id(image_id=nil)
Expand Down
32 changes: 22 additions & 10 deletions lib/sous/role.rb
Expand Up @@ -29,22 +29,34 @@ def aws_access_key_id(aws_access_key_id=nil)
self.attributes[:aws_access_key_id]
end

def aws_secret_access_key(aws_secret_access_key=nil)
self.attributes[:aws_secret_access_key] = aws_secret_access_key unless aws_secret_access_key.nil?
self.attributes[:aws_secret_access_key]
end

def handle
[cluster.name, environment.name, name].join("-")
end

def servers(reload=false)
# TODO: check to see if credentials are provided here for a separate cloud provider
# Otherwise, defer upward to the environment
environment.servers.select do |server|
server.groups.include?(handle)
def connection
@connection ||= if aws_access_key_id && aws_secret_access_key
Fog::AWS::EC2.new(
:aws_access_key_id => aws_access_key_id,
:aws_secret_access_key => aws_secret_access_key
)
else
environment.connection
end
end

def connection
# TODO: check to see if credentials are provided here for a separate cloud provider
# Otherwise, defer upward to the environment
environment.connection

def servers
if connection.servers.nil? || connection.servers.empty?
[]
else
connection.servers.select do |server|
server.state =~ /running|pending/
end
end
end

def cluster_attributes
Expand Down
5 changes: 4 additions & 1 deletion spec/sous/cluster_spec.rb
Expand Up @@ -5,7 +5,10 @@

before(:each) do
Fog.mock!
@cluster = Cluster.new(:test)
@cluster = Cluster.new(:test) do
aws_access_key_id 'test'
aws_secret_access_key 'test'
end
end

it "should require a name" do
Expand Down
17 changes: 10 additions & 7 deletions spec/sous/environment_spec.rb
Expand Up @@ -53,16 +53,13 @@
end

describe "servers" do
it "should default to an empty array" do
@environment.servers.should == []
before(:each) do
@environment.stub!(:connection).and_return(mock_connection)
end
it "should look to the cluster if authorization is not provided" do
@environment.cluster.should_receive(:servers)
it "should look to the connection for servers" do
@environment.should_receive(:connection)
@environment.servers
end
it "should query for servers if authorization is provided on the role" do
pending "TODO: add support for a separate, role-specific cloud provider"
end
end

protected
Expand All @@ -73,5 +70,11 @@ def mock_cluster
:servers => []
)
end

def mock_connection
@mock_connection ||= mock("connection",
:servers => []
)
end

end
30 changes: 20 additions & 10 deletions spec/sous/role_spec.rb
Expand Up @@ -5,6 +5,7 @@

before(:each) do
@role = Role.new(:test, mock_environment)
@role.stub!(:connection).and_return(mock_connection)
end

it "should require a name" do
Expand Down Expand Up @@ -35,11 +36,8 @@
end

describe "servers" do
it "should query for servers if authorization is provided on the role" do
pending "TODO: add support for a separate, role-specific cloud provider"
end
it "should look to the environment if authorization is not provided" do
mock_environment.should_receive(:servers)
it "should look to the connection for its servers" do
@role.should_receive(:connection).at_least(:once).and_return(mock_connection)
@role.servers
end
end
Expand All @@ -58,27 +56,39 @@ def mock_environment
:name => "environment",
:connection => mock_connection,
:servers => mock_servers,
:image_id => ""
:image_id => "",
:security_group => nil
)
end

def mock_cluster
@mock_cluster ||= mock(Cluster,
:verbose? => false,
:name => "cluster"
:name => "cluster",
:security_group => nil
)
end

def mock_connection
@mock_connection ||= mock("connection",
:servers => mock_servers
@mock_connection ||= mock(Fog::AWS::EC2,
:servers => mock_servers,
:security_groups => mock_security_groups
)
end

def mock_servers
@mock_servers ||= mock("servers",
:create => mock("new server"),
:length => 0
:length => 0,
:select => [],
:empty? => true
)
end

def mock_security_groups
@mock_security_groups ||= mock("security groups",
:create => [],
:collect => []
)
end

Expand Down

0 comments on commit bae6890

Please sign in to comment.