diff options
Diffstat (limited to 'spec')
54 files changed, 551 insertions, 1057 deletions
diff --git a/spec/controllers/projects/clusters/applications_controller_spec.rb b/spec/controllers/projects/clusters/applications_controller_spec.rb index 8b460646059..99fdff5f846 100644 --- a/spec/controllers/projects/clusters/applications_controller_spec.rb +++ b/spec/controllers/projects/clusters/applications_controller_spec.rb @@ -52,7 +52,7 @@ describe Projects::Clusters::ApplicationsController do context 'when application is already installing' do before do - create(:cluster_applications_helm, :installing, cluster: cluster) + create(:clusters_applications_helm, :installing, cluster: cluster) end it 'returns 400' do diff --git a/spec/factories/clusters/applications/helm.rb b/spec/factories/clusters/applications/helm.rb index d82fa8e34aa..775fbb3d27b 100644 --- a/spec/factories/clusters/applications/helm.rb +++ b/spec/factories/clusters/applications/helm.rb @@ -1,5 +1,5 @@ FactoryBot.define do - factory :cluster_applications_helm, class: Clusters::Applications::Helm do + factory :clusters_applications_helm, class: Clusters::Applications::Helm do cluster factory: %i(cluster provided_by_gcp) trait :not_installable do @@ -31,5 +31,8 @@ FactoryBot.define do installing updated_at ClusterWaitForAppInstallationWorker::TIMEOUT.ago end + + factory :clusters_applications_ingress, class: Clusters::Applications::Ingress + factory :clusters_applications_prometheus, class: Clusters::Applications::Prometheus end end diff --git a/spec/factories/clusters/applications/ingress.rb b/spec/factories/clusters/applications/ingress.rb deleted file mode 100644 index 85f54a9d744..00000000000 --- a/spec/factories/clusters/applications/ingress.rb +++ /dev/null @@ -1,35 +0,0 @@ -FactoryBot.define do - factory :cluster_applications_ingress, class: Clusters::Applications::Ingress do - cluster factory: %i(cluster provided_by_gcp) - - trait :not_installable do - status(-2) - end - - trait :installable do - status 0 - end - - trait :scheduled do - status 1 - end - - trait :installing do - status 2 - end - - trait :installed do - status 3 - end - - trait :errored do - status(-1) - status_reason 'something went wrong' - end - - trait :timeouted do - installing - updated_at ClusterWaitForAppInstallationWorker::TIMEOUT.ago - end - end -end diff --git a/spec/factories/issues.rb b/spec/factories/issues.rb index 5ed6b017dee..71dc169c6a2 100644 --- a/spec/factories/issues.rb +++ b/spec/factories/issues.rb @@ -14,6 +14,7 @@ FactoryBot.define do trait :closed do state :closed + closed_at { Time.now } end factory :closed_issue, traits: [:closed] diff --git a/spec/features/projects/clusters/applications_spec.rb b/spec/features/projects/clusters/applications_spec.rb index b34cd061ec6..9c4abec115f 100644 --- a/spec/features/projects/clusters/applications_spec.rb +++ b/spec/features/projects/clusters/applications_spec.rb @@ -73,7 +73,7 @@ feature 'Clusters Applications', :js do before do allow(ClusterInstallAppWorker).to receive(:perform_async).and_return(nil) - create(:cluster_applications_helm, :installed, cluster: cluster) + create(:clusters_applications_helm, :installed, cluster: cluster) page.within('.js-cluster-application-row-ingress') do page.find(:css, '.js-cluster-application-install-button').click diff --git a/spec/javascripts/clusters/components/applications_spec.js b/spec/javascripts/clusters/components/applications_spec.js index 7460da031c4..1a8affad4e3 100644 --- a/spec/javascripts/clusters/components/applications_spec.js +++ b/spec/javascripts/clusters/components/applications_spec.js @@ -21,6 +21,7 @@ describe('Applications', () => { helm: { title: 'Helm Tiller' }, ingress: { title: 'Ingress' }, runner: { title: 'GitLab Runner' }, + prometheus: { title: 'Prometheus' }, }, }); }); @@ -33,6 +34,10 @@ describe('Applications', () => { expect(vm.$el.querySelector('.js-cluster-application-row-ingress')).toBeDefined(); }); + it('renders a row for Prometheus', () => { + expect(vm.$el.querySelector('.js-cluster-application-row-prometheus')).toBeDefined(); + }); + /* * / it('renders a row for GitLab Runner', () => { expect(vm.$el.querySelector('.js-cluster-application-row-runner')).toBeDefined(); diff --git a/spec/javascripts/clusters/services/mock_data.js b/spec/javascripts/clusters/services/mock_data.js index af6b6a73819..253b3c45243 100644 --- a/spec/javascripts/clusters/services/mock_data.js +++ b/spec/javascripts/clusters/services/mock_data.js @@ -22,6 +22,11 @@ const CLUSTERS_MOCK_DATA = { name: 'runner', status: APPLICATION_INSTALLING, status_reason: null, + }, + { + name: 'prometheus', + status: APPLICATION_ERROR, + status_reason: 'Cannot connect', }], }, }, @@ -30,6 +35,7 @@ const CLUSTERS_MOCK_DATA = { '/gitlab-org/gitlab-shell/clusters/1/applications/helm': { }, '/gitlab-org/gitlab-shell/clusters/1/applications/ingress': { }, '/gitlab-org/gitlab-shell/clusters/1/applications/runner': { }, + '/gitlab-org/gitlab-shell/clusters/1/applications/prometheus': { }, }, }; diff --git a/spec/javascripts/clusters/stores/clusters_store_spec.js b/spec/javascripts/clusters/stores/clusters_store_spec.js index cb8b3d38e2e..ec2889355e6 100644 --- a/spec/javascripts/clusters/stores/clusters_store_spec.js +++ b/spec/javascripts/clusters/stores/clusters_store_spec.js @@ -82,6 +82,13 @@ describe('Clusters Store', () => { requestStatus: null, requestReason: null, }, + prometheus: { + title: 'Prometheus', + status: mockResponseData.applications[3].status, + statusReason: mockResponseData.applications[3].status_reason, + requestStatus: null, + requestReason: null, + }, }, }); }); diff --git a/spec/javascripts/monitoring/mock_data.js b/spec/javascripts/monitoring/mock_data.js index 1f4e858e731..2bbe963e393 100644 --- a/spec/javascripts/monitoring/mock_data.js +++ b/spec/javascripts/monitoring/mock_data.js @@ -1,6 +1,8 @@ /* eslint-disable quote-props, indent, comma-dangle */ -const metricsGroupsAPIResponse = { +export const mockApiEndpoint = `${gl.TEST_HOST}/monitoring/mock`; + +export const metricsGroupsAPIResponse = { 'success': true, 'data': [ { diff --git a/spec/javascripts/vue_shared/components/commit_spec.js b/spec/javascripts/vue_shared/components/commit_spec.js index d5754aaa9e7..fdead874209 100644 --- a/spec/javascripts/vue_shared/components/commit_spec.js +++ b/spec/javascripts/vue_shared/components/commit_spec.js @@ -10,7 +10,7 @@ describe('Commit component', () => { CommitComponent = Vue.extend(commitComp); }); - it('should render a code-fork icon if it does not represent a tag', () => { + it('should render a fork icon if it does not represent a tag', () => { component = new CommitComponent({ propsData: { tag: false, @@ -30,7 +30,7 @@ describe('Commit component', () => { }, }).$mount(); - expect(component.$el.querySelector('.icon-container i').classList).toContain('fa-code-fork'); + expect(component.$el.querySelector('.icon-container').children).toContain('svg'); }); describe('Given all the props', () => { diff --git a/spec/lib/banzai/filter/relative_link_filter_spec.rb b/spec/lib/banzai/filter/relative_link_filter_spec.rb index 08beede62db..ef306f1cd4a 100644 --- a/spec/lib/banzai/filter/relative_link_filter_spec.rb +++ b/spec/lib/banzai/filter/relative_link_filter_spec.rb @@ -5,6 +5,7 @@ describe Banzai::Filter::RelativeLinkFilter do contexts.reverse_merge!({ commit: commit, project: project, + group: group, project_wiki: project_wiki, ref: ref, requested_path: requested_path @@ -25,7 +26,12 @@ describe Banzai::Filter::RelativeLinkFilter do %(<a href="#{path}">#{path}</a>) end + def nested(element) + %(<div>#{element}</div>) + end + let(:project) { create(:project, :repository) } + let(:group) { nil } let(:project_path) { project.full_path } let(:ref) { 'markdown' } let(:commit) { project.commit(ref) } @@ -223,4 +229,79 @@ describe Banzai::Filter::RelativeLinkFilter do let(:commit) { nil } # force filter to use ref instead of commit include_examples :valid_repository end + + context 'with a /upload/ URL' do + # not needed + let(:commit) { nil } + let(:ref) { nil } + let(:requested_path) { nil } + + context 'to a project upload' do + it 'rebuilds relative URL for a link' do + doc = filter(link('/uploads/e90decf88d8f96fe9e1389afc2e4a91f/test.jpg')) + expect(doc.at_css('a')['href']) + .to eq "/#{project.full_path}/uploads/e90decf88d8f96fe9e1389afc2e4a91f/test.jpg" + + doc = filter(nested(link('/uploads/e90decf88d8f96fe9e1389afc2e4a91f/test.jpg'))) + expect(doc.at_css('a')['href']) + .to eq "/#{project.full_path}/uploads/e90decf88d8f96fe9e1389afc2e4a91f/test.jpg" + end + + it 'rebuilds relative URL for an image' do + doc = filter(image('/uploads/e90decf88d8f96fe9e1389afc2e4a91f/test.jpg')) + expect(doc.at_css('img')['src']) + .to eq "/#{project.full_path}/uploads/e90decf88d8f96fe9e1389afc2e4a91f/test.jpg" + + doc = filter(nested(image('/uploads/e90decf88d8f96fe9e1389afc2e4a91f/test.jpg'))) + expect(doc.at_css('img')['src']) + .to eq "/#{project.full_path}/uploads/e90decf88d8f96fe9e1389afc2e4a91f/test.jpg" + end + + it 'does not modify absolute URL' do + doc = filter(link('http://example.com')) + expect(doc.at_css('a')['href']).to eq 'http://example.com' + end + + it 'supports Unicode filenames' do + path = '/uploads/한글.png' + escaped = Addressable::URI.escape(path) + + # Stub these methods so the file doesn't actually need to be in the repo + allow_any_instance_of(described_class) + .to receive(:file_exists?).and_return(true) + allow_any_instance_of(described_class) + .to receive(:image?).with(path).and_return(true) + + doc = filter(image(escaped)) + expect(doc.at_css('img')['src']).to match "/#{project.full_path}/uploads/%ED%95%9C%EA%B8%80.png" + end + end + + context 'to a group upload' do + let(:upload_link) { link('/uploads/e90decf88d8f96fe9e1389afc2e4a91f/test.jpg') } + let(:group) { create(:group) } + let(:project) { nil } + let(:relative_path) { "/groups/#{group.full_path}/-/uploads/e90decf88d8f96fe9e1389afc2e4a91f/test.jpg" } + + it 'rewrites the link correctly' do + doc = filter(upload_link) + + expect(doc.at_css('a')['href']).to eq(relative_path) + end + + it 'rewrites the link correctly for subgroup' do + group.update!(parent: create(:group)) + + doc = filter(upload_link) + + expect(doc.at_css('a')['href']).to eq(relative_path) + end + + it 'does not modify absolute URL' do + doc = filter(link('http://example.com')) + + expect(doc.at_css('a')['href']).to eq 'http://example.com' + end + end + end end diff --git a/spec/lib/banzai/filter/upload_link_filter_spec.rb b/spec/lib/banzai/filter/upload_link_filter_spec.rb deleted file mode 100644 index 76bc0c36ab7..00000000000 --- a/spec/lib/banzai/filter/upload_link_filter_spec.rb +++ /dev/null @@ -1,133 +0,0 @@ -require 'spec_helper' - -describe Banzai::Filter::UploadLinkFilter do - def filter(doc, contexts = {}) - contexts.reverse_merge!({ - project: project - }) - - raw_filter(doc, contexts) - end - - def raw_filter(doc, contexts = {}) - described_class.call(doc, contexts) - end - - def image(path) - %(<img src="#{path}" />) - end - - def link(path) - %(<a href="#{path}">#{path}</a>) - end - - def nested_image(path) - %(<div><img src="#{path}" /></div>) - end - - def nested_link(path) - %(<div><a href="#{path}">#{path}</a></div>) - end - - let(:project) { create(:project) } - - shared_examples :preserve_unchanged do - it 'does not modify any relative URL in anchor' do - doc = filter(link('README.md')) - expect(doc.at_css('a')['href']).to eq 'README.md' - end - - it 'does not modify any relative URL in image' do - doc = filter(image('files/images/logo-black.png')) - expect(doc.at_css('img')['src']).to eq 'files/images/logo-black.png' - end - end - - it 'does not raise an exception on invalid URIs' do - act = link("://foo") - expect { filter(act) }.not_to raise_error - end - - context 'with a valid repository' do - it 'rebuilds relative URL for a link' do - doc = filter(link('/uploads/e90decf88d8f96fe9e1389afc2e4a91f/test.jpg')) - expect(doc.at_css('a')['href']) - .to eq "#{Gitlab.config.gitlab.url}/#{project.full_path}/uploads/e90decf88d8f96fe9e1389afc2e4a91f/test.jpg" - - doc = filter(nested_link('/uploads/e90decf88d8f96fe9e1389afc2e4a91f/test.jpg')) - expect(doc.at_css('a')['href']) - .to eq "#{Gitlab.config.gitlab.url}/#{project.full_path}/uploads/e90decf88d8f96fe9e1389afc2e4a91f/test.jpg" - end - - it 'rebuilds relative URL for an image' do - doc = filter(image('/uploads/e90decf88d8f96fe9e1389afc2e4a91f/test.jpg')) - expect(doc.at_css('img')['src']) - .to eq "#{Gitlab.config.gitlab.url}/#{project.full_path}/uploads/e90decf88d8f96fe9e1389afc2e4a91f/test.jpg" - - doc = filter(nested_image('/uploads/e90decf88d8f96fe9e1389afc2e4a91f/test.jpg')) - expect(doc.at_css('img')['src']) - .to eq "#{Gitlab.config.gitlab.url}/#{project.full_path}/uploads/e90decf88d8f96fe9e1389afc2e4a91f/test.jpg" - end - - it 'does not modify absolute URL' do - doc = filter(link('http://example.com')) - expect(doc.at_css('a')['href']).to eq 'http://example.com' - end - - it 'supports Unicode filenames' do - path = '/uploads/한글.png' - escaped = Addressable::URI.escape(path) - - # Stub these methods so the file doesn't actually need to be in the repo - allow_any_instance_of(described_class) - .to receive(:file_exists?).and_return(true) - allow_any_instance_of(described_class) - .to receive(:image?).with(path).and_return(true) - - doc = filter(image(escaped)) - expect(doc.at_css('img')['src']).to match "#{Gitlab.config.gitlab.url}/#{project.full_path}/uploads/%ED%95%9C%EA%B8%80.png" - end - end - - context 'in group context' do - let(:upload_link) { link('/uploads/e90decf88d8f96fe9e1389afc2e4a91f/test.jpg') } - let(:group) { create(:group) } - let(:filter_context) { { project: nil, group: group } } - let(:relative_path) { "groups/#{group.full_path}/-/uploads/e90decf88d8f96fe9e1389afc2e4a91f/test.jpg" } - - it 'rewrites the link correctly' do - doc = raw_filter(upload_link, filter_context) - - expect(doc.at_css('a')['href']).to eq("#{Gitlab.config.gitlab.url}/#{relative_path}") - end - - it 'rewrites the link correctly for subgroup' do - subgroup = create(:group, parent: group) - relative_path = "groups/#{subgroup.full_path}/-/uploads/e90decf88d8f96fe9e1389afc2e4a91f/test.jpg" - - doc = raw_filter(upload_link, { project: nil, group: subgroup }) - - expect(doc.at_css('a')['href']).to eq("#{Gitlab.config.gitlab.url}/#{relative_path}") - end - - it 'does not modify absolute URL' do - doc = filter(link('http://example.com'), filter_context) - - expect(doc.at_css('a')['href']).to eq 'http://example.com' - end - end - - context 'when project or group context does not exist' do - let(:upload_link) { link('/uploads/e90decf88d8f96fe9e1389afc2e4a91f/test.jpg') } - - it 'does not raise error' do - expect { raw_filter(upload_link, project: nil) }.not_to raise_error - end - - it 'does not rewrite link' do - doc = raw_filter(upload_link, project: nil) - - expect(doc.to_html).to eq upload_link - end - end -end diff --git a/spec/lib/gitlab/kubernetes/helm_spec.rb b/spec/lib/gitlab/kubernetes/helm/api_spec.rb index 15f99b0401f..69112fe90b1 100644 --- a/spec/lib/gitlab/kubernetes/helm_spec.rb +++ b/spec/lib/gitlab/kubernetes/helm/api_spec.rb @@ -1,22 +1,23 @@ require 'spec_helper' -describe Gitlab::Kubernetes::Helm do +describe Gitlab::Kubernetes::Helm::Api do let(:client) { double('kubernetes client') } let(:helm) { described_class.new(client) } - let(:namespace) { Gitlab::Kubernetes::Namespace.new(described_class::NAMESPACE, client) } + let(:gitlab_namespace) { Gitlab::Kubernetes::Helm::NAMESPACE } + let(:namespace) { Gitlab::Kubernetes::Namespace.new(gitlab_namespace, client) } let(:install_helm) { true } let(:chart) { 'stable/a_chart' } let(:application_name) { 'app_name' } - let(:command) { Gitlab::Kubernetes::Helm::InstallCommand.new(application_name, install_helm, chart) } + let(:command) { Gitlab::Kubernetes::Helm::InstallCommand.new(application_name, install_helm: install_helm, chart: chart) } subject { helm } before do - allow(Gitlab::Kubernetes::Namespace).to receive(:new).with(described_class::NAMESPACE, client).and_return(namespace) + allow(Gitlab::Kubernetes::Namespace).to receive(:new).with(gitlab_namespace, client).and_return(namespace) end describe '#initialize' do it 'creates a namespace object' do - expect(Gitlab::Kubernetes::Namespace).to receive(:new).with(described_class::NAMESPACE, client) + expect(Gitlab::Kubernetes::Namespace).to receive(:new).with(gitlab_namespace, client) subject end @@ -41,7 +42,7 @@ describe Gitlab::Kubernetes::Helm do let(:pod) { Kubeclient::Resource.new(status: { phase: phase }) } # partial representation it 'fetches POD phase from kubernetes cluster' do - expect(client).to receive(:get_pod).with(command.pod_name, described_class::NAMESPACE).once.and_return(pod) + expect(client).to receive(:get_pod).with(command.pod_name, gitlab_namespace).once.and_return(pod) expect(subject.installation_status(command.pod_name)).to eq(phase) end @@ -52,7 +53,7 @@ describe Gitlab::Kubernetes::Helm do let(:response) { RestClient::Response.new(log) } it 'fetches POD phase from kubernetes cluster' do - expect(client).to receive(:get_pod_log).with(command.pod_name, described_class::NAMESPACE).once.and_return(response) + expect(client).to receive(:get_pod_log).with(command.pod_name, gitlab_namespace).once.and_return(response) expect(subject.installation_log(command.pod_name)).to eq(log) end @@ -60,41 +61,9 @@ describe Gitlab::Kubernetes::Helm do describe '#delete_installation_pod!' do it 'deletes the POD from kubernetes cluster' do - expect(client).to receive(:delete_pod).with(command.pod_name, described_class::NAMESPACE).once + expect(client).to receive(:delete_pod).with(command.pod_name, gitlab_namespace).once subject.delete_installation_pod!(command.pod_name) end end - - describe '#helm_init_command' do - subject { helm.send(:helm_init_command, command) } - - context 'when command.install_helm is true' do - let(:install_helm) { true } - - it { is_expected.to eq('helm init >/dev/null') } - end - - context 'when command.install_helm is false' do - let(:install_helm) { false } - - it { is_expected.to eq('helm init --client-only >/dev/null') } - end - end - - describe '#helm_install_command' do - subject { helm.send(:helm_install_command, command) } - - context 'when command.chart is nil' do - let(:chart) { nil } - - it { is_expected.to be_nil } - end - - context 'when command.chart is set' do - let(:chart) { 'stable/a_chart' } - - it { is_expected.to eq("helm install #{chart} --name #{application_name} --namespace #{namespace.name} >/dev/null")} - end - end end diff --git a/spec/lib/gitlab/kubernetes/helm/install_command_spec.rb b/spec/lib/gitlab/kubernetes/helm/install_command_spec.rb new file mode 100644 index 00000000000..4afe48e72ad --- /dev/null +++ b/spec/lib/gitlab/kubernetes/helm/install_command_spec.rb @@ -0,0 +1,111 @@ +require 'rails_helper' + +describe Gitlab::Kubernetes::Helm::InstallCommand do + let(:prometheus) { create(:clusters_applications_prometheus) } + + describe "#initialize" do + context "With all the params" do + subject { described_class.new(prometheus.name, install_helm: true, chart: prometheus.chart, chart_values_file: prometheus.chart_values_file) } + + it 'should assign all parameters' do + expect(subject.name).to eq(prometheus.name) + expect(subject.install_helm).to be_truthy + expect(subject.chart).to eq(prometheus.chart) + expect(subject.chart_values_file).to eq("#{Rails.root}/vendor/prometheus/values.yaml") + end + end + + context 'when install_helm is not set' do + subject { described_class.new(prometheus.name, chart: prometheus.chart, chart_values_file: true) } + + it 'should set install_helm as false' do + expect(subject.install_helm).to be_falsy + end + end + + context 'when chart is not set' do + subject { described_class.new(prometheus.name, install_helm: true) } + + it 'should set chart as nil' do + expect(subject.chart).to be_falsy + end + end + + context 'when chart_values_file is not set' do + subject { described_class.new(prometheus.name, install_helm: true, chart: prometheus.chart) } + + it 'should set chart_values_file as nil' do + expect(subject.chart_values_file).to be_falsy + end + end + end + + describe "#generate_script" do + let(:install_command) { described_class.new(prometheus.name, install_helm: install_helm) } + let(:client) { double('kubernetes client') } + let(:namespace) { Gitlab::Kubernetes::Namespace.new(Gitlab::Kubernetes::Helm::NAMESPACE, client) } + subject { install_command.send(:generate_script, namespace.name) } + + context 'when install helm is true' do + let(:install_helm) { true } + let(:command) do + <<~MSG + set -eo pipefail + apk add -U ca-certificates openssl >/dev/null + wget -q -O - https://kubernetes-helm.storage.googleapis.com/helm-v2.7.0-linux-amd64.tar.gz | tar zxC /tmp >/dev/null + mv /tmp/linux-amd64/helm /usr/bin/ + + helm init >/dev/null + MSG + end + + it 'should return appropriate command' do + is_expected.to eq(command) + end + end + + context 'when install helm is false' do + let(:install_helm) { false } + let(:command) do + <<~MSG + set -eo pipefail + apk add -U ca-certificates openssl >/dev/null + wget -q -O - https://kubernetes-helm.storage.googleapis.com/helm-v2.7.0-linux-amd64.tar.gz | tar zxC /tmp >/dev/null + mv /tmp/linux-amd64/helm /usr/bin/ + + helm init --client-only >/dev/null + MSG + end + + it 'should return appropriate command' do + is_expected.to eq(command) + end + end + + context 'when chart is present' do + let(:install_command) { described_class.new(prometheus.name, chart: prometheus.chart) } + let(:command) do + <<~MSG.chomp + set -eo pipefail + apk add -U ca-certificates openssl >/dev/null + wget -q -O - https://kubernetes-helm.storage.googleapis.com/helm-v2.7.0-linux-amd64.tar.gz | tar zxC /tmp >/dev/null + mv /tmp/linux-amd64/helm /usr/bin/ + + helm init --client-only >/dev/null + helm install #{prometheus.chart} --name #{prometheus.name} --namespace #{namespace.name} >/dev/null + MSG + end + + it 'should return appropriate command' do + is_expected.to eq(command) + end + end + end + + describe "#pod_name" do + let(:install_command) { described_class.new(prometheus.name, install_helm: true, chart: prometheus.chart, chart_values_file: true) } + subject { install_command.send(:pod_name) } + + it { is_expected.to eq('install-prometheus') } + end +end diff --git a/spec/lib/gitlab/kubernetes/helm/pod_spec.rb b/spec/lib/gitlab/kubernetes/helm/pod_spec.rb new file mode 100644 index 00000000000..906b10b96d4 --- /dev/null +++ b/spec/lib/gitlab/kubernetes/helm/pod_spec.rb @@ -0,0 +1,86 @@ +require 'rails_helper' + +describe Gitlab::Kubernetes::Helm::Pod do + describe '#generate' do + let(:cluster) { create(:cluster) } + let(:app) { create(:clusters_applications_prometheus, cluster: cluster) } + let(:command) { app.install_command } + let(:client) { double('kubernetes client') } + let(:namespace) { Gitlab::Kubernetes::Namespace.new(Gitlab::Kubernetes::Helm::NAMESPACE, client) } + subject { described_class.new(command, namespace.name, client) } + + before do + allow(client).to receive(:create_config_map).and_return(nil) + end + + shared_examples 'helm pod' do + it 'should generate a Kubeclient::Resource' do + expect(subject.generate).to be_a_kind_of(Kubeclient::Resource) + end + + it 'should generate the appropriate metadata' do + metadata = subject.generate.metadata + expect(metadata.name).to eq("install-#{app.name}") + expect(metadata.namespace).to eq('gitlab-managed-apps') + expect(metadata.labels['gitlab.org/action']).to eq('install') + expect(metadata.labels['gitlab.org/application']).to eq(app.name) + end + + it 'should generate a container spec' do + spec = subject.generate.spec + expect(spec.containers.count).to eq(1) + end + + it 'should generate the appropriate specifications for the container' do + container = subject.generate.spec.containers.first + expect(container.name).to eq('helm') + expect(container.image).to eq('alpine:3.6') + expect(container.env.count).to eq(3) + expect(container.env.map(&:name)).to match_array([:HELM_VERSION, :TILLER_NAMESPACE, :COMMAND_SCRIPT]) + expect(container.command).to match_array(["/bin/sh"]) + expect(container.args).to match_array(["-c", "$(COMMAND_SCRIPT)"]) + end + + it 'should include a never restart policy' do + spec = subject.generate.spec + expect(spec.restartPolicy).to eq('Never') + end + end + + context 'with a configuration file' do + it_behaves_like 'helm pod' + + it 'should include volumes for the container' do + container = subject.generate.spec.containers.first + expect(container.volumeMounts.first['name']).to eq('config-volume') + expect(container.volumeMounts.first['mountPath']).to eq('/etc/config') + end + + it 'should include a volume inside the specification' do + spec = subject.generate.spec + expect(spec.volumes.first['name']).to eq('config-volume') + end + + it 'should mount configMap specification in the volume' do + spec = subject.generate.spec + expect(spec.volumes.first.configMap['name']).to eq('values-config') + end + end + + context 'without a configuration file' do + let(:app) { create(:clusters_applications_ingress, cluster: cluster) } + + it_behaves_like 'helm pod' + + it 'should not include volumeMounts inside the container' do + container = subject.generate.spec.containers.first + expect(container.volumeMounts).to be_nil + end + + it 'should not a volume inside the specification' do + spec = subject.generate.spec + expect(spec.volumes).to be_nil + end + end + end +end diff --git a/spec/models/clusters/applications/helm_spec.rb b/spec/models/clusters/applications/helm_spec.rb index f8855079842..eb57abaf6ef 100644 --- a/spec/models/clusters/applications/helm_spec.rb +++ b/spec/models/clusters/applications/helm_spec.rb @@ -40,13 +40,13 @@ describe Clusters::Applications::Helm do describe '#install_command' do it 'has all the needed information' do - expect(subject.install_command).to have_attributes(name: subject.name, install_helm: true, chart: nil) + expect(subject.install_command).to have_attributes(name: subject.name, install_helm: true) end end describe 'status state machine' do describe '#make_installing' do - subject { create(:cluster_applications_helm, :scheduled) } + subject { create(:clusters_applications_helm, :scheduled) } it 'is installing' do subject.make_installing! @@ -56,7 +56,7 @@ describe Clusters::Applications::Helm do end describe '#make_installed' do - subject { create(:cluster_applications_helm, :installing) } + subject { create(:clusters_applications_helm, :installing) } it 'is installed' do subject.make_installed @@ -66,7 +66,7 @@ describe Clusters::Applications::Helm do end describe '#make_errored' do - subject { create(:cluster_applications_helm, :installing) } + subject { create(:clusters_applications_helm, :installing) } let(:reason) { 'some errors' } it 'is errored' do @@ -78,7 +78,7 @@ describe Clusters::Applications::Helm do end describe '#make_scheduled' do - subject { create(:cluster_applications_helm, :installable) } + subject { create(:clusters_applications_helm, :installable) } it 'is scheduled' do subject.make_scheduled @@ -87,7 +87,7 @@ describe Clusters::Applications::Helm do end describe 'when was errored' do - subject { create(:cluster_applications_helm, :errored) } + subject { create(:clusters_applications_helm, :errored) } it 'clears #status_reason' do expect(subject.status_reason).not_to be_nil diff --git a/spec/models/clusters/applications/ingress_spec.rb b/spec/models/clusters/applications/ingress_spec.rb index b83472e1944..619c088b0bf 100644 --- a/spec/models/clusters/applications/ingress_spec.rb +++ b/spec/models/clusters/applications/ingress_spec.rb @@ -4,105 +4,5 @@ describe Clusters::Applications::Ingress do it { is_expected.to belong_to(:cluster) } it { is_expected.to validate_presence_of(:cluster) } - describe '#name' do - it 'is .application_name' do - expect(subject.name).to eq(described_class.application_name) - end - - it 'is recorded in Clusters::Cluster::APPLICATIONS' do - expect(Clusters::Cluster::APPLICATIONS[subject.name]).to eq(described_class) - end - end - - describe '#status' do - let(:cluster) { create(:cluster, :provided_by_gcp) } - - subject { described_class.new(cluster: cluster) } - - it 'defaults to :not_installable' do - expect(subject.status_name).to be(:not_installable) - end - - context 'when application helm is scheduled' do - before do - create(:cluster_applications_helm, :scheduled, cluster: cluster) - end - - it 'defaults to :not_installable' do - expect(subject.status_name).to be(:not_installable) - end - end - - context 'when application helm is installed' do - before do - create(:cluster_applications_helm, :installed, cluster: cluster) - end - - it 'defaults to :installable' do - expect(subject.status_name).to be(:installable) - end - end - end - - describe '#install_command' do - it 'has all the needed information' do - expect(subject.install_command).to have_attributes(name: subject.name, install_helm: false, chart: subject.chart) - end - end - - describe 'status state machine' do - describe '#make_installing' do - subject { create(:cluster_applications_ingress, :scheduled) } - - it 'is installing' do - subject.make_installing! - - expect(subject).to be_installing - end - end - - describe '#make_installed' do - subject { create(:cluster_applications_ingress, :installing) } - - it 'is installed' do - subject.make_installed - - expect(subject).to be_installed - end - end - - describe '#make_errored' do - subject { create(:cluster_applications_ingress, :installing) } - let(:reason) { 'some errors' } - - it 'is errored' do - subject.make_errored(reason) - - expect(subject).to be_errored - expect(subject.status_reason).to eq(reason) - end - end - - describe '#make_scheduled' do - subject { create(:cluster_applications_ingress, :installable) } - - it 'is scheduled' do - subject.make_scheduled - - expect(subject).to be_scheduled - end - - describe 'when was errored' do - subject { create(:cluster_applications_ingress, :errored) } - - it 'clears #status_reason' do - expect(subject.status_reason).not_to be_nil - - subject.make_scheduled! - - expect(subject.status_reason).to be_nil - end - end - end - end + include_examples 'cluster application specs', described_class end diff --git a/spec/models/clusters/applications/prometheus_spec.rb b/spec/models/clusters/applications/prometheus_spec.rb new file mode 100644 index 00000000000..696099f7cf7 --- /dev/null +++ b/spec/models/clusters/applications/prometheus_spec.rb @@ -0,0 +1,16 @@ +require 'rails_helper' + +describe Clusters::Applications::Prometheus do + it { is_expected.to belong_to(:cluster) } + it { is_expected.to validate_presence_of(:cluster) } + + include_examples 'cluster application specs', described_class + + describe "#chart_values_file" do + subject { create(:clusters_applications_prometheus).chart_values_file } + + it 'should return chart values file path' do + expect(subject).to eq("#{Rails.root}/vendor/prometheus/values.yaml") + end + end +end diff --git a/spec/models/clusters/cluster_spec.rb b/spec/models/clusters/cluster_spec.rb index 2683d21ddbe..799d7ced116 100644 --- a/spec/models/clusters/cluster_spec.rb +++ b/spec/models/clusters/cluster_spec.rb @@ -5,6 +5,9 @@ describe Clusters::Cluster do it { is_expected.to have_many(:projects) } it { is_expected.to have_one(:provider_gcp) } it { is_expected.to have_one(:platform_kubernetes) } + it { is_expected.to have_one(:application_helm) } + it { is_expected.to have_one(:application_ingress) } + it { is_expected.to have_one(:application_prometheus) } it { is_expected.to delegate_method(:status).to(:provider) } it { is_expected.to delegate_method(:status_reason).to(:provider) } it { is_expected.to delegate_method(:status_name).to(:provider) } @@ -190,11 +193,12 @@ describe Clusters::Cluster do end context 'when applications are created' do - let!(:helm) { create(:cluster_applications_helm, cluster: cluster) } - let!(:ingress) { create(:cluster_applications_ingress, cluster: cluster) } + let!(:helm) { create(:clusters_applications_helm, cluster: cluster) } + let!(:ingress) { create(:clusters_applications_ingress, cluster: cluster) } + let!(:prometheus) { create(:clusters_applications_prometheus, cluster: cluster) } it 'returns a list of created applications' do - is_expected.to contain_exactly(helm, ingress) + is_expected.to contain_exactly(helm, ingress, prometheus) end end end diff --git a/spec/models/diff_note_spec.rb b/spec/models/diff_note_spec.rb index 4d0b3245a13..2705421e540 100644 --- a/spec/models/diff_note_spec.rb +++ b/spec/models/diff_note_spec.rb @@ -112,22 +112,6 @@ describe DiffNote do end end - describe "#for_line?" do - context "when provided the correct diff line" do - it "returns true" do - expect(subject.for_line?(subject.diff_line)).to be true - end - end - - context "when provided a different diff line" do - it "returns false" do - some_line = subject.diff_file.diff_lines.first - - expect(subject.for_line?(some_line)).to be false - end - end - end - describe "#active?" do context "when noteable is a commit" do subject { build(:diff_note_on_commit, project: project, position: position) } diff --git a/spec/rubocop/cop/active_record_dependent_spec.rb b/spec/rubocop/cop/active_record_dependent_spec.rb deleted file mode 100644 index 599a032bfc5..00000000000 --- a/spec/rubocop/cop/active_record_dependent_spec.rb +++ /dev/null @@ -1,33 +0,0 @@ -require 'spec_helper' -require 'rubocop' -require 'rubocop/rspec/support' -require_relative '../../../rubocop/cop/active_record_dependent' - -describe RuboCop::Cop::ActiveRecordDependent do - include CopHelper - - subject(:cop) { described_class.new } - - context 'inside the app/models directory' do - it 'registers an offense when dependent: is used' do - allow(cop).to receive(:in_model?).and_return(true) - - inspect_source(cop, 'belongs_to :foo, dependent: :destroy') - - aggregate_failures do - expect(cop.offenses.size).to eq(1) - expect(cop.offenses.map(&:line)).to eq([1]) - end - end - end - - context 'outside the app/models directory' do - it 'does nothing' do - allow(cop).to receive(:in_model?).and_return(false) - - inspect_source(cop, 'belongs_to :foo, dependent: :destroy') - - expect(cop.offenses).to be_empty - end - end -end diff --git a/spec/rubocop/cop/active_record_serialize_spec.rb b/spec/rubocop/cop/active_record_serialize_spec.rb deleted file mode 100644 index b94b25cecd0..00000000000 --- a/spec/rubocop/cop/active_record_serialize_spec.rb +++ /dev/null @@ -1,33 +0,0 @@ -require 'spec_helper' -require 'rubocop' -require 'rubocop/rspec/support' -require_relative '../../../rubocop/cop/active_record_serialize' - -describe RuboCop::Cop::ActiveRecordSerialize do - include CopHelper - - subject(:cop) { described_class.new } - - context 'inside the app/models directory' do - it 'registers an offense when serialize is used' do - allow(cop).to receive(:in_model?).and_return(true) - - inspect_source(cop, 'serialize :foo') - - aggregate_failures do - expect(cop.offenses.size).to eq(1) - expect(cop.offenses.map(&:line)).to eq([1]) - end - end - end - - context 'outside the app/models directory' do - it 'does nothing' do - allow(cop).to receive(:in_model?).and_return(false) - - inspect_source(cop, 'serialize :foo') - - expect(cop.offenses).to be_empty - end - end -end diff --git a/spec/rubocop/cop/custom_error_class_spec.rb b/spec/rubocop/cop/custom_error_class_spec.rb deleted file mode 100644 index 381d7871a40..00000000000 --- a/spec/rubocop/cop/custom_error_class_spec.rb +++ /dev/null @@ -1,111 +0,0 @@ -require 'spec_helper' - -require 'rubocop' -require 'rubocop/rspec/support' - -require_relative '../../../rubocop/cop/custom_error_class' - -describe RuboCop::Cop::CustomErrorClass do - include CopHelper - - subject(:cop) { described_class.new } - - context 'when a class has a body' do - it 'does nothing' do - inspect_source(cop, 'class CustomError < StandardError; def foo; end; end') - - expect(cop.offenses).to be_empty - end - end - - context 'when a class has no explicit superclass' do - it 'does nothing' do - inspect_source(cop, 'class CustomError; end') - - expect(cop.offenses).to be_empty - end - end - - context 'when a class has a superclass that does not end in Error' do - it 'does nothing' do - inspect_source(cop, 'class CustomError < BasicObject; end') - - expect(cop.offenses).to be_empty - end - end - - context 'when a class is empty and inherits from a class ending in Error' do - context 'when the class is on a single line' do - let(:source) do - <<-SOURCE - module Foo - class CustomError < Bar::Baz::BaseError; end - end - SOURCE - end - - let(:expected) do - <<-EXPECTED - module Foo - CustomError = Class.new(Bar::Baz::BaseError) - end - EXPECTED - end - - it 'registers an offense' do - expected_highlights = source.split("\n")[1].strip - - inspect_source(cop, source) - - aggregate_failures do - expect(cop.offenses.size).to eq(1) - expect(cop.offenses.map(&:line)).to eq([2]) - expect(cop.highlights).to contain_exactly(expected_highlights) - end - end - - it 'autocorrects to the right version' do - autocorrected = autocorrect_source(cop, source, 'foo/custom_error.rb') - - expect(autocorrected).to eq(expected) - end - end - - context 'when the class is on multiple lines' do - let(:source) do - <<-SOURCE - module Foo - class CustomError < Bar::Baz::BaseError - end - end - SOURCE - end - - let(:expected) do - <<-EXPECTED - module Foo - CustomError = Class.new(Bar::Baz::BaseError) - end - EXPECTED - end - - it 'registers an offense' do - expected_highlights = source.split("\n")[1..2].join("\n").strip - - inspect_source(cop, source) - - aggregate_failures do - expect(cop.offenses.size).to eq(1) - expect(cop.offenses.map(&:line)).to eq([2]) - expect(cop.highlights).to contain_exactly(expected_highlights) - end - end - - it 'autocorrects to the right version' do - autocorrected = autocorrect_source(cop, source, 'foo/custom_error.rb') - - expect(autocorrected).to eq(expected) - end - end - end -end diff --git a/spec/rubocop/cop/gem_fetcher_spec.rb b/spec/rubocop/cop/gem_fetcher_spec.rb deleted file mode 100644 index c07f6a831dc..00000000000 --- a/spec/rubocop/cop/gem_fetcher_spec.rb +++ /dev/null @@ -1,46 +0,0 @@ -require 'spec_helper' - -require 'rubocop' -require 'rubocop/rspec/support' - -require_relative '../../../rubocop/cop/gem_fetcher' - -describe RuboCop::Cop::GemFetcher do - include CopHelper - - subject(:cop) { described_class.new } - - context 'in Gemfile' do - before do - allow(cop).to receive(:gemfile?).and_return(true) - end - - it 'registers an offense when a gem uses `git`' do - inspect_source(cop, 'gem "foo", git: "https://gitlab.com/foo/bar.git"') - - aggregate_failures do - expect(cop.offenses.size).to eq(1) - expect(cop.offenses.map(&:line)).to eq([1]) - expect(cop.highlights).to eq(['git: "https://gitlab.com/foo/bar.git"']) - end - end - - it 'registers an offense when a gem uses `github`' do - inspect_source(cop, 'gem "foo", github: "foo/bar.git"') - - aggregate_failures do - expect(cop.offenses.size).to eq(1) - expect(cop.offenses.map(&:line)).to eq([1]) - expect(cop.highlights).to eq(['github: "foo/bar.git"']) - end - end - end - - context 'outside of Gemfile' do - it 'registers no offense' do - inspect_source(cop, 'gem "foo", git: "https://gitlab.com/foo/bar.git"') - - expect(cop.offenses.size).to eq(0) - end - end -end diff --git a/spec/rubocop/cop/gitlab/module_with_instance_variables_spec.rb b/spec/rubocop/cop/gitlab/module_with_instance_variables_spec.rb index 1fd40653f79..8e2d5f70353 100644 --- a/spec/rubocop/cop/gitlab/module_with_instance_variables_spec.rb +++ b/spec/rubocop/cop/gitlab/module_with_instance_variables_spec.rb @@ -12,7 +12,7 @@ describe RuboCop::Cop::Gitlab::ModuleWithInstanceVariables do let(:offending_lines) { options[:offending_lines] } it 'registers an offense when instance variable is used in a module' do - inspect_source(cop, source) + inspect_source(source) aggregate_failures do expect(cop.offenses.size).to eq(offending_lines.size) @@ -23,7 +23,7 @@ describe RuboCop::Cop::Gitlab::ModuleWithInstanceVariables do shared_examples('not registering offense') do it 'does not register offenses' do - inspect_source(cop, source) + inspect_source(source) expect(cop.offenses).to be_empty end diff --git a/spec/rubocop/cop/in_batches_spec.rb b/spec/rubocop/cop/in_batches_spec.rb deleted file mode 100644 index 072481984c6..00000000000 --- a/spec/rubocop/cop/in_batches_spec.rb +++ /dev/null @@ -1,19 +0,0 @@ -require 'spec_helper' -require 'rubocop' -require 'rubocop/rspec/support' -require_relative '../../../rubocop/cop/in_batches' - -describe RuboCop::Cop::InBatches do - include CopHelper - - subject(:cop) { described_class.new } - - it 'registers an offense when in_batches is used' do - inspect_source(cop, 'foo.in_batches do; end') - - aggregate_failures do - expect(cop.offenses.size).to eq(1) - expect(cop.offenses.map(&:line)).to eq([1]) - end - end -end diff --git a/spec/rubocop/cop/include_sidekiq_worker_spec.rb b/spec/rubocop/cop/include_sidekiq_worker_spec.rb index 7f406535dda..f5109287876 100644 --- a/spec/rubocop/cop/include_sidekiq_worker_spec.rb +++ b/spec/rubocop/cop/include_sidekiq_worker_spec.rb @@ -1,6 +1,8 @@ require 'spec_helper' + require 'rubocop' require 'rubocop/rspec/support' + require_relative '../../../rubocop/cop/include_sidekiq_worker' describe RuboCop::Cop::IncludeSidekiqWorker do @@ -13,7 +15,7 @@ describe RuboCop::Cop::IncludeSidekiqWorker do let(:correct_source) { 'include ApplicationWorker' } it 'registers an offense ' do - inspect_source(cop, source) + inspect_source(source) aggregate_failures do expect(cop.offenses.size).to eq(1) @@ -23,7 +25,7 @@ describe RuboCop::Cop::IncludeSidekiqWorker do end it 'autocorrects to the right version' do - autocorrected = autocorrect_source(cop, source) + autocorrected = autocorrect_source(source) expect(autocorrected).to eq(correct_source) end diff --git a/spec/rubocop/cop/line_break_after_guard_clauses_spec.rb b/spec/rubocop/cop/line_break_after_guard_clauses_spec.rb deleted file mode 100644 index 8899dc85384..00000000000 --- a/spec/rubocop/cop/line_break_after_guard_clauses_spec.rb +++ /dev/null @@ -1,160 +0,0 @@ -require 'spec_helper' -require 'rubocop' -require 'rubocop/rspec/support' -require_relative '../../../rubocop/cop/line_break_after_guard_clauses' - -describe RuboCop::Cop::LineBreakAfterGuardClauses do - include CopHelper - - subject(:cop) { described_class.new } - - shared_examples 'examples with guard clause' do |title| - %w[if unless].each do |conditional| - it "flags violation for #{title} #{conditional} without line breaks" do - source = <<~RUBY - #{title} #{conditional} condition - do_stuff - RUBY - inspect_source(cop, source) - - expect(cop.offenses.size).to eq(1) - offense = cop.offenses.first - - expect(offense.line).to eq(1) - expect(cop.highlights).to eq(["#{title} #{conditional} condition"]) - expect(offense.message).to eq('Add a line break after guard clauses') - end - - it "doesn't flag violation for #{title} #{conditional} with line break" do - source = <<~RUBY - #{title} #{conditional} condition - - do_stuff - RUBY - inspect_source(cop, source) - - expect(cop.offenses).to be_empty - end - - it "doesn't flag violation for #{title} #{conditional} on multiple lines without line break" do - source = <<~RUBY - #{conditional} condition - #{title} - end - do_stuff - RUBY - inspect_source(cop, source) - - expect(cop.offenses).to be_empty - end - - it "doesn't flag violation for #{title} #{conditional} without line breaks when followed by end keyword" do - source = <<~RUBY - def test - #{title} #{conditional} condition - end - RUBY - inspect_source(cop, source) - - expect(cop.offenses).to be_empty - end - - it "doesn't flag violation for #{title} #{conditional} without line breaks when followed by elsif keyword" do - source = <<~RUBY - if model - #{title} #{conditional} condition - elsif - do_something - end - RUBY - inspect_source(cop, source) - - expect(cop.offenses).to be_empty - end - - it "doesn't flag violation for #{title} #{conditional} without line breaks when followed by else keyword" do - source = <<~RUBY - if model - #{title} #{conditional} condition - else - do_something - end - RUBY - inspect_source(cop, source) - - expect(cop.offenses).to be_empty - end - - it "doesn't flag violation for #{title} #{conditional} without line breaks when followed by when keyword" do - source = <<~RUBY - case model - when condition_a - #{title} #{conditional} condition - when condition_b - do_something - end - RUBY - inspect_source(cop, source) - - expect(cop.offenses).to be_empty - end - - it "doesn't flag violation for #{title} #{conditional} without line breaks when followed by rescue keyword" do - source = <<~RUBY - begin - #{title} #{conditional} condition - rescue StandardError - do_something - end - RUBY - inspect_source(cop, source) - - expect(cop.offenses).to be_empty - end - - it "doesn't flag violation for #{title} #{conditional} without line breaks when followed by ensure keyword" do - source = <<~RUBY - def foo - #{title} #{conditional} condition - ensure - do_something - end - RUBY - inspect_source(cop, source) - - expect(cop.offenses).to be_empty - end - - it "doesn't flag violation for #{title} #{conditional} without line breaks when followed by another guard clause" do - source = <<~RUBY - #{title} #{conditional} condition - #{title} #{conditional} condition - - do_stuff - RUBY - inspect_source(cop, source) - - expect(cop.offenses).to be_empty - end - - it "autocorrects #{title} #{conditional} guard clauses without line break" do - source = <<~RUBY - #{title} #{conditional} condition - do_stuff - RUBY - autocorrected = autocorrect_source(cop, source) - - expected_source = <<~RUBY - #{title} #{conditional} condition - - do_stuff - RUBY - expect(autocorrected).to eql(expected_source) - end - end - end - - %w[return fail raise next break throw].each do |example| - it_behaves_like 'examples with guard clause', example - end -end diff --git a/spec/rubocop/cop/migration/add_concurrent_foreign_key_spec.rb b/spec/rubocop/cop/migration/add_concurrent_foreign_key_spec.rb index 7cb24dc5646..1df1fffb94e 100644 --- a/spec/rubocop/cop/migration/add_concurrent_foreign_key_spec.rb +++ b/spec/rubocop/cop/migration/add_concurrent_foreign_key_spec.rb @@ -1,6 +1,8 @@ require 'spec_helper' + require 'rubocop' require 'rubocop/rspec/support' + require_relative '../../../../rubocop/cop/migration/add_concurrent_foreign_key' describe RuboCop::Cop::Migration::AddConcurrentForeignKey do @@ -10,7 +12,7 @@ describe RuboCop::Cop::Migration::AddConcurrentForeignKey do context 'outside of a migration' do it 'does not register any offenses' do - inspect_source(cop, 'def up; add_foreign_key(:projects, :users, column: :user_id); end') + inspect_source('def up; add_foreign_key(:projects, :users, column: :user_id); end') expect(cop.offenses).to be_empty end @@ -22,7 +24,7 @@ describe RuboCop::Cop::Migration::AddConcurrentForeignKey do end it 'registers an offense when using add_foreign_key' do - inspect_source(cop, 'def up; add_foreign_key(:projects, :users, column: :user_id); end') + inspect_source('def up; add_foreign_key(:projects, :users, column: :user_id); end') aggregate_failures do expect(cop.offenses.size).to eq(1) diff --git a/spec/rubocop/cop/migration/add_concurrent_index_spec.rb b/spec/rubocop/cop/migration/add_concurrent_index_spec.rb index 19a5718b0b1..9c1ebcc0ced 100644 --- a/spec/rubocop/cop/migration/add_concurrent_index_spec.rb +++ b/spec/rubocop/cop/migration/add_concurrent_index_spec.rb @@ -16,7 +16,7 @@ describe RuboCop::Cop::Migration::AddConcurrentIndex do end it 'registers an offense when add_concurrent_index is used inside a change method' do - inspect_source(cop, 'def change; add_concurrent_index :table, :column; end') + inspect_source('def change; add_concurrent_index :table, :column; end') aggregate_failures do expect(cop.offenses.size).to eq(1) @@ -25,7 +25,7 @@ describe RuboCop::Cop::Migration::AddConcurrentIndex do end it 'registers no offense when add_concurrent_index is used inside an up method' do - inspect_source(cop, 'def up; add_concurrent_index :table, :column; end') + inspect_source('def up; add_concurrent_index :table, :column; end') expect(cop.offenses.size).to eq(0) end @@ -33,7 +33,7 @@ describe RuboCop::Cop::Migration::AddConcurrentIndex do context 'outside of migration' do it 'registers no offense' do - inspect_source(cop, 'def change; add_concurrent_index :table, :column; end') + inspect_source('def change; add_concurrent_index :table, :column; end') expect(cop.offenses.size).to eq(0) end diff --git a/spec/rubocop/cop/migration/add_timestamps_spec.rb b/spec/rubocop/cop/migration/add_timestamps_spec.rb index 18df62dec3e..3a41c91add2 100644 --- a/spec/rubocop/cop/migration/add_timestamps_spec.rb +++ b/spec/rubocop/cop/migration/add_timestamps_spec.rb @@ -53,7 +53,7 @@ describe RuboCop::Cop::Migration::AddTimestamps do end it 'registers an offense when the "add_timestamps" method is used' do - inspect_source(cop, migration_with_add_timestamps) + inspect_source(migration_with_add_timestamps) aggregate_failures do expect(cop.offenses.size).to eq(1) @@ -62,7 +62,7 @@ describe RuboCop::Cop::Migration::AddTimestamps do end it 'does not register an offense when the "add_timestamps" method is not used' do - inspect_source(cop, migration_without_add_timestamps) + inspect_source(migration_without_add_timestamps) aggregate_failures do expect(cop.offenses.size).to eq(0) @@ -70,7 +70,7 @@ describe RuboCop::Cop::Migration::AddTimestamps do end it 'does not register an offense when the "add_timestamps_with_timezone" method is used' do - inspect_source(cop, migration_with_add_timestamps_with_timezone) + inspect_source(migration_with_add_timestamps_with_timezone) aggregate_failures do expect(cop.offenses.size).to eq(0) @@ -80,9 +80,9 @@ describe RuboCop::Cop::Migration::AddTimestamps do context 'outside of migration' do it 'registers no offense' do - inspect_source(cop, migration_with_add_timestamps) - inspect_source(cop, migration_without_add_timestamps) - inspect_source(cop, migration_with_add_timestamps_with_timezone) + inspect_source(migration_with_add_timestamps) + inspect_source(migration_without_add_timestamps) + inspect_source(migration_with_add_timestamps_with_timezone) expect(cop.offenses.size).to eq(0) end diff --git a/spec/rubocop/cop/migration/datetime_spec.rb b/spec/rubocop/cop/migration/datetime_spec.rb index b1dfcf1b048..9e844325371 100644 --- a/spec/rubocop/cop/migration/datetime_spec.rb +++ b/spec/rubocop/cop/migration/datetime_spec.rb @@ -67,7 +67,7 @@ describe RuboCop::Cop::Migration::Datetime do end it 'registers an offense when the ":datetime" data type is used' do - inspect_source(cop, migration_with_datetime) + inspect_source(migration_with_datetime) aggregate_failures do expect(cop.offenses.size).to eq(1) @@ -77,7 +77,7 @@ describe RuboCop::Cop::Migration::Datetime do end it 'registers an offense when the ":timestamp" data type is used' do - inspect_source(cop, migration_with_timestamp) + inspect_source(migration_with_timestamp) aggregate_failures do expect(cop.offenses.size).to eq(1) @@ -87,7 +87,7 @@ describe RuboCop::Cop::Migration::Datetime do end it 'does not register an offense when the ":datetime" data type is not used' do - inspect_source(cop, migration_without_datetime) + inspect_source(migration_without_datetime) aggregate_failures do expect(cop.offenses.size).to eq(0) @@ -95,7 +95,7 @@ describe RuboCop::Cop::Migration::Datetime do end it 'does not register an offense when the ":datetime_with_timezone" data type is used' do - inspect_source(cop, migration_with_datetime_with_timezone) + inspect_source(migration_with_datetime_with_timezone) aggregate_failures do expect(cop.offenses.size).to eq(0) @@ -105,10 +105,10 @@ describe RuboCop::Cop::Migration::Datetime do context 'outside of migration' do it 'registers no offense' do - inspect_source(cop, migration_with_datetime) - inspect_source(cop, migration_with_timestamp) - inspect_source(cop, migration_without_datetime) - inspect_source(cop, migration_with_datetime_with_timezone) + inspect_source(migration_with_datetime) + inspect_source(migration_with_timestamp) + inspect_source(migration_without_datetime) + inspect_source(migration_with_datetime_with_timezone) expect(cop.offenses.size).to eq(0) end diff --git a/spec/rubocop/cop/migration/hash_index_spec.rb b/spec/rubocop/cop/migration/hash_index_spec.rb index 9a8576a19e5..5d53dde9a79 100644 --- a/spec/rubocop/cop/migration/hash_index_spec.rb +++ b/spec/rubocop/cop/migration/hash_index_spec.rb @@ -16,7 +16,7 @@ describe RuboCop::Cop::Migration::HashIndex do end it 'registers an offense when creating a hash index' do - inspect_source(cop, 'def change; add_index :table, :column, using: :hash; end') + inspect_source('def change; add_index :table, :column, using: :hash; end') aggregate_failures do expect(cop.offenses.size).to eq(1) @@ -25,7 +25,7 @@ describe RuboCop::Cop::Migration::HashIndex do end it 'registers an offense when creating a concurrent hash index' do - inspect_source(cop, 'def change; add_concurrent_index :table, :column, using: :hash; end') + inspect_source('def change; add_concurrent_index :table, :column, using: :hash; end') aggregate_failures do expect(cop.offenses.size).to eq(1) @@ -34,7 +34,7 @@ describe RuboCop::Cop::Migration::HashIndex do end it 'registers an offense when creating a hash index using t.index' do - inspect_source(cop, 'def change; t.index :table, :column, using: :hash; end') + inspect_source('def change; t.index :table, :column, using: :hash; end') aggregate_failures do expect(cop.offenses.size).to eq(1) @@ -45,7 +45,7 @@ describe RuboCop::Cop::Migration::HashIndex do context 'outside of migration' do it 'registers no offense' do - inspect_source(cop, 'def change; index :table, :column, using: :hash; end') + inspect_source('def change; index :table, :column, using: :hash; end') expect(cop.offenses.size).to eq(0) end diff --git a/spec/rubocop/cop/migration/remove_column_spec.rb b/spec/rubocop/cop/migration/remove_column_spec.rb index 89112f01723..f1a64f431bd 100644 --- a/spec/rubocop/cop/migration/remove_column_spec.rb +++ b/spec/rubocop/cop/migration/remove_column_spec.rb @@ -21,7 +21,7 @@ describe RuboCop::Cop::Migration::RemoveColumn do end it 'registers an offense when remove_column is used in the change method' do - inspect_source(cop, source('change')) + inspect_source(source('change')) aggregate_failures do expect(cop.offenses.size).to eq(1) @@ -30,7 +30,7 @@ describe RuboCop::Cop::Migration::RemoveColumn do end it 'registers an offense when remove_column is used in the up method' do - inspect_source(cop, source('up')) + inspect_source(source('up')) aggregate_failures do expect(cop.offenses.size).to eq(1) @@ -39,7 +39,7 @@ describe RuboCop::Cop::Migration::RemoveColumn do end it 'registers no offense when remove_column is used in the down method' do - inspect_source(cop, source('down')) + inspect_source(source('down')) expect(cop.offenses.size).to eq(0) end @@ -52,7 +52,7 @@ describe RuboCop::Cop::Migration::RemoveColumn do end it 'registers no offense' do - inspect_source(cop, source) + inspect_source(source) expect(cop.offenses.size).to eq(0) end @@ -60,7 +60,7 @@ describe RuboCop::Cop::Migration::RemoveColumn do context 'outside of a migration' do it 'registers no offense' do - inspect_source(cop, source) + inspect_source(source) expect(cop.offenses.size).to eq(0) end diff --git a/spec/rubocop/cop/migration/remove_concurrent_index_spec.rb b/spec/rubocop/cop/migration/remove_concurrent_index_spec.rb index a714bf4e5d5..a23d5d022e3 100644 --- a/spec/rubocop/cop/migration/remove_concurrent_index_spec.rb +++ b/spec/rubocop/cop/migration/remove_concurrent_index_spec.rb @@ -16,7 +16,7 @@ describe RuboCop::Cop::Migration::RemoveConcurrentIndex do end it 'registers an offense when remove_concurrent_index is used inside a change method' do - inspect_source(cop, 'def change; remove_concurrent_index :table, :column; end') + inspect_source('def change; remove_concurrent_index :table, :column; end') aggregate_failures do expect(cop.offenses.size).to eq(1) @@ -25,7 +25,7 @@ describe RuboCop::Cop::Migration::RemoveConcurrentIndex do end it 'registers no offense when remove_concurrent_index is used inside an up method' do - inspect_source(cop, 'def up; remove_concurrent_index :table, :column; end') + inspect_source('def up; remove_concurrent_index :table, :column; end') expect(cop.offenses.size).to eq(0) end @@ -33,7 +33,7 @@ describe RuboCop::Cop::Migration::RemoveConcurrentIndex do context 'outside of migration' do it 'registers no offense' do - inspect_source(cop, 'def change; remove_concurrent_index :table, :column; end') + inspect_source('def change; remove_concurrent_index :table, :column; end') expect(cop.offenses.size).to eq(0) end diff --git a/spec/rubocop/cop/migration/remove_index_spec.rb b/spec/rubocop/cop/migration/remove_index_spec.rb index 31923cb7429..bbf2227e512 100644 --- a/spec/rubocop/cop/migration/remove_index_spec.rb +++ b/spec/rubocop/cop/migration/remove_index_spec.rb @@ -16,7 +16,7 @@ describe RuboCop::Cop::Migration::RemoveIndex do end it 'registers an offense when remove_index is used' do - inspect_source(cop, 'def change; remove_index :table, :column; end') + inspect_source('def change; remove_index :table, :column; end') aggregate_failures do expect(cop.offenses.size).to eq(1) @@ -27,7 +27,7 @@ describe RuboCop::Cop::Migration::RemoveIndex do context 'outside of migration' do it 'registers no offense' do - inspect_source(cop, 'def change; remove_index :table, :column; end') + inspect_source('def change; remove_index :table, :column; end') expect(cop.offenses.size).to eq(0) end diff --git a/spec/rubocop/cop/migration/reversible_add_column_with_default_spec.rb b/spec/rubocop/cop/migration/reversible_add_column_with_default_spec.rb index 3723d635083..ba8cd2c6c4a 100644 --- a/spec/rubocop/cop/migration/reversible_add_column_with_default_spec.rb +++ b/spec/rubocop/cop/migration/reversible_add_column_with_default_spec.rb @@ -16,7 +16,7 @@ describe RuboCop::Cop::Migration::ReversibleAddColumnWithDefault do end it 'registers an offense when add_column_with_default is used inside a change method' do - inspect_source(cop, 'def change; add_column_with_default :table, :column, default: false; end') + inspect_source('def change; add_column_with_default :table, :column, default: false; end') aggregate_failures do expect(cop.offenses.size).to eq(1) @@ -25,7 +25,7 @@ describe RuboCop::Cop::Migration::ReversibleAddColumnWithDefault do end it 'registers no offense when add_column_with_default is used inside an up method' do - inspect_source(cop, 'def up; add_column_with_default :table, :column, default: false; end') + inspect_source('def up; add_column_with_default :table, :column, default: false; end') expect(cop.offenses.size).to eq(0) end @@ -33,7 +33,7 @@ describe RuboCop::Cop::Migration::ReversibleAddColumnWithDefault do context 'outside of migration' do it 'registers no offense' do - inspect_source(cop, 'def change; add_column_with_default :table, :column, default: false; end') + inspect_source('def change; add_column_with_default :table, :column, default: false; end') expect(cop.offenses.size).to eq(0) end diff --git a/spec/rubocop/cop/migration/safer_boolean_column_spec.rb b/spec/rubocop/cop/migration/safer_boolean_column_spec.rb index 1006594499a..1c4f18fbcc3 100644 --- a/spec/rubocop/cop/migration/safer_boolean_column_spec.rb +++ b/spec/rubocop/cop/migration/safer_boolean_column_spec.rb @@ -32,7 +32,7 @@ describe RuboCop::Cop::Migration::SaferBooleanColumn do sources_and_offense.each do |source, offense| context "given the source \"#{source}\"" do it "registers the offense matching \"#{offense}\"" do - inspect_source(cop, source) + inspect_source(source) aggregate_failures do expect(cop.offenses.first.message).to match(offense) @@ -49,7 +49,7 @@ describe RuboCop::Cop::Migration::SaferBooleanColumn do inoffensive_sources.each do |source| context "given the source \"#{source}\"" do it "registers no offense" do - inspect_source(cop, source) + inspect_source(source) aggregate_failures do expect(cop.offenses).to be_empty @@ -61,14 +61,14 @@ describe RuboCop::Cop::Migration::SaferBooleanColumn do end it 'registers no offense for tables not listed in SMALL_TABLES' do - inspect_source(cop, "add_column :large_table, :column, :boolean") + inspect_source("add_column :large_table, :column, :boolean") expect(cop.offenses).to be_empty end it 'registers no offense for non-boolean columns' do table = described_class::SMALL_TABLES.sample - inspect_source(cop, "add_column :#{table}, :column, :string") + inspect_source("add_column :#{table}, :column, :string") expect(cop.offenses).to be_empty end @@ -77,7 +77,7 @@ describe RuboCop::Cop::Migration::SaferBooleanColumn do context 'outside of migration' do it 'registers no offense' do table = described_class::SMALL_TABLES.sample - inspect_source(cop, "add_column :#{table}, :column, :boolean") + inspect_source("add_column :#{table}, :column, :boolean") expect(cop.offenses).to be_empty end diff --git a/spec/rubocop/cop/migration/timestamps_spec.rb b/spec/rubocop/cop/migration/timestamps_spec.rb index cdf1423d0c5..685bdb21803 100644 --- a/spec/rubocop/cop/migration/timestamps_spec.rb +++ b/spec/rubocop/cop/migration/timestamps_spec.rb @@ -62,7 +62,7 @@ describe RuboCop::Cop::Migration::Timestamps do end it 'registers an offense when the "timestamps" method is used' do - inspect_source(cop, migration_with_timestamps) + inspect_source(migration_with_timestamps) aggregate_failures do expect(cop.offenses.size).to eq(1) @@ -71,7 +71,7 @@ describe RuboCop::Cop::Migration::Timestamps do end it 'does not register an offense when the "timestamps" method is not used' do - inspect_source(cop, migration_without_timestamps) + inspect_source(migration_without_timestamps) aggregate_failures do expect(cop.offenses.size).to eq(0) @@ -79,7 +79,7 @@ describe RuboCop::Cop::Migration::Timestamps do end it 'does not register an offense when the "timestamps_with_timezone" method is used' do - inspect_source(cop, migration_with_timestamps_with_timezone) + inspect_source(migration_with_timestamps_with_timezone) aggregate_failures do expect(cop.offenses.size).to eq(0) @@ -89,9 +89,9 @@ describe RuboCop::Cop::Migration::Timestamps do context 'outside of migration' do it 'registers no offense' do - inspect_source(cop, migration_with_timestamps) - inspect_source(cop, migration_without_timestamps) - inspect_source(cop, migration_with_timestamps_with_timezone) + inspect_source(migration_with_timestamps) + inspect_source(migration_without_timestamps) + inspect_source(migration_with_timestamps_with_timezone) expect(cop.offenses.size).to eq(0) end diff --git a/spec/rubocop/cop/migration/update_column_in_batches_spec.rb b/spec/rubocop/cop/migration/update_column_in_batches_spec.rb index 38b8f439a55..1c8ab0ad5d2 100644 --- a/spec/rubocop/cop/migration/update_column_in_batches_spec.rb +++ b/spec/rubocop/cop/migration/update_column_in_batches_spec.rb @@ -1,6 +1,8 @@ require 'spec_helper' + require 'rubocop' require 'rubocop/rspec/support' + require_relative '../../../../rubocop/cop/migration/update_column_in_batches' describe RuboCop::Cop::Migration::UpdateColumnInBatches do @@ -25,7 +27,7 @@ describe RuboCop::Cop::Migration::UpdateColumnInBatches do context 'outside of a migration' do it 'does not register any offenses' do - inspect_source(cop, migration_code) + inspect_source(migration_code) expect(cop.offenses).to be_empty end @@ -49,7 +51,7 @@ describe RuboCop::Cop::Migration::UpdateColumnInBatches do let(:relative_spec_filepath) { Pathname.new(spec_filepath).relative_path_from(tmp_rails_root) } it 'registers an offense when using update_column_in_batches' do - inspect_source(cop, migration_code, @migration_file) + inspect_source(migration_code, @migration_file) aggregate_failures do expect(cop.offenses.size).to eq(1) @@ -72,7 +74,7 @@ describe RuboCop::Cop::Migration::UpdateColumnInBatches do end it 'does not register any offenses' do - inspect_source(cop, migration_code, @migration_file) + inspect_source(migration_code, @migration_file) expect(cop.offenses).to be_empty end diff --git a/spec/rubocop/cop/migration/update_large_table_spec.rb b/spec/rubocop/cop/migration/update_large_table_spec.rb index 17b19e139e4..ef724fc8bad 100644 --- a/spec/rubocop/cop/migration/update_large_table_spec.rb +++ b/spec/rubocop/cop/migration/update_large_table_spec.rb @@ -18,7 +18,7 @@ describe RuboCop::Cop::Migration::UpdateLargeTable do shared_examples 'large tables' do |update_method| described_class::LARGE_TABLES.each do |table| it "registers an offense for the #{table} table" do - inspect_source(cop, "#{update_method} :#{table}, :column, default: true") + inspect_source("#{update_method} :#{table}, :column, default: true") aggregate_failures do expect(cop.offenses.size).to eq(1) @@ -37,7 +37,7 @@ describe RuboCop::Cop::Migration::UpdateLargeTable do end it 'registers no offense for non-blacklisted tables' do - inspect_source(cop, "add_column_with_default :table, :column, default: true") + inspect_source("add_column_with_default :table, :column, default: true") expect(cop.offenses).to be_empty end @@ -45,7 +45,7 @@ describe RuboCop::Cop::Migration::UpdateLargeTable do it 'registers no offense for non-blacklisted methods' do table = described_class::LARGE_TABLES.sample - inspect_source(cop, "some_other_method :#{table}, :column, default: true") + inspect_source("some_other_method :#{table}, :column, default: true") expect(cop.offenses).to be_empty end @@ -55,13 +55,13 @@ describe RuboCop::Cop::Migration::UpdateLargeTable do let(:table) { described_class::LARGE_TABLES.sample } it 'registers no offense for add_column_with_default' do - inspect_source(cop, "add_column_with_default :#{table}, :column, default: true") + inspect_source("add_column_with_default :#{table}, :column, default: true") expect(cop.offenses).to be_empty end it 'registers no offense for update_column_in_batches' do - inspect_source(cop, "add_column_with_default :#{table}, :column, default: true") + inspect_source("add_column_with_default :#{table}, :column, default: true") expect(cop.offenses).to be_empty end diff --git a/spec/rubocop/cop/polymorphic_associations_spec.rb b/spec/rubocop/cop/polymorphic_associations_spec.rb deleted file mode 100644 index 49959aa6419..00000000000 --- a/spec/rubocop/cop/polymorphic_associations_spec.rb +++ /dev/null @@ -1,33 +0,0 @@ -require 'spec_helper' -require 'rubocop' -require 'rubocop/rspec/support' -require_relative '../../../rubocop/cop/polymorphic_associations' - -describe RuboCop::Cop::PolymorphicAssociations do - include CopHelper - - subject(:cop) { described_class.new } - - context 'inside the app/models directory' do - it 'registers an offense when polymorphic: true is used' do - allow(cop).to receive(:in_model?).and_return(true) - - inspect_source(cop, 'belongs_to :foo, polymorphic: true') - - aggregate_failures do - expect(cop.offenses.size).to eq(1) - expect(cop.offenses.map(&:line)).to eq([1]) - end - end - end - - context 'outside the app/models directory' do - it 'does nothing' do - allow(cop).to receive(:in_model?).and_return(false) - - inspect_source(cop, 'belongs_to :foo, polymorphic: true') - - expect(cop.offenses).to be_empty - end - end -end diff --git a/spec/rubocop/cop/project_path_helper_spec.rb b/spec/rubocop/cop/project_path_helper_spec.rb index bc47b45cad7..84e6eb7d87f 100644 --- a/spec/rubocop/cop/project_path_helper_spec.rb +++ b/spec/rubocop/cop/project_path_helper_spec.rb @@ -15,7 +15,7 @@ describe RuboCop::Cop::ProjectPathHelper do let(:correct_source) { 'edit_project_issue_path(@issue.project, @issue)' } it 'registers an offense' do - inspect_source(cop, source) + inspect_source(source) aggregate_failures do expect(cop.offenses.size).to eq(1) @@ -25,7 +25,7 @@ describe RuboCop::Cop::ProjectPathHelper do end it 'autocorrects to the right version' do - autocorrected = autocorrect_source(cop, source) + autocorrected = autocorrect_source(source) expect(autocorrected).to eq(correct_source) end @@ -33,7 +33,7 @@ describe RuboCop::Cop::ProjectPathHelper do context 'when using namespace_project with a different namespace' do it 'registers no offense' do - inspect_source(cop, 'edit_namespace_project_issue_path(namespace, project)') + inspect_source('edit_namespace_project_issue_path(namespace, project)') expect(cop.offenses.size).to eq(0) end diff --git a/spec/rubocop/cop/redirect_with_status_spec.rb b/spec/rubocop/cop/redirect_with_status_spec.rb deleted file mode 100644 index 5ad63567f84..00000000000 --- a/spec/rubocop/cop/redirect_with_status_spec.rb +++ /dev/null @@ -1,86 +0,0 @@ -require 'spec_helper' - -require 'rubocop' -require 'rubocop/rspec/support' - -require_relative '../../../rubocop/cop/redirect_with_status' - -describe RuboCop::Cop::RedirectWithStatus do - include CopHelper - - subject(:cop) { described_class.new } - let(:controller_fixture_without_status) do - %q( - class UserController < ApplicationController - def show - user = User.find(params[:id]) - redirect_to user_path if user.name == 'John Wick' - end - - def destroy - user = User.find(params[:id]) - - if user.destroy - redirect_to root_path - else - render :show - end - end - end - ) - end - - let(:controller_fixture_with_status) do - %q( - class UserController < ApplicationController - def show - user = User.find(params[:id]) - redirect_to user_path if user.name == 'John Wick' - end - - def destroy - user = User.find(params[:id]) - - if user.destroy - redirect_to root_path, status: 302 - else - render :show - end - end - end - ) - end - - context 'in controller' do - before do - allow(cop).to receive(:in_controller?).and_return(true) - end - - it 'registers an offense when a "destroy" action uses "redirect_to" without "status"' do - inspect_source(cop, controller_fixture_without_status) - - aggregate_failures do - expect(cop.offenses.size).to eq(1) - expect(cop.offenses.map(&:line)).to eq([12]) # 'redirect_to' is located on 12th line in controller_fixture. - expect(cop.highlights).to eq(['redirect_to']) - end - end - - it 'does not register an offense when a "destroy" action uses "redirect_to" with "status"' do - inspect_source(cop, controller_fixture_with_status) - - aggregate_failures do - expect(cop.offenses.size).to eq(0) - end - end - end - - context 'outside of controller' do - it 'registers no offense' do - inspect_source(cop, controller_fixture_without_status) - inspect_source(cop, controller_fixture_with_status) - - expect(cop.offenses.size).to eq(0) - end - end -end diff --git a/spec/rubocop/cop/rspec/env_assignment_spec.rb b/spec/rubocop/cop/rspec/env_assignment_spec.rb index 4e859b6f6fa..659633f6467 100644 --- a/spec/rubocop/cop/rspec/env_assignment_spec.rb +++ b/spec/rubocop/cop/rspec/env_assignment_spec.rb @@ -17,7 +17,7 @@ describe RuboCop::Cop::RSpec::EnvAssignment do shared_examples 'an offensive ENV#[]= call' do |content| it "registers an offense for `#{content}`" do - inspect_source(cop, content, source_file) + inspect_source(content, source_file) expect(cop.offenses.size).to eq(1) expect(cop.offenses.map(&:line)).to eq([1]) @@ -27,7 +27,7 @@ describe RuboCop::Cop::RSpec::EnvAssignment do shared_examples 'an autocorrected ENV#[]= call' do |content, autocorrected_content| it "registers an offense for `#{content}` and autocorrects it to `#{autocorrected_content}`" do - autocorrected = autocorrect_source(cop, content, source_file) + autocorrected = autocorrect_source(content, source_file) expect(autocorrected).to eql(autocorrected_content) end @@ -51,7 +51,7 @@ describe RuboCop::Cop::RSpec::EnvAssignment do context 'outside of a spec file' do it "does not register an offense for `#{OFFENSE_CALL_SINGLE_QUOTES_KEY}` in a non-spec file" do - inspect_source(cop, OFFENSE_CALL_SINGLE_QUOTES_KEY) + inspect_source(OFFENSE_CALL_SINGLE_QUOTES_KEY) expect(cop.offenses.size).to eq(0) end diff --git a/spec/rubocop/cop/rspec/single_line_hook_spec.rb b/spec/rubocop/cop/rspec/single_line_hook_spec.rb deleted file mode 100644 index 6cf0831d3ad..00000000000 --- a/spec/rubocop/cop/rspec/single_line_hook_spec.rb +++ /dev/null @@ -1,66 +0,0 @@ -require 'spec_helper' - -require 'rubocop' -require 'rubocop/rspec/support' - -require_relative '../../../../rubocop/cop/rspec/single_line_hook' - -describe RuboCop::Cop::RSpec::SingleLineHook do - include CopHelper - - subject(:cop) { described_class.new } - - # Override `CopHelper#inspect_source` to always appear to be in a spec file, - # so that our RSpec-only cop actually runs - def inspect_source(*args) - super(*args, 'foo_spec.rb') - end - - it 'registers an offense for a single-line `before` block' do - inspect_source(cop, 'before { do_something }') - - expect(cop.offenses.size).to eq(1) - expect(cop.offenses.map(&:line)).to eq([1]) - expect(cop.highlights).to eq(['before { do_something }']) - end - - it 'registers an offense for a single-line `after` block' do - inspect_source(cop, 'after(:each) { undo_something }') - - expect(cop.offenses.size).to eq(1) - expect(cop.offenses.map(&:line)).to eq([1]) - expect(cop.highlights).to eq(['after(:each) { undo_something }']) - end - - it 'registers an offense for a single-line `around` block' do - inspect_source(cop, 'around { |ex| do_something_else }') - - expect(cop.offenses.size).to eq(1) - expect(cop.offenses.map(&:line)).to eq([1]) - expect(cop.highlights).to eq(['around { |ex| do_something_else }']) - end - - it 'ignores a multi-line `before` block' do - inspect_source(cop, ['before do', - ' do_something', - 'end']) - - expect(cop.offenses.size).to eq(0) - end - - it 'ignores a multi-line `after` block' do - inspect_source(cop, ['after(:each) do', - ' undo_something', - 'end']) - - expect(cop.offenses.size).to eq(0) - end - - it 'ignores a multi-line `around` block' do - inspect_source(cop, ['around do |ex|', - ' do_something_else', - 'end']) - - expect(cop.offenses.size).to eq(0) - end -end diff --git a/spec/rubocop/cop/rspec/verbose_include_metadata_spec.rb b/spec/rubocop/cop/rspec/verbose_include_metadata_spec.rb deleted file mode 100644 index 278662d32ea..00000000000 --- a/spec/rubocop/cop/rspec/verbose_include_metadata_spec.rb +++ /dev/null @@ -1,53 +0,0 @@ -require 'spec_helper' -require 'rubocop' -require 'rubocop/rspec/support' -require_relative '../../../../rubocop/cop/rspec/verbose_include_metadata' - -describe RuboCop::Cop::RSpec::VerboseIncludeMetadata do - include CopHelper - - subject(:cop) { described_class.new } - - let(:source_file) { 'foo_spec.rb' } - - # Override `CopHelper#inspect_source` to always appear to be in a spec file, - # so that our RSpec-only cop actually runs - def inspect_source(*args) - super(*args, source_file) - end - - shared_examples 'examples with include syntax' do |title| - it "flags violation for #{title} examples that uses verbose include syntax" do - inspect_source(cop, "#{title} 'Test', js: true do; end") - - expect(cop.offenses.size).to eq(1) - offense = cop.offenses.first - - expect(offense.line).to eq(1) - expect(cop.highlights).to eq(["#{title} 'Test', js: true"]) - expect(offense.message).to eq('Use `:js` instead of `js: true`.') - end - - it "doesn't flag violation for #{title} examples that uses compact include syntax", :aggregate_failures do - inspect_source(cop, "#{title} 'Test', :js do; end") - - expect(cop.offenses).to be_empty - end - - it "doesn't flag violation for #{title} examples that uses flag: symbol" do - inspect_source(cop, "#{title} 'Test', flag: :symbol do; end") - - expect(cop.offenses).to be_empty - end - - it "autocorrects #{title} examples that uses verbose syntax into compact syntax" do - autocorrected = autocorrect_source(cop, "#{title} 'Test', js: true do; end", source_file) - - expect(autocorrected).to eql("#{title} 'Test', :js do; end") - end - end - - %w(describe context feature example_group it specify example scenario its).each do |example| - it_behaves_like 'examples with include syntax', example - end -end diff --git a/spec/rubocop/cop/sidekiq_options_queue_spec.rb b/spec/rubocop/cop/sidekiq_options_queue_spec.rb index a31de381631..7f237d5ffbb 100644 --- a/spec/rubocop/cop/sidekiq_options_queue_spec.rb +++ b/spec/rubocop/cop/sidekiq_options_queue_spec.rb @@ -1,6 +1,8 @@ require 'spec_helper' + require 'rubocop' require 'rubocop/rspec/support' + require_relative '../../../rubocop/cop/sidekiq_options_queue' describe RuboCop::Cop::SidekiqOptionsQueue do @@ -9,7 +11,7 @@ describe RuboCop::Cop::SidekiqOptionsQueue do subject(:cop) { described_class.new } it 'registers an offense when `sidekiq_options` is used with the `queue` option' do - inspect_source(cop, 'sidekiq_options queue: "some_queue"') + inspect_source('sidekiq_options queue: "some_queue"') aggregate_failures do expect(cop.offenses.size).to eq(1) @@ -19,7 +21,7 @@ describe RuboCop::Cop::SidekiqOptionsQueue do end it 'does not register an offense when `sidekiq_options` is used with another option' do - inspect_source(cop, 'sidekiq_options retry: false') + inspect_source('sidekiq_options retry: false') expect(cop.offenses).to be_empty end diff --git a/spec/serializers/cluster_application_entity_spec.rb b/spec/serializers/cluster_application_entity_spec.rb index 87c7b2ad36e..b5a55b4ef6e 100644 --- a/spec/serializers/cluster_application_entity_spec.rb +++ b/spec/serializers/cluster_application_entity_spec.rb @@ -2,7 +2,7 @@ require 'spec_helper' describe ClusterApplicationEntity do describe '#as_json' do - let(:application) { build(:cluster_applications_helm) } + let(:application) { build(:clusters_applications_helm) } subject { described_class.new(application).as_json } it 'has name' do @@ -18,7 +18,7 @@ describe ClusterApplicationEntity do end context 'when application is errored' do - let(:application) { build(:cluster_applications_helm, :errored) } + let(:application) { build(:clusters_applications_helm, :errored) } it 'has corresponded data' do expect(subject[:status]).to eq(:errored) diff --git a/spec/services/clusters/applications/check_installation_progress_service_spec.rb b/spec/services/clusters/applications/check_installation_progress_service_spec.rb index 75fc05d36e9..6894c1797b0 100644 --- a/spec/services/clusters/applications/check_installation_progress_service_spec.rb +++ b/spec/services/clusters/applications/check_installation_progress_service_spec.rb @@ -3,7 +3,7 @@ require 'spec_helper' describe Clusters::Applications::CheckInstallationProgressService do RESCHEDULE_PHASES = Gitlab::Kubernetes::Pod::PHASES - [Gitlab::Kubernetes::Pod::SUCCEEDED, Gitlab::Kubernetes::Pod::FAILED].freeze - let(:application) { create(:cluster_applications_helm, :installing) } + let(:application) { create(:clusters_applications_helm, :installing) } let(:service) { described_class.new(application) } let(:phase) { Gitlab::Kubernetes::Pod::UNKNOWN } let(:errors) { nil } @@ -33,7 +33,7 @@ describe Clusters::Applications::CheckInstallationProgressService do end context 'when timeouted' do - let(:application) { create(:cluster_applications_helm, :timeouted) } + let(:application) { create(:clusters_applications_helm, :timeouted) } it_behaves_like 'a terminated installation' diff --git a/spec/services/clusters/applications/install_service_spec.rb b/spec/services/clusters/applications/install_service_spec.rb index 054a49ffedf..ad175226e92 100644 --- a/spec/services/clusters/applications/install_service_spec.rb +++ b/spec/services/clusters/applications/install_service_spec.rb @@ -2,17 +2,19 @@ require 'spec_helper' describe Clusters::Applications::InstallService do describe '#execute' do - let(:application) { create(:cluster_applications_helm, :scheduled) } + let(:application) { create(:clusters_applications_helm, :scheduled) } + let!(:install_command) { application.install_command } let(:service) { described_class.new(application) } - let(:helm_client) { instance_double(Gitlab::Kubernetes::Helm) } + let(:helm_client) { instance_double(Gitlab::Kubernetes::Helm::Api) } before do + allow(service).to receive(:install_command).and_return(install_command) allow(service).to receive(:helm_api).and_return(helm_client) end context 'when there are no errors' do before do - expect(helm_client).to receive(:install).with(application.install_command) + expect(helm_client).to receive(:install).with(install_command) allow(ClusterWaitForAppInstallationWorker).to receive(:perform_in).and_return(nil) end @@ -33,7 +35,7 @@ describe Clusters::Applications::InstallService do context 'when k8s cluster communication fails' do before do error = KubeException.new(500, 'system failure', nil) - expect(helm_client).to receive(:install).with(application.install_command).and_raise(error) + expect(helm_client).to receive(:install).with(install_command).and_raise(error) end it 'make the application errored' do @@ -45,7 +47,7 @@ describe Clusters::Applications::InstallService do end context 'when application cannot be persisted' do - let(:application) { build(:cluster_applications_helm, :scheduled) } + let(:application) { build(:clusters_applications_helm, :scheduled) } it 'make the application errored' do expect(application).to receive(:make_installing!).once.and_raise(ActiveRecord::RecordInvalid) diff --git a/spec/services/clusters/applications/schedule_installation_service_spec.rb b/spec/services/clusters/applications/schedule_installation_service_spec.rb index 047a6e44dab..473dbcbb692 100644 --- a/spec/services/clusters/applications/schedule_installation_service_spec.rb +++ b/spec/services/clusters/applications/schedule_installation_service_spec.rb @@ -34,7 +34,7 @@ describe Clusters::Applications::ScheduleInstallationService do end context 'when installation is already in progress' do - let(:application) { create(:cluster_applications_helm, :installing) } + let(:application) { create(:clusters_applications_helm, :installing) } let(:cluster) { application.cluster } it_behaves_like 'a failing service' diff --git a/spec/services/issues/move_service_spec.rb b/spec/services/issues/move_service_spec.rb index f2b35a8fadf..2cad695d7c4 100644 --- a/spec/services/issues/move_service_spec.rb +++ b/spec/services/issues/move_service_spec.rb @@ -289,6 +289,18 @@ describe Issues::MoveService do .to raise_error(StandardError, /Cannot move issue/) end end + + context 'project issue hooks' do + let(:hook) { create(:project_hook, project: old_project, issues_events: true) } + + it 'executes project issue hooks' do + # Ideally, we'd test that `WebHookWorker.jobs.size` increased by 1, + # but since the entire spec run takes place in a transaction, we never + # actually get to the `after_commit` hook that queues these jobs. + expect { move_service.execute(old_issue, new_project) } + .not_to raise_error # Sidekiq::Worker::EnqueueFromTransactionError + end + end end describe 'move permissions' do diff --git a/spec/support/cluster_application_spec.rb b/spec/support/cluster_application_spec.rb new file mode 100644 index 00000000000..ab77910a050 --- /dev/null +++ b/spec/support/cluster_application_spec.rb @@ -0,0 +1,105 @@ +shared_examples 'cluster application specs' do + let(:factory_name) { described_class.to_s.downcase.gsub("::", "_") } + + describe '#name' do + it 'is .application_name' do + expect(subject.name).to eq(described_class.application_name) + end + + it 'is recorded in Clusters::Cluster::APPLICATIONS' do + expect(Clusters::Cluster::APPLICATIONS[subject.name]).to eq(described_class) + end + end + + describe '#status' do + let(:cluster) { create(:cluster, :provided_by_gcp) } + + subject { described_class.new(cluster: cluster) } + + it 'defaults to :not_installable' do + expect(subject.status_name).to be(:not_installable) + end + + context 'when application helm is scheduled' do + before do + create(factory_name, :scheduled, cluster: cluster) + end + + it 'defaults to :not_installable' do + expect(subject.status_name).to be(:not_installable) + end + end + + context 'when application helm is installed' do + before do + create(:clusters_applications_helm, :installed, cluster: cluster) + end + + it 'defaults to :installable' do + expect(subject.status_name).to be(:installable) + end + end + end + + describe '#install_command' do + it 'has all the needed information' do + expect(subject.install_command).to have_attributes(name: subject.name, install_helm: false) + end + end + + describe 'status state machine' do + describe '#make_installing' do + subject { create(factory_name, :scheduled) } + + it 'is installing' do + subject.make_installing! + + expect(subject).to be_installing + end + end + + describe '#make_installed' do + subject { create(factory_name, :installing) } + + it 'is installed' do + subject.make_installed + + expect(subject).to be_installed + end + end + + describe '#make_errored' do + subject { create(factory_name, :installing) } + let(:reason) { 'some errors' } + + it 'is errored' do + subject.make_errored(reason) + + expect(subject).to be_errored + expect(subject.status_reason).to eq(reason) + end + end + + describe '#make_scheduled' do + subject { create(factory_name, :installable) } + + it 'is scheduled' do + subject.make_scheduled + + expect(subject).to be_scheduled + end + + describe 'when was errored' do + subject { create(factory_name, :errored) } + + it 'clears #status_reason' do + expect(subject.status_reason).not_to be_nil + + subject.make_scheduled! + + expect(subject.status_reason).to be_nil + end + end + end + end +end |