diff options
Diffstat (limited to 'spec/models')
-rw-r--r-- | spec/models/application_setting_spec.rb | 59 | ||||
-rw-r--r-- | spec/models/ci/pipeline_spec.rb | 98 | ||||
-rw-r--r-- | spec/models/ci/runner_spec.rb | 2 | ||||
-rw-r--r-- | spec/models/clusters/cluster_spec.rb | 1 | ||||
-rw-r--r-- | spec/models/clusters/platforms/kubernetes_spec.rb | 192 | ||||
-rw-r--r-- | spec/models/concerns/has_variable_spec.rb | 18 | ||||
-rw-r--r-- | spec/models/concerns/issuable_spec.rb | 18 | ||||
-rw-r--r-- | spec/models/environment_spec.rb | 51 | ||||
-rw-r--r-- | spec/models/merge_request_diff_spec.rb | 76 | ||||
-rw-r--r-- | spec/models/merge_request_spec.rb | 21 | ||||
-rw-r--r-- | spec/models/project_services/kubernetes_service_spec.rb | 4 | ||||
-rw-r--r-- | spec/models/project_spec.rb | 47 | ||||
-rw-r--r-- | spec/models/snippet_spec.rb | 2 |
13 files changed, 397 insertions, 192 deletions
diff --git a/spec/models/application_setting_spec.rb b/spec/models/application_setting_spec.rb index 51bf4e65e5d..0b7e16cc33c 100644 --- a/spec/models/application_setting_spec.rb +++ b/spec/models/application_setting_spec.rb @@ -219,6 +219,65 @@ describe ApplicationSetting do expect(subject).to be_valid end end + + context 'gitaly timeouts' do + [:gitaly_timeout_default, :gitaly_timeout_medium, :gitaly_timeout_fast].each do |timeout_name| + it do + is_expected.to validate_presence_of(timeout_name) + is_expected.to validate_numericality_of(timeout_name).only_integer + .is_greater_than_or_equal_to(0) + end + end + + [:gitaly_timeout_medium, :gitaly_timeout_fast].each do |timeout_name| + it "validates that #{timeout_name} is lower than timeout_default" do + subject[:gitaly_timeout_default] = 50 + subject[timeout_name] = 100 + + expect(subject).to be_invalid + end + end + + it 'accepts all timeouts equal' do + subject.gitaly_timeout_default = 0 + subject.gitaly_timeout_medium = 0 + subject.gitaly_timeout_fast = 0 + + expect(subject).to be_valid + end + + it 'accepts timeouts in descending order' do + subject.gitaly_timeout_default = 50 + subject.gitaly_timeout_medium = 30 + subject.gitaly_timeout_fast = 20 + + expect(subject).to be_valid + end + + it 'rejects timeouts in ascending order' do + subject.gitaly_timeout_default = 20 + subject.gitaly_timeout_medium = 30 + subject.gitaly_timeout_fast = 50 + + expect(subject).to be_invalid + end + + it 'rejects medium timeout larger than default' do + subject.gitaly_timeout_default = 30 + subject.gitaly_timeout_medium = 50 + subject.gitaly_timeout_fast = 20 + + expect(subject).to be_invalid + end + + it 'rejects medium timeout smaller than fast' do + subject.gitaly_timeout_default = 30 + subject.gitaly_timeout_medium = 15 + subject.gitaly_timeout_fast = 20 + + expect(subject).to be_invalid + end + end end describe '.current' do diff --git a/spec/models/ci/pipeline_spec.rb b/spec/models/ci/pipeline_spec.rb index 3a19a0753e2..d4b1e7c8dd4 100644 --- a/spec/models/ci/pipeline_spec.rb +++ b/spec/models/ci/pipeline_spec.rb @@ -557,10 +557,23 @@ describe Ci::Pipeline, :mailer do describe '#has_kubernetes_active?' do context 'when kubernetes is active' do - let(:project) { create(:kubernetes_project) } + shared_examples 'same behavior between KubernetesService and Platform::Kubernetes' do + it 'returns true' do + expect(pipeline).to have_kubernetes_active + end + end - it 'returns true' do - expect(pipeline).to have_kubernetes_active + context 'when user configured kubernetes from Integration > Kubernetes' do + let(:project) { create(:kubernetes_project) } + + it_behaves_like 'same behavior between KubernetesService and Platform::Kubernetes' + end + + context 'when user configured kubernetes from CI/CD > Clusters' do + let!(:cluster) { create(:cluster, :project, :provided_by_gcp) } + let(:project) { cluster.project } + + it_behaves_like 'same behavior between KubernetesService and Platform::Kubernetes' end end @@ -855,62 +868,59 @@ describe Ci::Pipeline, :mailer do end describe '#set_config_source' do - context 'on object initialisation' do - context 'when pipelines does not contain needed data' do - let(:pipeline) do - Ci::Pipeline.new - end + context 'when pipelines does not contain needed data' do + it 'defines source to be unknown' do + pipeline.set_config_source - it 'defines source to be unknown' do - expect(pipeline).to be_unknown_source - end + expect(pipeline).to be_unknown_source end + end - context 'when pipeline contains all needed data' do - let(:pipeline) do - Ci::Pipeline.new( - project: project, - sha: '1234', - ref: 'master', - source: :push) + context 'when pipeline contains all needed data' do + let(:pipeline) do + create(:ci_pipeline, project: project, + sha: '1234', + ref: 'master', + source: :push) + end + + context 'when the repository has a config file' do + before do + allow(project.repository).to receive(:gitlab_ci_yml_for) + .and_return('config') end - context 'when the repository has a config file' do - before do - allow(project.repository).to receive(:gitlab_ci_yml_for) - .and_return('config') - end + it 'defines source to be from repository' do + pipeline.set_config_source - it 'defines source to be from repository' do - expect(pipeline).to be_repository_source - end + expect(pipeline).to be_repository_source + end - context 'when loading an object' do - let(:new_pipeline) { Ci::Pipeline.find(pipeline.id) } + context 'when loading an object' do + let(:new_pipeline) { Ci::Pipeline.find(pipeline.id) } - it 'does not redefine the source' do - # force to overwrite the source - pipeline.unknown_source! + it 'does not redefine the source' do + # force to overwrite the source + pipeline.unknown_source! - expect(new_pipeline).to be_unknown_source - end + expect(new_pipeline).to be_unknown_source end end + end - context 'when the repository does not have a config file' do - let(:implied_yml) { Gitlab::Template::GitlabCiYmlTemplate.find('Auto-DevOps').content } + context 'when the repository does not have a config file' do + let(:implied_yml) { Gitlab::Template::GitlabCiYmlTemplate.find('Auto-DevOps').content } - context 'auto devops enabled' do - before do - stub_application_setting(auto_devops_enabled: true) - allow(project).to receive(:ci_config_path) { 'custom' } - end + context 'auto devops enabled' do + before do + stub_application_setting(auto_devops_enabled: true) + allow(project).to receive(:ci_config_path) { 'custom' } + end - it 'defines source to be auto devops' do - subject + it 'defines source to be auto devops' do + pipeline.set_config_source - expect(pipeline).to be_auto_devops_source - end + expect(pipeline).to be_auto_devops_source end end end diff --git a/spec/models/ci/runner_spec.rb b/spec/models/ci/runner_spec.rb index 584dfe9a5c1..a93e7e233a8 100644 --- a/spec/models/ci/runner_spec.rb +++ b/spec/models/ci/runner_spec.rb @@ -473,7 +473,7 @@ describe Ci::Runner do end describe '.search' do - let(:runner) { create(:ci_runner, token: '123abc') } + let(:runner) { create(:ci_runner, token: '123abc', description: 'test runner') } it 'returns runners with a matching token' do expect(described_class.search(runner.token)).to eq([runner]) diff --git a/spec/models/clusters/cluster_spec.rb b/spec/models/clusters/cluster_spec.rb index beb0e83f97a..2683d21ddbe 100644 --- a/spec/models/clusters/cluster_spec.rb +++ b/spec/models/clusters/cluster_spec.rb @@ -9,7 +9,6 @@ describe Clusters::Cluster do it { is_expected.to delegate_method(:status_reason).to(:provider) } it { is_expected.to delegate_method(:status_name).to(:provider) } it { is_expected.to delegate_method(:on_creation?).to(:provider) } - it { is_expected.to delegate_method(:update_kubernetes_integration!).to(:platform) } it { is_expected.to respond_to :project } describe '.enabled' do diff --git a/spec/models/clusters/platforms/kubernetes_spec.rb b/spec/models/clusters/platforms/kubernetes_spec.rb index ed76be703a5..53a4e545ff6 100644 --- a/spec/models/clusters/platforms/kubernetes_spec.rb +++ b/spec/models/clusters/platforms/kubernetes_spec.rb @@ -5,6 +5,8 @@ describe Clusters::Platforms::Kubernetes, :use_clean_rails_memory_store_caching include ReactiveCachingHelpers it { is_expected.to belong_to(:cluster) } + it { is_expected.to be_kind_of(Gitlab::Kubernetes) } + it { is_expected.to be_kind_of(ReactiveCaching) } it { is_expected.to respond_to :ca_pem } describe 'before_validation' do @@ -90,99 +92,175 @@ describe Clusters::Platforms::Kubernetes, :use_clean_rails_memory_store_caching end end - describe 'after_save from Clusters::Cluster' do - context 'when platform_kubernetes is being cerated' do - let(:enabled) { true } - let(:project) { create(:project) } - let(:cluster) { build(:cluster, provider_type: :gcp, platform_type: :kubernetes, platform_kubernetes: platform, provider_gcp: provider, enabled: enabled, projects: [project]) } - let(:platform) { build(:cluster_platform_kubernetes, :configured) } - let(:provider) { build(:cluster_provider_gcp) } - let(:kubernetes_service) { project.kubernetes_service } + describe '#actual_namespace' do + subject { kubernetes.actual_namespace } - it 'updates KubernetesService' do - cluster.save! + let!(:cluster) { create(:cluster, :project, platform_kubernetes: kubernetes) } + let(:project) { cluster.project } + let(:kubernetes) { create(:cluster_platform_kubernetes, :configured, namespace: namespace) } - expect(kubernetes_service.active).to eq(enabled) - expect(kubernetes_service.api_url).to eq(platform.api_url) - expect(kubernetes_service.namespace).to eq(platform.namespace) - expect(kubernetes_service.ca_pem).to eq(platform.ca_cert) - end + context 'when namespace is present' do + let(:namespace) { 'namespace-123' } + + it { is_expected.to eq(namespace) } end - context 'when platform_kubernetes has been created' do - let(:enabled) { false } - let!(:project) { create(:project) } - let!(:cluster) { create(:cluster, :provided_by_gcp, projects: [project]) } - let(:platform) { cluster.platform } - let(:kubernetes_service) { project.kubernetes_service } + context 'when namespace is not present' do + let(:namespace) { nil } + + it { is_expected.to eq("#{project.path}-#{project.id}") } + end + end - it 'updates KubernetesService' do - cluster.update(enabled: enabled) + describe '#default_namespace' do + subject { kubernetes.send(:default_namespace) } - expect(kubernetes_service.active).to eq(enabled) + let(:kubernetes) { create(:cluster_platform_kubernetes, :configured) } + + context 'when cluster belongs to a project' do + let!(:cluster) { create(:cluster, :project, platform_kubernetes: kubernetes) } + let(:project) { cluster.project } + + it { is_expected.to eq("#{project.path}-#{project.id}") } + end + + context 'when cluster belongs to nothing' do + let!(:cluster) { create(:cluster, platform_kubernetes: kubernetes) } + + it { is_expected.to be_nil } + end + end + + describe '#predefined_variables' do + let!(:cluster) { create(:cluster, :project, platform_kubernetes: kubernetes) } + let(:kubernetes) { create(:cluster_platform_kubernetes, api_url: api_url, ca_cert: ca_pem, token: token) } + let(:api_url) { 'https://kube.domain.com' } + let(:ca_pem) { 'CA PEM DATA' } + let(:token) { 'token' } + + let(:kubeconfig) do + config_file = expand_fixture_path('config/kubeconfig.yml') + config = YAML.load(File.read(config_file)) + config.dig('users', 0, 'user')['token'] = token + config.dig('contexts', 0, 'context')['namespace'] = namespace + config.dig('clusters', 0, 'cluster')['certificate-authority-data'] = + Base64.strict_encode64(ca_pem) + + YAML.dump(config) + end + + shared_examples 'setting variables' do + it 'sets the variables' do + expect(kubernetes.predefined_variables).to include( + { key: 'KUBE_URL', value: api_url, public: true }, + { key: 'KUBE_TOKEN', value: token, public: false }, + { key: 'KUBE_NAMESPACE', value: namespace, public: true }, + { key: 'KUBECONFIG', value: kubeconfig, public: false, file: true }, + { key: 'KUBE_CA_PEM', value: ca_pem, public: true }, + { key: 'KUBE_CA_PEM_FILE', value: ca_pem, public: true, file: true } + ) end end - context 'when kubernetes_service has been configured without cluster integration' do - let!(:project) { create(:project) } - let(:cluster) { build(:cluster, provider_type: :gcp, platform_type: :kubernetes, platform_kubernetes: platform, provider_gcp: provider, projects: [project]) } - let(:platform) { build(:cluster_platform_kubernetes, :configured, api_url: 'https://111.111.111.111') } - let(:provider) { build(:cluster_provider_gcp) } + context 'namespace is provided' do + let(:namespace) { 'my-project' } before do - create(:kubernetes_service, project: project) + kubernetes.namespace = namespace end - it 'raises an error' do - expect { cluster.save! }.to raise_error('Kubernetes service already configured') + it_behaves_like 'setting variables' + end + + context 'no namespace provided' do + let(:namespace) { kubernetes.actual_namespace } + + it_behaves_like 'setting variables' + + it 'sets the KUBE_NAMESPACE' do + kube_namespace = kubernetes.predefined_variables.find { |h| h[:key] == 'KUBE_NAMESPACE' } + + expect(kube_namespace).not_to be_nil + expect(kube_namespace[:value]).to match(/\A#{Gitlab::PathRegex::PATH_REGEX_STR}-\d+\z/) end end end - describe '#actual_namespace' do - subject { kubernetes.actual_namespace } + describe '#terminals' do + subject { service.terminals(environment) } - let!(:cluster) { create(:cluster, :project, platform_kubernetes: kubernetes) } + let!(:cluster) { create(:cluster, :project, platform_kubernetes: service) } let(:project) { cluster.project } - let(:kubernetes) { create(:cluster_platform_kubernetes, :configured, namespace: namespace) } + let(:service) { create(:cluster_platform_kubernetes, :configured) } + let(:environment) { build(:environment, project: project, name: "env", slug: "env-000000") } - context 'when namespace is present' do - let(:namespace) { 'namespace-123' } + context 'with invalid pods' do + it 'returns no terminals' do + stub_reactive_cache(service, pods: [{ "bad" => "pod" }]) - it { is_expected.to eq(namespace) } + is_expected.to be_empty + end end - context 'when namespace is not present' do - let(:namespace) { nil } + context 'with valid pods' do + let(:pod) { kube_pod(app: environment.slug) } + let(:terminals) { kube_terminals(service, pod) } - it { is_expected.to eq("#{project.path}-#{project.id}") } + before do + stub_reactive_cache( + service, + pods: [pod, pod, kube_pod(app: "should-be-filtered-out")] + ) + end + + it 'returns terminals' do + is_expected.to eq(terminals + terminals) + end + + it 'uses max session time from settings' do + stub_application_setting(terminal_max_session_time: 600) + + times = subject.map { |terminal| terminal[:max_session_time] } + expect(times).to eq [600, 600, 600, 600] + end end end - describe '.namespace_for_project' do - subject { described_class.namespace_for_project(project) } + describe '#calculate_reactive_cache' do + subject { service.calculate_reactive_cache } - let(:project) { create(:project) } + let!(:cluster) { create(:cluster, :project, enabled: enabled, platform_kubernetes: service) } + let(:service) { create(:cluster_platform_kubernetes, :configured) } + let(:enabled) { true } - it { is_expected.to eq("#{project.path}-#{project.id}") } - end + context 'when cluster is disabled' do + let(:enabled) { false } - describe '#default_namespace' do - subject { kubernetes.default_namespace } + it { is_expected.to be_nil } + end - let(:kubernetes) { create(:cluster_platform_kubernetes, :configured) } + context 'when kubernetes responds with valid pods' do + before do + stub_kubeclient_pods + end - context 'when cluster belongs to a project' do - let!(:cluster) { create(:cluster, :project, platform_kubernetes: kubernetes) } - let(:project) { cluster.project } + it { is_expected.to eq(pods: [kube_pod]) } + end - it { is_expected.to eq("#{project.path}-#{project.id}") } + context 'when kubernetes responds with 500s' do + before do + stub_kubeclient_pods(status: 500) + end + + it { expect { subject }.to raise_error(KubeException) } end - context 'when cluster belongs to nothing' do - let!(:cluster) { create(:cluster, platform_kubernetes: kubernetes) } + context 'when kubernetes responds with 404s' do + before do + stub_kubeclient_pods(status: 404) + end - it { is_expected.to be_nil } + it { is_expected.to eq(pods: []) } end end end diff --git a/spec/models/concerns/has_variable_spec.rb b/spec/models/concerns/has_variable_spec.rb index f4b24e6d1d9..f87869a2fdc 100644 --- a/spec/models/concerns/has_variable_spec.rb +++ b/spec/models/concerns/has_variable_spec.rb @@ -9,6 +9,24 @@ describe HasVariable do it { is_expected.not_to allow_value('foo bar').for(:key) } it { is_expected.not_to allow_value('foo/bar').for(:key) } + describe '#key=' do + context 'when the new key is nil' do + it 'strips leading and trailing whitespaces' do + subject.key = nil + + expect(subject.key).to eq('') + end + end + + context 'when the new key has leadind and trailing whitespaces' do + it 'strips leading and trailing whitespaces' do + subject.key = ' my key ' + + expect(subject.key).to eq('my key') + end + end + end + describe '#value' do before do subject.value = 'secret' diff --git a/spec/models/concerns/issuable_spec.rb b/spec/models/concerns/issuable_spec.rb index 4dfbb14952e..a53b59c4e08 100644 --- a/spec/models/concerns/issuable_spec.rb +++ b/spec/models/concerns/issuable_spec.rb @@ -67,6 +67,7 @@ describe Issuable do describe ".search" do let!(:searchable_issue) { create(:issue, title: "Searchable awesome issue") } + let!(:searchable_issue2) { create(:issue, title: 'Aw') } it 'returns issues with a matching title' do expect(issuable_class.search(searchable_issue.title)) @@ -86,8 +87,8 @@ describe Issuable do expect(issuable_class.search('searchable issue')).to eq([searchable_issue]) end - it 'returns all issues with a query shorter than 3 chars' do - expect(issuable_class.search('zz')).to eq(issuable_class.all) + it 'returns issues with a matching title for a query shorter than 3 chars' do + expect(issuable_class.search(searchable_issue2.title.downcase)).to eq([searchable_issue2]) end end @@ -95,6 +96,7 @@ describe Issuable do let!(:searchable_issue) do create(:issue, title: "Searchable awesome issue", description: 'Many cute kittens') end + let!(:searchable_issue2) { create(:issue, title: "Aw", description: "Cu") } it 'returns issues with a matching title' do expect(issuable_class.full_search(searchable_issue.title)) @@ -133,8 +135,8 @@ describe Issuable do expect(issuable_class.full_search('many kittens')).to eq([searchable_issue]) end - it 'returns all issues with a query shorter than 3 chars' do - expect(issuable_class.search('zz')).to eq(issuable_class.all) + it 'returns issues with a matching description for a query shorter than 3 chars' do + expect(issuable_class.full_search(searchable_issue2.description.downcase)).to eq([searchable_issue2]) end end @@ -283,7 +285,7 @@ describe Issuable do 'labels' => [[labels[0].hook_attrs], [labels[1].hook_attrs]] )) - issue.to_hook_data(user, old_labels: [labels[0]]) + issue.to_hook_data(user, old_associations: { labels: [labels[0]] }) end end @@ -302,7 +304,7 @@ describe Issuable do 'total_time_spent' => [1, 2] )) - issue.to_hook_data(user, old_total_time_spent: 1) + issue.to_hook_data(user, old_associations: { total_time_spent: 1 }) end end @@ -322,7 +324,7 @@ describe Issuable do 'assignees' => [[user.hook_attrs], [user.hook_attrs, user2.hook_attrs]] )) - issue.to_hook_data(user, old_assignees: [user]) + issue.to_hook_data(user, old_associations: { assignees: [user] }) end end @@ -345,7 +347,7 @@ describe Issuable do 'assignee' => [user.hook_attrs, user2.hook_attrs] )) - merge_request.to_hook_data(user, old_assignees: [user]) + merge_request.to_hook_data(user, old_associations: { assignees: [user] }) end end end diff --git a/spec/models/environment_spec.rb b/spec/models/environment_spec.rb index 1ce1d595c60..6f24a039998 100644 --- a/spec/models/environment_spec.rb +++ b/spec/models/environment_spec.rb @@ -327,15 +327,28 @@ describe Environment do context 'when the enviroment is available' do context 'with a deployment service' do - let(:project) { create(:kubernetes_project) } + shared_examples 'same behavior between KubernetesService and Platform::Kubernetes' do + context 'and a deployment' do + let!(:deployment) { create(:deployment, environment: environment) } + it { is_expected.to be_truthy } + end - context 'and a deployment' do - let!(:deployment) { create(:deployment, environment: environment) } - it { is_expected.to be_truthy } + context 'but no deployments' do + it { is_expected.to be_falsy } + end end - context 'but no deployments' do - it { is_expected.to be_falsy } + context 'when user configured kubernetes from Integration > Kubernetes' do + let(:project) { create(:kubernetes_project) } + + it_behaves_like 'same behavior between KubernetesService and Platform::Kubernetes' + end + + context 'when user configured kubernetes from CI/CD > Clusters' do + let!(:cluster) { create(:cluster, :project, :provided_by_gcp) } + let(:project) { cluster.project } + + it_behaves_like 'same behavior between KubernetesService and Platform::Kubernetes' end end @@ -356,7 +369,6 @@ describe Environment do end describe '#terminals' do - let(:project) { create(:kubernetes_project) } subject { environment.terminals } context 'when the environment has terminals' do @@ -364,12 +376,27 @@ describe Environment do allow(environment).to receive(:has_terminals?).and_return(true) end - it 'returns the terminals from the deployment service' do - expect(project.deployment_service) - .to receive(:terminals).with(environment) - .and_return(:fake_terminals) + shared_examples 'same behavior between KubernetesService and Platform::Kubernetes' do + it 'returns the terminals from the deployment service' do + expect(project.deployment_platform) + .to receive(:terminals).with(environment) + .and_return(:fake_terminals) + + is_expected.to eq(:fake_terminals) + end + end + + context 'when user configured kubernetes from Integration > Kubernetes' do + let(:project) { create(:kubernetes_project) } + + it_behaves_like 'same behavior between KubernetesService and Platform::Kubernetes' + end + + context 'when user configured kubernetes from CI/CD > Clusters' do + let!(:cluster) { create(:cluster, :project, :provided_by_gcp) } + let(:project) { cluster.project } - is_expected.to eq(:fake_terminals) + it_behaves_like 'same behavior between KubernetesService and Platform::Kubernetes' end end diff --git a/spec/models/merge_request_diff_spec.rb b/spec/models/merge_request_diff_spec.rb index e2a9233a496..d556004eccf 100644 --- a/spec/models/merge_request_diff_spec.rb +++ b/spec/models/merge_request_diff_spec.rb @@ -1,8 +1,10 @@ require 'spec_helper' describe MergeRequestDiff do + let(:diff_with_commits) { create(:merge_request).merge_request_diff } + describe 'create new record' do - subject { create(:merge_request).merge_request_diff } + subject { diff_with_commits } it { expect(subject).to be_valid } it { expect(subject).to be_persisted } @@ -23,57 +25,41 @@ describe MergeRequestDiff do end describe '#diffs' do - let(:mr) { create(:merge_request, :with_diffs) } - let(:mr_diff) { mr.merge_request_diff } - context 'when the :ignore_whitespace_change option is set' do it 'creates a new compare object instead of loading from the DB' do - expect(mr_diff).not_to receive(:load_diffs) - expect(mr_diff.compare).to receive(:diffs).and_call_original + expect(diff_with_commits).not_to receive(:load_diffs) + expect(diff_with_commits.compare).to receive(:diffs).and_call_original - mr_diff.raw_diffs(ignore_whitespace_change: true) + diff_with_commits.raw_diffs(ignore_whitespace_change: true) end end context 'when the raw diffs are empty' do before do - MergeRequestDiffFile.delete_all(merge_request_diff_id: mr_diff.id) - end - - it 'returns an empty DiffCollection' do - expect(mr_diff.raw_diffs).to be_a(Gitlab::Git::DiffCollection) - expect(mr_diff.raw_diffs).to be_empty - end - end - - context 'when the raw diffs have invalid content' do - before do - MergeRequestDiffFile.delete_all(merge_request_diff_id: mr_diff.id) - mr_diff.update_attributes(st_diffs: ["--broken-diff"]) + MergeRequestDiffFile.delete_all(merge_request_diff_id: diff_with_commits.id) end it 'returns an empty DiffCollection' do - expect(mr_diff.raw_diffs.to_a).to be_empty - expect(mr_diff.raw_diffs).to be_a(Gitlab::Git::DiffCollection) - expect(mr_diff.raw_diffs).to be_empty + expect(diff_with_commits.raw_diffs).to be_a(Gitlab::Git::DiffCollection) + expect(diff_with_commits.raw_diffs).to be_empty end end context 'when the raw diffs exist' do it 'returns the diffs' do - expect(mr_diff.raw_diffs).to be_a(Gitlab::Git::DiffCollection) - expect(mr_diff.raw_diffs).not_to be_empty + expect(diff_with_commits.raw_diffs).to be_a(Gitlab::Git::DiffCollection) + expect(diff_with_commits.raw_diffs).not_to be_empty end context 'when the :paths option is set' do - let(:diffs) { mr_diff.raw_diffs(paths: ['files/ruby/popen.rb', 'files/ruby/popen.rb']) } + let(:diffs) { diff_with_commits.raw_diffs(paths: ['files/ruby/popen.rb', 'files/ruby/popen.rb']) } it 'only returns diffs that match the (old path, new path) given' do expect(diffs.map(&:new_path)).to contain_exactly('files/ruby/popen.rb') end it 'uses the diffs from the DB' do - expect(mr_diff).to receive(:load_diffs) + expect(diff_with_commits).to receive(:load_diffs) diffs end @@ -117,51 +103,29 @@ describe MergeRequestDiff do end describe '#commit_shas' do - it 'returns all commits SHA using serialized commits' do - subject.st_commits = [ - { id: 'sha1' }, - { id: 'sha2' } - ] - - expect(subject.commit_shas).to eq(%w(sha1 sha2)) + it 'returns all commit SHAs using commits from the DB' do + expect(diff_with_commits.commit_shas).not_to be_empty + expect(diff_with_commits.commit_shas).to all(match(/\h{40}/)) end end describe '#compare_with' do - subject { create(:merge_request, source_branch: 'fix').merge_request_diff } - it 'delegates compare to the service' do expect(CompareService).to receive(:new).and_call_original - subject.compare_with(nil) + diff_with_commits.compare_with(nil) end it 'uses git diff A..B approach by default' do - diffs = subject.compare_with('0b4bc9a49b562e85de7cc9e834518ea6828729b9').diffs + diffs = diff_with_commits.compare_with('0b4bc9a49b562e85de7cc9e834518ea6828729b9').diffs - expect(diffs.size).to eq(3) + expect(diffs.size).to eq(21) end end describe '#commits_count' do it 'returns number of commits using serialized commits' do - subject.st_commits = [ - { id: 'sha1' }, - { id: 'sha2' } - ] - - expect(subject.commits_count).to eq 2 - end - end - - describe '#utf8_st_diffs' do - it 'does not raise error when a hash value is in binary' do - subject.st_diffs = [ - { diff: "\0" }, - { diff: "\x05\x00\x68\x65\x6c\x6c\x6f" } - ] - - expect { subject.utf8_st_diffs }.not_to raise_error + expect(diff_with_commits.commits_count).to eq(29) end end end diff --git a/spec/models/merge_request_spec.rb b/spec/models/merge_request_spec.rb index 3cf8fc816ff..728028746d8 100644 --- a/spec/models/merge_request_spec.rb +++ b/spec/models/merge_request_spec.rb @@ -259,7 +259,7 @@ describe MergeRequest do end describe '#source_branch_sha' do - let(:last_branch_commit) { subject.source_project.repository.commit(subject.source_branch) } + let(:last_branch_commit) { subject.source_project.repository.commit(Gitlab::Git::BRANCH_REF_PREFIX + subject.source_branch) } context 'with diffs' do subject { create(:merge_request, :with_diffs) } @@ -273,6 +273,21 @@ describe MergeRequest do it 'returns the sha of the source branch last commit' do expect(subject.source_branch_sha).to eq(last_branch_commit.sha) end + + context 'when there is a tag name matching the branch name' do + let(:tag_name) { subject.source_branch } + + it 'returns the sha of the source branch last commit' do + subject.source_project.repository.add_tag(subject.author, + tag_name, + subject.target_branch_sha, + 'Add a tag') + + expect(subject.source_branch_sha).to eq(last_branch_commit.sha) + + subject.source_project.repository.rm_tag(subject.author, tag_name) + end + end end context 'when the merge request is being created' do @@ -933,7 +948,7 @@ describe MergeRequest do context 'with a completely different branch' do before do - subject.update(target_branch: 'v1.0.0') + subject.update(target_branch: 'csv') end it_behaves_like 'returning all SHA' @@ -941,7 +956,7 @@ describe MergeRequest do context 'with a branch having no difference' do before do - subject.update(target_branch: 'v1.1.0') + subject.update(target_branch: 'branch-merged') subject.reload # make sure commits were not cached end diff --git a/spec/models/project_services/kubernetes_service_spec.rb b/spec/models/project_services/kubernetes_service_spec.rb index 1c629155e1e..f037ee77a94 100644 --- a/spec/models/project_services/kubernetes_service_spec.rb +++ b/spec/models/project_services/kubernetes_service_spec.rb @@ -4,8 +4,8 @@ describe KubernetesService, :use_clean_rails_memory_store_caching do include KubernetesHelpers include ReactiveCachingHelpers - let(:project) { build_stubbed(:kubernetes_project) } - let(:service) { project.kubernetes_service } + let(:project) { create(:kubernetes_project) } + let(:service) { project.deployment_platform } describe 'Associations' do it { is_expected.to belong_to :project } diff --git a/spec/models/project_spec.rb b/spec/models/project_spec.rb index 549c97a9afd..521b7bd70ba 100644 --- a/spec/models/project_spec.rb +++ b/spec/models/project_spec.rb @@ -138,6 +138,7 @@ describe Project do it { is_expected.to validate_length_of(:ci_config_path).is_at_most(255) } it { is_expected.to allow_value('').for(:ci_config_path) } it { is_expected.not_to allow_value('test/../foo').for(:ci_config_path) } + it { is_expected.not_to allow_value('/test/foo').for(:ci_config_path) } it { is_expected.to validate_presence_of(:creator) } @@ -1548,8 +1549,8 @@ describe Project do expect(project.ci_config_path).to eq('foo/.gitlab_ci.yml') end - it 'sets a string but removes all leading slashes and null characters' do - project.update!(ci_config_path: "///f\0oo/\0/.gitlab_ci.yml") + it 'sets a string but removes all null characters' do + project.update!(ci_config_path: "f\0oo/\0/.gitlab_ci.yml") expect(project.ci_config_path).to eq('foo//.gitlab_ci.yml') end @@ -2002,12 +2003,25 @@ describe Project do end context 'when project has a deployment service' do - let(:project) { create(:kubernetes_project) } + shared_examples 'same behavior between KubernetesService and Platform::Kubernetes' do + it 'returns variables from this service' do + expect(project.deployment_variables).to include( + { key: 'KUBE_TOKEN', value: project.deployment_platform.token, public: false } + ) + end + end + + context 'when user configured kubernetes from Integration > Kubernetes' do + let(:project) { create(:kubernetes_project) } + + it_behaves_like 'same behavior between KubernetesService and Platform::Kubernetes' + end + + context 'when user configured kubernetes from CI/CD > Clusters' do + let!(:cluster) { create(:cluster, :project, :provided_by_gcp) } + let(:project) { cluster.project } - it 'returns variables from this service' do - expect(project.deployment_variables).to include( - { key: 'KUBE_TOKEN', value: project.kubernetes_service.token, public: false } - ) + it_behaves_like 'same behavior between KubernetesService and Platform::Kubernetes' end end end @@ -3083,4 +3097,23 @@ describe Project do expect(project.wiki_repository_exists?).to eq(false) end end + + describe '#deployment_platform' do + subject { project.deployment_platform } + + let(:project) { create(:project) } + + context 'when user configured kubernetes from Integration > Kubernetes' do + let!(:kubernetes_service) { create(:kubernetes_service, project: project) } + + it { is_expected.to eq(kubernetes_service) } + end + + context 'when user configured kubernetes from CI/CD > Clusters' do + let!(:cluster) { create(:cluster, :provided_by_gcp, projects: [project]) } + let(:platform_kubernetes) { cluster.platform_kubernetes } + + it { is_expected.to eq(platform_kubernetes) } + end + end end diff --git a/spec/models/snippet_spec.rb b/spec/models/snippet_spec.rb index de3ca300ae3..e09d89d235d 100644 --- a/spec/models/snippet_spec.rb +++ b/spec/models/snippet_spec.rb @@ -88,7 +88,7 @@ describe Snippet do end describe '.search' do - let(:snippet) { create(:snippet) } + let(:snippet) { create(:snippet, title: 'test snippet') } it 'returns snippets with a matching title' do expect(described_class.search(snippet.title)).to eq([snippet]) |