|
1 | 1 | # frozen_string_literal: true |
2 | 2 |
|
3 | 3 | # Cloud Foundry Java Buildpack |
4 | | -# Copyright 2013-2020 the original author or authors. |
| 4 | +# Copyright 2013-2024 the original author or authors. |
5 | 5 | # |
6 | 6 | # Licensed under the Apache License, Version 2.0 (the "License"); |
7 | 7 | # you may not use this file except in compliance with the License. |
|
41 | 41 | before do |
42 | 42 | allow(services).to receive(:one_service?).with(/sealights/, 'token').and_return(true) |
43 | 43 | allow(services).to receive(:find_service).and_return('credentials' => credentials) |
| 44 | + uri = 'https://test-apiurl/getcustomagent/agent.zip' |
| 45 | + p = Pathname.new('spec/fixtures/stub-sealights-custom-agent.zip') |
| 46 | + allow(application_cache).to receive(:get).with(uri).and_yield(p.open, false) |
44 | 47 | end |
45 | 48 |
|
46 | 49 | it 'detects with sealights service' do |
47 | 50 | expect(component.detect).to eq("sealights-agent=#{version}") |
48 | 51 | end |
49 | 52 |
|
50 | 53 | context do |
51 | | - it 'updates JAVA_OPTS sl.tags' do |
| 54 | + it 'updates JAVA_OPTS sl.tags with buildpack version number' do |
| 55 | + allow_any_instance_of(JavaBuildpack::BuildpackVersion) |
| 56 | + .to receive(:to_hash).and_return({ 'version' => '1234', |
| 57 | + 'offline' => false, |
| 58 | + 'remote' => 'test-remote', |
| 59 | + 'hash' => 'test-hash' }) |
52 | 60 | component.release |
53 | 61 |
|
54 | | - expect(java_opts).to include('-Dsl.tags=pivotal_cloud_foundry') |
| 62 | + expect(java_opts).to include('-Dsl.tags=sl-pcf-1234') |
| 63 | + end |
| 64 | + |
| 65 | + it 'updates JAVA_OPTS sl.tags with buildpack version number and offline info' do |
| 66 | + allow_any_instance_of(JavaBuildpack::BuildpackVersion) |
| 67 | + .to receive(:to_hash).and_return({ 'version' => '1234', |
| 68 | + 'offline' => true, |
| 69 | + 'remote' => 'test-remote', |
| 70 | + 'hash' => 'test-hash' }) |
| 71 | + component.release |
| 72 | + |
| 73 | + expect(java_opts).to include('-Dsl.tags=sl-pcf-1234\(offline\)') |
| 74 | + end |
| 75 | + |
| 76 | + it 'updates JAVA_OPTS sl.tags with version number' do |
| 77 | + allow_any_instance_of(JavaBuildpack::BuildpackVersion) |
| 78 | + .to receive(:to_hash).and_return({ 'version' => '1234', |
| 79 | + 'remote' => 'test-remote', |
| 80 | + 'hash' => 'test-hash' }) |
| 81 | + component.release |
| 82 | + |
| 83 | + expect(java_opts).to include('-Dsl.tags=sl-pcf-1234') |
| 84 | + end |
| 85 | + |
| 86 | + it 'updates JAVA_OPTS sl.tags with information about unknown version number' do |
| 87 | + allow_any_instance_of(JavaBuildpack::BuildpackVersion).to receive(:to_hash).and_return({}) |
| 88 | + component.release |
| 89 | + |
| 90 | + expect(java_opts).to include('-Dsl.tags=sl-pcf-v-unknown') |
55 | 91 | end |
56 | 92 |
|
57 | 93 | it 'updates JAVA_OPTS sl.buildSessionId' do |
|
151 | 187 | end |
152 | 188 | end |
153 | 189 |
|
| 190 | + context do |
| 191 | + let(:credentials) { { 'token' => 'my_token' } } |
| 192 | + |
| 193 | + let(:configuration) do |
| 194 | + { 'build_session_id' => '1234', |
| 195 | + 'lab_id' => 'lab1', |
| 196 | + 'enable_upgrade' => true, |
| 197 | + 'customAgentUrl' => 'https://foo.com/getcustomagent/sealights-custom-agent.zip' } |
| 198 | + end |
| 199 | + |
| 200 | + before do |
| 201 | + allow(services).to receive(:one_service?).with(/sealights/, 'token').and_return(true) |
| 202 | + allow(services).to receive(:find_service).and_return('credentials' => credentials) |
| 203 | + uri = 'https://foo.com/getcustomagent/sealights-custom-agent.zip' |
| 204 | + p = Pathname.new('spec/fixtures/stub-sealights-custom-agent.zip') |
| 205 | + allow(application_cache).to receive(:get).with(uri).and_yield(p.open, false) |
| 206 | + allow(Net::HTTP).to receive(:start).with('foo.com', 443, use_ssl: true).and_call_original |
| 207 | + stub_request(:get, uri) |
| 208 | + .with( |
| 209 | + headers: { |
| 210 | + 'Accept' => '*/*', |
| 211 | + 'User-Agent' => 'Ruby' |
| 212 | + } |
| 213 | + ).to_return(status: 200, body: '', headers: {}) |
| 214 | + |
| 215 | + end |
| 216 | + |
| 217 | + it 'downloads custom agent jar', |
| 218 | + cache_fixture: 'stub-sealights-custom-agent.zip' do |
| 219 | + component.compile |
| 220 | + expect(sandbox + 'sl-test-listener-4.0.1.jar').to exist |
| 221 | + end |
| 222 | + |
| 223 | + it 'customAgentUrl from app configuration overwrites enableUpgrade to false', |
| 224 | + cache_fixture: 'stub-sealights-custom-agent.zip' do |
| 225 | + component.compile |
| 226 | + component.release |
| 227 | + expect(java_opts).to include('-Dsl.enableUpgrade=false') |
| 228 | + end |
| 229 | + end |
| 230 | + |
| 231 | + context do |
| 232 | + let(:credentials) do |
| 233 | + { 'token' => 'my_token', |
| 234 | + 'customAgentUrl' => 'https://foo.com/getcustomagent/sealights-custom-agent.zip' } |
| 235 | + end |
| 236 | + |
| 237 | + let(:configuration) do |
| 238 | + { 'build_session_id' => '1234', |
| 239 | + 'lab_id' => 'lab1', |
| 240 | + 'enable_upgrade' => true } |
| 241 | + end |
| 242 | + |
| 243 | + before do |
| 244 | + allow(services).to receive(:one_service?).with(/sealights/, 'token').and_return(true) |
| 245 | + allow(services).to receive(:find_service).and_return('credentials' => credentials) |
| 246 | + uri = 'https://foo.com/getcustomagent/sealights-custom-agent.zip' |
| 247 | + p = Pathname.new('spec/fixtures/stub-sealights-custom-agent.zip') |
| 248 | + allow(application_cache).to receive(:get).with(uri).and_yield(p.open, false) |
| 249 | + allow(Net::HTTP).to receive(:start).with('foo.com', 443, use_ssl: true).and_call_original |
| 250 | + stub_request(:get, uri) |
| 251 | + .with( |
| 252 | + headers: { |
| 253 | + 'Accept' => '*/*', |
| 254 | + 'User-Agent' => 'Ruby' |
| 255 | + } |
| 256 | + ).to_return(status: 200, body: '', headers: {}) |
| 257 | + |
| 258 | + end |
| 259 | + |
| 260 | + it 'downloads custom agent jar based on service settings', |
| 261 | + cache_fixture: 'stub-sealights-custom-agent.zip' do |
| 262 | + component.compile |
| 263 | + expect(sandbox + 'sl-test-listener-4.0.1.jar').to exist |
| 264 | + end |
| 265 | + |
| 266 | + it 'customAgentUrl from service settings forces overwrites enableUpgrade to false', |
| 267 | + cache_fixture: 'stub-sealights-custom-agent.zip' do |
| 268 | + component.compile |
| 269 | + component.release |
| 270 | + expect(java_opts).to include('-Dsl.enableUpgrade=false') |
| 271 | + end |
| 272 | + end |
| 273 | + |
| 274 | + context do |
| 275 | + let(:credentials) { { 'token' => 'my_token' } } |
| 276 | + |
| 277 | + let(:configuration) do |
| 278 | + { 'build_session_id' => '1234', |
| 279 | + 'lab_id' => 'lab1', |
| 280 | + 'customAgentUrl' => 'https://foo.com/getcustomagent/sealights-custom-agent-invalid.zip' } |
| 281 | + end |
| 282 | + |
| 283 | + before do |
| 284 | + allow(services).to receive(:one_service?).with(/sealights/, 'token').and_return(true) |
| 285 | + allow(services).to receive(:find_service).and_return('credentials' => credentials) |
| 286 | + uri = 'https://foo.com/getcustomagent/sealights-custom-agent-invalid.zip' |
| 287 | + p = Pathname.new('spec/fixtures/stub-sealights-custom-agent-invalid.zip') |
| 288 | + allow(application_cache).to receive(:get).with(uri).and_yield(p.open, false) |
| 289 | + stub_request(:get, 'https://foo.com/getcustomagent/sealights-custom-agent-invalid.zip') |
| 290 | + .with( |
| 291 | + headers: { |
| 292 | + 'Accept' => '*/*', |
| 293 | + 'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', |
| 294 | + 'User-Agent' => 'Ruby' |
| 295 | + } |
| 296 | + ).to_return(status: 200, body: '', headers: {}) |
| 297 | + end |
| 298 | + |
| 299 | + it 'test listener agent jar not found in downloaded zip', |
| 300 | + cache_fixture: 'stub-sealights-custom-agent-invalid.zip' do |
| 301 | + component.compile |
| 302 | + expect { component.release } |
| 303 | + .to raise_error(RuntimeError, |
| 304 | + /Failed to find jar which name starts with 'sl-test-listener' in downloaded zip/) |
| 305 | + end |
| 306 | + end |
| 307 | + |
154 | 308 | end |
155 | 309 |
|
156 | 310 | end |
0 commit comments