WARNING: THIS SITE IS A MIRROR OF GITHUB.COM / IT CANNOT LOGIN OR REGISTER ACCOUNTS / THE CONTENTS ARE PROVIDED AS-IS / THIS SITE ASSUMES NO RESPONSIBILITY FOR ANY DISPLAYED CONTENT OR LINKS / IF YOU FOUND SOMETHING MAY NOT GOOD FOR EVERYONE, CONTACT ADMIN AT ilovescratch@foxmail.com
Skip to content

Commit 0d1f072

Browse files
authored
allowing container checks to fail gracefully (#19607) (#19612)
1 parent cca7f62 commit 0d1f072

File tree

2 files changed

+49
-2
lines changed

2 files changed

+49
-2
lines changed

src/objectExplorer/objectExplorerService.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ import { ConnectionNode } from "./nodes/connectionNode";
6363
import { ConnectionGroupNode } from "./nodes/connectionGroupNode";
6464
import { getConnectionDisplayName } from "../models/connectionInfo";
6565
import { AddLocalContainerConnectionTreeNode } from "../containerDeployment/addLocalContainerConnectionTreeNode";
66+
import { getErrorMessage } from "../utils/utils";
6667

6768
export interface CreateSessionResult {
6869
sessionId?: string;
@@ -741,8 +742,19 @@ export class ObjectExplorerService {
741742
TelemetryViews.ContainerDeployment,
742743
TelemetryActions.ConnectToContainer,
743744
);
744-
// start docker and docker container
745-
await restartContainer(connectionProfile.containerName);
745+
try {
746+
// start docker and docker container
747+
const alreadyRunning = await restartContainer(connectionProfile.containerName);
748+
this._logger.verbose(
749+
alreadyRunning
750+
? `Docker container "${connectionProfile.containerName}" is already running.`
751+
: `Docker container "${connectionProfile.containerName}" has been restarted.`,
752+
);
753+
} catch (error) {
754+
this._logger.error(
755+
`Error when attempting to ensure container "${connectionProfile.containerName}" is started. Attempting to proceed normally.\n\nError:\n${getErrorMessage(error)}`,
756+
);
757+
}
746758
}
747759
if (connectionProfile.connectionString) {
748760
if (connectionProfile.savePassword) {

test/unit/objectExplorerService.test.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ import {
4747
SessionCreatedParameters,
4848
} from "../../src/models/contracts/objectExplorer/createSessionRequest";
4949
import { ObjectExplorerUtils } from "../../src/objectExplorer/objectExplorerUtils";
50+
import * as DockerUtils from "../../src/containerDeployment/dockerUtils";
5051
import { FirewallService } from "../../src/firewall/firewallService";
5152
import { ConnectionCredentials } from "../../src/models/connectionCredentials";
5253
import providerSettings from "../../src/azure/providerSettings";
@@ -947,6 +948,40 @@ suite("OE Service Tests", () => {
947948
).to.be.false;
948949
});
949950

951+
test("prepareConnectionProfile should proceed if container connection throws when attempting to check container status", async () => {
952+
// Create a mock SQL Login profile with empty password but savePassword=true
953+
const mockProfile: IConnectionProfile = {
954+
id: "test-id",
955+
server: "testServer",
956+
database: "testDB",
957+
authenticationType: "SqlLogin",
958+
user: "testUser",
959+
password: "", // Empty password
960+
savePassword: true,
961+
containerName: "someContainer",
962+
} as IConnectionProfile;
963+
964+
// Setup connection store to return a saved password
965+
const savedPassword = generateUUID(); //random password
966+
mockConnectionStore.lookupPassword.resolves(savedPassword);
967+
968+
const containerStub = sinon
969+
.stub(DockerUtils, "restartContainer")
970+
.throws(new Error("Failed to restart container"));
971+
972+
// Call the method with the mock profile
973+
const result = await (objectExplorerService as any).prepareConnectionProfile(
974+
mockProfile,
975+
);
976+
977+
expect(containerStub.called, "Container restart should be attempted").to.be.true;
978+
979+
// Verify the result has the saved password
980+
expect(result.password, "Result password should match saved password").to.equal(
981+
savedPassword,
982+
);
983+
});
984+
950985
test("prepareConnectionProfile should prompt for password for SQL Login with no saved password", async () => {
951986
// Create a mock SQL Login profile with empty password and savePassword=false
952987
const mockProfile: IConnectionProfile = {

0 commit comments

Comments
 (0)