-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: split TD008 into two plugins; new one is TD010 (#477)
- Loading branch information
1 parent
88d3b3f
commit f77742d
Showing
30 changed files
with
369 additions
and
46 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
/* | ||
Copyright 2019-2024 Google LLC | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
https://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
const ruleId = require("../myUtil.js").getRuleId(), | ||
xpath = require("xpath"); | ||
|
||
const plugin = { | ||
ruleId, | ||
name: "TargetEndpoint HTTPTargetConnection LoadBalancer with multiple fallback entries", | ||
message: "Multiple Server entries with IsFallback=true", | ||
fatal: false, | ||
severity: 1, // 1 = warn, 2 = error | ||
nodeType: "Endpoint", | ||
enabled: true | ||
}; | ||
|
||
const path = require("path"), | ||
debug = require("debug")("apigeelint:" + ruleId); | ||
|
||
const onTargetEndpoint = function (endpoint, cb) { | ||
const htc = endpoint.getHTTPTargetConnection(), | ||
shortFilename = path.basename(endpoint.getFileName()); | ||
let flagged = false; | ||
|
||
debug(`onTargetEndpoint shortfile(${shortFilename})`); | ||
if (htc) { | ||
try { | ||
const loadBalancers = htc.select("LoadBalancer"); | ||
debug(`loadBalancers(${loadBalancers.length})`); | ||
|
||
if (loadBalancers.length == 1) { | ||
const loadBalancer = loadBalancers[0]; | ||
// check multiple fallbacks | ||
const fallbacks = xpath.select( | ||
"Server[IsFallback = 'true']", | ||
loadBalancer | ||
); | ||
if (fallbacks.length > 1) { | ||
endpoint.addMessage({ | ||
plugin, | ||
line: loadBalancers[0].lineNumber, | ||
column: loadBalancers[0].columnNumber, | ||
message: plugin.message | ||
}); | ||
flagged = true; | ||
} | ||
const servers = xpath.select("Server", loadBalancer); | ||
if (servers.length == 1 && fallbacks.length == 1) { | ||
endpoint.addMessage({ | ||
plugin, | ||
line: loadBalancers[0].lineNumber, | ||
column: loadBalancers[0].columnNumber, | ||
message: | ||
"Only one server in a Load balancer; should not be marked IsFallback" | ||
}); | ||
flagged = true; | ||
} | ||
} | ||
} catch (exc1) { | ||
console.error("exception: " + exc1); | ||
debug(`onTargetEndpoint exc(${exc1})`); | ||
debug(`${exc1.stack}`); | ||
endpoint.addMessage({ | ||
plugin, | ||
message: "Exception while processing: " + exc1 | ||
}); | ||
|
||
flagged = true; | ||
} | ||
} | ||
|
||
if (typeof cb == "function") { | ||
debug(`onTargetEndpoint isFlagged(${flagged})`); | ||
cb(null, flagged); | ||
} | ||
}; | ||
|
||
module.exports = { | ||
plugin, | ||
onTargetEndpoint | ||
}; |
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
File renamed without changes.
This file was deleted.
Oops, something went wrong.
14 changes: 14 additions & 0 deletions
14
test/fixtures/resources/TD008/fail/t2-multiple-fallbacks-non-consecutive.xml
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 |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<TargetEndpoint name="default"> | ||
<HTTPTargetConnection> | ||
<LoadBalancer> | ||
<!-- two servers marked fallback will fail the TD008 check --> | ||
<Server name="server1"> | ||
<IsFallback>true</IsFallback> | ||
</Server> | ||
<Server name="server2"/> | ||
<Server name="server3"> | ||
<IsFallback>true</IsFallback> | ||
</Server> | ||
</LoadBalancer> | ||
</HTTPTargetConnection> | ||
</TargetEndpoint> |
9 changes: 9 additions & 0 deletions
9
test/fixtures/resources/TD008/fail/t3-one-server-marked-as-fallback.xml
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 |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<TargetEndpoint name="default"> | ||
<HTTPTargetConnection> | ||
<LoadBalancer> | ||
<Server name="server1"> | ||
<IsFallback>true</IsFallback> | ||
</Server> | ||
</LoadBalancer> | ||
</HTTPTargetConnection> | ||
</TargetEndpoint> |
This file was deleted.
Oops, something went wrong.
File renamed without changes.
File renamed without changes.
7 changes: 7 additions & 0 deletions
7
test/fixtures/resources/TD008/pass/t3-one-server-no-fallbacks.xml
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 |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<TargetEndpoint name="default"> | ||
<HTTPTargetConnection> | ||
<LoadBalancer> | ||
<Server name="server1"/> | ||
</LoadBalancer> | ||
</HTTPTargetConnection> | ||
</TargetEndpoint> |
This file was deleted.
Oops, something went wrong.
File renamed without changes.
File renamed without changes.
13 changes: 13 additions & 0 deletions
13
test/fixtures/resources/TD008/pass/t6-many-servers-some-dupes-no-fallbacks.xml
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 |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<TargetEndpoint name="default"> | ||
<HTTPTargetConnection> | ||
<LoadBalancer> | ||
<!-- this should fail the TD010 check, but pass the TD008 check --> | ||
<Server name="server1"/> | ||
<Server name="server2"/> | ||
<Server name="server3"/> | ||
<Server name="server2"/> | ||
<Server name="server4"/> | ||
<Server name="server2"/> | ||
</LoadBalancer> | ||
</HTTPTargetConnection> | ||
</TargetEndpoint> |
10 changes: 10 additions & 0 deletions
10
test/fixtures/resources/TD008/pass/t7-three-servers-with-duplicate-no-fallbacks.xml
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 |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<TargetEndpoint name="default"> | ||
<HTTPTargetConnection> | ||
<LoadBalancer> | ||
<!-- this should fail the TD010 check, but pass the TD008 check --> | ||
<Server name="server1"/> | ||
<Server name="server2"/> | ||
<Server name="server1"/> | ||
</LoadBalancer> | ||
</HTTPTargetConnection> | ||
</TargetEndpoint> |
9 changes: 9 additions & 0 deletions
9
test/fixtures/resources/TD008/pass/t8-two-servers-duplicate-no-fallback.xml
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 |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<TargetEndpoint name="default"> | ||
<HTTPTargetConnection> | ||
<LoadBalancer> | ||
<!-- this should fail the TD010 check, but pass the TD008 check --> | ||
<Server name="server1"/> | ||
<Server name="server1"/> | ||
</LoadBalancer> | ||
</HTTPTargetConnection> | ||
</TargetEndpoint> |
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 |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<TargetEndpoint name="default"> | ||
<HTTPTargetConnection> | ||
<LoadBalancer> | ||
<!-- no Server element at all --> | ||
<Swerve/> | ||
</LoadBalancer> | ||
</HTTPTargetConnection> | ||
</TargetEndpoint> |
10 changes: 10 additions & 0 deletions
10
test/fixtures/resources/TD010/fail/t1-missing-name-attr.xml
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 |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<TargetEndpoint name="default"> | ||
<HTTPTargetConnection> | ||
<LoadBalancer> | ||
<Server name="server1"/> | ||
<!-- TD010 should flag the missing name attribute --> | ||
<Server /> | ||
<Server name="server3"/> | ||
</LoadBalancer> | ||
</HTTPTargetConnection> | ||
</TargetEndpoint> |
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 |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<TargetEndpoint name="default"> | ||
<HTTPTargetConnection> | ||
<LoadBalancer> | ||
<!-- a duplicate server name - this should fail the TD010 check --> | ||
<Server name="server1"/> | ||
<Server name="server1"/> | ||
</LoadBalancer> | ||
</HTTPTargetConnection> | ||
</TargetEndpoint> |
10 changes: 10 additions & 0 deletions
10
test/fixtures/resources/TD010/fail/t3-non-consecutive-duplicate.xml
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 |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<TargetEndpoint name="default"> | ||
<HTTPTargetConnection> | ||
<LoadBalancer> | ||
<!-- this should fail the TD010 check --> | ||
<Server name="server1"/> | ||
<Server name="server2"/> | ||
<Server name="server1"/> | ||
</LoadBalancer> | ||
</HTTPTargetConnection> | ||
</TargetEndpoint> |
13 changes: 13 additions & 0 deletions
13
test/fixtures/resources/TD010/fail/t4-dupes-in-random-order.xml
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 |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<TargetEndpoint name="default"> | ||
<HTTPTargetConnection> | ||
<LoadBalancer> | ||
<!-- this should fail the TD010 check, but pass the TD008 check --> | ||
<Server name="server1"/> | ||
<Server name="server2"/> | ||
<Server name="server3"/> | ||
<Server name="server2"/> | ||
<Server name="server4"/> | ||
<Server name="server2"/> | ||
</LoadBalancer> | ||
</HTTPTargetConnection> | ||
</TargetEndpoint> |
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 |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<TargetEndpoint name="default"> | ||
<HTTPTargetConnection> | ||
<LoadBalancer> | ||
<!-- no TD010 problem here, these are not duplicates --> | ||
<Server name="server1"/> | ||
<Server name="server2"/> | ||
</LoadBalancer> | ||
</HTTPTargetConnection> | ||
</TargetEndpoint> |
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 |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<TargetEndpoint name="default"> | ||
<HTTPTargetConnection> | ||
<LoadBalancer> | ||
<!-- no TD010 problem here, just one server --> | ||
<Server name="server1"/> | ||
</LoadBalancer> | ||
</HTTPTargetConnection> | ||
</TargetEndpoint> |
12 changes: 12 additions & 0 deletions
12
test/fixtures/resources/TD010/pass/t4-three-servers-with-fallback.xml
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 |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<TargetEndpoint name="default"> | ||
<HTTPTargetConnection> | ||
<LoadBalancer> | ||
<Server name="server1"/> | ||
<Server name="server2"/> | ||
<!-- single fallback is OK --> | ||
<Server name="server3"> | ||
<IsFallback>true</IsFallback> | ||
</Server> | ||
</LoadBalancer> | ||
</HTTPTargetConnection> | ||
</TargetEndpoint> |
14 changes: 14 additions & 0 deletions
14
test/fixtures/resources/TD010/pass/t5-three-servers-multiple-fallbacks.xml
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 |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<TargetEndpoint name="default"> | ||
<HTTPTargetConnection> | ||
<LoadBalancer> | ||
<Server name="server1"/> | ||
<!-- two servers marked fallback will fail the TD008 check, but pass the TD010 check --> | ||
<Server name="server2"> | ||
<IsFallback>true</IsFallback> | ||
</Server> | ||
<Server name="server3"> | ||
<IsFallback>true</IsFallback> | ||
</Server> | ||
</LoadBalancer> | ||
</HTTPTargetConnection> | ||
</TargetEndpoint> |
14 changes: 14 additions & 0 deletions
14
test/fixtures/resources/TD010/pass/t6-three-servers-multiple-fallbacks.xml
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 |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<TargetEndpoint name="default"> | ||
<HTTPTargetConnection> | ||
<LoadBalancer> | ||
<!-- two servers marked fallback will fail the TD008 check, but pass the TD010 check --> | ||
<Server name="server1"> | ||
<IsFallback>true</IsFallback> | ||
</Server> | ||
<Server name="server2"/> | ||
<Server name="server3"> | ||
<IsFallback>true</IsFallback> | ||
</Server> | ||
</LoadBalancer> | ||
</HTTPTargetConnection> | ||
</TargetEndpoint> |
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
Oops, something went wrong.