department-of-veterans-affairs / va-mobile-app

"If VA were a company, it would have a flagship mobile app."
https://department-of-veterans-affairs.github.io/va-mobile-app/
17 stars 2 forks source link

Investigate Contract Testing as a way to proactively guard against upstream API changes #6850

Closed timwright12 closed 1 year ago

timwright12 commented 1 year ago

Description

Contract testing is a methodology for ensuring that two separate systems are compatible and can communicate with one other. It captures the interactions that are exchanged between each service, storing them in a contract, which then can be used to verify that both parties adhere to it.

On the mobile team we have a perceived issue with upstream services that frequently change and affect the downstream app. The team has to react to these issues as they come up and it often times can disrupt a day's/sprint's work.

This is ticket is to do some discovery work on the possibility of setting up contract tests between the mobile API and upstream services in order to proactively detect schema and data shape changes rather than waiting to logs to alert us.

I've linked to Pact below, which is a contract testing framework that was used with Web platform at one point, but we shouldn't feel tied to it just because it's familiar.

Resources

Definition of Done

kpethtel commented 1 year ago

Overview

I don't think contract testing as it's defined here will achieve the goals of this ticket. I believe what's being proposed is closer to schema testing.

One very important caveat to all of this is that while most vets-api client fetch methods return the data unaltered, either as a hash or struct, others process the data into either a Response object or as instances of models. In cases where we receive altered data, there's no way of getting the unaltered response without changing the client code, which isn't a viable option. The system simply isn't designed for it. But for the cases where we receive unaltered data, we can create a small library that can be used to check the schema. See list at bottom for examples of some endpoints where this simply won't work.

Another important question is whether it's worth testing the data received or only the schema. Testing the actual data would be fragile because some data is expected to change over time, so in my opinion it's probably not worth testing.

Options

1) We can create a cron job that periodically runs through a series of endpoint tests. We could keep the expected response schema information in config files. Each new data type would require for code to be added to that uses the appropriate vets-api client to make the call. That will make this relatively difficult to maintain.

Pros:

Cons:

2) Creating a schema discovery endpoint that only functions on staging. It could be something like mobile/discovery/:data_type?method=get. Similar to option 1, each endpoint would require some code setup to utilize the appropriate vets-api client, which means this will also be difficult to maintain. This endpoint could then be utilized by the staging tester, which would contain all of the config files for the expected responses.

Pros:

Cons:

3) Create a library that can be used within mobile vets-api code.

Pros:

Cons:

Conclusion

I believe that option 3 is our best choice. It has the flexibility to also work on all requests in production, which would give us the widest array of test cases, or to be limited to specific test users in staging.

Of the other two options, I would prefer option 1 because it's less complex.

List of show and indexes where we receive altered data:

timwright12 commented 1 year ago

@kpethtel can you link up the follow-on tickets here based on your recommendation just so we can connect the dots?