-
-
Notifications
You must be signed in to change notification settings - Fork 11.2k
Integrate local E2E test with yarn dev:forward #25552
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
Open
jonatansberg
wants to merge
2
commits into
main
Choose a base branch
from
ber-3077-integrate-e2e-yarn-dev-forward
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,128 @@ | ||
| import Docker from 'dockerode'; | ||
| import baseDebug from '@tryghost/debug'; | ||
| import logging from '@tryghost/logging'; | ||
| import {DEV_ENVIRONMENT} from './constants'; | ||
| import {DevGhostManager} from './service-managers/dev-ghost-manager'; | ||
| import {DockerCompose} from './docker-compose'; | ||
| import {GhostInstance, MySQLManager} from './service-managers'; | ||
| import {randomUUID} from 'crypto'; | ||
|
|
||
| const debug = baseDebug('e2e:DevEnvironmentManager'); | ||
|
|
||
| /** | ||
| * Orchestrates e2e test environment when dev infrastructure is available. | ||
| * | ||
| * Uses: | ||
| * - MySQLManager with DockerCompose pointing to ghost-dev project | ||
| * - DevGhostManager for Ghost/Gateway container lifecycle | ||
| * | ||
| * All e2e containers use the 'ghost-dev-e2e' project namespace for easy cleanup. | ||
| */ | ||
| export class DevEnvironmentManager { | ||
| private readonly workerIndex: number; | ||
| private readonly dockerCompose: DockerCompose; | ||
| private readonly mysql: MySQLManager; | ||
| private readonly ghost: DevGhostManager; | ||
| private initialized = false; | ||
|
|
||
| constructor() { | ||
| this.workerIndex = parseInt(process.env.TEST_PARALLEL_INDEX || '0', 10); | ||
|
|
||
| // Use DockerCompose pointing to ghost-dev project to find MySQL container | ||
| this.dockerCompose = new DockerCompose({ | ||
| composeFilePath: '', // Not needed for container lookup | ||
| projectName: 'ghost-dev', | ||
| docker: new Docker() | ||
| }); | ||
| this.mysql = new MySQLManager(this.dockerCompose); | ||
| this.ghost = new DevGhostManager({ | ||
| ...DEV_ENVIRONMENT, | ||
| workerIndex: this.workerIndex | ||
| }); | ||
| } | ||
|
|
||
| /** | ||
| * Global setup - creates database snapshot for test isolation. | ||
| * Mirrors the standalone environment: run migrations, then snapshot. | ||
| */ | ||
| async globalSetup(): Promise<void> { | ||
| logging.info('Starting dev environment global setup...'); | ||
|
|
||
| await this.cleanupResources(); | ||
|
|
||
| // Create base database, run migrations, then snapshot | ||
| // This mirrors what docker-compose does with ghost-migrations service | ||
| await this.mysql.recreateBaseDatabase('ghost_e2e_base'); | ||
| await this.ghost.runMigrations('ghost_e2e_base'); | ||
| await this.mysql.createSnapshot('ghost_e2e_base'); | ||
|
|
||
| logging.info('Dev environment global setup complete'); | ||
| } | ||
|
|
||
| /** | ||
| * Global teardown - cleanup resources. | ||
| */ | ||
| async globalTeardown(): Promise<void> { | ||
| if (this.shouldPreserveEnvironment()) { | ||
| logging.info('PRESERVE_ENV is set - skipping teardown'); | ||
| return; | ||
| } | ||
|
|
||
| logging.info('Starting dev environment global teardown...'); | ||
| await this.cleanupResources(); | ||
| logging.info('Dev environment global teardown complete'); | ||
| } | ||
|
|
||
| /** | ||
| * Per-test setup - creates containers on first call, then clones database and restarts Ghost. | ||
| */ | ||
| async perTestSetup(options: {config?: unknown} = {}): Promise<GhostInstance> { | ||
| // Lazy initialization of Ghost containers (once per worker) | ||
| if (!this.initialized) { | ||
| debug('Initializing Ghost containers for worker', this.workerIndex); | ||
| await this.ghost.setup(); | ||
| this.initialized = true; | ||
| } | ||
|
|
||
| const siteUuid = randomUUID(); | ||
| const instanceId = `ghost_e2e_${siteUuid.replace(/-/g, '_')}`; | ||
|
|
||
| // Setup database | ||
| await this.mysql.setupTestDatabase(instanceId, siteUuid); | ||
|
|
||
| // Restart Ghost with new database | ||
| const extraConfig = options.config as Record<string, string> | undefined; | ||
| await this.ghost.restartWithDatabase(instanceId, extraConfig); | ||
| await this.ghost.waitForReady(); | ||
|
|
||
| const port = this.ghost.getGatewayPort(); | ||
|
|
||
| return { | ||
| containerId: this.ghost.ghostContainerId!, | ||
| instanceId, | ||
| database: instanceId, | ||
| port, | ||
| baseUrl: `http://localhost:${port}`, | ||
| siteUuid | ||
| }; | ||
| } | ||
|
|
||
| /** | ||
| * Per-test teardown - drops test database. | ||
| */ | ||
| async perTestTeardown(instance: GhostInstance): Promise<void> { | ||
| await this.mysql.cleanupTestDatabase(instance.database); | ||
| } | ||
|
|
||
| private async cleanupResources(): Promise<void> { | ||
| logging.info('Cleaning up e2e resources...'); | ||
| await this.ghost.cleanupAllContainers(); | ||
| await this.mysql.dropAllTestDatabases(); | ||
| await this.mysql.deleteSnapshot(); | ||
| logging.info('E2E resources cleaned up'); | ||
| } | ||
|
|
||
| private shouldPreserveEnvironment(): boolean { | ||
| return process.env.PRESERVE_ENV === 'true'; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| import Docker from 'dockerode'; | ||
| import baseDebug from '@tryghost/debug'; | ||
| import {DEV_ENVIRONMENT} from './constants'; | ||
| import {DevEnvironmentManager} from './dev-environment-manager'; | ||
| import {EnvironmentManager} from './environment-manager'; | ||
|
|
||
| const debug = baseDebug('e2e:EnvironmentFactory'); | ||
|
|
||
| // Cached manager instance (one per worker process) | ||
| let cachedManager: EnvironmentManager | DevEnvironmentManager | null = null; | ||
|
|
||
| /** | ||
| * Check if the dev environment (yarn dev:forward) is running. | ||
| * Detects by checking for the ghost_dev network and running MySQL container. | ||
| */ | ||
| export async function isDevEnvironmentAvailable(): Promise<boolean> { | ||
ibalosh marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| const docker = new Docker(); | ||
|
|
||
| try { | ||
| const networks = await docker.listNetworks({ | ||
| filters: {name: [DEV_ENVIRONMENT.networkName]} | ||
| }); | ||
|
|
||
| if (networks.length === 0) { | ||
| debug('Dev environment not available: network not found'); | ||
| return false; | ||
| } | ||
|
|
||
| const containers = await docker.listContainers({ | ||
| filters: { | ||
| name: [DEV_ENVIRONMENT.mysql.host], | ||
| status: ['running'] | ||
| } | ||
| }); | ||
|
|
||
| if (containers.length === 0) { | ||
| debug('Dev environment not available: MySQL container not running'); | ||
| return false; | ||
| } | ||
|
|
||
| debug('Dev environment is available'); | ||
| return true; | ||
| } catch (error) { | ||
| debug('Error checking dev environment:', error); | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Get the environment manager for this worker. | ||
| * Creates and caches a manager on first call, returns cached instance thereafter. | ||
| */ | ||
| export async function getEnvironmentManager(): Promise<EnvironmentManager | DevEnvironmentManager> { | ||
| if (!cachedManager) { | ||
| const useDevEnv = await isDevEnvironmentAvailable(); | ||
| cachedManager = useDevEnv ? new DevEnvironmentManager() : new EnvironmentManager(); | ||
| } | ||
| return cachedManager; | ||
| } | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,6 @@ | ||
| export * from './service-managers'; | ||
| export * from './environment-manager'; | ||
| export * from './dev-environment-manager'; | ||
| export * from './environment-factory'; | ||
| export * from './service-availability'; | ||
|
|
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To avoid confusion, these seem to make more sense to be a separate file, so we have something like:
we will have some duplicates, but cleaner separation between environment configurations and cleaner switch between them in future. At the moment, this looks confusing, with having configurations overlap in a single file and no clear shape, which will differ depending on file from which you are loading them