Example #1
0
        /// <summary>
        /// Imports tenant ZIP archive to current tenant (see <see
        /// cref="ActionManager.CurrentTenant"/>). The ZIP file will be taken by path in
        /// <see cref="TestConfig.BrowserDownloadFolder"/>.
        /// </summary>
        /// <param name="fileName">Exported tenant archive file name without path (like
        /// tenantCode_[timestamp].zip)</param>
        public static void ImportTenant(string fileName)
        {
            var tenantFilePath = string.Empty;
            var token          = BackgroundTaskApi.GetPackageSas();

            try
            {
                var blob = new CloudAppendBlob(new Uri(token));
                tenantFilePath = Path.Combine(TestConfig.BrowserDownloadFolder, fileName);
                blob.UploadFromFile(tenantFilePath);
                blob.SetProperties();
            }
            catch (Exception e)
            {
                throw new FileLoadException(
                          $"Tenant API: Error uploading file {tenantFilePath} to cloud: {e.Message}");
            }

            var task = BackgroundTaskApi.NewBackgroundTask(TaskActionType.ImportTenant, fileName,
                                                           new FileInfo(tenantFilePath).Length,
                                                           token);

            if (task == null || string.IsNullOrEmpty(task.TaskId))
            {
                throw new Exception(
                          $"Could not start new background task for tenant archive '{fileName}' import");
            }
            var body = new AppImport
            {
                PackageSas = token,
                TaskId     = task.TaskId
            };

            BackgroundTaskApi.ProcessedObjects.AddOrUpdate(task.TaskId,
                                                           (null, null, new ManualResetEvent(false)),
                                                           (k, v) => (null, null, new ManualResetEvent(false)));
            Trace.TraceInformation($"Tenant API: Import task {task.TaskId} started");

            RestController.HttpRequestJson(UriCxm.BackgroundTaskExecution, Method.POST, body);

            if (BackgroundTaskApi.ProcessedObjects.TryGetValue(task.TaskId, out var tenantObj) &&
                tenantObj.Item3.WaitOne(TimeSpan.FromSeconds(TestConfig.TenantImportTimeout)))
            {
                Trace.TraceInformation($"Tenant API: Import task {task.TaskId} complete");
            }
            else
            {
                Trace.TraceError($"Tenant API: {task.TaskId} import timed out!");
                BackgroundTaskApi.DeleteTaskId(task.TaskId);
                throw new TimeoutException(
                          $"Tenant API: Import to tenant {ActionManager.CurrentTenantCode} timed out after " +
                          $"{TestConfig.TenantImportTimeout} s. File {fileName}.");
            }

            BackgroundTaskApi.ProcessedObjects.TryRemove(task.TaskId, out _);
            BackgroundTaskApi.DeleteTaskId(task.TaskId);
        }
Example #2
0
        /// <summary>
        /// Imports app from file by app version in file name
        /// </summary>
        /// <param name="path">Full path to file</param>
        /// <param name="mask">File name mask (like name*.zip) or file name without path</param>
        /// <param name="version">App version</param>
        /// <returns>(<see cref="AppResponse"/>) App object</returns>
        public static AppResponse ImportApp(string path, string mask, string version)
        {
            var appFilePath = string.Empty;
            var token       = BackgroundTaskApi.GetPackageSas();

            try
            {
                var blob = new CloudAppendBlob(new Uri(token));
                appFilePath = FileManager.GetFileByVersion(path, mask, version);
                blob.UploadFromFile(appFilePath);
                blob.SetProperties();
            }
            catch (Exception e)
            {
                throw new FileLoadException(
                          $"App API: Error uploading file {appFilePath} to cloud: {e.Message}");
            }

            var fileName = Path.GetFileName(appFilePath);
            var task     = BackgroundTaskApi.NewBackgroundTask(TaskActionType.UploadApp, fileName,
                                                               new FileInfo(appFilePath).Length,
                                                               token);

            if (task == null || string.IsNullOrEmpty(task.TaskId))
            {
                throw new Exception($"Could not start new background task for app '{fileName}' import");
            }
            var body = new AppImport
            {
                PackageSas = token,
                TaskId     = task.TaskId
            };

            BackgroundTaskApi.ProcessedObjects.AddOrUpdate(task.TaskId,
                                                           (null, null, new ManualResetEvent(false)),
                                                           (k, v) => (null, null, new ManualResetEvent(false)));
            Trace.TraceInformation($"App API: Import task {task.TaskId} started");

            RestController.HttpRequestJson(UriCxm.BackgroundTaskExecution, Method.POST, body);

            if (BackgroundTaskApi.ProcessedObjects.TryGetValue(task.TaskId, out var appObj) &&
                appObj.Item3.WaitOne(TimeSpan.FromSeconds(TestConfig.AppImportTimeout)))
            {
                Trace.TraceInformation($"App API: Import task {task.TaskId} complete");
            }
            else
            {
                Trace.TraceError($"App API: {task.TaskId} timed out!");
                throw new TimeoutException(
                          $"App API: Import on tenant {ActionManager.CurrentTenantCode} timed out after " +
                          $"{TestConfig.AppImportTimeout} s. File {fileName}.");
            }

            var result = BackgroundTaskApi.ProcessedObjects.TryGetValue(task.TaskId, out appObj) ?
                         appObj.Item1 as AppResponse :
                         null;

            BackgroundTaskApi.ProcessedObjects.TryRemove(task.TaskId, out _);

            return(result);
        }