summaryrefslogtreecommitdiff
path: root/spec/models/gcp/cluster_spec.rb
blob: 350fbc257d9e76efde001d799f0c9c7319df1d04 (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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
require 'spec_helper'

describe Gcp::Cluster do
  it { is_expected.to belong_to(:project) }
  it { is_expected.to belong_to(:user) }
  it { is_expected.to belong_to(:service) }

  it { is_expected.to validate_presence_of(:gcp_cluster_zone) }

  describe '#default_value_for' do
    let(:cluster) { described_class.new }

    it { expect(cluster.gcp_cluster_zone).to eq('us-central1-a') }
    it { expect(cluster.gcp_cluster_size).to eq(3) }
    it { expect(cluster.gcp_machine_type).to eq('n1-standard-4') }
  end

  describe '#validates' do
    subject { cluster.valid? }

    context 'when validates gcp_project_id' do
      let(:cluster) { build(:gcp_cluster, gcp_project_id: gcp_project_id) }

      context 'when valid' do
        let(:gcp_project_id) { 'gcp-project-12345' }

        it { is_expected.to be_truthy }
      end

      context 'when empty' do
        let(:gcp_project_id) { '' }

        it { is_expected.to be_falsey }
      end

      context 'when too long' do
        let(:gcp_project_id) { 'A' * 64 }

        it { is_expected.to be_falsey }
      end

      context 'when includes abnormal character' do
        let(:gcp_project_id) { '!!!!!!' }

        it { is_expected.to be_falsey }
      end
    end

    context 'when validates gcp_cluster_name' do
      let(:cluster) { build(:gcp_cluster, gcp_cluster_name: gcp_cluster_name) }

      context 'when valid' do
        let(:gcp_cluster_name) { 'test-cluster' }

        it { is_expected.to be_truthy }
      end

      context 'when empty' do
        let(:gcp_cluster_name) { '' }

        it { is_expected.to be_falsey }
      end

      context 'when too long' do
        let(:gcp_cluster_name) { 'A' * 64 }

        it { is_expected.to be_falsey }
      end

      context 'when includes abnormal character' do
        let(:gcp_cluster_name) { '!!!!!!' }

        it { is_expected.to be_falsey }
      end
    end

    context 'when validates gcp_cluster_size' do
      let(:cluster) { build(:gcp_cluster, gcp_cluster_size: gcp_cluster_size) }

      context 'when valid' do
        let(:gcp_cluster_size) { 1 }

        it { is_expected.to be_truthy }
      end

      context 'when zero' do
        let(:gcp_cluster_size) { 0 }

        it { is_expected.to be_falsey }
      end
    end

    context 'when validates project_namespace' do
      let(:cluster) { build(:gcp_cluster, project_namespace: project_namespace) }

      context 'when valid' do
        let(:project_namespace) { 'default-namespace' }

        it { is_expected.to be_truthy }
      end

      context 'when empty' do
        let(:project_namespace) { '' }

        it { is_expected.to be_truthy }
      end

      context 'when too long' do
        let(:project_namespace) { 'A' * 64 }

        it { is_expected.to be_falsey }
      end

      context 'when includes abnormal character' do
        let(:project_namespace) { '!!!!!!' }

        it { is_expected.to be_falsey }
      end
    end

    context 'when validates restrict_modification' do
      let(:cluster) { create(:gcp_cluster) }

      before do
        cluster.make_creating!
      end

      context 'when created' do
        before do
          cluster.make_created!
        end

        it { is_expected.to be_truthy }
      end

      context 'when creating' do
        it { is_expected.to be_falsey }
      end
    end
  end

  describe '#state_machine' do
    let(:cluster) { build(:gcp_cluster) }

    context 'when transits to created state' do
      before do
        cluster.gcp_token = 'tmp'
        cluster.gcp_operation_id = 'tmp'
        cluster.make_created!
      end

      it 'nullify gcp_token and gcp_operation_id' do
        expect(cluster.gcp_token).to be_nil
        expect(cluster.gcp_operation_id).to be_nil
        expect(cluster).to be_created
      end
    end

    context 'when transits to errored state' do
      let(:reason) { 'something wrong' }

      before do
        cluster.make_errored!(reason)
      end

      it 'sets status_reason' do
        expect(cluster.status_reason).to eq(reason)
        expect(cluster).to be_errored
      end
    end
  end

  describe '#project_namespace_placeholder' do
    subject { cluster.project_namespace_placeholder }

    let(:cluster) { create(:gcp_cluster) }

    it 'returns a placeholder' do
      is_expected.to eq("#{cluster.project.path}-#{cluster.project.id}")
    end
  end

  describe '#on_creation?' do
    subject { cluster.on_creation? }

    let(:cluster) { create(:gcp_cluster) }

    context 'when status is creating' do
      before do
        cluster.make_creating!
      end

      it { is_expected.to be_truthy }
    end

    context 'when status is created' do
      before do
        cluster.make_created!
      end

      it { is_expected.to be_falsey }
    end
  end

  describe '#api_url' do
    subject { cluster.api_url }

    let(:cluster) { create(:gcp_cluster, :created_on_gke) }
    let(:api_url) { 'https://' + cluster.endpoint }

    it { is_expected.to eq(api_url) }
  end

  describe '#restrict_modification' do
    subject { cluster.restrict_modification }

    let(:cluster) { create(:gcp_cluster) }

    context 'when status is created' do
      before do
        cluster.make_created!
      end

      it { is_expected.to be_truthy }
    end

    context 'when status is creating' do
      before do
        cluster.make_creating!
      end

      it { is_expected.to be_falsey }

      it 'sets error' do
        is_expected.to be_falsey
        expect(cluster.errors).not_to be_empty
      end
    end
  end
end