aboutsummaryrefslogtreecommitdiffstats
path: root/clustercontroller-core/src/test/java/com/yahoo/vespa/clustercontroller/core/CleanupZookeeperLogsOnSuccess.java
blob: 3c0d9f5e49dc4ebc8cfc3bc09b077cc2453f81e1 (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.clustercontroller.core;

import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.api.extension.TestWatcher;

public class CleanupZookeeperLogsOnSuccess implements TestWatcher {

    public CleanupZookeeperLogsOnSuccess() {}

    @Override
    public void testFailed(ExtensionContext context, Throwable cause) {
        System.err.println("TEST FAILED - NOT cleaning up zookeeper directory");
        shutdownZooKeeper(context, false);
    }

    @Override
    public void testSuccessful(ExtensionContext context) {
        System.err.println("TEST SUCCEEDED - cleaning up zookeeper directory");
        shutdownZooKeeper(context, true);
    }

    private void shutdownZooKeeper(ExtensionContext ctx, boolean cleanupZooKeeperDir) {
        FleetControllerTest test = (FleetControllerTest) ctx.getTestInstance().orElseThrow();
        if (test.zooKeeperServer != null) {
            test.zooKeeperServer.shutdown(cleanupZooKeeperDir);
            test.zooKeeperServer = null;
        }
    }
}