Welcome to mirror list, hosted at ThFree Co, Russian Federation.

pages_deployment_spec.rb « models « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: e74c7ee861268506d35f70dd49af9794ad518d15 (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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe PagesDeployment, feature_category: :pages do
  let_it_be(:project) { create(:project) }

  describe 'associations' do
    it { is_expected.to belong_to(:project).required }
    it { is_expected.to belong_to(:ci_build).optional }
  end

  describe 'validations' do
    it { is_expected.to validate_presence_of(:file) }

    it { is_expected.to validate_presence_of(:size) }
    it { is_expected.to validate_numericality_of(:size).only_integer.is_greater_than(0) }

    it { is_expected.to validate_presence_of(:file_count) }
    it { is_expected.to validate_numericality_of(:file_count).only_integer.is_greater_than_or_equal_to(0) }

    it { is_expected.to validate_presence_of(:file_sha256) }

    it { is_expected.to validate_inclusion_of(:file_store).in_array(ObjectStorage::SUPPORTED_STORES) }

    it 'is valid when created from the factory' do
      expect(create(:pages_deployment)).to be_valid
    end
  end

  context 'with deployments stored locally and remotely' do
    before do
      stub_pages_object_storage(::Pages::DeploymentUploader)
    end

    let!(:remote_deployment) { create(:pages_deployment, project: project, file_store: ::ObjectStorage::Store::REMOTE) }
    let!(:local_deployment) { create(:pages_deployment, project: project, file_store: ::ObjectStorage::Store::LOCAL) }

    describe '.with_files_stored_locally' do
      it 'only returns deployments with files stored locally' do
        expect(described_class.with_files_stored_locally).to contain_exactly(local_deployment)
      end
    end

    describe '.with_files_stored_remotely' do
      it 'only returns deployments with files stored remotely' do
        expect(described_class.with_files_stored_remotely).to contain_exactly(remote_deployment)
      end
    end
  end

  context 'when uploading the file' do
    before do
      stub_pages_object_storage(::Pages::DeploymentUploader)
    end

    it 'stores the file outsize of the transaction' do
      expect_next_instance_of(PagesDeployment) do |deployment|
        expect(deployment).to receive(:store_file_now!)
      end

      create(:pages_deployment, project: project)
    end

    it 'does nothing when the file did not change' do
      deployment = create(:pages_deployment, project: project)

      expect(deployment).not_to receive(:store_file_now!)

      deployment.touch
    end
  end

  describe '.deactivate_deployments_older_than', :freeze_time do
    let!(:other_project_deployment) do
      create(:pages_deployment)
    end

    let!(:other_path_prefix_deployment) do
      create(:pages_deployment, project: project, path_prefix: 'other')
    end

    let!(:deactivated_deployment) do
      create(:pages_deployment, project: project, deleted_at: 5.minutes.ago)
    end

    it 'updates only older deployments for the same project and path prefix' do
      deployment1 = create(:pages_deployment, project: project, updated_at: 5.minutes.ago)
      deployment2 = create(:pages_deployment, project: project, updated_at: 5.minutes.ago)
      deployment3 = create(:pages_deployment, project: project, updated_at: 5.minutes.ago)

      expect { described_class.deactivate_deployments_older_than(deployment2) }
        .to change { deployment1.reload.deleted_at }
        .from(nil).to(Time.zone.now)
        .and change { deployment1.reload.updated_at }
        .to(Time.zone.now)

      expect(deployment2.reload.deleted_at).to be_nil
      expect(deployment3.reload.deleted_at).to be_nil
      expect(other_project_deployment.deleted_at).to be_nil
      expect(other_path_prefix_deployment.reload.deleted_at).to be_nil
      expect(deactivated_deployment.reload.deleted_at).to eq(5.minutes.ago)
    end

    it 'updates only older deployments for the same project with the given time' do
      deployment1 = create(:pages_deployment, project: project, updated_at: 5.minutes.ago)
      deployment2 = create(:pages_deployment, project: project, updated_at: 5.minutes.ago)
      deployment3 = create(:pages_deployment, project: project, updated_at: 5.minutes.ago)
      time = 30.minutes.from_now

      expect { described_class.deactivate_deployments_older_than(deployment2, time: time) }
        .to change { deployment1.reload.deleted_at }
        .from(nil).to(time)
        .and change { deployment1.reload.updated_at }
        .to(Time.zone.now)

      expect(deployment2.reload.deleted_at).to be_nil
      expect(deployment3.reload.deleted_at).to be_nil
      expect(other_project_deployment.deleted_at).to be_nil
      expect(other_path_prefix_deployment.reload.deleted_at).to be_nil
      expect(deactivated_deployment.reload.deleted_at).to eq(5.minutes.ago)
    end
  end

  describe 'default for file_store' do
    let(:deployment) do
      filepath = Rails.root.join("spec/fixtures/pages.zip")

      described_class.create!(
        project: project,
        file: fixture_file_upload(filepath),
        file_sha256: Digest::SHA256.file(filepath).hexdigest,
        file_count: 3
      )
    end

    it 'uses local store when object storage is not enabled' do
      expect(deployment.file_store).to eq(ObjectStorage::Store::LOCAL)
    end

    it 'uses remote store when object storage is enabled' do
      stub_pages_object_storage(::Pages::DeploymentUploader)

      expect(deployment.file_store).to eq(ObjectStorage::Store::REMOTE)
    end
  end

  it 'saves size along with the file' do
    deployment = create(:pages_deployment)
    expect(deployment.size).to eq(deployment.file.size)
  end

  describe '.older_than' do
    it 'returns deployments with lower id' do
      old_deployments = create_list(:pages_deployment, 2)

      deployment = create(:pages_deployment)

      # new deployment
      create(:pages_deployment)

      expect(described_class.older_than(deployment.id)).to eq(old_deployments)
    end
  end
end