def test_project_build_detail_view_with_archived_artifacts(self): """ If we have archived artifacts for this build, we should provide the list of archived items in the response context. """ dependency = DependencyFactory.create() ProjectDependency.objects.create( project=self.project, dependency=dependency) projectbuild = build_project(self.project, queue_build=False) build = BuildFactory.create( job=dependency.job, build_id=projectbuild.build_key) artifact = ArtifactFactory.create(build=build, filename="file1.gz") process_build_dependencies(build.pk) archive = ArchiveFactory.create(policy="cdimage", default=True) items = [x for x in archive.add_build(build)[artifact] if x.projectbuild_dependency] url = reverse( "project_projectbuild_detail", kwargs={"project_pk": self.project.pk, "build_pk": projectbuild.pk}) response = self.app.get(url, user="******") self.assertEqual(items, list(response.context["archived_items"]))
def test_project_build_status_when_all_dependencies_have_builds(self): """ When we have FINALIZED builds for all the dependencies, the projectbuild state should be FINALIZED. """ dependency1 = DependencyFactory.create() ProjectDependency.objects.create( project=self.project, dependency=dependency1) dependency2 = DependencyFactory.create() ProjectDependency.objects.create( project=self.project, dependency=dependency2) from projects.helpers import build_project projectbuild = build_project(self.project, queue_build=False) for job in [dependency1.job, dependency2.job]: build = BuildFactory.create( job=job, build_id=projectbuild.build_key, phase=Build.FINALIZED) process_build_dependencies(build.pk) projectbuild = ProjectBuild.objects.get(pk=projectbuild.pk) self.assertEqual("SUCCESS", projectbuild.status) self.assertEqual(Build.FINALIZED, projectbuild.phase) self.assertIsNotNone(projectbuild.ended_at)
def test_process_build_artifacts(self): """ process_build_artifacts is chained from the Jenkins postbuild processing, it should arrange for the artifacts for the provided build to be archived in the default archive. """ 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) ArtifactFactory.create( build=build, filename="testing/testing.txt") # We need to ensure that the artifacts are all connected up. process_build_dependencies(build.pk) archive = ArchiveFactory.create( transport="local", basedir=self.basedir, default=True, policy="cdimage") with mock.patch("archives.transports.urllib2") as urllib2_mock: urllib2_mock.urlopen.side_effect = lambda x: StringIO( u"Artifact from Jenkins") process_build_artifacts(build.pk) [item1, item2] = list(archive.get_archived_artifacts_for_build(build)) filename = os.path.join(self.basedir, item1.archived_path) self.assertEqual(file(filename).read(), "Artifact from Jenkins") filename = os.path.join(self.basedir, item2.archived_path) self.assertEqual(file(filename).read(), "Artifact from Jenkins")
def test_build_with_several_projectbuild_dependencies(self): """ A build of dependency that's autotracked by several projects should trigger creation of all projectbuilds correctly. """ project1, dependency = self.create_dependencies() project2 = ProjectFactory.create() ProjectDependency.objects.create(project=project2, dependency=dependency) projectbuild = build_project(project1, queue_build=False) projectbuild.phase == Build.FINALIZED projectbuild.save() build = BuildFactory.create(job=dependency.job, build_id=projectbuild.build_key) process_build_dependencies(build.pk) self.assertEqual([dependency, dependency], sorted([ b.dependency for b in ProjectBuildDependency.objects.all() ])) self.assertEqual( [build, build], sorted([b.build for b in ProjectBuildDependency.objects.all()]))
def test_build_with_several_projectbuild_dependencies(self): """ A build of dependency that's autotracked by several projects should trigger creation of all projectbuilds correctly. """ project1, dependency = self.create_dependencies() project2 = ProjectFactory.create() ProjectDependency.objects.create(project=project2, dependency=dependency) projectbuild = build_project(project1, queue_build=False) build = BuildFactory.create( job=dependency.job, build_id=projectbuild.build_key) process_build_dependencies(build.pk) self.assertEqual( [dependency, dependency], sorted([b.dependency for b in ProjectBuildDependency.objects.all()])) self.assertEqual( [build, build], sorted([b.build for b in ProjectBuildDependency.objects.all()]))
def test_link_artifact_in_archive(self): """ The link_artifact_in_archive task should use the transport to link the specified artifacts. """ project = ProjectFactory.create() dependency = DependencyFactory.create() ProjectDependency.objects.create( project=project, dependency=dependency) build = BuildFactory.create(job=dependency.job, phase=Build.FINALIZED) artifact = ArtifactFactory.create( build=build, filename="testing/testing.txt") # We need to ensure that the artifacts are all connected up. process_build_dependencies(build.pk) archive = ArchiveFactory.create( transport="local", basedir=self.basedir, default=True) [item1, item2] = archive.add_build(artifact.build)[artifact] item1.archived_size = 1000 item1.save() transport = mock.Mock(spec=LocalTransport) with mock.patch.object( Archive, "get_transport", return_value=transport): link_artifact_in_archive(item1.pk, item2.pk) transport.link_filename_to_filename.assert_called_once_with( item1.archived_path, item2.archived_path) transport.link_to_current.assert_called_once_with(item2.archived_path) item1 = ArchiveArtifact.objects.get(pk=item1.pk) self.assertEqual(1000, item1.archived_size)
def test_project_build_status_when_all_dependencies_have_builds(self): """ When we have FINALIZED builds for all the dependencies, the projectbuild state should be FINALIZED. """ dependency1 = DependencyFactory.create() ProjectDependency.objects.create(project=self.project, dependency=dependency1) dependency2 = DependencyFactory.create() ProjectDependency.objects.create(project=self.project, dependency=dependency2) from projects.helpers import build_project projectbuild = build_project(self.project, queue_build=False) for job in [dependency1.job, dependency2.job]: build = BuildFactory.create(job=job, build_id=projectbuild.build_key, phase=Build.FINALIZED) process_build_dependencies(build.pk) projectbuild = ProjectBuild.objects.get(pk=projectbuild.pk) self.assertEqual("SUCCESS", projectbuild.status) self.assertEqual(Build.FINALIZED, projectbuild.phase) self.assertIsNotNone(projectbuild.ended_at)
def test_projectbuild_updates_when_build_created(self): """ If we have a ProjectBuild with a dependency, which is associated with a job, and we get a build from that job, then if the build_id is correct, we should associate the build dependency with that build. """ dependency1 = DependencyFactory.create() ProjectDependency.objects.create(project=self.project, dependency=dependency1) dependency2 = DependencyFactory.create() ProjectDependency.objects.create(project=self.project, dependency=dependency2) projectbuild = build_project(self.project, queue_build=False) build1 = BuildFactory.create(job=dependency1.job, build_id=projectbuild.build_key) process_build_dependencies(build1.pk) build_dependencies = ProjectBuildDependency.objects.filter( projectbuild=projectbuild) self.assertEqual(2, build_dependencies.count()) dependency = build_dependencies.get(dependency=dependency1) self.assertEqual(build1, dependency.build) dependency = build_dependencies.get(dependency=dependency2) self.assertIsNone(dependency.build)
def test_auto_track_dependency_triggers_project_build_creation(self): """ If we record a build of a project dependency that is auto-tracked, then this should trigger the creation of a new ProjectBuild for that project. """ dependency1 = DependencyFactory.create() ProjectDependency.objects.create(project=self.project, dependency=dependency1) dependency2 = DependencyFactory.create() existing_build = BuildFactory.create(job=dependency2.job, phase="FINISHED") ProjectDependency.objects.create(project=self.project, dependency=dependency2, current_build=existing_build) self.assertEqual(0, ProjectBuild.objects.filter(project=self.project).count()) build = BuildFactory.create(job=dependency1.job, phase="FINISHED") process_build_dependencies(build.pk) self.assertEqual(1, ProjectBuild.objects.filter(project=self.project).count()) projectbuild = ProjectBuild.objects.get(project=self.project) self.assertEqual(2, ProjectBuildDependency.objects.filter(projectbuild=projectbuild).count()) build_dependency1 = ProjectBuildDependency.objects.get(projectbuild=projectbuild, dependency=dependency1) self.assertEqual(build, build_dependency1.build) build_dependency2 = ProjectBuildDependency.objects.get(projectbuild=projectbuild, dependency=dependency2) self.assertEqual(existing_build, build_dependency2.build)
def test_link_artifact_in_archive(self): """ The link_artifact_in_archive task should use the transport to link the specified artifacts. """ project = ProjectFactory.create() dependency = DependencyFactory.create() ProjectDependency.objects.create(project=project, dependency=dependency) build = BuildFactory.create(job=dependency.job, phase=Build.FINALIZED) artifact = ArtifactFactory.create(build=build, filename="testing/testing.txt") # We need to ensure that the artifacts are all connected up. process_build_dependencies(build.pk) archive = ArchiveFactory.create(transport="local", basedir=self.basedir, default=True) [item1, item2] = archive.add_build(artifact.build)[artifact] item1.archived_size = 1000 item1.save() transport = mock.Mock(spec=LocalTransport) with mock.patch.object(Archive, "get_transport", return_value=transport): link_artifact_in_archive(item1.pk, item2.pk) transport.link_filename_to_filename.assert_called_once_with( item1.archived_path, item2.archived_path) transport.link_to_current.assert_called_once_with(item2.archived_path) item1 = ArchiveArtifact.objects.get(pk=item1.pk) self.assertEqual(1000, item1.archived_size)
def test_projectbuild_updates_when_build_created(self): """ If we have a ProjectBuild with a dependency, which is associated with a job, and we get a build from that job, then if the build_id is correct, we should associate the build dependency with that build. """ dependency1 = DependencyFactory.create() ProjectDependency.objects.create(project=self.project, dependency=dependency1) dependency2 = DependencyFactory.create() ProjectDependency.objects.create(project=self.project, dependency=dependency2) projectbuild = build_project(self.project, queue_build=False) build1 = BuildFactory.create(job=dependency1.job, build_id=projectbuild.build_key) process_build_dependencies(build1.pk) build_dependencies = ProjectBuildDependency.objects.filter(projectbuild=projectbuild) self.assertEqual(2, build_dependencies.count()) dependency = build_dependencies.get(dependency=dependency1) self.assertEqual(build1, dependency.build) dependency = build_dependencies.get(dependency=dependency2) self.assertIsNone(dependency.build)
def test_process_build_artifacts(self): """ process_build_artifacts is chained from the Jenkins postbuild processing, it should arrange for the artifacts for the provided build to be archived in the default archive. """ 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) ArtifactFactory.create(build=build, filename="testing/testing.txt") # We need to ensure that the artifacts are all connected up. process_build_dependencies(build.pk) archive = ArchiveFactory.create(transport="local", basedir=self.basedir, default=True, policy="cdimage") with mock.patch("archives.transports.urllib2") as urllib2_mock: urllib2_mock.urlopen.side_effect = lambda x: StringIO( u"Artifact from Jenkins") process_build_artifacts(build.pk) [item1, item2] = list(archive.get_archived_artifacts_for_build(build)) filename = os.path.join(self.basedir, item1.archived_path) self.assertEqual(file(filename).read(), "Artifact from Jenkins") filename = os.path.join(self.basedir, item2.archived_path) self.assertEqual(file(filename).read(), "Artifact from Jenkins")
def test_archive_artifact_from_finalized_projectbuild(self): """ If the build is complete, and the item being archived is in a FINALIZED ProjectBuild, it should use the transport to set the current directory correctly. """ project = ProjectFactory.create() dependency1 = DependencyFactory.create() ProjectDependency.objects.create(project=project, dependency=dependency1) dependency2 = DependencyFactory.create() ProjectDependency.objects.create(project=project, dependency=dependency2) projectbuild = build_project(project, queue_build=False) build1 = BuildFactory.create(job=dependency1.job, build_id=projectbuild.build_key, phase=Build.FINALIZED) build2 = BuildFactory.create(job=dependency2.job, build_id=projectbuild.build_key, phase=Build.FINALIZED) artifact = ArtifactFactory.create(build=build2, filename="testing/testing.txt") # We need to ensure that the artifacts are all connected up. process_build_dependencies(build1.pk) process_build_dependencies(build2.pk) archive = ArchiveFactory.create(transport="local", basedir=self.basedir, default=True) [item1, item2] = archive.add_build(artifact.build)[artifact] transport = LoggingTransport(archive) with mock.patch.object(Archive, "get_transport", return_value=transport): link_artifact_in_archive(item1.pk, item2.pk) # Both builds are complete, we expect this to be made the current # build. self.assertEqual([ "START", "Link %s to %s" % (item1.archived_path, item2.archived_path), "Make %s current" % item2.archived_path, "END" ], transport.log)
def test_archive_artifact_from_finalized_projectbuild(self): """ If the build is complete, and the item being archived is in a FINALIZED ProjectBuild, it should use the transport to set the current directory correctly. """ project = ProjectFactory.create() dependency1 = DependencyFactory.create() ProjectDependency.objects.create( project=project, dependency=dependency1) dependency2 = DependencyFactory.create() ProjectDependency.objects.create( project=project, dependency=dependency2) projectbuild = build_project(project, queue_build=False) build1 = BuildFactory.create( job=dependency1.job, build_id=projectbuild.build_key, phase=Build.FINALIZED) build2 = BuildFactory.create( job=dependency2.job, build_id=projectbuild.build_key, phase=Build.FINALIZED) artifact = ArtifactFactory.create( build=build2, filename="testing/testing.txt") # We need to ensure that the artifacts are all connected up. process_build_dependencies(build1.pk) process_build_dependencies(build2.pk) archive = ArchiveFactory.create( transport="local", basedir=self.basedir, default=True) [item1, item2] = archive.add_build(artifact.build)[artifact] transport = LoggingTransport(archive) with mock.patch.object( Archive, "get_transport", return_value=transport): link_artifact_in_archive(item1.pk, item2.pk) # Both builds are complete, we expect this to be made the current # build. self.assertEqual( ["START", "Link %s to %s" % (item1.archived_path, item2.archived_path), "Make %s current" % item2.archived_path, "END"], transport.log)
def test_archive_artifact_from_non_finalized_projectbuild(self): """ If the build is complete, and the item being archived is in a FINALIZED ProjectBuild, it should use the transport to set the current directory correctly. """ project = ProjectFactory.create() dependency1 = DependencyFactory.create() ProjectDependency.objects.create(project=project, dependency=dependency1) dependency2 = DependencyFactory.create() ProjectDependency.objects.create(project=project, dependency=dependency2) projectbuild = build_project(project, queue_build=False) build = BuildFactory.create(job=dependency1.job, build_id=projectbuild.build_key, phase=Build.FINALIZED) ProjectBuildDependency.objects.create(build=build, projectbuild=projectbuild, dependency=dependency1) artifact = ArtifactFactory.create(build=build, filename="testing/testing.txt") # We need to ensure that the artifacts are all connected up. process_build_dependencies(build.pk) archive = ArchiveFactory.create(transport="local", basedir=self.basedir, default=True) item = [ x for x in archive.add_build(artifact.build)[artifact] if x.projectbuild_dependency ][0] transport = LoggingTransport(archive) with mock.patch.object(Archive, "get_transport", return_value=transport): archive_artifact_from_jenkins(item.pk) self.assertEqual([ "START", "%s -> %s root:testing" % (artifact.url, item.archived_path), "END" ], transport.log)
def test_process_build_artifacts_with_multiple_artifacts(self): """ All the artifacts should be individually linked. """ 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) ArtifactFactory.create( build=build, filename="testing/testing1.txt") ArtifactFactory.create( build=build, filename="testing/testing2.txt") # We need to ensure that the artifacts are all connected up. process_build_dependencies(build.pk) archive = ArchiveFactory.create( transport="local", basedir=self.basedir, default=True, policy="cdimage") with mock.patch("archives.transports.urllib2") as urllib2_mock: urllib2_mock.urlopen.side_effect = lambda x: StringIO( u"Artifact %s") with mock.patch( "archives.tasks.archive_artifact_from_jenkins" ) as archive_task: with mock.patch( "archives.tasks.link_artifact_in_archive" ) as link_task: process_build_artifacts(build.pk) [item1, item2, item3, item4] = list( archive.get_archived_artifacts_for_build(build).order_by( "artifact")) self.assertEqual( [mock.call(item4.pk), mock.call(item2.pk)], archive_task.si.call_args_list) self.assertEqual( [mock.call(item4.pk, item3.pk), mock.call(item2.pk, item1.pk)], link_task.si.call_args_list)
def test_get_current_artifacts(self): """ Project.get_current_artifacts returns the current set of artifacts for this project. """ project = ProjectFactory.create() job = JobFactory.create() dependency = DependencyFactory.create(job=job) ProjectDependency.objects.create( project=project, dependency=dependency) build1 = BuildFactory.create(job=job) build2 = BuildFactory.create(job=job) ArtifactFactory.create(build=build1) artifact2 = ArtifactFactory.create(build=build2) process_build_dependencies(build2.pk) self.assertEqual([artifact2], list(project.get_current_artifacts()))
def test_get_current_artifacts(self): """ Project.get_current_artifacts returns the current set of artifacts for this project. """ project = ProjectFactory.create() job = JobFactory.create() dependency = DependencyFactory.create(job=job) ProjectDependency.objects.create(project=project, dependency=dependency) build1 = BuildFactory.create(job=job) build2 = BuildFactory.create(job=job) ArtifactFactory.create(build=build1) artifact2 = ArtifactFactory.create(build=build2) process_build_dependencies(build2.pk) self.assertEqual([artifact2], list(project.get_current_artifacts()))
def test_process_build_artifacts_with_multiple_artifacts(self): """ All the artifacts should be individually linked. """ 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) ArtifactFactory.create(build=build, filename="testing/testing1.txt") ArtifactFactory.create(build=build, filename="testing/testing2.txt") # We need to ensure that the artifacts are all connected up. process_build_dependencies(build.pk) archive = ArchiveFactory.create(transport="local", basedir=self.basedir, default=True, policy="cdimage") with mock.patch("archives.transports.urllib2") as urllib2_mock: urllib2_mock.urlopen.side_effect = lambda x: StringIO( u"Artifact %s") with mock.patch("archives.tasks.archive_artifact_from_jenkins" ) as archive_task: with mock.patch("archives.tasks.link_artifact_in_archive" ) as link_task: process_build_artifacts(build.pk) [item1, item2, item3, item4] = list( archive.get_archived_artifacts_for_build(build).order_by( "artifact")) self.assertEqual( [mock.call(item4.pk), mock.call(item2.pk)], archive_task.si.call_args_list) self.assertEqual( [mock.call(item4.pk, item3.pk), mock.call(item2.pk, item1.pk)], link_task.si.call_args_list)
def test_archive_artifact_from_non_finalized_projectbuild(self): """ If the build is complete, and the item being archived is in a FINALIZED ProjectBuild, it should use the transport to set the current directory correctly. """ project = ProjectFactory.create() dependency1 = DependencyFactory.create() ProjectDependency.objects.create( project=project, dependency=dependency1) dependency2 = DependencyFactory.create() ProjectDependency.objects.create( project=project, dependency=dependency2) projectbuild = build_project(project, queue_build=False) build = BuildFactory.create( job=dependency1.job, build_id=projectbuild.build_key, phase=Build.FINALIZED) ProjectBuildDependency.objects.create( build=build, projectbuild=projectbuild, dependency=dependency1) artifact = ArtifactFactory.create( build=build, filename="testing/testing.txt") # We need to ensure that the artifacts are all connected up. process_build_dependencies(build.pk) archive = ArchiveFactory.create( transport="local", basedir=self.basedir, default=True) item = [x for x in archive.add_build(artifact.build)[artifact] if x.projectbuild_dependency][0] transport = LoggingTransport(archive) with mock.patch.object( Archive, "get_transport", return_value=transport): archive_artifact_from_jenkins(item.pk) self.assertEqual( ["START", "%s -> %s root:testing" % (artifact.url, item.archived_path), "END"], transport.log)
def test_new_build_with_no_auto_track_build(self): """ If we create a new build for a dependency of a Project, and the ProjectDependency is not set to auto_track then the current_build should not be updated. """ build1 = BuildFactory.create() dependency = DependencyFactory.create(job=build1.job) project_dependency = ProjectDependency.objects.create( project=self.project, dependency=dependency, auto_track=False) project_dependency.current_build = build1 project_dependency.save() build2 = BuildFactory.create(job=build1.job) process_build_dependencies(build2.pk) # Reload the project dependency project_dependency = ProjectDependency.objects.get( pk=project_dependency.pk) self.assertEqual(build1, project_dependency.current_build)
def test_new_build_with_no_auto_track_build(self): """ If we create a new build for a dependency of a Project, and the ProjectDependency is not set to auto_track then the current_build should not be updated. """ build1 = BuildFactory.create() dependency = DependencyFactory.create(job=build1.job) project_dependency = ProjectDependency.objects.create( project=self.project, dependency=dependency, auto_track=False ) project_dependency.current_build = build1 project_dependency.save() build2 = BuildFactory.create(job=build1.job) process_build_dependencies(build2.pk) # Reload the project dependency project_dependency = ProjectDependency.objects.get(pk=project_dependency.pk) self.assertEqual(build1, project_dependency.current_build)
def test_build_with_projectbuild_dependencies(self): """ ProjectBuildDependencies should be tied to the newly created build. """ project1, dependency1, dependency2 = self.create_dependencies(2) project2 = ProjectFactory.create() ProjectDependency.objects.create(project=project2, dependency=dependency2) projectbuild = build_project(project1, queue_build=False) build1 = BuildFactory.create( job=dependency1.job, build_id=projectbuild.build_key) process_build_dependencies(build1.pk) dependencies = ProjectBuildDependency.objects.all().order_by( "dependency__name") self.assertEqual( sorted([dependency1, dependency2], key=lambda x: x.name), [b.dependency for b in dependencies]) self.assertEqual( [None, build1], sorted([b.build for b in dependencies]))
def test_build_with_projectbuild_dependencies(self): """ ProjectBuildDependencies should be tied to the newly created build. """ project1, dependency1, dependency2 = self.create_dependencies(2) project2 = ProjectFactory.create() ProjectDependency.objects.create(project=project2, dependency=dependency2) projectbuild = build_project(project1, queue_build=False) build1 = BuildFactory.create(job=dependency1.job, build_id=projectbuild.build_key) process_build_dependencies(build1.pk) dependencies = ProjectBuildDependency.objects.all().order_by( "dependency__name") self.assertEqual( sorted([dependency1, dependency2], key=lambda x: x.name), [b.dependency for b in dependencies]) self.assertEqual([None, build1], sorted([b.build for b in dependencies]))
def test_can_be_archived(self): """ A ProjectBuild knows whether or not it's ready to be archived. """ dependency1 = DependencyFactory.create() ProjectDependency.objects.create( project=self.project, dependency=dependency1) dependency2 = DependencyFactory.create() ProjectDependency.objects.create( project=self.project, dependency=dependency2) from projects.helpers import build_project projectbuild = build_project(self.project, queue_build=False) # Current status is STARTED so we can't archive this build. self.assertEqual("UNKNOWN", projectbuild.phase) self.assertFalse(projectbuild.can_be_archived) builds = [] for job in [dependency1.job, dependency2.job]: build = BuildFactory.create( job=job, build_id=projectbuild.build_key, phase=Build.FINALIZED) builds.append(build) process_build_dependencies(build.pk) projectbuild = ProjectBuild.objects.get(pk=projectbuild.pk) self.assertEqual(Build.FINALIZED, projectbuild.phase) self.assertFalse( projectbuild.can_be_archived, "Build with no artifacts can be archived") for build in builds: ArtifactFactory.create(build=build) self.assertTrue( projectbuild.can_be_archived, "Build with artifacts can't be archived") projectbuild.archived = timezone.now() self.assertFalse(projectbuild.can_be_archived)
def test_auto_track_dependency_triggers_project_build_creation(self): """ If we record a build of a project dependency that is auto-tracked, then this should trigger the creation of a new ProjectBuild for that project. """ dependency1 = DependencyFactory.create() ProjectDependency.objects.create(project=self.project, dependency=dependency1) dependency2 = DependencyFactory.create() existing_build = BuildFactory.create(job=dependency2.job, phase=Build.FINALIZED) ProjectDependency.objects.create(project=self.project, dependency=dependency2, current_build=existing_build) self.assertEqual( 0, ProjectBuild.objects.filter(project=self.project).count()) build = BuildFactory.create(job=dependency1.job, phase=Build.FINALIZED) process_build_dependencies(build.pk) self.assertEqual( 1, ProjectBuild.objects.filter(project=self.project).count()) projectbuild = ProjectBuild.objects.get(project=self.project) self.assertEqual( 2, ProjectBuildDependency.objects.filter( projectbuild=projectbuild).count()) build_dependency1 = ProjectBuildDependency.objects.get( projectbuild=projectbuild, dependency=dependency1) self.assertEqual(build, build_dependency1.build) build_dependency2 = ProjectBuildDependency.objects.get( projectbuild=projectbuild, dependency=dependency2) self.assertEqual(existing_build, build_dependency2.build)
def test_item_from_artifact_and_archived_artifact(self): """ Return an artifact or archived artifact in a standard format for display. """ 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, phase=Build.FINALIZED) # Create the artifact and check the display format artifact = ArtifactFactory.create(build=build, filename="file1.gz") artifact_item = ProjectDetailView.item_from_artifact(artifact) self.assertIsNotNone(artifact_item) self.assertTrue(isinstance(artifact_item, dict)) self.assertTrue("build_name" in artifact_item) self.assertTrue("filename" in artifact_item) self.assertTrue("url" in artifact_item) self.assertTrue("archived" in artifact_item) # Archive the artifact and check the display format process_build_dependencies(build.pk) archive = ArchiveFactory.create(policy="cdimage", default=True) items = [] for x in archive.add_build(build)[artifact]: if x.projectbuild_dependency: items.append(x) self.assertEquals(len(items), 1) archived_item = ProjectDetailView.item_from_archived_artifact(items[0]) self.assertIsNotNone(archived_item) self.assertTrue(isinstance(archived_item, dict)) self.assertTrue("build_name" in archived_item) self.assertTrue("filename" in archived_item) self.assertTrue("url" in archived_item) self.assertTrue("archived" in archived_item)
def test_project_detail_artifacts(self): """ The project detail should return artifacts with the URL from Jenkins or from the archive (for archived artifacts). """ 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, phase=Build.FINALIZED) artifact = ArtifactFactory.create(build=build, filename="file1.gz") process_build_dependencies(build.pk) # The project detail should link to the artifact from Jenkins project_url = reverse("project_detail", kwargs={"pk": project.pk}) response = self.app.get(project_url, user="******") self.assertEqual(200, response.status_code) self.assertIsNotNone( response.context["project"].get_current_projectbuild()) self.assertEqual( [ProjectDetailView.item_from_artifact(artifact)], response.context["current_artifacts"]) # Archive the artifact and the view should display the archived item archive = ArchiveFactory.create(policy="cdimage", default=True) items = [] for x in archive.add_build(build)[artifact]: if x.projectbuild_dependency: items.append(ProjectDetailView.item_from_archived_artifact(x)) project_url = reverse("project_detail", kwargs={"pk": project.pk}) response = self.app.get(project_url, user="******") self.assertEqual(200, response.status_code) self.assertEqual(items, response.context["current_artifacts"])
def test_can_be_archived(self): """ A ProjectBuild knows whether or not it's ready to be archived. """ dependency1 = DependencyFactory.create() ProjectDependency.objects.create(project=self.project, dependency=dependency1) dependency2 = DependencyFactory.create() ProjectDependency.objects.create(project=self.project, dependency=dependency2) from projects.helpers import build_project projectbuild = build_project(self.project, queue_build=False) # Current status is STARTED so we can't archive this build. self.assertEqual("UNKNOWN", projectbuild.phase) self.assertFalse(projectbuild.can_be_archived) builds = [] for job in [dependency1.job, dependency2.job]: build = BuildFactory.create(job=job, build_id=projectbuild.build_key, phase=Build.FINALIZED) builds.append(build) process_build_dependencies(build.pk) projectbuild = ProjectBuild.objects.get(pk=projectbuild.pk) self.assertEqual(Build.FINALIZED, projectbuild.phase) self.assertFalse(projectbuild.can_be_archived, "Build with no artifacts can be archived") for build in builds: ArtifactFactory.create(build=build) self.assertTrue(projectbuild.can_be_archived, "Build with artifacts can't be archived") projectbuild.archived = timezone.now() self.assertFalse(projectbuild.can_be_archived)
def test_auto_track_build(self): """ If we create a new build for a dependency of a Project, and the ProjectDependency is set to auto_track then the current_build should be updated to reflect the new build. """ build1 = BuildFactory.create() dependency = DependencyFactory.create(job=build1.job) project_dependency = ProjectDependency.objects.create(project=self.project, dependency=dependency) project_dependency.current_build = build1 project_dependency.save() build2 = BuildFactory.create(job=build1.job) result = process_build_dependencies(build2.pk) # Reload the project dependency project_dependency = ProjectDependency.objects.get(pk=project_dependency.pk) self.assertEqual(build2, project_dependency.current_build) self.assertEqual(build2.pk, result)
def test_auto_track_build(self): """ If we create a new build for a dependency of a Project, and the ProjectDependency is set to auto_track then the current_build should be updated to reflect the new build. """ build1 = BuildFactory.create() dependency = DependencyFactory.create(job=build1.job) project_dependency = ProjectDependency.objects.create( project=self.project, dependency=dependency) project_dependency.current_build = build1 project_dependency.save() build2 = BuildFactory.create(job=build1.job) result = process_build_dependencies(build2.pk) # Reload the project dependency project_dependency = ProjectDependency.objects.get( pk=project_dependency.pk) self.assertEqual(build2, project_dependency.current_build) self.assertEqual(build2.pk, result)