From 1a28ce66d13f3b6625c61ec57448e6e9f2c28d3e Mon Sep 17 00:00:00 2001 From: Liam Mackie Date: Fri, 14 Feb 2025 08:12:04 +1000 Subject: [PATCH] PR Feedback --- ...etesAgentMigrateFromPreinstallationTest.cs | 136 ++++++++---------- ...edKubernetesDeploymentTargetCommandTest.cs | 2 +- ...talledKubernetesDeploymentTargetCommand.cs | 9 +- 3 files changed, 68 insertions(+), 79 deletions(-) diff --git a/source/Octopus.Tentacle.Kubernetes.Tests.Integration/KubernetesAgent/KubernetesAgentMigrateFromPreinstallationTest.cs b/source/Octopus.Tentacle.Kubernetes.Tests.Integration/KubernetesAgent/KubernetesAgentMigrateFromPreinstallationTest.cs index 2f0cacb6a..6ab668685 100644 --- a/source/Octopus.Tentacle.Kubernetes.Tests.Integration/KubernetesAgent/KubernetesAgentMigrateFromPreinstallationTest.cs +++ b/source/Octopus.Tentacle.Kubernetes.Tests.Integration/KubernetesAgent/KubernetesAgentMigrateFromPreinstallationTest.cs @@ -14,15 +14,37 @@ public class KubernetesAgentMigrateFromPreinstallationTest readonly ISystemLog systemLog = new SystemLog(); + const string SourceConfigMapName = "tentacle-config-pre"; + const string SourceSecretName = "tentacle-secret-pre"; + const string DestinationConfigMapName = "tentacle-config"; + const string DestinationSecretName = "tentacle-secret"; + MigratePreInstalledKubernetesDeploymentTargetCommand commandToRun; + KubernetesFileWrappedProvider kubernetesConfigClient; + string commandNamespace; + k8s.Kubernetes client; + string[] commandArguments; + + [SetUp] + public void Init() + { + kubernetesConfigClient = new KubernetesFileWrappedProvider(KubernetesTestsGlobalContext.Instance.KubeConfigPath); + commandToRun = new MigratePreInstalledKubernetesDeploymentTargetCommand(new Lazy(kubernetesConfigClient), systemLog, new LogFileOnlyLogger()); + commandNamespace = Guid.NewGuid().ToString("N"); + Environment.SetEnvironmentVariable(KubernetesConfig.NamespaceVariableName, commandNamespace); + client = new k8s.Kubernetes(kubernetesConfigClient.Get()); + commandArguments = [ + $"--source-config-map-name={SourceConfigMapName}", + $"--source-secret-name={SourceSecretName}", + $"--destination-config-map-name={DestinationConfigMapName}", + $"--destination-secret-name={DestinationSecretName}", + $"--namespace={commandNamespace}" + ]; + } + [Test] - public async Task MigrationFromPreinstallHookShouldCopyData() + public async Task MigrationFromPreinstallHook_ShouldCopyData() { //Arrange - var kubernetesConfigClient = new KubernetesFileWrappedProvider(KubernetesTestsGlobalContext.Instance.KubeConfigPath); - var commandNamespace = Guid.NewGuid().ToString("N"); - Environment.SetEnvironmentVariable(KubernetesConfig.NamespaceVariableName, commandNamespace); - var commandToRun = new MigratePreInstalledKubernetesDeploymentTargetCommand(new Lazy(kubernetesConfigClient), systemLog, new LogFileOnlyLogger()); - var client = new k8s.Kubernetes(kubernetesConfigClient.Get()); var validationKey = Guid.NewGuid().ToString("N"); var sourceConfigMapData = new Dictionary { @@ -31,57 +53,41 @@ public async Task MigrationFromPreinstallHookShouldCopyData() }; var sourceSecretData = new Dictionary { - {"machine-iv", "testData"}, - {"machine-key", validationKey} + {"validationKey", validationKey} }; - const string sourceConfigMapName = "tentacle-config-pre"; - const string sourceSecretName = "tentacle-secret-pre"; - const string destinationConfigMapName = "tentacle-config"; - const string destinationSecretName = "tentacle-secret"; - - string[] commandArguments = [ - $"--source-config-map-name={sourceConfigMapName}", - $"--source-secret-name={sourceSecretName}", - $"--destination-config-map-name={destinationConfigMapName}", - $"--destination-secret-name={destinationSecretName}", - $"--namespace={commandNamespace}" - ]; - - //Act - await CreateCommandNamespace(client, commandNamespace); + // Namespace + await CreateCommandNamespace(commandNamespace); - //Sources - await CreateConfigmap(client, sourceConfigMapName, commandNamespace, sourceConfigMapData); - await CreateSecret(client, sourceSecretName, commandNamespace, sourceSecretData); + // Sources + await CreateConfigmap(SourceConfigMapName, commandNamespace, sourceConfigMapData); + await CreateSecret(SourceSecretName, commandNamespace, sourceSecretData); // Targets - await CreateConfigmap(client, destinationConfigMapName, commandNamespace); - await CreateSecret(client, destinationSecretName, commandNamespace); + await CreateConfigmap(DestinationConfigMapName, commandNamespace); + await CreateSecret(DestinationSecretName, commandNamespace); + + + //Act commandToRun.Start(commandArguments, new NoninteractiveHost(), []); //Assert - var configMap = await client.CoreV1.ReadNamespacedConfigMapAsync(destinationConfigMapName, commandNamespace); - var secret = await client.CoreV1.ReadNamespacedSecretAsync(destinationSecretName, commandNamespace); + var configMap = await client.CoreV1.ReadNamespacedConfigMapAsync(DestinationConfigMapName, commandNamespace); + var secret = await client.CoreV1.ReadNamespacedSecretAsync(DestinationSecretName, commandNamespace); - configMap.Data.TryGetValue("validationKey", out var validationKeyFromKubernetes); - validationKeyFromKubernetes.Should().NotBeNull().And.Be(validationKey); + configMap.Data.TryGetValue("validationKey", out var validationKeyFromKubernetesConfigMap); + validationKeyFromKubernetesConfigMap.Should().Be(validationKey); - secret.Data.TryGetValue("machine-key", out var hostKeyFromKubernetes); - hostKeyFromKubernetes.Should().NotBeNull().And.Equal(Encoding.UTF8.GetBytes(validationKey)); + secret.Data.TryGetValue("validationKey", out var validationKeyFromKubernetesSecret); + validationKeyFromKubernetesSecret.Should().Equal(Encoding.UTF8.GetBytes(validationKey)); } [Test] - public async Task MigrationFromPreinstallHookShouldOnlyRunWhenNotRegistered() + public async Task MigrationFromPreinstallHook_ShouldNotRunWhenTentacleIsAlreadyRegistered() { //Arrange - var kubernetesConfigClient = new KubernetesFileWrappedProvider(KubernetesTestsGlobalContext.Instance.KubeConfigPath); - var commandNamespace = Guid.NewGuid().ToString("N"); - Environment.SetEnvironmentVariable(KubernetesConfig.NamespaceVariableName, commandNamespace); - var commandToRun = new MigratePreInstalledKubernetesDeploymentTargetCommand(new Lazy(kubernetesConfigClient), systemLog, new LogFileOnlyLogger()); - var client = new k8s.Kubernetes(kubernetesConfigClient.Get()); var validationKey = Guid.NewGuid().ToString("N"); var sourceConfigMapData = new Dictionary { @@ -90,8 +96,7 @@ public async Task MigrationFromPreinstallHookShouldOnlyRunWhenNotRegistered() }; var sourceSecretData = new Dictionary { - {"machine-iv", "testData"}, - {"machine-key", validationKey} + {"validationKey", "should-not-be-here"} }; var destinationConfigMapData = new Dictionary { @@ -100,51 +105,36 @@ public async Task MigrationFromPreinstallHookShouldOnlyRunWhenNotRegistered() }; var destinationSecretData = new Dictionary { - {"machine-iv", "testData"}, - {"machine-key", validationKey} + {"validationKey", validationKey} }; - - - const string sourceConfigMapName = "tentacle-config-pre"; - const string sourceSecretName = "tentacle-secret-pre"; - const string destinationConfigMapName = "tentacle-config"; - const string destinationSecretName = "tentacle-secret"; - - string[] commandArguments = [ - $"--source-config-map-name={sourceConfigMapName}", - $"--source-secret-name={sourceSecretName}", - $"--destination-config-map-name={destinationConfigMapName}", - $"--destination-secret-name={destinationSecretName}", - $"--namespace={commandNamespace}" - ]; - - //Act - await CreateCommandNamespace(client, commandNamespace); + // namespace + await CreateCommandNamespace(commandNamespace); //Sources - await CreateConfigmap(client, sourceConfigMapName, commandNamespace, sourceConfigMapData); - await CreateSecret(client, sourceSecretName, commandNamespace, sourceSecretData); - + await CreateConfigmap(SourceConfigMapName, commandNamespace, sourceConfigMapData); + await CreateSecret(SourceSecretName, commandNamespace, sourceSecretData); // Targets - await CreateConfigmap(client, destinationConfigMapName, commandNamespace, destinationConfigMapData); - await CreateSecret(client, destinationSecretName, commandNamespace, destinationSecretData); + await CreateConfigmap(DestinationConfigMapName, commandNamespace, destinationConfigMapData); + await CreateSecret(DestinationSecretName, commandNamespace, destinationSecretData); + + //Act commandToRun.Start(commandArguments, new NoninteractiveHost(), []); //Assert - var configMap = await client.CoreV1.ReadNamespacedConfigMapAsync(destinationConfigMapName, commandNamespace); - var secret = await client.CoreV1.ReadNamespacedSecretAsync(destinationSecretName, commandNamespace); + var configMap = await client.CoreV1.ReadNamespacedConfigMapAsync(DestinationConfigMapName, commandNamespace); + var secret = await client.CoreV1.ReadNamespacedSecretAsync(DestinationSecretName, commandNamespace); configMap.Data.TryGetValue("validationKey", out var validationKeyFromKubernetes); - validationKeyFromKubernetes.Should().NotBeNull().And.Be(validationKey); + validationKeyFromKubernetes.Should().Be(validationKey); secret.Data.TryGetValue("machine-key", out var hostKeyFromKubernetes); - hostKeyFromKubernetes.Should().NotBeNull().And.Equal(Encoding.UTF8.GetBytes(validationKey)); + hostKeyFromKubernetes.Should().Equal(Encoding.UTF8.GetBytes(validationKey)); } - async Task CreateCommandNamespace(k8s.Kubernetes client, string name) + async Task CreateCommandNamespace(string name) { await client.CoreV1.CreateNamespaceAsync(new V1Namespace { @@ -155,7 +145,7 @@ await client.CoreV1.CreateNamespaceAsync(new V1Namespace }); } - async Task CreateConfigmap(k8s.Kubernetes client, string name, string ns, Dictionary? data = null) + async Task CreateConfigmap(string name, string ns, Dictionary? data = null) { return await client.CoreV1.CreateNamespacedConfigMapAsync(new V1ConfigMap { @@ -167,7 +157,7 @@ async Task CreateConfigmap(k8s.Kubernetes client, string name, stri }, ns); } - async Task CreateSecret(k8s.Kubernetes client, string name, string ns, Dictionary? data = null) + async Task CreateSecret(string name, string ns, Dictionary? data = null) { return await client.CoreV1.CreateNamespacedSecretAsync(new V1Secret { diff --git a/source/Octopus.Tentacle.Tests/Commands/MigratePreInstalledKubernetesDeploymentTargetCommandTest.cs b/source/Octopus.Tentacle.Tests/Commands/MigratePreInstalledKubernetesDeploymentTargetCommandTest.cs index 4b95e77b7..eced28820 100644 --- a/source/Octopus.Tentacle.Tests/Commands/MigratePreInstalledKubernetesDeploymentTargetCommandTest.cs +++ b/source/Octopus.Tentacle.Tests/Commands/MigratePreInstalledKubernetesDeploymentTargetCommandTest.cs @@ -12,7 +12,7 @@ namespace Octopus.Tentacle.Tests.Commands public class MigratePreInstalledKubernetesDeploymentTargetCommandFixture { [Test] - public void ShouldNotMigrate_ifNoSource() + public void ShouldNotMigrate_IfNoSource() { var targetConfigMap = Substitute.For(); var targetSecret = Substitute.For(); diff --git a/source/Octopus.Tentacle/Commands/MigratePreInstalledKubernetesDeploymentTargetCommand.cs b/source/Octopus.Tentacle/Commands/MigratePreInstalledKubernetesDeploymentTargetCommand.cs index f8611f179..ae195846c 100644 --- a/source/Octopus.Tentacle/Commands/MigratePreInstalledKubernetesDeploymentTargetCommand.cs +++ b/source/Octopus.Tentacle/Commands/MigratePreInstalledKubernetesDeploymentTargetCommand.cs @@ -34,7 +34,7 @@ public MigratePreInstalledKubernetesDeploymentTargetCommand(Lazy(Func kubernetesFunc) where T : class + static T? TryGetCoreV1Object(Func kubernetesFunc) where T : class { try {