diff options
Diffstat (limited to 'spec/models/clusters')
-rw-r--r-- | spec/models/clusters/agent_spec.rb | 1 | ||||
-rw-r--r-- | spec/models/clusters/agent_token_spec.rb | 13 | ||||
-rw-r--r-- | spec/models/clusters/applications/elastic_stack_spec.rb | 110 | ||||
-rw-r--r-- | spec/models/clusters/applications/prometheus_spec.rb | 87 | ||||
-rw-r--r-- | spec/models/clusters/integrations/elastic_stack_spec.rb | 19 | ||||
-rw-r--r-- | spec/models/clusters/integrations/prometheus_spec.rb | 56 |
6 files changed, 160 insertions, 126 deletions
diff --git a/spec/models/clusters/agent_spec.rb b/spec/models/clusters/agent_spec.rb index a85a72eba0b..ea7a55480a8 100644 --- a/spec/models/clusters/agent_spec.rb +++ b/spec/models/clusters/agent_spec.rb @@ -8,6 +8,7 @@ RSpec.describe Clusters::Agent do it { is_expected.to belong_to(:created_by_user).class_name('User').optional } it { is_expected.to belong_to(:project).class_name('::Project') } it { is_expected.to have_many(:agent_tokens).class_name('Clusters::AgentToken') } + it { is_expected.to have_many(:last_used_agent_tokens).class_name('Clusters::AgentToken') } it { is_expected.to validate_presence_of(:name) } it { is_expected.to validate_length_of(:name).is_at_most(63) } diff --git a/spec/models/clusters/agent_token_spec.rb b/spec/models/clusters/agent_token_spec.rb index 680b351d24a..bde4798abec 100644 --- a/spec/models/clusters/agent_token_spec.rb +++ b/spec/models/clusters/agent_token_spec.rb @@ -9,6 +9,19 @@ RSpec.describe Clusters::AgentToken do it { is_expected.to validate_length_of(:name).is_at_most(255) } it { is_expected.to validate_presence_of(:name) } + describe 'scopes' do + describe '.order_last_used_at_desc' do + let_it_be(:token_1) { create(:cluster_agent_token, last_used_at: 7.days.ago) } + let_it_be(:token_2) { create(:cluster_agent_token, last_used_at: nil) } + let_it_be(:token_3) { create(:cluster_agent_token, last_used_at: 2.days.ago) } + + it 'sorts by last_used_at descending, with null values at last' do + expect(described_class.order_last_used_at_desc) + .to eq([token_3, token_1, token_2]) + end + end + end + describe '#token' do it 'is generated on save' do agent_token = build(:cluster_agent_token, token_encrypted: nil) diff --git a/spec/models/clusters/applications/elastic_stack_spec.rb b/spec/models/clusters/applications/elastic_stack_spec.rb index 74cacd486b0..af2802d5e47 100644 --- a/spec/models/clusters/applications/elastic_stack_spec.rb +++ b/spec/models/clusters/applications/elastic_stack_spec.rb @@ -10,6 +10,41 @@ RSpec.describe Clusters::Applications::ElasticStack do include_examples 'cluster application version specs', :clusters_applications_elastic_stack include_examples 'cluster application helm specs', :clusters_applications_elastic_stack + describe 'cluster.integration_elastic_stack state synchronization' do + let!(:application) { create(:clusters_applications_elastic_stack) } + let(:cluster) { application.cluster } + let(:integration) { cluster.integration_elastic_stack } + + describe 'after_destroy' do + it 'disables the corresponding integration' do + application.destroy! + + expect(integration).not_to be_enabled + end + end + + describe 'on install' do + it 'enables the corresponding integration' do + application.make_scheduled! + application.make_installing! + application.make_installed! + + expect(integration).to be_enabled + end + end + + describe 'on uninstall' do + it 'disables the corresponding integration' do + application.make_scheduled! + application.make_installing! + application.make_installed! + application.make_externally_uninstalled! + + expect(integration).not_to be_enabled + end + end + end + describe '#install_command' do let!(:elastic_stack) { create(:clusters_applications_elastic_stack) } @@ -138,78 +173,5 @@ RSpec.describe Clusters::Applications::ElasticStack do end end - describe '#elasticsearch_client' do - context 'cluster is nil' do - it 'returns nil' do - expect(subject.cluster).to be_nil - expect(subject.elasticsearch_client).to be_nil - end - end - - context "cluster doesn't have kubeclient" do - let(:cluster) { create(:cluster) } - - subject { create(:clusters_applications_elastic_stack, cluster: cluster) } - - it 'returns nil' do - expect(subject.elasticsearch_client).to be_nil - end - end - - context 'cluster has kubeclient' do - let(:cluster) { create(:cluster, :project, :provided_by_gcp) } - let(:kubernetes_url) { subject.cluster.platform_kubernetes.api_url } - let(:kube_client) { subject.cluster.kubeclient.core_client } - - subject { create(:clusters_applications_elastic_stack, cluster: cluster) } - - before do - subject.cluster.platform_kubernetes.namespace = 'a-namespace' - stub_kubeclient_discover(cluster.platform_kubernetes.api_url) - - create(:cluster_kubernetes_namespace, - cluster: cluster, - cluster_project: cluster.cluster_project, - project: cluster.cluster_project.project) - end - - it 'creates proxy elasticsearch_client' do - expect(subject.elasticsearch_client).to be_instance_of(Elasticsearch::Transport::Client) - end - - it 'copies proxy_url, options and headers from kube client to elasticsearch_client' do - expect(Elasticsearch::Client) - .to(receive(:new)) - .with(url: a_valid_url) - .and_call_original - - client = subject.elasticsearch_client - faraday_connection = client.transport.connections.first.connection - - expect(faraday_connection.headers["Authorization"]).to eq(kube_client.headers[:Authorization]) - expect(faraday_connection.ssl.cert_store).to be_instance_of(OpenSSL::X509::Store) - expect(faraday_connection.ssl.verify).to eq(1) - expect(faraday_connection.options.timeout).to be_nil - end - - context 'when cluster is not reachable' do - before do - allow(kube_client).to receive(:proxy_url).and_raise(Kubeclient::HttpError.new(401, 'Unauthorized', nil)) - end - - it 'returns nil' do - expect(subject.elasticsearch_client).to be_nil - end - end - - context 'when timeout is provided' do - it 'sets timeout in elasticsearch_client' do - client = subject.elasticsearch_client(timeout: 123) - faraday_connection = client.transport.connections.first.connection - - expect(faraday_connection.options.timeout).to eq(123) - end - end - end - end + it_behaves_like 'cluster-based #elasticsearch_client', :clusters_applications_elastic_stack end diff --git a/spec/models/clusters/applications/prometheus_spec.rb b/spec/models/clusters/applications/prometheus_spec.rb index 5a0ccabd467..549a273e2d7 100644 --- a/spec/models/clusters/applications/prometheus_spec.rb +++ b/spec/models/clusters/applications/prometheus_spec.rb @@ -13,16 +13,13 @@ RSpec.describe Clusters::Applications::Prometheus do include_examples 'cluster application initial status specs' describe 'after_destroy' do - context 'cluster type is project' do - let(:cluster) { create(:cluster, :with_installed_helm) } - let(:application) { create(:clusters_applications_prometheus, :installed, cluster: cluster) } + let(:cluster) { create(:cluster, :with_installed_helm) } + let(:application) { create(:clusters_applications_prometheus, :installed, cluster: cluster) } - it 'deactivates prometheus_service after destroy' do - expect(Clusters::Applications::DeactivateServiceWorker) - .to receive(:perform_async).with(cluster.id, 'prometheus') + it 'disables the corresponding integration' do + application.destroy! - application.destroy! - end + expect(cluster.integration_prometheus).not_to be_enabled end end @@ -31,11 +28,10 @@ RSpec.describe Clusters::Applications::Prometheus do let(:cluster) { create(:cluster, :with_installed_helm) } let(:application) { create(:clusters_applications_prometheus, :installing, cluster: cluster) } - it 'schedules post installation job' do - expect(Clusters::Applications::ActivateServiceWorker) - .to receive(:perform_async).with(cluster.id, 'prometheus') - + it 'enables the corresponding integration' do application.make_installed + + expect(cluster.integration_prometheus).to be_enabled end end @@ -44,11 +40,10 @@ RSpec.describe Clusters::Applications::Prometheus do let(:cluster) { create(:cluster, :with_installed_helm) } let(:application) { create(:clusters_applications_prometheus, :installing, cluster: cluster) } - it 'schedules post installation job' do - expect(Clusters::Applications::ActivateServiceWorker) - .to receive(:perform_async).with(cluster.id, 'prometheus') - + it 'enables the corresponding integration' do application.make_externally_installed! + + expect(cluster.integration_prometheus).to be_enabled end end @@ -65,6 +60,26 @@ RSpec.describe Clusters::Applications::Prometheus do end end + describe '#managed_prometheus?' do + subject { prometheus.managed_prometheus? } + + let(:prometheus) { build(:clusters_applications_prometheus) } + + it { is_expected.to be_truthy } + + context 'externally installed' do + let(:prometheus) { build(:clusters_applications_prometheus, :externally_installed) } + + it { is_expected.to be_falsey } + end + + context 'uninstalled' do + let(:prometheus) { build(:clusters_applications_prometheus, :uninstalled) } + + it { is_expected.to be_falsey } + end + end + describe '#can_uninstall?' do let(:prometheus) { create(:clusters_applications_prometheus) } @@ -318,42 +333,10 @@ RSpec.describe Clusters::Applications::Prometheus do describe 'alert manager token' do subject { create(:clusters_applications_prometheus) } - context 'when not set' do - it 'is empty by default' do - expect(subject.alert_manager_token).to be_nil - expect(subject.encrypted_alert_manager_token).to be_nil - expect(subject.encrypted_alert_manager_token_iv).to be_nil - end - - describe '#generate_alert_manager_token!' do - it 'generates a token' do - subject.generate_alert_manager_token! - - expect(subject.alert_manager_token).to match(/\A\h{32}\z/) - end - end - end - - context 'when set' do - let(:token) { SecureRandom.hex } - - before do - subject.update!(alert_manager_token: token) - end - - it 'reads the token' do - expect(subject.alert_manager_token).to eq(token) - expect(subject.encrypted_alert_manager_token).not_to be_nil - expect(subject.encrypted_alert_manager_token_iv).not_to be_nil - end - - describe '#generate_alert_manager_token!' do - it 'does not re-generate the token' do - subject.generate_alert_manager_token! - - expect(subject.alert_manager_token).to eq(token) - end - end + it 'is autogenerated on creation' do + expect(subject.alert_manager_token).to match(/\A\h{32}\z/) + expect(subject.encrypted_alert_manager_token).not_to be_nil + expect(subject.encrypted_alert_manager_token_iv).not_to be_nil end end end diff --git a/spec/models/clusters/integrations/elastic_stack_spec.rb b/spec/models/clusters/integrations/elastic_stack_spec.rb new file mode 100644 index 00000000000..be4d59b52a2 --- /dev/null +++ b/spec/models/clusters/integrations/elastic_stack_spec.rb @@ -0,0 +1,19 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe Clusters::Integrations::ElasticStack do + include KubernetesHelpers + include StubRequests + + describe 'associations' do + it { is_expected.to belong_to(:cluster).class_name('Clusters::Cluster') } + end + + describe 'validations' do + it { is_expected.to validate_presence_of(:cluster) } + it { is_expected.not_to allow_value(nil).for(:enabled) } + end + + it_behaves_like 'cluster-based #elasticsearch_client', :clusters_integrations_elastic_stack +end diff --git a/spec/models/clusters/integrations/prometheus_spec.rb b/spec/models/clusters/integrations/prometheus_spec.rb index a7be1673ce2..680786189ad 100644 --- a/spec/models/clusters/integrations/prometheus_spec.rb +++ b/spec/models/clusters/integrations/prometheus_spec.rb @@ -15,6 +15,62 @@ RSpec.describe Clusters::Integrations::Prometheus do it { is_expected.not_to allow_value(nil).for(:enabled) } end + describe 'after_destroy' do + subject(:integration) { create(:clusters_integrations_prometheus, cluster: cluster, enabled: true) } + + let(:cluster) { create(:cluster, :with_installed_helm) } + + it 'deactivates prometheus_service' do + expect(Clusters::Applications::DeactivateServiceWorker) + .to receive(:perform_async).with(cluster.id, 'prometheus') + + integration.destroy! + end + end + + describe 'after_save' do + subject(:integration) { create(:clusters_integrations_prometheus, cluster: cluster, enabled: enabled) } + + let(:cluster) { create(:cluster, :with_installed_helm) } + let(:enabled) { true } + + context 'when no change to enabled status' do + it 'does not touch project services' do + integration # ensure integration exists before we set the expectations + + expect(Clusters::Applications::DeactivateServiceWorker) + .not_to receive(:perform_async) + + expect(Clusters::Applications::ActivateServiceWorker) + .not_to receive(:perform_async) + + integration.update!(enabled: enabled) + end + end + + context 'when enabling' do + let(:enabled) { false } + + it 'deactivates prometheus_service' do + expect(Clusters::Applications::ActivateServiceWorker) + .to receive(:perform_async).with(cluster.id, 'prometheus') + + integration.update!(enabled: true) + end + end + + context 'when disabling' do + let(:enabled) { true } + + it 'activates prometheus_service' do + expect(Clusters::Applications::DeactivateServiceWorker) + .to receive(:perform_async).with(cluster.id, 'prometheus') + + integration.update!(enabled: false) + end + end + end + describe '#prometheus_client' do include_examples '#prometheus_client shared' do let(:factory) { :clusters_integrations_prometheus } |