Commit 24002101 authored by Alishan Ladhani's avatar Alishan Ladhani

Make TerraformState work with object storage

- Default `file_store` to the terraform_state
  object storage setting
- Update `file_store` whenever the file changes
parent 487c3ae9
......@@ -5,5 +5,17 @@ class TerraformState < ApplicationRecord
validates :project_id, presence: true
after_save :update_file_store, if: :saved_change_to_file?
mount_uploader :file, TerraformStateUploader
def update_file_store
# The file.object_store is set during `uploader.store!`
# which happens after object is inserted/updated
self.update_column(:file_store, file.object_store)
end
def file_store
super || TerraformStateUploader.default_store
end
end
......@@ -34,5 +34,9 @@ class TerraformStateUploader < GitlabUploader
def proxy_download_enabled?
true
end
def default_store
object_store_enabled? ? ObjectStorage::Store::REMOTE : ObjectStorage::Store::LOCAL
end
end
end
......@@ -3,7 +3,50 @@
require 'spec_helper'
describe TerraformState do
subject { create(:terraform_state, :with_file) }
it { is_expected.to belong_to(:project) }
it { is_expected.to validate_presence_of(:project_id) }
before do
stub_terraform_state_object_storage(TerraformStateUploader)
end
describe '#file_store' do
context 'when no value is set' do
it 'returns the default store of the uploader' do
[ObjectStorage::Store::LOCAL, ObjectStorage::Store::REMOTE].each do |store|
expect(TerraformStateUploader).to receive(:default_store).and_return(store)
expect(described_class.new.file_store).to eq(store)
end
end
end
context 'when a value is set' do
it 'returns the value' do
[ObjectStorage::Store::LOCAL, ObjectStorage::Store::REMOTE].each do |store|
expect(build(:terraform_state, file_store: store).file_store).to eq(store)
end
end
end
end
describe '#update_file_store' do
context 'when file is stored in object storage' do
it 'sets file_store to remote' do
expect(subject.file_store).to eq(ObjectStorage::Store::REMOTE)
end
end
context 'when file is stored locally' do
before do
stub_terraform_state_object_storage(Terraform::StateUploader, enabled: false)
end
it 'sets file_store to local' do
expect(subject.file_store).to eq(ObjectStorage::Store::LOCAL)
end
end
end
end
......@@ -61,4 +61,22 @@ describe TerraformStateUploader do
expect(described_class.proxy_download_enabled?).to eq(true)
end
end
describe '.default_store' do
context 'when object storage is enabled' do
it 'returns REMOTE' do
expect(described_class.default_store).to eq(ObjectStorage::Store::REMOTE)
end
end
context 'when object storage is disabled' do
before do
stub_terraform_state_object_storage(enabled: false)
end
it 'returns LOCAL' do
expect(described_class.default_store).to eq(ObjectStorage::Store::LOCAL)
end
end
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment