summaryrefslogtreecommitdiff
path: root/spec/finders/clusters/knative_services_finder_spec.rb
blob: 159724b3c1f79c7d63435bf31e271b842f5e8a48 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# frozen_string_literal: true

require 'spec_helper'

describe Clusters::KnativeServicesFinder do
  include KubernetesHelpers
  include ReactiveCachingHelpers

  let(:cluster) { create(:cluster, :project, :provided_by_gcp) }
  let(:service) { environment.deployment_platform }
  let(:project) { cluster.cluster_project.project }
  let(:environment) { create(:environment, project: project) }
  let!(:deployment) { create(:deployment, :success, environment: environment, cluster: cluster) }
  let(:namespace) do
    create(:cluster_kubernetes_namespace,
      cluster: cluster,
      project: project,
      environment: environment)
  end

  let(:finder) { described_class.new(cluster, environment) }

  before do
    stub_kubeclient_knative_services(namespace: namespace.namespace)
    stub_kubeclient_service_pods(
      kube_response(
        kube_knative_pods_body(
          project.name, namespace.namespace
        )
      ),
      namespace: namespace.namespace
    )
  end

  shared_examples 'a cached data' do
    it 'has an unintialized cache' do
      is_expected.to be_blank
    end

    context 'when using synchronous reactive cache' do
      before do
        synchronous_reactive_cache(finder)
      end

      context 'when there are functions for cluster namespace' do
        it { is_expected.not_to be_blank }
      end

      context 'when there are no functions for cluster namespace' do
        before do
          stub_kubeclient_knative_services(
            namespace: namespace.namespace,
            response: kube_response({ "kind" => "ServiceList", "items" => [] })
          )
          stub_kubeclient_service_pods(
            kube_response({ "kind" => "PodList", "items" => [] }),
            namespace: namespace.namespace
          )
        end

        it { is_expected.to be_blank }
      end
    end
  end

  describe '#service_pod_details' do
    subject { finder.service_pod_details(project.name) }

    it_behaves_like 'a cached data'
  end

  describe '#services' do
    subject { finder.services }

    it_behaves_like 'a cached data'
  end

  describe '#knative_detected' do
    subject { finder.knative_detected }
    before do
      synchronous_reactive_cache(finder)
    end

    context 'when knative is installed' do
      before do
        stub_kubeclient_discover(service.api_url)
      end

      it { is_expected.to be_truthy }
      it "discovers knative installation" do
        expect { subject }
          .to change { finder.cluster.kubeclient.knative_client.discovered }
          .from(false)
          .to(true)
      end
    end

    context 'when knative is not installed' do
      before do
        stub_kubeclient_discover_knative_not_found(service.api_url)
      end

      it { is_expected.to be_falsy }
      it "does not discover knative installation" do
        expect { subject }.not_to change { cluster.kubeclient.knative_client.discovered }
      end
    end
  end
end