2020-07-20 05:09:22 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
RSpec.describe Clusters::Agent do
|
|
|
|
subject { create(:cluster_agent) }
|
|
|
|
|
2021-02-16 19:09:19 -05:00
|
|
|
it { is_expected.to belong_to(:created_by_user).class_name('User').optional }
|
2020-07-20 05:09:22 -04:00
|
|
|
it { is_expected.to belong_to(:project).class_name('::Project') }
|
2022-05-24 20:09:46 -04:00
|
|
|
it { is_expected.to have_many(:agent_tokens).class_name('Clusters::AgentToken').order(Clusters::AgentToken.arel_table[:last_used_at].desc.nulls_last) }
|
2021-08-23 05:10:23 -04:00
|
|
|
it { is_expected.to have_many(:group_authorizations).class_name('Clusters::Agents::GroupAuthorization') }
|
|
|
|
it { is_expected.to have_many(:authorized_groups).through(:group_authorizations) }
|
2021-09-16 08:09:35 -04:00
|
|
|
it { is_expected.to have_many(:project_authorizations).class_name('Clusters::Agents::ProjectAuthorization') }
|
|
|
|
it { is_expected.to have_many(:authorized_projects).through(:project_authorizations).class_name('::Project') }
|
2020-07-20 05:09:22 -04:00
|
|
|
|
|
|
|
it { is_expected.to validate_presence_of(:name) }
|
2020-07-23 02:09:19 -04:00
|
|
|
it { is_expected.to validate_length_of(:name).is_at_most(63) }
|
2020-07-20 05:09:22 -04:00
|
|
|
it { is_expected.to validate_uniqueness_of(:name).scoped_to(:project_id) }
|
2020-07-23 02:09:19 -04:00
|
|
|
|
2020-09-11 08:08:50 -04:00
|
|
|
describe 'scopes' do
|
2020-10-19 11:08:58 -04:00
|
|
|
describe '.ordered_by_name' do
|
|
|
|
let(:names) { %w(agent-d agent-b agent-a agent-c) }
|
|
|
|
|
|
|
|
subject { described_class.ordered_by_name }
|
|
|
|
|
|
|
|
before do
|
|
|
|
names.each do |name|
|
|
|
|
create(:cluster_agent, name: name)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it { expect(subject.map(&:name)).to eq(names.sort) }
|
|
|
|
end
|
|
|
|
|
2020-09-11 08:08:50 -04:00
|
|
|
describe '.with_name' do
|
|
|
|
let!(:matching_name) { create(:cluster_agent, name: 'matching-name') }
|
|
|
|
let!(:other_name) { create(:cluster_agent, name: 'other-name') }
|
|
|
|
|
|
|
|
subject { described_class.with_name(matching_name.name) }
|
|
|
|
|
|
|
|
it { is_expected.to contain_exactly(matching_name) }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-07-23 02:09:19 -04:00
|
|
|
describe 'validation' do
|
|
|
|
describe 'name validation' do
|
|
|
|
it 'rejects names that do not conform to RFC 1123', :aggregate_failures do
|
|
|
|
%w[Agent agentA agentAagain gent- -agent agent.a agent/a agent>a].each do |name|
|
|
|
|
agent = build(:cluster_agent, name: name)
|
|
|
|
|
|
|
|
expect(agent).not_to be_valid
|
|
|
|
expect(agent.errors[:name]).to eq(["can contain only lowercase letters, digits, and '-', but cannot start or end with '-'"])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'accepts valid names', :aggregate_failures do
|
|
|
|
%w[agent agent123 agent-123].each do |name|
|
|
|
|
agent = build(:cluster_agent, name: name)
|
|
|
|
|
|
|
|
expect(agent).to be_valid
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2020-11-27 07:09:14 -05:00
|
|
|
|
|
|
|
describe '#has_access_to?' do
|
|
|
|
let(:agent) { build(:cluster_agent) }
|
|
|
|
|
|
|
|
it 'has access to own project' do
|
|
|
|
expect(agent.has_access_to?(agent.project)).to be_truthy
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not have access to other projects' do
|
|
|
|
expect(agent.has_access_to?(create(:project))).to be_falsey
|
|
|
|
end
|
|
|
|
end
|
2021-12-08 07:13:04 -05:00
|
|
|
|
2021-12-21 22:15:20 -05:00
|
|
|
describe '#connected?' do
|
2021-12-08 07:13:04 -05:00
|
|
|
let_it_be(:agent) { create(:cluster_agent) }
|
|
|
|
|
|
|
|
let!(:token) { create(:cluster_agent_token, agent: agent, last_used_at: last_used_at) }
|
|
|
|
|
2021-12-21 22:15:20 -05:00
|
|
|
subject { agent.connected? }
|
2021-12-08 07:13:04 -05:00
|
|
|
|
|
|
|
context 'agent has never connected' do
|
|
|
|
let(:last_used_at) { nil }
|
|
|
|
|
|
|
|
it { is_expected.to be_falsey }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'agent has connected, but not recently' do
|
|
|
|
let(:last_used_at) { 2.hours.ago }
|
|
|
|
|
|
|
|
it { is_expected.to be_falsey }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'agent has connected recently' do
|
|
|
|
let(:last_used_at) { 2.minutes.ago }
|
|
|
|
|
|
|
|
it { is_expected.to be_truthy }
|
2021-12-21 22:15:20 -05:00
|
|
|
|
|
|
|
context 'agent token has been revoked' do
|
|
|
|
before do
|
|
|
|
token.revoked!
|
|
|
|
end
|
|
|
|
|
|
|
|
it { is_expected.to be_falsey }
|
|
|
|
end
|
2021-12-08 07:13:04 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'agent has multiple tokens' do
|
|
|
|
let!(:inactive_token) { create(:cluster_agent_token, agent: agent, last_used_at: 2.hours.ago) }
|
|
|
|
let(:last_used_at) { 2.minutes.ago }
|
|
|
|
|
|
|
|
it { is_expected.to be_truthy }
|
|
|
|
end
|
|
|
|
end
|
2022-01-14 04:14:36 -05:00
|
|
|
|
|
|
|
describe '#activity_event_deletion_cutoff' do
|
|
|
|
let_it_be(:agent) { create(:cluster_agent) }
|
|
|
|
let_it_be(:event1) { create(:agent_activity_event, agent: agent, recorded_at: 1.hour.ago) }
|
|
|
|
let_it_be(:event2) { create(:agent_activity_event, agent: agent, recorded_at: 2.hours.ago) }
|
|
|
|
let_it_be(:event3) { create(:agent_activity_event, agent: agent, recorded_at: 3.hours.ago) }
|
|
|
|
|
|
|
|
subject { agent.activity_event_deletion_cutoff }
|
|
|
|
|
|
|
|
before do
|
|
|
|
stub_const("#{described_class}::ACTIVITY_EVENT_LIMIT", 2)
|
|
|
|
end
|
|
|
|
|
|
|
|
it { is_expected.to be_like_time(event2.recorded_at) }
|
|
|
|
end
|
2020-07-20 05:09:22 -04:00
|
|
|
end
|