I'm having trouble getting integration tests to run on Google's Cloud Build.
Unit tests run fine, but integration tests that make requests to an external API (using Axios) display this error in Cloud Build: connect ECONNREFUSED 127.0.0.1:80.
It's a React app built with Create React App. Here's the cloudbuild.json:
{
"steps": [
{
"name": "gcr.io/cloud-builders/npm",
"entrypoint": "npm",
"args": [
"install"
],
},
{
"name": "gcr.io/cloud-builders/npm",
"entrypoint": "npm",
"args": [
"run", "build"
],
},
{
"name": "gcr.io/cloud-builders/npm",
"entrypoint": "npm",
"args": [
"test"
],
"env": [
"CI=true",
],
}
]
}
Here's an example error:
Step #1: src/reducers/readings › should update state appropriately when starting a fetch readings request
Step #1:
Step #1: connect ECONNREFUSED 127.0.0.1:80
Any help would be appreciated!
--
Follow up:
I finally traced down the issue with this. The external API url was defined in an .env file. Since Cloudbuild didn't have access to these variables, Axios calls defaulted to 127.0.0.1 (localhost), which failed.
The issue was fixed by encrypting the env file, storing it as a Cloud KMS key, and giving the cloud builder access to it.
# Decrypt env variables
- name: gcr.io/cloud-builders/gcloud
args:
- kms
- decrypt
- --ciphertext-file=.env.enc
- --plaintext-file=.env
- --location=global
- --keyring=[KEYRING]
- --key=[KEY]
Thanks for the pointers @ffd03e.
Is the external API running in Cloud Build or somewhere else? It would be helpful to see a test. Also, is CI=true getting picked up or do the tests hang on watch mode? (https://facebook.github.io/create-react-app/docs/running-tests#linux-macos-bash)
It seems like your test is trying to connect to
localhost
, which fails because nothing is running onlocalhost:80
. Cloud Build should be able to connect to an external API. Here is an example:mkdir gcb-connect-test && cd gcb-connect-test
npx create-react-app .
touch cloudbuild.yaml
src/App.test.js
cloudbuild.yaml
(I prefer yaml because you can add comments (-: )gcloud builds submit .
If this ends up being a weirder issue than just accidentally connecting to localhost the #cloudbuild channel on gcp slack is a good resource: slack sign up link