Cross-test pollution causing failures where a URL expects `localhost` but returns `127.0.0.1:port`

Seeing build failures like the following:

  8) MergeRequests::GetUrlsService#execute pushing new branch and existing branch (with merge request created) at once returns 2 urls for both creating new and showing merge request
     Failure/Error:
       expect(result).to match([{
         branch_name: "new_branch",
         url: new_merge_request_url,
         new_merge_request: true
       }, {
         branch_name: "existing_branch",
         url: show_merge_request_url,
         new_merge_request: false
       }])

       expected [{:branch_name=>"new_branch", :url=>"http://127.0.0.1:40695/namespace604/gitlabhq/merge_requests/new?..., :url=>"http://127.0.0.1:40695/namespace604/gitlabhq/merge_requests/1", :new_merge_request=>false}] to match [{:branch_name=>"new_branch", :url=>"http://localhost/namespace604/gitlabhq/merge_requests/new?merge_request%5Bsource_branch%5D=new_branch", :new_merge_request=>true}, {:branch_name=>"existing_branch", :url=>"http://localhost/namespace604/gitlabhq/merge_requests/1", :new_merge_request=>false}]
       Diff:

       @@ -1,8 +1,8 @@
        [{:branch_name=>"new_branch",
          :url=>
       -   "http://localhost/namespace604/gitlabhq/merge_requests/new?merge_request%5Bsource_branch%5D=new_branch",
       +   "http://127.0.0.1:40695/namespace604/gitlabhq/merge_requests/new?merge_request%5Bsource_branch%5D=new_branch",
          :new_merge_request=>true},
         {:branch_name=>"existing_branch",
       -  :url=>"http://localhost/namespace604/gitlabhq/merge_requests/1",
       +  :url=>"http://127.0.0.1:40695/namespace604/gitlabhq/merge_requests/1",
          :new_merge_request=>false}]
     # ./spec/services/merge_requests/get_urls_service_spec.rb:122:in `block (4 levels) in <top (required)>'
     # ./spec/spec_helper.rb:97:in `block (2 levels) in <top (required)>'

in https://gitlab.com/gitlab-org/gitlab-ce/-/jobs/24678170

@mkozono fixed a similar failure in https://gitlab.com/gitlab-org/gitlab-ce/merge_requests/13105 by updating the spec to be less picky, but there are other specs where the URL does actually matter, and aren't as easily fixed.

@smcgivern suspected this might be cross-test pollution and he appears to be right -- I was able to reproduce the failures locally with the following:

bundle exec rspec --order defined spec/models/merge_request_spec.rb spec/models/concerns/noteable_spec.rb spec/features/issuables/markdown_references_spec.rb spec/features/projects/user_browses_files_spec.rb spec/services/projects/destroy_service_spec.rb spec/lib/gitlab/diff/position_tracer_spec.rb spec/features/security/project/snippet/private_access_spec.rb spec/finders/group_projects_finder_spec.rb spec/requests/api/v3/milestones_spec.rb spec/features/dashboard/issues_spec.rb spec/features/boards/modal_filter_spec.rb spec/features/issuables/default_sort_order_spec.rb spec/features/merge_requests/assign_issues_spec.rb spec/requests/api/v3/services_spec.rb spec/unicorn/unicorn_spec.rb spec/requests/api/v3/environments_spec.rb spec/features/dashboard/issues_filter_spec.rb spec/services/boards/lists/move_service_spec.rb spec/features/issues/spam_issues_spec.rb spec/services/merge_requests/get_urls_service_spec.rb

Currently running this with --bisect to try to isolate it.

Edited by Robert Speicher