Skip to content

Commit 0701a22

Browse files
authored
Adding a unit test to detect when StackTemplateRegistry#REGISTRY_VERSION needs to be incremented (elastic#136666)
1 parent d557a58 commit 0701a22

File tree

3 files changed

+107
-49
lines changed

3 files changed

+107
-49
lines changed

x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/template/IngestPipelineConfig.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,10 @@ public List<String> getPipelineDependencies() {
6868
return dependencies;
6969
}
7070

71+
public String getFileName() {
72+
return resource;
73+
}
74+
7175
public abstract XContentType getXContentType();
7276

7377
public abstract BytesReference loadConfig();

x-pack/plugin/stack/src/main/java/org/elasticsearch/xpack/stack/StackTemplateRegistry.java

Lines changed: 63 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ public class StackTemplateRegistry extends IndexTemplateRegistry {
3939
// to built-in templates.
4040
public static final int REGISTRY_VERSION = 19;
4141

42+
// The computed checksum of all templates and components that are registered in this registry.
43+
// This is used by a test to ensure that REGISTRY_VERSION is updated when any of the components change.
44+
static final String COMPUTED_CHECKSUM = "228d8ef7";
45+
4246
public static final String TEMPLATE_VERSION_VARIABLE = "xpack.stack.template.version";
4347
public static final Setting<Boolean> STACK_TEMPLATES_ENABLED = Setting.boolSetting(
4448
"stack.templates.enabled",
@@ -128,7 +132,21 @@ public StackTemplateRegistry(
128132

129133
private Map<String, ComponentTemplate> loadComponentTemplateConfigs() {
130134
final Map<String, ComponentTemplate> componentTemplates = new HashMap<>();
131-
for (IndexTemplateConfig config : List.of(
135+
for (IndexTemplateConfig config : getComponentTemplateConfigsAsConfigs()) {
136+
try {
137+
componentTemplates.put(
138+
config.getTemplateName(),
139+
ComponentTemplate.parse(JsonXContent.jsonXContent.createParser(XContentParserConfiguration.EMPTY, config.loadBytes()))
140+
);
141+
} catch (IOException e) {
142+
throw new AssertionError(e);
143+
}
144+
}
145+
return Map.copyOf(componentTemplates);
146+
}
147+
148+
static List<IndexTemplateConfig> getComponentTemplateConfigsAsConfigs() {
149+
return List.of(
132150
new IndexTemplateConfig(
133151
DATA_STREAMS_MAPPINGS_COMPONENT_TEMPLATE_NAME,
134152
@@ -227,17 +245,7 @@ private Map<String, ComponentTemplate> loadComponentTemplateConfigs() {
227245
TEMPLATE_VERSION_VARIABLE,
228246
ADDITIONAL_TEMPLATE_VARIABLES
229247
)
230-
)) {
231-
try {
232-
componentTemplates.put(
233-
config.getTemplateName(),
234-
ComponentTemplate.parse(JsonXContent.jsonXContent.createParser(XContentParserConfiguration.EMPTY, config.loadBytes()))
235-
);
236-
} catch (IOException e) {
237-
throw new AssertionError(e);
238-
}
239-
}
240-
return Map.copyOf(componentTemplates);
248+
);
241249
}
242250

243251
@Override
@@ -259,7 +267,7 @@ private void updateEnabledSetting(boolean newValue) {
259267
}
260268
}
261269

262-
private static final List<LifecyclePolicyConfig> LIFECYCLE_POLICY_CONFIGS = List.of(
270+
static final List<LifecyclePolicyConfig> LIFECYCLE_POLICY_CONFIGS = List.of(
263271
new LifecyclePolicyConfig(LOGS_ILM_POLICY_NAME, "/[email protected]", ADDITIONAL_TEMPLATE_VARIABLES),
264272
new LifecyclePolicyConfig(METRICS_ILM_POLICY_NAME, "/[email protected]", ADDITIONAL_TEMPLATE_VARIABLES),
265273
new LifecyclePolicyConfig(SYNTHETICS_ILM_POLICY_NAME, "/[email protected]", ADDITIONAL_TEMPLATE_VARIABLES),
@@ -287,43 +295,49 @@ protected Map<String, ComponentTemplate> getComponentTemplateConfigs() {
287295
}
288296

289297
private static final Map<String, ComposableIndexTemplate> COMPOSABLE_INDEX_TEMPLATE_CONFIGS = parseComposableTemplates(
290-
new IndexTemplateConfig(
291-
LOGS_INDEX_TEMPLATE_NAME,
292-
293-
REGISTRY_VERSION,
294-
TEMPLATE_VERSION_VARIABLE,
295-
ADDITIONAL_TEMPLATE_VARIABLES
296-
),
297-
new IndexTemplateConfig(
298-
METRICS_INDEX_TEMPLATE_NAME,
299-
300-
REGISTRY_VERSION,
301-
TEMPLATE_VERSION_VARIABLE,
302-
ADDITIONAL_TEMPLATE_VARIABLES
303-
),
304-
new IndexTemplateConfig(
305-
SYNTHETICS_INDEX_TEMPLATE_NAME,
306-
307-
REGISTRY_VERSION,
308-
TEMPLATE_VERSION_VARIABLE,
309-
ADDITIONAL_TEMPLATE_VARIABLES
310-
),
311-
new IndexTemplateConfig(
312-
AGENTLESS_INDEX_TEMPLATE_NAME,
313-
314-
REGISTRY_VERSION,
315-
TEMPLATE_VERSION_VARIABLE,
316-
ADDITIONAL_TEMPLATE_VARIABLES
317-
),
318-
new IndexTemplateConfig(
319-
KIBANA_REPORTING_INDEX_TEMPLATE_NAME,
320-
321-
REGISTRY_VERSION,
322-
TEMPLATE_VERSION_VARIABLE,
323-
ADDITIONAL_TEMPLATE_VARIABLES
324-
)
298+
getComposableTemplateConfigsAsConfigs().toArray(new IndexTemplateConfig[0])
325299
);
326300

301+
static List<IndexTemplateConfig> getComposableTemplateConfigsAsConfigs() {
302+
return List.of(
303+
new IndexTemplateConfig(
304+
LOGS_INDEX_TEMPLATE_NAME,
305+
306+
REGISTRY_VERSION,
307+
TEMPLATE_VERSION_VARIABLE,
308+
ADDITIONAL_TEMPLATE_VARIABLES
309+
),
310+
new IndexTemplateConfig(
311+
METRICS_INDEX_TEMPLATE_NAME,
312+
313+
REGISTRY_VERSION,
314+
TEMPLATE_VERSION_VARIABLE,
315+
ADDITIONAL_TEMPLATE_VARIABLES
316+
),
317+
new IndexTemplateConfig(
318+
SYNTHETICS_INDEX_TEMPLATE_NAME,
319+
320+
REGISTRY_VERSION,
321+
TEMPLATE_VERSION_VARIABLE,
322+
ADDITIONAL_TEMPLATE_VARIABLES
323+
),
324+
new IndexTemplateConfig(
325+
AGENTLESS_INDEX_TEMPLATE_NAME,
326+
327+
REGISTRY_VERSION,
328+
TEMPLATE_VERSION_VARIABLE,
329+
ADDITIONAL_TEMPLATE_VARIABLES
330+
),
331+
new IndexTemplateConfig(
332+
KIBANA_REPORTING_INDEX_TEMPLATE_NAME,
333+
334+
REGISTRY_VERSION,
335+
TEMPLATE_VERSION_VARIABLE,
336+
ADDITIONAL_TEMPLATE_VARIABLES
337+
)
338+
);
339+
}
340+
327341
@Override
328342
protected Map<String, ComposableIndexTemplate> getComposableTemplateConfigs() {
329343
if (stackTemplateEnabled) {
@@ -333,7 +347,7 @@ protected Map<String, ComposableIndexTemplate> getComposableTemplateConfigs() {
333347
}
334348
}
335349

336-
private static final List<IngestPipelineConfig> INGEST_PIPELINE_CONFIGS = List.of(
350+
static final List<IngestPipelineConfig> INGEST_PIPELINE_CONFIGS = List.of(
337351
new JsonIngestPipelineConfig(
338352
"logs@json-pipeline",
339353

x-pack/plugin/stack/src/test/java/org/elasticsearch/xpack/stack/StackTemplateRegistryTests.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,17 +51,21 @@
5151
import org.elasticsearch.xpack.core.ilm.OperationMode;
5252
import org.elasticsearch.xpack.core.ilm.action.ILMActions;
5353
import org.elasticsearch.xpack.core.ilm.action.PutLifecycleRequest;
54+
import org.elasticsearch.xpack.core.template.IndexTemplateConfig;
5455
import org.elasticsearch.xpack.core.template.IngestPipelineConfig;
56+
import org.elasticsearch.xpack.core.template.LifecyclePolicyConfig;
5557
import org.junit.After;
5658
import org.junit.Before;
5759

5860
import java.io.IOException;
61+
import java.io.InputStream;
5962
import java.util.Collections;
6063
import java.util.HashMap;
6164
import java.util.List;
6265
import java.util.Map;
6366
import java.util.concurrent.atomic.AtomicInteger;
6467
import java.util.stream.Collectors;
68+
import java.util.zip.CRC32;
6569

6670
import static org.hamcrest.Matchers.anEmptyMap;
6771
import static org.hamcrest.Matchers.anyOf;
@@ -533,6 +537,42 @@ public void testThatTemplatesAreNotDeprecated() {
533537
.forEach(p -> assertFalse((Boolean) p.get("deprecated")));
534538
}
535539

540+
public void testRegistryIsUpToDate() throws Exception {
541+
CRC32 crc32 = new CRC32();
542+
for (IndexTemplateConfig config : StackTemplateRegistry.getComponentTemplateConfigsAsConfigs()) {
543+
crc32.update(loadTemplate(config.getFileName()));
544+
}
545+
for (LifecyclePolicyConfig config : StackTemplateRegistry.LIFECYCLE_POLICY_CONFIGS) {
546+
crc32.update(loadTemplate(config.getFileName()));
547+
}
548+
for (IndexTemplateConfig config : StackTemplateRegistry.getComposableTemplateConfigsAsConfigs()) {
549+
crc32.update(loadTemplate(config.getFileName()));
550+
}
551+
for (IngestPipelineConfig config : StackTemplateRegistry.INGEST_PIPELINE_CONFIGS) {
552+
crc32.update(loadTemplate(config.getFileName()));
553+
}
554+
String computedChecksum = Long.toHexString(crc32.getValue());
555+
assertEquals(
556+
"The StackTemplateRegistry.REGISTRY_VERSION must be incremented when templates are changed. "
557+
+ "Please update REGISTRY_VERSION to "
558+
+ (StackTemplateRegistry.REGISTRY_VERSION + 1)
559+
+ " and update COMPUTED_CHECKSUM to \""
560+
+ computedChecksum
561+
+ "\"",
562+
StackTemplateRegistry.COMPUTED_CHECKSUM,
563+
computedChecksum
564+
);
565+
}
566+
567+
private byte[] loadTemplate(String name) throws IOException {
568+
try (InputStream is = StackTemplateRegistry.class.getResourceAsStream(name)) {
569+
if (is == null) {
570+
throw new IOException("Template [" + name + "] not found");
571+
}
572+
return is.readAllBytes();
573+
}
574+
}
575+
536576
// -------------
537577

538578
/**

0 commit comments

Comments
 (0)