| /* |
| Copyright 2019 Google Inc. |
| |
| Licensed under the Apache License, Version 2.0 (the "License"); |
| you may not use this file except in compliance with the License. |
| You may obtain a copy of the License at |
| |
| https://www.apache.org/licenses/LICENSE-2.0 |
| |
| Unless required by applicable law or agreed to in writing, software |
| distributed under the License is distributed on an "AS IS" BASIS, |
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| See the License for the specific language governing permissions and |
| limitations under the License. |
| */ |
| package main |
| |
| import ( |
| "context" |
| "flag" |
| "log" |
| |
| "github.com/maps-booking-v3/api" |
| "github.com/maps-booking-v3/booking" |
| fpb "github.com/maps-booking-v3/feeds" |
| "github.com/maps-booking-v3/utils" |
| ) |
| |
| var ( |
| serverAddr = flag.String("server_addr", "example.com:80", "Your http server's address in the format of host:port") |
| credentialsFile = flag.String("credentials_file", "", "File containing credentials for your server. Leave blank to bypass authentication. File should have exactly one line of the form 'username:password'.") |
| testSlots = flag.Int("num_test_slots", 10, "Maximum number of slots to test from availability_feed. Slots will be selected randomly") |
| allFlows = flag.Bool("all_tests", false, "Whether to test all endpoints.") |
| healthFlow = flag.Bool("health_check_test", false, "Whether to test the Health endpoint.") |
| checkFlow = flag.Bool("check_availability_test", false, "Whether to test availability lookup. Will use BatchAvailabilityLookup or CheckAvailability endpoint depending on value of the use_batch_availability_lookup flag.") |
| bookFlow = flag.Bool("booking_test", false, "Whether to test the CreateBooking endpoint.") |
| listFlow = flag.Bool("list_bookings_test", false, "Whether to test the ListBookings endpoint") |
| statusFlow = flag.Bool("booking_status_test", false, "Whether to test the GetBookingStatus endpoint.") |
| rescheduleFlow = flag.Bool("rescheduling_test", false, "Whether to test the UpdateBooking endpoint.") |
| cancelAllBookings = flag.Bool("cancel_all_bookings", false, "This option assumes that the ListBookings and UpdateBooking endpoints are fully functional. This is a convenience flag for purging your system of all previously created bookings.") |
| availabilityFeed = flag.String("availability_feed", "", "Absolute path to availability feed required for all tests except health. Feeds can be in either json or pb3 format") |
| outputDir = flag.String("output_dir", "", "Absolute path of dir to dump log file.") |
| caFile = flag.String("ca_file", "", "Absolute path to your server's Certificate Authority root cert. Downloading all roots currently recommended by the Google Internet Authority is a suitable alternative https://pki.google.com/roots.pem. Leave blank to connect using http rather than https.") |
| fullServerName = flag.String("full_server_name", "", "Fully qualified domain name. Same name used to sign CN. Only necessary if ca_file is specified and the base URL differs from the server address.") |
| outputToTerminal = flag.Bool("output_to_terminal", false, "Output to terminal rather than a file.") |
| useBal = flag.Bool("use_batch_availability_lookup", false, "Whether to use the BatchAvailabilityLookup RPC (as opposed to the deprecated CheckAvailability)") |
| ) |
| |
| func makeConfig(logger *log.Logger) *utils.Config { |
| conn, err := api.InitHTTPConnection(*serverAddr, *credentialsFile, *caFile, *fullServerName) |
| if err != nil { |
| logger.Fatalf("Failed to init http connection %v", err) |
| } |
| return &utils.Config{Conn: conn, |
| BookingAllFlows: *allFlows, |
| BookingHealthFlow: *healthFlow, |
| BookingCheckFlow: *checkFlow, |
| BookingBookFlow: *bookFlow, |
| BookingListFlow: *listFlow, |
| BookingStatusFlow: *statusFlow, |
| BookingRescheduleFlow: *rescheduleFlow, |
| BookingCancelAllBookings: *cancelAllBookings, |
| BookingUseBal: *useBal} |
| } |
| |
| func main() { |
| flag.Parse() |
| |
| logger, f, err := utils.MakeLogger(*outputToTerminal, *outputDir) |
| if err != nil { |
| log.Fatal("Could not create logger: ", err) |
| } |
| if f != nil { |
| defer f.Close() |
| } |
| |
| config := makeConfig(logger) |
| |
| // Health check doesn't affect the cancel booking flow so we let it through. |
| if *cancelAllBookings && (*allFlows || *checkFlow || *bookFlow || *listFlow || *statusFlow || *rescheduleFlow) { |
| logger.Fatal("cancel_all_bookings is not supported with other test flows") |
| } |
| |
| var summary utils.TestSummary |
| var av []*fpb.Availability |
| var avForRescheduling []*fpb.Availability |
| |
| needAvailability := !*cancelAllBookings || *allFlows || *checkFlow || *bookFlow || *listFlow || *statusFlow || *rescheduleFlow |
| |
| if needAvailability { |
| // Build availablility records. |
| if *availabilityFeed == "" { |
| logger.Fatal("Please set availability_feed flag if you wish to test anything except health check.") |
| } |
| av, err = utils.AvailabilityFrom(logger, *availabilityFeed, *testSlots, false) |
| if err != nil { |
| logger.Fatalf("Failed to get availability: %v", err.Error()) |
| } |
| summary.BookingTotalSlotsProcessed += len(av) |
| avForRescheduling, err = utils.AvailabilityFrom(logger, *availabilityFeed, *testSlots, true) |
| if err != nil { |
| logger.Fatalf("Failed to get availability for rescheduling test: %v", err.Error()) |
| } |
| } |
| booking.RunTests(context.Background(), logger, config, av, avForRescheduling, &summary) |
| } |