Fix *most* tests

This commit is contained in:
Jess Bees
2023-12-21 16:10:50 -05:00
parent 0486580c63
commit 4be34033fc

View File

@@ -1,4 +1,6 @@
const core = require('@actions/core')
// For mocking network calls with core http (http-client)
const nock = require('nock')
// For mocking network calls with native Fetch (octokit)
const { MockAgent, setGlobalDispatcher } = require('undici')
@@ -7,6 +9,8 @@ const { Deployment, MAX_TIMEOUT, ONE_GIGABYTE, SIZE_LIMIT_DESCRIPTION } = requir
const fakeJwt =
'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJqdGkiOiJiNjllMWIxOC1jOGFiLTRhZGQtOGYxOC03MzVlMzVjZGJhZjAiLCJzdWIiOiJyZXBvOnBhcGVyLXNwYS9taW55aTplbnZpcm9ubWVudDpQcm9kdWN0aW9uIiwiYXVkIjoiaHR0cHM6Ly9naXRodWIuY29tL3BhcGVyLXNwYSIsInJlZiI6InJlZnMvaGVhZHMvbWFpbiIsInNoYSI6ImEyODU1MWJmODdiZDk3NTFiMzdiMmM0YjM3M2MxZjU3NjFmYWM2MjYiLCJyZXBvc2l0b3J5IjoicGFwZXItc3BhL21pbnlpIiwicmVwb3NpdG9yeV9vd25lciI6InBhcGVyLXNwYSIsInJ1bl9pZCI6IjE1NDY0NTkzNjQiLCJydW5fbnVtYmVyIjoiMzQiLCJydW5fYXR0ZW1wdCI6IjIiLCJhY3RvciI6IllpTXlzdHkiLCJ3b3JrZmxvdyI6IkNJIiwiaGVhZF9yZWYiOiIiLCJiYXNlX3JlZiI6IiIsImV2ZW50X25hbWUiOiJwdXNoIiwicmVmX3R5cGUiOiJicmFuY2giLCJlbnZpcm9ubWVudCI6IlByb2R1Y3Rpb24iLCJqb2Jfd29ya2Zsb3dfcmVmIjoicGFwZXItc3BhL21pbnlpLy5naXRodWIvd29ya2Zsb3dzL2JsYW5rLnltbEByZWZzL2hlYWRzL21haW4iLCJpc3MiOiJodHRwczovL3Rva2VuLmFjdGlvbnMuZ2l0aHVidXNlcmNvbnRlbnQuY29tIiwibmJmIjoxNjM4ODI4MDI4LCJleHAiOjE2Mzg4Mjg5MjgsImlhdCI6MTYzODgyODYyOH0.1wyupfxu1HGoTyIqatYg0hIxy2-0bMO-yVlmLSMuu2w'
const LIST_ARTIFACTS_TWIRP_PATH = '/twirp/github.actions.results.api.v1.ArtifactService/ListArtifacts';
describe('Deployment', () => {
let mockPool
@@ -51,7 +55,7 @@ describe('Deployment', () => {
jest.spyOn(core, 'debug').mockImplementation(jest.fn())
// Set up Fetch mocking
const mockAgent = new MockAgent()
let mockAgent = new MockAgent()
mockAgent.disableNetConnect()
setGlobalDispatcher(mockAgent)
mockPool = mockAgent.get('https://api.github.com')
@@ -66,16 +70,13 @@ describe('Deployment', () => {
it('can successfully create a deployment', async () => {
process.env.GITHUB_SHA = 'valid-build-version'
mockPool
.intercept({
path: `/repos/${process.env.GITHUB_REPOSITORY}/actions/runs/${process.env.GITHUB_RUN_ID}/artifacts?name=github-pages`,
method: 'GET'
})
const twirpScope = nock(process.env.ACTIONS_RESULTS_URL)
.post(LIST_ARTIFACTS_TWIRP_PATH)
.reply(
200,
{
total_count: 1,
artifacts: [{ id: 11, name: `github-pages`, size_in_bytes: 221 }]
artifacts: [{ databaseId: 11, name: `github-pages`, size_in_bytes: 221 }]
},
{ headers: { 'content-type': 'application/json' } }
)
@@ -117,21 +118,19 @@ describe('Deployment', () => {
expect(core.info).toHaveBeenLastCalledWith(
expect.stringMatching(new RegExp(`^Created deployment for ${process.env.GITHUB_SHA}`))
)
twirpScope.done()
})
it('can successfully create a preview deployment', async () => {
process.env.GITHUB_SHA = 'valid-build-version'
mockPool
.intercept({
path: `/repos/${process.env.GITHUB_REPOSITORY}/actions/runs/${process.env.GITHUB_RUN_ID}/artifacts?name=github-pages`,
method: 'GET'
})
const twirpScope = nock(process.env.ACTIONS_RESULTS_URL)
.post(LIST_ARTIFACTS_TWIRP_PATH)
.reply(
200,
{
total_count: 1,
artifacts: [{ id: 11, name: `github-pages`, size_in_bytes: 221 }]
artifacts: [{ databaseId: 11, name: `github-pages`, size_in_bytes: 221 }]
},
{ headers: { 'content-type': 'application/json' } }
)
@@ -179,16 +178,14 @@ describe('Deployment', () => {
expect(core.info).toHaveBeenLastCalledWith(
expect.stringMatching(new RegExp(`^Created deployment for ${process.env.GITHUB_SHA}`))
)
twirpScope.done()
})
it('reports errors with failed artifact metadata exchange', async () => {
process.env.GITHUB_SHA = 'invalid-build-version'
mockPool
.intercept({
path: `/repos/${process.env.GITHUB_REPOSITORY}/actions/runs/${process.env.GITHUB_RUN_ID}/artifacts?name=github-pages`,
method: 'GET'
})
const twirpScope = nock(process.env.ACTIONS_RESULTS_URL)
.post(LIST_ARTIFACTS_TWIRP_PATH)
.reply(400, { message: 'Bad request' }, { headers: { 'content-type': 'application/json' } })
// Create the deployment
@@ -198,20 +195,18 @@ describe('Deployment', () => {
`Failed to create deployment (status: 400) with build version ${process.env.GITHUB_SHA}. Responded with: Bad request`
)
)
twirpScope.done()
})
it('reports errors with a failed 500 in a deployment', async () => {
process.env.GITHUB_SHA = 'build-version'
mockPool
.intercept({
path: `/repos/${process.env.GITHUB_REPOSITORY}/actions/runs/${process.env.GITHUB_RUN_ID}/artifacts?name=github-pages`,
method: 'GET'
})
const twirpScope = nock(process.env.ACTIONS_RESULTS_URL)
.post(LIST_ARTIFACTS_TWIRP_PATH)
.reply(
200,
{
total_count: 1,
artifacts: [{ id: 11, name: `github-pages`, size_in_bytes: 221 }]
artifacts: [{ databaseId: 11, name: `github-pages`, size_in_bytes: 221 }]
},
{ headers: { 'content-type': 'application/json' } }
)
@@ -241,20 +236,18 @@ describe('Deployment', () => {
`Failed to create deployment (status: 500) with build version ${process.env.GITHUB_SHA}. Server error, is githubstatus.com reporting a Pages outage? Please re-run the deployment at a later time.`
)
)
twirpScope.done()
})
it('reports errors with an unexpected 403 during deployment', async () => {
process.env.GITHUB_SHA = 'build-version'
mockPool
.intercept({
path: `/repos/${process.env.GITHUB_REPOSITORY}/actions/runs/${process.env.GITHUB_RUN_ID}/artifacts?name=github-pages`,
method: 'GET'
})
const twirpScope = nock(process.env.ACTIONS_RESULTS_URL)
.post(LIST_ARTIFACTS_TWIRP_PATH)
.reply(
200,
{
total_count: 1,
artifacts: [{ id: 11, name: `github-pages`, size_in_bytes: 221 }]
artifacts: [{ databaseId: 11, name: `github-pages`, size_in_bytes: 221 }]
},
{ headers: { 'content-type': 'application/json' } }
)
@@ -284,20 +277,18 @@ describe('Deployment', () => {
`Failed to create deployment (status: 403) with build version ${process.env.GITHUB_SHA}. Ensure GITHUB_TOKEN has permission "pages: write".`
)
)
twirpScope.done()
})
it('reports errors with an unexpected 404 during deployment', async () => {
process.env.GITHUB_SHA = 'build-version'
mockPool
.intercept({
path: `/repos/${process.env.GITHUB_REPOSITORY}/actions/runs/${process.env.GITHUB_RUN_ID}/artifacts?name=github-pages`,
method: 'GET'
})
const twirpScope = nock(process.env.ACTIONS_RESULTS_URL)
.post(LIST_ARTIFACTS_TWIRP_PATH)
.reply(
200,
{
total_count: 1,
artifacts: [{ id: 11, name: `github-pages`, size_in_bytes: 221 }]
artifacts: [{ databaseId: 11, name: `github-pages`, size_in_bytes: 221 }]
},
{ headers: { 'content-type': 'application/json' } }
)
@@ -327,20 +318,18 @@ describe('Deployment', () => {
`Failed to create deployment (status: 404) with build version ${process.env.GITHUB_SHA}. Ensure GitHub Pages has been enabled: https://github.com/actions/is-awesome/settings/pages`
)
)
twirpScope.done()
})
it('reports errors with failed deployments', async () => {
process.env.GITHUB_SHA = 'invalid-build-version'
mockPool
.intercept({
path: `/repos/${process.env.GITHUB_REPOSITORY}/actions/runs/${process.env.GITHUB_RUN_ID}/artifacts?name=github-pages`,
method: 'GET'
})
const twirpScope = nock(process.env.ACTIONS_RESULTS_URL)
.post(LIST_ARTIFACTS_TWIRP_PATH)
.reply(
200,
{
total_count: 1,
artifacts: [{ id: 11, name: `github-pages`, size_in_bytes: 221 }]
artifacts: [{ databaseId: 11, name: `github-pages`, size_in_bytes: 221 }]
},
{ headers: { 'content-type': 'application/json' } }
)
@@ -370,31 +359,21 @@ describe('Deployment', () => {
`Failed to create deployment (status: 400) with build version ${process.env.GITHUB_SHA}. Responded with: Bad request`
)
)
twirpScope.done()
})
it('fails if there are multiple artifacts with the same name', async () => {
process.env.GITHUB_SHA = 'valid-build-version'
mockPool
.intercept({
path: `/repos/${process.env.GITHUB_REPOSITORY}/actions/runs/${process.env.GITHUB_RUN_ID}/artifacts?name=github-pages`,
method: 'GET'
})
const twirpScope = nock(process.env.ACTIONS_RESULTS_URL)
.post(LIST_ARTIFACTS_TWIRP_PATH)
.reply(
200,
{
total_count: 2,
total_count: 1,
artifacts: [
{
id: 13,
name: `github-pages`,
size_in_bytes: 1400
},
{
id: 14,
name: `github-pages`,
size_in_bytes: 1620
}
{ databaseId: 13, name: `github-pages`, size_in_bytes: 1400 },
{ databaseId: 14, name: `github-pages`, size_in_bytes: 1620 },
]
},
{ headers: { 'content-type': 'application/json' } }
@@ -402,18 +381,16 @@ describe('Deployment', () => {
const deployment = new Deployment()
await expect(deployment.create(fakeJwt)).rejects.toThrow(
`Multiple artifact unexpectedly found for workflow run ${process.env.GITHUB_RUN_ID}. Artifact count is 2.`
`Multiple artifacts unexpectedly found for this workflow run. Artifact count is 2.`
)
twirpScope.done()
})
it('fails if there are no artifacts found', async () => {
process.env.GITHUB_SHA = 'valid-build-version'
mockPool
.intercept({
path: `/repos/${process.env.GITHUB_REPOSITORY}/actions/runs/${process.env.GITHUB_RUN_ID}/artifacts?name=github-pages`,
method: 'GET'
})
const twirpScope = nock(process.env.ACTIONS_RESULTS_URL)
.post(LIST_ARTIFACTS_TWIRP_PATH)
.reply(
200,
{
@@ -425,40 +402,36 @@ describe('Deployment', () => {
const deployment = new Deployment()
await expect(deployment.create(fakeJwt)).rejects.toThrow(
`No artifacts found for workflow run ${process.env.GITHUB_RUN_ID}. Ensure artifacts are uploaded with actions/artifact@v4 or later.`
`No artifacts found for this workflow run. Ensure artifacts are uploaded with actions/artifact@v4 or later.`
)
twirpScope.done()
})
it('fails with error message if list artifact endpoint returns 500', async () => {
process.env.GITHUB_SHA = 'valid-build-version'
mockPool
.intercept({
path: `/repos/${process.env.GITHUB_REPOSITORY}/actions/runs/${process.env.GITHUB_RUN_ID}/artifacts?name=github-pages`,
method: 'GET'
})
const twirpScope = nock(process.env.ACTIONS_RESULTS_URL)
.post(LIST_ARTIFACTS_TWIRP_PATH)
.reply(500, { message: 'oh no' }, { headers: { 'content-type': 'application/json' } })
const deployment = new Deployment()
await expect(deployment.create(fakeJwt)).rejects.toThrow(
`Failed to create deployment (status: 500) with build version valid-build-version. Server error, is githubstatus.com reporting a Pages outage? Please re-run the deployment at a later time.`
)
twirpScope.done()
})
it('warns if the artifact size is bigger than maximum', async () => {
process.env.GITHUB_SHA = 'valid-build-version'
const artifactSize = ONE_GIGABYTE + 1
mockPool
.intercept({
path: `/repos/${process.env.GITHUB_REPOSITORY}/actions/runs/${process.env.GITHUB_RUN_ID}/artifacts?name=github-pages`,
method: 'GET'
})
const twirpScope = nock(process.env.ACTIONS_RESULTS_URL)
.post(LIST_ARTIFACTS_TWIRP_PATH)
.reply(
200,
{
total_count: 1,
artifacts: [{ id: 12, name: `github-pages`, size_in_bytes: artifactSize }]
artifacts: [{ databaseId: 12, name: `github-pages`, size_in_bytes: artifactSize }]
},
{ headers: { 'content-type': 'application/json' } }
)
@@ -500,21 +473,19 @@ describe('Deployment', () => {
expect(core.info).toHaveBeenLastCalledWith(
expect.stringMatching(new RegExp(`^Created deployment for ${process.env.GITHUB_SHA}`))
)
twirpScope.done()
})
it('warns when the timeout is greater than the maximum allowed', async () => {
process.env.GITHUB_SHA = 'valid-build-version'
mockPool
.intercept({
path: `/repos/${process.env.GITHUB_REPOSITORY}/actions/runs/${process.env.GITHUB_RUN_ID}/artifacts?name=github-pages`,
method: 'GET'
})
const twirpScope = nock(process.env.ACTIONS_RESULTS_URL)
.post(LIST_ARTIFACTS_TWIRP_PATH)
.reply(
200,
{
total_count: 1,
artifacts: [{ id: 11, name: `github-pages`, size_in_bytes: 221 }]
artifacts: [{ databaseId: 11, name: `github-pages`, size_in_bytes: 221 }]
},
{ headers: { 'content-type': 'application/json' } }
)
@@ -570,6 +541,7 @@ describe('Deployment', () => {
expect(core.warning).toBeCalledWith(
`Warning: timeout value is greater than the allowed maximum - timeout set to the maximum of ${MAX_TIMEOUT} milliseconds.`
)
twirpScope.done()
})
})
@@ -577,16 +549,13 @@ describe('Deployment', () => {
it('sets output to success when deployment is successful', async () => {
process.env.GITHUB_SHA = 'valid-build-version'
mockPool
.intercept({
path: `/repos/${process.env.GITHUB_REPOSITORY}/actions/runs/${process.env.GITHUB_RUN_ID}/artifacts?name=github-pages`,
method: 'GET'
})
const twirpScope = nock(process.env.ACTIONS_RESULTS_URL)
.post(LIST_ARTIFACTS_TWIRP_PATH)
.reply(
200,
{
total_count: 1,
artifacts: [{ id: 11, name: `github-pages`, size_in_bytes: 221 }]
artifacts: [{ databaseId: 11, name: `github-pages`, size_in_bytes: 221 }]
},
{ headers: { 'content-type': 'application/json' } }
)
@@ -634,6 +603,7 @@ describe('Deployment', () => {
expect(core.setOutput).toBeCalledWith('status', 'succeed')
expect(core.info).toHaveBeenLastCalledWith('Reported success!')
twirpScope.done()
})
it('fails check when no deployment is found', async () => {
@@ -646,16 +616,13 @@ describe('Deployment', () => {
it('exits early when deployment is not in progress', async () => {
process.env.GITHUB_SHA = 'valid-build-version'
mockPool
.intercept({
path: `/repos/${process.env.GITHUB_REPOSITORY}/actions/runs/${process.env.GITHUB_RUN_ID}/artifacts?name=github-pages`,
method: 'GET'
})
const twirpScope = nock(process.env.ACTIONS_RESULTS_URL)
.post(LIST_ARTIFACTS_TWIRP_PATH)
.reply(
200,
{
total_count: 1,
artifacts: [{ id: 11, name: `github-pages`, size_in_bytes: 221 }]
artifacts: [{ databaseId: 11, name: `github-pages`, size_in_bytes: 221 }]
},
{ headers: { 'content-type': 'application/json' } }
)
@@ -694,21 +661,19 @@ describe('Deployment', () => {
deployment.deploymentInfo.pending = false
await deployment.check()
expect(core.setFailed).toBeCalledWith('Unable to get deployment status.')
twirpScope.done()
})
it('enforces max timeout', async () => {
process.env.GITHUB_SHA = 'valid-build-version'
mockPool
.intercept({
path: `/repos/${process.env.GITHUB_REPOSITORY}/actions/runs/${process.env.GITHUB_RUN_ID}/artifacts?name=github-pages`,
method: 'GET'
})
const twirpScope = nock(process.env.ACTIONS_RESULTS_URL)
.post(LIST_ARTIFACTS_TWIRP_PATH)
.reply(
200,
{
total_count: 1,
artifacts: [{ id: 11, name: `github-pages`, size_in_bytes: 221 }]
artifacts: [{ databaseId: 11, name: `github-pages`, size_in_bytes: 221 }]
},
{ headers: { 'content-type': 'application/json' } }
)
@@ -793,21 +758,19 @@ describe('Deployment', () => {
expect(deployment.timeout).toEqual(MAX_TIMEOUT)
expect(core.error).toBeCalledWith('Timeout reached, aborting!')
expect(core.setFailed).toBeCalledWith('Timeout reached, aborting!')
twirpScope.done()
})
it('sets timeout to user timeout if user timeout is less than max timeout', async () => {
process.env.GITHUB_SHA = 'valid-build-version'
mockPool
.intercept({
path: `/repos/${process.env.GITHUB_REPOSITORY}/actions/runs/${process.env.GITHUB_RUN_ID}/artifacts?name=github-pages`,
method: 'GET'
})
const twirpScope = nock(process.env.ACTIONS_RESULTS_URL)
.post(LIST_ARTIFACTS_TWIRP_PATH)
.reply(
200,
{
total_count: 1,
artifacts: [{ id: 11, name: `github-pages`, size_in_bytes: 221 }]
artifacts: [{ databaseId: 11, name: `github-pages`, size_in_bytes: 221 }]
},
{ headers: { 'content-type': 'application/json' } }
)
@@ -881,21 +844,19 @@ describe('Deployment', () => {
expect(deployment.timeout).toEqual(42)
expect(core.error).toBeCalledWith('Timeout reached, aborting!')
expect(core.setFailed).toBeCalledWith('Timeout reached, aborting!')
twirpScope.done()
})
it('sets output to success when timeout is set but not reached', async () => {
process.env.GITHUB_SHA = 'valid-build-version'
mockPool
.intercept({
path: `/repos/${process.env.GITHUB_REPOSITORY}/actions/runs/${process.env.GITHUB_RUN_ID}/artifacts?name=github-pages`,
method: 'GET'
})
const twirpScope = nock(process.env.ACTIONS_RESULTS_URL)
.post(LIST_ARTIFACTS_TWIRP_PATH)
.reply(
200,
{
total_count: 1,
artifacts: [{ id: 11, name: `github-pages`, size_in_bytes: 221 }]
artifacts: [{ databaseId: 11, name: `github-pages`, size_in_bytes: 221 }]
},
{ headers: { 'content-type': 'application/json' } }
)
@@ -970,6 +931,7 @@ describe('Deployment', () => {
expect(core.error).not.toBeCalled()
expect(core.setOutput).toBeCalledWith('status', 'succeed')
expect(core.info).toHaveBeenLastCalledWith('Reported success!')
twirpScope.done()
})
})
@@ -977,16 +939,13 @@ describe('Deployment', () => {
it('can successfully cancel a deployment', async () => {
process.env.GITHUB_SHA = 'valid-build-version'
mockPool
.intercept({
path: `/repos/${process.env.GITHUB_REPOSITORY}/actions/runs/${process.env.GITHUB_RUN_ID}/artifacts?name=github-pages`,
method: 'GET'
})
const twirpScope = nock(process.env.ACTIONS_RESULTS_URL)
.post(LIST_ARTIFACTS_TWIRP_PATH)
.reply(
200,
{
total_count: 1,
artifacts: [{ id: 11, name: `github-pages`, size_in_bytes: 221 }]
artifacts: [{ databaseId: 11, name: `github-pages`, size_in_bytes: 221 }]
},
{ headers: { 'content-type': 'application/json' } }
)
@@ -1035,6 +994,7 @@ describe('Deployment', () => {
await deployment.cancel()
expect(core.info).toHaveBeenLastCalledWith(`Canceled deployment with ID ${process.env.GITHUB_SHA}`)
twirpScope.done()
})
it('can exit if a pages deployment was not created and none need to be cancelled', async () => {
@@ -1053,16 +1013,13 @@ describe('Deployment', () => {
it('catches an error when trying to cancel a deployment', async () => {
process.env.GITHUB_SHA = 'valid-build-version'
mockPool
.intercept({
path: `/repos/${process.env.GITHUB_REPOSITORY}/actions/runs/${process.env.GITHUB_RUN_ID}/artifacts?name=github-pages`,
method: 'GET'
})
const twirpScope = nock(process.env.ACTIONS_RESULTS_URL)
.post(LIST_ARTIFACTS_TWIRP_PATH)
.reply(
200,
{
total_count: 1,
artifacts: [{ id: 11, name: `github-pages`, size_in_bytes: 221 }]
artifacts: [{ databaseId: 11, name: `github-pages`, size_in_bytes: 221 }]
},
{ headers: { 'content-type': 'application/json' } }
)
@@ -1111,6 +1068,7 @@ describe('Deployment', () => {
await deployment.cancel()
expect(core.error).toHaveBeenCalledWith(`Canceling Pages deployment failed`, expect.anything())
twirpScope.done()
})
})
})