Skip to content

Commit

Permalink
fix: upstream linter check either nodes or service discovery
Browse files Browse the repository at this point in the history
  • Loading branch information
bzp2010 committed Jul 26, 2024
1 parent d8c9bcc commit 90c793a
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 9 deletions.
20 changes: 11 additions & 9 deletions apps/cli/src/linter/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,15 +106,17 @@ const upstreamSchema = z
},
)
.optional(),
nodes: z.array(
z.object({
host: z.string(),
port: portSchema.optional(),
weight: z.number().int().min(0),
priority: z.number().default(0).optional(),
metadata: z.record(z.string(), z.any()).optional(),
}),
),
nodes: z
.array(
z.object({
host: z.string(),
port: portSchema.optional(),
weight: z.number().int().min(0),
priority: z.number().default(0).optional(),
metadata: z.record(z.string(), z.any()).optional(),
}),
)
.optional(),
scheme: z
.enum(['grpc', 'grpcs', 'http', 'https', 'tcp', 'tls', 'udp', 'kafka'])
.default('http')
Expand Down
74 changes: 74 additions & 0 deletions apps/cli/src/linter/specs/upstream.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import * as ADCSDK from '@api7/adc-sdk';

import { check } from '../';

describe('Upstream Linter', () => {
const cases = [
{
name: 'should check either nodes or discovery (neither)',
input: {
services: [
{
name: 'No_Node_And_Discovery',
upstream: {},
},
],
} as ADCSDK.Configuration,
expect: false,
errors: [
{
code: 'custom',
message:
'Upstream must either explicitly specify nodes or use service discovery and not both',
path: ['services', 0, 'upstream'],
},
],
},
{
name: 'should check either nodes or discovery (with nodes)',
input: {
services: [
{
name: 'No_Node_And_Discovery',
upstream: {
nodes: [
{
host: '1.1.1.1',
port: 443,
weight: 100,
},
],
},
},
],
} as ADCSDK.Configuration,
expect: true,
},
{
name: 'should check either nodes or discovery (with discovery and service name)',
input: {
services: [
{
name: 'No_Node_And_Discovery',
upstream: {
discovery_type: 'mock',
service_name: 'service_mock',
},
},
],
} as ADCSDK.Configuration,
expect: true,
},
];

// test cases runner
cases.forEach((item) => {
it(item.name, () => {
const result = check(item.input);
expect(result.success).toEqual(item.expect);
if (!item.expect) {
expect(result.error.errors).toEqual(item.errors);
}
});
});
});

0 comments on commit 90c793a

Please sign in to comment.