Commit 23008948 authored by charlie ablett's avatar charlie ablett

Merge branch '331631-apifuzzing-update-ci' into 'master'

Promote unstable API Fuzzing CI template to stable (breaking)

See merge request gitlab-org/gitlab!62407
parents c235f967 1537a8a3
...@@ -4,12 +4,9 @@ require 'spec_helper' ...@@ -4,12 +4,9 @@ require 'spec_helper'
RSpec.describe "CI YML Templates" do RSpec.describe "CI YML Templates" do
using RSpec::Parameterized::TableSyntax using RSpec::Parameterized::TableSyntax
subject { Gitlab::Ci::YamlProcessor.new(content).execute } subject { Gitlab::Ci::YamlProcessor.new(content).execute }
where(:template_name) do let(:all_templates) { Gitlab::Template::GitlabCiYmlTemplate.all.map(&:full_name) }
Gitlab::Template::GitlabCiYmlTemplate.all.map(&:full_name)
end
before do before do
stub_feature_flags( stub_feature_flags(
...@@ -19,27 +16,24 @@ RSpec.describe "CI YML Templates" do ...@@ -19,27 +16,24 @@ RSpec.describe "CI YML Templates" do
redirect_to_latest_template_jobs_browser_performance_testing: false) redirect_to_latest_template_jobs_browser_performance_testing: false)
end end
with_them do shared_examples 'require default stages to be included' do
let(:content) do it 'require default stages to be included' do
if template_name == 'Security/DAST-API.gitlab-ci.yml' expect(subject.stages).to include(*Gitlab::Ci::Config::Entry::Stages.default)
# The DAST-API template purposly excludes a stages end
# definition. end
<<~EOS context 'that support autodevops' do
include: non_autodevops_templates = [
- template: #{template_name} 'Security/DAST-API.gitlab-ci.yml',
'Security/API-Fuzzing.gitlab-ci.yml'
]
stages: where(:template_name) do
- build all_templates - non_autodevops_templates
- test end
- deploy
- dast
concrete_build_implemented_by_a_user: with_them do
stage: test let(:content) do
script: do something
EOS
else
<<~EOS <<~EOS
include: include:
- template: #{template_name} - template: #{template_name}
...@@ -49,14 +43,104 @@ RSpec.describe "CI YML Templates" do ...@@ -49,14 +43,104 @@ RSpec.describe "CI YML Templates" do
script: do something script: do something
EOS EOS
end end
it 'are valid with default stages' do
expect(subject).to be_valid
end
include_examples 'require default stages to be included'
end end
end
context 'that do not support autodevops' do
context 'when DAST API template' do
# The DAST API template purposly excludes a stages
# definition.
let(:template_name) { 'Security/DAST-API.gitlab-ci.yml' }
context 'with default stages' do
let(:content) do
<<~EOS
include:
- template: #{template_name}
it 'is valid' do concrete_build_implemented_by_a_user:
expect(subject).to be_valid stage: test
script: do something
EOS
end
it { is_expected.not_to be_valid }
end
context 'with defined stages' do
let(:content) do
<<~EOS
include:
- template: #{template_name}
stages:
- build
- test
- deploy
- dast
concrete_build_implemented_by_a_user:
stage: test
script: do something
EOS
end
it { is_expected.to be_valid }
include_examples 'require default stages to be included'
end
end end
it 'require default stages to be included' do context 'when API Fuzzing template' do
expect(subject.stages).to include(*Gitlab::Ci::Config::Entry::Stages.default) # The API Fuzzing template purposly excludes a stages
# definition.
let(:template_name) { 'Security/API-Fuzzing.gitlab-ci.yml' }
context 'with default stages' do
let(:content) do
<<~EOS
include:
- template: #{template_name}
concrete_build_implemented_by_a_user:
stage: test
script: do something
EOS
end
it { is_expected.not_to be_valid }
end
context 'with defined stages' do
let(:content) do
<<~EOS
include:
- template: #{template_name}
stages:
- build
- test
- deploy
- fuzz
concrete_build_implemented_by_a_user:
stage: test
script: do something
EOS
end
it { is_expected.to be_valid }
include_examples 'require default stages to be included'
end
end end
end end
end end
...@@ -5,6 +5,8 @@ require 'spec_helper' ...@@ -5,6 +5,8 @@ require 'spec_helper'
RSpec.describe 'API-Fuzzing.gitlab-ci.yml' do RSpec.describe 'API-Fuzzing.gitlab-ci.yml' do
subject(:template) { Gitlab::Template::GitlabCiYmlTemplate.find('API-Fuzzing') } subject(:template) { Gitlab::Template::GitlabCiYmlTemplate.find('API-Fuzzing') }
specify { expect(template).not_to be_nil }
describe 'the template file' do describe 'the template file' do
let(:template_filename) { Rails.root.join("lib/gitlab/ci/templates/" + template.full_name) } let(:template_filename) { Rails.root.join("lib/gitlab/ci/templates/" + template.full_name) }
let(:contents) { File.read(template_filename) } let(:contents) { File.read(template_filename) }
...@@ -36,106 +38,103 @@ RSpec.describe 'API-Fuzzing.gitlab-ci.yml' do ...@@ -36,106 +38,103 @@ RSpec.describe 'API-Fuzzing.gitlab-ci.yml' do
let(:pipeline) { service.execute!(:push) } let(:pipeline) { service.execute!(:push) }
let(:build_names) { pipeline.builds.pluck(:name) } let(:build_names) { pipeline.builds.pluck(:name) }
before do context 'when no stages' do
stub_ci_pipeline_yaml_file(template.content)
allow_next_instance_of(Ci::BuildScheduleWorker) do |worker|
allow(worker).to receive(:perform).and_return(true)
end
allow(project).to receive(:default_branch).and_return(default_branch)
end
context 'when project has no license' do
before do before do
create(:ci_variable, project: project, key: 'FUZZAPI_HAR', value: 'testing.har') stub_ci_pipeline_yaml_file(template.content)
create(:ci_variable, project: project, key: 'FUZZAPI_TARGET_URL', value: 'http://example.com') allow_next_instance_of(Ci::BuildScheduleWorker) do |worker|
allow(worker).to receive(:perform).and_return(true)
end
allow(project).to receive(:default_branch).and_return(default_branch)
end end
it 'includes job to display error' do context 'when project has no stages' do
expect(build_names).to match_array(%w[apifuzzer_fuzz_unlicensed]) it 'includes no jobs' do
expect(build_names).to be_empty
end
end end
end end
context 'when project has Ultimate license' do context 'when stages includes fuzz' do
let(:license) { build(:license, plan: License::ULTIMATE_PLAN) } let(:ci_pipeline_yaml) { "stages: [\"fuzz\"]\n" }
before do before do
allow(License).to receive(:current).and_return(license) stub_ci_pipeline_yaml_file(ci_pipeline_yaml + template.content)
end
context 'by default' do allow_next_instance_of(Ci::BuildScheduleWorker) do |worker|
it 'includes a job' do allow(worker).to receive(:perform).and_return(true)
expect(build_names).to match_array(%w[apifuzzer_fuzz])
end end
allow(project).to receive(:default_branch).and_return(default_branch)
end end
context 'when configured with HAR' do context 'when project has no license' do
before do before do
create(:ci_variable, project: project, key: 'FUZZAPI_HAR', value: 'testing.har') create(:ci_variable, project: project, key: 'FUZZAPI_HAR', value: 'testing.har')
create(:ci_variable, project: project, key: 'FUZZAPI_TARGET_URL', value: 'http://example.com') create(:ci_variable, project: project, key: 'FUZZAPI_TARGET_URL', value: 'http://example.com')
end end
it 'includes job' do it 'includes job to display error' do
expect(build_names).to match_array(%w[apifuzzer_fuzz]) expect(build_names).to match_array(%w[apifuzzer_fuzz])
end end
end end
context 'when configured with OpenAPI' do context 'when project has Ultimate license' do
let(:license) { build(:license, plan: License::ULTIMATE_PLAN) }
before do before do
create(:ci_variable, project: project, key: 'FUZZAPI_OPENAPI', value: 'testing.json') allow(License).to receive(:current).and_return(license)
create(:ci_variable, project: project, key: 'FUZZAPI_TARGET_URL', value: 'http://example.com')
end end
it 'includes job' do context 'by default' do
expect(build_names).to match_array(%w[apifuzzer_fuzz]) it 'includes a job' do
expect(build_names).to match_array(%w[apifuzzer_fuzz])
end
end end
end
context 'when configured with Postman' do context 'when configured with HAR' do
before do before do
create(:ci_variable, project: project, key: 'FUZZAPI_POSTMAN_COLLECTION', value: 'testing.json') create(:ci_variable, project: project, key: 'FUZZAPI_HAR', value: 'testing.har')
create(:ci_variable, project: project, key: 'FUZZAPI_TARGET_URL', value: 'http://example.com') create(:ci_variable, project: project, key: 'FUZZAPI_TARGET_URL', value: 'http://example.com')
end end
it 'includes job' do it 'includes job' do
expect(build_names).to match_array(%w[apifuzzer_fuzz]) expect(build_names).to match_array(%w[apifuzzer_fuzz])
end
end end
end
context 'when FUZZAPI_D_TARGET_IMAGE is present' do context 'when configured with OpenAPI' do
before do before do
create(:ci_variable, project: project, key: 'FUZZAPI_D_TARGET_IMAGE', value: 'imagename:latest') create(:ci_variable, project: project, key: 'FUZZAPI_OPENAPI', value: 'testing.json')
create(:ci_variable, project: project, key: 'FUZZAPI_HAR', value: 'testing.har') create(:ci_variable, project: project, key: 'FUZZAPI_TARGET_URL', value: 'http://example.com')
create(:ci_variable, project: project, key: 'FUZZAPI_TARGET_URL', value: 'http://example.com') end
end
it 'includes dnd job' do it 'includes job' do
expect(build_names).to match_array(%w[apifuzzer_fuzz_dnd]) expect(build_names).to match_array(%w[apifuzzer_fuzz])
end
end end
end
end
context 'when API_FUZZING_DISABLED=1' do context 'when configured with Postman' do
before do before do
create(:ci_variable, project: project, key: 'API_FUZZING_DISABLED', value: '1') create(:ci_variable, project: project, key: 'FUZZAPI_POSTMAN_COLLECTION', value: 'testing.json')
create(:ci_variable, project: project, key: 'FUZZAPI_HAR', value: 'testing.har') create(:ci_variable, project: project, key: 'FUZZAPI_TARGET_URL', value: 'http://example.com')
create(:ci_variable, project: project, key: 'FUZZAPI_TARGET_URL', value: 'http://example.com') end
end
it 'includes no jobs' do it 'includes job' do
expect { pipeline }.to raise_error(Ci::CreatePipelineService::CreateError) expect(build_names).to match_array(%w[apifuzzer_fuzz])
end end
end end
context 'when API_FUZZING_DISABLED=1 with DnD' do context 'when API_FUZZING_DISABLED=1' do
before do before do
create(:ci_variable, project: project, key: 'API_FUZZING_DISABLED', value: '1') create(:ci_variable, project: project, key: 'API_FUZZING_DISABLED', value: '1')
create(:ci_variable, project: project, key: 'FUZZAPI_D_TARGET_IMAGE', value: 'imagename:latest') create(:ci_variable, project: project, key: 'FUZZAPI_HAR', value: 'testing.har')
create(:ci_variable, project: project, key: 'FUZZAPI_HAR', value: 'testing.har') create(:ci_variable, project: project, key: 'FUZZAPI_TARGET_URL', value: 'http://example.com')
create(:ci_variable, project: project, key: 'FUZZAPI_TARGET_URL', value: 'http://example.com') end
end
it 'includes no jobs' do it 'includes no jobs' do
expect { pipeline }.to raise_error(Ci::CreatePipelineService::CreateError) expect { pipeline }.to raise_error(Ci::CreatePipelineService::CreateError)
end
end
end end
end end
end end
......
...@@ -6,33 +6,104 @@ RSpec.describe 'CI YML Templates' do ...@@ -6,33 +6,104 @@ RSpec.describe 'CI YML Templates' do
subject { Gitlab::Ci::YamlProcessor.new(content).execute } subject { Gitlab::Ci::YamlProcessor.new(content).execute }
let(:all_templates) { Gitlab::Template::GitlabCiYmlTemplate.all.map(&:full_name) } let(:all_templates) { Gitlab::Template::GitlabCiYmlTemplate.all.map(&:full_name) }
let(:excluded_templates) do let(:excluded_templates) do
all_templates.select do |name| all_templates.select do |name|
Gitlab::Template::GitlabCiYmlTemplate.excluded_patterns.any? { |pattern| pattern.match?(name) } Gitlab::Template::GitlabCiYmlTemplate.excluded_patterns.any? { |pattern| pattern.match?(name) }
end end
end end
context 'when including available templates in a CI YAML configuration' do before do
using RSpec::Parameterized::TableSyntax stub_feature_flags(
redirect_to_latest_template_terraform: false,
redirect_to_latest_template_security_api_fuzzing: false,
redirect_to_latest_template_security_dast: false)
end
shared_examples 'require default stages to be included' do
it 'require default stages to be included' do
expect(subject.stages).to include(*Gitlab::Ci::Config::Entry::Stages.default)
end
end
context 'that support autodevops' do
non_autodevops_templates = [
'Security/DAST-API.gitlab-ci.yml',
'Security/API-Fuzzing.gitlab-ci.yml'
]
context 'when including available templates in a CI YAML configuration' do
using RSpec::Parameterized::TableSyntax
where(:template_name) do
all_templates - excluded_templates - non_autodevops_templates
end
with_them do
let(:content) do
<<~EOS
include:
- template: #{template_name}
concrete_build_implemented_by_a_user:
stage: test
script: do something
EOS
end
it { is_expected.to be_valid }
where(:template_name) do include_examples 'require default stages to be included'
all_templates - excluded_templates end
end end
before do context 'when including unavailable templates in a CI YAML configuration' do
stub_feature_flags( using RSpec::Parameterized::TableSyntax
redirect_to_latest_template_terraform: false,
redirect_to_latest_template_security_api_fuzzing: false, where(:template_name) do
redirect_to_latest_template_security_dast: false) excluded_templates
end
with_them do
let(:content) do
<<~EOS
include:
- template: #{template_name}
concrete_build_implemented_by_a_user:
stage: test
script: do something
EOS
end
it { is_expected.not_to be_valid }
end
end end
end
describe 'that do not support autodevops' do
context 'when DAST API template' do
# The DAST API template purposly excludes a stages
# definition.
let(:template_name) { 'Security/DAST-API.gitlab-ci.yml' }
with_them do context 'with default stages' do
let(:content) do let(:content) do
if template_name == 'Security/DAST-API.gitlab-ci.yml' <<~EOS
# The DAST-API template purposly excludes a stages include:
# definition. - template: #{template_name}
concrete_build_implemented_by_a_user:
stage: test
script: do something
EOS
end
it { is_expected.not_to be_valid }
end
context 'with defined stages' do
let(:content) do
<<~EOS <<~EOS
include: include:
- template: #{template_name} - template: #{template_name}
...@@ -47,7 +118,22 @@ RSpec.describe 'CI YML Templates' do ...@@ -47,7 +118,22 @@ RSpec.describe 'CI YML Templates' do
stage: test stage: test
script: do something script: do something
EOS EOS
else end
it { is_expected.to be_valid }
include_examples 'require default stages to be included'
end
end
context 'when API Fuzzing template' do
# The API Fuzzing template purposly excludes a stages
# definition.
let(:template_name) { 'Security/API-Fuzzing.gitlab-ci.yml' }
context 'with default stages' do
let(:content) do
<<~EOS <<~EOS
include: include:
- template: #{template_name} - template: #{template_name}
...@@ -57,39 +143,31 @@ RSpec.describe 'CI YML Templates' do ...@@ -57,39 +143,31 @@ RSpec.describe 'CI YML Templates' do
script: do something script: do something
EOS EOS
end end
end
it 'is valid' do
expect(subject).to be_valid
end
it 'require default stages to be included' do it { is_expected.not_to be_valid }
expect(subject.stages).to include(*Gitlab::Ci::Config::Entry::Stages.default)
end end
end
end
context 'when including unavailable templates in a CI YAML configuration' do context 'with defined stages' do
using RSpec::Parameterized::TableSyntax let(:content) do
<<~EOS
include:
- template: #{template_name}
where(:template_name) do stages:
excluded_templates - build
end - test
- deploy
- fuzz
with_them do concrete_build_implemented_by_a_user:
let(:content) do stage: test
<<~EOS script: do something
include: EOS
- template: #{template_name} end
concrete_build_implemented_by_a_user: it { is_expected.to be_valid }
stage: test
script: do something
EOS
end
it 'is not valid' do include_examples 'require default stages to be included'
expect(subject).not_to be_valid
end end
end end
end end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment