예제 #1
0
    def test_generate_checksums(self):
        """
        generate_checksums should send commands to the ssh client
        to generate an sha256sum for the passed in archived artifact.
        """
        # a project with a build and an archived artifact
        project = ProjectFactory.create()
        dependency = DependencyFactory.create()
        ProjectDependency.objects.create(project=project,
                                         dependency=dependency)
        projectbuild = build_project(project, queue_build=False)
        build = BuildFactory.create(job=dependency.job,
                                    build_id=projectbuild.build_key)
        projectbuild_dependency = ProjectBuildDependency.objects.create(
            build=build, projectbuild=projectbuild, dependency=dependency)
        artifact = ArtifactFactory.create(build=build,
                                          filename="artifact_filename")
        archived_artifact = ArchiveArtifact.objects.create(
            build=build,
            archive=self.archive,
            artifact=artifact,
            archived_path="/srv/builds/200101.01/artifact_filename",
            projectbuild_dependency=projectbuild_dependency)

        transport = SshTransport(self.archive)

        with mock.patch.object(transport, "_run_command") as mock_run:
            transport.generate_checksums(archived_artifact)

        mock_run.assert_called_once_with(
            "cd `dirname /var/tmp/srv/builds/200101.01/artifact_filename` "
            "&& sha256sum artifact_filename >> SHA256SUMS")
예제 #2
0
    def test_get_ssh_clients(self):
        """
        _get_ssh_clients should return an SSHClient and SFTPClient configured
        to talk to the archive"s credentials.
        """
        with mock.patch.object(self.archive.ssh_credentials,
                               "get_pkey",
                               return_value="KEY"):  # noqa
            with mock.patch("archives.transports.SSHClient") as mock_client:
                mock_client.return_value.get_transport.return_value = "MockTransport"  # noqa
                with mock.patch("archives.transports.SFTPClient") as mock_sftp:
                    with mock.patch("archives.transports.WarningPolicy"
                                    ) as mock_hostpolicy:  # noqa
                        mock_hostpolicy.return_value = "MockWarningPolicy"
                        transport = SshTransport(self.archive)
                        transport._get_ssh_clients()

        mock_client.return_value.assert_has_calls([
            mock.call.set_missing_host_key_policy("MockWarningPolicy"),
            mock.call.connect("archive.example.com",
                              username="******",
                              pkey="KEY"),
            mock.call.get_transport()
        ])
        mock_sftp.from_transport.assert_called_once_with("MockTransport")
예제 #3
0
    def test_generate_checksums(self):
        """
        generate_checksums should send commands to the ssh client
        to generate an sha256sum for the passed in archived artifact.
        """
        # a project with a build and an archived artifact
        project = ProjectFactory.create()
        dependency = DependencyFactory.create()
        ProjectDependency.objects.create(
            project=project, dependency=dependency)
        projectbuild = build_project(project, queue_build=False)
        build = BuildFactory.create(
            job=dependency.job, build_id=projectbuild.build_key)
        projectbuild_dependency = ProjectBuildDependency.objects.create(
            build=build, projectbuild=projectbuild, dependency=dependency)
        artifact = ArtifactFactory.create(
            build=build, filename="artifact_filename")
        archived_artifact = ArchiveArtifact.objects.create(
            build=build, archive=self.archive, artifact=artifact,
            archived_path="/srv/builds/200101.01/artifact_filename",
            projectbuild_dependency=projectbuild_dependency)

        transport = SshTransport(self.archive)

        with mock.patch.object(transport, "_run_command") as mock_run:
            transport.generate_checksums(archived_artifact)

        mock_run.assert_called_once_with(
            "cd `dirname /var/tmp/srv/builds/200101.01/artifact_filename` "
            "&& sha256sum artifact_filename >> SHA256SUMS")
예제 #4
0
    def test_get_ssh_clients(self):
        """
        _get_ssh_clients should return an SSHClient and SFTPClient configured to
        talk to the archive"s credentials.
        """
        with mock.patch.object(self.archive.ssh_credentials, "get_pkey", return_value="KEY"):
            with mock.patch("archives.transports.SSHClient") as mock_client:
                mock_client.return_value.get_transport.return_value = "MockTransport"
                with mock.patch("archives.transports.SFTPClient") as mock_sftp:
                    with mock.patch("archives.transports.WarningPolicy") as mock_hostpolicy:  # noqa
                        mock_hostpolicy.return_value = "MockWarningPolicy"
                        transport = SshTransport(self.archive)
                        transport._get_ssh_clients()

        mock_client.return_value.assert_has_calls([
            mock.call.set_missing_host_key_policy("MockWarningPolicy"),
            mock.call.connect("archive.example.com", username="******", pkey="KEY"),
            mock.call.get_transport()])
        mock_sftp.from_transport.assert_called_once_with("MockTransport")
예제 #5
0
    def test_link_artifact_to_current(self):
        """
        Create a link in the parent directory for this artifact,
        pointing to the directory of the artifact, with the name "current".
        """
        mock_ssh = mock.Mock()
        mock_stdout = mock.Mock()
        mock_ssh.exec_command.return_value = None, mock_stdout, None
        mock_sftp = mock.Mock()

        transport = SshTransport(self.archive)
        with mock.patch.object(
                transport, "_get_ssh_clients",
                return_value=(mock_ssh, mock_sftp)):
            transport.start()
            current_dir = transport.link_to_current("/project/1234/temp.gz")
        mock_ssh.exec_command.assert_has_calls(
            [mock.call('cd "/var/tmp/project" && rm -f current '
                       '&& ln -sf "1234" current')])
        self.assertEqual("/var/tmp/project/current", current_dir)
        mock_ssh.close.assert_called_once()
예제 #6
0
    def test_link_artifact_to_current(self):
        """
        Create a link in the parent directory for this artifact,
        pointing to the directory of the artifact, with the name "current".
        """
        mock_ssh = mock.Mock()
        mock_stdout = mock.Mock()
        mock_ssh.exec_command.return_value = None, mock_stdout, None
        mock_sftp = mock.Mock()

        transport = SshTransport(self.archive)
        with mock.patch.object(transport,
                               "_get_ssh_clients",
                               return_value=(mock_ssh, mock_sftp)):
            transport.start()
            current_dir = transport.link_to_current("/project/1234/temp.gz")
        mock_ssh.exec_command.assert_has_calls([
            mock.call('cd "/var/tmp/project" && rm -f current '
                      '&& ln -sf "1234" current')
        ])
        self.assertEqual("/var/tmp/project/current", current_dir)
        mock_ssh.close.assert_called_once()
예제 #7
0
    def test_link_filename_to_filename(self):
        """
        archive_file should ensure that there's a directory relative to the
        base to hold the file, and then stream the file to the remote server.
        """
        mock_ssh = mock.Mock()
        mock_stdout = mock.Mock()
        mock_ssh.exec_command.return_value = None, mock_stdout, None
        mock_sftp = mock.Mock()

        transport = SshTransport(self.archive)
        with mock.patch.object(
                transport, "_get_ssh_clients",
                return_value=(mock_ssh, mock_sftp)):
            transport.start()
            transport.link_filename_to_filename(
                "/temp/temp.gz", "/temp2/temp.gz")
        mock_ssh.exec_command.assert_has_calls(
            [mock.call("mkdir -p `dirname /var/tmp/temp2/temp.gz`"),
             mock.call('ln "/var/tmp/temp/temp.gz" "/var/tmp/temp2/temp.gz"')])

        mock_ssh.close.assert_called_once()
예제 #8
0
    def test_archive_file(self):
        """
        archive_file should ensure that there's a directory relative to the base
        to hold the file, and then stream the file to the remote server.
        """
        mock_ssh = mock.Mock()
        mock_stdout = mock.Mock()
        mock_ssh.exec_command.return_value = None, mock_stdout, None
        mock_sftp = mock.Mock()
        fakefile = StringIO(u"This is the artifact")

        transport = SshTransport(self.archive)
        with mock.patch.object(
                transport, "_get_ssh_clients",
                return_value=(mock_ssh, mock_sftp)):
            transport.start()
            transport.archive_file(fakefile, "/temp/temp.gz")
        mock_ssh.exec_command.assert_called_once_with(
            "mkdir -p `dirname /var/tmp/temp/temp.gz`")

        mock_sftp.stream_file_to_remote.assert_called_once_with(
            fakefile, "/var/tmp/temp/temp.gz")

        mock_ssh.close.assert_called_once()
예제 #9
0
    def test_link_filename_to_filename(self):
        """
        archive_file should ensure that there's a directory relative to the
        base to hold the file, and then stream the file to the remote server.
        """
        mock_ssh = mock.Mock()
        mock_stdout = mock.Mock()
        mock_ssh.exec_command.return_value = None, mock_stdout, None
        mock_sftp = mock.Mock()

        transport = SshTransport(self.archive)
        with mock.patch.object(transport,
                               "_get_ssh_clients",
                               return_value=(mock_ssh, mock_sftp)):
            transport.start()
            transport.link_filename_to_filename("/temp/temp.gz",
                                                "/temp2/temp.gz")
        mock_ssh.exec_command.assert_has_calls([
            mock.call("mkdir -p `dirname /var/tmp/temp2/temp.gz`"),
            mock.call('ln "/var/tmp/temp/temp.gz" "/var/tmp/temp2/temp.gz"')
        ])

        mock_ssh.close.assert_called_once()
예제 #10
0
    def test_archive_file(self):
        """
        archive_file should ensure that there's a directory relative to the
        base to hold the file, and then stream the file to the remote server.
        """
        mock_ssh = mock.Mock()
        mock_stdout = mock.Mock()
        mock_ssh.exec_command.return_value = None, mock_stdout, None
        mock_sftp = mock.Mock()
        fakefile = StringIO(u"This is the artifact")

        transport = SshTransport(self.archive)
        with mock.patch.object(transport,
                               "_get_ssh_clients",
                               return_value=(mock_ssh, mock_sftp)):
            transport.start()
            transport.archive_file(fakefile, "/temp/temp.gz")
        mock_ssh.exec_command.assert_called_once_with(
            "mkdir -p `dirname /var/tmp/temp/temp.gz`")

        mock_sftp.stream_file_to_remote.assert_called_once_with(
            fakefile, "/var/tmp/temp/temp.gz")

        mock_ssh.close.assert_called_once()