public async void FileBackupSuccessCallsEncrypt() { if (File.Exists(this.tempFileName)) { File.Delete(this.tempFileName); } var underlying = new Mock <IConfigSource>(); underlying.SetupSequence(t => t.GetDeploymentConfigInfoAsync()) .ReturnsAsync(ValidConfigInfo1); ISerde <DeploymentConfigInfo> serde = this.GetSerde(); var encryptionProvider = new Mock <IEncryptionProvider>(); encryptionProvider.Setup(ep => ep.EncryptAsync(It.IsAny <string>())) .ReturnsAsync(serde.Serialize(ValidConfigInfo1)); using (IConfigSource configSource = new FileBackupConfigSource(this.tempFileName, underlying.Object, serde, encryptionProvider.Object)) { DeploymentConfigInfo config1 = await configSource.GetDeploymentConfigInfoAsync(); Assert.NotNull(config1); Assert.True(File.Exists(this.tempFileName)); string backupJson = await DiskFile.ReadAllAsync(this.tempFileName); string returnedJson = serde.Serialize(config1); Assert.Equal(backupJson, returnedJson, true); encryptionProvider.Verify(ep => ep.EncryptAsync(It.IsAny <string>())); } }
public async void FileBackupSuccessWhenFileNotExists() { if (File.Exists(this.tempFileName)) { File.Delete(this.tempFileName); } var underlying = new Mock <IConfigSource>(); underlying.SetupSequence(t => t.GetDeploymentConfigInfoAsync()) .ReturnsAsync(ValidConfigInfo1) .ThrowsAsync(new InvalidOperationException()); ISerde <DeploymentConfigInfo> serde = this.GetSerde(); using (IConfigSource configSource = new FileBackupConfigSource(this.tempFileName, underlying.Object, serde, NullEncryptionProvider.Instance)) { DeploymentConfigInfo config1 = await configSource.GetDeploymentConfigInfoAsync(); Assert.NotNull(config1); Assert.True(File.Exists(this.tempFileName)); string backupJson = await DiskFile.ReadAllAsync(this.tempFileName); string returnedJson = serde.Serialize(config1); Assert.True(string.Equals(backupJson, returnedJson, StringComparison.OrdinalIgnoreCase)); DeploymentConfigInfo config2 = await configSource.GetDeploymentConfigInfoAsync(); Assert.NotNull(config2); Assert.Equal(serde.Serialize(config1), serde.Serialize(config2)); } }
public async void ReadDeploymentConfigFromSecret() { ISerde <DeploymentConfigInfo> serde = this.GetSerde(); var secretData = new Dictionary <string, byte[]> { ["backup.json"] = System.Text.Encoding.UTF8.GetBytes(serde.Serialize(ValidConfigInfo1)) }; var secret = new V1Secret(data: secretData); var response = new HttpOperationResponse <V1Secret>() { Body = secret, Response = new HttpResponseMessage(HttpStatusCode.OK) }; var client = new Mock <IKubernetes>(MockBehavior.Strict); client.Setup(c => c.ReadNamespacedSecretWithHttpMessagesAsync(DefaultSecretName, DefaultNamespace, It.IsAny <bool?>(), It.IsAny <bool?>(), null, null, It.IsAny <CancellationToken>())) .ReturnsAsync(response); var backupSource = new DeploymentSecretBackup(DefaultSecretName, DefaultNamespace, DefaultOwner, serde, client.Object); var deploymentConfigInfo = await backupSource.ReadFromBackupAsync(); string returnedJson = serde.Serialize(deploymentConfigInfo); string expectedJson = serde.Serialize(ValidConfigInfo1); Assert.Equal(expectedJson, returnedJson, ignoreCase: true); client.VerifyAll(); }
public async void FileBackupReadFromBackupCallsEncryptDecrypt() { if (File.Exists(this.tempFileName)) { File.Delete(this.tempFileName); } ISerde <DeploymentConfigInfo> serde = this.GetSerde(); var encryptionProvider = new Mock <IEncryptionProvider>(); encryptionProvider.Setup(ep => ep.EncryptAsync(It.IsAny <string>())) .ReturnsAsync(serde.Serialize(ValidConfigInfo1)); encryptionProvider.Setup(ep => ep.DecryptAsync(It.IsAny <string>())) .ReturnsAsync(serde.Serialize(ValidConfigInfo1)); IDeploymentBackupSource fileBackup = new DeploymentFileBackup(this.tempFileName, serde, encryptionProvider.Object); await fileBackup.BackupDeploymentConfigAsync(ValidConfigInfo1); DeploymentConfigInfo config1 = await fileBackup.ReadFromBackupAsync(); Assert.NotNull(config1); Assert.True(File.Exists(this.tempFileName)); string backupJson = await DiskFile.ReadAllAsync(this.tempFileName); string returnedJson = serde.Serialize(config1); string expectedJson = serde.Serialize(ValidConfigInfo1); Assert.Equal(expectedJson, backupJson, ignoreCase: true); Assert.Equal(expectedJson, returnedJson, ignoreCase: true); encryptionProvider.Verify(ep => ep.EncryptAsync(It.IsAny <string>())); encryptionProvider.Verify(ep => ep.DecryptAsync(It.IsAny <string>())); }
public async void BackupDeploymentDoesNotThrowOnFailure() { ISerde <DeploymentConfigInfo> serde = this.GetSerde(); string expectedJson = serde.Serialize(ValidConfigInfo1); var readSecretData = new Dictionary <string, byte[]> { ["backup.json"] = System.Text.Encoding.UTF8.GetBytes(serde.Serialize(ValidConfigInfo2)) }; var readSecret = new V1Secret(data: readSecretData); var replaceSecretData = new Dictionary <string, byte[]> { ["backup.json"] = System.Text.Encoding.UTF8.GetBytes(expectedJson) }; var replaceSecret = new V1Secret(data: readSecretData); var readResponse = new HttpOperationResponse <V1Secret>() { Body = readSecret, Response = new HttpResponseMessage(HttpStatusCode.OK) }; var client = new Mock <IKubernetes>(MockBehavior.Strict); client.Setup(c => c.ReadNamespacedSecretWithHttpMessagesAsync(DefaultSecretName, DefaultNamespace, It.IsAny <bool?>(), It.IsAny <bool?>(), null, null, It.IsAny <CancellationToken>())) .ReturnsAsync(readResponse); client.Setup(c => c.ReplaceNamespacedSecretWithHttpMessagesAsync(It.IsAny <V1Secret>(), DefaultSecretName, DefaultNamespace, null, null, null, null, It.IsAny <CancellationToken>())) .ThrowsAsync(new HttpOperationException("Not Permitted")); var backupSource = new DeploymentSecretBackup(DefaultSecretName, DefaultNamespace, DefaultOwner, serde, client.Object); await backupSource.BackupDeploymentConfigAsync(ValidConfigInfo1); client.VerifyAll(); }
public async void BackupDeploymentConfigReplacesSecret() { ISerde<DeploymentConfigInfo> serde = this.GetSerde(); string expectedJson = serde.Serialize(ValidConfigInfo1); var readSecretData = new Dictionary<string, byte[]> { ["backup.json"] = System.Text.Encoding.UTF8.GetBytes(serde.Serialize(ValidConfigInfo2)) }; var readSecret = new V1Secret(data: readSecretData); var replaceSecretData = new Dictionary<string, byte[]> { ["backup.json"] = System.Text.Encoding.UTF8.GetBytes(expectedJson) }; var replaceSecret = new V1Secret(data: readSecretData); var readResponse = new HttpOperationResponse<V1Secret>() { Body = readSecret, Response = new HttpResponseMessage(HttpStatusCode.OK) }; var replaceResponse = new HttpOperationResponse<V1Secret>() { Body = replaceSecret, Response = new HttpResponseMessage(HttpStatusCode.OK) }; byte[] receivedData = default(byte[]); var client = new Mock<IKubernetes>(MockBehavior.Strict); client.Setup(c => c.ReadNamespacedSecretWithHttpMessagesAsync( DefaultSecretName, DefaultNamespace, It.IsAny<string>(), // pretty null, // customHeaders It.IsAny<CancellationToken>())) .ReturnsAsync(readResponse); client.Setup(c => c.ReplaceNamespacedSecretWithHttpMessagesAsync( It.IsAny<V1Secret>(), DefaultSecretName, DefaultNamespace, null, // dryRun null, // fieldmanager null, // pretty null, // customHeaders It.IsAny<CancellationToken>())) .Callback((V1Secret body, string name, string namespaceParameter, string dryRun, string fieldManager, string pretty, Dictionary<string, List<string>> customHeaders, CancellationToken cancellationToken) => { Assert.True(body.Data != null); Assert.True(body.Data.TryGetValue("backup.json", out receivedData)); }) .ReturnsAsync(replaceResponse); var backupSource = new DeploymentSecretBackup(DefaultSecretName, DefaultNamespace, DefaultOwner, serde, client.Object); await backupSource.BackupDeploymentConfigAsync(ValidConfigInfo1); string backupJson = System.Text.Encoding.UTF8.GetString(receivedData); Assert.Equal(expectedJson, backupJson, ignoreCase: true); client.VerifyAll(); }
public async void FileBackupReadFromBackupCallsEncryptDecrypt() { if (File.Exists(this.tempFileName)) { File.Delete(this.tempFileName); } var underlying = new Mock <IConfigSource>(); underlying.SetupSequence(t => t.GetDeploymentConfigInfoAsync()) .ReturnsAsync(ValidConfigInfo1) .ThrowsAsync(new InvalidOperationException()); ISerde <DeploymentConfigInfo> serde = this.GetSerde(); var encryptionProvider = new Mock <IEncryptionProvider>(); encryptionProvider.Setup(ep => ep.EncryptAsync(It.IsAny <string>())) .ReturnsAsync(serde.Serialize(ValidConfigInfo1)); encryptionProvider.Setup(ep => ep.DecryptAsync(It.IsAny <string>())) .ReturnsAsync(serde.Serialize(ValidConfigInfo1)); DeploymentConfigInfo config1; DeploymentConfigInfo config2; using (IConfigSource configSource = new FileBackupConfigSource(this.tempFileName, underlying.Object, serde, encryptionProvider.Object)) { config1 = await configSource.GetDeploymentConfigInfoAsync(); Assert.NotNull(config1); Assert.True(File.Exists(this.tempFileName)); string backupJson = await DiskFile.ReadAllAsync(this.tempFileName); string returnedJson = serde.Serialize(config1); Assert.True(string.Equals(backupJson, returnedJson, StringComparison.OrdinalIgnoreCase)); } using (IConfigSource configSource = new FileBackupConfigSource(this.tempFileName, underlying.Object, serde, encryptionProvider.Object)) { config2 = await configSource.GetDeploymentConfigInfoAsync(); Assert.NotNull(config2); } Assert.Equal(serde.Serialize(config1), serde.Serialize(config2)); encryptionProvider.Verify(ep => ep.EncryptAsync(It.IsAny <string>())); encryptionProvider.Verify(ep => ep.DecryptAsync(It.IsAny <string>())); }
public async void SecretDataNoKeyReturnsEmptyConfig() { ISerde<DeploymentConfigInfo> serde = this.GetSerde(); var secretData = new Dictionary<string, byte[]> { ["no match"] = System.Text.Encoding.UTF8.GetBytes(serde.Serialize(ValidConfigInfo1)) }; var secret = new V1Secret(data: secretData); var response = new HttpOperationResponse<V1Secret>() { Body = secret, Response = new HttpResponseMessage(HttpStatusCode.OK) }; var client = new Mock<IKubernetes>(MockBehavior.Strict); client.Setup(c => c.ReadNamespacedSecretWithHttpMessagesAsync( DefaultSecretName, DefaultNamespace, It.IsAny<string>(), // pretty null, // customHeaders It.IsAny<CancellationToken>())) .ReturnsAsync(response); var backupSource = new DeploymentSecretBackup(DefaultSecretName, DefaultNamespace, DefaultOwner, serde, client.Object); var deploymentConfigInfo = await backupSource.ReadFromBackupAsync(); Assert.NotNull(deploymentConfigInfo); Assert.Equal(DeploymentConfigInfo.Empty, deploymentConfigInfo); client.VerifyAll(); }
public async void FileBackupShouldNotThrowWhenDecryptFails() { if (File.Exists(this.tempFileName)) { File.Delete(this.tempFileName); } var underlying = new Mock <IConfigSource>(); underlying.SetupSequence(t => t.GetDeploymentConfigInfoAsync()) .ReturnsAsync(ValidConfigInfo1) .ThrowsAsync(new InvalidOperationException()); ISerde <DeploymentConfigInfo> serde = this.GetSerde(); var encryptionProvider = new Mock <IEncryptionProvider>(); encryptionProvider.Setup(ep => ep.EncryptAsync(It.IsAny <string>())) .ReturnsAsync(serde.Serialize(ValidConfigInfo1)); encryptionProvider.Setup(ep => ep.DecryptAsync(It.IsAny <string>())) .ThrowsAsync(new WorkloadCommunicationException("failed", 404)); using (IConfigSource configSource = new FileBackupConfigSource(this.tempFileName, underlying.Object, serde, encryptionProvider.Object)) { DeploymentConfigInfo config1 = await configSource.GetDeploymentConfigInfoAsync(); Assert.NotNull(config1); Assert.True(File.Exists(this.tempFileName)); string backupJson = await DiskFile.ReadAllAsync(this.tempFileName); string returnedJson = serde.Serialize(config1); Assert.Equal(backupJson, returnedJson, ignoreCase: true); } using (IConfigSource configSource = new FileBackupConfigSource(this.tempFileName, underlying.Object, serde, encryptionProvider.Object)) { DeploymentConfigInfo config2 = await configSource.GetDeploymentConfigInfoAsync(); Assert.NotNull(config2); Assert.Equal(DeploymentConfigInfo.Empty, config2); encryptionProvider.Verify(ep => ep.EncryptAsync(It.IsAny <string>())); encryptionProvider.Verify(ep => ep.DecryptAsync(It.IsAny <string>())); } }
public async void FileBackupSuccessWhenFileNotExists() { if (File.Exists(this.tempFileName)) { File.Delete(this.tempFileName); } ISerde <DeploymentConfigInfo> serde = this.GetSerde(); IDeploymentBackupSource fileBackup = new DeploymentFileBackup(this.tempFileName, serde, NullEncryptionProvider.Instance); await fileBackup.BackupDeploymentConfigAsync(ValidConfigInfo1); Assert.True(File.Exists(this.tempFileName)); string backupJson = await DiskFile.ReadAllAsync(this.tempFileName); string expectedJson = serde.Serialize(ValidConfigInfo1); Assert.Equal(expectedJson, backupJson, ignoreCase: true); }
public async void FileBackupDoesNotHappenIfConfigSourceReportsException() { if (File.Exists(this.tempFileName)) { File.Delete(this.tempFileName); } // Arrange var underlying = new Mock <IConfigSource>(); underlying.SetupSequence(cs => cs.GetDeploymentConfigInfoAsync()) .ReturnsAsync(ValidConfigInfo1) .ReturnsAsync(new DeploymentConfigInfo(10, new InvalidOperationException())); ISerde <DeploymentConfigInfo> serde = this.GetSerde(); // Act using (IConfigSource configSource = new FileBackupConfigSource(this.tempFileName, underlying.Object, serde, NullEncryptionProvider.Instance)) { // this call should fetch the config properly DeploymentConfigInfo config1 = await configSource.GetDeploymentConfigInfoAsync(); Assert.NotNull(config1); Assert.Equal(0, config1.Version); Assert.False(config1.Exception.HasValue); // this should cause the version with the exception to be returned DeploymentConfigInfo config2 = await configSource.GetDeploymentConfigInfoAsync(); // Assert Assert.NotNull(config2); Assert.True(config2.Exception.HasValue); Assert.IsType <InvalidOperationException>(config2.Exception.OrDefault()); // this should still be the JSON from the first config - config1 string backupJson = await DiskFile.ReadAllAsync(this.tempFileName); string returnedJson = serde.Serialize(config1); Assert.True(string.Equals(backupJson, returnedJson, StringComparison.OrdinalIgnoreCase)); } }
public async void FileBackupReadThrowsWhenDecryptFails() { if (File.Exists(this.tempFileName)) { File.Delete(this.tempFileName); } ISerde <DeploymentConfigInfo> serde = this.GetSerde(); var encryptionProvider = new Mock <IEncryptionProvider>(); encryptionProvider.Setup(ep => ep.EncryptAsync(It.IsAny <string>())) .ReturnsAsync(serde.Serialize(ValidConfigInfo1)); encryptionProvider.Setup(ep => ep.DecryptAsync(It.IsAny <string>())) .ThrowsAsync(new WorkloadCommunicationException("failed", 404)); IDeploymentBackupSource fileBackup = new DeploymentFileBackup(this.tempFileName, serde, encryptionProvider.Object); await fileBackup.BackupDeploymentConfigAsync(ValidConfigInfo1); await Assert.ThrowsAsync <WorkloadCommunicationException>(async() => await fileBackup.ReadFromBackupAsync()); }
public async void FileBackupDoesNotHappenIfConfigSourceEmpty() { if (File.Exists(this.tempFileName)) { File.Delete(this.tempFileName); } // Arrange var underlying = new Mock <IConfigSource>(); underlying.SetupSequence(cs => cs.GetDeploymentConfigInfoAsync()) .ReturnsAsync(ValidConfigInfo1) .ReturnsAsync(DeploymentConfigInfo.Empty); ISerde <DeploymentConfigInfo> serde = this.GetSerde(); // Act using (IConfigSource configSource = new FileBackupConfigSource(this.tempFileName, underlying.Object, serde, NullEncryptionProvider.Instance)) { // this call should fetch the config properly DeploymentConfigInfo config1 = await configSource.GetDeploymentConfigInfoAsync(); Assert.NotNull(config1); Assert.Equal(0, config1.Version); // this should cause the version with the exception to be returned DeploymentConfigInfo config2 = await configSource.GetDeploymentConfigInfoAsync(); // Assert Assert.NotNull(config2); Assert.Equal(0, config2.Version); Assert.Equal(config2.DeploymentConfig.Modules, config1.DeploymentConfig.Modules); // this should still be the JSON from the first config - config1 string backupJson = await DiskFile.ReadAllAsync(this.tempFileName); string returnedJson = serde.Serialize(config1); Assert.Equal(backupJson, returnedJson, ignoreCase: true); } }
public async void FileBackupWriteOnlyWhenConfigurationChanges() { if (File.Exists(this.tempFileName)) { File.Delete(this.tempFileName); } var underlying = new Mock <IConfigSource>(); underlying.SetupSequence(t => t.GetDeploymentConfigInfoAsync()) .ReturnsAsync(ValidConfigInfo1) .ReturnsAsync(ValidConfigInfo1) .ReturnsAsync(ValidConfigInfo2) .ReturnsAsync(ValidConfigInfo2); ISerde <DeploymentConfigInfo> serde = this.GetSerde(); using (IConfigSource configSource = new FileBackupConfigSource(this.tempFileName, underlying.Object, serde, NullEncryptionProvider.Instance)) { DeploymentConfigInfo config1 = await configSource.GetDeploymentConfigInfoAsync(); Assert.NotNull(config1); Assert.True(File.Exists(this.tempFileName)); string backupJson = await DiskFile.ReadAllAsync(this.tempFileName); string returnedJson = serde.Serialize(config1); Assert.True(string.Equals(backupJson, returnedJson, StringComparison.OrdinalIgnoreCase)); DateTime modifiedTime1 = File.GetLastWriteTimeUtc(this.tempFileName); Assert.True(DateTime.UtcNow - modifiedTime1 < TimeSpan.FromSeconds(5)); DeploymentConfigInfo config2 = await configSource.GetDeploymentConfigInfoAsync(); Assert.NotNull(config2); Assert.Equal(serde.Serialize(config1), serde.Serialize(config2)); DateTime modifiedTime2 = File.GetLastWriteTimeUtc(this.tempFileName); Assert.Equal(modifiedTime2, modifiedTime1); DeploymentConfigInfo config3 = await configSource.GetDeploymentConfigInfoAsync(); Assert.NotNull(config3); Assert.True(File.Exists(this.tempFileName)); backupJson = await DiskFile.ReadAllAsync(this.tempFileName); returnedJson = serde.Serialize(config3); Assert.True(string.Equals(backupJson, returnedJson, StringComparison.OrdinalIgnoreCase)); DateTime modifiedTime3 = File.GetLastWriteTimeUtc(this.tempFileName); Assert.True(DateTime.UtcNow - modifiedTime1 < TimeSpan.FromSeconds(5)); Assert.NotEqual(modifiedTime1, modifiedTime3); DeploymentConfigInfo config4 = await configSource.GetDeploymentConfigInfoAsync(); Assert.NotNull(config4); Assert.Equal(serde.Serialize(config4), serde.Serialize(config4)); DateTime modifiedTime4 = File.GetLastWriteTimeUtc(this.tempFileName); Assert.Equal(modifiedTime4, modifiedTime3); } }
public async Task FileBackupReadOnlyWhenUninitialized() { if (File.Exists(this.tempFileName)) { File.Delete(this.tempFileName); } var underlying = new Mock <IConfigSource>(); underlying.SetupSequence(t => t.GetDeploymentConfigInfoAsync()) .ReturnsAsync(ValidConfigInfo1) .ReturnsAsync(DeploymentConfigInfo.Empty) .ThrowsAsync(new InvalidOperationException()) .ReturnsAsync(ValidConfigInfo1) .ReturnsAsync(DeploymentConfigInfo.Empty) .ThrowsAsync(new InvalidOperationException()); ISerde <DeploymentConfigInfo> serde = this.GetSerde(); DeploymentConfigInfo config1; using (IConfigSource configSource = new FileBackupConfigSource(this.tempFileName, underlying.Object, serde, NullEncryptionProvider.Instance)) { config1 = await configSource.GetDeploymentConfigInfoAsync(); Assert.NotNull(config1); Assert.True(File.Exists(this.tempFileName)); string backupJson = await DiskFile.ReadAllAsync(this.tempFileName); string returnedJson = serde.Serialize(config1); Assert.Equal(backupJson, returnedJson, ignoreCase: true); File.Delete(this.tempFileName); DeploymentConfigInfo config2 = await configSource.GetDeploymentConfigInfoAsync(); Assert.NotNull(config2); Assert.Equal(serde.Serialize(config1), serde.Serialize(config2)); DeploymentConfigInfo config3 = await configSource.GetDeploymentConfigInfoAsync(); Assert.NotNull(config3); Assert.Equal(serde.Serialize(config1), serde.Serialize(config3)); } using (IConfigSource configSource = new FileBackupConfigSource(this.tempFileName, underlying.Object, serde, NullEncryptionProvider.Instance)) { config1 = await configSource.GetDeploymentConfigInfoAsync(); Assert.NotNull(config1); Assert.True(File.Exists(this.tempFileName)); string backupJson = await DiskFile.ReadAllAsync(this.tempFileName); string returnedJson = serde.Serialize(config1); Assert.Equal(backupJson, returnedJson, ignoreCase: true); } using (IConfigSource configSource = new FileBackupConfigSource(this.tempFileName, underlying.Object, serde, NullEncryptionProvider.Instance)) { DeploymentConfigInfo config5 = await configSource.GetDeploymentConfigInfoAsync(); Assert.NotNull(config5); Assert.Equal(serde.Serialize(config1), serde.Serialize(config5)); } using (IConfigSource configSource = new FileBackupConfigSource(this.tempFileName, underlying.Object, serde, NullEncryptionProvider.Instance)) { DeploymentConfigInfo config5 = await configSource.GetDeploymentConfigInfoAsync(); Assert.NotNull(config5); Assert.Equal(serde.Serialize(config1), serde.Serialize(config5)); } File.Delete(this.tempFileName); using (IConfigSource configSource = new FileBackupConfigSource(this.tempFileName, underlying.Object, serde, NullEncryptionProvider.Instance)) { DeploymentConfigInfo config6 = await configSource.GetDeploymentConfigInfoAsync(); Assert.NotNull(config6); Assert.Equal(config6, DeploymentConfigInfo.Empty); } }