summaryrefslogtreecommitdiff
path: root/spec/models/ci/resource_group_spec.rb
blob: 50a786419f22e0ea213c850d26c31072882028c2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Ci::ResourceGroup do
  describe 'validation' do
    it 'valids when key includes allowed character' do
      resource_group = build(:ci_resource_group, key: 'test')

      expect(resource_group).to be_valid
    end

    it 'invalids when key includes invalid character' do
      resource_group = build(:ci_resource_group, key: ':::')

      expect(resource_group).not_to be_valid
    end
  end

  describe '#ensure_resource' do
    it 'creates one resource when resource group is created' do
      resource_group = create(:ci_resource_group)

      expect(resource_group.resources.count).to eq(1)
      expect(resource_group.resources.all?(&:persisted?)).to eq(true)
    end
  end

  describe '#assign_resource_to' do
    subject { resource_group.assign_resource_to(build) }

    let(:build) { create(:ci_build) }
    let(:resource_group) { create(:ci_resource_group) }

    it 'retains resource for the processable' do
      expect(resource_group.resources.first.processable).to be_nil

      is_expected.to eq(true)

      expect(resource_group.resources.first.processable).to eq(build)
    end

    context 'when there are no free resources' do
      before do
        resource_group.assign_resource_to(create(:ci_build))
      end

      it 'fails to retain resource' do
        is_expected.to eq(false)
      end
    end

    context 'when the build has already retained a resource' do
      let!(:another_resource) { create(:ci_resource, resource_group: resource_group, processable: build) }

      it 'fails to retain resource' do
        expect { subject }.to raise_error(ActiveRecord::RecordNotUnique)
      end
    end
  end

  describe '#release_resource_from' do
    subject { resource_group.release_resource_from(build) }

    let(:build) { create(:ci_build) }
    let(:resource_group) { create(:ci_resource_group) }

    context 'when the build has already retained a resource' do
      before do
        resource_group.assign_resource_to(build)
      end

      it 'releases resource from the build' do
        expect(resource_group.resources.first.processable).to eq(build)

        is_expected.to eq(true)

        expect(resource_group.resources.first.processable).to be_nil
      end
    end

    context 'when the build has already released a resource' do
      it 'fails to release resource' do
        is_expected.to eq(false)
      end
    end
  end
end