/// <summary>
        /// Gets the runtime startup task regardless of its order in the startup tasks.
        /// </summary>
        /// <param name="roleStartup">The role startup tasks</param>
        /// <returns>The runtime startup task</returns>
        public static Task GetRuntimeStartupTask(Startup roleStartup)
        {
            if (roleStartup.Task != null)
            {
                return roleStartup.Task.FirstOrDefault<Task>(t =>
                t.commandLine.Equals(Resources.WebRoleStartupTaskCommandLine)
             || t.commandLine.Equals(Resources.WorkerRoleStartupTaskCommandLine));
            }

            return null;
        }
        private static void CacheClientCommonConfiguration(
            CloudServiceProject cloudServiceProject,
            string roleName,
            bool isWebRole,
            string cacheWorkerRole,
            Startup startup,
            Endpoints endpoints,
            LocalResources localResources,
            ref DefinitionConfigurationSetting[] configurationSettings)
        {
            if (isWebRole)
            {
                // Generate cache scaffolding for web role
                cloudServiceProject.GenerateScaffolding(Path.Combine(Resources.CacheScaffolding, RoleType.WebRole.ToString()),
                    roleName, new Dictionary<string, object>());

                // Adjust web.config to enable auto discovery for the caching role.
                string webCloudConfigPath = Path.Combine(cloudServiceProject.Paths.RootPath, roleName, Resources.WebCloudConfig);
                string webConfigPath = Path.Combine(cloudServiceProject.Paths.RootPath, roleName, Resources.WebConfigTemplateFileName);

                UpdateWebConfig(roleName, cacheWorkerRole, webCloudConfigPath);
                UpdateWebConfig(roleName, cacheWorkerRole, webConfigPath);
            }
            else
            {
                // Generate cache scaffolding for worker role
                Dictionary<string, object> parameters = new Dictionary<string, object>();
                parameters[ScaffoldParams.RoleName] = cacheWorkerRole;

                cloudServiceProject.GenerateScaffolding(Path.Combine(Resources.CacheScaffolding, RoleType.WorkerRole.ToString()),
                    roleName, parameters);
            }

            // Add default memcache internal endpoint.
            InternalEndpoint memcacheEndpoint = new InternalEndpoint
            {
                name = Resources.MemcacheEndpointName,
                protocol = InternalProtocol.tcp,
                port = Resources.MemcacheEndpointPort
            };
            endpoints.InternalEndpoint = GeneralUtilities.ExtendArray<InternalEndpoint>(endpoints.InternalEndpoint, memcacheEndpoint);

            // Enable cache diagnostic
            LocalStore localStore = new LocalStore
            {
                name = Resources.CacheDiagnosticStoreName,
                cleanOnRoleRecycle = false
            };
            localResources.LocalStorage = GeneralUtilities.ExtendArray<LocalStore>(localResources.LocalStorage, localStore);

            DefinitionConfigurationSetting diagnosticLevel = new DefinitionConfigurationSetting { name = Resources.CacheClientDiagnosticLevelAssemblyName };
            configurationSettings = GeneralUtilities.ExtendArray<DefinitionConfigurationSetting>(configurationSettings, diagnosticLevel);

            // Add ClientDiagnosticLevel setting to service configuration.
            AddClientDiagnosticLevelToConfig(cloudServiceProject.Components.GetCloudConfigRole(roleName));
            AddClientDiagnosticLevelToConfig(cloudServiceProject.Components.GetLocalConfigRole(roleName));
        }
        /// <summary>
        /// Checks if memcache is already enabled or not for the given role startup.
        /// It does this by checking the role startup task.
        /// </summary>
        /// <param name="startup">The role startup</param>
        /// <returns>Either enabled or not</returns>
        private bool IsCacheEnabled(Startup startup)
        {
            if (startup.Task != null)
            {
                return Array.Exists<Variable>(CloudRuntime.GetRuntimeStartupTask(startup).Environment,
                v => v.name.Equals(Resources.RuntimeTypeKey) && v.value.Contains(Resources.CacheRuntimeValue));
            }

            return false;
        }
        /// <summary>
        /// Factory method to apply memcache required configuration based on the installed SDK version.
        /// </summary>
        /// <param name="cloudServiceProject">The azure service instance</param>
        /// <param name="webRole">The web role to enable caching on</param>
        /// <param name="isWebRole">Flag indicating if the provided role is web or not</param>
        /// <param name="cacheWorkerRole">The memcache worker role name</param>
        /// <param name="startup">The role startup</param>
        /// <param name="endpoints">The role endpoints</param>
        /// <param name="localResources">The role local resources</param>
        /// <param name="configurationSettings">The role configuration settings</param>
        /// <param name="sdkVersion">The current SDK version</param>
        private void CachingConfigurationFactoryMethod(
            CloudServiceProject cloudServiceProject,
            string roleName,
            bool isWebRole,
            string cacheWorkerRole,
            Startup startup,
            Endpoints endpoints,
            LocalResources localResources,
            ref DefinitionConfigurationSetting[] configurationSettings,
            string sdkVersion)
        {
            switch (sdkVersion)
            {
                case SDKVersion.Version180:
                    Version180Configuration(
                        cloudServiceProject,
                        roleName,
                        isWebRole,
                        cacheWorkerRole,
                        startup,
                        endpoints,
                        localResources,
                        ref configurationSettings);
                    break;

                default:
                    throw new Exception(string.Format(Resources.AzureSdkVersionNotSupported,
                        Resources.MinSupportAzureSdkVersion, Resources.MaxSupportAzureSdkVersion));
            }
        }
 /// <summary>
 /// Asserts that given environment variable exists with it's associated value.
 /// </summary>
 /// <param name="roleStartup">The role startup</param>
 /// <param name="variableName">The environment variable name</param>
 /// <param name="expectedValue">The expected value</param>
 public static void ValidateRoleRuntimeVariable(Startup roleStartup, string variableName, string expectedValue)
 {
     string actualValue;
     Assert.IsTrue(TryGetEnvironmentValue(roleStartup.Task, variableName, out actualValue));
     Assert.AreEqual<string>(expectedValue, actualValue);
 }