1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
|
// 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.model.api.ConfigChangeAction;
import com.yahoo.config.model.producer.TreeConfigProducer;
import com.yahoo.config.model.producer.AbstractConfigProducerRoot;
import com.yahoo.config.model.test.MockRoot;
import com.yahoo.vespa.model.AbstractService;
import com.yahoo.vespa.model.Host;
import com.yahoo.vespa.model.HostResource;
import com.yahoo.vespa.model.PortAllocBridge;
import org.junit.jupiter.api.Test;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
public class StartupCommandChangeValidatorTest {
@Test
void requireThatDifferentStartupCommandIsDetected() {
MockRoot oldRoot = createRootWithChildren(new ServiceStub("evilservice", "rm -rf /"));
MockRoot newRoot = createRootWithChildren(new ServiceStub("evilservice", "rm -rf *"));
List<ConfigChangeAction> changes = getStartupCommandChanges(oldRoot, newRoot);
assertEquals(1, changes.size());
assertEquals("evilservice", changes.get(0).getServices().get(0).getConfigId());
}
@Test
void requireEmptyResultForEqualStartupCommand() {
MockRoot oldRoot = createRootWithChildren(new ServiceStub("evilservice", "./hax.sh"));
MockRoot newRoot = createRootWithChildren(new ServiceStub("evilservice", "./hax.sh"));
List<ConfigChangeAction> changes = getStartupCommandChanges(oldRoot, newRoot);
assertTrue(changes.isEmpty());
}
@Test
void requireEmptyResultForDifferentServices() {
MockRoot oldRoot = createRootWithChildren(new ServiceStub("evilservice", "./hax.sh"));
MockRoot newRoot = createRootWithChildren(new ServiceStub("goodservice", "./hax.sh"));
List<ConfigChangeAction> changes = getStartupCommandChanges(oldRoot, newRoot);
assertTrue(changes.isEmpty());
}
private static List<ConfigChangeAction> getStartupCommandChanges(
AbstractConfigProducerRoot currentModel, AbstractConfigProducerRoot nextModel) {
StartupCommandChangeValidator validator = new StartupCommandChangeValidator();
return validator.findServicesWithChangedStartupCommand(currentModel, nextModel).toList();
}
private static MockRoot createRootWithChildren(TreeConfigProducer<?>... children) {
MockRoot root = new MockRoot();
Arrays.asList(children).forEach(root::addChild);
root.freezeModelTopology();
return root;
}
private static class ServiceStub extends AbstractService {
private final String startupCommand;
public ServiceStub(String name, String startupCommand) {
super(name);
setHostResource(new HostResource(new Host(null, "localhost")));
this.startupCommand = startupCommand;
}
@Override
public Optional<String> getStartupCommand() {
return Optional.ofNullable(startupCommand);
}
@Override
public int getPortCount() {
return 0;
}
@Override public void allocatePorts(int start, PortAllocBridge from) { }
}
}
|