-
-
Notifications
You must be signed in to change notification settings - Fork 203
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reuse PortForwarder container if it is running (#680)
- Loading branch information
1 parent
e2dd217
commit d593d47
Showing
5 changed files
with
229 additions
and
59 deletions.
There are no files selected for viewing
This file contains 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 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 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 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,8 +1,34 @@ | ||
import { PortForwarderInstance } from "./port-forwarder/port-forwarder"; | ||
import { getContainerRuntimeClient } from "./container-runtime"; | ||
import { log } from "./common"; | ||
|
||
export class TestContainers { | ||
public static async exposeHostPorts(...ports: number[]): Promise<void> { | ||
const portForwarder = await PortForwarderInstance.getInstance(); | ||
await Promise.all(ports.map((port) => portForwarder.exposeHostPort(port))); | ||
|
||
await Promise.all( | ||
ports.map((port) => | ||
portForwarder.exposeHostPort(port).catch(async (err) => { | ||
if (await this.isHostPortExposed(portForwarder.getContainerId(), port)) { | ||
log.debug(`Host port ${port} is already exposed`); | ||
} else { | ||
throw err; | ||
} | ||
}) | ||
) | ||
); | ||
} | ||
|
||
private static async isHostPortExposed(portForwarderContainerId: string, hostPort: number): Promise<boolean> { | ||
const client = await getContainerRuntimeClient(); | ||
const container = client.container.getById(portForwarderContainerId); | ||
|
||
const { exitCode } = await client.container.exec(container, [ | ||
"sh", | ||
"-c", | ||
`netstat -tl | grep ${hostPort} | grep LISTEN`, | ||
]); | ||
|
||
return exitCode === 0; | ||
} | ||
} |
This file contains 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