Commit dd112ef1 authored by Timothy Andrew's avatar Timothy Andrew

Test the `staging` cycle analytics phase.

Remove overlap from the "start + end" durations in the happy test
case. For the `staging` phase, the end time is the _first_ deployment
that happens after the MR merge.

If we have 5 MRs where the `start_time`s (merge time) are the
same, and all the `end_time`s (deploy to production) a few days from
now, only the earliest deploy will get picked up, because that
consitutes a deploy for _all_ the MRs.

We fix this by removing overlap. Every `start_time` is now generated to
be _after_ the preceding `end_time`.
parent 0f44c5a5
...@@ -85,6 +85,8 @@ class CycleAnalytics ...@@ -85,6 +85,8 @@ class CycleAnalytics
lambda do |data_point| lambda do |data_point|
merge_request = data_point[:merge_request] merge_request = data_point[:merge_request]
if merge_request.metrics.present? if merge_request.metrics.present?
# The first production deploy to the target branch that occurs after the merge request has been merged in.
# TODO: Does this need to account for reverts?
deployments = Deployment.joins(:environment).where(ref: merge_request.target_branch, "environments.name" => "production"). deployments = Deployment.joins(:environment).where(ref: merge_request.target_branch, "environments.name" => "production").
where("deployments.created_at > ?", merge_request.metrics.merged_at) where("deployments.created_at > ?", merge_request.metrics.merged_at)
deployment = deployments.order(:created_at).first deployment = deployments.order(:created_at).first
......
require 'spec_helper'
describe 'CycleAnalytics#staging', feature: true do
let(:project) { create(:project) }
let(:from_date) { 10.days.ago }
let(:user) { create(:user, :admin) }
subject { CycleAnalytics.new(project, from: from_date) }
def create_merge_request_closing_issue(issue, message: nil)
source_branch = random_git_name
project.repository.add_branch(user, source_branch, 'master')
sha = project.repository.commit_file(user, random_git_name, "content", "commit message", source_branch, false)
project.repository.commit(sha)
opts = {
title: 'Awesome merge_request',
description: message || "Fixes #{issue.to_reference}",
source_branch: source_branch,
target_branch: 'master'
}
MergeRequests::CreateService.new(project, user, opts).execute
end
def merge_merge_requests_closing_issue(issue)
merge_requests = issue.closed_by_merge_requests
merge_requests.each { |merge_request| MergeRequests::MergeService.new(project, user).execute(merge_request) }
end
def deploy_master(environment: 'production')
CreateDeploymentService.new(project, user, {
environment: environment,
ref: 'master',
tag: false,
sha: project.repository.commit('master').sha
}).execute
end
generate_cycle_analytics_spec(phase: :staging,
data_fn: lambda do |context|
issue = context.create(:issue, project: context.project)
{ issue: issue, merge_request: context.create_merge_request_closing_issue(issue) }
end,
start_time_conditions: [["merge request that closes issue is merged", -> (context, data) { context.merge_merge_requests_closing_issue(data[:issue])} ]],
end_time_conditions: [["merge request that closes issue is deployed to production", -> (context, data) { context.deploy_master }],
["production deploy happens after merge request is merged (along with other changes)",
lambda do |context, data|
# Make other changes on master
sha = context.project.repository.commit_file(context.user, context.random_git_name, "content", "commit message", 'master', false)
context.project.repository.commit(sha)
context.deploy_master
end]])
context "when a regular merge request (that doesn't close the issue) is merged and deployed" do
it "returns nil" do
5.times do
merge_request = create(:merge_request)
MergeRequests::MergeService.new(project, user).execute(merge_request)
deploy_master
end
expect(subject.staging).to be_nil
end
end
context "when the deployment happens to a non-production environment" do
it "returns nil" do
5.times do
issue = create(:issue, project: project)
merge_request = create_merge_request_closing_issue(issue)
MergeRequests::MergeService.new(project, user).execute(merge_request)
deploy_master(environment: 'staging')
end
expect(subject.staging).to be_nil
end
end
end
...@@ -26,10 +26,10 @@ module CycleAnalyticsHelpers ...@@ -26,10 +26,10 @@ module CycleAnalyticsHelpers
context "start condition: #{start_time_conditions.map(&:first).to_sentence}" do context "start condition: #{start_time_conditions.map(&:first).to_sentence}" do
context "end condition: #{end_time_conditions.map(&:first).to_sentence}" do context "end condition: #{end_time_conditions.map(&:first).to_sentence}" do
it "finds the median of available durations between the two conditions" do it "finds the median of available durations between the two conditions" do
time_differences = Array.new(5) do time_differences = Array.new(5) do |index|
data = data_fn[self] data = data_fn[self]
start_time = Time.now start_time = (index * 10).days.from_now
end_time = rand(1..10).days.from_now end_time = start_time + rand(1..5).days
start_time_conditions.each do |condition_name, condition_fn| start_time_conditions.each do |condition_name, condition_fn|
Timecop.freeze(start_time) { condition_fn[self, data] } Timecop.freeze(start_time) { condition_fn[self, data] }
...@@ -43,7 +43,7 @@ module CycleAnalyticsHelpers ...@@ -43,7 +43,7 @@ module CycleAnalyticsHelpers
end end
median_time_difference = time_differences.sort[2] median_time_difference = time_differences.sort[2]
expect(subject.send(phase)).to be_within(2).of(median_time_difference) expect(subject.send(phase)).to be_within(5).of(median_time_difference)
end end
context "when the data belongs to another project" do context "when the data belongs to another project" do
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment