summaryrefslogtreecommitdiff
path: root/spec/lib
diff options
context:
space:
mode:
authorDouwe Maan <douwe@gitlab.com>2015-11-17 15:40:54 +0100
committerDouwe Maan <douwe@gitlab.com>2015-11-17 15:40:54 +0100
commite849b51cae8956c9d4eebe9c42804443a55edb83 (patch)
tree69d96404ed3d9fb6f10d59334cffce4c4cf62725 /spec/lib
parent56ea71a3b19396b01b3b8495337fbf22c534524f (diff)
parent1c040b3f0a3ecb18fc8fdea3cf99b70edad8d873 (diff)
downloadgitlab-ce-e849b51cae8956c9d4eebe9c42804443a55edb83.tar.gz
Merge branch 'master' into james11/gitlab-ce-removable-group-owner
Diffstat (limited to 'spec/lib')
-rw-r--r--spec/lib/ci/gitlab_ci_yaml_processor_spec.rb432
-rw-r--r--spec/lib/gitlab/inline_diff_spec.rb (renamed from spec/lib/gitlab/diff/inline_diff_spec.rb)0
-rw-r--r--spec/lib/gitlab/lfs/lfs_router_spec.rb650
-rw-r--r--spec/lib/gitlab/markdown/sanitization_filter_spec.rb101
-rw-r--r--spec/lib/gitlab/push_data_builder_spec.rb6
-rw-r--r--spec/lib/gitlab/sherlock/collection_spec.rb82
-rw-r--r--spec/lib/gitlab/sherlock/file_sample_spec.rb54
-rw-r--r--spec/lib/gitlab/sherlock/line_profiler_spec.rb73
-rw-r--r--spec/lib/gitlab/sherlock/line_sample_spec.rb33
-rw-r--r--spec/lib/gitlab/sherlock/location_spec.rb40
-rw-r--r--spec/lib/gitlab/sherlock/middleware_spec.rb79
-rw-r--r--spec/lib/gitlab/sherlock/query_spec.rb113
-rw-r--r--spec/lib/gitlab/sherlock/transaction_spec.rb222
13 files changed, 1796 insertions, 89 deletions
diff --git a/spec/lib/ci/gitlab_ci_yaml_processor_spec.rb b/spec/lib/ci/gitlab_ci_yaml_processor_spec.rb
index abdb6b89ac5..7d90f9877c6 100644
--- a/spec/lib/ci/gitlab_ci_yaml_processor_spec.rb
+++ b/spec/lib/ci/gitlab_ci_yaml_processor_spec.rb
@@ -2,7 +2,8 @@ require 'spec_helper'
module Ci
describe GitlabCiYamlProcessor do
-
+ let(:path) { 'path' }
+
describe "#builds_for_ref" do
let(:type) { 'test' }
@@ -12,7 +13,7 @@ module Ci
rspec: { script: "rspec" }
})
- config_processor = GitlabCiYamlProcessor.new(config)
+ config_processor = GitlabCiYamlProcessor.new(config, path)
expect(config_processor.builds_for_stage_and_ref(type, "master").size).to eq(1)
expect(config_processor.builds_for_stage_and_ref(type, "master").first).to eq({
@@ -28,78 +29,218 @@ module Ci
when: "on_success"
})
end
+
+ describe :only do
+ it "does not return builds if only has another branch" do
+ config = YAML.dump({
+ before_script: ["pwd"],
+ rspec: { script: "rspec", only: ["deploy"] }
+ })
- it "does not return builds if only has another branch" do
- config = YAML.dump({
- before_script: ["pwd"],
- rspec: { script: "rspec", only: ["deploy"] }
- })
+ config_processor = GitlabCiYamlProcessor.new(config, path)
- config_processor = GitlabCiYamlProcessor.new(config)
+ expect(config_processor.builds_for_stage_and_ref(type, "master").size).to eq(0)
+ end
- expect(config_processor.builds_for_stage_and_ref(type, "master").size).to eq(0)
- end
+ it "does not return builds if only has regexp with another branch" do
+ config = YAML.dump({
+ before_script: ["pwd"],
+ rspec: { script: "rspec", only: ["/^deploy$/"] }
+ })
- it "does not return builds if only has regexp with another branch" do
- config = YAML.dump({
- before_script: ["pwd"],
- rspec: { script: "rspec", only: ["/^deploy$/"] }
- })
+ config_processor = GitlabCiYamlProcessor.new(config, path)
- config_processor = GitlabCiYamlProcessor.new(config)
+ expect(config_processor.builds_for_stage_and_ref(type, "master").size).to eq(0)
+ end
- expect(config_processor.builds_for_stage_and_ref(type, "master").size).to eq(0)
- end
+ it "returns builds if only has specified this branch" do
+ config = YAML.dump({
+ before_script: ["pwd"],
+ rspec: { script: "rspec", only: ["master"] }
+ })
- it "returns builds if only has specified this branch" do
- config = YAML.dump({
- before_script: ["pwd"],
- rspec: { script: "rspec", only: ["master"] }
- })
+ config_processor = GitlabCiYamlProcessor.new(config, path)
- config_processor = GitlabCiYamlProcessor.new(config)
+ expect(config_processor.builds_for_stage_and_ref(type, "master").size).to eq(1)
+ end
- expect(config_processor.builds_for_stage_and_ref(type, "master").size).to eq(1)
- end
+ it "returns builds if only has a list of branches including specified" do
+ config = YAML.dump({
+ before_script: ["pwd"],
+ rspec: { script: "rspec", type: type, only: ["master", "deploy"] }
+ })
- it "does not build tags" do
- config = YAML.dump({
- before_script: ["pwd"],
- rspec: { script: "rspec", except: ["tags"] }
- })
+ config_processor = GitlabCiYamlProcessor.new(config, path)
- config_processor = GitlabCiYamlProcessor.new(config)
+ expect(config_processor.builds_for_stage_and_ref(type, "deploy").size).to eq(1)
+ end
- expect(config_processor.builds_for_stage_and_ref(type, "0-1", true).size).to eq(0)
- end
+ it "returns builds if only has a branches keyword specified" do
+ config = YAML.dump({
+ before_script: ["pwd"],
+ rspec: { script: "rspec", type: type, only: ["branches"] }
+ })
- it "returns builds if only has a list of branches including specified" do
- config = YAML.dump({
- before_script: ["pwd"],
- rspec: { script: "rspec", type: type, only: ["master", "deploy"] }
- })
+ config_processor = GitlabCiYamlProcessor.new(config, path)
- config_processor = GitlabCiYamlProcessor.new(config)
+ expect(config_processor.builds_for_stage_and_ref(type, "deploy").size).to eq(1)
+ end
+
+ it "does not return builds if only has a tags keyword" do
+ config = YAML.dump({
+ before_script: ["pwd"],
+ rspec: { script: "rspec", type: type, only: ["tags"] }
+ })
+
+ config_processor = GitlabCiYamlProcessor.new(config, path)
+
+ expect(config_processor.builds_for_stage_and_ref(type, "deploy").size).to eq(0)
+ end
+
+ it "returns builds if only has current repository path" do
+ config = YAML.dump({
+ before_script: ["pwd"],
+ rspec: { script: "rspec", type: type, only: ["branches@path"] }
+ })
+
+ config_processor = GitlabCiYamlProcessor.new(config, path)
+
+ expect(config_processor.builds_for_stage_and_ref(type, "deploy").size).to eq(1)
+ end
+
+ it "does not return builds if only has different repository path" do
+ config = YAML.dump({
+ before_script: ["pwd"],
+ rspec: { script: "rspec", type: type, only: ["branches@fork"] }
+ })
+
+ config_processor = GitlabCiYamlProcessor.new(config, path)
+
+ expect(config_processor.builds_for_stage_and_ref(type, "deploy").size).to eq(0)
+ end
+
+ it "returns build only for specified type" do
+
+ config = YAML.dump({
+ before_script: ["pwd"],
+ rspec: { script: "rspec", type: "test", only: ["master", "deploy"] },
+ staging: { script: "deploy", type: "deploy", only: ["master", "deploy"] },
+ production: { script: "deploy", type: "deploy", only: ["master@path", "deploy"] },
+ })
- expect(config_processor.builds_for_stage_and_ref(type, "deploy").size).to eq(1)
+ config_processor = GitlabCiYamlProcessor.new(config, 'fork')
+
+ expect(config_processor.builds_for_stage_and_ref("deploy", "deploy").size).to eq(2)
+ expect(config_processor.builds_for_stage_and_ref("test", "deploy").size).to eq(1)
+ expect(config_processor.builds_for_stage_and_ref("deploy", "master").size).to eq(1)
+ end
end
- it "returns build only for specified type" do
+ describe :except do
+ it "returns builds if except has another branch" do
+ config = YAML.dump({
+ before_script: ["pwd"],
+ rspec: { script: "rspec", except: ["deploy"] }
+ })
- config = YAML.dump({
- before_script: ["pwd"],
- build: { script: "build", type: "build", only: ["master", "deploy"] },
- rspec: { script: "rspec", type: type, only: ["master", "deploy"] },
- staging: { script: "deploy", type: "deploy", only: ["master", "deploy"] },
- production: { script: "deploy", type: "deploy", only: ["master", "deploy"] },
- })
+ config_processor = GitlabCiYamlProcessor.new(config, path)
- config_processor = GitlabCiYamlProcessor.new(config)
+ expect(config_processor.builds_for_stage_and_ref(type, "master").size).to eq(1)
+ end
+
+ it "returns builds if except has regexp with another branch" do
+ config = YAML.dump({
+ before_script: ["pwd"],
+ rspec: { script: "rspec", except: ["/^deploy$/"] }
+ })
+
+ config_processor = GitlabCiYamlProcessor.new(config, path)
+
+ expect(config_processor.builds_for_stage_and_ref(type, "master").size).to eq(1)
+ end
+
+ it "does not return builds if except has specified this branch" do
+ config = YAML.dump({
+ before_script: ["pwd"],
+ rspec: { script: "rspec", except: ["master"] }
+ })
+
+ config_processor = GitlabCiYamlProcessor.new(config, path)
- expect(config_processor.builds_for_stage_and_ref("production", "deploy").size).to eq(0)
- expect(config_processor.builds_for_stage_and_ref(type, "deploy").size).to eq(1)
- expect(config_processor.builds_for_stage_and_ref("deploy", "deploy").size).to eq(2)
+ expect(config_processor.builds_for_stage_and_ref(type, "master").size).to eq(0)
+ end
+
+ it "does not return builds if except has a list of branches including specified" do
+ config = YAML.dump({
+ before_script: ["pwd"],
+ rspec: { script: "rspec", type: type, except: ["master", "deploy"] }
+ })
+
+ config_processor = GitlabCiYamlProcessor.new(config, path)
+
+ expect(config_processor.builds_for_stage_and_ref(type, "deploy").size).to eq(0)
+ end
+
+ it "does not return builds if except has a branches keyword specified" do
+ config = YAML.dump({
+ before_script: ["pwd"],
+ rspec: { script: "rspec", type: type, except: ["branches"] }
+ })
+
+ config_processor = GitlabCiYamlProcessor.new(config, path)
+
+ expect(config_processor.builds_for_stage_and_ref(type, "deploy").size).to eq(0)
+ end
+
+ it "returns builds if except has a tags keyword" do
+ config = YAML.dump({
+ before_script: ["pwd"],
+ rspec: { script: "rspec", type: type, except: ["tags"] }
+ })
+
+ config_processor = GitlabCiYamlProcessor.new(config, path)
+
+ expect(config_processor.builds_for_stage_and_ref(type, "deploy").size).to eq(1)
+ end
+
+ it "does not return builds if except has current repository path" do
+ config = YAML.dump({
+ before_script: ["pwd"],
+ rspec: { script: "rspec", type: type, except: ["branches@path"] }
+ })
+
+ config_processor = GitlabCiYamlProcessor.new(config, path)
+
+ expect(config_processor.builds_for_stage_and_ref(type, "deploy").size).to eq(0)
+ end
+
+ it "returns builds if except has different repository path" do
+ config = YAML.dump({
+ before_script: ["pwd"],
+ rspec: { script: "rspec", type: type, except: ["branches@fork"] }
+ })
+
+ config_processor = GitlabCiYamlProcessor.new(config, path)
+
+ expect(config_processor.builds_for_stage_and_ref(type, "deploy").size).to eq(1)
+ end
+
+ it "returns build except specified type" do
+ config = YAML.dump({
+ before_script: ["pwd"],
+ rspec: { script: "rspec", type: "test", except: ["master", "deploy", "test@fork"] },
+ staging: { script: "deploy", type: "deploy", except: ["master"] },
+ production: { script: "deploy", type: "deploy", except: ["master@fork"] },
+ })
+
+ config_processor = GitlabCiYamlProcessor.new(config, 'fork')
+
+ expect(config_processor.builds_for_stage_and_ref("deploy", "deploy").size).to eq(2)
+ expect(config_processor.builds_for_stage_and_ref("test", "test").size).to eq(0)
+ expect(config_processor.builds_for_stage_and_ref("deploy", "master").size).to eq(0)
+ end
end
+
end
describe "Image and service handling" do
@@ -111,7 +252,7 @@ module Ci
rspec: { script: "rspec" }
})
- config_processor = GitlabCiYamlProcessor.new(config)
+ config_processor = GitlabCiYamlProcessor.new(config, path)
expect(config_processor.builds_for_stage_and_ref("test", "master").size).to eq(1)
expect(config_processor.builds_for_stage_and_ref("test", "master").first).to eq({
@@ -139,7 +280,7 @@ module Ci
rspec: { image: "ruby:2.5", services: ["postgresql"], script: "rspec" }
})
- config_processor = GitlabCiYamlProcessor.new(config)
+ config_processor = GitlabCiYamlProcessor.new(config, path)
expect(config_processor.builds_for_stage_and_ref("test", "master").size).to eq(1)
expect(config_processor.builds_for_stage_and_ref("test", "master").first).to eq({
@@ -172,7 +313,7 @@ module Ci
rspec: { script: "rspec" }
})
- config_processor = GitlabCiYamlProcessor.new(config)
+ config_processor = GitlabCiYamlProcessor.new(config, path)
expect(config_processor.variables).to eq(variables)
end
end
@@ -184,7 +325,7 @@ module Ci
rspec: { script: "rspec", when: when_state }
})
- config_processor = GitlabCiYamlProcessor.new(config)
+ config_processor = GitlabCiYamlProcessor.new(config, path)
builds = config_processor.builds_for_stage_and_ref("test", "master")
expect(builds.size).to eq(1)
expect(builds.first[:when]).to eq(when_state)
@@ -192,6 +333,97 @@ module Ci
end
end
+ describe "Caches" do
+ it "returns cache when defined globally" do
+ config = YAML.dump({
+ cache: { paths: ["logs/", "binaries/"], untracked: true },
+ rspec: {
+ script: "rspec"
+ }
+ })
+
+ config_processor = GitlabCiYamlProcessor.new(config)
+
+ expect(config_processor.builds_for_stage_and_ref("test", "master").size).to eq(1)
+ expect(config_processor.builds_for_stage_and_ref("test", "master").first[:options][:cache]).to eq(
+ paths: ["logs/", "binaries/"],
+ untracked: true,
+ )
+ end
+
+ it "returns cache when defined in a job" do
+ config = YAML.dump({
+ rspec: {
+ cache: { paths: ["logs/", "binaries/"], untracked: true },
+ script: "rspec"
+ }
+ })
+
+ config_processor = GitlabCiYamlProcessor.new(config)
+
+ expect(config_processor.builds_for_stage_and_ref("test", "master").size).to eq(1)
+ expect(config_processor.builds_for_stage_and_ref("test", "master").first[:options][:cache]).to eq(
+ paths: ["logs/", "binaries/"],
+ untracked: true,
+ )
+ end
+
+ it "overwrite cache when defined for a job and globally" do
+ config = YAML.dump({
+ cache: { paths: ["logs/", "binaries/"], untracked: true },
+ rspec: {
+ script: "rspec",
+ cache: { paths: ["test/"], untracked: false },
+ }
+ })
+
+ config_processor = GitlabCiYamlProcessor.new(config)
+
+ expect(config_processor.builds_for_stage_and_ref("test", "master").size).to eq(1)
+ expect(config_processor.builds_for_stage_and_ref("test", "master").first[:options][:cache]).to eq(
+ paths: ["test/"],
+ untracked: false,
+ )
+ end
+ end
+
+ describe "Artifacts" do
+ it "returns artifacts when defined" do
+ config = YAML.dump({
+ image: "ruby:2.1",
+ services: ["mysql"],
+ before_script: ["pwd"],
+ rspec: {
+ artifacts: { paths: ["logs/", "binaries/"], untracked: true },
+ script: "rspec"
+ }
+ })
+
+ config_processor = GitlabCiYamlProcessor.new(config)
+
+ expect(config_processor.builds_for_stage_and_ref("test", "master").size).to eq(1)
+ expect(config_processor.builds_for_stage_and_ref("test", "master").first).to eq({
+ except: nil,
+ stage: "test",
+ stage_idx: 1,
+ name: :rspec,
+ only: nil,
+ commands: "pwd\nrspec",
+ tag_list: [],
+ options: {
+ image: "ruby:2.1",
+ services: ["mysql"],
+ artifacts: {
+ paths: ["logs/", "binaries/"],
+ untracked: true
+ }
+ },
+ when: "on_success",
+ allow_failure: false
+ })
+ end
+ end
+
describe "Error handling" do
it "indicates that object is invalid" do
expect{GitlabCiYamlProcessor.new("invalid_yaml\n!ccdvlf%612334@@@@")}.to raise_error(GitlabCiYamlProcessor::ValidationError)
@@ -200,156 +432,198 @@ module Ci
it "returns errors if tags parameter is invalid" do
config = YAML.dump({ rspec: { script: "test", tags: "mysql" } })
expect do
- GitlabCiYamlProcessor.new(config)
+ GitlabCiYamlProcessor.new(config, path)
end.to raise_error(GitlabCiYamlProcessor::ValidationError, "rspec job: tags parameter should be an array of strings")
end
it "returns errors if before_script parameter is invalid" do
config = YAML.dump({ before_script: "bundle update", rspec: { script: "test" } })
expect do
- GitlabCiYamlProcessor.new(config)
+ GitlabCiYamlProcessor.new(config, path)
end.to raise_error(GitlabCiYamlProcessor::ValidationError, "before_script should be an array of strings")
end
it "returns errors if image parameter is invalid" do
config = YAML.dump({ image: ["test"], rspec: { script: "test" } })
expect do
- GitlabCiYamlProcessor.new(config)
+ GitlabCiYamlProcessor.new(config, path)
end.to raise_error(GitlabCiYamlProcessor::ValidationError, "image should be a string")
end
it "returns errors if job name is blank" do
config = YAML.dump({ '' => { script: "test" } })
expect do
- GitlabCiYamlProcessor.new(config)
+ GitlabCiYamlProcessor.new(config, path)
end.to raise_error(GitlabCiYamlProcessor::ValidationError, "job name should be non-empty string")
end
it "returns errors if job name is non-string" do
config = YAML.dump({ 10 => { script: "test" } })
expect do
- GitlabCiYamlProcessor.new(config)
+ GitlabCiYamlProcessor.new(config, path)
end.to raise_error(GitlabCiYamlProcessor::ValidationError, "job name should be non-empty string")
end
it "returns errors if job image parameter is invalid" do
config = YAML.dump({ rspec: { script: "test", image: ["test"] } })
expect do
- GitlabCiYamlProcessor.new(config)
+ GitlabCiYamlProcessor.new(config, path)
end.to raise_error(GitlabCiYamlProcessor::ValidationError, "rspec job: image should be a string")
end
it "returns errors if services parameter is not an array" do
config = YAML.dump({ services: "test", rspec: { script: "test" } })
expect do
- GitlabCiYamlProcessor.new(config)
+ GitlabCiYamlProcessor.new(config, path)
end.to raise_error(GitlabCiYamlProcessor::ValidationError, "services should be an array of strings")
end
it "returns errors if services parameter is not an array of strings" do
config = YAML.dump({ services: [10, "test"], rspec: { script: "test" } })
expect do
- GitlabCiYamlProcessor.new(config)
+ GitlabCiYamlProcessor.new(config, path)
end.to raise_error(GitlabCiYamlProcessor::ValidationError, "services should be an array of strings")
end
it "returns errors if job services parameter is not an array" do
config = YAML.dump({ rspec: { script: "test", services: "test" } })
expect do
- GitlabCiYamlProcessor.new(config)
+ GitlabCiYamlProcessor.new(config, path)
end.to raise_error(GitlabCiYamlProcessor::ValidationError, "rspec job: services should be an array of strings")
end
it "returns errors if job services parameter is not an array of strings" do
config = YAML.dump({ rspec: { script: "test", services: [10, "test"] } })
expect do
- GitlabCiYamlProcessor.new(config)
+ GitlabCiYamlProcessor.new(config, path)
end.to raise_error(GitlabCiYamlProcessor::ValidationError, "rspec job: services should be an array of strings")
end
it "returns errors if there are unknown parameters" do
config = YAML.dump({ extra: "bundle update" })
expect do
- GitlabCiYamlProcessor.new(config)
+ GitlabCiYamlProcessor.new(config, path)
end.to raise_error(GitlabCiYamlProcessor::ValidationError, "Unknown parameter: extra")
end
it "returns errors if there are unknown parameters that are hashes, but doesn't have a script" do
config = YAML.dump({ extra: { services: "test" } })
expect do
- GitlabCiYamlProcessor.new(config)
+ GitlabCiYamlProcessor.new(config, path)
end.to raise_error(GitlabCiYamlProcessor::ValidationError, "Unknown parameter: extra")
end
it "returns errors if there is no any jobs defined" do
config = YAML.dump({ before_script: ["bundle update"] })
expect do
- GitlabCiYamlProcessor.new(config)
+ GitlabCiYamlProcessor.new(config, path)
end.to raise_error(GitlabCiYamlProcessor::ValidationError, "Please define at least one job")
end
it "returns errors if job allow_failure parameter is not an boolean" do
config = YAML.dump({ rspec: { script: "test", allow_failure: "string" } })
expect do
- GitlabCiYamlProcessor.new(config)
+ GitlabCiYamlProcessor.new(config, path)
end.to raise_error(GitlabCiYamlProcessor::ValidationError, "rspec job: allow_failure parameter should be an boolean")
end
it "returns errors if job stage is not a string" do
config = YAML.dump({ rspec: { script: "test", type: 1, allow_failure: "string" } })
expect do
- GitlabCiYamlProcessor.new(config)
+ GitlabCiYamlProcessor.new(config, path)
end.to raise_error(GitlabCiYamlProcessor::ValidationError, "rspec job: stage parameter should be build, test, deploy")
end
it "returns errors if job stage is not a pre-defined stage" do
config = YAML.dump({ rspec: { script: "test", type: "acceptance", allow_failure: "string" } })
expect do
- GitlabCiYamlProcessor.new(config)
+ GitlabCiYamlProcessor.new(config, path)
end.to raise_error(GitlabCiYamlProcessor::ValidationError, "rspec job: stage parameter should be build, test, deploy")
end
it "returns errors if job stage is not a defined stage" do
config = YAML.dump({ types: ["build", "test"], rspec: { script: "test", type: "acceptance", allow_failure: "string" } })
expect do
- GitlabCiYamlProcessor.new(config)
+ GitlabCiYamlProcessor.new(config, path)
end.to raise_error(GitlabCiYamlProcessor::ValidationError, "rspec job: stage parameter should be build, test")
end
it "returns errors if stages is not an array" do
config = YAML.dump({ types: "test", rspec: { script: "test" } })
expect do
- GitlabCiYamlProcessor.new(config)
+ GitlabCiYamlProcessor.new(config, path)
end.to raise_error(GitlabCiYamlProcessor::ValidationError, "stages should be an array of strings")
end
it "returns errors if stages is not an array of strings" do
config = YAML.dump({ types: [true, "test"], rspec: { script: "test" } })
expect do
- GitlabCiYamlProcessor.new(config)
+ GitlabCiYamlProcessor.new(config, path)
end.to raise_error(GitlabCiYamlProcessor::ValidationError, "stages should be an array of strings")
end
it "returns errors if variables is not a map" do
config = YAML.dump({ variables: "test", rspec: { script: "test" } })
expect do
- GitlabCiYamlProcessor.new(config)
+ GitlabCiYamlProcessor.new(config, path)
end.to raise_error(GitlabCiYamlProcessor::ValidationError, "variables should be a map of key-valued strings")
end
it "returns errors if variables is not a map of key-valued strings" do
config = YAML.dump({ variables: { test: false }, rspec: { script: "test" } })
expect do
- GitlabCiYamlProcessor.new(config)
+ GitlabCiYamlProcessor.new(config, path)
end.to raise_error(GitlabCiYamlProcessor::ValidationError, "variables should be a map of key-valued strings")
end
it "returns errors if job when is not on_success, on_failure or always" do
config = YAML.dump({ rspec: { script: "test", when: 1 } })
expect do
- GitlabCiYamlProcessor.new(config)
+ GitlabCiYamlProcessor.new(config, path)
end.to raise_error(GitlabCiYamlProcessor::ValidationError, "rspec job: when parameter should be on_success, on_failure or always")
end
+
+ it "returns errors if job artifacts:untracked is not an array of strings" do
+ config = YAML.dump({ types: ["build", "test"], rspec: { script: "test", artifacts: { untracked: "string" } } })
+ expect do
+ GitlabCiYamlProcessor.new(config)
+ end.to raise_error(GitlabCiYamlProcessor::ValidationError, "rspec job: artifacts:untracked parameter should be an boolean")
+ end
+
+ it "returns errors if job artifacts:paths is not an array of strings" do
+ config = YAML.dump({ types: ["build", "test"], rspec: { script: "test", artifacts: { paths: "string" } } })
+ expect do
+ GitlabCiYamlProcessor.new(config)
+ end.to raise_error(GitlabCiYamlProcessor::ValidationError, "rspec job: artifacts:paths parameter should be an array of strings")
+ end
+
+ it "returns errors if cache:untracked is not an array of strings" do
+ config = YAML.dump({ cache: { untracked: "string" }, rspec: { script: "test" } })
+ expect do
+ GitlabCiYamlProcessor.new(config)
+ end.to raise_error(GitlabCiYamlProcessor::ValidationError, "cache:untracked parameter should be an boolean")
+ end
+
+ it "returns errors if cache:paths is not an array of strings" do
+ config = YAML.dump({ cache: { paths: "string" }, rspec: { script: "test" } })
+ expect do
+ GitlabCiYamlProcessor.new(config)
+ end.to raise_error(GitlabCiYamlProcessor::ValidationError, "cache:paths parameter should be an array of strings")
+ end
+
+ it "returns errors if job cache:untracked is not an array of strings" do
+ config = YAML.dump({ types: ["build", "test"], rspec: { script: "test", cache: { untracked: "string" } } })
+ expect do
+ GitlabCiYamlProcessor.new(config)
+ end.to raise_error(GitlabCiYamlProcessor::ValidationError, "rspec job: cache:untracked parameter should be an boolean")
+ end
+
+ it "returns errors if job cache:paths is not an array of strings" do
+ config = YAML.dump({ types: ["build", "test"], rspec: { script: "test", cache: { paths: "string" } } })
+ expect do
+ GitlabCiYamlProcessor.new(config)
+ end.to raise_error(GitlabCiYamlProcessor::ValidationError, "rspec job: cache:paths parameter should be an array of strings")
+ end
end
end
end
diff --git a/spec/lib/gitlab/diff/inline_diff_spec.rb b/spec/lib/gitlab/inline_diff_spec.rb
index 2e0a05088cc..2e0a05088cc 100644
--- a/spec/lib/gitlab/diff/inline_diff_spec.rb
+++ b/spec/lib/gitlab/inline_diff_spec.rb
diff --git a/spec/lib/gitlab/lfs/lfs_router_spec.rb b/spec/lib/gitlab/lfs/lfs_router_spec.rb
new file mode 100644
index 00000000000..cebcb5bc887
--- /dev/null
+++ b/spec/lib/gitlab/lfs/lfs_router_spec.rb
@@ -0,0 +1,650 @@
+require 'spec_helper'
+
+describe Gitlab::Lfs::Router do
+ let(:project) { create(:project) }
+ let(:public_project) { create(:project, :public) }
+ let(:forked_project) { fork_project(public_project, user) }
+
+ let(:user) { create(:user) }
+ let(:user_two) { create(:user) }
+ let!(:lfs_object) { create(:lfs_object, :with_file) }
+
+ let(:request) { Rack::Request.new(env) }
+ let(:env) do
+ {
+ 'rack.input' => '',
+ 'REQUEST_METHOD' => 'GET',
+ }
+ end
+
+ let(:lfs_router_auth) { new_lfs_router(project, user) }
+ let(:lfs_router_noauth) { new_lfs_router(project, nil) }
+ let(:lfs_router_public_auth) { new_lfs_router(public_project, user) }
+ let(:lfs_router_public_noauth) { new_lfs_router(public_project, nil) }
+ let(:lfs_router_forked_noauth) { new_lfs_router(forked_project, nil) }
+ let(:lfs_router_forked_auth) { new_lfs_router(forked_project, user_two) }
+
+ let(:sample_oid) { "b68143e6463773b1b6c6fd009a76c32aeec041faff32ba2ed42fd7f708a17f80" }
+ let(:sample_size) { 499013 }
+
+ describe 'when lfs is disabled' do
+ before do
+ allow(Gitlab.config.lfs).to receive(:enabled).and_return(false)
+ env["PATH_INFO"] = "#{project.repository.path_with_namespace}.git/info/lfs/objects/#{sample_oid}"
+ end
+
+ it 'responds with 501' do
+ respond_with_disabled = [ 501,
+ { "Content-Type"=>"application/vnd.git-lfs+json" },
+ ["{\"message\":\"Git LFS is not enabled on this GitLab server, contact your admin.\",\"documentation_url\":\"#{Gitlab.config.gitlab.url}/help\"}"]
+ ]
+ expect(lfs_router_auth.try_call).to match_array(respond_with_disabled)
+ end
+ end
+
+ describe 'when fetching lfs object' do
+ before do
+ enable_lfs
+ env['HTTP_ACCEPT'] = "application/vnd.git-lfs+json; charset=utf-8"
+ env["PATH_INFO"] = "#{project.repository.path_with_namespace}.git/info/lfs/objects/#{sample_oid}"
+ end
+
+ describe 'when user is authenticated' do
+ context 'and user has project download access' do
+ before do
+ @auth = authorize(user)
+ env["HTTP_AUTHORIZATION"] = @auth
+ project.lfs_objects << lfs_object
+ project.team << [user, :master]
+ end
+
+ it "responds with status 200" do
+ expect(lfs_router_auth.try_call.first).to eq(200)
+ end
+
+ it "responds with download hypermedia" do
+ json_response = ActiveSupport::JSON.decode(lfs_router_auth.try_call.last.first)
+
+ expect(json_response['_links']['download']['href']).to eq("#{Gitlab.config.gitlab.url}/#{project.path_with_namespace}.git/gitlab-lfs/objects/#{sample_oid}")
+ expect(json_response['_links']['download']['header']).to eq("Authorization" => @auth, "Accept" => "application/vnd.git-lfs+json; charset=utf-8")
+ end
+ end
+
+ context 'and user does not have project access' do
+ it "responds with status 403" do
+ expect(lfs_router_auth.try_call.first).to eq(403)
+ end
+ end
+ end
+
+ describe 'when user is unauthenticated' do
+ context 'and user does not have download access' do
+ it "responds with status 401" do
+ expect(lfs_router_noauth.try_call.first).to eq(401)
+ end
+ end
+
+ context 'and user has download access' do
+ before do
+ project.team << [user, :master]
+ end
+
+ it "responds with status 401" do
+ expect(lfs_router_noauth.try_call.first).to eq(401)
+ end
+ end
+ end
+
+ describe 'and project is public' do
+ context 'and project has access to the lfs object' do
+ before do
+ public_project.lfs_objects << lfs_object
+ end
+
+ context 'and user is authenticated' do
+ it "responds with status 200 and sends download hypermedia" do
+ expect(lfs_router_public_auth.try_call.first).to eq(200)
+ json_response = ActiveSupport::JSON.decode(lfs_router_public_auth.try_call.last.first)
+
+ expect(json_response['_links']['download']['href']).to eq("#{Gitlab.config.gitlab.url}/#{public_project.path_with_namespace}.git/gitlab-lfs/objects/#{sample_oid}")
+ expect(json_response['_links']['download']['header']).to eq("Accept" => "application/vnd.git-lfs+json; charset=utf-8")
+ end
+ end
+
+ context 'and user is unauthenticated' do
+ it "responds with status 200 and sends download hypermedia" do
+ expect(lfs_router_public_noauth.try_call.first).to eq(200)
+ json_response = ActiveSupport::JSON.decode(lfs_router_public_noauth.try_call.last.first)
+
+ expect(json_response['_links']['download']['href']).to eq("#{Gitlab.config.gitlab.url}/#{public_project.path_with_namespace}.git/gitlab-lfs/objects/#{sample_oid}")
+ expect(json_response['_links']['download']['header']).to eq("Accept" => "application/vnd.git-lfs+json; charset=utf-8")
+ end
+ end
+ end
+
+ context 'and project does not have access to the lfs object' do
+ it "responds with status 404" do
+ expect(lfs_router_public_auth.try_call.first).to eq(404)
+ end
+ end
+ end
+
+ describe 'and request comes from gitlab-workhorse' do
+ before do
+ env["PATH_INFO"] = "#{project.repository.path_with_namespace}.git/gitlab-lfs/objects/#{sample_oid}"
+ end
+ context 'without user being authorized' do
+ it "responds with status 401" do
+ expect(lfs_router_noauth.try_call.first).to eq(401)
+ end
+ end
+
+ context 'with required headers' do
+ before do
+ env['HTTP_X_SENDFILE_TYPE'] = "X-Sendfile"
+ end
+
+ context 'when user does not have project access' do
+ it "responds with status 403" do
+ expect(lfs_router_auth.try_call.first).to eq(403)
+ end
+ end
+
+ context 'when user has project access' do
+ before do
+ project.lfs_objects << lfs_object
+ project.team << [user, :master]
+ end
+
+ it "responds with status 200" do
+ expect(lfs_router_auth.try_call.first).to eq(200)
+ end
+
+ it "responds with the file location" do
+ expect(lfs_router_auth.try_call[1]['Content-Type']).to eq("application/octet-stream")
+ expect(lfs_router_auth.try_call[1]['X-Sendfile']).to eq(lfs_object.file.path)
+ end
+ end
+ end
+
+ context 'without required headers' do
+ it "responds with status 403" do
+ expect(lfs_router_auth.try_call.first).to eq(403)
+ end
+ end
+ end
+
+ describe 'from a forked public project' do
+ before do
+ env['HTTP_ACCEPT'] = "application/vnd.git-lfs+json; charset=utf-8"
+ env["PATH_INFO"] = "#{forked_project.repository.path_with_namespace}.git/info/lfs/objects/#{sample_oid}"
+ end
+
+ context "when fetching a lfs object" do
+ context "and user has project download access" do
+ before do
+ public_project.lfs_objects << lfs_object
+ end
+
+ it "can download the lfs object" do
+ expect(lfs_router_forked_auth.try_call.first).to eq(200)
+ json_response = ActiveSupport::JSON.decode(lfs_router_forked_auth.try_call.last.first)
+
+ expect(json_response['_links']['download']['href']).to eq("#{Gitlab.config.gitlab.url}/#{forked_project.path_with_namespace}.git/gitlab-lfs/objects/#{sample_oid}")
+ expect(json_response['_links']['download']['header']).to eq("Accept" => "application/vnd.git-lfs+json; charset=utf-8")
+ end
+ end
+
+ context "and user is not authenticated but project is public" do
+ before do
+ public_project.lfs_objects << lfs_object
+ end
+
+ it "can download the lfs object" do
+ expect(lfs_router_forked_auth.try_call.first).to eq(200)
+ end
+ end
+
+ context "and user has project download access" do
+ before do
+ env["PATH_INFO"] = "#{forked_project.repository.path_with_namespace}.git/info/lfs/objects/91eff75a492a3ed0dfcb544d7f31326bc4014c8551849c192fd1e48d4dd2c897"
+ @auth = ActionController::HttpAuthentication::Basic.encode_credentials(user.username, user.password)
+ env["HTTP_AUTHORIZATION"] = @auth
+ lfs_object_two = create(:lfs_object, :with_file, oid: "91eff75a492a3ed0dfcb544d7f31326bc4014c8551849c192fd1e48d4dd2c897", size: 1575078)
+ public_project.lfs_objects << lfs_object_two
+ end
+
+ it "can get a lfs object that is not in the forked project" do
+ expect(lfs_router_forked_auth.try_call.first).to eq(200)
+
+ json_response = ActiveSupport::JSON.decode(lfs_router_forked_auth.try_call.last.first)
+ expect(json_response['_links']['download']['href']).to eq("#{Gitlab.config.gitlab.url}/#{forked_project.path_with_namespace}.git/gitlab-lfs/objects/91eff75a492a3ed0dfcb544d7f31326bc4014c8551849c192fd1e48d4dd2c897")
+ expect(json_response['_links']['download']['header']).to eq("Accept" => "application/vnd.git-lfs+json; charset=utf-8", "Authorization" => @auth)
+ end
+ end
+
+ context "and user has project download access" do
+ before do
+ env["PATH_INFO"] = "#{forked_project.repository.path_with_namespace}.git/info/lfs/objects/267c8b1d876743971e3a9978405818ff5ca731c4c870b06507619cd9b1847b6b"
+ lfs_object_three = create(:lfs_object, :with_file, oid: "267c8b1d876743971e3a9978405818ff5ca731c4c870b06507619cd9b1847b6b", size: 127192524)
+ project.lfs_objects << lfs_object_three
+ end
+
+ it "cannot get a lfs object that is not in the project" do
+ expect(lfs_router_forked_auth.try_call.first).to eq(404)
+ end
+ end
+ end
+ end
+ end
+
+ describe 'when initiating pushing of the lfs object' do
+ before do
+ enable_lfs
+ env['REQUEST_METHOD'] = 'POST'
+ env["PATH_INFO"] = "#{project.repository.path_with_namespace}.git/info/lfs/objects/batch"
+ end
+
+ describe 'when user is authenticated' do
+ before do
+ body = { 'objects' => [{
+ 'oid' => sample_oid,
+ 'size' => sample_size
+ }]
+ }.to_json
+ env['rack.input'] = StringIO.new(body)
+ end
+
+ describe 'when user has project push access' do
+ before do
+ @auth = authorize(user)
+ env["HTTP_AUTHORIZATION"] = @auth
+ project.team << [user, :master]
+ end
+
+ context 'when pushing an lfs object that already exists' do
+ before do
+ public_project.lfs_objects << lfs_object
+ end
+
+ it "responds with status 200 and links the object to the project" do
+ response_body = lfs_router_auth.try_call.last
+ response = ActiveSupport::JSON.decode(response_body.first)
+
+ expect(response['objects']).to be_kind_of(Array)
+ expect(response['objects'].first['oid']).to eq(sample_oid)
+ expect(response['objects'].first['size']).to eq(sample_size)
+ expect(lfs_object.projects.pluck(:id)).to_not include(project.id)
+ expect(lfs_object.projects.pluck(:id)).to include(public_project.id)
+ expect(response['objects'].first).to have_key('_links')
+ end
+ end
+
+ context 'when pushing a lfs object that does not exist' do
+ before do
+ body = {
+ 'objects' => [{
+ 'oid' => '91eff75a492a3ed0dfcb544d7f31326bc4014c8551849c192fd1e48d4dd2c897',
+ 'size' => 1575078
+ }]
+ }.to_json
+ env['rack.input'] = StringIO.new(body)
+ end
+
+ it "responds with status 200 and upload hypermedia link" do
+ response = lfs_router_auth.try_call
+ expect(response.first).to eq(200)
+
+ response_body = ActiveSupport::JSON.decode(response.last.first)
+ expect(response_body['objects']).to be_kind_of(Array)
+ expect(response_body['objects'].first['oid']).to eq("91eff75a492a3ed0dfcb544d7f31326bc4014c8551849c192fd1e48d4dd2c897")
+ expect(response_body['objects'].first['size']).to eq(1575078)
+ expect(lfs_object.projects.pluck(:id)).not_to include(project.id)
+ expect(response_body['objects'].first['_links']['upload']['href']).to eq("#{Gitlab.config.gitlab.url}/#{project.path_with_namespace}.git/gitlab-lfs/objects/91eff75a492a3ed0dfcb544d7f31326bc4014c8551849c192fd1e48d4dd2c897/1575078")
+ expect(response_body['objects'].first['_links']['upload']['header']).to eq("Authorization" => @auth)
+ end
+ end
+
+ context 'when pushing one new and one existing lfs object' do
+ before do
+ body = {
+ 'objects' => [
+ { 'oid' => '91eff75a492a3ed0dfcb544d7f31326bc4014c8551849c192fd1e48d4dd2c897',
+ 'size' => 1575078
+ },
+ { 'oid' => sample_oid,
+ 'size' => sample_size
+ }
+ ]
+ }.to_json
+ env['rack.input'] = StringIO.new(body)
+ public_project.lfs_objects << lfs_object
+ end
+
+ it "responds with status 200 with upload hypermedia link for the new object" do
+ response = lfs_router_auth.try_call
+ expect(response.first).to eq(200)
+
+ response_body = ActiveSupport::JSON.decode(response.last.first)
+ expect(response_body['objects']).to be_kind_of(Array)
+
+
+ expect(response_body['objects'].first['oid']).to eq("91eff75a492a3ed0dfcb544d7f31326bc4014c8551849c192fd1e48d4dd2c897")
+ expect(response_body['objects'].first['size']).to eq(1575078)
+ expect(response_body['objects'].first['_links']['upload']['href']).to eq("#{Gitlab.config.gitlab.url}/#{project.path_with_namespace}.git/gitlab-lfs/objects/91eff75a492a3ed0dfcb544d7f31326bc4014c8551849c192fd1e48d4dd2c897/1575078")
+ expect(response_body['objects'].first['_links']['upload']['header']).to eq("Authorization" => @auth)
+
+ expect(response_body['objects'].last['oid']).to eq(sample_oid)
+ expect(response_body['objects'].last['size']).to eq(sample_size)
+ expect(lfs_object.projects.pluck(:id)).to_not include(project.id)
+ expect(lfs_object.projects.pluck(:id)).to include(public_project.id)
+ expect(response_body['objects'].last).to have_key('_links')
+ end
+ end
+ end
+
+ context 'when user does not have push access' do
+ it 'responds with 403' do
+ expect(lfs_router_auth.try_call.first).to eq(403)
+ end
+ end
+ end
+
+ context 'when user is not authenticated' do
+ context 'when user has push access' do
+ before do
+ project.team << [user, :master]
+ end
+
+ it "responds with status 401" do
+ expect(lfs_router_public_noauth.try_call.first).to eq(401)
+ end
+ end
+
+ context 'when user does not have push access' do
+ it "responds with status 401" do
+ expect(lfs_router_public_noauth.try_call.first).to eq(401)
+ end
+ end
+ end
+ end
+
+ describe 'when pushing a lfs object' do
+ before do
+ enable_lfs
+ env['REQUEST_METHOD'] = 'PUT'
+ end
+
+ describe 'to one project' do
+ describe 'when user has push access to the project' do
+ before do
+ project.team << [user, :master]
+ end
+
+ describe 'when user is authenticated' do
+ context 'and request is sent by gitlab-workhorse to authorize the request' do
+ before do
+ header_for_upload_authorize(project)
+ end
+
+ it 'responds with status 200, location of lfs store and object details' do
+ json_response = ActiveSupport::JSON.decode(lfs_router_auth.try_call.last.first)
+
+ expect(lfs_router_auth.try_call.first).to eq(200)
+ expect(json_response['StoreLFSPath']).to eq("#{Gitlab.config.shared.path}/lfs-objects/tmp/upload")
+ expect(json_response['LfsOid']).to eq(sample_oid)
+ expect(json_response['LfsSize']).to eq(sample_size)
+ end
+ end
+
+ context 'and request is sent by gitlab-workhorse to finalize the upload' do
+ before do
+ headers_for_upload_finalize(project)
+ end
+
+ it 'responds with status 200 and lfs object is linked to the project' do
+ expect(lfs_router_auth.try_call.first).to eq(200)
+ expect(lfs_object.projects.pluck(:id)).to include(project.id)
+ end
+ end
+ end
+
+ describe 'when user is unauthenticated' do
+ let(:lfs_router_noauth) { new_lfs_router(project, nil) }
+
+ context 'and request is sent by gitlab-workhorse to authorize the request' do
+ before do
+ header_for_upload_authorize(project)
+ end
+
+ it 'responds with status 401' do
+ expect(lfs_router_noauth.try_call.first).to eq(401)
+ end
+ end
+
+ context 'and request is sent by gitlab-workhorse to finalize the upload' do
+ before do
+ headers_for_upload_finalize(project)
+ end
+
+ it 'responds with status 401' do
+ expect(lfs_router_noauth.try_call.first).to eq(401)
+ end
+ end
+
+ context 'and request is sent with a malformed headers' do
+ before do
+ env["PATH_INFO"] = "#{project.repository.path_with_namespace}.git/gitlab-lfs/objects/#{sample_oid}/#{sample_size}"
+ env["HTTP_X_GITLAB_LFS_TMP"] = "cat /etc/passwd"
+ end
+
+ it 'does not recognize it as a valid lfs command' do
+ expect(lfs_router_noauth.try_call).to eq(nil)
+ end
+ end
+ end
+ end
+
+ describe 'and user does not have push access' do
+ describe 'when user is authenticated' do
+ context 'and request is sent by gitlab-workhorse to authorize the request' do
+ before do
+ header_for_upload_authorize(project)
+ end
+
+ it 'responds with 403' do
+ expect(lfs_router_auth.try_call.first).to eq(403)
+ end
+ end
+
+ context 'and request is sent by gitlab-workhorse to finalize the upload' do
+ before do
+ headers_for_upload_finalize(project)
+ end
+
+ it 'responds with 403' do
+ expect(lfs_router_auth.try_call.first).to eq(403)
+ end
+ end
+ end
+
+ describe 'when user is unauthenticated' do
+ let(:lfs_router_noauth) { new_lfs_router(project, nil) }
+
+ context 'and request is sent by gitlab-workhorse to authorize the request' do
+ before do
+ header_for_upload_authorize(project)
+ end
+
+ it 'responds with 401' do
+ expect(lfs_router_noauth.try_call.first).to eq(401)
+ end
+ end
+
+ context 'and request is sent by gitlab-workhorse to finalize the upload' do
+ before do
+ headers_for_upload_finalize(project)
+ end
+
+ it 'responds with 401' do
+ expect(lfs_router_noauth.try_call.first).to eq(401)
+ end
+ end
+ end
+ end
+ end
+
+ describe "to a forked project" do
+ let(:forked_project) { fork_project(public_project, user) }
+
+ describe 'when user has push access to the project' do
+ before do
+ forked_project.team << [user_two, :master]
+ end
+
+ describe 'when user is authenticated' do
+ context 'and request is sent by gitlab-workhorse to authorize the request' do
+ before do
+ header_for_upload_authorize(forked_project)
+ end
+
+ it 'responds with status 200, location of lfs store and object details' do
+ json_response = ActiveSupport::JSON.decode(lfs_router_forked_auth.try_call.last.first)
+
+ expect(lfs_router_forked_auth.try_call.first).to eq(200)
+ expect(json_response['StoreLFSPath']).to eq("#{Gitlab.config.shared.path}/lfs-objects/tmp/upload")
+ expect(json_response['LfsOid']).to eq(sample_oid)
+ expect(json_response['LfsSize']).to eq(sample_size)
+ end
+ end
+
+ context 'and request is sent by gitlab-workhorse to finalize the upload' do
+ before do
+ headers_for_upload_finalize(forked_project)
+ end
+
+ it 'responds with status 200 and lfs object is linked to the source project' do
+ expect(lfs_router_forked_auth.try_call.first).to eq(200)
+ expect(lfs_object.projects.pluck(:id)).to include(public_project.id)
+ end
+ end
+ end
+
+ describe 'when user is unauthenticated' do
+ context 'and request is sent by gitlab-workhorse to authorize the request' do
+ before do
+ header_for_upload_authorize(forked_project)
+ end
+
+ it 'responds with status 401' do
+ expect(lfs_router_forked_noauth.try_call.first).to eq(401)
+ end
+ end
+
+ context 'and request is sent by gitlab-workhorse to finalize the upload' do
+ before do
+ headers_for_upload_finalize(forked_project)
+ end
+
+ it 'responds with status 401' do
+ expect(lfs_router_forked_noauth.try_call.first).to eq(401)
+ end
+ end
+ end
+ end
+
+ describe 'and user does not have push access' do
+ describe 'when user is authenticated' do
+ context 'and request is sent by gitlab-workhorse to authorize the request' do
+ before do
+ header_for_upload_authorize(forked_project)
+ end
+
+ it 'responds with 403' do
+ expect(lfs_router_forked_auth.try_call.first).to eq(403)
+ end
+ end
+
+ context 'and request is sent by gitlab-workhorse to finalize the upload' do
+ before do
+ headers_for_upload_finalize(forked_project)
+ end
+
+ it 'responds with 403' do
+ expect(lfs_router_forked_auth.try_call.first).to eq(403)
+ end
+ end
+ end
+
+ describe 'when user is unauthenticated' do
+ context 'and request is sent by gitlab-workhorse to authorize the request' do
+ before do
+ header_for_upload_authorize(forked_project)
+ end
+
+ it 'responds with 401' do
+ expect(lfs_router_forked_noauth.try_call.first).to eq(401)
+ end
+ end
+
+ context 'and request is sent by gitlab-workhorse to finalize the upload' do
+ before do
+ headers_for_upload_finalize(forked_project)
+ end
+
+ it 'responds with 401' do
+ expect(lfs_router_forked_noauth.try_call.first).to eq(401)
+ end
+ end
+ end
+ end
+
+ describe 'and second project not related to fork or a source project' do
+ let(:second_project) { create(:project) }
+ let(:lfs_router_second_project) { new_lfs_router(second_project, user) }
+
+ before do
+ public_project.lfs_objects << lfs_object
+ headers_for_upload_finalize(second_project)
+ end
+
+ context 'when pushing the same lfs object to the second project' do
+ before do
+ second_project.team << [user, :master]
+ end
+
+ it 'responds with 200 and links the lfs object to the project' do
+ expect(lfs_router_second_project.try_call.first).to eq(200)
+ expect(lfs_object.projects.pluck(:id)).to include(second_project.id, public_project.id)
+ end
+ end
+ end
+ end
+ end
+
+ def enable_lfs
+ allow(Gitlab.config.lfs).to receive(:enabled).and_return(true)
+ end
+
+ def authorize(user)
+ ActionController::HttpAuthentication::Basic.encode_credentials(user.username, user.password)
+ end
+
+ def new_lfs_router(project, user)
+ Gitlab::Lfs::Router.new(project, user, request)
+ end
+
+ def header_for_upload_authorize(project)
+ env["PATH_INFO"] = "#{project.repository.path_with_namespace}.git/gitlab-lfs/objects/#{sample_oid}/#{sample_size}/authorize"
+ end
+
+ def headers_for_upload_finalize(project)
+ env["PATH_INFO"] = "#{project.repository.path_with_namespace}.git/gitlab-lfs/objects/#{sample_oid}/#{sample_size}"
+ env["HTTP_X_GITLAB_LFS_TMP"] = "#{sample_oid}6e561c9d4"
+ end
+
+ def fork_project(project, user, object = nil)
+ allow(RepositoryForkWorker).to receive(:perform_async).and_return(true)
+ Projects::ForkService.new(project, user, {}).execute
+ end
+end
diff --git a/spec/lib/gitlab/markdown/sanitization_filter_spec.rb b/spec/lib/gitlab/markdown/sanitization_filter_spec.rb
index e50c82d0b3c..27cd00e8054 100644
--- a/spec/lib/gitlab/markdown/sanitization_filter_spec.rb
+++ b/spec/lib/gitlab/markdown/sanitization_filter_spec.rb
@@ -44,7 +44,7 @@ module Gitlab::Markdown
instance = described_class.new('Foo')
3.times { instance.whitelist }
- expect(instance.whitelist[:transformers].size).to eq 4
+ expect(instance.whitelist[:transformers].size).to eq 5
end
it 'allows syntax highlighting' do
@@ -77,19 +77,100 @@ module Gitlab::Markdown
end
it 'removes `rel` attribute from `a` elements' do
- doc = filter(%q{<a href="#" rel="nofollow">Link</a>})
+ act = %q{<a href="#" rel="nofollow">Link</a>}
+ exp = %q{<a href="#">Link</a>}
- expect(doc.css('a').size).to eq 1
- expect(doc.at_css('a')['href']).to eq '#'
- expect(doc.at_css('a')['rel']).to be_nil
+ expect(filter(act).to_html).to eq exp
end
- it 'removes script-like `href` attribute from `a` elements' do
- html = %q{<a href="javascript:alert('Hi')">Hi</a>}
- doc = filter(html)
+ # Adapted from the Sanitize test suite: http://git.io/vczrM
+ protocols = {
+ 'protocol-based JS injection: simple, no spaces' => {
+ input: '<a href="javascript:alert(\'XSS\');">foo</a>',
+ output: '<a>foo</a>'
+ },
+
+ 'protocol-based JS injection: simple, spaces before' => {
+ input: '<a href="javascript :alert(\'XSS\');">foo</a>',
+ output: '<a>foo</a>'
+ },
+
+ 'protocol-based JS injection: simple, spaces after' => {
+ input: '<a href="javascript: alert(\'XSS\');">foo</a>',
+ output: '<a>foo</a>'
+ },
+
+ 'protocol-based JS injection: simple, spaces before and after' => {
+ input: '<a href="javascript : alert(\'XSS\');">foo</a>',
+ output: '<a>foo</a>'
+ },
+
+ 'protocol-based JS injection: preceding colon' => {
+ input: '<a href=":javascript:alert(\'XSS\');">foo</a>',
+ output: '<a>foo</a>'
+ },
+
+ 'protocol-based JS injection: UTF-8 encoding' => {
+ input: '<a href="javascript&#58;">foo</a>',
+ output: '<a>foo</a>'
+ },
+
+ 'protocol-based JS injection: long UTF-8 encoding' => {
+ input: '<a href="javascript&#0058;">foo</a>',
+ output: '<a>foo</a>'
+ },
+
+ 'protocol-based JS injection: long UTF-8 encoding without semicolons' => {
+ input: '<a href=&#0000106&#0000097&#0000118&#0000097&#0000115&#0000099&#0000114&#0000105&#0000112&#0000116&#0000058&#0000097&#0000108&#0000101&#0000114&#0000116&#0000040&#0000039&#0000088&#0000083&#0000083&#0000039&#0000041>foo</a>',
+ output: '<a>foo</a>'
+ },
+
+ 'protocol-based JS injection: hex encoding' => {
+ input: '<a href="javascript&#x3A;">foo</a>',
+ output: '<a>foo</a>'
+ },
+
+ 'protocol-based JS injection: long hex encoding' => {
+ input: '<a href="javascript&#x003A;">foo</a>',
+ output: '<a>foo</a>'
+ },
+
+ 'protocol-based JS injection: hex encoding without semicolons' => {
+ input: '<a href=&#x6A&#x61&#x76&#x61&#x73&#x63&#x72&#x69&#x70&#x74&#x3A&#x61&#x6C&#x65&#x72&#x74&#x28&#x27&#x58&#x53&#x53&#x27&#x29>foo</a>',
+ output: '<a>foo</a>'
+ },
+
+ 'protocol-based JS injection: null char' => {
+ input: "<a href=java\0script:alert(\"XSS\")>foo</a>",
+ output: '<a href="java"></a>'
+ },
+
+ 'protocol-based JS injection: spaces and entities' => {
+ input: '<a href=" &#14; javascript:alert(\'XSS\');">foo</a>',
+ output: '<a href="">foo</a>'
+ },
+ }
+
+ protocols.each do |name, data|
+ it "handles #{name}" do
+ doc = filter(data[:input])
+
+ expect(doc.to_html).to eq data[:output]
+ end
+ end
+
+ it 'allows non-standard anchor schemes' do
+ exp = %q{<a href="irc://irc.freenode.net/git">IRC</a>}
+ act = filter(exp)
+
+ expect(act.to_html).to eq exp
+ end
+
+ it 'allows relative links' do
+ exp = %q{<a href="foo/bar.md">foo/bar.md</a>}
+ act = filter(exp)
- expect(doc.css('a').size).to eq 1
- expect(doc.at_css('a')['href']).to be_nil
+ expect(act.to_html).to eq exp
end
end
diff --git a/spec/lib/gitlab/push_data_builder_spec.rb b/spec/lib/gitlab/push_data_builder_spec.rb
index 1b8ba7b4d43..02710742625 100644
--- a/spec/lib/gitlab/push_data_builder_spec.rb
+++ b/spec/lib/gitlab/push_data_builder_spec.rb
@@ -17,6 +17,9 @@ describe 'Gitlab::PushDataBuilder' do
it { expect(data[:repository][:git_ssh_url]).to eq(project.ssh_url_to_repo) }
it { expect(data[:repository][:visibility_level]).to eq(project.visibility_level) }
it { expect(data[:total_commits_count]).to eq(3) }
+ it { expect(data[:added]).to eq(["gitlab-grack"]) }
+ it { expect(data[:modified]).to eq([".gitmodules", "files/ruby/popen.rb", "files/ruby/regex.rb"]) }
+ it { expect(data[:removed]).to eq([]) }
end
describe :build do
@@ -35,5 +38,8 @@ describe 'Gitlab::PushDataBuilder' do
it { expect(data[:ref]).to eq('refs/tags/v1.1.0') }
it { expect(data[:commits]).to be_empty }
it { expect(data[:total_commits_count]).to be_zero }
+ it { expect(data[:added]).to eq([]) }
+ it { expect(data[:modified]).to eq([]) }
+ it { expect(data[:removed]).to eq([]) }
end
end
diff --git a/spec/lib/gitlab/sherlock/collection_spec.rb b/spec/lib/gitlab/sherlock/collection_spec.rb
new file mode 100644
index 00000000000..a8a9d6fc7bc
--- /dev/null
+++ b/spec/lib/gitlab/sherlock/collection_spec.rb
@@ -0,0 +1,82 @@
+require 'spec_helper'
+
+describe Gitlab::Sherlock::Collection do
+ let(:collection) { described_class.new }
+
+ let(:transaction) do
+ Gitlab::Sherlock::Transaction.new('POST', '/cat_pictures')
+ end
+
+ describe '#add' do
+ it 'adds a new transaction' do
+ collection.add(transaction)
+
+ expect(collection).to_not be_empty
+ end
+
+ it 'is aliased as <<' do
+ collection << transaction
+
+ expect(collection).to_not be_empty
+ end
+ end
+
+ describe '#each' do
+ it 'iterates over every transaction' do
+ collection.add(transaction)
+
+ expect { |b| collection.each(&b) }.to yield_with_args(transaction)
+ end
+ end
+
+ describe '#clear' do
+ it 'removes all transactions' do
+ collection.add(transaction)
+
+ collection.clear
+
+ expect(collection).to be_empty
+ end
+ end
+
+ describe '#empty?' do
+ it 'returns true for an empty collection' do
+ expect(collection).to be_empty
+ end
+
+ it 'returns false for a collection with a transaction' do
+ collection.add(transaction)
+
+ expect(collection).to_not be_empty
+ end
+ end
+
+ describe '#find_transaction' do
+ it 'returns the transaction for the given ID' do
+ collection.add(transaction)
+
+ expect(collection.find_transaction(transaction.id)).to eq(transaction)
+ end
+
+ it 'returns nil when no transaction could be found' do
+ collection.add(transaction)
+
+ expect(collection.find_transaction('cats')).to be_nil
+ end
+ end
+
+ describe '#newest_first' do
+ it 'returns transactions sorted from new to old' do
+ trans1 = Gitlab::Sherlock::Transaction.new('POST', '/cat_pictures')
+ trans2 = Gitlab::Sherlock::Transaction.new('POST', '/more_cat_pictures')
+
+ allow(trans1).to receive(:finished_at).and_return(Time.utc(2015, 1, 1))
+ allow(trans2).to receive(:finished_at).and_return(Time.utc(2015, 1, 2))
+
+ collection.add(trans1)
+ collection.add(trans2)
+
+ expect(collection.newest_first).to eq([trans2, trans1])
+ end
+ end
+end
diff --git a/spec/lib/gitlab/sherlock/file_sample_spec.rb b/spec/lib/gitlab/sherlock/file_sample_spec.rb
new file mode 100644
index 00000000000..f05a59f56f6
--- /dev/null
+++ b/spec/lib/gitlab/sherlock/file_sample_spec.rb
@@ -0,0 +1,54 @@
+require 'spec_helper'
+
+describe Gitlab::Sherlock::FileSample do
+ let(:sample) { described_class.new(__FILE__, [], 150.4, 2) }
+
+ describe '#id' do
+ it 'returns the ID' do
+ expect(sample.id).to be_an_instance_of(String)
+ end
+ end
+
+ describe '#file' do
+ it 'returns the file path' do
+ expect(sample.file).to eq(__FILE__)
+ end
+ end
+
+ describe '#line_samples' do
+ it 'returns the line samples' do
+ expect(sample.line_samples).to eq([])
+ end
+ end
+
+ describe '#events' do
+ it 'returns the total number of events' do
+ expect(sample.events).to eq(2)
+ end
+ end
+
+ describe '#duration' do
+ it 'returns the total execution time' do
+ expect(sample.duration).to eq(150.4)
+ end
+ end
+
+ describe '#relative_path' do
+ it 'returns the relative path' do
+ expect(sample.relative_path).
+ to eq('spec/lib/gitlab/sherlock/file_sample_spec.rb')
+ end
+ end
+
+ describe '#to_param' do
+ it 'returns the sample ID' do
+ expect(sample.to_param).to eq(sample.id)
+ end
+ end
+
+ describe '#source' do
+ it 'returns the contents of the file' do
+ expect(sample.source).to eq(File.read(__FILE__))
+ end
+ end
+end
diff --git a/spec/lib/gitlab/sherlock/line_profiler_spec.rb b/spec/lib/gitlab/sherlock/line_profiler_spec.rb
new file mode 100644
index 00000000000..8f2e1299714
--- /dev/null
+++ b/spec/lib/gitlab/sherlock/line_profiler_spec.rb
@@ -0,0 +1,73 @@
+require 'spec_helper'
+
+describe Gitlab::Sherlock::LineProfiler do
+ let(:profiler) { described_class.new }
+
+ describe '#profile' do
+ it 'runs the profiler when using MRI' do
+ allow(profiler).to receive(:mri?).and_return(true)
+ allow(profiler).to receive(:profile_mri)
+
+ profiler.profile { 'cats' }
+ end
+
+ it 'raises NotImplementedError when profiling an unsupported platform' do
+ allow(profiler).to receive(:mri?).and_return(false)
+
+ expect { profiler.profile { 'cats' } }.to raise_error(NotImplementedError)
+ end
+ end
+
+ describe '#profile_mri' do
+ it 'returns an Array containing the return value and profiling samples' do
+ allow(profiler).to receive(:lineprof).
+ and_yield.
+ and_return({ __FILE__ => [[0, 0, 0, 0]] })
+
+ retval, samples = profiler.profile_mri { 42 }
+
+ expect(retval).to eq(42)
+ expect(samples).to eq([])
+ end
+ end
+
+ describe '#aggregate_rblineprof' do
+ let(:raw_samples) do
+ { __FILE__ => [[30000, 30000, 5, 0], [15000, 15000, 4, 0]] }
+ end
+
+ it 'returns an Array of FileSample objects' do
+ samples = profiler.aggregate_rblineprof(raw_samples)
+
+ expect(samples).to be_an_instance_of(Array)
+ expect(samples[0]).to be_an_instance_of(Gitlab::Sherlock::FileSample)
+ end
+
+ describe 'the first FileSample object' do
+ let(:file_sample) do
+ profiler.aggregate_rblineprof(raw_samples)[0]
+ end
+
+ it 'uses the correct file path' do
+ expect(file_sample.file).to eq(__FILE__)
+ end
+
+ it 'contains a list of line samples' do
+ line_sample = file_sample.line_samples[0]
+
+ expect(line_sample).to be_an_instance_of(Gitlab::Sherlock::LineSample)
+
+ expect(line_sample.duration).to eq(15.0)
+ expect(line_sample.events).to eq(4)
+ end
+
+ it 'contains the total file execution time' do
+ expect(file_sample.duration).to eq(30.0)
+ end
+
+ it 'contains the total amount of file events' do
+ expect(file_sample.events).to eq(5)
+ end
+ end
+ end
+end
diff --git a/spec/lib/gitlab/sherlock/line_sample_spec.rb b/spec/lib/gitlab/sherlock/line_sample_spec.rb
new file mode 100644
index 00000000000..5f02f6a3213
--- /dev/null
+++ b/spec/lib/gitlab/sherlock/line_sample_spec.rb
@@ -0,0 +1,33 @@
+require 'spec_helper'
+
+describe Gitlab::Sherlock::LineSample do
+ let(:sample) { described_class.new(150.0, 4) }
+
+ describe '#duration' do
+ it 'returns the duration' do
+ expect(sample.duration).to eq(150.0)
+ end
+ end
+
+ describe '#events' do
+ it 'returns the amount of events' do
+ expect(sample.events).to eq(4)
+ end
+ end
+
+ describe '#percentage_of' do
+ it 'returns the percentage of 1500.0' do
+ expect(sample.percentage_of(1500.0)).to be_within(0.1).of(10.0)
+ end
+ end
+
+ describe '#majority_of' do
+ it 'returns true if the sample takes up the majority of the given duration' do
+ expect(sample.majority_of?(500.0)).to eq(true)
+ end
+
+ it "returns false if the sample doesn't take up the majority of the given duration" do
+ expect(sample.majority_of?(1500.0)).to eq(false)
+ end
+ end
+end
diff --git a/spec/lib/gitlab/sherlock/location_spec.rb b/spec/lib/gitlab/sherlock/location_spec.rb
new file mode 100644
index 00000000000..b295a624b35
--- /dev/null
+++ b/spec/lib/gitlab/sherlock/location_spec.rb
@@ -0,0 +1,40 @@
+require 'spec_helper'
+
+describe Gitlab::Sherlock::Location do
+ let(:location) { described_class.new(__FILE__, 1) }
+
+ describe 'from_ruby_location' do
+ it 'creates a Location from a Thread::Backtrace::Location' do
+ input = caller_locations[0]
+ output = described_class.from_ruby_location(input)
+
+ expect(output).to be_an_instance_of(described_class)
+ expect(output.path).to eq(input.path)
+ expect(output.line).to eq(input.lineno)
+ end
+ end
+
+ describe '#path' do
+ it 'returns the file path' do
+ expect(location.path).to eq(__FILE__)
+ end
+ end
+
+ describe '#line' do
+ it 'returns the line number' do
+ expect(location.line).to eq(1)
+ end
+ end
+
+ describe '#application?' do
+ it 'returns true for an application frame' do
+ expect(location.application?).to eq(true)
+ end
+
+ it 'returns false for a non application frame' do
+ loc = described_class.new('/tmp/cats.rb', 1)
+
+ expect(loc.application?).to eq(false)
+ end
+ end
+end
diff --git a/spec/lib/gitlab/sherlock/middleware_spec.rb b/spec/lib/gitlab/sherlock/middleware_spec.rb
new file mode 100644
index 00000000000..aa74fc53a79
--- /dev/null
+++ b/spec/lib/gitlab/sherlock/middleware_spec.rb
@@ -0,0 +1,79 @@
+require 'spec_helper'
+
+describe Gitlab::Sherlock::Middleware do
+ let(:app) { double(:app) }
+ let(:middleware) { described_class.new(app) }
+
+ describe '#call' do
+ describe 'when instrumentation is enabled' do
+ it 'instruments a request' do
+ allow(middleware).to receive(:instrument?).and_return(true)
+ allow(middleware).to receive(:call_with_instrumentation)
+
+ middleware.call({})
+ end
+ end
+
+ describe 'when instrumentation is disabled' do
+ it "doesn't instrument a request" do
+ allow(middleware).to receive(:instrument).and_return(false)
+ allow(app).to receive(:call)
+
+ middleware.call({})
+ end
+ end
+ end
+
+ describe '#call_with_instrumentation' do
+ it 'instruments a request' do
+ trans = double(:transaction)
+ retval = 'cats are amazing'
+ env = {}
+
+ allow(app).to receive(:call).with(env).and_return(retval)
+ allow(middleware).to receive(:transaction_from_env).and_return(trans)
+ allow(trans).to receive(:run).and_yield.and_return(retval)
+ allow(Gitlab::Sherlock.collection).to receive(:add).with(trans)
+
+ middleware.call_with_instrumentation(env)
+ end
+ end
+
+ describe '#instrument?' do
+ it 'returns false for a text/css request' do
+ env = { 'HTTP_ACCEPT' => 'text/css', 'REQUEST_URI' => '/' }
+
+ expect(middleware.instrument?(env)).to eq(false)
+ end
+
+ it 'returns false for a request to a Sherlock route' do
+ env = {
+ 'HTTP_ACCEPT' => 'text/html',
+ 'REQUEST_URI' => '/sherlock/transactions'
+ }
+
+ expect(middleware.instrument?(env)).to eq(false)
+ end
+
+ it 'returns true for a request that should be instrumented' do
+ env = {
+ 'HTTP_ACCEPT' => 'text/html',
+ 'REQUEST_URI' => '/cats'
+ }
+
+ expect(middleware.instrument?(env)).to eq(true)
+ end
+ end
+
+ describe '#transaction_from_env' do
+ it 'returns a Transaction' do
+ env = {
+ 'HTTP_ACCEPT' => 'text/html',
+ 'REQUEST_URI' => '/cats'
+ }
+
+ expect(middleware.transaction_from_env(env)).
+ to be_an_instance_of(Gitlab::Sherlock::Transaction)
+ end
+ end
+end
diff --git a/spec/lib/gitlab/sherlock/query_spec.rb b/spec/lib/gitlab/sherlock/query_spec.rb
new file mode 100644
index 00000000000..a9afef5dc1d
--- /dev/null
+++ b/spec/lib/gitlab/sherlock/query_spec.rb
@@ -0,0 +1,113 @@
+require 'spec_helper'
+
+describe Gitlab::Sherlock::Query do
+ let(:started_at) { Time.utc(2015, 1, 1) }
+ let(:finished_at) { started_at + 5 }
+
+ let(:query) do
+ described_class.new('SELECT COUNT(*) FROM users', started_at, finished_at)
+ end
+
+ describe 'new_with_bindings' do
+ it 'returns a Query' do
+ sql = 'SELECT COUNT(*) FROM users WHERE id = $1'
+ bindings = [[double(:column), 10]]
+
+ query = described_class.
+ new_with_bindings(sql, bindings, started_at, finished_at)
+
+ expect(query.query).to eq('SELECT COUNT(*) FROM users WHERE id = 10;')
+ end
+ end
+
+ describe '#id' do
+ it 'returns a String' do
+ expect(query.id).to be_an_instance_of(String)
+ end
+ end
+
+ describe '#query' do
+ it 'returns the query with a trailing semi-colon' do
+ expect(query.query).to eq('SELECT COUNT(*) FROM users;')
+ end
+ end
+
+ describe '#started_at' do
+ it 'returns the start time' do
+ expect(query.started_at).to eq(started_at)
+ end
+ end
+
+ describe '#finished_at' do
+ it 'returns the completion time' do
+ expect(query.finished_at).to eq(finished_at)
+ end
+ end
+
+ describe '#backtrace' do
+ it 'returns the backtrace' do
+ expect(query.backtrace).to be_an_instance_of(Array)
+ end
+ end
+
+ describe '#duration' do
+ it 'returns the duration in milliseconds' do
+ expect(query.duration).to be_within(0.1).of(5000.0)
+ end
+ end
+
+ describe '#to_param' do
+ it 'returns the query ID' do
+ expect(query.to_param).to eq(query.id)
+ end
+ end
+
+ describe '#formatted_query' do
+ it 'returns a formatted version of the query' do
+ expect(query.formatted_query).to eq(<<-EOF.strip)
+SELECT COUNT(*)
+FROM users;
+ EOF
+ end
+ end
+
+ describe '#last_application_frame' do
+ it 'returns the last application frame' do
+ frame = query.last_application_frame
+
+ expect(frame).to be_an_instance_of(Gitlab::Sherlock::Location)
+ expect(frame.path).to eq(__FILE__)
+ end
+ end
+
+ describe '#application_backtrace' do
+ it 'returns an Array of application frames' do
+ frames = query.application_backtrace
+
+ expect(frames).to be_an_instance_of(Array)
+ expect(frames).to_not be_empty
+
+ frames.each do |frame|
+ expect(frame.path).to start_with(Rails.root.to_s)
+ end
+ end
+ end
+
+ describe '#explain' do
+ it 'returns the query plan as a String' do
+ lines = [
+ ['Aggregate (cost=123 rows=1)'],
+ [' -> Index Only Scan using index_cats_are_amazing']
+ ]
+
+ result = double(:result, values: lines)
+
+ allow(query).to receive(:raw_explain).and_return(result)
+
+ expect(query.explain).to eq(<<-EOF.strip)
+Aggregate (cost=123 rows=1)
+ -> Index Only Scan using index_cats_are_amazing
+ EOF
+ end
+ end
+end
diff --git a/spec/lib/gitlab/sherlock/transaction_spec.rb b/spec/lib/gitlab/sherlock/transaction_spec.rb
new file mode 100644
index 00000000000..bb49fb65cf8
--- /dev/null
+++ b/spec/lib/gitlab/sherlock/transaction_spec.rb
@@ -0,0 +1,222 @@
+require 'spec_helper'
+
+describe Gitlab::Sherlock::Transaction do
+ let(:transaction) { described_class.new('POST', '/cat_pictures') }
+
+ describe '#id' do
+ it 'returns the transaction ID' do
+ expect(transaction.id).to be_an_instance_of(String)
+ end
+ end
+
+ describe '#type' do
+ it 'returns the type' do
+ expect(transaction.type).to eq('POST')
+ end
+ end
+
+ describe '#path' do
+ it 'returns the path' do
+ expect(transaction.path).to eq('/cat_pictures')
+ end
+ end
+
+ describe '#queries' do
+ it 'returns an Array of queries' do
+ expect(transaction.queries).to be_an_instance_of(Array)
+ end
+ end
+
+ describe '#file_samples' do
+ it 'returns an Array of file samples' do
+ expect(transaction.file_samples).to be_an_instance_of(Array)
+ end
+ end
+
+ describe '#started_at' do
+ it 'returns the start time' do
+ allow(transaction).to receive(:profile_lines).and_yield
+
+ transaction.run { 'cats are amazing' }
+
+ expect(transaction.started_at).to be_an_instance_of(Time)
+ end
+ end
+
+ describe '#finished_at' do
+ it 'returns the completion time' do
+ allow(transaction).to receive(:profile_lines).and_yield
+
+ transaction.run { 'cats are amazing' }
+
+ expect(transaction.finished_at).to be_an_instance_of(Time)
+ end
+ end
+
+ describe '#view_counts' do
+ it 'returns a Hash' do
+ expect(transaction.view_counts).to be_an_instance_of(Hash)
+ end
+
+ it 'sets the default value of a key to 0' do
+ expect(transaction.view_counts['cats.rb']).to be_zero
+ end
+ end
+
+ describe '#run' do
+ it 'runs the transaction' do
+ allow(transaction).to receive(:profile_lines).and_yield
+
+ retval = transaction.run { 'cats are amazing' }
+
+ expect(retval).to eq('cats are amazing')
+ end
+ end
+
+ describe '#duration' do
+ it 'returns the duration in seconds' do
+ start_time = Time.now
+
+ allow(transaction).to receive(:started_at).and_return(start_time)
+ allow(transaction).to receive(:finished_at).and_return(start_time + 5)
+
+ expect(transaction.duration).to be_within(0.1).of(5.0)
+ end
+ end
+
+ describe '#to_param' do
+ it 'returns the transaction ID' do
+ expect(transaction.to_param).to eq(transaction.id)
+ end
+ end
+
+ describe '#sorted_queries' do
+ it 'returns the queries in descending order' do
+ start_time = Time.now
+
+ query1 = Gitlab::Sherlock::Query.new('SELECT 1', start_time, start_time)
+
+ query2 = Gitlab::Sherlock::Query.
+ new('SELECT 2', start_time, start_time + 5)
+
+ transaction.queries << query1
+ transaction.queries << query2
+
+ expect(transaction.sorted_queries).to eq([query2, query1])
+ end
+ end
+
+ describe '#sorted_file_samples' do
+ it 'returns the file samples in descending order' do
+ sample1 = Gitlab::Sherlock::FileSample.new(__FILE__, [], 10.0, 1)
+ sample2 = Gitlab::Sherlock::FileSample.new(__FILE__, [], 15.0, 1)
+
+ transaction.file_samples << sample1
+ transaction.file_samples << sample2
+
+ expect(transaction.sorted_file_samples).to eq([sample2, sample1])
+ end
+ end
+
+ describe '#find_query' do
+ it 'returns a Query when found' do
+ query = Gitlab::Sherlock::Query.new('SELECT 1', Time.now, Time.now)
+
+ transaction.queries << query
+
+ expect(transaction.find_query(query.id)).to eq(query)
+ end
+
+ it 'returns nil when no query could be found' do
+ expect(transaction.find_query('cats')).to be_nil
+ end
+ end
+
+ describe '#find_file_sample' do
+ it 'returns a FileSample when found' do
+ sample = Gitlab::Sherlock::FileSample.new(__FILE__, [], 10.0, 1)
+
+ transaction.file_samples << sample
+
+ expect(transaction.find_file_sample(sample.id)).to eq(sample)
+ end
+
+ it 'returns nil when no file sample could be found' do
+ expect(transaction.find_file_sample('cats')).to be_nil
+ end
+ end
+
+ describe '#profile_lines' do
+ describe 'when line profiling is enabled' do
+ it 'yields the block using the line profiler' do
+ allow(Gitlab::Sherlock).to receive(:enable_line_profiler?).
+ and_return(true)
+
+ allow_any_instance_of(Gitlab::Sherlock::LineProfiler).
+ to receive(:profile).and_return('cats are amazing', [])
+
+ retval = transaction.profile_lines { 'cats are amazing' }
+
+ expect(retval).to eq('cats are amazing')
+ end
+ end
+
+ describe 'when line profiling is disabled' do
+ it 'yields the block' do
+ allow(Gitlab::Sherlock).to receive(:enable_line_profiler?).
+ and_return(false)
+
+ retval = transaction.profile_lines { 'cats are amazing' }
+
+ expect(retval).to eq('cats are amazing')
+ end
+ end
+ end
+
+ describe '#subscribe_to_active_record' do
+ let(:subscription) { transaction.subscribe_to_active_record }
+ let(:time) { Time.now }
+ let(:query_data) { { sql: 'SELECT 1', binds: [] } }
+
+ after do
+ ActiveSupport::Notifications.unsubscribe(subscription)
+ end
+
+ it 'tracks executed queries' do
+ expect(transaction).to receive(:track_query).
+ with('SELECT 1', [], time, time)
+
+ subscription.publish('test', time, time, nil, query_data)
+ end
+
+ it 'only tracks queries triggered from the transaction thread' do
+ expect(transaction).to_not receive(:track_query)
+
+ Thread.new { subscription.publish('test', time, time, nil, query_data) }.
+ join
+ end
+ end
+
+ describe '#subscribe_to_action_view' do
+ let(:subscription) { transaction.subscribe_to_action_view }
+ let(:time) { Time.now }
+ let(:view_data) { { identifier: 'foo.rb' } }
+
+ after do
+ ActiveSupport::Notifications.unsubscribe(subscription)
+ end
+
+ it 'tracks rendered views' do
+ expect(transaction).to receive(:track_view).with('foo.rb')
+
+ subscription.publish('test', time, time, nil, view_data)
+ end
+
+ it 'only tracks views rendered from the transaction thread' do
+ expect(transaction).to_not receive(:track_view)
+
+ Thread.new { subscription.publish('test', time, time, nil, view_data) }.
+ join
+ end
+ end
+end