diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2019-10-17 00:07:22 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2019-10-17 00:07:22 +0300 |
commit | e924e9e7cb9df21b3bc3d51d5f955da28ba3a225 (patch) | |
tree | 598ccb6f09e55ad06e628a90d27628f20ae693fe /spec | |
parent | 8e45d25f7dde6508839ffee719c0ddc2cf6b12d3 (diff) |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'spec')
-rw-r--r-- | spec/db/schema_spec.rb | 2 | ||||
-rw-r--r-- | spec/factories/aws/roles.rb | 10 | ||||
-rw-r--r-- | spec/factories/clusters/clusters.rb | 8 | ||||
-rw-r--r-- | spec/factories/clusters/providers/aws.rb | 38 | ||||
-rw-r--r-- | spec/lib/gitlab/danger/roulette_spec.rb | 16 | ||||
-rw-r--r-- | spec/lib/gitlab/danger/teammate_spec.rb | 22 | ||||
-rw-r--r-- | spec/lib/gitlab/regex_spec.rb | 11 | ||||
-rw-r--r-- | spec/models/aws/role_spec.rb | 34 | ||||
-rw-r--r-- | spec/models/clusters/cluster_spec.rb | 35 | ||||
-rw-r--r-- | spec/models/clusters/providers/aws_spec.rb | 76 | ||||
-rw-r--r-- | spec/models/project_services/jira_service_spec.rb | 34 | ||||
-rw-r--r-- | spec/support/shared_examples/models/clusters/providers/provider_status.rb | 2 |
12 files changed, 266 insertions, 22 deletions
diff --git a/spec/db/schema_spec.rb b/spec/db/schema_spec.rb index 5216683bd36..53f4a261092 100644 --- a/spec/db/schema_spec.rb +++ b/spec/db/schema_spec.rb @@ -19,6 +19,7 @@ describe 'Database schema' do approver_groups: %w[target_id], audit_events: %w[author_id entity_id], award_emoji: %w[awardable_id user_id], + aws_roles: %w[role_external_id], boards: %w[milestone_id], chat_names: %w[chat_id service_id team_id user_id], chat_teams: %w[team_id], @@ -26,6 +27,7 @@ describe 'Database schema' do ci_pipelines: %w[user_id], ci_runner_projects: %w[runner_id], ci_trigger_requests: %w[commit_id], + cluster_providers_aws: %w[security_group_id vpc_id access_key_id], cluster_providers_gcp: %w[gcp_project_id operation_id], deploy_keys_projects: %w[deploy_key_id], deployments: %w[deployable_id environment_id user_id], diff --git a/spec/factories/aws/roles.rb b/spec/factories/aws/roles.rb new file mode 100644 index 00000000000..c078033dfad --- /dev/null +++ b/spec/factories/aws/roles.rb @@ -0,0 +1,10 @@ +# frozen_string_literal: true + +FactoryBot.define do + factory :aws_role, class: Aws::Role do + user + + role_arn { 'arn:aws:iam::123456789012:role/role-name' } + sequence(:role_external_id) { |n| "external-id-#{n}" } + end +end diff --git a/spec/factories/clusters/clusters.rb b/spec/factories/clusters/clusters.rb index b43a88b39e3..63f33633a3c 100644 --- a/spec/factories/clusters/clusters.rb +++ b/spec/factories/clusters/clusters.rb @@ -53,6 +53,14 @@ FactoryBot.define do platform_kubernetes factory: [:cluster_platform_kubernetes, :configured] end + trait :provided_by_aws do + provider_type { :aws } + platform_type { :kubernetes } + + provider_aws factory: [:cluster_provider_aws, :created] + platform_kubernetes factory: [:cluster_platform_kubernetes, :configured] + end + trait :providing_by_gcp do provider_type { :gcp } provider_gcp factory: [:cluster_provider_gcp, :creating] diff --git a/spec/factories/clusters/providers/aws.rb b/spec/factories/clusters/providers/aws.rb new file mode 100644 index 00000000000..f4bc61455c5 --- /dev/null +++ b/spec/factories/clusters/providers/aws.rb @@ -0,0 +1,38 @@ +# frozen_string_literal: true + +FactoryBot.define do + factory :cluster_provider_aws, class: Clusters::Providers::Aws do + cluster + created_by_user factory: :user + + role_arn { 'arn:aws:iam::123456789012:role/role-name' } + vpc_id { 'vpc-00000000000000000' } + subnet_ids { %w(subnet-00000000000000000 subnet-11111111111111111) } + security_group_id { 'sg-00000000000000000' } + key_name { 'user' } + + trait :scheduled do + access_key_id { 'access_key_id' } + secret_access_key { 'secret_access_key' } + session_token { 'session_token' } + end + + trait :creating do + after(:build) do |provider| + provider.make_creating + end + end + + trait :created do + after(:build) do |provider| + provider.make_created + end + end + + trait :errored do + after(:build) do |provider| + provider.make_errored('An error occurred') + end + end + end +end diff --git a/spec/lib/gitlab/danger/roulette_spec.rb b/spec/lib/gitlab/danger/roulette_spec.rb index 121c5d8ecd9..4d41e2c45aa 100644 --- a/spec/lib/gitlab/danger/roulette_spec.rb +++ b/spec/lib/gitlab/danger/roulette_spec.rb @@ -104,11 +104,13 @@ describe Gitlab::Danger::Roulette do let(:person2) { Gitlab::Danger::Teammate.new('username' => 'godfat') } let(:author) { Gitlab::Danger::Teammate.new('username' => 'filipa') } let(:ooo) { Gitlab::Danger::Teammate.new('username' => 'jacopo-beschi') } + let(:no_capacity) { Gitlab::Danger::Teammate.new('username' => 'uncharged') } before do - stub_person_message(person1, 'making GitLab magic') - stub_person_message(person2, 'making GitLab magic') - stub_person_message(ooo, 'OOO till 15th') + stub_person_status(person1, message: 'making GitLab magic') + stub_person_status(person2, message: 'making GitLab magic') + stub_person_status(ooo, message: 'OOO till 15th') + stub_person_status(no_capacity, message: 'At capacity for the next few days', emoji: 'red_circle') # we don't stub Filipa, as she is the author and # we should not fire request checking for her @@ -131,10 +133,14 @@ describe Gitlab::Danger::Roulette do expect(subject.spin_for_person([author], random: Random.new)).to be_nil end + it 'excludes person with no capacity' do + expect(subject.spin_for_person([no_capacity], random: Random.new)).to be_nil + end + private - def stub_person_message(person, message) - body = { message: message }.to_json + def stub_person_status(person, message: 'dummy message', emoji: 'unicorn') + body = { message: message, emoji: emoji }.to_json WebMock .stub_request(:get, "https://gitlab.com/api/v4/users/#{person.username}/status") diff --git a/spec/lib/gitlab/danger/teammate_spec.rb b/spec/lib/gitlab/danger/teammate_spec.rb index 36486cbbc7d..1e9cf0017be 100644 --- a/spec/lib/gitlab/danger/teammate_spec.rb +++ b/spec/lib/gitlab/danger/teammate_spec.rb @@ -135,17 +135,17 @@ describe Gitlab::Danger::Teammate do end end - describe '#out_of_office?' do + describe '#available?' do using RSpec::Parameterized::TableSyntax let(:capabilities) { ['dry head'] } where(:status, :result) do - nil | false - {} | false - { message: 'dear reader' } | false - { message: 'OOO: massage' } | true - { message: 'love it SOOO much' } | true + {} | true + { message: 'dear reader' } | true + { message: 'OOO: massage' } | false + { message: 'love it SOOO much' } | false + { emoji: 'red_circle' } | false end with_them do @@ -154,7 +154,15 @@ describe Gitlab::Danger::Teammate do .and_return(status&.stringify_keys) end - it { expect(subject.out_of_office?).to be result } + it { expect(subject.available?).to be result } + end + + it 'returns true if request fails' do + expect(Gitlab::Danger::RequestHelper).to receive(:http_get_json) + .exactly(2).times + .and_raise(Gitlab::Danger::RequestHelper::HTTPError.new) + + expect(subject.available?).to be true end end end diff --git a/spec/lib/gitlab/regex_spec.rb b/spec/lib/gitlab/regex_spec.rb index 3036e3a9754..b557baed258 100644 --- a/spec/lib/gitlab/regex_spec.rb +++ b/spec/lib/gitlab/regex_spec.rb @@ -64,4 +64,15 @@ describe Gitlab::Regex do it { is_expected.not_to match('.my/image') } it { is_expected.not_to match('my/image.') } end + + describe '.aws_account_id_regex' do + subject { described_class.aws_arn_regex } + + it { is_expected.to match('arn:aws:iam::123456789012:role/role-name') } + it { is_expected.to match('arn:aws:s3:::bucket/key') } + it { is_expected.to match('arn:aws:ec2:us-east-1:123456789012:volume/vol-1') } + it { is_expected.to match('arn:aws:rds:us-east-1:123456789012:pg:prod') } + it { is_expected.not_to match('123456789012') } + it { is_expected.not_to match('role/role-name') } + end end diff --git a/spec/models/aws/role_spec.rb b/spec/models/aws/role_spec.rb new file mode 100644 index 00000000000..c40752e40a6 --- /dev/null +++ b/spec/models/aws/role_spec.rb @@ -0,0 +1,34 @@ +# frozen_string_literal: true + +require 'spec_helper' + +describe Aws::Role do + it { is_expected.to belong_to(:user) } + it { is_expected.to validate_length_of(:role_external_id).is_at_least(1).is_at_most(64) } + + describe 'custom validations' do + subject { role.valid? } + + context ':role_arn' do + let(:role) { build(:aws_role, role_arn: role_arn) } + + context 'length is zero' do + let(:role_arn) { '' } + + it { is_expected.to be_falsey } + end + + context 'length is longer than 2048' do + let(:role_arn) { '1' * 2049 } + + it { is_expected.to be_falsey } + end + + context 'ARN is valid' do + let(:role_arn) { 'arn:aws:iam::123456789012:role/test-role' } + + it { is_expected.to be_truthy } + end + end + end +end diff --git a/spec/models/clusters/cluster_spec.rb b/spec/models/clusters/cluster_spec.rb index b2379283aa7..48e3b4d6bae 100644 --- a/spec/models/clusters/cluster_spec.rb +++ b/spec/models/clusters/cluster_spec.rb @@ -17,6 +17,7 @@ describe Clusters::Cluster, :use_clean_rails_memory_store_caching do it { is_expected.to have_many(:cluster_groups) } it { is_expected.to have_many(:groups) } it { is_expected.to have_one(:provider_gcp) } + it { is_expected.to have_one(:provider_aws) } it { is_expected.to have_one(:platform_kubernetes) } it { is_expected.to have_one(:application_helm) } it { is_expected.to have_one(:application_ingress) } @@ -108,6 +109,31 @@ describe Clusters::Cluster, :use_clean_rails_memory_store_caching do it { is_expected.to contain_exactly(cluster) } end + describe '.aws_provided' do + subject { described_class.aws_provided } + + let!(:cluster) { create(:cluster, :provided_by_aws) } + + before do + create(:cluster, :provided_by_user) + end + + it { is_expected.to contain_exactly(cluster) } + end + + describe '.aws_installed' do + subject { described_class.aws_installed } + + let!(:cluster) { create(:cluster, :provided_by_aws) } + + before do + errored_cluster = create(:cluster, :provided_by_aws) + errored_cluster.provider.make_errored!("Error message") + end + + it { is_expected.to contain_exactly(cluster) } + end + describe '.managed' do subject do described_class.managed @@ -398,7 +424,14 @@ describe Clusters::Cluster, :use_clean_rails_memory_store_caching do it 'returns a provider' do is_expected.to eq(cluster.provider_gcp) - expect(subject.class.name.deconstantize).to eq(Clusters::Providers.to_s) + end + end + + context 'when provider is aws' do + let(:cluster) { create(:cluster, :provided_by_aws) } + + it 'returns a provider' do + is_expected.to eq(cluster.provider_aws) end end diff --git a/spec/models/clusters/providers/aws_spec.rb b/spec/models/clusters/providers/aws_spec.rb new file mode 100644 index 00000000000..ec8159a7ee0 --- /dev/null +++ b/spec/models/clusters/providers/aws_spec.rb @@ -0,0 +1,76 @@ +# frozen_string_literal: true + +require 'spec_helper' + +describe Clusters::Providers::Aws do + it { is_expected.to belong_to(:cluster) } + it { is_expected.to belong_to(:created_by_user) } + + it { is_expected.to validate_length_of(:key_name).is_at_least(1).is_at_most(255) } + it { is_expected.to validate_length_of(:region).is_at_least(1).is_at_most(255) } + it { is_expected.to validate_length_of(:instance_type).is_at_least(1).is_at_most(255) } + it { is_expected.to validate_length_of(:security_group_id).is_at_least(1).is_at_most(255) } + it { is_expected.to validate_presence_of(:subnet_ids) } + + include_examples 'provider status', :cluster_provider_aws + + describe 'default_value_for' do + let(:provider) { build(:cluster_provider_aws) } + + it "sets default values" do + expect(provider.region).to eq('us-east-1') + expect(provider.num_nodes).to eq(3) + expect(provider.instance_type).to eq('m5.large') + end + end + + describe 'custom validations' do + subject { provider.valid? } + + context ':num_nodes' do + let(:provider) { build(:cluster_provider_aws, num_nodes: num_nodes) } + + context 'contains non-digit characters' do + let(:num_nodes) { 'A3' } + + it { is_expected.to be_falsey } + end + + context 'is blank' do + let(:num_nodes) { nil } + + it { is_expected.to be_falsey } + end + + context 'is less than 1' do + let(:num_nodes) { 0 } + + it { is_expected.to be_falsey } + end + + context 'is a positive integer' do + let(:num_nodes) { 3 } + + it { is_expected.to be_truthy } + end + end + end + + describe '#nullify_credentials' do + let(:provider) { create(:cluster_provider_aws, :scheduled) } + + subject { provider.nullify_credentials } + + before do + expect(provider.access_key_id).to be_present + expect(provider.secret_access_key).to be_present + end + + it 'removes access_key_id and secret_access_key' do + subject + + expect(provider.access_key_id).to be_nil + expect(provider.secret_access_key).to be_nil + end + end +end diff --git a/spec/models/project_services/jira_service_spec.rb b/spec/models/project_services/jira_service_spec.rb index bdb7c7aeff4..5feb8ca7839 100644 --- a/spec/models/project_services/jira_service_spec.rb +++ b/spec/models/project_services/jira_service_spec.rb @@ -15,20 +15,22 @@ describe JiraService do let(:transition_id) { 'test27' } describe '#options' do - let(:service) do - described_class.create( + let(:options) do + { project: create(:project), active: true, - username: 'username ', + username: 'username', password: 'test', jira_issue_transition_id: 24, url: 'http://jira.test.com/path/' - ) + } end + let(:service) { described_class.create(options) } + it 'sets the URL properly' do - # jira-ruby gem parses the URI and handles trailing slashes - # fine: https://github.com/sumoheavy/jira-ruby/blob/v1.4.1/lib/jira/http_client.rb#L59 + # jira-ruby gem parses the URI and handles trailing slashes fine: + # https://github.com/sumoheavy/jira-ruby/blob/v1.7.0/lib/jira/http_client.rb#L62 expect(service.options[:site]).to eq('http://jira.test.com/') end @@ -36,14 +38,30 @@ describe JiraService do expect(service.options[:context_path]).to eq('/path') end - it 'leaves out trailing whitespaces in username' do - expect(service.options[:username]).to eq('username') + context 'username with trailing whitespaces' do + before do + options.merge!(username: 'username ') + end + + it 'leaves out trailing whitespaces in username' do + expect(service.options[:username]).to eq('username') + end end it 'provides additional cookies to allow basic auth with oracle webgate' do expect(service.options[:use_cookies]).to eq(true) expect(service.options[:additional_cookies]).to eq(['OBBasicAuth=fromDialog']) end + + context 'using api URL' do + before do + options.merge!(api_url: 'http://jira.test.com/api_path/') + end + + it 'leaves out trailing slashes in context' do + expect(service.options[:context_path]).to eq('/api_path') + end + end end describe 'Associations' do diff --git a/spec/support/shared_examples/models/clusters/providers/provider_status.rb b/spec/support/shared_examples/models/clusters/providers/provider_status.rb index af758b07d96..63cb9a56f5b 100644 --- a/spec/support/shared_examples/models/clusters/providers/provider_status.rb +++ b/spec/support/shared_examples/models/clusters/providers/provider_status.rb @@ -17,7 +17,7 @@ shared_examples 'provider status' do |factory| let(:provider) { build(factory) } let(:operation_id) { 'operation-xxx' } - it 'calls #operation_id on the provider' do + it 'calls #assign_operation_id on the provider' do expect(provider).to receive(:assign_operation_id).with(operation_id).and_call_original provider.make_creating(operation_id) |