diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2023-03-20 18:19:03 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2023-03-20 18:19:03 +0300 |
commit | 14bd84b61276ef29b97d23642d698de769bacfd2 (patch) | |
tree | f9eba90140c1bd874211dea17750a0d422c04080 /spec/lib/gitlab/loggable_spec.rb | |
parent | 891c388697b2db0d8ee0c8358a9bdbf6dc56d581 (diff) |
Add latest changes from gitlab-org/gitlab@15-10-stable-eev15.10.0-rc42
Diffstat (limited to 'spec/lib/gitlab/loggable_spec.rb')
-rw-r--r-- | spec/lib/gitlab/loggable_spec.rb | 68 |
1 files changed, 68 insertions, 0 deletions
diff --git a/spec/lib/gitlab/loggable_spec.rb b/spec/lib/gitlab/loggable_spec.rb new file mode 100644 index 00000000000..8238e47014b --- /dev/null +++ b/spec/lib/gitlab/loggable_spec.rb @@ -0,0 +1,68 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe Gitlab::Loggable, feature_category: :logging do + subject(:klass_instance) do + Class.new do + include Gitlab::Loggable + + def self.name + 'MyTestClass' + end + end.new + end + + describe '#build_structured_payload' do + it 'adds class and returns formatted json' do + expected = { + 'class' => 'MyTestClass', + 'message' => 'test' + } + + expect(klass_instance.build_structured_payload(message: 'test')).to eq(expected) + end + + it 'appends additional params and returns formatted json' do + expected = { + 'class' => 'MyTestClass', + 'message' => 'test', + 'extra_param' => 1 + } + + expect(klass_instance.build_structured_payload(message: 'test', extra_param: 1)).to eq(expected) + end + + it 'does not raise an error in loggers when passed non-symbols' do + expected = { + 'class' => 'MyTestClass', + 'message' => 'test', + '["hello", "thing"]' => :world + } + + payload = klass_instance.build_structured_payload(message: 'test', %w[hello thing] => :world) + expect(payload).to eq(expected) + expect { Gitlab::Export::Logger.info(payload) }.not_to raise_error + end + + it 'handles anonymous classes' do + anonymous_klass_instance = Class.new { include Gitlab::Loggable }.new + + expected = { + 'class' => '<Anonymous>', + 'message' => 'test' + } + + expect(anonymous_klass_instance.build_structured_payload(message: 'test')).to eq(expected) + end + + it 'handles duplicate keys' do + expected = { + 'class' => 'MyTestClass', + 'message' => 'test2' + } + + expect(klass_instance.build_structured_payload(message: 'test', 'message' => 'test2')).to eq(expected) + end + end +end |