diff options
Diffstat (limited to 'spec/models/clusters')
-rw-r--r-- | spec/models/clusters/agent_token_spec.rb | 49 | ||||
-rw-r--r-- | spec/models/clusters/applications/prometheus_spec.rb | 94 | ||||
-rw-r--r-- | spec/models/clusters/cluster_spec.rb | 76 | ||||
-rw-r--r-- | spec/models/clusters/integrations/prometheus_spec.rb | 57 |
4 files changed, 196 insertions, 80 deletions
diff --git a/spec/models/clusters/agent_token_spec.rb b/spec/models/clusters/agent_token_spec.rb index a1b45df1970..680b351d24a 100644 --- a/spec/models/clusters/agent_token_spec.rb +++ b/spec/models/clusters/agent_token_spec.rb @@ -24,4 +24,53 @@ RSpec.describe Clusters::AgentToken do expect(agent_token.token.length).to be >= 50 end end + + describe '#track_usage', :clean_gitlab_redis_cache do + let(:agent_token) { create(:cluster_agent_token) } + + subject { agent_token.track_usage } + + 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 + end + + def expect_redis_update + Gitlab::Redis::Cache.with do |redis| + redis_key = "cache:#{described_class.name}:#{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/models/clusters/applications/prometheus_spec.rb b/spec/models/clusters/applications/prometheus_spec.rb index 032de6aa7c2..5a0ccabd467 100644 --- a/spec/models/clusters/applications/prometheus_spec.rb +++ b/spec/models/clusters/applications/prometheus_spec.rb @@ -39,6 +39,19 @@ RSpec.describe Clusters::Applications::Prometheus do end end + describe 'transition to externally_installed' do + let(:project) { create(:project) } + 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') + + application.make_externally_installed! + end + end + describe 'transition to updating' do let(:project) { create(:project) } let(:cluster) { create(:cluster, projects: [project]) } @@ -61,85 +74,8 @@ RSpec.describe Clusters::Applications::Prometheus do end describe '#prometheus_client' do - shared_examples 'exception caught for prometheus client' do - before do - allow(kube_client).to receive(:proxy_url).and_raise(exception) - end - - it 'returns nil' do - expect(subject.prometheus_client).to be_nil - end - end - - context 'cluster is nil' do - it 'returns nil' do - expect(subject.cluster).to be_nil - expect(subject.prometheus_client).to be_nil - end - end - - context "cluster doesn't have kubeclient" do - let(:cluster) { create(:cluster) } - - subject { create(:clusters_applications_prometheus, cluster: cluster) } - - it 'returns nil' do - expect(subject.prometheus_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_prometheus, 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 prometheus_client' do - expect(subject.prometheus_client).to be_instance_of(Gitlab::PrometheusClient) - end - - it 'merges proxy_url, options and headers from kube client with prometheus_client options' do - expect(Gitlab::PrometheusClient) - .to(receive(:new)) - .with(a_valid_url, kube_client.rest_client.options.merge({ - headers: kube_client.headers, - timeout: PrometheusAdapter::DEFAULT_PROMETHEUS_REQUEST_TIMEOUT_SEC - })) - subject.prometheus_client - end - - context 'when cluster is not reachable' do - it_behaves_like 'exception caught for prometheus client' do - let(:exception) { Kubeclient::HttpError.new(401, 'Unauthorized', nil) } - end - end - - context 'when there is a socket error while contacting cluster' do - it_behaves_like 'exception caught for prometheus client' do - let(:exception) { Errno::ECONNREFUSED } - end - - it_behaves_like 'exception caught for prometheus client' do - let(:exception) { Errno::ECONNRESET } - end - end - - context 'when the network is unreachable' do - it_behaves_like 'exception caught for prometheus client' do - let(:exception) { Errno::ENETUNREACH } - end - end + include_examples '#prometheus_client shared' do + let(:factory) { :clusters_applications_prometheus } end end diff --git a/spec/models/clusters/cluster_spec.rb b/spec/models/clusters/cluster_spec.rb index a8f81cba285..b2ed64fd9b0 100644 --- a/spec/models/clusters/cluster_spec.rb +++ b/spec/models/clusters/cluster_spec.rb @@ -21,6 +21,7 @@ RSpec.describe Clusters::Cluster, :use_clean_rails_memory_store_caching do it { is_expected.to have_one(:provider_gcp) } it { is_expected.to have_one(:provider_aws) } it { is_expected.to have_one(:platform_kubernetes) } + it { is_expected.to have_one(:integration_prometheus) } it { is_expected.to have_one(:application_helm) } it { is_expected.to have_one(:application_ingress) } it { is_expected.to have_one(:application_prometheus) } @@ -40,7 +41,6 @@ RSpec.describe Clusters::Cluster, :use_clean_rails_memory_store_caching do it { is_expected.to delegate_method(:rbac?).to(:platform_kubernetes).with_prefix } it { is_expected.to delegate_method(:available?).to(:application_helm).with_prefix } it { is_expected.to delegate_method(:available?).to(:application_ingress).with_prefix } - it { is_expected.to delegate_method(:available?).to(:application_prometheus).with_prefix } it { is_expected.to delegate_method(:available?).to(:application_knative).with_prefix } it { is_expected.to delegate_method(:available?).to(:application_elastic_stack).with_prefix } it { is_expected.to delegate_method(:external_ip).to(:application_ingress).with_prefix } @@ -1349,6 +1349,80 @@ RSpec.describe Clusters::Cluster, :use_clean_rails_memory_store_caching do end end + describe '#application_prometheus_available?' do + let_it_be_with_reload(:cluster) { create(:cluster, :project) } + + subject { cluster.application_prometheus_available? } + + it { is_expected.to be_falsey } + + context 'has a integration_prometheus' do + let_it_be(:integration) { create(:clusters_integrations_prometheus, cluster: cluster) } + + it { is_expected.to be_truthy } + + context 'disabled' do + before do + cluster.integration_prometheus.enabled = false + end + + it { is_expected.to be_falsey } + end + end + + context 'has a application_prometheus' do + let_it_be(:application) { create(:clusters_applications_prometheus, :installed, :no_helm_installed, cluster: cluster) } + + it { is_expected.to be_truthy } + + context 'errored' do + before do + cluster.application_prometheus.status = Clusters::Applications::Prometheus.state_machines[:status].states[:errored] + end + + it { is_expected.to be_falsey } + end + + context 'also has a integration_prometheus' do + let_it_be(:integration) { create(:clusters_integrations_prometheus, cluster: cluster) } + + it { is_expected.to be_truthy } + end + end + end + + describe '#prometheus_adapter' do + let_it_be_with_reload(:cluster) { create(:cluster, :project) } + + it 'returns nothing' do + expect(cluster.prometheus_adapter).to be_nil + end + + context 'has a integration_prometheus' do + let_it_be(:integration) { create(:clusters_integrations_prometheus, cluster: cluster) } + + it 'returns the integration' do + expect(cluster.prometheus_adapter).to eq(integration) + end + end + + context 'has a application_prometheus' do + let_it_be(:application) { create(:clusters_applications_prometheus, :no_helm_installed, cluster: cluster) } + + it 'returns the application' do + expect(cluster.prometheus_adapter).to eq(application) + end + + context 'also has a integration_prometheus' do + let_it_be(:integration) { create(:clusters_integrations_prometheus, cluster: cluster) } + + it 'returns the integration' do + expect(cluster.prometheus_adapter).to eq(integration) + end + end + end + end + describe '#delete_cached_resources!' do let!(:cluster) { create(:cluster, :project) } let!(:staging_namespace) { create(:cluster_kubernetes_namespace, cluster: cluster, namespace: 'staging') } diff --git a/spec/models/clusters/integrations/prometheus_spec.rb b/spec/models/clusters/integrations/prometheus_spec.rb new file mode 100644 index 00000000000..a7be1673ce2 --- /dev/null +++ b/spec/models/clusters/integrations/prometheus_spec.rb @@ -0,0 +1,57 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe Clusters::Integrations::Prometheus 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 + + describe '#prometheus_client' do + include_examples '#prometheus_client shared' do + let(:factory) { :clusters_integrations_prometheus } + end + end + + describe '#configured?' do + let(:prometheus) { create(:clusters_integrations_prometheus, cluster: cluster) } + + subject { prometheus.configured? } + + context 'when a kubenetes client is present' do + let(:cluster) { create(:cluster, :project, :provided_by_gcp) } + + it { is_expected.to be_truthy } + + context 'when it is disabled' do + let(:prometheus) { create(:clusters_integrations_prometheus, :disabled, cluster: cluster) } + + it { is_expected.to be_falsey } + end + + context 'when the kubernetes URL is blocked' do + before do + blocked_ip = '127.0.0.1' # localhost addresses are blocked by default + + stub_all_dns(cluster.platform.api_url, ip_address: blocked_ip) + end + + it { is_expected.to be_falsey } + end + end + + context 'when a kubenetes client is not present' do + let(:cluster) { create(:cluster) } + + it { is_expected.to be_falsy } + end + end +end |