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

Module not found #1016

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
Open
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
5 changes: 5 additions & 0 deletions .github/workflows/build-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,11 @@ jobs:
nodeVersion: ['18.x', '20.x', '22.x']
steps:
- uses: actions/checkout@v4
- uses: actions/setup-java@v4
with:
distribution: 'temurin'
java-version: '17'
java-package: 'jre'
- uses: actions/setup-node@v4
with:
node-version: ${{ matrix.nodeVersion }}
Expand Down
2 changes: 1 addition & 1 deletion ci/_init.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export DRIVER_NAME=nodejs
BUILD_IMAGE_VERSION=1

# Test Images
TEST_IMAGE_VERSION=1
TEST_IMAGE_VERSION=2

declare -A BUILD_IMAGE_NAMES=(
[$DRIVER_NAME-chainguard-node18]=$DOCKER_REGISTRY_NAME/client-$DRIVER_NAME-chainguard-node18-build:$BUILD_IMAGE_VERSION
Expand Down
2 changes: 2 additions & 0 deletions ci/container/test_component.sh
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ else
export PATH=$WORKSPACE/node_modules/.bin:$PATH
fi
cp $SOURCE_ROOT/ci/container/package.json .

cat $SOURCE_ROOT/ci/container/package.json
npm install

PACKAGE_NAME=$(cd $WORKSPACE && ls snowflake-sdk*.tgz)
Expand Down
5 changes: 4 additions & 1 deletion ci/image/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@ FROM $IMAGE

USER root

RUN apk update && apk add python3 jq aws-cli gosu
RUN apk update && apk add python3 python3-dev py3-pip jq aws-cli gosu openjdk-17
RUN pip install -U snowflake-connector-python

ENV JAVA_HOME=/usr/lib/jvm/java-17-openjdk
ENV PATH="$PATH:/usr/lib/jvm/java-17-openjdk/bin"

# workspace
RUN mkdir -p /home/user && \
chmod 777 /home/user
Expand Down
2 changes: 1 addition & 1 deletion ci/test_authentication.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ docker run \
-v $(cd $THIS_DIR/.. && pwd):/mnt/host \
-v $WORKSPACE:/mnt/workspace \
--rm \
nexus.int.snowflakecomputing.com:8086/docker/snowdrivers-test-external-browser:3 \
nexus.int.snowflakecomputing.com:8086/docker/snowdrivers-test-external-browser:7 \
"/mnt/host/ci/container/test_authentication.sh"
3 changes: 3 additions & 0 deletions lib/connection/connection_config.js
Original file line number Diff line number Diff line change
Expand Up @@ -1000,15 +1000,18 @@ function createParameters() {
{
name: PARAM_RETRY_SF_MAX_NUM_RETRIES,
defaultValue: 1000,
external: true,
validate: isNonNegativeInteger
},
{
name: PARAM_RETRY_SF_STARTING_SLEEP_TIME,
defaultValue: 1,
external: true,
validate: isNonNegativeNumber
},
{
name: PARAM_RETRY_SF_MAX_SLEEP_TIME,
external: true,
defaultValue: 16,
validate: isNonNegativeNumber
}
Expand Down
5 changes: 2 additions & 3 deletions lib/connection/statement.js
Original file line number Diff line number Diff line change
Expand Up @@ -1486,9 +1486,8 @@ function sendSfRequest(statementContext, options, appendQueryParamOnRetry) {
// if we haven't exceeded the maximum number of retries yet and the server
// came back with a retryable error code
if (numRetries < maxNumRetries &&
err && Util.isRetryableHttpError(
err.response, false // no retry for HTTP 403
)) {
err && (Util.isRetryableHttpError(err.response, false) || Util.isNetworkError(err))
) {
// increment the retry count
numRetries++;
lastStatusCodeForRetry = err.response ? err.response.statusCode : 0;
Expand Down
4 changes: 4 additions & 0 deletions lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,10 @@ exports.isRetryableHttpError = function (response, retry403) {
(response.statusCode === 429));
};

exports.isNetworkError = function (err) {
return err.code === Errors.codes.ERR_SF_NETWORK_COULD_NOT_CONNECT;
};

exports.validateClientSessionKeepAliveHeartbeatFrequency = function (input, masterValidity) {
let heartbeatFrequency = input;
const realMax = Math.floor(masterValidity / 4);
Expand Down
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
"description": "Node.js driver for Snowflake",
"dependencies": {
"@aws-sdk/client-s3": "^3.726.0",
"@smithy/node-http-handler": "^4.0.1",
"@azure/storage-blob": "12.26.x",
"@google-cloud/storage": "^7.7.0",
"@smithy/node-http-handler": "^4.0.1",
"@techteamer/ocsp": "1.0.1",
"asn1.js-rfc2560": "^5.0.0",
"asn1.js-rfc5280": "^3.0.0",
Expand All @@ -32,7 +32,9 @@
"simple-lru-cache": "^0.0.2",
"toml": "^3.0.0",
"uuid": "^8.3.2",
"winston": "^3.1.0"
"winston": "^3.1.0",
"wiremock": "^3.10.0",
"wiremock-rest-client": "^1.11.0"
},
"devDependencies": {
"@aws-sdk/types": "^3.387.0",
Expand Down
12 changes: 12 additions & 0 deletions test/authentication/connectionParameters.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,17 @@ const keypairEncryptedPrivateKeyPath =
authenticator: 'SNOWFLAKE_JWT'
};

const wiremock =
{
...baseParameters,
accessUrl: null,
username: 'MOCK_USERNAME',
account: 'MOCK_ACCOUNT_NAME',
host: 'localhost',
protocol: 'http',
authenticator: 'OAUTH',
};

exports.externalBrowser = externalBrowser;
exports.okta = okta;
exports.oauth = oauth;
Expand All @@ -93,3 +104,4 @@ exports.snowflakeAuthTestOauthClientSecret = snowflakeAuthTestOauthClientSecret;
exports.snowflakeAuthTestOauthUrl = snowflakeAuthTestOauthUrl;
exports.snowflakeAuthTestPrivateKeyPath = snowflakeAuthTestPrivateKeyPath;
exports.snowflakeAuthTestInvalidPrivateKeyPath = snowflakeAuthTestInvalidPrivateKeyPath;
exports.wiremock = wiremock;
64 changes: 64 additions & 0 deletions test/integration/testConnectionRetries.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* Copyright (c) 2015-2024 Snowflake Computing Inc. All rights reserved.
*/

const { runWireMockAsync, addWireMockMappingsFromFile } = require('../wiremockRunner');
const connParameters = require('../authentication/connectionParameters');
const testUtil = require('../integration/testUtil');
const snowflake = require('../../lib/snowflake');
const assert = require('assert');

describe.only('Connection test', function () {
this.timeout(500000);
let port;
let wireMock;

before(async () => {
port = await testUtil.getFreePort();
wireMock = await runWireMockAsync(port);
snowflake.configure({
logLevel: 'DEBUG',
disableOCSPChecks: true
});
});

afterEach(async () => {
wireMock.scenarios.resetAllScenarios();
});

after(async () => {
await wireMock.global.shutdown();
});

it('Test retries after connection reset - success', async function () {
await addWireMockMappingsFromFile(wireMock, 'wiremock/mappings/six_reset_connection_and_correct_response.json');
const connectionOption = { ...connParameters.wiremock, password: 'MOCK_TOKEN', port: port, sfRetryMaxSleepTime: 2 };
const connection = testUtil.createConnection(connectionOption);
await testUtil.connectAsync(connection);
await assert.doesNotReject(async () => await testUtil.executeCmdAsync(connection, ' Select 1'));
});

it('Test retries after alformed response', async function () {
await addWireMockMappingsFromFile(wireMock, 'wiremock/mappings/six_malformed_and_correct.json');
const connectionOption = { ...connParameters.wiremock, password: 'MOCK_TOKEN', port: port, sfRetryMaxSleepTime: 2 };
const connection = testUtil.createConnection(connectionOption);
await testUtil.connectAsync(connection);
await assert.doesNotReject(async () => await testUtil.executeCmdAsync(connection, ' Select 1'));
});

it('Test retries after connection reset - fail', async function () {
await addWireMockMappingsFromFile(wireMock, 'wiremock/mappings/six_reset_connection_and_correct_response.json');
const connectionOption = { ...connParameters.wiremock, password: 'MOCK_TOKEN', port: port, sfRetryMaxNumRetries: 1, sfRetryMaxSleepTime: 2 };
const connection = testUtil.createConnection(connectionOption);
await testUtil.connectAsync(connection);
await assert.rejects(
testUtil.executeCmdAsync(connection, ' Select 1'),
(err) => {
assert.match(err.message, /Network error. Could not reach Snowflake./);
return true;
},
);
});
});


12 changes: 12 additions & 0 deletions test/integration/testUtil.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const crypto = require('crypto');
const Logger = require('../../lib/logger');
const path = require('path');
const os = require('os');
const net = require('net');

module.exports.createConnection = function (validConnectionOptionsOverride = {}, coreInstance) {
coreInstance = coreInstance || snowflake;
Expand Down Expand Up @@ -389,3 +390,14 @@ module.exports.isRequestCancelledError = function (error) {
assert.equal(error.name, 'CanceledError', `Expected error name "CanceledError", but received ${error.name}`);
assert.equal(error.code, 'ERR_CANCELED', `Expected error code "ERR_CANCELED", but received ${error.code}`);
};


module.exports.getFreePort = async function () {
return new Promise(res => {
const srv = net.createServer();
srv.listen(0, () => {
const port = srv.address().port;
srv.close(() => res(port));
});
});
};
33 changes: 33 additions & 0 deletions test/integration/wiremock/testWiremockRunner.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
const assert = require('assert');
const fs = require('fs');
const axios = require('axios');
const { runWireMockAsync } = require('../../wiremockRunner');
const os = require('os');
const testUtil = require('../testUtil');

if (os.platform !== 'win32') {
describe('Wiremock test', function () {
let port, wireMock;
before(async () => {
port = await testUtil.getFreePort();
wireMock = await runWireMockAsync(port);
});
after(async () => {
await wireMock.global.shutdown();
});
it('Run Wiremock instance, wait, verify connection and shutdown', async function () {
assert.doesNotReject(async () => await wireMock.mappings.getAllMappings());
});
it('Add mappings', async function () {
const requests = JSON.parse(fs.readFileSync('wiremock/mappings/testMapping.json', 'utf8'));
for (const mapping of requests.mappings) {
await wireMock.mappings.createMapping(mapping);
}
const mappings = await wireMock.mappings.getAllMappings();
assert.strictEqual(mappings.mappings.length, 2);
const response = await axios.get(`http://localhost:${port}/test/authorize.html`);
assert.strictEqual(response.status, 200);
});
});

}
58 changes: 58 additions & 0 deletions test/wiremockRunner.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
const { exec } = require('child_process');
const Logger = require('../lib/logger');
const fs = require('fs');
const WireMockRestClient = require('wiremock-rest-client').WireMockRestClient;


async function runWireMockAsync(port) {
let timeoutHandle;
const waitingWireMockPromise = new Promise( (resolve, reject) => {
try {
exec(`npx wiremock --enable-browser-proxying --proxy-pass-through false --port ${port} `);
const wireMock = new WireMockRestClient(`http://localhost:${port}`, { logLevel: 'debug' });
const readyWireMock = waitForWiremockStarted(wireMock);
resolve(readyWireMock);
} catch (err) {
reject(err);
}
});

const timeout = new Promise((resolve, reject) =>
timeoutHandle = setTimeout(
() => reject('Wiremock unavailable after 60s.'),
60000));
return Promise.race([waitingWireMockPromise, timeout])
.then(result => {
clearTimeout(timeoutHandle);
return result;
});
}

async function waitForWiremockStarted(wireMock) {
return fetch(wireMock.baseUri)
.then(async (resp) => {
if (resp.ok) {
return Promise.resolve(wireMock);
} else {
await new Promise(resolve => setTimeout(resolve, 1000));
Logger.getInstance().info(`Retry connection to WireMock after wrong response status: ${resp.status}`);
return await waitForWiremockStarted(wireMock);
}
})
.catch(async (err) => {
await new Promise(resolve => setTimeout(resolve, 1000));
Logger.getInstance().info(`Retry connection to WireMock after error: ${err}`);
return await waitForWiremockStarted(wireMock);
});
}

async function addWireMockMappingsFromFile(wireMock, filePath) {
const requests = JSON.parse(fs.readFileSync(filePath, 'utf8'));
for (const mapping of requests.mappings) {
await wireMock.mappings.createMapping(mapping);
}
}

exports.runWireMockAsync = runWireMockAsync;
exports.addWireMockMappingsFromFile = addWireMockMappingsFromFile;

Loading
Loading