Skip to content

Commit

Permalink
Fix importer tests using mirror_node user (#10309)
Browse files Browse the repository at this point in the history
* Change importer tests from running as `mirror_node` to `mirror_importer` db user
* Fix some test regressions due to use of regular JdbcOperations

---------

Signed-off-by: Steven Sheehy <[email protected]>
  • Loading branch information
steven-sheehy authored Feb 6, 2025
1 parent 54b09d3 commit b94461e
Show file tree
Hide file tree
Showing 25 changed files with 83 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,11 @@
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.flyway.FlywayProperties;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.boot.autoconfigure.jdbc.JdbcConnectionDetails;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.boot.testcontainers.service.connection.ServiceConnection;
import org.springframework.boot.testcontainers.lifecycle.TestcontainersStartup;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Primary;
import org.springframework.transaction.support.TransactionOperations;
Expand Down Expand Up @@ -97,7 +99,6 @@ MeterRegistry meterRegistry() {
}

@Bean(POSTGRESQL)
@ServiceConnection("postgresql")
PostgreSQLContainer<?> postgresql() {
var imageName = v2 ? "gcr.io/mirrornode/citus:12.1.1" : "postgres:16-alpine";
var dockerImageName = DockerImageName.parse(imageName).asCompatibleSubstituteFor("postgres");
Expand All @@ -114,6 +115,37 @@ PostgreSQLContainer<?> postgresql() {
.withUsername("mirror_node");
}

// Avoid using @ServiceConnection and use our own custom connection details so we can pass mirror_importer as user
@Bean
JdbcConnectionDetails jdbcConnectionDetails(
DataSourceProperties dataSourceProperties, PostgreSQLContainer<?> postgresql) {
TestcontainersStartup.start(postgresql);
return new JdbcConnectionDetails() {

@Override
public String getDriverClassName() {
return postgresql.getDriverClassName();
}

@Override
public String getJdbcUrl() {
return postgresql.getJdbcUrl();
}

@Override
public String getPassword() {
var password = dataSourceProperties.getPassword();
return password.contains("importer") ? password : postgresql.getPassword();
}

@Override
public String getUsername() {
var username = dataSourceProperties.getUsername();
return username.contains("importer") ? username : postgresql.getUsername();
}
};
}

@RequiredArgsConstructor
public static class FilteringConsumer implements Consumer<OutputFrame> {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,7 @@ private Optional<MigrationAddressBook> findById(long id) {

private void runMigration() throws IOException {
log.info("Run migration: {}", sql.getName());
jdbcOperations.update(FileUtils.readFileToString(sql, "UTF-8"));
ownerJdbcTemplate.update(FileUtils.readFileToString(sql, "UTF-8"));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ void migrate() {

@SneakyThrows
private void runMigration() {
jdbcOperations.update(FileUtils.readFileToString(migrationSql, "UTF-8"));
ownerJdbcTemplate.update(FileUtils.readFileToString(migrationSql, "UTF-8"));
}

private IterableAssert<Entity> assertEntities() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ insert into nft (account_id, created_timestamp, delegating_spender, deleted, mod
@SneakyThrows
private void runMigration() {
try (var is = sql.getInputStream()) {
jdbcOperations.update(StreamUtils.copyToString(is, StandardCharsets.UTF_8));
ownerJdbcTemplate.update(StreamUtils.copyToString(is, StandardCharsets.UTF_8));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ private MigrationContractResult contractResult(long consensusTimestamp, Long con
}

private void migrate() throws Exception {
jdbcOperations.update(FileUtils.readFileToString(migrationSql, "UTF-8"));
ownerJdbcTemplate.update(FileUtils.readFileToString(migrationSql, "UTF-8"));
}

private List<MigrationContractLog> retrieveContractLogs() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -569,7 +569,7 @@ void minFrequency() {
@SneakyThrows
private void addBalanceDeduplicateFunctions() {
try (var is = migrationSql.getInputStream()) {
jdbcOperations.execute(StreamUtils.copyToString(is, StandardCharsets.UTF_8));
ownerJdbcTemplate.execute(StreamUtils.copyToString(is, StandardCharsets.UTF_8));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,10 @@ void migrate() {
}

private void persistTransactionHashes(Collection<TransactionHash> transactionHashes) {
jdbcOperations.batchUpdate(
ownerJdbcTemplate.batchUpdate(
"""
insert into transaction_hash (consensus_timestamp, hash, payer_account_id) values (?, ?, ?)
""",
insert into transaction_hash (consensus_timestamp, hash, payer_account_id) values (?, ?, ?)
""",
transactionHashes,
transactionHashes.size(),
(ps, transactionHash) -> {
Expand All @@ -104,7 +104,7 @@ insert into transaction_hash (consensus_timestamp, hash, payer_account_id) value
private void runMigration() {
try (var is = migrationSql.getInputStream()) {
var script = StreamUtils.copyToString(is, StandardCharsets.UTF_8);
jdbcOperations.execute(script);
ownerJdbcTemplate.execute(script);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@ private PreMigrationEntity entity(boolean deleted, long id, EntityType entityTyp

@SneakyThrows
private void migrate() {
jdbcOperations.update(FileUtils.readFileToString(migrationSql, "UTF-8"));
ownerJdbcTemplate.update(FileUtils.readFileToString(migrationSql, "UTF-8"));
}

/**
Expand Down Expand Up @@ -422,10 +422,10 @@ private List<PostMigrationEntity> retrieveEntities() {
*/
private void setEntityTablesPreV_1_36() {
// remove entity table if present
jdbcOperations.execute("drop table if exists entity cascade;");
ownerJdbcTemplate.execute("drop table if exists entity cascade;");

// add t_entities if not present
jdbcOperations.execute(
ownerJdbcTemplate.execute(
"""
create table if not exists t_entities (
entity_num bigint not null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ private MigrationContractLog contractLog(
}

private void migrate() throws Exception {
jdbcOperations.update(FileUtils.readFileToString(migrationSql, "UTF-8"));
ownerJdbcTemplate.update(FileUtils.readFileToString(migrationSql, "UTF-8"));
}

private List<MigrationContractLog> retrieveContractLogs() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,6 @@ private ContractState convert(ContractStateChange contractStateChange, long crea

@SneakyThrows
private void runMigration() {
jdbcOperations.update(FileUtils.readFileToString(migrationSql, "UTF-8"));
ownerJdbcTemplate.update(FileUtils.readFileToString(migrationSql, "UTF-8"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.hedera.mirror.importer.migration;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

import com.hedera.mirror.importer.ImporterIntegrationTest;
import com.hedera.mirror.importer.ImporterProperties;
Expand All @@ -25,6 +26,7 @@
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import org.springframework.context.annotation.Lazy;
import org.springframework.dao.DataAccessException;

@RequiredArgsConstructor
@Tag("migration")
Expand All @@ -38,6 +40,18 @@ void checksum() {
assertThat(dummyMigration.getChecksum()).isEqualTo(5);
}

@Test
void verifyPermissions() {
final var sql =
"""
create table if not exists test (id bigint primary key);
insert into test (id) values (1);
drop table test
""";
assertThatThrownBy(() -> jdbcOperations.update(sql)).isInstanceOf(DataAccessException.class);
ownerJdbcTemplate.update(sql); // Succeeds
}

static class DummyMigration extends RepeatableMigration {

@Getter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,8 @@ void migrate() {
@Test
void migrateWithoutBalanceSnapshots() {
setup();
jdbcOperations.update("truncate account_balance");
jdbcOperations.update("truncate token_balance");
ownerJdbcTemplate.update("truncate account_balance");
ownerJdbcTemplate.update("truncate token_balance");
runMigration();

softly.assertThat(tokenAccountRepository.findAll()).containsExactlyInAnyOrderElementsOf(expectedTokenAccounts);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ void migrate(boolean hasTokenTransfers, long expectedAmount1, long expectedAmoun
@SneakyThrows
private void runMigration() {
try (var is = migrationSql.getInputStream()) {
jdbcOperations.update(StreamUtils.copyToString(is, StandardCharsets.UTF_8));
ownerJdbcTemplate.update(StreamUtils.copyToString(is, StandardCharsets.UTF_8));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
@DisableRepeatableSqlMigration
@Tag("migration")
@RequiredArgsConstructor
public class FixNodeTransactionsMigrationTest extends ImporterIntegrationTest {
class FixNodeTransactionsMigrationTest extends ImporterIntegrationTest {

private final FixNodeTransactionsMigration migration;
private final NodeRepository nodeRepository;
Expand All @@ -48,7 +48,6 @@ void empty() {
runMigration();
softly.assertThat(nodeRepository.count()).isZero();
softly.assertThat(findHistory(Node.class)).isEmpty();
softly.assertAll();
}

@Test
Expand All @@ -61,8 +60,6 @@ void migrateNoHistory() {

softly.assertThat(nodeRepository.count()).isEqualTo(3);
softly.assertThat(nodeRepository.findAll()).containsExactlyInAnyOrderElementsOf(expectedNodes);

softly.assertAll();
}

@Test
Expand Down Expand Up @@ -132,8 +129,6 @@ void migrateHistory() {

softly.assertThat(nodeRepository.findAll()).containsExactlyInAnyOrderElementsOf(newNodes);
softly.assertThat(findHistory(Node.class)).containsExactlyInAnyOrderElementsOf(historyNodes);

softly.assertAll();
}

@SneakyThrows
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.Resource;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.JdbcOperations;
import org.springframework.test.context.TestPropertySource;
import org.springframework.transaction.support.TransactionTemplate;
import org.springframework.util.StreamUtils;
Expand All @@ -58,21 +58,18 @@ class GasConsumedMigrationTest extends ImporterIntegrationTest {

private static final String REVERT_DDL = "alter table contract_result drop column gas_consumed";

private final JdbcTemplate jdbcTemplate;

@Value("classpath:db/migration/v1/V1.94.1.1__add_gas_consumed_field.sql")
private final Resource sql;

private final TransactionTemplate transactionTemplate;

private final ContractActionRepository contractActionRepository;
private final ContractRepository contractRepository;
private final ContractResultRepository contractResultRepository;
private final EntityRepository entityRepository;

@AfterEach
void teardown() {
jdbcOperations.update(REVERT_DDL);
ownerJdbcTemplate.update(REVERT_DDL);
}

@Test
Expand Down Expand Up @@ -124,7 +121,7 @@ private void persistData(EthereumTransaction ethTx, boolean successTopLevelCreat
contract.getId(),
failedInitCode,
domainBuilder);
persistMigrationContractResult(migrateContractResult, jdbcTemplate);
persistMigrationContractResult(migrateContractResult, jdbcOperations);
domainBuilder
.contractAction()
.customize(ca -> ca.consensusTimestamp(ethTx.getConsensusTimestamp()))
Expand All @@ -143,7 +140,7 @@ private void runMigration() {
try (final var is = sql.getInputStream()) {
transactionTemplate.executeWithoutResult(s -> {
try {
jdbcTemplate.update(StreamUtils.copyToString(is, StandardCharsets.UTF_8));
ownerJdbcTemplate.update(StreamUtils.copyToString(is, StandardCharsets.UTF_8));
} catch (final IOException e) {
throw new RuntimeException(e);
}
Expand Down Expand Up @@ -198,7 +195,8 @@ public static MigrationContractResult createMigrationContractResult(
.build();
}

public static void persistMigrationContractResult(final MigrationContractResult result, JdbcTemplate jdbcTemplate) {
public static void persistMigrationContractResult(
final MigrationContractResult result, JdbcOperations jdbcOperations) {
final String sql =
"""
insert into contract_result
Expand All @@ -208,7 +206,7 @@ public static void persistMigrationContractResult(final MigrationContractResult
transaction_result) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
""";

jdbcTemplate.update(sql, ps -> {
jdbcOperations.update(sql, ps -> {
ps.setLong(1, result.getAmount());
ps.setBytes(2, result.getBloom());
ps.setBytes(3, result.getCallResult());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ void verifyEmpty() {

@SneakyThrows
private void migrate() {
jdbcOperations.execute(FileUtils.readFileToString(migrationSql, "UTF-8"));
ownerJdbcTemplate.execute(FileUtils.readFileToString(migrationSql, "UTF-8"));
}

private MigrationEntityV1_47_1 entity(long id, long num, EntityType type) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ private Transaction transaction(

@SneakyThrows
private void migrate() {
jdbcOperations.update(FileUtils.readFileToString(migrationSql, "UTF-8"));
ownerJdbcTemplate.update(FileUtils.readFileToString(migrationSql, "UTF-8"));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ void verify() {

@SneakyThrows
private void migrate() {
jdbcOperations.execute(FileUtils.readFileToString(migrationSql, "UTF-8"));
ownerJdbcTemplate.execute(FileUtils.readFileToString(migrationSql, "UTF-8"));
}

private MigrationEntity entity(Token token) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@ private void runMigration(String partitionStartDate, String partitionTimeInterva
var script = StreamUtils.copyToString(is, StandardCharsets.UTF_8)
.replaceAll("\\$\\{partitionStartDate}", partitionStartDate)
.replaceAll("\\$\\{partitionTimeInterval}", partitionTimeInterval);
jdbcOperations.execute(script);
ownerJdbcTemplate.execute(script);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ insert into token_account (account_id, associated, automatic_association, create

@SneakyThrows
private void runMigration() {
jdbcOperations.update(FileUtils.readFileToString(migrationSql, "UTF-8"));
ownerJdbcTemplate.update(FileUtils.readFileToString(migrationSql, "UTF-8"));
}

@SuppressWarnings("java:S1854") // No useless assignments in the method, since they help avoiding code repetition
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ private PreMigrationEntity updateEntity(PreMigrationEntity previous) {
private void runMigration() {
String migrationFilepath = isV1() ? "v1/V1.103.2__topic_custom_fees.sql" : "v2/V2.8.2__topic_custom_fees.sql";
var file = TestUtils.getResource("db/migration/" + migrationFilepath);
jdbcOperations.execute(FileUtils.readFileToString(file, StandardCharsets.UTF_8));
ownerJdbcTemplate.execute(FileUtils.readFileToString(file, StandardCharsets.UTF_8));
}

@Builder(toBuilder = true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ private MigrationTransaction transaction(long consensusTimestamp, long payerAcco
}

private void migrate() throws Exception {
jdbcOperations.update(FileUtils.readFileToString(migrationSql, "UTF-8"));
ownerJdbcTemplate.update(FileUtils.readFileToString(migrationSql, "UTF-8"));
}

private List<MigrationTopicMessage> retrieveTopicMessages() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,7 @@ private MigrationTransaction transaction(
}

private void migrate() throws Exception {
jdbcOperations.update(FileUtils.readFileToString(migrationSql, "UTF-8"));
ownerJdbcTemplate.update(FileUtils.readFileToString(migrationSql, "UTF-8"));
}

private MigrationNftTransfer nftTransfer(
Expand Down
Loading

0 comments on commit b94461e

Please sign in to comment.