Skip to content

Instantly share code, notes, and snippets.

@Gerg
Created October 6, 2020 21:51
Show Gist options
  • Save Gerg/f1cbd8cbd0fa02be743833d7ed3e9411 to your computer and use it in GitHub Desktop.
Save Gerg/f1cbd8cbd0fa02be743833d7ed3e9411 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
require 'date'
require 'open3'
class Main
class << self
def resources
%w[
app_usage_events
audit_events
builds
buildpacks
deployments
routes
service_brokers
service_instances
service_offerings
service_plans
stacks
apps/9485d178-eb93-49c6-9289-33597fbff59f/revisions
service_bindings
apps/e3a57066-096e-4b26-a085-8dee8cfa47db/sidecars
]
end
def per_page
5
end
def filters
%w[guids]
end
def filters_map(filter)
{
'created_ats' => 'created_at',
'updated_ats' => 'updated_at',
'guids' => 'guid',
}[filter]
end
def main
resources.each do |resource|
filters.each do |filter|
puts "Testing #{filter} filter for #{resource}.\n\n"
field = filters_map(filter)
request = "/v3/#{resource}?per_page=#{per_page}"
parsed_response = make_request(request, field)
actual_resources = parsed_response.length
raise "Expected #{per_page} resources, got #{actual_resources}" unless actual_resources == per_page
guid1 = parsed_response[1]
guid2 = parsed_response[3]
request = "/v3/#{resource}?per_page=#{per_page}&guids=#{guid1},#{guid2}"
filtered_resources = make_request(request, field)
raise "Expected #{filtered_resources} to not be empty" unless filtered_resources.length
raise "Expected #{filtered_resources} to have length 2" unless filtered_resources.length == 2
raise "Expected #{filtered_resources} to include #{guid1} and #{guid2}" unless (filtered_resources == [guid1, guid2])
end
end
end
def make_request(request, field)
p request
response, status = Open3.capture2("cf curl '#{request}' | jq .resources[].#{field}")
raise response unless status == 0
parsed_response = response.split("\n").map { |string| string.tr('\"','') }
p parsed_response
puts "\n"
parsed_response
end
end
end
Main.main
# Copyright 2020 Greg Cobb
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment