Skip to content

Update Rspec job factories to write to ci_job_definitions; update tests to stop mutating options & yaml_variables

What does this MR do and why?

Context

We will eventually introduce the feature flag stop_writing_builds_metadata (roll-out issue: #552065), which will prevent us from updating metadata attributes such as options and yaml_variables after the job is created. These attributes should not be mutable because we will later populate job_definitions (#551860 (closed)) and that table is insert-only.

We currently mutate these attributes a lot in our spec tests so we need to move away from this approach. Moreover, we need to update the job factories to write to both ci_builds_metadata and ci_job_definitions. If a test mutates options= directly, for example, the change will only update in metadata but not job_definition. As such, the test will break since our code prioritizes reading from job_definition because the FF read_from_new_ci_destinations is default enabled in our specs.

This MR

This MR helps to prepare for the introduction of the FF stop_writing_builds_metadata for options/yaml_variables. Details:

  1. Updates the job factory to write to both ci_builds_metadata and ci_job_definitions/ci_job_definition_instances.
  • This more accurately represents the data state as we migrate to ci_job_definitions.
  1. Updates Ci::Metadatable#degenerate! to also destroy the job_definition_instance record.
  • Since this is just a small change, I included it to easily fix the degenerable job tests.
  1. Updates the tests to stop mutating options or yaml_variables.
  • We only need to address these two attributes for now because they're directly impacted by Ci::Metadatable#read_metadata_attribute which prioritizes reading from job_definition in the tests. The other attributes will be addressed as part of #552057 (closed).

Test updates

I generally implemented one of 3 strategies to fix the tests:

  1. Set the value of options/yaml_variables directly in the factory on creation/build.
  • This allows us to make changes to the tests in the least invasive way. It also ensures that the options value follows the correct schema (when FF ci_validate_config_options is removed) and that it's being read correctly according to the read_metadata_attribute logic.
  • In the future, we could consider refactoring the create tests to use build or build_stubbed (and ensuring .valid?), but for this iteration we're just making the simplest changes.
  1. If the object is only created once (e.g. with let_it_be), or the options/yaml_variables value can remain static in the context and option (1) would be impractical, then I stubbed the value with allow(x).to receive(y).and_return(z).

  2. For all other cases, the feature flag read_from_new_ci_destinations is stubbed as false.

References

MR acceptance checklist

Evaluate this MR against the MR acceptance checklist. It helps you analyze changes to reduce risks in quality, performance, reliability, security, and maintainability.

Related to #552057 (closed)

Edited by Leaminn Ma

Merge request reports

Loading