Skip to content

Commit 02fa4f9

Browse files
Adding integration tests
1 parent 642ac18 commit 02fa4f9

File tree

5 files changed

+113
-10
lines changed

5 files changed

+113
-10
lines changed

x-pack/plugin/inference/src/internalClusterTest/java/org/elasticsearch/xpack/inference/integration/AuthorizationTaskExecutorIT.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ public static void cleanUpClass() {
106106
@Override
107107
protected Settings nodeSettings() {
108108
return Settings.builder()
109-
// Disable CCM to ensure that only the authorization task executor is initialized in the inference plugin when it is created
109+
// Disable CCM to ensure that we don't rely on a CCM configuration existing
110110
.put(CCMSettings.CCM_SUPPORTED_ENVIRONMENT.getKey(), false)
111111
.put(ElasticInferenceServiceSettings.ELASTIC_INFERENCE_SERVICE_URL.getKey(), gatewayUrl)
112112
// Ensure that the polling logic only occurs once so we can deterministically control when an authorization response is
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License
4+
* 2.0; you may not use this file except in compliance with the Elastic License
5+
* 2.0.
6+
*/
7+
8+
package org.elasticsearch.xpack.inference.integration;
9+
10+
import org.elasticsearch.action.support.TestPlainActionFuture;
11+
import org.elasticsearch.action.support.master.AcknowledgedResponse;
12+
import org.elasticsearch.cluster.metadata.ProjectId;
13+
import org.elasticsearch.common.settings.Settings;
14+
import org.elasticsearch.core.TimeValue;
15+
import org.elasticsearch.plugins.Plugin;
16+
import org.elasticsearch.reindex.ReindexPlugin;
17+
import org.elasticsearch.test.ESSingleNodeTestCase;
18+
import org.elasticsearch.xpack.inference.LocalStateInferencePlugin;
19+
import org.elasticsearch.xpack.inference.services.elastic.ElasticInferenceServiceSettings;
20+
import org.elasticsearch.xpack.inference.services.elastic.ccm.CCMEnablementService;
21+
import org.elasticsearch.xpack.inference.services.elastic.ccm.CCMSettings;
22+
import org.junit.Before;
23+
24+
import java.util.Collection;
25+
26+
import static org.hamcrest.Matchers.is;
27+
28+
// TODO create similar tests but when ccm is not support in the environment
29+
public class CCMEnablementServiceIT extends ESSingleNodeTestCase {
30+
31+
private CCMEnablementService ccmEnablementService;
32+
33+
@Before
34+
public void createComponents() {
35+
ccmEnablementService = node().injector().getInstance(CCMEnablementService.class);
36+
}
37+
38+
// Ensure we have a node that doesn't contain any enablement cluster state
39+
protected boolean resetNodeAfterTest() {
40+
return true;
41+
}
42+
43+
@Override
44+
protected Settings nodeSettings() {
45+
return Settings.builder()
46+
.put(CCMSettings.CCM_SUPPORTED_ENVIRONMENT.getKey(), true)
47+
// Disable the authorization task so we don't get errors about inconsistent state while we're
48+
// changing enablement
49+
.put(ElasticInferenceServiceSettings.AUTHORIZATION_ENABLED.getKey(), false)
50+
.build();
51+
}
52+
53+
@Override
54+
protected Collection<Class<? extends Plugin>> getPlugins() {
55+
return pluginList(ReindexPlugin.class, LocalStateInferencePlugin.class);
56+
}
57+
58+
public void testSetEnabled() {
59+
assertEnablementState(false);
60+
61+
setEnablementState(true);
62+
assertEnablementState(true);
63+
}
64+
65+
public void testIsEnabled() {
66+
assertEnablementState(false);
67+
68+
setEnablementState(true);
69+
assertEnablementState(true);
70+
71+
setEnablementState(false);
72+
assertEnablementState(false);
73+
}
74+
75+
private void setEnablementState(boolean enabled) {
76+
var listener = new TestPlainActionFuture<AcknowledgedResponse>();
77+
ccmEnablementService.setEnabled(ProjectId.DEFAULT, enabled, listener);
78+
assertThat(listener.actionGet(TimeValue.THIRTY_SECONDS), is(AcknowledgedResponse.TRUE));
79+
}
80+
81+
private void assertEnablementState(boolean expectedEnabled) {
82+
assertThat(expectedEnabled, is(ccmEnablementService.isEnabled(ProjectId.DEFAULT)));
83+
}
84+
}

x-pack/plugin/inference/src/main/java/org/elasticsearch/xpack/inference/services/elastic/ElasticInferenceServiceSettings.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,15 @@ public class ElasticInferenceServiceSettings {
4343
Setting.Property.NodeScope
4444
);
4545

46+
/**
47+
* This setting is for testing only. It controls whether authorization is performed at all.
48+
*/
49+
public static final Setting<Boolean> AUTHORIZATION_ENABLED = Setting.boolSetting(
50+
"xpack.inference.elastic.authorization.enabled",
51+
true,
52+
Setting.Property.NodeScope
53+
);
54+
4655
private static final TimeValue DEFAULT_AUTH_REQUEST_INTERVAL = TimeValue.timeValueMinutes(10);
4756
static final Setting<TimeValue> AUTHORIZATION_REQUEST_INTERVAL = Setting.timeSetting(
4857
"xpack.inference.elastic.authorization_request_interval",
@@ -89,6 +98,7 @@ public class ElasticInferenceServiceSettings {
8998
private volatile TimeValue authRequestInterval;
9099
private volatile TimeValue maxAuthorizationRequestJitter;
91100
private final TimeValue connectionTtl;
101+
private final boolean isAuthorizationEnabled;
92102

93103
public ElasticInferenceServiceSettings(Settings settings) {
94104
eisGatewayUrl = EIS_GATEWAY_URL.get(settings);
@@ -97,6 +107,7 @@ public ElasticInferenceServiceSettings(Settings settings) {
97107
authRequestInterval = AUTHORIZATION_REQUEST_INTERVAL.get(settings);
98108
maxAuthorizationRequestJitter = MAX_AUTHORIZATION_REQUEST_JITTER.get(settings);
99109
connectionTtl = CONNECTION_TTL_SETTING.get(settings);
110+
isAuthorizationEnabled = AUTHORIZATION_ENABLED.get(settings);
100111
}
101112

102113
/**
@@ -132,6 +143,10 @@ public TimeValue getConnectionTtl() {
132143
return connectionTtl;
133144
}
134145

146+
public boolean isAuthorizationEnabled() {
147+
return isAuthorizationEnabled;
148+
}
149+
135150
public static List<Setting<?>> getSettingsDefinitions() {
136151
ArrayList<Setting<?>> settings = new ArrayList<>();
137152
settings.add(EIS_GATEWAY_URL);
@@ -142,6 +157,7 @@ public static List<Setting<?>> getSettingsDefinitions() {
142157
settings.add(AUTHORIZATION_REQUEST_INTERVAL);
143158
settings.add(MAX_AUTHORIZATION_REQUEST_JITTER);
144159
settings.add(CONNECTION_TTL_SETTING);
160+
settings.add(AUTHORIZATION_ENABLED);
145161
return settings;
146162
}
147163

x-pack/plugin/inference/src/main/java/org/elasticsearch/xpack/inference/services/elastic/authorization/AuthorizationTaskExecutor.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,9 @@ public static AuthorizationTaskExecutor create(
121121
* get an error indicating that it isn't aware of whether the task is a cluster scoped task.
122122
*/
123123
public synchronized void startAndLazyCreateTask() {
124-
startInternal();
124+
if (pollerParameters.elasticInferenceServiceSettings().isAuthorizationEnabled()) {
125+
startInternal();
126+
}
125127
}
126128

127129
private void startInternal() {
@@ -168,9 +170,8 @@ private boolean shouldSkipCreatingTask(@Nullable ClusterState state) {
168170
return true;
169171
}
170172

171-
return clusterCanSupportFeature(state) == false
172-
|| running.get() == false
173-
|| authorizationTaskExists(state)
173+
return clusterCanSupportFeature(state) == false || running.get() == false || authorizationTaskExists(state)
174+
// TODO write some integration tests for this
174175
|| ccmSupportedButNotYetConfigured();
175176
}
176177

x-pack/plugin/inference/src/test/java/org/elasticsearch/xpack/inference/services/elastic/ccm/CCMEnablementServiceTests.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
import org.elasticsearch.action.support.TestPlainActionFuture;
1212
import org.elasticsearch.action.support.master.AcknowledgedResponse;
1313
import org.elasticsearch.cluster.ClusterState;
14-
import org.elasticsearch.cluster.metadata.Metadata;
1514
import org.elasticsearch.cluster.metadata.ProjectId;
15+
import org.elasticsearch.cluster.metadata.ProjectMetadata;
1616
import org.elasticsearch.cluster.service.ClusterService;
1717
import org.elasticsearch.core.TimeValue;
1818
import org.elasticsearch.features.FeatureService;
@@ -81,9 +81,10 @@ public void testIsEnabled_ReturnsFalse_WhenClusterIsNotUpgradedFully() {
8181

8282
public void testIsEnabled_ReturnsFalse_WhenConfigurationNotEnabledYet() {
8383
var state = ClusterState.builder(ClusterState.EMPTY_STATE)
84-
.metadata(
85-
Metadata.builder(ClusterState.EMPTY_STATE.metadata())
84+
.putProjectMetadata(
85+
ProjectMetadata.builder(ProjectId.DEFAULT)
8686
.putCustom(CCMEnablementService.EnablementMetadata.NAME, new CCMEnablementService.EnablementMetadata(false))
87+
.build()
8788
)
8889
.build();
8990

@@ -102,9 +103,10 @@ public void testIsEnabled_ReturnsFalse_WhenConfigurationNotEnabledYet() {
102103

103104
public void testIsEnabled_ReturnsTrue_WhenConfigurationExists() {
104105
var state = ClusterState.builder(ClusterState.EMPTY_STATE)
105-
.metadata(
106-
Metadata.builder(ClusterState.EMPTY_STATE.metadata())
106+
.putProjectMetadata(
107+
ProjectMetadata.builder(ProjectId.DEFAULT)
107108
.putCustom(CCMEnablementService.EnablementMetadata.NAME, new CCMEnablementService.EnablementMetadata(true))
109+
.build()
108110
)
109111
.build();
110112

0 commit comments

Comments
 (0)