Exemplo n.º 1
0
        public void TestJobsMagic()
        {
            // no arguments - should print job status of all jobs
            var azureClient = new MockAzureClient();
            var jobsMagic   = new JobsMagic(azureClient, new UnitTestLogger <JobsMagic>());

            jobsMagic.Test(string.Empty);
            Assert.AreEqual(AzureClientAction.GetJobList, azureClient.LastAction);

            // with default argument - should still print job status
            azureClient = new MockAzureClient();
            jobsMagic   = new JobsMagic(azureClient, new UnitTestLogger <JobsMagic>());
            jobsMagic.Test($"{jobId}");
            Assert.AreEqual(AzureClientAction.GetJobList, azureClient.LastAction);

            // with default and count arguments - should still print job status
            azureClient = new MockAzureClient();
            jobsMagic   = new JobsMagic(azureClient, new UnitTestLogger <JobsMagic>());
            jobsMagic.Test($"{jobId} count=1");
            Assert.AreEqual(AzureClientAction.GetJobList, azureClient.LastAction);

            // only with count argument - should still print job status
            azureClient = new MockAzureClient();
            jobsMagic   = new JobsMagic(azureClient, new UnitTestLogger <JobsMagic>());
            jobsMagic.Test($"count=1");
            Assert.AreEqual(AzureClientAction.GetJobList, azureClient.LastAction);
        }
Exemplo n.º 2
0
        public void TestExecuteMagic()
        {
            // no arguments
            var azureClient  = new MockAzureClient();
            var executeMagic = new ExecuteMagic(azureClient);

            executeMagic.Test(string.Empty);
            Assert.AreEqual(AzureClientAction.ExecuteJob, azureClient.LastAction);

            // single argument
            executeMagic.Test($"{operationName}");
            Assert.AreEqual(AzureClientAction.ExecuteJob, azureClient.LastAction);
            Assert.IsTrue(azureClient.ExecutedJobs.Contains(operationName));
        }
Exemplo n.º 3
0
        public void TestSubmitMagic()
        {
            // no arguments
            var azureClient = new MockAzureClient();
            var submitMagic = new SubmitMagic(azureClient);

            submitMagic.Test(string.Empty);
            Assert.AreEqual(AzureClientAction.SubmitJob, azureClient.LastAction);

            // single argument
            submitMagic.Test($"{operationName}");
            Assert.AreEqual(AzureClientAction.SubmitJob, azureClient.LastAction);
            Assert.IsTrue(azureClient.SubmittedJobs.Contains(operationName));
        }
Exemplo n.º 4
0
        public void TestJobsMagic()
        {
            // no arguments - should print job status of all jobs
            var azureClient = new MockAzureClient();
            var jobsMagic   = new JobsMagic(azureClient);

            jobsMagic.Test(string.Empty);
            Assert.AreEqual(AzureClientAction.GetJobList, azureClient.LastAction);

            // with arguments - should still print job status
            azureClient = new MockAzureClient();
            jobsMagic   = new JobsMagic(azureClient);
            jobsMagic.Test($"{jobId}");
            Assert.AreEqual(AzureClientAction.GetJobList, azureClient.LastAction);
        }
Exemplo n.º 5
0
        public void TestConnectMagicFromEnvironment()
        {
            var azureClient  = new MockAzureClient();
            var logger       = new UnitTestLogger <ConnectMagic>();
            var config       = new ConfigurationSource(skipLoading: true);
            var connectMagic = new ConnectMagic(azureClient, config, logger);

            // no input
            connectMagic.Test(string.Empty);
            Assert.AreEqual(AzureClientAction.GetConnectionStatus, azureClient.LastAction);

            // Missing environment variables
            connectMagic.Test($"credential=ENVIRONMENT", ExecuteStatus.Error);

            // Pick up environment variables
            System.Environment.SetEnvironmentVariable(EnvironmentSubscriptionId, subscriptionId);
            System.Environment.SetEnvironmentVariable(EnvironmentResourceGroup, resourceGroupName);
            System.Environment.SetEnvironmentVariable(EnvironmentWorkspaceName, workspaceName);
            System.Environment.SetEnvironmentVariable(EnvironmentLocation, location);

            // Temporarily set the environment variables with old prefix, as the renaming
            // has not being applied to the C# client yet
            System.Environment.SetEnvironmentVariable("AZUREQUANTUM_SUBSCRIPTION_ID", subscriptionId);
            System.Environment.SetEnvironmentVariable("AZUREQUANTUM_WORKSPACE_RG", resourceGroupName);
            System.Environment.SetEnvironmentVariable("AZUREQUANTUM_WORKSPACE_NAME", workspaceName);
            System.Environment.SetEnvironmentVariable("AZUREQUANTUM_WORKSPACE_LOCATION", location);

            connectMagic.Test("credential=ENVIRONMENT");
            Assert.AreEqual(AzureClientAction.Connect, azureClient.LastAction);
            Assert.AreEqual(subscriptionId, azureClient.SubscriptionId);
            Assert.AreEqual(resourceGroupName, azureClient.ResourceGroupName);
            Assert.AreEqual(workspaceName, azureClient.WorkspaceName);
            Assert.AreEqual(string.Empty, azureClient.ConnectionString);
            Assert.AreEqual(CredentialType.Environment, azureClient.CredentialType);
            Assert.AreEqual(location, azureClient.Location);

            // Reset env variables:
            System.Environment.SetEnvironmentVariable(EnvironmentSubscriptionId, string.Empty);
            System.Environment.SetEnvironmentVariable(EnvironmentResourceGroup, string.Empty);
            System.Environment.SetEnvironmentVariable(EnvironmentWorkspaceName, string.Empty);
            System.Environment.SetEnvironmentVariable(EnvironmentLocation, string.Empty);
        }
Exemplo n.º 6
0
        public void TestTargetMagic()
        {
            // single argument - should set active target
            var azureClient = new MockAzureClient();
            var targetMagic = new TargetMagic(azureClient, new UnitTestLogger <TargetMagic>());

            targetMagic.Test(targetId);
            Assert.AreEqual(AzureClientAction.SetActiveTarget, azureClient.LastAction);

            // single argument with quotes - should set active target
            targetMagic = new TargetMagic(azureClient, new UnitTestLogger <TargetMagic>());
            targetMagic.Test($"\"{targetId}\"");
            Assert.AreEqual(AzureClientAction.SetActiveTarget, azureClient.LastAction);

            // no arguments - should print active target
            azureClient = new MockAzureClient();
            targetMagic = new TargetMagic(azureClient, new UnitTestLogger <TargetMagic>());
            targetMagic.Test(string.Empty);
            Assert.AreEqual(AzureClientAction.GetActiveTarget, azureClient.LastAction);
        }
Exemplo n.º 7
0
        public void TestOutputMagic()
        {
            // no arguments - should print job result of most recent job
            var azureClient = new MockAzureClient();
            var outputMagic = new OutputMagic(azureClient, new UnitTestLogger <OutputMagic>());

            outputMagic.Test(string.Empty);
            Assert.AreEqual(AzureClientAction.GetJobResult, azureClient.LastAction);

            // single argument - should print job result
            azureClient = new MockAzureClient();
            outputMagic = new OutputMagic(azureClient, new UnitTestLogger <OutputMagic>());
            outputMagic.Test($"{jobId}");
            Assert.AreEqual(AzureClientAction.GetJobResult, azureClient.LastAction);

            // single argument with quotes - should print job result
            azureClient = new MockAzureClient();
            outputMagic = new OutputMagic(azureClient, new UnitTestLogger <OutputMagic>());
            outputMagic.Test($"'{jobId}'");
            Assert.AreEqual(AzureClientAction.GetJobResult, azureClient.LastAction);
        }
Exemplo n.º 8
0
        public void TestStatusMagic()
        {
            // no arguments - should print job status of most recent job
            var azureClient = new MockAzureClient();
            var statusMagic = new StatusMagic(azureClient, new UnitTestLogger <StatusMagic>());

            statusMagic.Test(string.Empty);
            Assert.AreEqual(AzureClientAction.GetJobStatus, azureClient.LastAction);

            // single argument - should print job status
            azureClient = new MockAzureClient();
            statusMagic = new StatusMagic(azureClient, new UnitTestLogger <StatusMagic>());
            statusMagic.Test($"{jobId}");
            Assert.AreEqual(AzureClientAction.GetJobStatus, azureClient.LastAction);

            // single argument with quotes - should print job status
            azureClient = new MockAzureClient();
            statusMagic = new StatusMagic(azureClient, new UnitTestLogger <StatusMagic>());
            statusMagic.Test($"\"{jobId}\"");
            Assert.AreEqual(AzureClientAction.GetJobStatus, azureClient.LastAction);
        }
Exemplo n.º 9
0
        public void TestSubmitMagic()
        {
            // no arguments
            var azureClient = new MockAzureClient();
            var submitMagic = new SubmitMagic(azureClient, new UnitTestLogger <SubmitMagic>());

            submitMagic.Test(string.Empty);
            Assert.AreEqual(AzureClientAction.SubmitJob, azureClient.LastAction);

            // single argument
            submitMagic.Test($"{operationName}");
            Assert.AreEqual(AzureClientAction.SubmitJob, azureClient.LastAction);
            Assert.IsTrue(azureClient.SubmittedJobs.Contains(operationName));

            // jobParams argument
            Assert.IsTrue(azureClient.JobParams.IsEmpty);
            submitMagic.Test($"{operationName} jobParams={jobParams}");
            Assert.IsTrue(azureClient.JobParams.TryGetValue(jobParamsKey1, out string value1));
            Assert.AreEqual(value1, jobParamsVal1);
            Assert.IsTrue(azureClient.JobParams.TryGetValue(jobParamsKey2, out string value2));
            Assert.AreEqual(value2, jobParamsVal2);
        }
Exemplo n.º 10
0
        public void TestConnectMagic()
        {
            var azureClient  = new MockAzureClient();
            var logger       = new UnitTestLogger <ConnectMagic>();
            var config       = new ConfigurationSource(skipLoading: true);
            var connectMagic = new ConnectMagic(azureClient, config, logger);

            // no input
            connectMagic.Test(string.Empty);
            Assert.AreEqual(AzureClientAction.GetConnectionStatus, azureClient.LastAction);

            // unrecognized input
            connectMagic.Test($"invalid", ExecuteStatus.Error);

            // valid input with resource ID (and to verify case-insensitivity of resourceId parsing)
            connectMagic.Test($"resourceId=/subscriptions/{subscriptionId}/RESOurceGroups/{resourceGroupName}/providers/Microsoft.Quantum/Workspaces/{workspaceName}");
            Assert.AreEqual(AzureClientAction.Connect, azureClient.LastAction);
            Assert.AreEqual(subscriptionId, azureClient.SubscriptionId);
            Assert.AreEqual(resourceGroupName, azureClient.ResourceGroupName);
            Assert.AreEqual(workspaceName, azureClient.WorkspaceName);
            Assert.AreEqual(string.Empty, azureClient.ConnectionString);
            Assert.AreEqual(string.Empty, azureClient.Location);

            // valid input with implied resource ID key, without surrounding quotes
            connectMagic.Test($"/subscriptions/{subscriptionId}/RESOurceGroups/{resourceGroupName}/providers/Microsoft.Quantum/Workspaces/{workspaceName}");
            Assert.AreEqual(AzureClientAction.Connect, azureClient.LastAction);
            Assert.AreEqual(subscriptionId, azureClient.SubscriptionId);
            Assert.AreEqual(resourceGroupName, azureClient.ResourceGroupName);
            Assert.AreEqual(workspaceName, azureClient.WorkspaceName);
            Assert.AreEqual(string.Empty, azureClient.ConnectionString);
            Assert.AreEqual(string.Empty, azureClient.Location);
            Assert.AreEqual(CredentialType.Default, azureClient.CredentialType);

            // valid input with implied resource ID key, with surrounding quotes
            connectMagic.Test($"\"/subscriptions/{subscriptionId}/RESOurceGroups/{resourceGroupName}/providers/Microsoft.Quantum/Workspaces/{workspaceName}\"");
            Assert.AreEqual(AzureClientAction.Connect, azureClient.LastAction);
            Assert.AreEqual(subscriptionId, azureClient.SubscriptionId);
            Assert.AreEqual(resourceGroupName, azureClient.ResourceGroupName);
            Assert.AreEqual(workspaceName, azureClient.WorkspaceName);
            Assert.AreEqual(string.Empty, azureClient.ConnectionString);
            Assert.AreEqual(string.Empty, azureClient.Location);
            Assert.AreEqual(CredentialType.Default, azureClient.CredentialType);

            // valid input with resource ID and storage account connection string
            connectMagic.Test(
                @$ "resourceId=/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Quantum/Workspaces/{workspaceName}
                   storage={storageAccountConnectionString}
                   credential=cli");
            Assert.AreEqual(AzureClientAction.Connect, azureClient.LastAction);
            Assert.AreEqual(subscriptionId, azureClient.SubscriptionId);
            Assert.AreEqual(resourceGroupName, azureClient.ResourceGroupName);
            Assert.AreEqual(workspaceName, azureClient.WorkspaceName);
            Assert.AreEqual(storageAccountConnectionString, azureClient.ConnectionString);
            Assert.AreEqual(string.Empty, azureClient.Location);
            Assert.AreEqual(CredentialType.CLI, azureClient.CredentialType);

            // valid input with individual parameters
            connectMagic.Test(
                @$ "subscription={subscriptionId}
                   resourceGroup={resourceGroupName}
                   workspace={workspaceName}
                   storage={storageAccountConnectionString}
                   location={location}
                   credential=interactive");
            Assert.AreEqual(AzureClientAction.Connect, azureClient.LastAction);
            Assert.AreEqual(subscriptionId, azureClient.SubscriptionId);
            Assert.AreEqual(resourceGroupName, azureClient.ResourceGroupName);
            Assert.AreEqual(workspaceName, azureClient.WorkspaceName);
            Assert.AreEqual(location, azureClient.Location);
            Assert.AreEqual(storageAccountConnectionString, azureClient.ConnectionString);
            Assert.AreEqual(CredentialType.Interactive, azureClient.CredentialType);


            // valid input with extra whitespace and quotes
            connectMagic.Test(
                @$ "location ={location}
                   subscription   =   {subscriptionId}
                   resourceGroup=  " "{resourceGroupName}" "
                   workspace  ={workspaceName}
                   credential=ENVIRONMENT
                   storage = '{storageAccountConnectionString}'");
            Assert.AreEqual(AzureClientAction.Connect, azureClient.LastAction);
            Assert.AreEqual(subscriptionId, azureClient.SubscriptionId);
            Assert.AreEqual(resourceGroupName, azureClient.ResourceGroupName);
            Assert.AreEqual(workspaceName, azureClient.WorkspaceName);
            Assert.AreEqual(storageAccountConnectionString, azureClient.ConnectionString);
            Assert.AreEqual(CredentialType.Environment, azureClient.CredentialType);
            Assert.AreEqual(location, azureClient.Location);

            // refresh parameter, which has been deprecated so has no effect:
            connectMagic.Test(
                @$ "refresh subscription={subscriptionId}
                   resourceGroup={resourceGroupName}
                   workspace={workspaceName}
                   storage={storageAccountConnectionString}");
            Assert.AreEqual(AzureClientAction.Connect, azureClient.LastAction);
            Assert.AreEqual(subscriptionId, azureClient.SubscriptionId);
            Assert.AreEqual(resourceGroupName, azureClient.ResourceGroupName);
            Assert.AreEqual(workspaceName, azureClient.WorkspaceName);
            Assert.AreEqual(storageAccountConnectionString, azureClient.ConnectionString);
            Assert.AreEqual(string.Empty, azureClient.Location);
            Assert.AreEqual(CredentialType.Default, azureClient.CredentialType);

            connectMagic.Test($"refresh /subscriptions/{subscriptionId}/RESOurceGroups/{resourceGroupName}/providers/Microsoft.Quantum/Workspaces/{workspaceName}");
            connectMagic.Test($"/subscriptions/{subscriptionId}/RESOurceGroups/{resourceGroupName}/providers/Microsoft.Quantum/Workspaces/{workspaceName} refresh");
            connectMagic.Test($"/subscriptions/{subscriptionId}/RESOurceGroups/{resourceGroupName}/providers/Microsoft.Quantum/Workspaces/{workspaceName}");
        }
Exemplo n.º 11
0
        public void TestConnectMagic()
        {
            var azureClient  = new MockAzureClient();
            var connectMagic = new ConnectMagic(azureClient);

            // no input
            connectMagic.Test(string.Empty);
            Assert.AreEqual(AzureClientAction.GetConnectionStatus, azureClient.LastAction);

            // unrecognized input
            connectMagic.Test($"invalid", ExecuteStatus.Error);

            // valid input with resource ID (and to verify case-insensitivity of resourceId parsing)
            connectMagic.Test($"resourceId=/subscriptions/{subscriptionId}/RESOurceGroups/{resourceGroupName}/providers/Microsoft.Quantum/Workspaces/{workspaceName}");
            Assert.AreEqual(AzureClientAction.Connect, azureClient.LastAction);
            Assert.IsFalse(azureClient.RefreshCredentials);
            Assert.AreEqual(subscriptionId, azureClient.SubscriptionId);
            Assert.AreEqual(resourceGroupName, azureClient.ResourceGroupName);
            Assert.AreEqual(workspaceName, azureClient.WorkspaceName);
            Assert.AreEqual(string.Empty, azureClient.ConnectionString);

            // valid input with implied resource ID key, without surrounding quotes
            connectMagic.Test($"/subscriptions/{subscriptionId}/RESOurceGroups/{resourceGroupName}/providers/Microsoft.Quantum/Workspaces/{workspaceName}");
            Assert.AreEqual(AzureClientAction.Connect, azureClient.LastAction);
            Assert.IsFalse(azureClient.RefreshCredentials);
            Assert.AreEqual(subscriptionId, azureClient.SubscriptionId);
            Assert.AreEqual(resourceGroupName, azureClient.ResourceGroupName);
            Assert.AreEqual(workspaceName, azureClient.WorkspaceName);
            Assert.AreEqual(string.Empty, azureClient.ConnectionString);

            // valid input with implied resource ID key, with surrounding quotes
            connectMagic.Test($"\"/subscriptions/{subscriptionId}/RESOurceGroups/{resourceGroupName}/providers/Microsoft.Quantum/Workspaces/{workspaceName}\"");
            Assert.AreEqual(AzureClientAction.Connect, azureClient.LastAction);
            Assert.IsFalse(azureClient.RefreshCredentials);
            Assert.AreEqual(subscriptionId, azureClient.SubscriptionId);
            Assert.AreEqual(resourceGroupName, azureClient.ResourceGroupName);
            Assert.AreEqual(workspaceName, azureClient.WorkspaceName);
            Assert.AreEqual(string.Empty, azureClient.ConnectionString);

            // valid input with resource ID and storage account connection string
            connectMagic.Test(
                @$ "resourceId=/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Quantum/Workspaces/{workspaceName}
                   storage={storageAccountConnectionString}");
            Assert.AreEqual(AzureClientAction.Connect, azureClient.LastAction);
            Assert.IsFalse(azureClient.RefreshCredentials);
            Assert.AreEqual(subscriptionId, azureClient.SubscriptionId);
            Assert.AreEqual(resourceGroupName, azureClient.ResourceGroupName);
            Assert.AreEqual(workspaceName, azureClient.WorkspaceName);
            Assert.AreEqual(storageAccountConnectionString, azureClient.ConnectionString);

            // valid input with individual parameters
            connectMagic.Test(
                @$ "subscription={subscriptionId}
                   resourceGroup={resourceGroupName}
                   workspace={workspaceName}
                   storage={storageAccountConnectionString}");
            Assert.AreEqual(AzureClientAction.Connect, azureClient.LastAction);
            Assert.IsFalse(azureClient.RefreshCredentials);
            Assert.AreEqual(subscriptionId, azureClient.SubscriptionId);
            Assert.AreEqual(resourceGroupName, azureClient.ResourceGroupName);
            Assert.AreEqual(workspaceName, azureClient.WorkspaceName);
            Assert.AreEqual(storageAccountConnectionString, azureClient.ConnectionString);

            // valid input with extra whitespace and quotes
            connectMagic.Test(
                @$ "subscription   =   {subscriptionId}
                   resourceGroup=  " "{resourceGroupName}" "
                   workspace  ={workspaceName}
                   storage = '{storageAccountConnectionString}'");
            Assert.AreEqual(AzureClientAction.Connect, azureClient.LastAction);
            Assert.IsFalse(azureClient.RefreshCredentials);
            Assert.AreEqual(subscriptionId, azureClient.SubscriptionId);
            Assert.AreEqual(resourceGroupName, azureClient.ResourceGroupName);
            Assert.AreEqual(workspaceName, azureClient.WorkspaceName);
            Assert.AreEqual(storageAccountConnectionString, azureClient.ConnectionString);

            // valid input with forced login
            connectMagic.Test(
                @$ "refresh subscription={subscriptionId}
                   resourceGroup={resourceGroupName}
                   workspace={workspaceName}
                   storage={storageAccountConnectionString}");
            Assert.IsTrue(azureClient.RefreshCredentials);

            // forced login with implied resource ID before, after, and missing
            connectMagic.Test($"refresh /subscriptions/{subscriptionId}/RESOurceGroups/{resourceGroupName}/providers/Microsoft.Quantum/Workspaces/{workspaceName}");
            Assert.IsTrue(azureClient.RefreshCredentials);
            connectMagic.Test($"/subscriptions/{subscriptionId}/RESOurceGroups/{resourceGroupName}/providers/Microsoft.Quantum/Workspaces/{workspaceName} refresh");
            Assert.IsTrue(azureClient.RefreshCredentials);
            connectMagic.Test($"/subscriptions/{subscriptionId}/RESOurceGroups/{resourceGroupName}/providers/Microsoft.Quantum/Workspaces/{workspaceName}");
            Assert.IsFalse(azureClient.RefreshCredentials);
        }