Skip to content

Commit e76ce39

Browse files
committed
lib: only use gRPC to connect to SaaS
For testing, allow saas tokens with `testing` string.
1 parent 240a1cf commit e76ce39

35 files changed

+614
-580
lines changed

agents/grpc/src/grpc_agent.cc

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1034,7 +1034,7 @@ int GrpcAgent::config(const json& config) {
10341034
auto insecure_str =
10351035
per_process::system_environment->Get(kNSOLID_GRPC_INSECURE);
10361036
// Only parse the insecure flag in non SaaS mode.
1037-
if (!saas_ && insecure_str.has_value()) {
1037+
if (insecure_str.has_value() && (!saas_ || saas_->testing)) {
10381038
// insecure = std::stoull(insecure_str.value());
10391039
insecure = std::stoi(insecure_str.value());
10401040
}
@@ -1666,11 +1666,28 @@ void GrpcAgent::parse_saas_token(const std::string& token) {
16661666
return;
16671667
}
16681668

1669+
std::string endpoint;
1670+
bool is_testing = false;
16691671
bool is_staging = token.find("staging") != std::string::npos;
1670-
std::string endpoint = is_staging ?
1671-
console_id + ".grpc.staging.nodesource.io:443" :
1672-
console_id + ".grpc.nodesource.io:443";
1673-
saas_ = std::make_unique<SaaSInfo>(SaaSInfo{token, std::move(endpoint)});
1672+
if (is_staging) {
1673+
endpoint = console_id + ".grpc.staging.nodesource.io:443";
1674+
} else {
1675+
is_testing = token.find("testing") != std::string::npos;
1676+
if (is_testing) {
1677+
// For testing, set endpoint to the string after the last dot in the token
1678+
size_t last_dot = token.rfind('.');
1679+
if (last_dot != std::string::npos && last_dot + 1 < token.size()) {
1680+
endpoint = token.substr(last_dot + 1);
1681+
} else {
1682+
endpoint = "localhost:50051"; // fallback if no dot is found
1683+
}
1684+
} else {
1685+
endpoint = console_id + ".grpc.nodesource.io:443";
1686+
}
1687+
}
1688+
1689+
saas_ = std::make_unique<SaaSInfo>(
1690+
SaaSInfo{token, std::move(endpoint), is_testing});
16741691
}
16751692

16761693
bool GrpcAgent::pending_profiles() const {

agents/grpc/src/grpc_agent.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ class GrpcAgent: public std::enable_shared_from_this<GrpcAgent>,
145145
struct SaaSInfo {
146146
std::string token;
147147
std::string endpoint;
148+
bool testing;
148149
};
149150

150151
GrpcAgent();

lib/nsolid.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -715,7 +715,7 @@ function updateConfig(config = {}) {
715715

716716
if (nsolidConfig.saas) {
717717
if (!config.command) {
718-
nsolidConfig.command = undefined;
718+
nsolidConfig.command = null;
719719
}
720720

721721
if (nsolidConfig.command) {
@@ -724,8 +724,6 @@ function updateConfig(config = {}) {
724724
const url = parseSaasEnvVar(nsolidConfig.saas, 0);
725725
if (!url) {
726726
nsolidConfig.saas = undefined;
727-
} else if (!nsolidConfig.grpc) {
728-
nsolidConfig.command = url;
729727
} else {
730728
nsolidConfig.grpc = '' + config.grpc;
731729
}

test/agents/test-grpc-basic.mjs

Lines changed: 34 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ const tests = [];
1313

1414
tests.push({
1515
name: 'should work if agent is killed with signal',
16-
test: async () => {
16+
test: async (getEnv) => {
1717
return new Promise((resolve) => {
1818
const grpcServer = new GRPCServer();
1919
grpcServer.start(mustSucceed(async (port) => {
@@ -23,11 +23,7 @@ tests.push({
2323
resolve();
2424
}));
2525

26-
const env = {
27-
NODE_DEBUG_NATIVE: 'nsolid_grpc_agent',
28-
NSOLID_GRPC_INSECURE: 1,
29-
NSOLID_GRPC: `localhost:${port}`,
30-
};
26+
const env = getEnv(port);
3127

3228
const opts = {
3329
stdio: ['inherit', 'inherit', 'inherit', 'ipc'],
@@ -43,7 +39,7 @@ tests.push({
4339

4440
tests.push({
4541
name: 'should work if agent exits gracefully without error',
46-
test: async () => {
42+
test: async (getEnv) => {
4743
return new Promise((resolve) => {
4844
const grpcServer = new GRPCServer();
4945
grpcServer.start(mustSucceed(async (port) => {
@@ -53,11 +49,7 @@ tests.push({
5349
resolve();
5450
}));
5551

56-
const env = {
57-
NODE_DEBUG_NATIVE: 'nsolid_grpc_agent',
58-
NSOLID_GRPC_INSECURE: 1,
59-
NSOLID_GRPC: `localhost:${port}`,
60-
};
52+
const env = getEnv(port);
6153

6254
const opts = {
6355
stdio: ['inherit', 'inherit', 'inherit', 'ipc'],
@@ -76,7 +68,7 @@ tests.push({
7668

7769
tests.push({
7870
name: 'should work if agent exits gracefully with error code',
79-
test: async () => {
71+
test: async (getEnv) => {
8072
return new Promise((resolve) => {
8173
const grpcServer = new GRPCServer();
8274
grpcServer.start(mustSucceed(async (port) => {
@@ -86,11 +78,7 @@ tests.push({
8678
resolve();
8779
}));
8880

89-
const env = {
90-
NODE_DEBUG_NATIVE: 'nsolid_grpc_agent',
91-
NSOLID_GRPC_INSECURE: 1,
92-
NSOLID_GRPC: `localhost:${port}`,
93-
};
81+
const env = getEnv(port);
9482

9583
const opts = {
9684
stdio: ['inherit', 'inherit', 'inherit', 'ipc'],
@@ -109,7 +97,7 @@ tests.push({
10997

11098
tests.push({
11199
name: 'should work if agent exits with exception',
112-
test: async () => {
100+
test: async (getEnv) => {
113101
return new Promise((resolve) => {
114102
const grpcServer = new GRPCServer();
115103
grpcServer.start(mustSucceed(async (port) => {
@@ -120,11 +108,7 @@ tests.push({
120108
resolve();
121109
}));
122110

123-
const env = {
124-
NODE_DEBUG_NATIVE: 'nsolid_grpc_agent',
125-
NSOLID_GRPC_INSECURE: 1,
126-
NSOLID_GRPC: `localhost:${port}`,
127-
};
111+
const env = getEnv(port);
128112

129113
const opts = {
130114
stdio: ['inherit', 'inherit', 'inherit', 'ipc'],
@@ -141,7 +125,30 @@ tests.push({
141125
},
142126
});
143127

144-
for (const { name, test } of tests) {
145-
console.log(`[basic] ${name}`);
146-
await test();
128+
const testConfigs = [
129+
{
130+
getEnv: (port) => {
131+
return {
132+
NODE_DEBUG_NATIVE: 'nsolid_grpc_agent',
133+
NSOLID_GRPC_INSECURE: 1,
134+
NSOLID_GRPC: `localhost:${port}`,
135+
};
136+
},
137+
},
138+
{
139+
getEnv: (port) => {
140+
return {
141+
NODE_DEBUG_NATIVE: 'nsolid_grpc_agent',
142+
NSOLID_GRPC_INSECURE: 1,
143+
NSOLID_SAAS: `aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbbbbbbbbbbbbtesting.localhost:${port}`,
144+
};
145+
},
146+
},
147+
];
148+
149+
for (const testConfig of testConfigs) {
150+
for (const { name, test } of tests) {
151+
console.log(`[basic] ${name}`);
152+
await test(testConfig.getEnv);
153+
}
147154
}

test/agents/test-grpc-blocked-loop.mjs

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ const tests = [];
144144

145145
tests.push({
146146
name: 'should work in the main thread',
147-
test: async () => {
147+
test: async (getEnv) => {
148148
return new Promise((resolve) => {
149149
const grpcServer = new GRPCServer();
150150
grpcServer.start(mustSucceed(async (port) => {
@@ -159,12 +159,7 @@ tests.push({
159159
resolve();
160160
}));
161161

162-
const env = {
163-
NODE_DEBUG_NATIVE: 'nsolid_grpc_agent',
164-
NSOLID_GRPC_INSECURE: 1,
165-
NSOLID_GRPC: `localhost:${port}`,
166-
NSOLID_BLOCKED_LOOP_THRESHOLD: 100,
167-
};
162+
const env = getEnv(port);
168163

169164
const opts = {
170165
stdio: ['inherit', 'inherit', 'inherit', 'ipc'],
@@ -180,7 +175,7 @@ tests.push({
180175

181176
tests.push({
182177
name: 'should work for workers',
183-
test: async () => {
178+
test: async (getEnv) => {
184179
return new Promise((resolve) => {
185180
const grpcServer = new GRPCServer();
186181
grpcServer.start(mustSucceed(async (port) => {
@@ -195,12 +190,7 @@ tests.push({
195190
resolve();
196191
}));
197192

198-
const env = {
199-
NODE_DEBUG_NATIVE: 'nsolid_grpc_agent',
200-
NSOLID_GRPC_INSECURE: 1,
201-
NSOLID_GRPC: `localhost:${port}`,
202-
NSOLID_BLOCKED_LOOP_THRESHOLD: 100,
203-
};
193+
const env = getEnv(port);
204194

205195
const opts = {
206196
stdio: ['inherit', 'inherit', 'inherit', 'ipc'],
@@ -216,7 +206,30 @@ tests.push({
216206
},
217207
});
218208

219-
for (const { name, test } of tests) {
220-
console.log(`blocked loop generation ${name}`);
221-
await test();
209+
const testConfigs = [
210+
{
211+
getEnv: (port) => {
212+
return {
213+
NODE_DEBUG_NATIVE: 'nsolid_grpc_agent',
214+
NSOLID_GRPC_INSECURE: 1,
215+
NSOLID_GRPC: `localhost:${port}`,
216+
};
217+
},
218+
},
219+
{
220+
getEnv: (port) => {
221+
return {
222+
NODE_DEBUG_NATIVE: 'nsolid_grpc_agent',
223+
NSOLID_GRPC_INSECURE: 1,
224+
NSOLID_SAAS: `aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbbbbbbbbbbbbtesting.localhost:${port}`,
225+
};
226+
},
227+
},
228+
];
229+
230+
for (const testConfig of testConfigs) {
231+
for (const { name, test } of tests) {
232+
console.log(`blocked loop generation ${name}`);
233+
await test(testConfig.getEnv);
234+
}
222235
}

0 commit comments

Comments
 (0)