Fix Feature Flag Controller Spec Sorting on Name
What does this MR do?
Fix a sorting issue in feature flag controller specs.
We sort feature flags by name for GET
requests to the index:
- https://gitlab.com/gitlab-org/gitlab/-/blob/97865775c433f5b41af40a5ef95b4936d54baaf2/ee/app/controllers/projects/feature_flags_controller.rb#L23
- https://gitlab.com/gitlab-org/gitlab/-/blob/97865775c433f5b41af40a5ef95b4936d54baaf2/ee/app/finders/feature_flags_finder.rb#L21
- https://gitlab.com/gitlab-org/gitlab/-/blob/97865775c433f5b41af40a5ef95b4936d54baaf2/ee/app/models/operations/feature_flag.rb#L42
The feature flag factory generates a random name of the form feature_flag_#{n}
:
Our specs for the FeatureFlagController#index
assume that the flags will be sorted in the order they are created. Most of the time, this will be true. Consider the case when we generate names like feature_flag_79
, feature_flag_80
, feature_flag_81
, and so on:
gitlabhq_development=# SELECT id, name FROM (VALUES (79, 'feature_flag_79'), (80, 'feature_flag_80'), (81, 'feature_flag_81')) AS t(id, name) ORDER BY name;
id | name
----+-----------------
79 | feature_flag_79
80 | feature_flag_80
81 | feature_flag_81
(3 rows)
gitlabhq_development=#
However, we may generate feature flag names like feature_flag_99
, feature_flag_100
, in which case Postgres will sort the second created flag first:
gitlabhq_development=# SELECT id, name FROM (VALUES (99, 'feature_flag_99'), (100, 'feature_flag_100')) AS t(id, name) ORDER BY name;
id | name
-----+------------------
100 | feature_flag_100
99 | feature_flag_99
(2 rows)
gitlabhq_development=#
If we run enough specs thereby bumping up the sequence number in the factory, there is a chance that this generates names that suddenly sort in an order unexpected by the specs, causing a spec to randomly fail.
I ran into this locally while working on #212318 (closed).
I can consistently cause this spec fail to by running bin/rspec ee/spec/controllers/projects/feature_flag_issues_controller_spec.rb ee/spec/controllers/projects/feature_flags_controller_spec.rb
, because the two flags in the spec are generated with the names feature_flag_99
and feature_flag_100
.
This MR fixes the issue by giving the flags created in the specs consistent, rather than randomly generated, names so they should always sort in the same order.
Does this MR meet the acceptance criteria?
Conformity
- [-] Changelog entry
- [-] Documentation (if required)
-
Code review guidelines -
Merge request performance guidelines -
Style guides -
Database guides -
Separation of EE specific content
Availability and Testing
-
Review and add/update tests for this feature/bug. Consider all test levels. See the Test Planning Process. -
Tested in all supported browsers -
Informed Infrastructure department of a default or new setting change, if applicable per definition of done
Security
If this MR contains changes to processing or storing of credentials or tokens, authorization and authentication methods and other items described in the security review guidelines:
-
Label as security and @ mention @gitlab-com/gl-security/appsec
-
The MR includes necessary changes to maintain consistency between UI, API, email, or other methods -
Security reports checked/validated by a reviewer from the AppSec team