Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: netlify edge deployment #1821

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions e2e/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { dockerDeployment } from './tests/docker'
import { DeploymentConfiguration } from './types'
import { env, getCommitId } from './utils'
import { vercelDeployment } from './tests/vercel'
import { netlifyDeployment } from './tests/netlify-edge'

const AVAILABLE_TEST_PLANS = {
'cf-worker': cloudFlareDeployment,
Expand All @@ -15,6 +16,7 @@ const AVAILABLE_TEST_PLANS = {
'aws-lambda': awsLambdaDeployment,
'vercel-function': vercelDeployment,
'docker-node-17': dockerDeployment('node:17.8.0-alpine3.14'),
'netlify-edge': netlifyDeployment,
}

async function main() {
Expand Down
9 changes: 5 additions & 4 deletions e2e/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@
"version": "0.0.0",
"license": "MIT",
"dependencies": {
"@pulumi/pulumi": "3.40.1",
"@pulumi/cloudflare": "4.12.1",
"@pulumi/azure-native": "1.78.0",
"@pulumi/awsx": "0.40.1",
"@pulumi/aws": "5.15.0",
"@pulumi/awsx": "0.40.1",
"@pulumi/azure-native": "1.78.0",
"@pulumi/cloudflare": "4.12.1",
"@pulumi/docker": "3.4.1",
"@pulumi/pulumi": "3.40.1",
"@types/node": "16.11.60",
"typescript": "4.8.4",
"ts-node": "10.9.1",
"ts-node": "10.9.1"
},
"scripts": {
Expand Down
159 changes: 159 additions & 0 deletions e2e/tests/netlify-edge.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
import * as pulumi from '@pulumi/pulumi'
import {
assertGraphiQL,
assertQuery,
env,
execPromise,
fsPromises,
waitForEndpoint,
} from '../utils'
import { DeploymentConfiguration } from '../types'

type ProviderInputs = {
name: string
files: {
file: string
data: string
}[]
}

type NetlifyDeploymentInputs = {
[K in keyof ProviderInputs]: pulumi.Input<ProviderInputs[K]>
}

class NetlifyProvider implements pulumi.dynamic.ResourceProvider {
private baseUrl = 'https://api.netlify.com/api/'
private authToken = env('NETLIFY_AUTH_TOKEN')

async delete(id: string) {
const response = await fetch(`${this.baseUrl}/v1/sites/${id}`, {
method: 'DELETE',
headers: {
Authorization: `Bearer ${this.authToken}`,
},
})

// https://open-api.netlify.com/#tag/site/operation/deleteSite
if (response.status !== 204) {
throw new Error(
`Failed to delete Netlify site deployment ${id}: invalid status code (${
response.status
}), body: ${await response.text()}`,
)
}
}

async create(inputs: ProviderInputs): Promise<pulumi.dynamic.CreateResult> {
// First we create a site
const createSiteResponse = await fetch(`${this.baseUrl}/v1/sites`, {
method: 'POST',
headers: {
'content-type': 'application/json',
Authorization: `Bearer ${this.authToken}`,
},
body: JSON.stringify({
name: inputs.name,
}),
})

// https://open-api.netlify.com/#tag/site/operation/createSite
if (createSiteResponse.status !== 201) {
throw new Error(
`Failed to create Netlify deployment: invalid status code (${
createSiteResponse.status
}), body: ${await createSiteResponse.text()}`,
)
}

const createResponseJson = await createSiteResponse.json()
const siteId = createResponseJson.id

const createDeploymentResponse = await fetch(
`${this.baseUrl}/v1/sites/${siteId}/deploys`,
{
method: 'POST',
headers: {
'content-type': 'application/json',
Authorization: `Bearer ${this.authToken}`,
},
body: JSON.stringify({
files: inputs.files,
}),
},
)

// https://open-api.netlify.com/#tag/deploy/operation/createSiteDeploy
if (createDeploymentResponse.status !== 200) {
throw new Error(
`Failed to create Netlify deployment: invalid status code (${
createDeploymentResponse.status
}), body: ${await createDeploymentResponse.text()}`,
)
}

const createDeploymentResponseJson = await createDeploymentResponse.json()

return {
id: createDeploymentResponseJson['site_id'],
outs: {
url: createDeploymentResponseJson['deploy_ssl_url'],
},
}
}
}

export class NetlifyDeployment extends pulumi.dynamic.Resource {
public readonly url: pulumi.Output<string>

constructor(
name: string,
props: NetlifyDeploymentInputs,
opts?: pulumi.CustomResourceOptions,
) {
super(
new NetlifyProvider(),
name,
{
url: undefined,
...props,
},
opts,
)
}
}

export const netlifyDeployment: DeploymentConfiguration<{
functionUrl: string
}> = {
prerequisites: async () => {
// Build and bundle the function
console.info('\t\tℹ️ Bundling the Netlify Function....')
await execPromise('yarn build', {
cwd: '../examples/netlify-edge',
})
},
program: async () => {
const deployment = new NetlifyDeployment('netlify-function', {
files: [
// {
// file: '/api/graphql.js',
// data: await fsPromises.readFile(
// '../examples/nextjs/dist/index.js',
// 'utf-8',
// ),
// },
],
name: `yoga-e2e-testing`,
})

return {
functionUrl: pulumi.interpolate`${deployment.url}/api/graphql`,
}
},
test: async ({ functionUrl }) => {
console.log(`ℹ️ Netlify Function deployed to URL: ${functionUrl.value}`)
// await waitForEndpoint(functionUrl.value, 5, 10000)
// await assertGraphiQL(functionUrl.value)
// await assertQuery(functionUrl.value)
},
}