Skip to content

Commit

Permalink
fix test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
Rakhi Mundhada authored and Rakhi Mundhada committed Oct 17, 2024
1 parent 37d5f19 commit 9a5fb66
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 13 deletions.
19 changes: 9 additions & 10 deletions collectors/googlestackdriver/collector.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,17 +183,17 @@ class GooglestackdriverCollector extends PawsCollector {
}

generateFilter(state) {
const logIds = process.env.paws_collector_param_string_2 ? JSON.parse(process.env.paws_collector_param_string_2) : [];
const logNames = process.env.paws_collector_param_string_2 ? JSON.parse(process.env.paws_collector_param_string_2) : [];
const filterConditions = [];
let logFilterCondition;

if (logIds.length > 0) {
logIds.forEach(logId => {
const trimmedLogId = logId?.trim();
if (logNames.length > 0) {
logNames.forEach(logName => {
const trimmedlogName = logName?.trim();

if (trimmedLogId) {
const encodeLogId = this.isUriEncoded(logId) ? logId : encodeURIComponent(logId);
filterConditions.push(`logName:"${encodeLogId}"`);
if (trimmedlogName) {
const encodelogName = this.isUriEncoded(logName) ? logName : encodeURIComponent(logName);
filterConditions.push(`logName:"${encodelogName}"`);
} else {
AlLogger.warn("Skipping empty log ID.");
}
Expand All @@ -204,16 +204,15 @@ class GooglestackdriverCollector extends PawsCollector {
}
// Construct the basic timestamp filter
let filterQuery = `timestamp >= "${state.since}" AND timestamp < "${state.until}"`;

if (logFilterCondition) {
// Combine the LogName and timesamp filter
filterQuery = `${filterQuery} AND (${logFilterCondition})`;
}
return filterQuery;
}

isUriEncoded(logId) {
return decodeURIComponent(logId) !== logId;
isUriEncoded(logName) {
return decodeURIComponent(logName) !== logName;
}


Expand Down
5 changes: 2 additions & 3 deletions collectors/googlestackdriver/test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -555,15 +555,15 @@ describe('Unit Tests', function() {
const startDate = moment().subtract(20, 'minutes');
let since = startDate.toISOString();
let until = startDate.add(collector.pollInterval, 'seconds').toISOString();
process.env.paws_collector_param_string_2 = "[\"cloudaudit.googleapis.com%2Factivity\",\"\",\"cloudfunctions.googleapis.com%2Fcloud-functions\"]";
process.env.paws_collector_param_string_2 = "[\"cloudaudit.googleapis.com%2Factivity\",\"\",\"cloudfunctions.googleapis.com/cloud-functions\"]";
const curState = {
since: since,
until: until,
poll_interval_sec: 1,
stream: 'projects/imran-49253',
};
// Expected filter string
const expectedFilter = `timestamp >= "${since}" AND timestamp < "${until}" AND (logName="projects/imran-49253/logs/cloudaudit.googleapis.com%2Factivity" OR logName="projects/imran-49253/logs/cloudfunctions.googleapis.com%2Fcloud-functions")`;
const expectedFilter = `timestamp >= "${since}" AND timestamp < "${until}" AND (logName:"cloudaudit.googleapis.com%2Factivity" OR logName:"cloudfunctions.googleapis.com%2Fcloud-functions")`;

// Call the function to generate the filter
const filter = collector.generateFilter(curState);
Expand Down Expand Up @@ -622,7 +622,6 @@ describe('Unit Tests', function() {
const startDate = moment().subtract(20, 'minutes');
let since = startDate.toISOString();
let until = startDate.add(collector.pollInterval, 'seconds').toISOString();
process.env.paws_collector_param_string_2 = null;
const curState = {
since: since,
until: until,
Expand Down

0 comments on commit 9a5fb66

Please sign in to comment.