Skip to content

Commit 05d72c3

Browse files
Merge split URL configuration properties to a single URL field (#641)
1 parent 1e4037f commit 05d72c3

File tree

9 files changed

+53
-51
lines changed

9 files changed

+53
-51
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ All notable changes to this project will be documented in this file.
1515
- Refactor `RestTemplateConfig` to use `HttpClient 5` and improve proxy handling. (#626)
1616
- Replace deprecated `connect` with `connectAsync` in `StompClientService`. (#627)
1717
- Remove redundant blockchain calls to diminish pressure on Ethereum JSON-RPC API. (#632)
18-
- Harmonize YML internal variables to proper case. (#638)
1918
- Stop using `TestUtils` in `ContributionServiceTests`. (#640)
2019

2120
### Breaking API changes
@@ -24,6 +23,8 @@ All notable changes to this project will be documented in this file.
2423
- Move `WorkerModel` from `iexec-common` to `iexec-core-library`. (#633)
2524
- Move `TaskAbortCause` from `iexec-commons-poco` to `iexec-core-library`. (#634)
2625
- Move `Contribution` from `iexec-common` to `iexec-worker`. (#636)
26+
- Harmonize YML internal variables to proper case. (#638 #641)
27+
- Merge split URL configuration properties (protocol, host, port) to a single URL field to offer URL validation at startup. (#641)
2728

2829
### Dependency Upgrades
2930

README.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,7 @@ You can configure the _iExec Worker_ with the following properties:
1919
| Environment variable | Description | Type | Default value |
2020
|-------------------------------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------|--------------------------|-------------------------------------------------------------|
2121
| `IEXEC_WORKER_PORT` | Server HTTP port of the _iExec Worker_. | Positive integer | `13100` |
22-
| `IEXEC_CORE_PROTOCOL` | Protocol to connect to the _iExec Core Scheduler_. | String | `http` |
23-
| `IEXEC_CORE_HOST` | Host to connect to the _iExec Core Scheduler_. | String | `localhost` |
24-
| `IEXEC_CORE_PORT` | Port to connect to the _iExec Core Scheduler_. | Positive integer | `13000` |
22+
| `IEXEC_CORE_URL` | URL to connect to the _iExec Core Scheduler_. | String | `http://localhost:13000` |
2523
| `IEXEC_WORKER_NAME` | Public name of the _iExec Worker_. | String | `worker` |
2624
| `POOL_ADDRESS` | On-chain address of the workerpool managed by the _iExec Core Scheduler_, the default value must be changed. | String | `0x0` |
2725
| `IEXEC_WORKER_BASE_DIR` | Path to the folder within which the _iExec Worker_ will read-and-write inputs and outputs of tasks. | String | `/tmp/iexec-worker` |

src/main/java/com/iexec/worker/config/SchedulerConfiguration.java

Lines changed: 13 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -20,30 +20,23 @@
2020
import com.iexec.core.api.SchedulerClientBuilder;
2121
import feign.Logger;
2222
import jakarta.annotation.PostConstruct;
23-
import lombok.Getter;
23+
import jakarta.validation.constraints.NotEmpty;
24+
import lombok.Value;
2425
import org.apache.commons.lang3.StringUtils;
25-
import org.springframework.beans.factory.annotation.Value;
26+
import org.hibernate.validator.constraints.URL;
27+
import org.springframework.boot.context.properties.ConfigurationProperties;
2628
import org.springframework.context.annotation.Bean;
27-
import org.springframework.context.annotation.Configuration;
29+
import org.springframework.validation.annotation.Validated;
2830

29-
@Configuration
31+
@Value
32+
@Validated
33+
@ConfigurationProperties(prefix = "core")
3034
public class SchedulerConfiguration {
3135

32-
private final String protocol;
33-
private final String host;
34-
private final String port;
35-
@Getter
36-
private final String poolAddress;
37-
38-
public SchedulerConfiguration(@Value("${core.protocol}") String protocol,
39-
@Value("${core.host}") String host,
40-
@Value("${core.port}") String port,
41-
@Value("${core.pool-address}") String poolAddress) {
42-
this.protocol = protocol;
43-
this.host = host;
44-
this.port = port;
45-
this.poolAddress = poolAddress;
46-
}
36+
@URL(message = "URL must be a valid URL")
37+
@NotEmpty(message = "URL must not be empty")
38+
String url;
39+
String poolAddress;
4740

4841
@PostConstruct
4942
private void postConstruct() {
@@ -53,12 +46,8 @@ private void postConstruct() {
5346
}
5447
}
5548

56-
public String getUrl() {
57-
return String.format("%s://%s:%s", protocol, host, port);
58-
}
59-
6049
@Bean
6150
SchedulerClient schedulerClient() {
62-
return SchedulerClientBuilder.getInstance(Logger.Level.FULL, getUrl());
51+
return SchedulerClientBuilder.getInstance(Logger.Level.FULL, url);
6352
}
6453
}

src/main/resources/application.yml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@ server:
22
port: ${IEXEC_WORKER_PORT:13100}
33

44
core:
5-
protocol: ${IEXEC_CORE_PROTOCOL:http}
6-
host: ${IEXEC_CORE_HOST:localhost}
7-
port: ${IEXEC_CORE_PORT:13000}
5+
url: ${IEXEC_CORE_URL:http://localhost:13000}
86
pool-address: ${POOL_ADDRESS:0x0}
97

108
worker:

src/test/java/com/iexec/worker/chain/ContributionServiceTests.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ void getCannotContributeStatusShouldReturnChainUnreachable() {
115115
final String chainTaskId = "chainTaskId";
116116

117117
when(workerpoolAuthorizationService.getWorkerpoolAuthorization(chainTaskId))
118-
.thenReturn(new WorkerpoolAuthorization());
118+
.thenReturn(getTeeWorkerpoolAuth());
119119
when(iexecHubService.getChainTask(chainTaskId)).thenReturn(Optional.empty());
120120

121121
assertThat(contributionService.getCannotContributeStatusCause(chainTaskId).orElse(null))
@@ -129,7 +129,7 @@ void getCannotContributeStatusShouldReturnStakeTooLow() {
129129
final String chainTaskId = chainTask.getChainTaskId();
130130

131131
when(workerpoolAuthorizationService.getWorkerpoolAuthorization(chainTaskId))
132-
.thenReturn(new WorkerpoolAuthorization());
132+
.thenReturn(getTeeWorkerpoolAuth());
133133
when(iexecHubService.getChainTask(chainTaskId)).thenReturn(Optional.of(chainTask));
134134
when(iexecHubService.getChainAccount()).thenReturn(Optional.of(ChainAccount.builder().deposit(0).build()));
135135
when(iexecHubService.getChainDeal(CHAIN_DEAL_ID)).thenReturn(Optional.of(ChainDeal.builder().workerStake(BigInteger.valueOf(5)).build()));
@@ -152,7 +152,7 @@ void getCannotContributeStatusShouldReturnTaskNotActive() {
152152
final String chainTaskId = inactiveTask.getChainTaskId();
153153

154154
when(workerpoolAuthorizationService.getWorkerpoolAuthorization(chainTaskId))
155-
.thenReturn(new WorkerpoolAuthorization());
155+
.thenReturn(getTeeWorkerpoolAuth());
156156
when(iexecHubService.getChainTask(chainTaskId)).thenReturn(Optional.of(inactiveTask));
157157
when(iexecHubService.getChainAccount()).thenReturn(Optional.of(ChainAccount.builder().deposit(1000).build()));
158158
when(iexecHubService.getChainDeal(CHAIN_DEAL_ID)).thenReturn(Optional.of(ChainDeal.builder().workerStake(BigInteger.valueOf(5)).build()));
@@ -176,7 +176,7 @@ void getCannotContributeStatusShouldReturnAfterDeadline() {
176176
final String chainTaskId = timedOutChainTask.getChainTaskId();
177177

178178
when(workerpoolAuthorizationService.getWorkerpoolAuthorization(chainTaskId))
179-
.thenReturn(new WorkerpoolAuthorization());
179+
.thenReturn(getTeeWorkerpoolAuth());
180180
when(iexecHubService.getChainTask(chainTaskId)).thenReturn(Optional.of(timedOutChainTask));
181181
when(iexecHubService.getChainAccount())
182182
.thenReturn(Optional.of(ChainAccount.builder().deposit(1000).build()));
@@ -203,7 +203,7 @@ void getCannotContributeStatusShouldReturnContributionAlreadySet() {
203203
final String chainTaskId = alreadyContributedChainTask.getChainTaskId();
204204

205205
when(workerpoolAuthorizationService.getWorkerpoolAuthorization(chainTaskId))
206-
.thenReturn(new WorkerpoolAuthorization());
206+
.thenReturn(getTeeWorkerpoolAuth());
207207
when(iexecHubService.getChainTask(chainTaskId)).thenReturn(Optional.of(alreadyContributedChainTask));
208208
when(iexecHubService.getChainAccount())
209209
.thenReturn(Optional.of(ChainAccount.builder().deposit(1000).build()));
@@ -223,7 +223,7 @@ void getCannotContributeStatusCauseShouldReturnEmpty() {
223223
final String chainTaskId = chainTask.getChainTaskId();
224224

225225
when(workerpoolAuthorizationService.getWorkerpoolAuthorization(chainTaskId))
226-
.thenReturn(new WorkerpoolAuthorization());
226+
.thenReturn(getTeeWorkerpoolAuth());
227227
when(iexecHubService.getChainTask(chainTaskId))
228228
.thenReturn(Optional.of(chainTask));
229229
when(iexecHubService.getChainAccount())

src/test/java/com/iexec/worker/chain/WalletConfigurationTest.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2024 IEXEC BLOCKCHAIN TECH
2+
* Copyright 2024-2025 IEXEC BLOCKCHAIN TECH
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -52,8 +52,7 @@ class WalletConfigurationTest {
5252
void shouldCreateBeans() throws Exception {
5353
final String tempWalletName = WalletUtils.generateFullNewWalletFile("changeit", tempWalletDir);
5454
final String tempWalletPath = tempWalletDir.getAbsolutePath() + File.separator + tempWalletName;
55-
runner.withPropertyValues("core.protocol=http", "core.host=localhost", "core.port=" + wmServer.getMappedPort(WIREMOCK_PORT),
56-
"worker.name=worker", "worker.worker-base-dir=/tmp", "worker.override-available-cpu-count=",
55+
runner.withPropertyValues("worker.name=worker", "worker.worker-base-dir=/tmp", "worker.override-available-cpu-count=",
5756
"worker.gpu-enabled=false", "worker.gas-price-multiplier=1.0", "worker.gas-price-cap=22000000000",
5857
"worker.override-blockchain-node-address=", "worker.developer-logger-enabled=true",
5958
"worker.tee-compute-max-heap-size-gb=8", "worker.docker-network-name=iexec-worker-net")
@@ -63,7 +62,7 @@ void shouldCreateBeans() throws Exception {
6362
.withBean(WalletConfiguration.class, tempWalletPath, "changeit")
6463
.withBean(Web3jService.class)
6564
.withBean(WorkerConfigurationService.class)
66-
.withUserConfiguration(SchedulerConfiguration.class)
65+
.withBean(SchedulerConfiguration.class, "http://localhost:" + wmServer.getMappedPort(WIREMOCK_PORT), "0x365E7BABAa85eC61Dffe5b520763062e6C29dA27")
6766
.run(context -> assertThat(context)
6867
.hasSingleBean(ConfigServerClient.class)
6968
.hasSingleBean(ConfigServerConfigurationService.class)

src/test/java/com/iexec/worker/chain/WebSocketBlockchainListenerTests.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,10 @@ class WebSocketBlockchainListenerTests {
5757

5858
@DynamicPropertySource
5959
static void registerProperties(DynamicPropertyRegistry registry) {
60-
registry.add("core.protocol", () -> "http");
61-
registry.add("core.host", () -> environment.getServiceHost(CORE_SVC_NAME, CORE_SVC_PORT));
62-
registry.add("core.port", () -> environment.getServicePort(CORE_SVC_NAME, CORE_SVC_PORT));
60+
final String coreHost = environment.getServiceHost(CORE_SVC_NAME, CORE_SVC_PORT);
61+
final int corePort = environment.getServicePort(CORE_SVC_NAME, CORE_SVC_PORT);
62+
63+
registry.add("core.url", () -> getServiceUrl(coreHost, corePort));
6364
registry.add("core.pool-address", () -> "0x1");
6465
registry.add("worker.override-blockchain-node-address", () -> getServiceUrl(
6566
environment.getServiceHost(CHAIN_SVC_NAME, CHAIN_SVC_PORT),

src/test/java/com/iexec/worker/chain/WorkerpoolAuthorizationServiceTests.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,7 @@ void shouldFailToPutWorkerpoolAuthorizationWhenCantGetWorkerPoolOwner() {
8888

8989
@Test
9090
void shouldFailToPutWorkerpoolAuthorizationIfChainTaskIdIsNullInWorkerpoolAuthorization() {
91-
WorkerpoolAuthorization workerpoolAuthorization = getWorkerpoolAuthorization();
92-
workerpoolAuthorization.setChainTaskId(null);
91+
final WorkerpoolAuthorization workerpoolAuthorization = WorkerpoolAuthorization.builder().chainTaskId(null).build();
9392
assertFalse(workerpoolAuthorizationService.putWorkerpoolAuthorization(workerpoolAuthorization));
9493
}
9594

src/test/java/com/iexec/worker/config/SchedulerConfigurationTests.java

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import org.junit.jupiter.params.ParameterizedTest;
2222
import org.junit.jupiter.params.provider.ValueSource;
2323
import org.springframework.beans.factory.BeanCreationException;
24-
import org.springframework.boot.context.annotation.UserConfigurations;
2524
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
2625

2726
import static org.assertj.core.api.AssertionsForClassTypes.assertThatThrownBy;
@@ -32,8 +31,7 @@ class SchedulerConfigurationTests {
3231

3332
@Test
3433
void shouldCreateBeanInstance() {
35-
runner.withPropertyValues("core.protocol=http", "core.host=localhost", "core.port=13000", "core.pool-address=0x365E7BABAa85eC61Dffe5b520763062e6C29dA27")
36-
.withConfiguration(UserConfigurations.of(SchedulerConfiguration.class))
34+
runner.withBean(SchedulerConfiguration.class, "http://localhost:13000", "0x365E7BABAa85eC61Dffe5b520763062e6C29dA27")
3735
.run(context -> {
3836
assertThat(context).hasSingleBean(SchedulerClient.class);
3937
assertThat(context).getBean("schedulerConfiguration", SchedulerConfiguration.class)
@@ -45,13 +43,32 @@ void shouldCreateBeanInstance() {
4543
@ParameterizedTest
4644
@ValueSource(strings = {"", "0x0"})
4745
void shouldFailedAndRaisedExceptionWhenPoolAddressIsInvalid(String poolAddress) {
48-
runner.withPropertyValues("core.protocol=http", "core.host=localhost", "core.port=13000", "core.pool-address=" + poolAddress)
49-
.withConfiguration(UserConfigurations.of(SchedulerConfiguration.class))
46+
runner.withBean(SchedulerConfiguration.class, "http://localhost:13000", poolAddress)
5047
.run(context -> {
5148
assertThatThrownBy(() -> context.getBean(SchedulerConfiguration.class))
5249
.isInstanceOf(IllegalStateException.class)
5350
.hasCauseInstanceOf(BeanCreationException.class)
5451
.hasRootCauseMessage("The workerpool address must be filled in");
5552
});
5653
}
54+
55+
@Test
56+
void shouldFailWhenUrlIsEmpty() {
57+
runner.withBean(SchedulerConfiguration.class, "", "0x365E7BABAa85eC61Dffe5b520763062e6C29dA27")
58+
.run(context -> {
59+
assertThat(context).hasFailed();
60+
assertThat(context.getStartupFailure())
61+
.isInstanceOf(BeanCreationException.class)
62+
.hasMessageContaining("Error creating bean with name 'schedulerClient'");
63+
});
64+
}
65+
66+
@Test
67+
void shouldPassWithValidUrl() {
68+
runner.withBean(SchedulerConfiguration.class, "http://localhost:8080", "0x365E7BABAa85eC61Dffe5b520763062e6C29dA27")
69+
.run(context -> {
70+
SchedulerConfiguration config = context.getBean(SchedulerConfiguration.class);
71+
assertThat(config.getUrl()).isEqualTo("http://localhost:8080");
72+
});
73+
}
5774
}

0 commit comments

Comments
 (0)