This repository has been archived by the owner on Sep 5, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathdeploy_service_spec.rb
117 lines (90 loc) · 5.07 KB
/
deploy_service_spec.rb
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
describe DeployService do
describe "#deploy" do
let(:service_id) { "123321abc" }
let(:service_types) { %w[healthcheck cache_settings request_settings response_object header gzip] }
it "deploys the VCL" do
@requests = stub_fastly_get_service(service_id)
@requests += stub_fastly_delete_old_vcl(service_id, service_types)
@requests += stub_fastly_upload_new_vcl(service_id)
# Get the settings
@requests << stub_request(:get, "https://api.fastly.com/service/#{service_id}/version/3/settings")
.to_return(body: File.read("spec/fixtures/fastly-get-settings.json"))
# And update them
@requests << stub_request(:put, "https://api.fastly.com/service/#{service_id}/version/3/settings")
.to_return(body: "{}")
# Check that the new config is good
@requests << stub_request(:get, "https://api.fastly.com/service/#{service_id}/version/3/validate")
.to_return(body: JSON.dump(status: "ok"))
@requests << stub_fastly_activate_new_vcl(service_id)
ClimateControl.modify FASTLY_API_KEY: "fastly@example.com" do
DeployService.new.deploy!(%w[test production])
expect(@requests).to all(have_been_requested.at_least_once)
end
end
it "raises error when vhost or environment is missing" do
ClimateControl.modify FASTLY_API_KEY: "fastly@example.com" do
expect { DeployService.new.deploy!(%w[test]) }
.to raise_error(RuntimeError, /Usage: #{$PROGRAM_NAME} <configuration> <environment>/)
end
end
it "raises error when vhost and environment combination is not in fastly.yaml" do
ClimateControl.modify FASTLY_API_KEY: "fastly@example.com" do
expect { DeployService.new.deploy!(%w[test staging]) }
.to raise_error(RuntimeError, /Error: Unknown configuration\/environment combination: test staging. Check this combination exists in fastly.yaml./)
end
end
it "fails when delete ui objects fails" do
@requests = stub_fastly_get_service(service_id)
@requests += stub_fastly_unsuccessfully_delete_old_vcl(service_id, "healthcheck")
ClimateControl.modify FASTLY_API_KEY: "fastly@example.com" do
expect { DeployService.new.deploy!(%w[test production]) }
.to raise_error(RuntimeError, /Error: Failed to delete configuration/)
expect(@requests).to all(have_been_requested.at_least_once)
end
end
it "fails when there are no active versions of the configuration" do
@requests = []
# Fastly#get_service. Return a service with two VCL "versions" (https://docs.fastly.com/api/config#version)
@requests << stub_request(:get, "https://api.fastly.com/service/#{service_id}")
.to_return(body: File.read("spec/fixtures/fastly-get-service-response-inactive-versions.json"))
@requests += stub_fastly_delete_old_vcl(service_id, service_types)
# We first check the latest version
@requests << stub_request(:get, "https://api.fastly.com/service/#{service_id}/version/3/vcl/main")
.to_return(body: "{}")
# We then delete it
@requests << stub_request(:delete, "https://api.fastly.com/service/#{service_id}/version/3/vcl/main")
.to_return(body: "{}")
# Then send the actual VCL
# https://docs.fastly.com/api/config#vcl_7ade6ab5926b903b6acf3335a85060cc
@requests << stub_request(:post, "https://api.fastly.com/service/#{service_id}/version/3/vcl")
.to_return(body: File.read("spec/fixtures/fastly-post-vcl.json"))
# Test the VCL of the previous version
@requests << stub_request(:put, "https://api.fastly.com/service/#{service_id}/version/2/vcl/test-vcl/main")
.to_return(body: "{}")
ClimateControl.modify FASTLY_API_KEY: "fastly@example.com" do
expect { DeployService.new.deploy!(%w[test production]) }
.to raise_error(RuntimeError, /There are no active versions of this configuration/)
expect(@requests).to all(have_been_requested.at_least_once)
end
end
it "fails when the new configuration is invalid" do
@requests = stub_fastly_get_service(service_id)
@requests += stub_fastly_delete_old_vcl(service_id, service_types)
@requests += stub_fastly_upload_new_vcl(service_id)
# Get the settings
@requests << stub_request(:get, "https://api.fastly.com/service/#{service_id}/version/3/settings")
.to_return(body: File.read("spec/fixtures/fastly-get-settings.json"))
# And update them
@requests << stub_request(:put, "https://api.fastly.com/service/#{service_id}/version/3/settings")
.to_return(body: "{}")
# Check that the new config is valid
@requests << stub_request(:get, "https://api.fastly.com/service/#{service_id}/version/3/validate")
.to_return(body: JSON.dump(status: 500, msg: "Error message"))
ClimateControl.modify FASTLY_API_KEY: "fastly@example.com" do
expect { DeployService.new.deploy!(%w[test production]) }
.to raise_error(RuntimeError, /Error: Invalid configuration:\n Error message/)
expect(@requests).to all(have_been_requested.at_least_once)
end
end
end
end