// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.model.application.validation.change;
import com.yahoo.config.application.api.ValidationOverrides.ValidationException;
import com.yahoo.config.model.api.ConfigChangeAction;
import com.yahoo.config.model.api.ConfigChangeReindexAction;
import com.yahoo.config.provision.Environment;
import com.yahoo.vespa.model.VespaModel;
import com.yahoo.vespa.model.application.validation.ValidationTester;
import org.junit.jupiter.api.Test;
import java.util.List;
import java.util.stream.Collectors;
import static org.junit.jupiter.api.Assertions.*;
/**
* @author bratseth
* @author bjorncs
*/
public class IndexingModeChangeValidatorTest {
@Test
void testChangingIndexModeFromIndexedToStreamingWhenDisallowed() {
ValidationTester tester = new ValidationTester();
VespaModel oldModel =
tester.deploy(null, getServices("index"), Environment.prod, "").getFirst();
try {
List changeActions =
tester.deploy(oldModel, getServices("streaming"), Environment.prod, "").getSecond();
fail("Should throw on disallowed config change action");
}
catch (ValidationException e) {
assertEquals("indexing-mode-change:\n" +
"\tDocument type 'music' in cluster 'default' changed indexing mode from 'indexed' to 'streaming'\n" +
"To allow this add indexing-mode-change to validation-overrides.xml, see https://docs.vespa.ai/en/reference/validation-overrides.html",
e.getMessage());
}
}
@Test
void testChangingIndexModeFromIndexedToStreaming() {
ValidationTester tester = new ValidationTester();
VespaModel oldModel =
tester.deploy(null, getServices("index"), Environment.prod, validationOverrides).getFirst();
List changeActions =
tester.deploy(oldModel, getServices("streaming"), Environment.prod, validationOverrides).getSecond();
assertReindexingChange( // allowed=true due to validation override
"Document type 'music' in cluster 'default' changed indexing mode from 'indexed' to 'streaming'",
changeActions);
}
@Test
void testChangingIndexModeFromStoreOnlyToIndexed() {
ValidationTester tester = new ValidationTester();
VespaModel oldModel =
tester.deploy(null, getServices("index"), Environment.prod, validationOverrides).getFirst();
List changeActions =
tester.deploy(oldModel, getServices("store-only"), Environment.prod, validationOverrides).getSecond();
assertReindexingChange( // allowed=true due to validation override
"Document type 'music' in cluster 'default' changed indexing mode from 'indexed' to 'store-only'",
changeActions);
}
private void assertReindexingChange(String message, List changeActions) {
List reindexingActions = changeActions.stream()
.filter(a -> a instanceof ConfigChangeReindexAction)
.toList();
assertEquals(1, reindexingActions.size());
assertTrue(reindexingActions.get(0) instanceof ConfigChangeReindexAction);
assertEquals("indexing-mode-change", ((ConfigChangeReindexAction)reindexingActions.get(0)).name());
assertEquals(message, reindexingActions.get(0).getMessage());
}
private static String getServices(String indexingMode) {
return "" +
" " +
" " +
" " +
" " +
" 1" +
" " +
" " +
" " +
" " +
" " +
"";
}
private static final String validationOverrides =
"\n" +
" indexing-mode-change\n" +
"\n";
}