diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2020-06-18 14:18:50 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2020-06-18 14:18:50 +0300 |
commit | 8c7f4e9d5f36cff46365a7f8c4b9c21578c1e781 (patch) | |
tree | a77e7fe7a93de11213032ed4ab1f33a3db51b738 /spec/lib/gitlab/web_ide | |
parent | 00b35af3db1abfe813a778f643dad221aad51fca (diff) |
Add latest changes from gitlab-org/gitlab@13-1-stable-ee
Diffstat (limited to 'spec/lib/gitlab/web_ide')
-rw-r--r-- | spec/lib/gitlab/web_ide/config/entry/global_spec.rb | 164 | ||||
-rw-r--r-- | spec/lib/gitlab/web_ide/config/entry/terminal_spec.rb | 156 | ||||
-rw-r--r-- | spec/lib/gitlab/web_ide/config_spec.rb | 78 |
3 files changed, 398 insertions, 0 deletions
diff --git a/spec/lib/gitlab/web_ide/config/entry/global_spec.rb b/spec/lib/gitlab/web_ide/config/entry/global_spec.rb new file mode 100644 index 00000000000..04b0752c6fe --- /dev/null +++ b/spec/lib/gitlab/web_ide/config/entry/global_spec.rb @@ -0,0 +1,164 @@ +# frozen_string_literal: true + +require 'spec_helper' + +describe Gitlab::WebIde::Config::Entry::Global do + let(:global) { described_class.new(hash) } + + describe '.nodes' do + it 'returns a hash' do + expect(described_class.nodes).to be_a(Hash) + end + + context 'when filtering all the entry/node names' do + it 'contains the expected node names' do + expect(described_class.nodes.keys) + .to match_array(%i[terminal]) + end + end + end + + context 'when configuration is valid' do + context 'when some entries defined' do + let(:hash) do + { terminal: { before_script: ['ls'], variables: {}, script: 'sleep 10s', services: ['mysql'] } } + end + + describe '#compose!' do + before do + global.compose! + end + + it 'creates nodes hash' do + expect(global.descendants).to be_an Array + end + + it 'creates node object for each entry' do + expect(global.descendants.count).to eq 1 + end + + it 'creates node object using valid class' do + expect(global.descendants.first) + .to be_an_instance_of Gitlab::WebIde::Config::Entry::Terminal + end + + it 'sets correct description for nodes' do + expect(global.descendants.first.description) + .to eq 'Configuration of the webide terminal.' + end + + describe '#leaf?' do + it 'is not leaf' do + expect(global).not_to be_leaf + end + end + end + + context 'when not composed' do + describe '#terminal_value' do + it 'returns nil' do + expect(global.terminal_value).to be nil + end + end + + describe '#leaf?' do + it 'is leaf' do + expect(global).to be_leaf + end + end + end + + context 'when composed' do + before do + global.compose! + end + + describe '#errors' do + it 'has no errors' do + expect(global.errors).to be_empty + end + end + + describe '#terminal_value' do + it 'returns correct script' do + expect(global.terminal_value).to eq({ + tag_list: [], + yaml_variables: [], + options: { + before_script: ['ls'], + script: ['sleep 10s'], + services: [{ name: "mysql" }] + } + }) + end + end + end + end + end + + context 'when configuration is not valid' do + before do + global.compose! + end + + context 'when job does not have valid before script' do + let(:hash) do + { terminal: { before_script: 100 } } + end + + describe '#errors' do + it 'reports errors about missing script' do + expect(global.errors) + .to include "terminal:before_script config should be an array containing strings and arrays of strings" + end + end + end + end + + context 'when value is not a hash' do + let(:hash) { [] } + + describe '#valid?' do + it 'is not valid' do + expect(global).not_to be_valid + end + end + + describe '#errors' do + it 'returns error about invalid type' do + expect(global.errors.first).to match /should be a hash/ + end + end + end + + describe '#specified?' do + it 'is concrete entry that is defined' do + expect(global.specified?).to be true + end + end + + describe '#[]' do + before do + global.compose! + end + + let(:hash) do + { terminal: { before_script: ['ls'] } } + end + + context 'when entry exists' do + it 'returns correct entry' do + expect(global[:terminal]) + .to be_an_instance_of Gitlab::WebIde::Config::Entry::Terminal + expect(global[:terminal][:before_script].value).to eq ['ls'] + end + end + + context 'when entry does not exist' do + it 'always return unspecified node' do + expect(global[:some][:unknown][:node]) + .not_to be_specified + end + end + end +end diff --git a/spec/lib/gitlab/web_ide/config/entry/terminal_spec.rb b/spec/lib/gitlab/web_ide/config/entry/terminal_spec.rb new file mode 100644 index 00000000000..882e389e040 --- /dev/null +++ b/spec/lib/gitlab/web_ide/config/entry/terminal_spec.rb @@ -0,0 +1,156 @@ +# frozen_string_literal: true + +require 'spec_helper' + +describe Gitlab::WebIde::Config::Entry::Terminal do + let(:entry) { described_class.new(config, with_image_ports: true) } + + describe '.nodes' do + context 'when filtering all the entry/node names' do + subject { described_class.nodes.keys } + + let(:result) do + %i[before_script script image services variables] + end + + it { is_expected.to match_array result } + end + end + + describe 'validations' do + before do + entry.compose! + end + + context 'when entry config value is correct' do + let(:config) { { script: 'rspec' } } + + describe '#valid?' do + it 'is valid' do + expect(entry).to be_valid + end + end + + context 'when the same port is not duplicated' do + let(:config) do + { + image: { name: "ruby", ports: [80] }, + services: [{ name: "mysql", alias: "service1", ports: [81] }, { name: "mysql", alias: "service2", ports: [82] }] + } + end + + describe '#valid?' do + it 'is valid' do + expect(entry).to be_valid + end + end + end + + context 'when unknown port keys detected' do + let(:config) do + { + image: { name: "ruby", ports: [80] }, + services: [{ name: "mysql", alias: "service2", ports: [{ number: 81, invalid_key: 'foobar' }] }] + } + end + + it 'is not valid' do + expect(entry).not_to be_valid + expect(entry.errors.first) + .to match /port config contains unknown keys: invalid_key/ + end + end + end + + context 'when entry value is not correct' do + context 'incorrect config value type' do + let(:config) { ['incorrect'] } + + describe '#errors' do + it 'reports error about a config type' do + expect(entry.errors) + .to include 'terminal config should be a hash' + end + end + end + + context 'when config is empty' do + let(:config) { {} } + + describe '#valid' do + it 'is valid' do + expect(entry).to be_valid + end + end + end + + context 'when unknown keys detected' do + let(:config) { { unknown: true } } + + describe '#valid' do + it 'is not valid' do + expect(entry).not_to be_valid + end + end + end + + context 'when the same port is duplicated' do + let(:config) do + { + image: { name: "ruby", ports: [80] }, + services: [{ name: "mysql", ports: [80] }, { name: "mysql", ports: [81] }] + } + end + + describe '#valid?' do + it 'is invalid' do + expect(entry).not_to be_valid + expect(entry.errors.count).to eq 1 + expect(entry.errors.first).to match "each port number can only be referenced once" + end + end + end + end + end + + describe '#relevant?' do + it 'is a relevant entry' do + entry = described_class.new({ script: 'rspec' }) + + expect(entry).to be_relevant + end + end + + context 'when composed' do + before do + entry.compose! + end + + describe '#value' do + context 'when entry is correct' do + let(:config) do + { before_script: %w[ls pwd], + script: 'sleep 100', + tags: ['webide'], + image: 'ruby:2.5', + services: ['mysql'], + variables: { KEY: 'value' } } + end + + it 'returns correct value' do + expect(entry.value) + .to eq( + tag_list: ['webide'], + yaml_variables: [{ key: 'KEY', value: 'value', public: true }], + options: { + image: { name: "ruby:2.5" }, + services: [{ name: "mysql" }], + before_script: %w[ls pwd], + script: ['sleep 100'] + } + ) + end + end + end + end +end diff --git a/spec/lib/gitlab/web_ide/config_spec.rb b/spec/lib/gitlab/web_ide/config_spec.rb new file mode 100644 index 00000000000..c1dafd01197 --- /dev/null +++ b/spec/lib/gitlab/web_ide/config_spec.rb @@ -0,0 +1,78 @@ +# frozen_string_literal: true + +require 'spec_helper' + +describe Gitlab::WebIde::Config do + let(:config) do + described_class.new(yml) + end + + context 'when config is valid' do + let(:yml) do + <<-EOS + terminal: + image: ruby:2.7 + before_script: + - gem install rspec + EOS + end + + describe '#to_hash' do + it 'returns hash created from string' do + hash = { + terminal: { + image: 'ruby:2.7', + before_script: ['gem install rspec'] + } + } + + expect(config.to_hash).to eq hash + end + + describe '#valid?' do + it 'is valid' do + expect(config).to be_valid + end + + it 'has no errors' do + expect(config.errors).to be_empty + end + end + end + end + + context 'when config is invalid' do + context 'when yml is incorrect' do + let(:yml) { '// invalid' } + + describe '.new' do + it 'raises error' do + expect { config }.to raise_error( + described_class::ConfigError, + /Invalid configuration format/ + ) + end + end + end + + context 'when config logic is incorrect' do + let(:yml) { 'terminal: { before_script: "ls" }' } + + describe '#valid?' do + it 'is not valid' do + expect(config).not_to be_valid + end + + it 'has errors' do + expect(config.errors).not_to be_empty + end + end + + describe '#errors' do + it 'returns an array of strings' do + expect(config.errors).to all(be_an_instance_of(String)) + end + end + end + end +end |