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

Add Cloudflare Modules E2E test #1440

Merged
merged 4 commits into from
Sep 27, 2022
Merged
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
10 changes: 9 additions & 1 deletion .github/workflows/deployment-e2e.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,16 @@ jobs:
strategy:
fail-fast: false
matrix:
plan: ['cf-worker', 'azure-function', 'aws-lambda', 'vercel-function']
plan:
[
'cf-worker',
'cf-modules',
'azure-function',
'aws-lambda',
'vercel-function',
]
name: e2e / ${{ matrix.plan }}

runs-on: ubuntu-latest
if: github.event.pull_request.head.repo.full_name == github.repository
steps:
Expand Down
2 changes: 2 additions & 0 deletions e2e/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ import { LocalWorkspace } from '@pulumi/pulumi/automation'
import { awsLambdaDeployment } from './tests/aws-lambda'
import { azureFunctionDeployment } from './tests/azure-function'
import { cloudFlareDeployment } from './tests/cf-worker'
import { cfModulesDeployment } from './tests/cf-modules'
import { dockerDeployment } from './tests/docker'
import { DeploymentConfiguration } from './types'
import { env, getCommitId } from './utils'
import { vercelDeployment } from './tests/vercel'

const AVAILABLE_TEST_PLANS = {
'cf-worker': cloudFlareDeployment,
'cf-modules': cfModulesDeployment,
'azure-function': azureFunctionDeployment,
'aws-lambda': awsLambdaDeployment,
'vercel-function': vercelDeployment,
Expand Down
6 changes: 6 additions & 0 deletions e2e/tests/cf-modules.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { createCFDeployment } from './create-cf-deployment'

export const cfModulesDeployment = createCFDeployment(
'cloudflare-modules',
true,
)
81 changes: 2 additions & 79 deletions e2e/tests/cf-worker.ts
Original file line number Diff line number Diff line change
@@ -1,80 +1,3 @@
import { Stack } from '@pulumi/pulumi/automation'
import { DeploymentConfiguration } from '../types'
import * as cf from '@pulumi/cloudflare'
import {
assertGraphiQL,
assertQuery,
env,
execPromise,
fsPromises,
waitForEndpoint,
} from '../utils'
import * as pulumi from '@pulumi/pulumi'
import { version } from '@pulumi/cloudflare/package.json'
import { createCFDeployment } from './create-cf-deployment'

export const cloudFlareDeployment: DeploymentConfiguration<{
workerUrl: string
}> = {
prerequisites: async (stack: Stack) => {
console.info('\t\tℹ️ Installing Pulumi CF plugin...')
// Intall Pulumi CF Plugin
await stack.workspace.installPlugin('cloudflare', version, 'resource')

// Build and bundle the worker
console.info('\t\tℹ️ Bundling the CF Worker....')
await execPromise('yarn build', {
cwd: '../examples/service-worker',
})
},
config: async (stack: Stack) => {
// Configure the Pulumi environment with the CloudFlare credentials
// This will allow Pulummi program to just run without caring about secrets/configs.
// See: https://www.pulumi.com/registry/packages/cloudflare/installation-configuration/
await stack.setConfig('cloudflare:apiToken', {
value: env('CLOUDFLARE_API_TOKEN'),
})
await stack.setConfig('cloudflare:accountId', {
value: env('CLOUDFLARE_ACCOUNT_ID'),
})
},
program: async () => {
const stackName = pulumi.getStack()
const workerUrl = `e2e.graphql-yoga.com/${stackName}`

// Deploy CF script as Worker
const workerScript = new cf.WorkerScript('worker', {
content: await fsPromises.readFile(
'../examples/service-worker/dist/index.js',
'utf-8',
),
secretTextBindings: [
{
name: 'GRAPHQL_ROUTE',
text: `/${stackName}`,
},
{
name: 'DEBUG',
text: 'true',
},
],
name: stackName,
})

// Create a nice route for easy testing
new cf.WorkerRoute('worker-route', {
scriptName: workerScript.name,
pattern: workerUrl,
zoneId: env('CLOUDFLARE_ZONE_ID'),
})

return {
workerUrl: `https://${workerUrl}`,
}
},
test: async ({ workerUrl }) => {
console.log(`ℹ️ CloudFlare Worker deployed to URL: ${workerUrl.value}`)
await waitForEndpoint(workerUrl.value, 5, 10000)
await assertGraphiQL(workerUrl.value)
await assertQuery(workerUrl.value)
},
}
export const cloudFlareDeployment = createCFDeployment('service-worker')
86 changes: 86 additions & 0 deletions e2e/tests/create-cf-deployment.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import { Stack } from '@pulumi/pulumi/automation'
import { DeploymentConfiguration } from '../types'
import * as cf from '@pulumi/cloudflare'
import {
assertGraphiQL,
assertQuery,
env,
execPromise,
fsPromises,
waitForEndpoint,
} from '../utils'
import * as pulumi from '@pulumi/pulumi'
import { version } from '@pulumi/cloudflare/package.json'

export function createCFDeployment(
projectName: string,
isModule = false,
): DeploymentConfiguration<{
workerUrl: string
}> {
return {
prerequisites: async (stack: Stack) => {
console.info('\t\tℹ️ Installing Pulumi CF plugin...')
// Intall Pulumi CF Plugin
await stack.workspace.installPlugin('cloudflare', version, 'resource')

// Build and bundle the worker
console.info('\t\tℹ️ Bundling the CF Worker....')
await execPromise('yarn build', {
cwd: '../examples/' + projectName,
})
},
config: async (stack: Stack) => {
// Configure the Pulumi environment with the CloudFlare credentials
// This will allow Pulummi program to just run without caring about secrets/configs.
// See: https://www.pulumi.com/registry/packages/cloudflare/installation-configuration/
await stack.setConfig('cloudflare:apiToken', {
value: env('CLOUDFLARE_API_TOKEN'),
})
await stack.setConfig('cloudflare:accountId', {
value: env('CLOUDFLARE_ACCOUNT_ID'),
})
},
program: async () => {
const stackName = pulumi.getStack()
const workerUrl = `e2e.graphql-yoga.com/${stackName}`

// Deploy CF script as Worker
const workerScript = new cf.WorkerScript('worker', {
content: await fsPromises.readFile(
`../examples/${projectName}/dist/index.js`,
'utf-8',
),
module: isModule,
secretTextBindings: [
{
name: 'GRAPHQL_ROUTE',
text: `/${stackName}`,
},
{
name: 'DEBUG',
text: 'true',
},
],
name: stackName,
})

// Create a nice route for easy testing
new cf.WorkerRoute('worker-route', {
scriptName: workerScript.name,
pattern: workerUrl,
zoneId: env('CLOUDFLARE_ZONE_ID'),
})

return {
workerUrl: `https://${workerUrl}`,
}
},
test: async ({ workerUrl }) => {
console.log(`ℹ️ CloudFlare Worker deployed to URL: ${workerUrl.value}`)
await waitForEndpoint(workerUrl.value, 5, 10000)
await assertGraphiQL(workerUrl.value)
await assertQuery(workerUrl.value)
},
}
}
4 changes: 3 additions & 1 deletion examples/cloudflare-modules/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
// src/index.mjs
import { createServer } from '@graphql-yoga/common'

export default createServer()
const { fetch } = createServer()

export default { fetch }