Project

General

Profile

Revision 3bc253a2

Added by David Davis over 8 years ago

Refs #7642 - Create docker image and tag models

View differences:

.gitignore
8 8
test/dummy/log/*.log
9 9
test/dummy/tmp/
10 10
test/dummy/.sass-cache
11
.ruby-version
12
.ruby-gemset
.rubocop.yml
1
AllCops:
2
  RunRailsCops: true # always run the rails cops
3

  
1 4
# Don't enforce documentation
2 5
Style/Documentation:
3 6
  Enabled: false
......
10 13
Rails/ActionFilter:
11 14
  EnforcedStyle: filter
12 15

  
13
Style/LineLength:
14
  Max: 100
16
Metrics/LineLength:
17
  Max: 132 # Half of a 1920x1080 screen
15 18

  
16
Style/MethodLength:
19
Metrics/MethodLength:
17 20
  Max: 20
18 21

  
19 22
StringLiterals:
app/controllers/containers/steps_controller.rb
21 21
      when :preliminary
22 22
        @container.update_attribute(:compute_resource_id, params[:container][:compute_resource_id])
23 23
      when :image
24
        @container.update_attribute(:image, params[:image])
24
        @container.image = params[:image]
25
        @container.update_attributes(params[:container])
25 26
      when :configuration
26 27
        @container.update_attributes(params[:container])
27 28
      when :environment
app/models/container.rb
1 1
class Container < ActiveRecord::Base
2 2
  belongs_to :compute_resource
3
  belongs_to :docker_image
4
  belongs_to :docker_tag
3 5

  
4
  attr_accessible :command, :image, :name, :compute_resource_id, :tag, :entrypoint,
6
  attr_accessible :command, :image, :name, :compute_resource_id, :entrypoint,
5 7
                  :cpu_set, :cpu_shares, :memory, :tty, :attach_stdin,
6
                  :attach_stdout, :attach_stderr
8
                  :attach_stdout, :attach_stderr, :tag
7 9

  
8 10
  def parametrize
9
    { :name => name, :cmd => [command], :image => image, :tty => tty,
11
    { :name => name, :cmd => [command], :image => docker_image.image_id, :tty => tty,
10 12
      :attach_stdout => attach_stdout, :attach_stdout => attach_stdout,
11 13
      :attach_stderr => attach_stderr, :cpushares => cpu_shares, :cpuset => cpu_set,
12 14
      :memory => memory }
13 15
  end
16

  
17
  def image
18
    docker_image.try(:image_id)
19
  end
20

  
21
  def image=(image_id)
22
    image = DockerImage.find_or_create_by_image_id!(image_id)
23
    self.docker_image_id = image.id
24
  end
25

  
26
  def tag
27
    docker_tag.try(:tag)
28
  end
29

  
30
  def tag=(tag_name)
31
    tag = DockerTag.find_or_create_by_tag_and_docker_image_id!(tag_name, docker_image_id)
32
    self.docker_tag_id = tag.id
33
  end
14 34
end
app/models/docker_image.rb
1
class DockerImage < ActiveRecord::Base
2
  has_many :docker_tags, :dependent => :destroy
3
  has_many :containers
4

  
5
  attr_accessible :image_id, :size
6
end
app/models/docker_tag.rb
1
class DockerTag < ActiveRecord::Base
2
  belongs_to :docker_image
3

  
4
  attr_accessible :tag, :docker_image_id
5

  
6
  validates :docker_image_id, :presence => true
7
end
db/migrate/.rubocop.yml
1
inherit_from: ../../.rubocop.yml
2

  
3
# migrations line up column names
4
Style/SingleSpaceBeforeFirstArg:
5
  Enabled: false
db/migrate/20141010173220_create_docker_images.rb
1
class CreateDockerImages < ActiveRecord::Migration
2
  def up
3
    create_table :docker_images do |t|
4
      t.string  :image_id
5
      t.integer :size
6
      t.timestamps
7
    end
8

  
9
    create_table :docker_tags do |t|
10
      t.string  :tag
11
      t.references :docker_image, :null => false
12
      t.timestamps
13
    end
14
    add_foreign_key :docker_tags, :docker_images,
15
                    :column => :docker_image_id
16

  
17
    remove_column :containers, :image
18
    remove_column :containers, :tag
19
    add_column :containers, :docker_image_id, :integer
20
    add_column :containers, :docker_tag_id, :integer
21
    add_foreign_key :containers, :docker_images,
22
                    :column => :docker_image_id
23
    add_foreign_key :containers, :docker_tags,
24
                    :column => :docker_tag_id
25
  end
26

  
27
  def down
28
    drop_table :docker_images
29
    drop_table :docker_tags
30

  
31
    add_column :containers, :image, :string
32
    add_column :containers, :tag, :string
33
    remove_column :containers, :docker_image_id
34
    remove_column :containers, :docker_tag_id
35
  end
36
end
test/factories/docker_image.rb
1
FactoryGirl.define do
2
  factory :docker_image do
3
    sequence(:image_id) { |n| "image#{n}" }
4
  end
5
end
test/factories/docker_tag.rb
1
FactoryGirl.define do
2
  factory :docker_tag do
3
    sequence(:tag) { |n| "tag#{n}" }
4
    docker_image
5
  end
6
end
test/functionals/containers_controller_test.rb
1
require 'test_plugin_helper'
2

  
3
class ContainersControllerTest < ActionController::TestCase
4
  test 'redirect if Docker provider is not available' do
5
    get :index, {}, set_session_user
6
    assert_redirected_to new_compute_resource_path
7
  end
8

  
9
  test 'index if Docker resource is available' do
10
    Fog.mock!
11
    # Avoid rendering errors by not retrieving any container
12
    ComputeResource.any_instance.stubs(:vms).returns([])
13
    FactoryGirl.create(:docker_cr)
14
    get :index, {}, set_session_user
15
    assert_template 'index'
16
  end
17

  
18
  test 'deleting a container in compute resource redirects to containers index' do
19
    Fog.mock!
20
    container_resource = FactoryGirl.create(:docker_cr)
21
    container          = container_resource.vms.first
22
    container.class.any_instance.expects(:destroy).returns(true)
23
    delete :destroy, { :compute_resource_id => container_resource,
24
                       :id                  => container.id }, set_session_user
25
    assert_redirected_to containers_path
26
  end
27
end
test/functionals/containers_steps_controller_test.rb
1
require 'test_plugin_helper'
2

  
3
module Containers
4
  class StepsControllerTest < ActionController::TestCase
5
    test 'setting a docker image and tag for a new container' do
6
      @container = FactoryGirl.create(:container)
7
      put :update, :id => :image,
8
                   :container_id => @container,
9
                   :image => "centos",
10
                   :container => { :tag => "latest" }
11
      assert_response 302
12
      assert_equal "latest", @container.reload.tag
13
      assert_equal "centos", @container.image
14
    end
15
  end
16
end

Also available in: Unified diff