public async Task GivenACacheIdentityWhichDoesntExistInAnyCache_FindItemInCache_ReturnsItemFromDataStoreQueryAndAddsToCaches() { // Arrange. var loggingService = A.Fake <ILoggingService>(); var l1Cache = A.Fake <ICacheProvider>(); var l2Cache = A.Fake <ICacheProvider>(); var cacheLayer = new CacheManagerTest(loggingService, new[] { l1Cache, l2Cache }); const string cacheKey = "Test"; var expiry = TimeSpan.FromMinutes(1); var cacheIdentity = new CacheIdentity(cacheKey, expiry); var testCacheObject = A.Fake <TestCacheObject>(); var dataStoreQuery = A.Fake <Func <Task <TestCacheObject> > >(); A.CallTo(() => l1Cache.GetAsync <TestCacheObject>(cacheIdentity.CacheKey)).Returns(Task.FromResult <TestCacheObject>(null)); A.CallTo(() => l2Cache.GetAsync <TestCacheObject>(cacheIdentity.CacheKey)).Returns(Task.FromResult <TestCacheObject>(null)); // Act. var result = await cacheLayer.GetAsync(cacheIdentity, dataStoreQuery); // Assert. result.ShouldNotBe(null); result.Key.ShouldBe(testCacheObject.Key); A.CallTo(() => l1Cache.GetAsync <TestCacheObject>(cacheIdentity.CacheKey)).MustHaveHappened(Repeated.Exactly.Once); A.CallTo(() => l2Cache.GetAsync <TestCacheObject>(cacheIdentity.CacheKey)).MustHaveHappened(Repeated.Exactly.Once); A.CallTo(() => l1Cache.AddAsync(cacheIdentity.CacheKey, A <TestCacheObject> ._, A <TimeSpan> ._)).MustHaveHappened(Repeated.Exactly.Once); A.CallTo(() => l2Cache.AddAsync(cacheIdentity.CacheKey, A <TestCacheObject> ._, A <TimeSpan> ._)).MustHaveHappened(Repeated.Exactly.Once); A.CallTo(() => dataStoreQuery.Invoke()).MustHaveHappened(); }
/// <summary> /// Convenience method for defining the creation options for a cache /// </summary> /// <remarks> /// Equivalent of defining the options to create a cache with a <paramref name="cacheId"/> and extended with the /// <see cref="CacheDecoratorOptions.Default"/> set of cache decorators /// </remarks> public static CacheCreationOptions DefaultsWith(CacheIdentity cacheId) { return(new CacheCreationOptions { Name = cacheId.Name, InstanceName = cacheId.InstanceName, DecoratorOptions = CacheDecoratorOptions.Default }); }
private IHttpActionResult StartOrPauseCache(CacheIdentity cacheId, bool isPaused) { Func <CacheIdentity, bool> action = isPaused ? (Func <CacheIdentity, bool>)_cacheAdministator.PauseCache : _cacheAdministator.StartCache; bool isSuccess = action(cacheId); if (isSuccess) { return(Ok()); } else { return(NotFound()); } }
public void TestInvalidSku() { this.testOutputHelper.WriteLine($"Running in {HttpMockServer.GetCurrentMode()} mode."); using (StorageCacheTestContext context = new StorageCacheTestContext(this)) { var client = context.GetClient <StorageCacheManagementClient>(); client.ApiVersion = StorageCacheTestEnvironmentUtilities.APIVersion; this.fixture.CacheHelper.StoragecacheManagementClient = client; var cacheIdentity = new CacheIdentity() { Type = CacheIdentityType.SystemAssigned }; CloudErrorException ex = Assert.Throws <CloudErrorException>( () => this.fixture.CacheHelper.Create("InvalidCacheSku", "InvalidSku", 3072, identity: cacheIdentity)); this.testOutputHelper.WriteLine($"{ex.Body.Error.Message}"); Assert.Equal("InvalidParameter", ex.Body.Error.Code); Assert.Equal("sku", ex.Body.Error.Target); } }
public void TestInvalidSubscription() { this.testOutputHelper.WriteLine($"Running in {HttpMockServer.GetCurrentMode()} mode."); using (StorageCacheTestContext context = new StorageCacheTestContext(this)) { var client = context.GetClient <StorageCacheManagementClient>(); client.ApiVersion = StorageCacheTestEnvironmentUtilities.APIVersion; client.SubscriptionId = "4525f627-2e14-411c-96b9-7df2be6eeb93"; this.fixture.CacheHelper.StoragecacheManagementClient = client; var cacheIdentity = new CacheIdentity() { Type = CacheIdentityType.SystemAssigned }; CloudErrorException ex = Assert.Throws <CloudErrorException>( () => this.fixture.CacheHelper.Create("InvalidSubscriptionId", this.fixture.Cache.Sku.Name, 3072, identity: cacheIdentity, skipGet: true)); this.testOutputHelper.WriteLine($"{ex.Body.Error.Message}"); Assert.Equal("SubscriptionNotFound", ex.Body.Error.Code); client.SubscriptionId = this.fixture.SubscriptionID; this.fixture.CacheHelper.StoragecacheManagementClient = client; } }
public async Task <SampleObject> GetDaDataAsync() { var cacheIdentity = new CacheIdentity("Test-Cache-Key", TimeSpan.FromSeconds(30)); return(await FindItemInCacheOrDataStoreAsync(cacheIdentity, GetFromDataStore)); }
/// <summary> /// Initializes a new instance of the <see cref="StorageCacheTestFixture"/> class. /// </summary> public StorageCacheTestFixture() { notes = new List <string>(); notes.Add("Starting StorageCacheTestFixture"); using (this.Context = new StorageCacheTestContext(this)) { notes.Add("StorageCacheTestFixture Using New Context"); notes.Add(Context.notes.ToString()); this.Context = new StorageCacheTestContext(this); try { notes.Add("StorageCacheTestFixture Using Second New Context"); notes.Add(Context.notes.ToString()); notes.Add("StorageCacheTestFixture After Second StorageCacheTestContext"); StorageCacheManagementClient storagecacheMgmtClient = this.Context.GetClient <StorageCacheManagementClient>(); storagecacheMgmtClient.ApiVersion = StorageCacheTestEnvironmentUtilities.APIVersion; notes.Add("StorageCacheTestFixture After GetClient."); notes.Add(Context.notes.ToString()); if (string.IsNullOrEmpty(StorageCacheTestEnvironmentUtilities.ResourceGroupName) && string.IsNullOrEmpty(StorageCacheTestEnvironmentUtilities.CacheName)) { this.resGroupName = StorageCacheTestUtilities.GenerateName(StorageCacheTestEnvironmentUtilities.ResourcePrefix); this.virNetworkName = "VNet-" + this.resGroupName; this.subnetName = "Subnet-" + this.resGroupName; this.cacheName = "Cache-" + this.resGroupName; notes.Add($"StorageCacheTestFixture ResourceGroupName && CacheName is null or empty, using Group Name {resGroupName}, Vnet {virNetworkName}, Subnet {subnetName}, Cache {cacheName}."); } else { this.resGroupName = StorageCacheTestEnvironmentUtilities.ResourceGroupName; this.cacheName = StorageCacheTestEnvironmentUtilities.CacheName; this.ResourceGroup = this.Context.GetOrCreateResourceGroup(this.resGroupName, StorageCacheTestEnvironmentUtilities.Location); this.Cache = this.Context.GetCacheIfExists(this.ResourceGroup, this.cacheName); notes.Add($"StorageCacheTestFixture ResourceGroupName && CacheName is not null or empty, using Group Name {resGroupName}, Cache {cacheName}."); notes.Add("SCTFixture Get cache if exists."); notes.Add(Context.notes.ToString()); if (this.Cache != null) { Match subnetMatch = SubnetRegex.Match(this.Cache.Subnet); this.virNetworkName = subnetMatch.Groups["VNetName"].Value; this.subnetName = subnetMatch.Groups["SubnetName"].Value; notes.Add($"SCTFixture Cache is not null using vnet {virNetworkName} and subnet {subnetName}"); } else { this.virNetworkName = "VNet-" + this.resGroupName; this.subnetName = "Subnet-" + this.resGroupName; notes.Add($"SCTFixture Cache is null using vnet {virNetworkName} and subnet {subnetName}"); } } if (this.ResourceGroup == null) { notes.Add("SCTFixture ResourceGroup is null. Get or create resource group"); this.ResourceGroup = this.Context.GetOrCreateResourceGroup(this.resGroupName, StorageCacheTestEnvironmentUtilities.Location); notes.Add(Context.notes.ToString()); } this.VirtualNetwork = this.Context.GetOrCreateVirtualNetwork(this.ResourceGroup, this.virNetworkName); this.SubNet = this.Context.GetOrCreateSubnet(this.ResourceGroup, this.VirtualNetwork, this.subnetName); notes.Add("SCTFixture Get or create vnet and subnet."); notes.Add(Context.notes.ToString()); this.SubscriptionID = StorageCacheTestEnvironmentUtilities.SubscriptionId(); notes.Add($"SCTFixture Using subscription id {SubscriptionID.ToString()}."); this.CacheHelper = new CacheHelper(this.SubscriptionID, storagecacheMgmtClient, this.ResourceGroup, this.VirtualNetwork, this.SubNet); notes.Add($"SCTFixture New CacheHelper"); notes.Add(CacheHelper.notes.ToString()); var sku = StorageCacheTestEnvironmentUtilities.CacheSku; var size = StorageCacheTestEnvironmentUtilities.CacheSize; var int_size = int.Parse(size); if (this.Cache == null) { this.Cache = null; CacheIdentity cacheIdentity; if (StorageCacheTestEnvironmentUtilities.APIVersion == "2019-11-01") { cacheIdentity = new CacheIdentity() { Type = CacheIdentityType.None }; } else { cacheIdentity = new CacheIdentity() { Type = CacheIdentityType.SystemAssigned }; } CacheNetworkSettings cacheNetworkSettings = new CacheNetworkSettings(); cacheNetworkSettings.NtpServer = "time.windows.com"; cacheNetworkSettings.Mtu = 1500; // Build up cache security settings. CacheSecuritySettings cacheSecuritySettings = new CacheSecuritySettings(); NfsAccessPolicy nfsAccessPolicy = new NfsAccessPolicy(); nfsAccessPolicy.Name = "testAccessPolicy"; NfsAccessRule nfsAccessRule = new NfsAccessRule { Access = "rw", Scope = "default", Suid = false, SubmountAccess = true, RootSquash = false }; List <NfsAccessRule> accessRules = new List <NfsAccessRule>(); accessRules.Add(nfsAccessRule); nfsAccessPolicy.AccessRules = accessRules; List <NfsAccessPolicy> accessPolicies = new List <NfsAccessPolicy>(); accessPolicies.Add(nfsAccessPolicy); cacheSecuritySettings.AccessPolicies = accessPolicies; notes.Add("SCTFixture CacheHelper Create Cache"); this.Cache = this.CacheHelper.Create(this.cacheName, sku, int_size, identity: cacheIdentity, networkSettings: cacheNetworkSettings); if (HttpMockServer.Mode == HttpRecorderMode.Record) { notes.Add("SCTFixture CacheHelper Create Cache Record Mode Check Cache State"); this.CacheHelper.CheckCacheState(this.cacheName); } } } catch (Exception) { this.Context.Dispose(); throw; } } notes.Add("SCTFixture End"); }
/// <summary> /// Initializes a new instance of the <see cref="StorageCacheTestFixture"/> class. /// </summary> public StorageCacheTestFixture() { using (this.Context = new StorageCacheTestContext(this)) { this.Context = new StorageCacheTestContext(this); try { StorageCacheManagementClient storagecacheMgmtClient = this.Context.GetClient <StorageCacheManagementClient>(); storagecacheMgmtClient.ApiVersion = StorageCacheTestEnvironmentUtilities.APIVersion; if (string.IsNullOrEmpty(StorageCacheTestEnvironmentUtilities.ResourceGroupName) && string.IsNullOrEmpty(StorageCacheTestEnvironmentUtilities.CacheName)) { this.resGroupName = StorageCacheTestUtilities.GenerateName(StorageCacheTestEnvironmentUtilities.ResourcePrefix); this.virNetworkName = "VNet-" + this.resGroupName; this.subnetName = "Subnet-" + this.resGroupName; this.cacheName = "Cache-" + this.resGroupName; } else { this.resGroupName = StorageCacheTestEnvironmentUtilities.ResourceGroupName; this.cacheName = StorageCacheTestEnvironmentUtilities.CacheName; this.ResourceGroup = this.Context.GetOrCreateResourceGroup(this.resGroupName, StorageCacheTestEnvironmentUtilities.Location); this.Cache = this.Context.GetCacheIfExists(this.ResourceGroup, this.cacheName); if (this.Cache != null) { Match subnetMatch = SubnetRegex.Match(this.Cache.Subnet); this.virNetworkName = subnetMatch.Groups["VNetName"].Value; this.subnetName = subnetMatch.Groups["SubnetName"].Value; } else { this.virNetworkName = "VNet-" + this.resGroupName; this.subnetName = "Subnet-" + this.resGroupName; } } if (this.ResourceGroup == null) { this.ResourceGroup = this.Context.GetOrCreateResourceGroup(this.resGroupName, StorageCacheTestEnvironmentUtilities.Location); } this.VirtualNetwork = this.Context.GetOrCreateVirtualNetwork(this.ResourceGroup, this.virNetworkName); this.SubNet = this.Context.GetOrCreateSubnet(this.ResourceGroup, this.VirtualNetwork, this.subnetName); this.SubscriptionID = StorageCacheTestEnvironmentUtilities.SubscriptionId(); this.CacheHelper = new CacheHelper(this.SubscriptionID, storagecacheMgmtClient, this.ResourceGroup, this.VirtualNetwork, this.SubNet); var sku = StorageCacheTestEnvironmentUtilities.CacheSku; var size = StorageCacheTestEnvironmentUtilities.CacheSize; var int_size = int.Parse(size); if (this.Cache == null) { this.Cache = null; CacheIdentity cacheIdentity; if (StorageCacheTestEnvironmentUtilities.APIVersion == "2019-11-01") { cacheIdentity = new CacheIdentity() { Type = CacheIdentityType.None }; } else { cacheIdentity = new CacheIdentity() { Type = CacheIdentityType.SystemAssigned }; } this.Cache = this.CacheHelper.Create(this.cacheName, sku, int_size, identity: cacheIdentity); if (HttpMockServer.Mode == HttpRecorderMode.Record) { this.CacheHelper.CheckCacheState(this.cacheName); } } } catch (Exception) { this.Context.Dispose(); throw; } } }
public CacheManagerWrapper(CacheIdentity id, ICacheManager <object> impl) : base(id) { Impl = impl; }
public async Task <TestCacheObject> GetAsync(CacheIdentity cacheIdentity, Func <Task <TestCacheObject> > dbQuery) { return(await FindItemInCacheOrDataStoreAsync(cacheIdentity, dbQuery)); }
/// <summary> /// Create cache. /// </summary> /// <param name="name">Name of the cache.</param> /// <param name="sku">Name of the SKU.</param> /// <param name="cacheSize">Size of cache.</param> /// <param name="identity">Cache identity type.</param> /// <param name="keyVaultResourceId">Describes a resource Id to source Key vault.</param> /// <param name="encryptionKeyURL">The URL referencing a key encryption key in key vault.</param> /// <param name="skipGet">Skip get cache before creating it.</param> /// <returns>Cache object.</returns> public Cache Create(string name, string sku, int cacheSize, CacheIdentity identity, KeyVaultKeyReferenceSourceVault keyVaultResourceId = null, string encryptionKeyURL = null, bool skipGet = false) { Cache cache; CacheEncryptionSettings cacheEncryptionSettings; KeyVaultKeyReference keyVaultKeyReference; if (!skipGet) { try { cache = this.Get(name); } catch (CloudErrorException ex) { if (ex.Body.Error.Code == "ResourceNotFound") { cache = null; } else { throw; } } } else { cache = null; } if (cache == null) { var cacheSku = new CacheSku() { Name = sku }; var subnetUri = $"/subscriptions/{this.subscriptionId}/resourcegroups/{this.resourceGroup.Name}/providers/Microsoft.Network/virtualNetworks/{this.virtualNetwork.Name}/subnets/{this.subNet.Name}"; if (encryptionKeyURL is null || keyVaultResourceId is null) { keyVaultKeyReference = new KeyVaultKeyReference() { }; cacheEncryptionSettings = new CacheEncryptionSettings() { }; } else { keyVaultKeyReference = new KeyVaultKeyReference() { KeyUrl = encryptionKeyURL, SourceVault = keyVaultResourceId, }; cacheEncryptionSettings = new CacheEncryptionSettings() { KeyEncryptionKey = keyVaultKeyReference, }; } var cacheParameters = new Cache() { CacheSizeGB = cacheSize, Location = this.resourceGroup.Location, Sku = cacheSku, Subnet = subnetUri, Identity = identity, EncryptionSettings = cacheEncryptionSettings, }; cache = this.StoragecacheManagementClient.Caches.CreateOrUpdate(this.resourceGroup.Name, name, cacheParameters); }