-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathe2e.rb
More file actions
277 lines (244 loc) · 11.3 KB
/
e2e.rb
File metadata and controls
277 lines (244 loc) · 11.3 KB
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
require 'json'
require "ostruct"
desc 'Support for E2E tests: building XCRemoteCache-enabled xcodeproj using xcodebuild'
namespace :e2e do
COCOAPODS_DIR = 'cocoapods-plugin'
COCOAPODS_GEMSPEC_FILENAME = "cocoapods-xcremotecache.gemspec"
E2E_COCOAPODS_SAMPLE_DIR = 'e2eTests/XCRemoteCacheSample'
E2E_STANDALONE_SAMPLE_DIR = 'e2eTests/StandaloneSampleApp'
GIT_REMOTE_NAME = 'self'
# Location of the remote address that points to itself
GIT_REMOTE_ADDRESS = '.'
GIT_BRANCH = 'e2e-test-branch'
LOG_NAME = "xcodebuild.log"
DERIVED_DATA_PATH = './DerivedData'
NGINX_ROOT_DIR = '/tmp/cache'
XCRC_BINARIES = 'XCRC'
SHARED_COCOAPODS_CONFIG = {
'cache_addresses' => ['http://localhost:8080/cache/pods'],
'primary_repo' => GIT_REMOTE_ADDRESS,
'primary_branch' => GIT_BRANCH,
'mode' => 'consumer',
'final_target' => 'XCRemoteCacheSample',
'artifact_maximum_age' => 0
}.freeze
DEFAULT_EXPECTATIONS = {
'misses' => 0,
'hit_rate' => 100
}.freeze
Stats = Struct.new(:hits, :misses, :hit_rate)
# run E2E tests
task :run => [:run_cocoapods, :run_standalone]
# run E2E tests for CocoaPods-powered projects
task :run_cocoapods do
install_cocoapods_plugin
start_nginx
configure_git
# Run scenarios for all Podfile scenarios
for podfile_path in Dir.glob('e2eTests/**/*.Podfile')
run_cocoapods_scenario(podfile_path)
end
# Revert all side effects
clean
end
# run E2E tests for standalone (non-CocoaPods) projects
task :run_standalone do
clean_server
start_nginx
configure_git
configuration = 'Release'
# Prepare binaries for the standalone mode
prepare_for_standalone(E2E_STANDALONE_SAMPLE_DIR)
puts 'Building standalone producer...'
####### Producer #########
Dir.chdir(E2E_STANDALONE_SAMPLE_DIR) do
clean_git
# Run integrate the project
system("pwd")
system("#{XCRC_BINARIES}/xcprepare integrate --input StandaloneApp.xcodeproj --mode producer --configurations-exclude Debug")
# Build the project to fill in the cache
build_project(nil, "StandaloneApp.xcodeproj", 'WatchExtension', 'watch', 'watchOS', configuration)
build_project(nil, "StandaloneApp.xcodeproj", 'StandaloneApp', 'iphone', 'iOS', configuration)
system("#{XCRC_BINARIES}/xcprepare stats --reset --format json")
system("#{XCRC_BINARIES}/xcprepare mark --configuration Release --platform iphonesimulator")
end
puts 'Building standalone consumer...'
####### Consumer #########
# new dir to emulate different srcroot
consumer_srcroot = "#{E2E_STANDALONE_SAMPLE_DIR}_consumer"
system("mv #{E2E_STANDALONE_SAMPLE_DIR} #{consumer_srcroot}")
at_exit { puts("reverting #{E2E_STANDALONE_SAMPLE_DIR}"); system("mv #{consumer_srcroot} #{E2E_STANDALONE_SAMPLE_DIR}") }
prepare_for_standalone(consumer_srcroot)
Dir.chdir(consumer_srcroot) do
system("#{XCRC_BINARIES}/xcprepare integrate --input StandaloneApp.xcodeproj --mode consumer --final-producer-target StandaloneApp --consumer-eligible-configurations Release --configurations-exclude Debug")
build_project(nil, "StandaloneApp.xcodeproj", 'WatchExtension', 'watch', 'watchOS', configuration, {'derivedDataPath' => "#{DERIVED_DATA_PATH}_consumer"})
build_project(nil, "StandaloneApp.xcodeproj", 'StandaloneApp', 'iphone', 'iOS', configuration, {'derivedDataPath' => "#{DERIVED_DATA_PATH}_consumer"})
valide_hit_rate(OpenStruct.new(DEFAULT_EXPECTATIONS))
puts 'Building standalone consumer with local change...'
# Extra: validate local compilation of the Standalone ObjC code
system("echo '' >> StandaloneApp/StandaloneObjc.m")
build_project(nil, "StandaloneApp.xcodeproj", 'WatchExtension', 'watch', 'watchOS', configuration, {'derivedDataPath' => "#{DERIVED_DATA_PATH}_consumer_local"})
build_project(nil, "StandaloneApp.xcodeproj", 'StandaloneApp', 'iphone', 'iOS', configuration, {'derivedDataPath' => "#{DERIVED_DATA_PATH}_consumer_local"})
end
# Revert all side effects
clean
end
# Build and install a plugin
def self.install_cocoapods_plugin
Dir.chdir(COCOAPODS_DIR) do
gemfile_path = "cocoapods-xcremotecache.gem"
system("gem build #{COCOAPODS_GEMSPEC_FILENAME} -o #{gemfile_path}")
system("gem install #{gemfile_path}")
end
end
def self.start_nginx
# Start nginx server
system('nginx -c $PWD/e2eTests/nginx/nginx.conf')
puts('starting nginx')
# Call cleanup on exit
at_exit { puts('resetting ngingx'); system('nginx -s stop') }
end
# Create a new branch out of a current commit and
# add remote that points to itself
def self.configure_git
system("git checkout -B #{GIT_BRANCH}")
system("git remote add #{GIT_REMOTE_NAME} #{GIT_REMOTE_ADDRESS} && git fetch -q #{GIT_REMOTE_NAME}")
# Revert new remote on exit
at_exit { system("git remote remove #{GIT_REMOTE_NAME}")}
end
def self.pre_producer_setup
clean_git
clean_server
# Link prebuilt binaries to the Project
system("ln -s $(pwd)/releases #{E2E_COCOAPODS_SAMPLE_DIR}/#{XCRC_BINARIES}")
end
def self.pre_consumer_setup
clean_git
# Link prebuilt binaries to the Project
system("ln -s $(pwd)/releases #{E2E_COCOAPODS_SAMPLE_DIR}/#{XCRC_BINARIES}")
end
def self.clean_server
system("rm -rf #{NGINX_ROOT_DIR}")
end
# Revert any local changes in the test project
def self.clean_git
system("git clean -xdf #{E2E_COCOAPODS_SAMPLE_DIR}")
end
# Cleans all extra locations that a test creates
def self.clean
clean_git
clean_server
end
# xcremotecache configuration to add to Podfile
def self.cocoapods_configuration_string(extra_configs = {})
configuration_lines = ['xcremotecache({']
all_properties = SHARED_COCOAPODS_CONFIG.merge(extra_configs)
config_lines = all_properties.map {|key, value| " \"#{key}\" => #{value.inspect},"}
configuration_lines.push(*config_lines)
configuration_lines << '})'
configuration_lines.join("\n")
end
def self.dump_podfile(config, source)
# Create producer Podfile
File.open("#{E2E_COCOAPODS_SAMPLE_DIR}/Podfile", 'w') do |f|
# Copy podfile template
File.foreach(source) { |line| f.puts line }
f.write(config)
end
end
def self.build_project(workspace, project, scheme, sdk = 'iphone', platform = 'iOS', configuration = 'Debug', extra_args = {})
xcodebuild_args = {
'workspace' => workspace,
'project' => project,
'scheme' => scheme,
'configuration' => configuration,
'sdk' => "#{sdk}simulator",
'destination' => "generic/platform=#{platform} Simulator",
'derivedDataPath' => DERIVED_DATA_PATH,
}.merge(extra_args).compact
xcodebuild_vars = {
'EXCLUDED_ARCHS' => 'arm64'
}
args = ['set -o pipefail;', 'xcodebuild']
args.push(*xcodebuild_args.map {|k,v| "-#{k} '#{v}'"})
args.push(*xcodebuild_vars.map {|k,v| "#{k}='#{v}'"})
args.push('clean build')
args.push("| tee #{LOG_NAME}")
puts 'Building a project with xcodebuild...'
system(args.join(' '))
unless $?.success?
system("tail #{LOG_NAME}")
raise "xcodebuild failed."
end
end
def self.build_project_cocoapods(sdk = 'iphone', platform = 'iOS', configuration = 'Debug', extra_args = {})
system('pod install')
build_project('XCRemoteCacheSample.xcworkspace', nil, 'XCRemoteCacheSample', sdk, platform, configuration, extra_args)
end
def self.read_stats
stats_json_string = JSON.parse(`#{XCRC_BINARIES}/xcprepare stats --format json`)
misses = stats_json_string.fetch('miss_count', 0)
hits = stats_json_string.fetch('hit_count', 0)
all_targets = misses + hits
hit_rate = all_targets == 0 ? nil : hits * 100 / all_targets
Stats.new(hits, misses, hit_rate)
end
# validate 100% hit rate
def self.valide_hit_rate(expectations)
status = read_stats()
all_targets = status.misses + status.hits
unless expectations.misses.nil?
raise "Failure: Unexpected misses: #{status.misses} (#{all_targets}). Expected #{expectations.misses}" if status.misses != expectations.misses
end
unless expectations.hit_rate.nil?
raise "Failure: Hit rate is #{status.hit_rate}% (#{all_targets}). Expected #{expectations.hit_rate}%" if status.hit_rate != expectations.hit_rate
end
unless expectations.hits.nil?
raise "Failure: Hits count is #{status.hit_rate}% (#{all_targets}). Expected #{expectations.hits}" if status.hits != expectations.hits
end
puts("Hit rate: #{status.hit_rate}% (#{status.hits}/#{all_targets})")
end
def self.run_cocoapods_scenario(template_path)
# Optional file, which adds extra cocoapods configs to a template
template_config_path = "#{template_path}.config"
extra_config = File.exist?(template_config_path) ? JSON.load(File.read(template_config_path)) : {}
producer_configuration = cocoapods_configuration_string({'mode' => 'producer'}.merge(extra_config))
consumer_configuration = cocoapods_configuration_string(extra_config)
expectations = build_expectations(template_path)
puts("****** Scenario: #{template_path}")
# Run producer build
pre_producer_setup
dump_podfile(producer_configuration, template_path)
puts('Building producer ...')
Dir.chdir(E2E_COCOAPODS_SAMPLE_DIR) do
build_project_cocoapods
# reset XCRemoteCache stats
system("#{XCRC_BINARIES}/xcprepare stats --reset --format json")
end
# Run consumer build
pre_consumer_setup
dump_podfile(consumer_configuration, template_path)
puts('Building consumer ...')
Dir.chdir(E2E_COCOAPODS_SAMPLE_DIR) do
build_project_cocoapods('iphone', 'iOS', 'Debug', {'derivedDataPath' => "#{DERIVED_DATA_PATH}_consumer"})
valide_hit_rate(expectations)
end
end
def self.prepare_for_standalone(dir)
clean_git
system("ln -s $(pwd)/releases #{dir}/#{XCRC_BINARIES}")
end
# Returns a hash of all expectations that should be validated for a template
# The implementation assumes 100% hitrate and extra expecations can be provided in an optional file
# #{template_path}.expectations
def self.build_expectations(template_path)
expectations = DEFAULT_EXPECTATIONS.dup
return expectations if template_path.nil?
template_config_path = "#{template_path}.expectations"
if File.exist?(template_config_path)
extra_config = File.exist?(template_config_path) ? JSON.load(File.read(template_config_path)) : {}
expectations.merge!(extra_config)
end
OpenStruct.new(expectations)
end
end