aboutsummaryrefslogtreecommitdiffstats
path: root/config-model/src/test/java/com/yahoo/vespa/model/application/validation/ContainerInCloudValidatorTest.java
blob: 43c51bea04acb8c8156d750e1dbbb8637d6d76b2 (plain) (blame)
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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.model.application.validation;

import com.yahoo.config.application.api.ApplicationPackage;
import com.yahoo.config.model.NullConfigModelRegistry;
import com.yahoo.config.model.api.ApplicationClusterEndpoint;
import com.yahoo.config.model.api.ContainerEndpoint;
import com.yahoo.config.model.deploy.DeployState;
import com.yahoo.config.model.deploy.TestProperties;
import com.yahoo.config.model.test.MockApplicationPackage;
import com.yahoo.vespa.model.VespaModel;
import org.junit.jupiter.api.Test;
import org.xml.sax.SAXException;

import java.io.IOException;
import java.util.List;
import java.util.Set;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

public class ContainerInCloudValidatorTest {

    @Test
    void failsWhenNoContainerInCloud() throws IOException, SAXException {
        String noContainer = "";
        String container = """
                           <container id='routing' version='1.0'>
                             <nodes count='2' />
                           </container>
                           """;
        runValidatorOnApp(false, container);
        runValidatorOnApp(false, noContainer);
        runValidatorOnApp(true, container);
        assertEquals("Vespa Cloud applications must have at least one container cluster",
                     assertThrows(IllegalArgumentException.class,
                                  () -> runValidatorOnApp(true, noContainer))
                             .getMessage());
    }

    private static void runValidatorOnApp(boolean isHosted, String container) throws IOException, SAXException {
        String servicesXml = """
                        <services version='1.0'>
                          %s
                          <content id='foo' version='1.0'>
                            <redundancy>2</redundancy>
                            <documents>
                            </documents>
                            <nodes count='2' />
                          </content>
                        </services>
                """.formatted(container);
        ApplicationPackage app = new MockApplicationPackage.Builder()
                .withServices(servicesXml)
                .build();
        DeployState.Builder builder = new DeployState.Builder()
                .applicationPackage(app)
                .properties(new TestProperties().setHostedVespa(isHosted).setAllowUserFilters(false));
        if (isHosted) {
            builder.endpoints(Set.of(new ContainerEndpoint("routing", ApplicationClusterEndpoint.Scope.zone, List.of("routing.example.com"))));
        }
        DeployState deployState = builder.build();
        VespaModel model = new VespaModel(new NullConfigModelRegistry(), deployState);
        new ContainerInCloudValidator().validate(model, deployState);
    }

}