diff options
Diffstat (limited to 'spec/services/clusters')
4 files changed, 165 insertions, 1 deletions
diff --git a/spec/services/clusters/agent_tokens/track_usage_service_spec.rb b/spec/services/clusters/agent_tokens/track_usage_service_spec.rb new file mode 100644 index 00000000000..3350b15a5ce --- /dev/null +++ b/spec/services/clusters/agent_tokens/track_usage_service_spec.rb @@ -0,0 +1,84 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe Clusters::AgentTokens::TrackUsageService do + let_it_be(:agent) { create(:cluster_agent) } + + describe '#execute', :clean_gitlab_redis_cache do + let(:agent_token) { create(:cluster_agent_token, agent: agent) } + + subject { described_class.new(agent_token).execute } + + context 'when last_used_at was updated recently' do + before do + agent_token.update!(last_used_at: 10.minutes.ago) + end + + it 'updates cache but not database' do + expect { subject }.not_to change { agent_token.reload.read_attribute(:last_used_at) } + + expect_redis_update + end + end + + context 'when last_used_at was not updated recently' do + it 'updates cache and database' do + does_db_update + expect_redis_update + end + + context 'with invalid token' do + before do + agent_token.description = SecureRandom.hex(2000) + end + + it 'still updates caches and database' do + expect(agent_token).to be_invalid + + does_db_update + expect_redis_update + end + end + + context 'agent is not connected' do + before do + allow(agent).to receive(:connected?).and_return(false) + end + + it 'creates an activity event' do + expect { subject }.to change { agent.activity_events.count } + + event = agent.activity_events.last + expect(event).to have_attributes( + kind: 'agent_connected', + level: 'info', + recorded_at: agent_token.reload.read_attribute(:last_used_at), + agent_token: agent_token + ) + end + end + + context 'agent is connected' do + before do + allow(agent).to receive(:connected?).and_return(true) + end + + it 'does not create an activity event' do + expect { subject }.not_to change { agent.activity_events.count } + end + end + end + + def expect_redis_update + Gitlab::Redis::Cache.with do |redis| + redis_key = "cache:#{agent_token.class}:#{agent_token.id}:attributes" + expect(redis.get(redis_key)).to be_present + end + end + + def does_db_update + expect { subject }.to change { agent_token.reload.read_attribute(:last_used_at) } + end + end +end diff --git a/spec/services/clusters/agents/create_activity_event_service_spec.rb b/spec/services/clusters/agents/create_activity_event_service_spec.rb new file mode 100644 index 00000000000..7a8f0e16d60 --- /dev/null +++ b/spec/services/clusters/agents/create_activity_event_service_spec.rb @@ -0,0 +1,44 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe Clusters::Agents::CreateActivityEventService do + let_it_be(:agent) { create(:cluster_agent) } + let_it_be(:token) { create(:cluster_agent_token, agent: agent) } + let_it_be(:user) { create(:user) } + + describe '#execute' do + let(:params) do + { + kind: :token_created, + level: :info, + recorded_at: token.created_at, + user: user, + agent_token: token + } + end + + subject { described_class.new(agent, **params).execute } + + it 'creates an activity event record' do + expect { subject }.to change(agent.activity_events, :count).from(0).to(1) + + event = agent.activity_events.last + + expect(event).to have_attributes( + kind: 'token_created', + level: 'info', + recorded_at: token.reload.created_at, + user: user, + agent_token_id: token.id + ) + end + + it 'schedules the cleanup worker' do + expect(Clusters::Agents::DeleteExpiredEventsWorker).to receive(:perform_at) + .with(1.hour.from_now.change(min: agent.id % 60), agent.id) + + subject + end + end +end diff --git a/spec/services/clusters/agents/delete_expired_events_service_spec.rb b/spec/services/clusters/agents/delete_expired_events_service_spec.rb new file mode 100644 index 00000000000..3dc166f54eb --- /dev/null +++ b/spec/services/clusters/agents/delete_expired_events_service_spec.rb @@ -0,0 +1,36 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe Clusters::Agents::DeleteExpiredEventsService do + let_it_be(:agent) { create(:cluster_agent) } + + describe '#execute' do + 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) } + let_it_be(:event4) { create(:agent_activity_event, agent: agent, recorded_at: 4.hours.ago) } + let_it_be(:event5) { create(:agent_activity_event, agent: agent, recorded_at: 5.hours.ago) } + + let(:deletion_cutoff) { 1.day.ago } + + subject { described_class.new(agent).execute } + + before do + allow(agent).to receive(:activity_event_deletion_cutoff).and_return(deletion_cutoff) + end + + it 'does not delete events if the limit has not been reached' do + expect { subject }.not_to change(agent.activity_events, :count) + end + + context 'there are more events than the limit' do + let(:deletion_cutoff) { event3.recorded_at } + + it 'removes events to remain at the limit, keeping the most recent' do + expect { subject }.to change(agent.activity_events, :count).from(5).to(3) + expect(agent.activity_events).to contain_exactly(event1, event2, event3) + end + end + end +end diff --git a/spec/services/clusters/integrations/create_service_spec.rb b/spec/services/clusters/integrations/create_service_spec.rb index 14653236ab1..6dac97ebf8f 100644 --- a/spec/services/clusters/integrations/create_service_spec.rb +++ b/spec/services/clusters/integrations/create_service_spec.rb @@ -7,7 +7,7 @@ RSpec.describe Clusters::Integrations::CreateService, '#execute' do let_it_be_with_reload(:cluster) { create(:cluster, :provided_by_gcp, projects: [project]) } let(:service) do - described_class.new(container: project, cluster: cluster, current_user: project.owner, params: params) + described_class.new(container: project, cluster: cluster, current_user: project.first_owner, params: params) end shared_examples_for 'a cluster integration' do |application_type| |