@Test
    public void application_deployment_reuses_node_indexes() {
        ProvisioningTester tester = new ProvisioningTester.Builder().zone(new Zone(Environment.prod, RegionName.from("us-east"))).build();

        ApplicationId app1 = ProvisioningTester.applicationId("app1");

        tester.makeReadyHosts(21, defaultResources).activateTenantHosts();

        // deploy
        SystemState state1 = prepare(app1, 2, 2, 3, 3, defaultResources, tester);
        tester.activate(app1, state1.allHosts);
        Set<Integer> state1Indexes = state1.allHosts.stream().map(hostSpec -> hostSpec.membership().get().index()).collect(Collectors.toSet());

        // deallocate 2 nodes with index 0
        Node node1 = tester.nodeRepository().nodes().node(tester.removeOne(state1.container0).hostname()).get();
        Node node2 = tester.nodeRepository().nodes().node(tester.removeOne(state1.content0).hostname()).get();
        tester.nodeRepository().nodes().removeRecursively(node1, true);
        tester.nodeRepository().nodes().removeRecursively(node2, true);

        // redeploy to get new nodes
        SystemState state2 = prepare(app1, 2, 2, 3, 3, defaultResources, tester);
        Set<Integer> state2Indexes = state2.allHosts.stream().map(hostSpec -> hostSpec.membership().get().index()).collect(Collectors.toSet());
        assertEquals("Indexes are reused", state1Indexes, state2Indexes);

        // if nodes are e.g failed indexes are not reused as they are still allocated
        tester.nodeRepository().nodes().fail(tester.removeOne(state2.container0).hostname(), Agent.system, "test");
        tester.nodeRepository().nodes().fail(tester.removeOne(state2.content0).hostname(), Agent.system, "test");
        SystemState state3 = prepare(app1, 2, 2, 3, 3, defaultResources, tester);
        Set<Integer> state3Indexes = state3.allHosts.stream().map(hostSpec -> hostSpec.membership().get().index()).collect(Collectors.toSet());
        assertNotEquals("Indexes are not reused", state2Indexes, state3Indexes);

    }
Various allocation sequence scenarios

@author bratseth
@author mpolden
node-repository/src/test/java/com/yahoo/vespa/hosted/provision/provisioning/ProvisioningTest.java