public void BeforeEach()
        {
            _deployment = new GkeDeployment {
                Metadata = new GkeMetadata {
                    Name = DeploymentName
                }
            };

            _getClusterListTaskSource = new TaskCompletionSource <IList <Cluster> >();
            _getDeploymentsSource     = new TaskCompletionSource <IList <GkeDeployment> >();
            _validateGcloudSource     = new TaskCompletionSource <GCloudValidationResult>();

            _propertyServiceMock = new Mock <IVsProjectPropertyService>();
            _browserServiceMock  = new Mock <IBrowserService>();
            _kubectlContextMock  = new Mock <IKubectlContext>();
            _kubectlContextMock.Setup(kube => kube.GetDeploymentsAsync()).Returns(_getDeploymentsSource.Task);
            _gkeDeploymentServiceMock = new Mock <IGkeDeploymentService>();
            _gkeDeploymentServiceMock
            .Setup(
                s => s.DeployProjectToGkeAsync(
                    It.IsAny <IParsedProject>(),
                    It.IsAny <GkeDeploymentService.Options>()))
            .Returns(Task.CompletedTask);


            PackageMock.Setup(p => p.GetMefService <IGCloudWrapper>().ValidateGCloudAsync(It.IsAny <GCloudComponent>()))
            .Returns(() => _validateGcloudSource.Task);
            PackageMock.Setup(p => p.GetMefService <IVsProjectPropertyService>()).Returns(_propertyServiceMock.Object);
            PackageMock.Setup(p => p.GetMefService <IBrowserService>()).Returns(_browserServiceMock.Object);
            PackageMock.Setup(p => p.GetMefService <IApiManager>()).Returns(Mock.Of <IApiManager>(
                                                                                x => x.AreServicesEnabledAsync(It.IsAny <IList <string> >()) == Task.FromResult(true) &&
                                                                                x.EnableServicesAsync(It.IsAny <IEnumerable <string> >()) == Task.FromResult(true)));
            PackageMock.Setup(p => p.GetMefServiceLazy <IDataSourceFactory>())
            .Returns(
                MockHelpers.LazyOf <IDataSourceFactory>(
                    dsf => dsf.CreateGkeDataSource().GetClusterListAsync() == _getClusterListTaskSource.Task));
            PackageMock.Setup(
                p => p.GetMefService <IKubectlContextProvider>()
                .GetKubectlContextForClusterAsync(It.IsAny <Cluster>()))
            .Returns(_kubectlContextMock.ToTask);
            PackageMock.Setup(p => p.GetMefServiceLazy <IGkeDeploymentService>())
            .Returns(_gkeDeploymentServiceMock.ToLazy);

            _parsedProject = new FakeParsedProject {
                Name = VisualStudioProjectName
            };
            _parsedProject.ProjectMock.Setup(p => p.ConfigurationManager.ConfigurationRowNames).Returns(new string[0]);

            _objectUnderTest = new GkeStepViewModel(Mock.Of <IPublishDialog>(pd => pd.Project == _parsedProject));

            _changedProperties = new List <string>();
            _objectUnderTest.PropertyChanged += (sender, args) => _changedProperties.Add(args.PropertyName);
            _objectUnderTest.PublishCommandAsync.CanExecuteChanged += (sender, args) => _canPublishChangedCount++;
        }
        public async Task TestPublishCommand_CallsGkeDeploymentServiceWithSelectedDeployment()
        {
            // Initalize KubectlContext task.
            _objectUnderTest.SelectedCluster = s_aCluster;
            var expectedDeployment = new GkeDeployment();

            _objectUnderTest.SelectedDeployment = expectedDeployment;
            _objectUnderTest.PublishCommandAsync.Execute(null);
            await _objectUnderTest.PublishCommandAsync.LatestExecution.SafeTask;

            _gkeDeploymentServiceMock
            .Verify(
                s => s.DeployProjectToGkeAsync(
                    It.IsAny <IParsedProject>(),
                    It.Is <GkeDeploymentService.Options>(o => o.ExistingDeployment == expectedDeployment)));
        }
Beispiel #3
0
        /// <summary>
        /// Start the publish operation.
        /// </summary>
        public override async void Publish()
        {
            if (!ValidateInput())
            {
                Debug.WriteLine("Invalid input cancelled the operation.");
                return;
            }

            var project = _publishDialog.Project;

            try
            {
                var verifyGCloudTask = VerifyGCloudDependencies();
                _publishDialog.TrackTask(verifyGCloudTask);
                if (!await verifyGCloudTask)
                {
                    Debug.WriteLine("Aborting deployment, no kubectl was found.");
                    return;
                }

                var gcloudContext = new GCloudContext
                {
                    CredentialsPath = CredentialsStore.Default.CurrentAccountPath,
                    ProjectId       = CredentialsStore.Default.CurrentProjectId,
                    AppName         = GoogleCloudExtensionPackage.ApplicationName,
                    AppVersion      = GoogleCloudExtensionPackage.ApplicationVersion,
                };

                var kubectlContextTask = GCloudWrapper.GetKubectlContextForClusterAsync(
                    cluster: SelectedCluster.Name,
                    zone: SelectedCluster.Zone,
                    context: gcloudContext);
                _publishDialog.TrackTask(kubectlContextTask);

                using (var kubectlContext = await kubectlContextTask)
                {
                    var deploymentExistsTask = KubectlWrapper.DeploymentExistsAsync(DeploymentName, kubectlContext);
                    _publishDialog.TrackTask(deploymentExistsTask);
                    if (await deploymentExistsTask)
                    {
                        if (!UserPromptUtils.ActionPrompt(
                                String.Format(Resources.GkePublishDeploymentAlreadyExistsMessage, DeploymentName),
                                Resources.GkePublishDeploymentAlreadyExistsTitle,
                                actionCaption: Resources.UiUpdateButtonCaption))
                        {
                            return;
                        }
                    }

                    var options = new GkeDeployment.DeploymentOptions
                    {
                        Cluster                     = SelectedCluster.Name,
                        Zone                        = SelectedCluster.Zone,
                        DeploymentName              = DeploymentName,
                        DeploymentVersion           = DeploymentVersion,
                        ExposeService               = ExposeService,
                        GCloudContext               = gcloudContext,
                        KubectlContext              = kubectlContext,
                        Replicas                    = int.Parse(Replicas),
                        WaitingForServiceIpCallback = () => GcpOutputWindow.OutputLine(Resources.GkePublishWaitingForServiceIpMessage)
                    };

                    GcpOutputWindow.Activate();
                    GcpOutputWindow.Clear();
                    GcpOutputWindow.OutputLine(String.Format(Resources.GkePublishDeployingToGkeMessage, project.Name));

                    _publishDialog.FinishFlow();

                    GkeDeploymentResult result;
                    using (var frozen = StatusbarHelper.Freeze())
                        using (var animationShown = StatusbarHelper.ShowDeployAnimation())
                            using (var progress = StatusbarHelper.ShowProgressBar(Resources.GkePublishDeploymentStatusMessage))
                                using (var deployingOperation = ShellUtils.SetShellUIBusy())
                                {
                                    result = await GkeDeployment.PublishProjectAsync(
                                        project.FullPath,
                                        options,
                                        progress,
                                        GcpOutputWindow.OutputLine);
                                }

                    if (result != null)
                    {
                        GcpOutputWindow.OutputLine(String.Format(Resources.GkePublishDeploymentSuccessMessage, project.Name));
                        if (result.DeploymentUpdated)
                        {
                            GcpOutputWindow.OutputLine(String.Format(Resources.GkePublishDeploymentUpdatedMessage, options.DeploymentName));
                        }
                        if (result.DeploymentScaled)
                        {
                            GcpOutputWindow.OutputLine(String.Format(Resources.GkePublishDeploymentScaledMessage, options.DeploymentName, options.Replicas));
                        }

                        if (result.WasExposed)
                        {
                            if (result.ServiceIpAddress != null)
                            {
                                GcpOutputWindow.OutputLine(
                                    String.Format(Resources.GkePublishServiceIpMessage, DeploymentName, result.ServiceIpAddress));
                            }
                            else
                            {
                                GcpOutputWindow.OutputLine(Resources.GkePublishServiceIpTimeoutMessage);
                            }
                        }
                        StatusbarHelper.SetText(Resources.PublishSuccessStatusMessage);

                        if (OpenWebsite && result.WasExposed && result.ServiceIpAddress != null)
                        {
                            Process.Start($"http://{result.ServiceIpAddress}");
                        }
                    }
                    else
                    {
                        GcpOutputWindow.OutputLine(String.Format(Resources.GkePublishDeploymentFailureMessage, project.Name));
                        StatusbarHelper.SetText(Resources.PublishFailureStatusMessage);
                    }
                }
            }
            catch (Exception ex) when(!ErrorHandlerUtils.IsCriticalException(ex))
            {
                GcpOutputWindow.OutputLine(String.Format(Resources.GkePublishDeploymentFailureMessage, project.Name));
                StatusbarHelper.SetText(Resources.PublishFailureStatusMessage);
                _publishDialog.FinishFlow();
            }
        }
 public GkeDeploymentOptionsBuilder SetExistingDeployment(GkeDeployment existingDeployment)
 {
     _existingDeployment = existingDeployment;
     return(this);
 }