public void Test_CreateDeployemntTask_RunsProperly_WhenAllIsWell()
        {
            var objectFactory = new Mock<IObjectFactory>(MockBehavior.Strict);
              var envInfoRepository = new Mock<IEnvironmentInfoRepository>(MockBehavior.Loose);
              var artifactsRepository = new Mock<IArtifactsRepository>(MockBehavior.Loose);
              var taskScheduler = new Mock<ITaskScheduler>(MockBehavior.Loose);
              var ntServiceManager = new Mock<INtServiceManager>(MockBehavior.Loose);
              var passwordCollector = new Mock<IPasswordCollector>(MockBehavior.Loose);
              var failoverClusterManager = new Mock<IFailoverClusterManager>(MockBehavior.Loose);

              var projectInfo =
            new NtServiceProjectInfo(
              _ProjectName,
              _ArtifactsRepositoryName,
              _ArtifactsRepositoryDirName,
              _ArtifactsAreNotEnvironmentSpecific,
              _NtServiceName,
              _NtServiceDirName,
              _NtServiceDisplayName,
              _NtServiceExeName,
              _NtServiceUserId);

              objectFactory.Setup(o => o.CreateEnvironmentInfoRepository()).Returns(envInfoRepository.Object);
              objectFactory.Setup(o => o.CreateArtifactsRepository()).Returns(artifactsRepository.Object);
              objectFactory.Setup(o => o.CreateTaskScheduler()).Returns(taskScheduler.Object);
              objectFactory.Setup(o => o.CreateNtServiceManager()).Returns(ntServiceManager.Object);
              objectFactory.Setup(o => o.CreatePasswordCollector()).Returns(passwordCollector.Object);
              objectFactory.Setup(o => o.CreateFailoverClusterManager()).Returns(failoverClusterManager.Object);

              projectInfo.CreateDeploymentTask(
            objectFactory.Object, "configName", "buildID", "targetEnvironmentName");
        }
        public DeployNtServiceDeploymentTask(
      IEnvironmentInfoRepository environmentInfoRepository,
      IArtifactsRepository artifactsRepository,
      INtServiceManager ntServiceManager,
      IPasswordCollector passwordCollector,
      IFailoverClusterManager failoverClusterManager,
      NtServiceProjectInfo projectInfo,
      string projectConfigurationName,
      string projectConfigurationBuildId,
      string targetEnvironmentName)
            : base(environmentInfoRepository, targetEnvironmentName)
        {
            if (artifactsRepository == null)
              {
            throw new ArgumentNullException("artifactsRepository");
              }

              if (ntServiceManager == null)
              {
            throw new ArgumentNullException("ntServiceManager");
              }

              if (projectInfo == null)
              {
            throw new ArgumentNullException("projectInfo");
              }

              if (string.IsNullOrEmpty(projectConfigurationName))
              {
            throw new ArgumentException("Argument can't be null nor empty.", "projectConfigurationName");
              }

              if (string.IsNullOrEmpty(projectConfigurationBuildId))
              {
            throw new ArgumentException("Argument can't be null nor empty.", "projectConfigurationBuildId");
              }

              if (passwordCollector == null)
              {
            throw new ArgumentNullException("passwordCollector");
              }

              if (failoverClusterManager == null)
              {
            throw new ArgumentNullException("failoverClusterManager");
              }

              _artifactsRepository = artifactsRepository;
              _projectInfo = projectInfo;
              _projectConfigurationName = projectConfigurationName;
              _projectConfigurationBuildId = projectConfigurationBuildId;
              _ntServiceManager = ntServiceManager;
              _passwordCollector = passwordCollector;
              _failoverClusterManager = failoverClusterManager;
        }
        public void Test_GetTargetFolders_RunsProperly_WhenAllIsWell()
        {
            string machine = Environment.MachineName;
              const string baseDirPath = "c:\\basedir";

              var envInfo =
            new EnvironmentInfo(
              "name",
              true,
              "templates",
              machine,
              "failover",
              new[] { "webmachine" },
              "terminalmachine",
              new[] { "schedulerServerTasksMachineName1", "schedulerServerTasksMachineName2", },
              new[] { "schedulerServerBinariesMachineName1", "schedulerServerBinariesMachineName2", },
              baseDirPath,
              "webbasedir",
              "c:\\scheduler",
              "terminal",
              false,
              TestData.EnvironmentUsers,
              TestData.AppPoolInfos,
              TestData.DatabaseServers,
              TestData.ProjectToFailoverClusterGroupMappings,
              TestData.WebAppProjectConfigurationOverrides,
              TestData.DbProjectConfigurationOverrides,
              "terminalAppsShortcutFolder",
              "artifactsDeploymentDirPath",
              "domain-name",
              TestData.CustomEnvMachines);

              var projectInfo =
            new NtServiceProjectInfo(
              _ProjectName,
              _ArtifactsRepositoryName,
              _AllowedEnvironmentNames,
              _ArtifactsRepositoryDirName,
              _ArtifactsAreNotEnvironmentSpecific,
              _NtServiceName,
              _NtServiceDirName,
              _NtServiceDisplayName,
              _NtServiceExeName,
              _NtServiceUserId,
              "");

              var targetFolders = projectInfo.GetTargetFolders(_objectFactoryFake.Object, envInfo).ToList();

              Assert.IsNotNull(targetFolders);
              Assert.AreEqual(1, targetFolders.Count);
              Assert.AreEqual("\\\\" + machine + "\\c$\\basedir\\" + _NtServiceDirName, targetFolders[0]);
        }
        public void Test_InstallNtServiceDeploymentStepServiceDirPathDoesntExist()
        {
            const string serviceName = "serviceName";
              const string serviceDir = "serviceDir";
              const string artifactsRepo = "artifactsRepo";
              const string projectName = "projectName";
              const string buildID = "id";
              const string envName = "envName";
              const string baseDirPath = "c:\\basedir";

              string machine = Environment.MachineName;

              var ntServiceProjectInfo =
            new NtServiceProjectInfo(
              "name",
              artifactsRepo,
              "artifactsRepoDir",
              false,
              serviceName,
              serviceDir,
              "serviceDisplayed",
              "exeName",
              "Sample.User");

              var envInfo =
            new EnvironmentInfo(
              "name",
              "templates",
              machine,
              "failover",
              new[] { "webmachine" },
              "terminalmachine",
              "databasemachine",
              baseDirPath,
              "webbasedir",
              "scheduler",
              "terminal",
              false,
              _EnvironmentUsers,
              _ProjectToFailoverClusterGroupMappings);

              _environmentInfoRepository
            .Setup(e => e.GetByName(envName))
            .Returns(envInfo);

              _passwordCollector
            .Setup(pc => pc.CollectPasswordForUser(envInfo.Name, envInfo.AppServerMachineName, envInfo.GetEnvironmentUserByName(ntServiceProjectInfo.NtServiceUserId).UserName))
            .Returns("some password");

              _ntServiceManager
            .Setup(n => n.DoesServiceExist(machine, serviceName))
            .Returns(false);

              _artifactsRepository
            .Setup(a => a.GetArtifacts(artifactsRepo,projectName, buildID, It.IsAny<string>()));

              var deployNTService =
            new DeployNtServiceDeploymentTask(
              _environmentInfoRepository.Object,
              _artifactsRepository.Object,
              _ntServiceManager.Object,
              _passwordCollector.Object,
              _failoverClusterManager.Object,
              ntServiceProjectInfo,
              projectName,
              buildID,
              envName);

              Assert.Throws<DeploymentTaskException>(deployNTService.PrepareAndExecute);
        }
        protected override void DoPrepare()
        {
            EnvironmentInfo environmentInfo = GetEnvironmentInfo();

              _projectInfo = GetProjectInfo<NtServiceProjectInfo>();

              // create a step for downloading the artifacts
              var downloadArtifactsDeploymentStep =
            new DownloadArtifactsDeploymentStep(
              _projectInfo,
              DeploymentInfo,
              GetTempDirPath(),
              _artifactsRepository);

              AddSubTask(downloadArtifactsDeploymentStep);

              // create a step for extracting the artifacts
              var extractArtifactsDeploymentStep =
            new ExtractArtifactsDeploymentStep(
              _projectInfo,
              environmentInfo,
              DeploymentInfo,
              downloadArtifactsDeploymentStep.ArtifactsFilePath,
              GetTempDirPath(),
              _fileAdapter,
              _directoryAdapter,
              _zipFileAdapter);

              AddSubTask(extractArtifactsDeploymentStep);

              if (_projectInfo.ArtifactsAreEnvironmentSpecific)
              {
            var binariesConfiguratorStep =
              new ConfigureBinariesStep(
            environmentInfo.ConfigurationTemplateName,
            GetTempDirPath());

            AddSubTask(binariesConfiguratorStep);
              }

              bool deployToClusteredEnvironment =
            environmentInfo.EnableFailoverClusteringForNtServices;

              if (deployToClusteredEnvironment)
              {
            PostDiagnosticMessage("Will deploy to a clustered environment.", DiagnosticMessageType.Trace);

            if (string.IsNullOrEmpty(environmentInfo.GetFailoverClusterGroupNameForProject(DeploymentInfo.ProjectName)))
            {
              throw new InvalidOperationException(string.Format("Failover clustering for NT services is enabled for environment '{0}' but there is no cluster group mapping for project '{1}'.", environmentInfo.Name, DeploymentInfo.ProjectName));
            }

            DoPrepareDeploymentToClusteredEnvironment(
              environmentInfo,
              new Lazy<string>(() => extractArtifactsDeploymentStep.BinariesDirPath));
              }
              else
              {
            PostDiagnosticMessage("Will deploy to a non-clustered environment.", DiagnosticMessageType.Trace);

            DoPrepareDeploymentToStandardEnvironment(
              environmentInfo,
              new Lazy<string>(() => extractArtifactsDeploymentStep.BinariesDirPath));
              }
        }
        public void Test_CreateDeploymentTask_Thows_When_ObjectFactory_null()
        {
            var projectInfo =
            new NtServiceProjectInfo(
              _ProjectName,
              _ArtifactsRepositoryName,
              _ArtifactsRepositoryDirName,
              _ArtifactsAreNotEnvironmentSpecific,
              _NtServiceName,
              _NtServiceDirName,
              _NtServiceDisplayName,
              _NtServiceExeName,
              _NtServiceUserId);

              Assert.Throws<ArgumentNullException>(
            () => projectInfo.CreateDeploymentTask(null, "configName", "buildID", "targetEnvironmentName"));
        }
        public void Test_GetTargetFolders_Throws_EnvInfo_null()
        {
            var projectInfo =
            new NtServiceProjectInfo(
              _ProjectName,
              _ArtifactsRepositoryName,
              _ArtifactsRepositoryDirName,
              _ArtifactsAreNotEnvironmentSpecific,
              _NtServiceName,
              _NtServiceDirName,
              _NtServiceDisplayName,
              _NtServiceExeName,
              _NtServiceUserId);

              Assert.Throws<ArgumentNullException>(() => projectInfo.GetTargetFolders(null));
        }
        public void Test_GetTargetFolders_RunsProperly_WhenAllIsWell()
        {
            string machine = Environment.MachineName;
              const string baseDirPath = "c:\\basedir";

              var envInfo =
            new EnvironmentInfo(
              "name",
              "templates",
              machine,
              "failover",
              new[] { "webmachine" },
              "terminalmachine",
              "databasemachine",
              baseDirPath,
              "webbasedir",
              "c:\\scheduler",
              "terminal",
              false,
              _EnvironmentUsers,
              _ProjectToFailoverClusterGroupMappings);

              var projectInfo =
            new NtServiceProjectInfo(
              _ProjectName,
              _ArtifactsRepositoryName,
              _ArtifactsRepositoryDirName,
              _ArtifactsAreNotEnvironmentSpecific,
              _NtServiceName,
              _NtServiceDirName,
              _NtServiceDisplayName,
              _NtServiceExeName,
              _NtServiceUserId);

              var targetFolders = projectInfo.GetTargetFolders(envInfo).ToList();

              Assert.IsNotNull(targetFolders);
              Assert.AreEqual(1, targetFolders.Count);
              Assert.AreEqual("\\\\" + machine + "\\c$\\basedir\\" + _NtServiceDirName, targetFolders[0]);
        }
        protected override void DoPrepare()
        {
            _environmentInfo = GetEnvironmentInfo();
              _projectInfo = GetProjectInfo<ExtensionProjectInfo>();
              _extendedProjectInfo = GetProjectInfo<NtServiceProjectInfo>(_projectInfo.ExtendedProjectName);

              // create a step for downloading the artifacts
              var downloadArtifactsDeploymentStep =
            new DownloadArtifactsDeploymentStep(
              _projectInfo,
              DeploymentInfo,
              GetTempDirPath(),
              _artifactsRepository);

              AddSubTask(downloadArtifactsDeploymentStep);

              // create a step for extracting the artifacts
              var extractArtifactsDeploymentStep =
            new ExtractArtifactsDeploymentStep(
              _projectInfo,
              _environmentInfo,
              DeploymentInfo,
              downloadArtifactsDeploymentStep.ArtifactsFilePath,
              GetTempDirPath(),
              _fileAdapter,
              _directoryAdapter,
              _zipFileAdapter);

              AddSubTask(extractArtifactsDeploymentStep);

              bool deployToClusteredEnvironment =
            _environmentInfo.EnableFailoverClusteringForNtServices;

              if (deployToClusteredEnvironment)
              {
            PostDiagnosticMessage("Will deploy to a clustered environment.", DiagnosticMessageType.Trace);

            if (string.IsNullOrEmpty(_environmentInfo.GetFailoverClusterGroupNameForProject(_extendedProjectInfo.Name)))
            {
              throw new InvalidOperationException(string.Format("Failover clustering for NT services is enabled for environment '{0}' but there is no cluster group mapping for project '{1}'.", _environmentInfo.Name, _extendedProjectInfo.Name));
            }

            DoPrepareDeploymentToClusteredEnvironment(
              new Lazy<string>(() => extractArtifactsDeploymentStep.BinariesDirPath),
              _extendedProjectInfo);
              }
              else
              {
            PostDiagnosticMessage("Will deploy to a non-clustered environment.", DiagnosticMessageType.Trace);

            AddSubTask(
              new StopNtServiceDeploymentStep(
            _ntServiceManager,
            _environmentInfo.AppServerMachineName,
            _extendedProjectInfo.NtServiceName));

            DoPrepareCommonDeploymentSteps(_environmentInfo.GetAppServerNetworkPath, new Lazy<string>(() => extractArtifactsDeploymentStep.BinariesDirPath) );

            AddSubTask(
              new StartNtServiceDeploymentStep(
            _ntServiceManager,
            _environmentInfo.AppServerMachineName,
            _extendedProjectInfo.NtServiceName));
              }
        }
        private void DoPrepareDeploymentToClusteredEnvironment(Lazy<string> artifactsBinariesDirPathProvider, NtServiceProjectInfo extendedServiceProjectInfo)
        {
            string clusterGroupName = _environmentInfo.GetFailoverClusterGroupNameForProject(extendedServiceProjectInfo.Name);

              if (string.IsNullOrEmpty(clusterGroupName))
              {
            throw new InvalidOperationException(string.Format("There is no cluster group defined for project '{0}' in environment '{1}'.", extendedServiceProjectInfo.Name, _environmentInfo.Name));
              }

              string failoverClusterMachineName = _environmentInfo.FailoverClusterMachineName;

              if (string.IsNullOrEmpty(failoverClusterMachineName))
              {
            throw new InvalidOperationException(string.Format("Environment '{0}' has no failover cluster machine name defined.", _environmentInfo.Name));
              }

              string currentNodeName =
            _failoverClusterManager.GetCurrentNodeName(failoverClusterMachineName, clusterGroupName);

              if (string.IsNullOrEmpty(currentNodeName))
              {
            throw new InvalidOperationException(string.Format("Cluster group '{0}' has no current node in a cluster '{1}' in environment '{2}'.", clusterGroupName, _environmentInfo.FailoverClusterMachineName, _environmentInfo.Name));
              }

              PostDiagnosticMessage(string.Format("Current node: '{0}'.", currentNodeName), DiagnosticMessageType.Trace);

              List<string> possibleNodeNames =
            _failoverClusterManager.GetPossibleNodeNames(failoverClusterMachineName, clusterGroupName)
              .ToList();

              PostDiagnosticMessage(string.Format("Possible nodes: {0}.", string.Join(", ", possibleNodeNames.Select(n => string.Format("'{0}'", n)))), DiagnosticMessageType.Trace);

              if (possibleNodeNames.Count < 2)
              {
            throw new InvalidOperationException(string.Format("There is only one possible node for cluster group '{0}' in a cluster '{1}' in environment '{2}'.", clusterGroupName, _environmentInfo.FailoverClusterMachineName, _environmentInfo.Name));
              }

              foreach (string possibleNodeName in possibleNodeNames)
              {
            string machineName = possibleNodeName;

            if (string.Equals(machineName, currentNodeName, StringComparison.OrdinalIgnoreCase))
            {
              continue;
            }

            DoPrepareCommonDeploymentSteps(
              absoluteLocalPath => EnvironmentInfo.GetNetworkPath(machineName, absoluteLocalPath),
              artifactsBinariesDirPathProvider);
              }

              // move cluster group to another node
              string targetNodeName =
            possibleNodeNames.FirstOrDefault(nodeName => nodeName != currentNodeName);

              PostDiagnosticMessage(string.Format("Target node: '{0}'.", targetNodeName), DiagnosticMessageType.Trace);

              if (string.IsNullOrEmpty(targetNodeName))
              {
            throw new InvalidOperationException(string.Format("There is no node in cluster '{0}' that we can move cluster group '{1}' to.", failoverClusterMachineName, clusterGroupName));
              }

              AddSubTask(
              new MoveClusterGroupToAnotherNodeDeploymentStep(
            _failoverClusterManager,
            failoverClusterMachineName,
            clusterGroupName,
            targetNodeName));

              // update nt service on the machine that was the previous owner node
              string previousMachineName = currentNodeName;

              DoPrepareCommonDeploymentSteps(
            absoluteLocalPath => EnvironmentInfo.GetNetworkPath(previousMachineName, absoluteLocalPath),
            artifactsBinariesDirPathProvider);
        }