From: Jan Provaznik <[email protected]>

---
 src/app/models/template.rb                        |    6 +-
 src/features/step_definitions/template_steps.rb   |   43 +++++++
 src/features/support/custom.rb                    |   10 ++
 src/features/support/paths.rb                     |    9 ++
 src/features/template.feature                     |   37 ++++++
 src/spec/controllers/templates_controller_spec.rb |   46 ++++----
 src/spec/factories/image.rb                       |   10 +-
 src/spec/factories/instance.rb                    |    2 +-
 src/spec/factories/template.rb                    |   21 ++++
 src/spec/fixtures/packagegroups.json              |   54 +++++++++
 src/spec/fixtures/packages.json                   |  128 +++++++++++++++++++++
 src/spec/fixtures/repositories.json               |   19 +++
 src/spec/models/image_spec.rb                     |   52 ++-------
 src/spec/models/instance_spec.rb                  |    6 +-
 src/spec/models/template_spec.rb                  |   19 +++
 src/spec/utils/repository_manager.rb              |   45 +++++++
 16 files changed, 426 insertions(+), 81 deletions(-)
 create mode 100644 src/features/step_definitions/template_steps.rb
 create mode 100644 src/features/template.feature
 create mode 100644 src/spec/factories/template.rb
 create mode 100644 src/spec/fixtures/packagegroups.json
 create mode 100644 src/spec/fixtures/packages.json
 create mode 100644 src/spec/fixtures/repositories.json
 create mode 100644 src/spec/models/template_spec.rb
 create mode 100644 src/spec/utils/repository_manager.rb

diff --git a/src/app/models/template.rb b/src/app/models/template.rb
index 6469b04..3336081 100644
--- a/src/app/models/template.rb
+++ b/src/app/models/template.rb
@@ -3,13 +3,13 @@ require 'typhoeus'
 
 class Template < ActiveRecord::Base
   has_many :images,  :dependent => :destroy
-  before_validation :update_attrs
+  before_save :update_attrs
 
   WAREHOUSE_CONFIG = YAML.load_file("#{RAILS_ROOT}/config/image_warehouse.yml")
 
-  validates_presence_of :uuid
+  #validates_presence_of :uuid
   #validates_presence_of :name
-  validates_uniqueness_of :uuid
+  #validates_uniqueness_of :uuid
 
   def update_xml_attributes!(opts = {})
     doc = xml
diff --git a/src/features/step_definitions/template_steps.rb 
b/src/features/step_definitions/template_steps.rb
new file mode 100644
index 0000000..c708fce
--- /dev/null
+++ b/src/features/step_definitions/template_steps.rb
@@ -0,0 +1,43 @@
+Given /^There is a mock pulp repository$/ do
+  dir = File.join(Rails.root, 'spec', 'fixtures')
+  hydra = Typhoeus::Hydra.hydra
+  hydra.stub(:get, "http://pulptest/repositories/";).and_return(
+    Typhoeus::Response.new(:code => 200,
+                           :body => File.read(File.join(dir, 
'repositories.json'))))
+  hydra.stub(:get, 
"http://pulptest/repositories/jboss/packagegroups/";).and_return(
+    Typhoeus::Response.new(:code => 200,
+                           :body => File.read(File.join(dir, 
'packagegroups.json'))))
+  hydra.stub(:get, "http://pulptest/repositories/jboss/packages/";).and_return(
+    Typhoeus::Response.new(:code => 200,
+                           :body => File.read(File.join(dir, 
'packages.json'))))
+
+end
+
+Given /^There is a "([^"]*)" template$/ do |name|
+  @template = Template.new
+  @template.xml.name = name
+  @template.save_xml!
+end
+
+Given /^there is a package group$/ do
+  RepositoryManager.new.all_groups.should have_at_least(1).item
+end
+
+Given /^no package is selected$/ do
+  @template.xml.packages = []
+end
+
+Given /^there is one selected package$/ do
+  pkg = RepositoryManager.new.all_packages.first
+  @template.xml.packages = []
+  @template.xml.add_package(pkg['name'], nil)
+  @template.save_xml!
+end
+
+Given /^I jump on the "([^"]*)" template software page$/ do |name|
+  visit url_for :action => 'software', :controller => 'templates', :id => 
@template
+end
+
+Then /^I should have a template named "([^"]*)"$/ do |name|
+  Template.first(:order => 'created_at DESC').xml.name.should eql(name)
+end
diff --git a/src/features/support/custom.rb b/src/features/support/custom.rb
index 73a6300..3adbea3 100644
--- a/src/features/support/custom.rb
+++ b/src/features/support/custom.rb
@@ -44,3 +44,13 @@ CloudAccount.class_eval do
     @key   
   end
 end
+
+RepositoryManager.class_eval do
+  def config
+    [{
+      'baseurl' => 'http://pulptest',
+      'yumurl'  => 'http://pulptest',
+      'type'    => 'pulp',
+    }]
+  end
+end
diff --git a/src/features/support/paths.rb b/src/features/support/paths.rb
index 537dd31..6b45674 100644
--- a/src/features/support/paths.rb
+++ b/src/features/support/paths.rb
@@ -65,6 +65,15 @@ module NavigationHelpers
     when /the new permission page/
       url_for :action => 'new', :controller => 'permissions', :only_path => 
true
 
+    when /the new template page/
+      url_for :action => 'new', :controller => 'templates', :only_path => true
+
+    when /the template services page/
+      url_for :action => 'services', :controller => 'templates', :only_path => 
true
+
+    when /the template software page/
+      url_for :action => 'software', :controller => 'templates', :only_path => 
true
+
     # Add more mappings here.
     # Here is an example that pulls values out of the Regexp:
     #
diff --git a/src/features/template.feature b/src/features/template.feature
new file mode 100644
index 0000000..ed4cd68
--- /dev/null
+++ b/src/features/template.feature
@@ -0,0 +1,37 @@
+Feature: Manage Templates
+  In order to manage my cloud infrastructure
+  As a user
+  I want to manage templates
+
+  Background:
+    Given I am an authorised user
+    And I am logged in
+    And There is a mock pulp repository
+
+  Scenario: Add basic info to a new Template
+         Given I am on the homepage
+         When I follow "Create a Template"
+         Then I should be on the new template page
+         And I should see "Create a New Template"
+         When I fill in the following:
+      | xml_name         | mocktemplate  |
+      | xml_platform     | rhel          |
+      | xml_description  | mockdesc      |
+         And I press "Next"
+         Then I should be on the template services page
+         And I should have a template named "mocktemplate"
+
+  Scenario: Add a package to the template
+    Given There is a "mocktemplate" template
+    And I am on the template software page
+    And there is a package group
+    And no package is selected
+    When I follow "Select" within ".selection_list"
+    Then I should see "Remove" within "#selected_packages"
+
+  Scenario: Remove a package from the template
+    Given There is a "mocktemplate" template
+    And there is one selected package
+    And I jump on the "mocktemplate" template software page
+    When I follow "Remove" within "#selected_packages"
+    Then I should not see "Remove" within "#selected_packages"
diff --git a/src/spec/controllers/templates_controller_spec.rb 
b/src/spec/controllers/templates_controller_spec.rb
index af4fb2c..ba27ecc 100644
--- a/src/spec/controllers/templates_controller_spec.rb
+++ b/src/spec/controllers/templates_controller_spec.rb
@@ -10,23 +10,23 @@ describe TemplatesController do
     activate_authlogic
   end
 
-  it "should allow a user with image_modify permission to create new 
image_descriptor" do
+  it "should allow a user with image_modify permission to create new template" 
do
      UserSession.create(@admin)
      lambda do
        post :new, :xml => { :name => 'fooimg', :platform => 'fedora' }, :next 
=> true
-     end.should change(ImageDescriptor, :count).by(1)
-     id = ImageDescriptor.find(:first, :order => 'created_at DESC').id
+     end.should change(Template, :count).by(1)
+     id = Template.find(:first, :order => 'created_at DESC').id
      response.should redirect_to("http://test.host/templates/services/#{id}";)
   end
 
   it "should allow a user with image_modify permission to add service" do
      UserSession.create(@admin)
-     img = ImageDescriptor.new(:xml => '')
+     tpl = Template.new(:xml => '')
      lambda do
-       img.save!
-     end.should change(ImageDescriptor, :count).by(1)
-     post :services, :xml => { :xml => {:services => ['jboss']} }, :next => 
true, :id => img.id
-     response.should 
redirect_to("http://test.host/templates/software/#{img.id}";)
+       tpl.save!
+     end.should change(Template, :count).by(1)
+     post :services, :xml => { :xml => {:services => ['jboss']} }, :next => 
true, :id => tpl.id
+     response.should 
redirect_to("http://test.host/templates/software/#{tpl.id}";)
   end
 
   # FIXME: these two tests depends on jboss repository which defines 'JBoss 
Core
@@ -35,32 +35,32 @@ describe TemplatesController do
   #
   #it "should allow a user with image_modify permission to add and remove a 
group" do
   #   UserSession.create(@admin)
-  #   img = ImageDescriptor.new(:xml => '')
+  #   tpl = Template.new(:xml => '')
   #   lambda do
-  #     img.save!
-  #   end.should change(ImageDescriptor, :count).by(1)
-  #   post :select_group, :id => img.id, :group => 'JBoss Core Packages'
-  #   response.should 
redirect_to("http://test.host/templates/software/#{img.id}";)
-  #   post :remove_group, :id => img.id, :group => 'JBoss Core Packages'
-  #   response.should 
redirect_to("http://test.host/templates/software/#{img.id}";)
+  #     tpl.save!
+  #   end.should change(Template, :count).by(1)
+  #   post :select_group, :id => tpl.id, :group => 'JBoss Core Packages'
+  #   response.should 
redirect_to("http://test.host/templates/software/#{tpl.id}";)
+  #   post :remove_group, :id => tpl.id, :group => 'JBoss Core Packages'
+  #   response.should 
redirect_to("http://test.host/templates/software/#{tpl.id}";)
   #end
 
   #it "should allow a user with image_modify permission to add a package" do
   #   UserSession.create(@admin)
-  #   img = ImageDescriptor.new(:xml => '')
+  #   tpl = Template.new(:xml => '')
   #   lambda do
-  #     img.save!
-  #     post :select_package, :id => img.id, :package => 'jboss-rails', :group 
=> 'JBoss Core Packages'
-  #   end.should change(ImageDescriptor, :count).by(1)
-  #   response.should 
redirect_to("http://test.host/templates/software/#{img.id}";)
+  #     tpl.save!
+  #     post :select_package, :id => tpl.id, :package => 'jboss-rails', :group 
=> 'JBoss Core Packages'
+  #   end.should change(Template, :count).by(1)
+  #   response.should 
redirect_to("http://test.host/templates/software/#{tpl.id}";)
   #end
 
   it "should allow a user with image_modify permission to build image 
descriptor" do
      UserSession.create(@admin)
-     img = ImageDescriptor.new
+     tpl = Factory :template
      lambda do
-       post :summary, :id => img.id, :targets => ['rhevm'], :build => true
-     end.should change(ImageDescriptorTarget, :count).by(1)
+       post :summary, :id => tpl.id, :targets => ['ec2'], :build => true
+     end.should change(Image, :count).by(1)
   end
 
   it "should deny access to new template ui without image modify permission" do
diff --git a/src/spec/factories/image.rb b/src/spec/factories/image.rb
index cc0bc17..293ac22 100644
--- a/src/spec/factories/image.rb
+++ b/src/spec/factories/image.rb
@@ -3,11 +3,7 @@
 
 Factory.define :image do |i|
   i.sequence(:name) { |n| "image#{n}" }
-  i.sequence(:external_key) { |n| "key#{n}" }
-  i.architecture 'i686'
-  i.provider { |p| Provider.new }
-end
-
-Factory.define :front_end_image, :parent => :image do |i|
-  i.provider nil
+  i.status 'queued'
+  i.target 'ec2'
+  i.association(:template)
 end
diff --git a/src/spec/factories/instance.rb b/src/spec/factories/instance.rb
index 7533c64..3dc37e9 100644
--- a/src/spec/factories/instance.rb
+++ b/src/spec/factories/instance.rb
@@ -3,7 +3,7 @@ Factory.define :instance do |i|
   i.sequence(:external_key) { |n| "key#{n}" }
   i.association :hardware_profile, :factory => :mock_hwp1
   i.association :cloud_account, :factory => :mock_cloud_account
-  i.association :image, :factory => :image
+  i.association :template, :factory => :template
   i.association :pool, :factory => :pool
   i.state "running"
 end
diff --git a/src/spec/factories/template.rb b/src/spec/factories/template.rb
new file mode 100644
index 0000000..4774148
--- /dev/null
+++ b/src/spec/factories/template.rb
@@ -0,0 +1,21 @@
+Factory.define :template do |i|
+  i.sequence(:name) { |n| "template#{n}" }
+  i.xml <<EOF
+<image>
+  <name>tpl</name>
+  <repos>
+    <repo>https://localhost/pulp/repos/jboss2</repo>
+  </repos>
+  <os>fedora</os>
+  <description/>
+  <services/>
+  <groups>
+    <group>JBoss Core Packages</group>
+  </groups>
+  <packages>
+    <package><name>jboss-as5</name><group>JBoss Core Packages</group></package>
+    <package><name>jboss-jgroups</name><group>JBoss Core 
Packages</group></package>
+  </packages>
+</image>
+EOF
+end
diff --git a/src/spec/fixtures/packagegroups.json 
b/src/spec/fixtures/packagegroups.json
new file mode 100644
index 0000000..9dd6008
--- /dev/null
+++ b/src/spec/fixtures/packagegroups.json
@@ -0,0 +1,54 @@
+{ "JBoss   Social Networking Web Application" : { "_id" : "JBoss Social 
Networking Web  Application",
+      "conditional_package_names" : {  },
+      "default" : true,
+      "default_package_names" : [  ],
+      "description" : "This group contains a JBoss social networking 
application",
+      "display_order" : 1024,
+      "id" : "JBoss Social Networking Web Application",
+      "immutable" : true,
+      "langonly" : null,
+      "mandatory_package_names" : [  ],
+      "name" : "JBoss Social Networking Web Application",
+      "optional_package_names" : [ "JSocNet" ],
+      "repo_defined" : true,
+      "translated_description" : {  },
+      "translated_name" : {  },
+      "user_visible" : true
+    },
+  "JBoss Drools" : { "_id" : "JBoss Drools",
+      "conditional_package_names" : {  },
+      "default" : true,
+      "default_package_names" : [  ],
+      "description" : "This group contains the JBoss Drools component",
+      "display_order" : 1024,
+      "id" : "JBoss Drools",
+      "immutable" : true,
+      "langonly" : null,
+      "mandatory_package_names" : [  ],
+      "name" : "JBoss Drools",
+      "optional_package_names" : [ "drools-guvnor" ],
+      "repo_defined" : true,
+      "translated_description" : {  },
+      "translated_name" : {  },
+      "user_visible" : true
+    },
+  "JBoss" : { "_id" : "JBoss",
+      "conditional_package_names" : {  },
+      "default" : true,
+      "default_package_names" : [ "jboss-jgroups",
+          "jboss-as5"
+        ],
+      "description" : "This group is a collection of core JBoss packages",
+      "display_order" : 1024,
+      "id" : "JBoss",
+      "immutable" : true,
+      "langonly" : null,
+      "mandatory_package_names" : [  ],
+      "name" : "JBoss Core Packages",
+      "optional_package_names" : [ "jboss-       rails" ],
+      "repo_defined" : true,
+      "translated_description" : {  },
+      "translated_name" : {  },
+      "user_visible" : true
+    }
+}
diff --git a/src/spec/fixtures/packages.json b/src/spec/fixtures/packages.json
new file mode 100644
index 0000000..bb98881
--- /dev/null
+++ b/src/spec/fixtures/packages.json
@@ -0,0 +1,128 @@
+{ "101f17d4-018b-4af4-bfed-46b142c8b721" : { "_id" : 
"101f17d4-018b-4af4-bfed-46b142c8b721",
+      "_ns" : "packages",
+      "arch" : "noarch",
+      "checksum" : { "sha256" : "ec00a837f6995b4af77b0eaa5edf69570e0149a8" },
+      "description" : "The Drools Guvnor Repository for JBoss AS 5",
+      "download_url" : 
"http://localhost//pub/jboss/J-SocialNet-1.0.1.GA-1.noarch.rpm";,
+      "epoch" : "0",
+      "filename" : "J-SocialNet-1.0.1.GA-1.noarch.rpm",
+      "id" : "101f17d4-018b-4af4-bfed-46b142c8b721",
+      "name" : "J-SocialNet",
+      "provides" : [ "J-SocialNet" ],
+      "release" : "1",
+      "requires" : [ "/bin/sh",
+          "jboss- as5"
+        ],
+      "vendor" : null,
+      "version" : "1.0.1.    GA"
+    },
+  "124eef28-0d3d-46eb-8b8b-ae1acba104e0" : { "_id" : 
"124eef28-0d3d-46eb-8b8b-ae1acba104e0",
+      "_ns" : "packages",
+      "arch" : "noarch",
+      "checksum" : { "sha256" : "cf09c311b72b9b0fb0a93ab94fa40aa8c31550de" },
+      "description" : "JBoss JGroups",
+      "download_url" : "http://localhost//pub/jboss/jboss-jgroups-2.6.7.GA-1.  
 noarch.rpm",
+      "epoch" : "0",
+      "filename" : "jboss-jgroups-2.6.7.GA-1.noarch.rpm",
+      "id" : "124eef28-0d3d-46eb-8b8b-ae1acba104e0",
+      "name" : "jboss-jgroups",
+      "provides" : [ "jboss-jgroups" ],
+      "release" : "1",
+      "requires" : [ "/bin/sh" ],
+      "vendor" : null,
+      "version" : "2.6.7.GA"
+    },
+  "1668d633-b86d-4cf0-86d9-745b023c1a16" : { "_id" : 
"1668d633-b86d-4cf0-86d9-745b023c1a16",
+      "_ns" : "packages",
+      "arch" : "noarch",
+      "checksum" : { "sha256" : "7b3c66d475357a7d1fc1697f81bc8076388d35aa" },
+      "description" : "The JBoss Rails deployer for AS5",
+      "download_url" : 
"http://localhost//pub/jboss/jboss-rails-1.0.0.Beta3-1.noarch.rpm";,
+      "epoch" : "0",
+      "filename" : "jboss-rails-1.0.0.Beta3-1.noarch.rpm",
+      "id" : "1668d633-b86d-4cf0-86d9-745b023c1a16",
+      "name" : "jboss-rails",
+      "provides" : [ "perl(jdbc_adapter)",
+          "jboss-rails"
+        ],
+      "release" : "1",
+      "requires" : [ "/bin/sh" ],
+      "vendor" : null,
+      "version" : "1.0.0.Beta3"
+    },
+  "229f8733-0996-4d38-af2d-8e10ceea6661" : { "_id" : 
"229f8733-0996-4d38-af2d-8e10ceea6661",
+      "_ns" : "packages",
+      "arch" : "noarch",
+      "checksum" : { "sha256" : "5148c747ced6a5e25728e49b55f97b5cb8d2e7a0" },
+      "description" : "The Drools Guvnor Repository for JBoss AS 5",
+      "download_url" : 
"http://localhost//pub/jboss/drools-guvnor-5.0.1.GA-1.noarch.rpm";,
+      "epoch" : "0",
+      "filename" : "drools-guvnor-5.0.1.GA-1.noarch.rpm",
+      "id" : "229f8733-0996-4d38-af2d-8e10ceea6661",
+      "name" : "drools-guvnor",
+      "provides" : [ "drools-guvnor" ],
+      "release" : "1",
+      "requires" : [ "/bin/sh",
+          "jboss-as5"
+        ],
+      "vendor" : null,
+      "version" : "5.0.1.GA"
+    },
+  "9fcca05a-af6e-47f0-a4ea-0e31d4a38192" : { "_id" : 
"9fcca05a-af6e-47f0-a4ea-0e31d4a38192",
+      "_ns" : "packages",
+      "arch" : "noarch",
+      "checksum" : { "sha256" : "d373add70015c6aa4d0ef24175ea4ca74f26e54f" },
+      "description" : "A script that produces javadoc-style documentation 
from\nwell-formed JavaScript sourcefiles. At the moment, this means 
it\nsupports    sourcefiles where all functions are mapped to a class\nusing 
prototype-based inheritance.  Anonymous function\ndefinitions (e.g. 
Circle.prototype.getRadius =         function(){ ...} )\nare supported.",
+      "download_url" : 
"http://localhost//pub/jboss/JSDoc-1.10.2-7.fc11.noarch.rpm";,
+      "epoch" : "0",
+      "filename" : "JSDoc-1.10.2-7.fc11.noarch.rpm",
+      "id" : "9fcca05a-af6e-47f0-a4ea-          0e31d4a38192",
+      "name" : "JSDoc",
+      "provides" : [ "perl(JSDoc::XML)",
+          "perl(JSDoc)",
+          "JSDoc",
+          "perl(JavaScript::Syntax::HTML)",
+          "perl(JSDoc::XMI)"
+        ],
+      "release" : "7.fc11",
+      "requires" : [ "perl(Data::Dumper)",
+          "perl(constant)",
+          "perl(:MODULE_COMPAT_5.10.0)",
+          "perl(File::Basename)",
+          "perl(JavaScript::Syntax::HTML)",
+          "perl(warnings)",
+          "perl(JSDoc)",
+          "perl(File::Path)",
+          "perl(JSDoc::XML)",
+          "perl(lib)",
+          "perl(Exporter)",
+          "perl",
+          "/usr/bin/perl",
+          "perl(HTML::         Template)",
+          "perl(Getopt::Long)",
+          "perl(vars)",
+          "perl(File::Copy)",
+          "perl(File::Find)",
+          "perl(JSDoc::XMI)",
+          "perl(strict)"
+        ],
+      "vendor" : null,
+      "version" : "1.10.2"
+    },
+  "a2d1440e-2f22-480f-ace9-04d40361856f" : { "_id" : 
"a2d1440e-2f22-480f-ace9-04d40361856f",
+      "_ns" : "packages",
+      "arch" : "noarch",
+      "checksum" : { "sha256" : "0c77cb7f6c57a39b00ec6ece7fb372ac6ed08277" },
+      "description" : "The JBossAS 5 Java Application Server",
+      "download_url" : 
"http://localhost//pub/jboss/jboss-as5-5.0.0.GA-1.noarch.rpm";,
+      "epoch" : "0",
+      "filename" : "jboss-as5-5.0.0.GA-1.noarch.rpm",
+      "id" : "a2d1440e-2f22-480f-ace9-04d40361856f",
+      "name" : "jboss-as5",
+      "provides" : [ "jboss-as5" ],
+      "release" : "1",
+      "requires" : [ "/bin/sh" ],
+      "vendor" : null,
+      "version" : "5.0.0.GA"
+    }
+}
diff --git a/src/spec/fixtures/repositories.json 
b/src/spec/fixtures/repositories.json
new file mode 100644
index 0000000..f3a2723
--- /dev/null
+++ b/src/spec/fixtures/repositories.json
@@ -0,0 +1,19 @@
+[ { "_id" : "jboss",
+    "arch" : "x86_64",
+    "errata" : "/repositories/jboss/errata/",
+    "id" : "jboss",
+    "name" : "jboss",
+    "packagegroupcategories" : "/repositories/jboss/packagegroupcategories/",
+    "packagegroups" : "/repositories/jboss/packagegroups/",
+    "packages" : "/repositories/jboss/packages/",
+    "source" : { "supported_types" : [ "yum",
+            "local",
+            "rhn"
+          ],
+        "type" : "yum",
+        "url" : "http://example/jboss";
+      },
+    "sync_schedule" : null,
+    "uri_ref" : "/repositories/jboss/",
+    "use_symlinks" : false
+  } ]
diff --git a/src/spec/models/image_spec.rb b/src/spec/models/image_spec.rb
index a0fd8ca..030e792 100644
--- a/src/spec/models/image_spec.rb
+++ b/src/spec/models/image_spec.rb
@@ -1,23 +1,6 @@
 require 'spec_helper'
 
 describe Image do
-  before(:each) do
-    @provider = Factory.build(:mock_provider)
-    @client = mock('DeltaCloud', :null_object => true)
-    @provider.stub!(:connect).and_return(@client)
-  end
-
-  it "should have a unique external key" do
-    i1 = Factory.create(:image, :provider => @provider)
-    i2 = Factory.create(:image, :provider => @provider)
-    @provider.images = [i1, i2]
-    i1.should be_valid
-    i2.should be_valid
-
-    i2.external_key = i1.external_key
-    i2.should_not be_valid
-  end
-
   it "should have a name" do
     i = Factory.build(:image, :name => nil)
     i.should_not be_valid
@@ -38,36 +21,17 @@ describe Image do
     i.should be_valid
   end
 
-  it "should have an architecture if it has a provider" do
-    i = Factory.build(:image, :architecture => nil)
-    i.should_not be_valid
-
-    i.architecture = 'i686'
-    i.should be_valid
-  end
-
-  it "should have provider images only if it has a provider" do
-    i = Factory.create(:image, :provider => nil)
-
-    i.aggregator_images << i
-    i.should have(1).error_on(:aggregator_images)
-    i.errors.on(:aggregator_images).should eql(
-      "Aggregator image only allowed for provider images")
-
-    i.aggregator_images.clear
-    i.should be_valid
+  it "should have automatically generated uuid after save" do
+    i = Factory.build(:image)
+    i.save
+    i.uuid.should_not be_nil
   end
 
-  it "should have aggregator images only if it has a pool" do
-    i = Factory.create(:image)
-
-    i.provider_images << i
-    i.should have(1).error_on(:provider_images)
-    i.errors.on(:provider_images).should eql(
-      "Provider images only allowed for aggregator images")
+  it "should have template_id" do
+    i = Factory.build(:image, :template_id => nil)
+    i.should_not be_valid
 
-    i.provider_images.clear
+    i.template_id = 1
     i.should be_valid
   end
-
 end
diff --git a/src/spec/models/instance_spec.rb b/src/spec/models/instance_spec.rb
index 16e8eb9..49bf625 100644
--- a/src/spec/models/instance_spec.rb
+++ b/src/spec/models/instance_spec.rb
@@ -24,11 +24,11 @@ describe Instance do
     @instance.should be_valid
   end
 
-  it "should require image to be set" do
-    @instance.image_id = nil
+  it "should require template to be set" do
+    @instance.template_id = nil
     @instance.should_not be_valid
 
-    @instance.image_id = 1
+    @instance.template_id = 1
     @instance.should be_valid
   end
 
diff --git a/src/spec/models/template_spec.rb b/src/spec/models/template_spec.rb
new file mode 100644
index 0000000..ef30fc1
--- /dev/null
+++ b/src/spec/models/template_spec.rb
@@ -0,0 +1,19 @@
+require 'spec_helper'
+
+describe Template do
+  it "should have automatically generated uuid after validation" do
+    t = Factory.build(:template)
+    t.uuid = nil
+    t.save
+    t.uuid.should_not be_nil
+  end
+
+  it "should return list of providers who provides images built from this 
template" do
+    tpl = Factory.build(:template)
+    img = Factory.build(:image, :template_id => tpl)
+    provider = Factory.build(:mock_provider)
+    rimg = ReplicatedImage.new(:provider_id => provider, :image_id => img)
+    rimg.save
+    tpl.providers.size.should eql(1)
+  end
+end
diff --git a/src/spec/utils/repository_manager.rb 
b/src/spec/utils/repository_manager.rb
new file mode 100644
index 0000000..cac151c
--- /dev/null
+++ b/src/spec/utils/repository_manager.rb
@@ -0,0 +1,45 @@
+require 'spec_helper'
+
+describe RepositoryManager do
+  before(:all) do
+    @repositories_json = File.read(File.join(File.dirname(__FILE__),
+                                             '../fixtures/repositories.json'))
+    @packagegroups_json = File.read(File.join(File.dirname(__FILE__),
+                                              
'../fixtures/packagegroups.json'))
+    @packages_json = File.read(File.join(File.dirname(__FILE__),
+                                         '../fixtures/packages.json'))
+  end
+
+  before(:each) do
+    hydra = Typhoeus::Hydra.hydra
+    hydra.stub(:get, "http://pulptest/repositories/";).and_return(
+      Typhoeus::Response.new(:code => 200, :body => @repositories_json))
+    hydra.stub(:get, 
"http://pulptest/repositories/jboss/packagegroups/";).and_return(
+      Typhoeus::Response.new(:code => 200, :body => @packagegroups_json))
+    hydra.stub(:get, 
"http://pulptest/repositories/jboss/packages/";).and_return(
+      Typhoeus::Response.new(:code => 200, :body => @packages_json))
+
+    @rmanager = RepositoryManager.new(:config => [{
+      'baseurl' => 'http://pulptest',
+      'yumurl' => 'http://pulptest',
+      'type'    => 'pulp',
+    }])
+  end
+
+  it "should return a list of repositories" do
+    @rmanager.repositories.should have(1).items
+    @rmanager.repositories.first.id.should eql('jboss')
+  end
+
+  it "should return a list of packagegroups" do
+    rep = @rmanager.repositories.first
+    rep.groups.keys.sort.should == ["JBoss Core Packages", "JBoss Drools",
+      "JBoss Social Networking Web Application"]
+  end
+
+  it "should return a list of packages" do
+    rep = @rmanager.repositories.first
+    rep.packages.map {|p| p[:name]}.sort.should == ["J-SocialNet", "JSDoc",
+      "drools-guvnor", "jboss-as5", "jboss-jgroups", "jboss-rails"]
+  end
+end
-- 
1.7.2.2

_______________________________________________
deltacloud-devel mailing list
[email protected]
https://fedorahosted.org/mailman/listinfo/deltacloud-devel

Reply via email to