Search in sources :

Example 1 with ApiClient

use of bio.terra.datarepo.client.ApiClient in project jade-data-repo by DataBiosphere.

the class TestRunner method executeTestConfiguration.

void executeTestConfiguration() throws Exception {
    // specify any value overrides in the Helm chart, then deploy
    if (!config.server.skipDeployment) {
    // modifyHelmValuesAndDeploy();
    }
    // update any Kubernetes properties specified by the test configuration
    if (!config.server.skipKubernetes) {
        KubernetesClientUtils.buildKubernetesClientObject(config.server);
        modifyKubernetesPostDeployment();
    }
    // get an instance of the API client per test user
    for (TestUserSpecification testUser : config.testUsers) {
        ApiClient apiClient = new ApiClient();
        apiClient.setBasePath(config.server.uri);
        GoogleCredentials userCredential = AuthenticationUtils.getDelegatedUserCredential(testUser);
        AccessToken userAccessToken = AuthenticationUtils.getAccessToken(userCredential);
        apiClient.setAccessToken(userAccessToken.getTokenValue());
        apiClientsForUsers.put(testUser.name, apiClient);
    }
    // get an instance of each test script class
    for (TestScriptSpecification testScriptSpecification : config.testScripts) {
        try {
            TestScript testScriptInstance = testScriptSpecification.scriptClass.newInstance();
            // set the billing account for the test script to use
            testScriptInstance.setBillingAccount(config.billingAccount);
            // set any parameters specified by the configuration
            testScriptInstance.setParameters(testScriptSpecification.parameters);
            scripts.add(testScriptInstance);
        } catch (IllegalAccessException | InstantiationException niEx) {
            throw new IllegalArgumentException("Error calling constructor of TestScript class: " + testScriptSpecification.scriptClass.getName(), niEx);
        }
    }
    // call the setup method of each test script
    Exception setupExceptionThrown = callTestScriptSetups();
    if (setupExceptionThrown != null) {
        // ignore any exceptions thrown by cleanup methods
        callTestScriptCleanups();
        throw new RuntimeException("Error calling test script setup methods.", setupExceptionThrown);
    }
    // for each test script
    List<ApiClient> apiClientList = new ArrayList<>(apiClientsForUsers.values());
    for (int tsCtr = 0; tsCtr < scripts.size(); tsCtr++) {
        TestScript testScript = scripts.get(tsCtr);
        TestScriptSpecification testScriptSpecification = config.testScripts.get(tsCtr);
        // add a description to the user journey threads/results that includes any parameters
        String userJourneyDescription = testScriptSpecification.name;
        if (testScriptSpecification.parameters != null) {
            userJourneyDescription += ": " + String.join(",", testScriptSpecification.parameters);
        }
        // create a thread pool for running its user journeys
        ThreadPoolExecutor threadPool = (ThreadPoolExecutor) Executors.newFixedThreadPool(testScriptSpecification.numberToRunInParallel);
        threadPools.add(threadPool);
        // kick off the user journey(s), one per thread
        List<UserJourneyThread> userJourneyThreads = new ArrayList<>();
        for (int ujCtr = 0; ujCtr < testScriptSpecification.totalNumberToRun; ujCtr++) {
            ApiClient apiClient = apiClientList.get(ujCtr % apiClientList.size());
            userJourneyThreads.add(new UserJourneyThread(testScript, userJourneyDescription, apiClient));
        }
        // TODO: support different patterns of kicking off user journeys. here they're all queued at
        // once
        List<Future<UserJourneyResult>> userJourneyFutures = threadPool.invokeAll(userJourneyThreads);
        userJourneyFutureLists.add(userJourneyFutures);
    }
    // wait until all threads either finish or time out
    for (int ctr = 0; ctr < scripts.size(); ctr++) {
        TestScriptSpecification testScriptSpecification = config.testScripts.get(ctr);
        ThreadPoolExecutor threadPool = threadPools.get(ctr);
        threadPool.shutdown();
        long totalTerminationTime = testScriptSpecification.expectedTimeForEach * testScriptSpecification.totalNumberToRun;
        boolean terminatedByItself = threadPool.awaitTermination(totalTerminationTime, testScriptSpecification.expectedTimeForEachUnitObj);
        // if the threads didn't finish in the expected time, then send them interrupts
        if (!terminatedByItself) {
            threadPool.shutdownNow();
        }
        if (!threadPool.awaitTermination(secondsToWaitForPoolShutdown, TimeUnit.SECONDS)) {
            System.out.println("Thread pool for test script " + ctr + " (" + testScriptSpecification.name + ") failed to terminate.");
        }
    }
    // compile the results from all thread pools
    for (int ctr = 0; ctr < scripts.size(); ctr++) {
        List<Future<UserJourneyResult>> userJourneyFutureList = userJourneyFutureLists.get(ctr);
        TestScriptSpecification testScriptSpecification = config.testScripts.get(ctr);
        for (Future<UserJourneyResult> userJourneyFuture : userJourneyFutureList) {
            UserJourneyResult result = null;
            if (userJourneyFuture.isDone())
                try {
                    // user journey thread completed and populated its own return object, which may include
                    // an exception
                    result = userJourneyFuture.get();
                    result.completed = true;
                } catch (ExecutionException execEx) {
                    // user journey thread threw an exception and didn't populate its own return object
                    result = new UserJourneyResult(testScriptSpecification.name, "");
                    result.completed = false;
                    result.exceptionThrown = execEx;
                }
            else {
                // user journey either was never started or got cancelled before it finished
                result = new UserJourneyResult(testScriptSpecification.name, "");
                result.completed = false;
            }
            userJourneyResults.add(result);
        }
    }
    // call the cleanup method of each test script
    Exception cleanupExceptionThrown = callTestScriptCleanups();
    if (cleanupExceptionThrown != null) {
        throw new RuntimeException("Error calling test script cleanup methods.", cleanupExceptionThrown);
    }
    // delete the deployment and restore any Kubernetes settings
    if (!config.server.skipDeployment) {
        deleteDeployment();
    }
    if (!config.server.skipKubernetes) {
        restoreKubernetesSettings();
    }
    // cleanup data project
    cleanupLeftoverTestData();
}
Also used : ArrayList(java.util.ArrayList) AccessToken(com.google.auth.oauth2.AccessToken) GoogleCredentials(com.google.auth.oauth2.GoogleCredentials) TestScriptSpecification(runner.config.TestScriptSpecification) TestUserSpecification(runner.config.TestUserSpecification) ExecutionException(java.util.concurrent.ExecutionException) ApiClient(bio.terra.datarepo.client.ApiClient) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) Future(java.util.concurrent.Future) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor)

Example 2 with ApiClient

use of bio.terra.datarepo.client.ApiClient in project jade-data-repo by DataBiosphere.

the class TestScript method main.

/**
 * This method runs the test script methods in the expected order: setup, userJourney, cleanup. It
 * is intended for easier debugging (e.g. API calls, building request models) when writing a new
 * script.
 *
 * @param args
 */
public static void main(String[] args) throws Exception {
    // get an instance of the script and the API client
    TestScript testScript = new TestScript();
    ApiClient apiClient = Configuration.getDefaultApiClient();
    Map<String, ApiClient> apiClients = new HashMap<>();
    apiClients.put("default", apiClient);
    testScript.setup(apiClients);
    testScript.userJourney(apiClient);
    testScript.cleanup(apiClients);
}
Also used : HashMap(java.util.HashMap) ApiClient(bio.terra.datarepo.client.ApiClient)

Example 3 with ApiClient

use of bio.terra.datarepo.client.ApiClient in project jade-data-repo by DataBiosphere.

the class IngestFile method cleanup.

public void cleanup(Map<String, ApiClient> apiClients) throws Exception {
    // get the ApiClient for the dataset creator
    ApiClient datasetCreatorClient = apiClients.get(datasetCreator);
    ResourcesApi resourcesApi = new ResourcesApi(datasetCreatorClient);
    RepositoryApi repositoryApi = new RepositoryApi(datasetCreatorClient);
    // make the delete request and wait for the job to finish
    JobModel deleteDatasetJobResponse = repositoryApi.deleteDataset(datasetSummaryModel.getId());
    deleteDatasetJobResponse = DataRepoUtils.waitForJobToFinish(repositoryApi, deleteDatasetJobResponse);
    DataRepoUtils.expectJobSuccess(repositoryApi, deleteDatasetJobResponse, DeleteResponseModel.class);
    System.out.println("successfully deleted dataset: " + datasetSummaryModel.getName());
    // delete the profile
    resourcesApi.deleteProfile(billingProfileModel.getId());
    System.out.println("successfully deleted profile: " + billingProfileModel.getProfileName());
}
Also used : RepositoryApi(bio.terra.datarepo.api.RepositoryApi) ResourcesApi(bio.terra.datarepo.api.ResourcesApi) JobModel(bio.terra.datarepo.model.JobModel) ApiClient(bio.terra.datarepo.client.ApiClient)

Example 4 with ApiClient

use of bio.terra.datarepo.client.ApiClient in project jade-data-repo by DataBiosphere.

the class IngestFile method setup.

public void setup(Map<String, ApiClient> apiClients) throws Exception {
    // pick the first user to be the dataset creator
    List<String> apiClientList = new ArrayList<>(apiClients.keySet());
    datasetCreator = apiClientList.get(0);
    // get the ApiClient for the dataset creator
    ApiClient datasetCreatorClient = apiClients.get(datasetCreator);
    ResourcesApi resourcesApi = new ResourcesApi(datasetCreatorClient);
    RepositoryApi repositoryApi = new RepositoryApi(datasetCreatorClient);
    // create a new profile
    billingProfileModel = DataRepoUtils.createProfile(resourcesApi, billingAccount, "profile-simple", true);
    System.out.println("successfully created profile: " + billingProfileModel.getProfileName());
    // make the create dataset request and wait for the job to finish
    JobModel createDatasetJobResponse = DataRepoUtils.createDataset(repositoryApi, billingProfileModel.getId(), "dataset-simple.json", true);
    // save a reference to the dataset summary model so we can delete it in cleanup()
    datasetSummaryModel = DataRepoUtils.expectJobSuccess(repositoryApi, createDatasetJobResponse, DatasetSummaryModel.class);
    System.out.println("successfully created dataset: " + datasetSummaryModel.getName());
}
Also used : ArrayList(java.util.ArrayList) RepositoryApi(bio.terra.datarepo.api.RepositoryApi) ResourcesApi(bio.terra.datarepo.api.ResourcesApi) JobModel(bio.terra.datarepo.model.JobModel) DatasetSummaryModel(bio.terra.datarepo.model.DatasetSummaryModel) ApiClient(bio.terra.datarepo.client.ApiClient)

Example 5 with ApiClient

use of bio.terra.datarepo.client.ApiClient in project jade-data-repo by DataBiosphere.

the class RetrieveDataset method cleanup.

public void cleanup(Map<String, ApiClient> apiClients) throws Exception {
    // get the ApiClient for the dataset creator
    ApiClient datasetCreatorClient = apiClients.get(datasetCreator);
    ResourcesApi resourcesApi = new ResourcesApi(datasetCreatorClient);
    RepositoryApi repositoryApi = new RepositoryApi(datasetCreatorClient);
    // make the delete dataset request and wait for the job to finish
    JobModel deleteDatasetJobResponse = repositoryApi.deleteDataset(datasetSummaryModel.getId());
    deleteDatasetJobResponse = DataRepoUtils.waitForJobToFinish(repositoryApi, deleteDatasetJobResponse);
    DataRepoUtils.expectJobSuccess(repositoryApi, deleteDatasetJobResponse, DeleteResponseModel.class);
    System.out.println("successfully deleted dataset: " + datasetSummaryModel.getName());
    // delete the profile
    resourcesApi.deleteProfile(billingProfileModel.getId());
    System.out.println("successfully deleted profile: " + billingProfileModel.getProfileName());
}
Also used : RepositoryApi(bio.terra.datarepo.api.RepositoryApi) ResourcesApi(bio.terra.datarepo.api.ResourcesApi) JobModel(bio.terra.datarepo.model.JobModel) ApiClient(bio.terra.datarepo.client.ApiClient)

Aggregations

ApiClient (bio.terra.datarepo.client.ApiClient)7 RepositoryApi (bio.terra.datarepo.api.RepositoryApi)4 ResourcesApi (bio.terra.datarepo.api.ResourcesApi)4 JobModel (bio.terra.datarepo.model.JobModel)4 ArrayList (java.util.ArrayList)3 DatasetSummaryModel (bio.terra.datarepo.model.DatasetSummaryModel)2 AccessToken (com.google.auth.oauth2.AccessToken)1 GoogleCredentials (com.google.auth.oauth2.GoogleCredentials)1 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 ExecutionException (java.util.concurrent.ExecutionException)1 Future (java.util.concurrent.Future)1 ThreadPoolExecutor (java.util.concurrent.ThreadPoolExecutor)1 TestScriptSpecification (runner.config.TestScriptSpecification)1 TestUserSpecification (runner.config.TestUserSpecification)1