Skip to content

Commit

Permalink
Fix some minor test issues raised by CodeQL.
Browse files Browse the repository at this point in the history
Despite actually being minor problems, these are all at "error" (high sev)
level, so fixing them will make it easier to raise future patches without
tripping into CodeQL failures.
  • Loading branch information
gianm committed Jan 9, 2025
1 parent 9906544 commit 5da55a6
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,38 +20,25 @@
package org.apache.druid.k8s.overlord;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.base.Supplier;
import org.apache.druid.indexing.common.TestUtils;
import org.apache.druid.indexing.common.config.TaskConfig;
import org.apache.druid.indexing.common.config.TaskConfigBuilder;
import org.apache.druid.java.util.emitter.service.ServiceEmitter;
import org.apache.druid.k8s.overlord.common.DruidKubernetesClient;
import org.apache.druid.k8s.overlord.execution.KubernetesTaskRunnerDynamicConfig;
import org.apache.druid.k8s.overlord.taskadapter.TaskAdapter;
import org.apache.druid.server.DruidNode;
import org.apache.druid.server.log.StartupLoggingConfig;
import org.apache.druid.tasklogs.NoopTaskLogs;
import org.apache.druid.tasklogs.TaskLogs;
import org.easymock.Mock;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import java.util.Properties;

public class KubernetesTaskRunnerFactoryTest
{
private ObjectMapper objectMapper;
private KubernetesTaskRunnerConfig kubernetesTaskRunnerConfig;
private StartupLoggingConfig startupLoggingConfig;
private TaskLogs taskLogs;
private DruidNode druidNode;
private TaskConfig taskConfig;
private Properties properties;

private DruidKubernetesClient druidKubernetesClient;
@Mock private ServiceEmitter emitter;
@Mock private Supplier<KubernetesTaskRunnerDynamicConfig> dynamicConfigRef;
@Mock private TaskAdapter taskAdapter;

@Before
Expand All @@ -61,19 +48,7 @@ public void setup()
kubernetesTaskRunnerConfig = KubernetesTaskRunnerConfig.builder()
.withCapacity(1)
.build();
startupLoggingConfig = new StartupLoggingConfig();
taskLogs = new NoopTaskLogs();
druidNode = new DruidNode(
"test",
"",
false,
0,
1,
true,
false
);
taskConfig = new TaskConfigBuilder().setBaseDir("/tmp").build();
properties = new Properties();
druidKubernetesClient = new DruidKubernetesClient();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,12 @@ public List reverseFetchKeys(Object value)
}

@Override
@SuppressWarnings("EqualsHashCode")
public int hashCode()
{
return 0;
}

@Override
public boolean equals(Object obj)
{
return obj instanceof MockDataFetcher;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,12 @@ public List reverseFetchKeys(Object value)
}

@Override
@SuppressWarnings("EqualsHashCode")
public int hashCode()
{
return 0;
}

@Override
public boolean equals(Object obj)
{
return obj instanceof MockDataFetcher;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,12 @@ public List reverseFetchKeys(Object value)
}

@Override
@SuppressWarnings("EqualsHashCode")
public int hashCode()
{
return 0;
}

@Override
public boolean equals(Object obj)
{
return obj instanceof MockDataFetcher;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -716,7 +716,7 @@ public File fetchSegmentFile(
location.getBucketId()
);
if (!zippedFile.isPresent()) {
throw new ISE("Can't find segment file for location[%s] at path[%s]", location);
throw new ISE("Can't find segment file for location[%s] at path[%s]", location, zippedFile);
}
final File fetchedFile = new File(partitionDir, StringUtils.format("temp_%s", location.getSubTaskId()));
FileUtils.writeAtomically(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,12 @@ public LookupExtractor get()
}

@Override
@SuppressWarnings("EqualsHashCode")
public int hashCode()
{
return 0;
}

@Override
public boolean equals(Object other)
{
return other instanceof TestLookupExtractorFactory;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2567,37 +2567,15 @@ private Iterable<Result<SearchResultValue>> makeSearchResults(String dim, Object

List<SearchHit> values = new ArrayList<>();
while (index < objects.length && !(objects[index] instanceof DateTime)) {
values.add(new SearchHit(dim, objects[index++].toString(), (Integer) objects[index++]));
values.add(new SearchHit(dim, objects[index].toString(), (Integer) objects[index + 1]));

Check failure

Code scanning / CodeQL

Array index out of bounds Error test

This array access might be out of bounds, as the index might be equal to the array length.
index += 2;
}

retVal.add(new Result<>(timestamp, new SearchResultValue(values)));
}
return retVal;
}

private Iterable<ResultRow> makeGroupByResults(GroupByQuery query, Object... objects)
{
List<ResultRow> retVal = new ArrayList<>();
int index = 0;
while (index < objects.length) {
final DateTime timestamp = (DateTime) objects[index++];
final Map<String, Object> rowMap = (Map<String, Object>) objects[index++];
final ResultRow row = ResultRow.create(query.getResultRowSizeWithoutPostAggregators());

if (query.getResultRowHasTimestamp()) {
row.set(0, timestamp.getMillis());
}

for (Map.Entry<String, Object> entry : rowMap.entrySet()) {
final int position = query.getResultRowSignature().indexOf(entry.getKey());
row.set(position, entry.getValue());
}

retVal.add(row);
}
return retVal;
}

private <T> T makeMock(List<Object> mocks, Class<T> clazz)
{
T obj = EasyMock.createMock(clazz);
Expand Down

0 comments on commit 5da55a6

Please sign in to comment.