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.

1 Answers

0
ffd03e On

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 on localhost: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
  • add tests to src/App.test.js
// This test fails
it('connects with localhost', async () => {
  const response = await axios.get('localhost');
  console.log('axios localhost response: ' + response.data);
  expect(response).toBeTruthy();
});

// This test passes
it('connect with external source', async () => {
    const response = await axios.get('https://jsonplaceholder.typicode.com/users/10');
    console.log('axios external response: ' + response.data.name);
    expect(response.data.name).toBeTruthy();
});
  • edit cloudbuild.yaml (I prefer yaml because you can add comments (-: )
steps:
  # npm install
- name: 'gcr.io/cloud-builders/npm'
  args: ['install']
  # npm run build
- name: 'gcr.io/cloud-builders/npm'
  args: ['run', 'build']

  # bash -c | CI=true npm test
  # syntax to add commands before npm (-:
- name: 'gcr.io/cloud-builders/npm'
  entrypoint: 'bash'
  args:
      - '-c'
      - |
        CI=true npm test
  • 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