Ejemplo n.º 1
0
        public void GetSSHKeyReturnsPublicKeyIfItExists(bool ensurePublicKey)
        {
            // Arrange
            string sshPath   = @"x:\path\.ssh";
            string publicKey = "this-is-my-public-key";
            var    fileBase  = new Mock <FileBase>(MockBehavior.Strict);

            fileBase.Setup(s => s.Exists(sshPath + "\\id_rsa.pub"))
            .Returns(true);
            fileBase.Setup(s => s.ReadAllText(sshPath + "\\id_rsa.pub"))
            .Returns(publicKey);

            var fileSystem = new Mock <IFileSystem>(MockBehavior.Strict);

            fileSystem.SetupGet(f => f.File).Returns(fileBase.Object);
            FileSystemHelpers.Instance = fileSystem.Object;

            var environment = new Mock <IEnvironment>();

            environment.SetupGet(e => e.SSHKeyPath).Returns(sshPath);

            var sshKeyManager = new SSHKeyManager(environment.Object, traceFactory: null, deploymentSettingManager: Mock.Of <IDeploymentSettingsManager>());

            // Act
            var actual = sshKeyManager.GetPublicKey(ensurePublicKey);

            // Assert
            Assert.Equal(publicKey, actual);
        }
Ejemplo n.º 2
0
        public void GetSSHKeyNoOpsIfPublicKeyPairIsNotFoundAndEnsurePublicKeyIsNotSet()
        {
            // Arrange
            string sshPath  = @"x:\path\.ssh";
            var    fileBase = new Mock <FileBase>(MockBehavior.Strict);

            fileBase.Setup(s => s.Exists(It.IsAny <string>()))
            .Returns(false);

            var fileSystem = new Mock <IFileSystem>(MockBehavior.Strict);

            fileSystem.SetupGet(f => f.File).Returns(fileBase.Object);
            FileSystemHelpers.Instance = fileSystem.Object;

            var environment = new Mock <IEnvironment>();

            environment.SetupGet(e => e.SSHKeyPath).Returns(sshPath);

            var sshKeyManager = new SSHKeyManager(environment.Object, traceFactory: null, deploymentSettingManager: Mock.Of <IDeploymentSettingsManager>());

            // Act
            var actual = sshKeyManager.GetPublicKey(ensurePublicKey: false);

            // Assert
            fileBase.Verify();
            Assert.Null(actual);
        }
Ejemplo n.º 3
0
        public void GetSSHKeyCreatesKeyIfPublicAndPrivateKeyDoesNotAlreadyExistAndEnsurePublicKeyIsSet()
        {
            // Arrange
            string sshPath   = @"x:\path\.ssh";
            string keyOnDisk = null;
            var    fileBase  = new Mock <FileBase>(MockBehavior.Strict);

            fileBase.Setup(s => s.Exists(It.IsAny <string>()))
            .Returns(false);
            fileBase.Setup(s => s.WriteAllText(sshPath + "\\id_rsa.pub", It.IsAny <string>()))
            .Callback((string name, string value) => { keyOnDisk = value; })
            .Verifiable();
            fileBase.Setup(s => s.WriteAllText(sshPath + "\\id_rsa", It.IsAny <string>()))
            .Verifiable();
            fileBase.Setup(s => s.WriteAllText(sshPath + @"\config", "HOST *\r\n  StrictHostKeyChecking no"))
            .Verifiable();

            var fileSystem = new Mock <IFileSystem>(MockBehavior.Strict);

            fileSystem.SetupGet(f => f.File).Returns(fileBase.Object);
            FileSystemHelpers.Instance = fileSystem.Object;

            var environment = new Mock <IEnvironment>();

            environment.SetupGet(e => e.SSHKeyPath).Returns(sshPath);

            var sshKeyManager = new SSHKeyManager(environment.Object, traceFactory: null, deploymentSettingManager: Mock.Of <IDeploymentSettingsManager>());

            // Act
            var actual = sshKeyManager.GetPublicKey(ensurePublicKey: true);

            // Assert
            fileBase.Verify();
            Assert.Equal(keyOnDisk, actual);
        }
Ejemplo n.º 4
0
        public void GetSSHKeyReturnsExistingKeyIfPresentOnDisk(bool ensurePublicKey)
        {
            // Arrange
            string sshPath  = @"x:\path\.ssh";
            string expected = "my-public-key";
            var    fileBase = new Mock <FileBase>(MockBehavior.Strict);

            fileBase.Setup(s => s.Exists(sshPath + "\\id_rsa.pub")).Returns(true);
            fileBase.Setup(s => s.ReadAllText(sshPath + "\\id_rsa.pub")).Returns(expected);

            var fileSystem = new Mock <IFileSystem>(MockBehavior.Strict);

            fileSystem.SetupGet(f => f.File).Returns(fileBase.Object);
            FileSystemHelpers.Instance = fileSystem.Object;

            var environment = new Mock <IEnvironment>();

            environment.SetupGet(e => e.SSHKeyPath).Returns(sshPath);

            var sshKeyManager = new SSHKeyManager(environment.Object, traceFactory: null);

            // Act
            var actual = sshKeyManager.GetPublicKey(ensurePublicKey);

            // Assert
            Assert.Equal(expected, actual);
        }
        public void GetSSHKeyReturnsPublicKeyIfItExists(bool ensurePublicKey)
        {
            // Arrange
            string sshPath = @"x:\path\.ssh";
            string publicKey = "this-is-my-public-key";
            var fileBase = new Mock<FileBase>(MockBehavior.Strict);
            fileBase.Setup(s => s.Exists(sshPath + "\\id_rsa.pub"))
                    .Returns(true);
            fileBase.Setup(s => s.ReadAllText(sshPath + "\\id_rsa.pub"))
                    .Returns(publicKey);

            var fileSystem = new Mock<IFileSystem>(MockBehavior.Strict);
            fileSystem.SetupGet(f => f.File).Returns(fileBase.Object);

            var environment = new Mock<IEnvironment>();
            environment.SetupGet(e => e.SSHKeyPath).Returns(sshPath);

            var sshKeyManager = new SSHKeyManager(environment.Object, fileSystem.Object, traceFactory: null);

            // Act 
            var actual = sshKeyManager.GetPublicKey(ensurePublicKey);

            // Assert
            Assert.Equal(publicKey, actual);
        }
        public void GetSSHKeyCreatesKeyIfPublicAndPrivateKeyDoesNotAlreadyExistAndEnsurePublicKeyIsSet()
        {
            // Arrange
            string sshPath = @"x:\path\.ssh";
            string keyOnDisk = null;
            var fileBase = new Mock<FileBase>(MockBehavior.Strict);
            fileBase.Setup(s => s.Exists(It.IsAny<string>()))
                    .Returns(false);
            fileBase.Setup(s => s.WriteAllText(sshPath + "\\id_rsa.pub", It.IsAny<string>()))
                   .Callback((string name, string value) => { keyOnDisk = value; })
                   .Verifiable();
            fileBase.Setup(s => s.WriteAllText(sshPath + "\\id_rsa", It.IsAny<string>()))
                    .Verifiable();
            fileBase.Setup(s => s.WriteAllText(sshPath + @"\config", "HOST *\r\n  StrictHostKeyChecking no"))
                    .Verifiable();

            var fileSystem = new Mock<IFileSystem>(MockBehavior.Strict);
            fileSystem.SetupGet(f => f.File).Returns(fileBase.Object);

            var environment = new Mock<IEnvironment>();
            environment.SetupGet(e => e.SSHKeyPath).Returns(sshPath);

            var sshKeyManager = new SSHKeyManager(environment.Object, fileSystem.Object, traceFactory: null);

            // Act 
            var actual = sshKeyManager.GetPublicKey(ensurePublicKey: true);

            // Assert
            fileBase.Verify();
            Assert.Equal(keyOnDisk, actual);
        }
        public void GetSSHKeyNoOpsIfPublicKeyPairIsNotFoundAndEnsurePublicKeyIsNotSet()
        {
            // Arrange
            string sshPath = @"x:\path\.ssh";
            var fileBase = new Mock<FileBase>(MockBehavior.Strict);
            fileBase.Setup(s => s.Exists(It.IsAny<string>()))
                    .Returns(false);

            var fileSystem = new Mock<IFileSystem>(MockBehavior.Strict);
            fileSystem.SetupGet(f => f.File).Returns(fileBase.Object);

            var environment = new Mock<IEnvironment>();
            environment.SetupGet(e => e.SSHKeyPath).Returns(sshPath);

            var sshKeyManager = new SSHKeyManager(environment.Object, fileSystem.Object, traceFactory: null);

            // Act 
            var actual = sshKeyManager.GetPublicKey(ensurePublicKey: false);

            // Assert
            fileBase.Verify();
            Assert.Null(actual);
        }
Ejemplo n.º 8
0
        public void GetSSHKeyReturnsExistingKeyIfPresentOnDisk(bool ensurePublicKey)
        {
            // Arrange
            string sshPath = @"x:\path\.ssh";
            string expected = "my-public-key";
            var fileBase = new Mock<FileBase>(MockBehavior.Strict);
            fileBase.Setup(s => s.Exists(sshPath + "\\id_rsa.pub")).Returns(true);
            fileBase.Setup(s => s.ReadAllText(sshPath + "\\id_rsa.pub")).Returns(expected);

            var fileSystem = new Mock<IFileSystem>(MockBehavior.Strict);
            fileSystem.SetupGet(f => f.File).Returns(fileBase.Object);
            FileSystemHelpers.Instance = fileSystem.Object;

            var environment = new Mock<IEnvironment>();
            environment.SetupGet(e => e.SSHKeyPath).Returns(sshPath);

            var sshKeyManager = new SSHKeyManager(environment.Object, traceFactory: null);

            // Act 
            var actual = sshKeyManager.GetPublicKey(ensurePublicKey);

            // Assert
            Assert.Equal(expected, actual);
        }