@Test
    public void delete_host_only_after_all_the_children_have_been_deleted() {
        NodeRepositoryTester tester = new NodeRepositoryTester();

        tester.addHost("id1", "host1", "default", NodeType.host);
        tester.addHost("id2", "host2", "default", NodeType.host);
        tester.addNode("node10", "node10", "host1", "docker", NodeType.tenant);
        tester.addNode("node11", "node11", "host1", "docker", NodeType.tenant);
        tester.addNode("node12", "node12", "host1", "docker", NodeType.tenant);
        tester.addNode("node20", "node20", "host2", "docker", NodeType.tenant);
        tester.setNodeState("node11", Node.State.active);
        assertEquals(6, tester.nodeRepository().nodes().list().size());

        try {
            tester.nodeRepository().nodes().removeRecursively("host1");
            fail("Should not be able to delete host node, one of the children is in state active");
        } catch (IllegalArgumentException ignored) {
            // Expected
        }
        assertEquals(6, tester.nodeRepository().nodes().list().size());

        // Should be OK to delete host2 as both host2 and its only child, node20, are in state provisioned
        tester.nodeRepository().nodes().removeRecursively("host2");
        assertEquals(5, tester.nodeRepository().nodes().list().size());
        assertEquals(Node.State.deprovisioned, tester.nodeRepository().nodes().node("host2").get().state());

        // Now node10 is in provisioned, set node11 to failed and node12 to ready, and it should be OK to delete host1
        tester.nodeRepository().nodes().fail("node11", Agent.system, getClass().getSimpleName());
        tester.nodeRepository().nodes().setReady(tester.nodeRepository().nodes().lockAndGetRequired("node12"), Agent.system, getClass().getSimpleName());
        tester.nodeRepository().nodes().removeRecursively("node12"); // Remove one of the children first instead
        assertEquals(4, tester.nodeRepository().nodes().list().size());
        tester.nodeRepository().nodes().removeRecursively("host1");
        assertEquals(Node.State.deprovisioned, tester.nodeRepository().nodes().node("host1").get().state());
        assertEquals(IP.Config.EMPTY.primary(), tester.nodeRepository().nodes().node("host1").get().ipConfig().primary());
    }
tests basic operation of the node repository

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