summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTimothy Andrew <mail@timothyandrew.net>2016-09-07 11:25:01 +0530
committerTimothy Andrew <mail@timothyandrew.net>2016-09-07 12:12:51 +0530
commitdd112ef1174b2b0207769a361cddfc44dfe2e6d0 (patch)
treecc78741339cbfcb938c5da892d5c09db0587fe7e
parent0f44c5a56fdc5986797808c508d7b42236cee450 (diff)
downloadgitlab-ce-dd112ef1174b2b0207769a361cddfc44dfe2e6d0.tar.gz
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`.
-rw-r--r--app/models/cycle_analytics/queries.rb2
-rw-r--r--spec/models/cycle_analytics/staging_spec.rb79
-rw-r--r--spec/support/cycle_analytics_helpers.rb8
3 files changed, 85 insertions, 4 deletions
diff --git a/app/models/cycle_analytics/queries.rb b/app/models/cycle_analytics/queries.rb
index 7816c96a0ed..2d134a0d9a4 100644
--- a/app/models/cycle_analytics/queries.rb
+++ b/app/models/cycle_analytics/queries.rb
@@ -85,6 +85,8 @@ class CycleAnalytics
lambda do |data_point|
merge_request = data_point[:merge_request]
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").
where("deployments.created_at > ?", merge_request.metrics.merged_at)
deployment = deployments.order(:created_at).first
diff --git a/spec/models/cycle_analytics/staging_spec.rb b/spec/models/cycle_analytics/staging_spec.rb
new file mode 100644
index 00000000000..23e01532500
--- /dev/null
+++ b/spec/models/cycle_analytics/staging_spec.rb
@@ -0,0 +1,79 @@
+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
diff --git a/spec/support/cycle_analytics_helpers.rb b/spec/support/cycle_analytics_helpers.rb
index b2ca42d7cce..4819f8b517a 100644
--- a/spec/support/cycle_analytics_helpers.rb
+++ b/spec/support/cycle_analytics_helpers.rb
@@ -26,10 +26,10 @@ module CycleAnalyticsHelpers
context "start condition: #{start_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
- time_differences = Array.new(5) do
+ time_differences = Array.new(5) do |index|
data = data_fn[self]
- start_time = Time.now
- end_time = rand(1..10).days.from_now
+ start_time = (index * 10).days.from_now
+ end_time = start_time + rand(1..5).days
start_time_conditions.each do |condition_name, condition_fn|
Timecop.freeze(start_time) { condition_fn[self, data] }
@@ -43,7 +43,7 @@ module CycleAnalyticsHelpers
end
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
context "when the data belongs to another project" do