Commit e68fdc58 authored by Alishan Ladhani's avatar Alishan Ladhani

Generate serverless UUID and assign it by default

- Create a method to generate serverless UUIDs
- Assign a UUID to Serverless::DomainCluster by default
parent 6c765178
......@@ -4,11 +4,16 @@ module Serverless
class DomainCluster < ApplicationRecord
self.table_name = 'serverless_domain_cluster'
HEX_REGEXP = %r{\A\h+\z}.freeze
belongs_to :pages_domain
belongs_to :knative, class_name: 'Clusters::Applications::Knative', foreign_key: 'clusters_applications_knative_id'
belongs_to :creator, class_name: 'User', optional: true
validates :pages_domain, :knative, :uuid, presence: true
validates :uuid, uniqueness: true, length: { is: 14 }
validates :pages_domain, :knative, presence: true
validates :uuid, presence: true, uniqueness: true, length: { is: Gitlab::Serverless::Domain::UUID_LENGTH },
format: { with: HEX_REGEXP, message: 'only allows hex characters' }
default_value_for(:uuid, allows_nil: false) { Gitlab::Serverless::Domain.generate_uuid }
end
end
......@@ -20,7 +20,7 @@ module Gitlab
protected
def context(function_id)
function = Serverless::Function.find_by_id(function_id)
function = ::Serverless::Function.find_by_id(function_id)
{
function_name: function.name,
kube_namespace: function.namespace
......
# frozen_string_literal: true
module Gitlab
module Serverless
class Domain
UUID_LENGTH = 14
def self.generate_uuid
SecureRandom.hex(UUID_LENGTH / 2)
end
end
end
end
......@@ -5,6 +5,5 @@ FactoryBot.define do
pages_domain { create(:pages_domain) }
knative { create(:clusters_applications_knative) }
creator { create(:user) }
uuid { SecureRandom.hex(7) }
end
end
# frozen_string_literal: true
require 'spec_helper'
describe Gitlab::Serverless::Domain do
describe '.generate_uuid' do
it 'has 14 characters' do
expect(described_class.generate_uuid.length).to eq(described_class::UUID_LENGTH)
end
it 'consists of only hexadecimal characters' do
expect(described_class.generate_uuid).to match(/\A\h+\z/)
end
it 'uses random characters' do
uuid = 'abcd1234567890'
expect(SecureRandom).to receive(:hex).with(described_class::UUID_LENGTH / 2).and_return(uuid)
expect(described_class.generate_uuid).to eq(uuid)
end
end
end
......@@ -8,10 +8,17 @@ describe Serverless::DomainCluster do
describe 'validations' do
it { is_expected.to validate_presence_of(:pages_domain) }
it { is_expected.to validate_presence_of(:knative) }
it { is_expected.to validate_presence_of(:uuid) }
it { is_expected.to validate_presence_of(:uuid) }
it { is_expected.to validate_length_of(:uuid).is_equal_to(Gitlab::Serverless::Domain::UUID_LENGTH) }
it { is_expected.to validate_uniqueness_of(:uuid) }
it { is_expected.to validate_length_of(:uuid).is_equal_to(14) }
it 'validates that uuid has only hex characters' do
subject = build(:serverless_domain_cluster, uuid: 'z1234567890123')
subject.valid?
expect(subject.errors[:uuid]).to include('only allows hex characters')
end
end
describe 'associations' do
......@@ -19,4 +26,24 @@ describe Serverless::DomainCluster do
it { is_expected.to belong_to(:knative) }
it { is_expected.to belong_to(:creator).optional }
end
describe 'uuid' do
context 'when nil' do
it 'generates a value by default' do
attributes = build(:serverless_domain_cluster).attributes.merge(uuid: nil)
expect(Gitlab::Serverless::Domain).to receive(:generate_uuid).and_call_original
subject = Serverless::DomainCluster.new(attributes)
expect(subject.uuid).not_to be_blank
end
end
context 'when not nil' do
it 'does not override the existing value' do
uuid = 'abcd1234567890'
expect(build(:serverless_domain_cluster, uuid: uuid).uuid).to eq(uuid)
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