From 20b8f3c17ad72b069d94268e97c7c2558159614d Mon Sep 17 00:00:00 2001 From: Jan Kessler Date: Wed, 4 Dec 2024 12:23:18 +0100 Subject: [PATCH] add tests for extended tag feature --- spec/models/server_spec.rb | 44 ++++++++++++++++++++++++++++++++++++ test/models/server_test.rb | 46 +++++++++++++++++++++++++++++++++++++- 2 files changed, 89 insertions(+), 1 deletion(-) diff --git a/spec/models/server_spec.rb b/spec/models/server_spec.rb index 54ca5b95..0ee651c2 100644 --- a/spec/models/server_spec.rb +++ b/spec/models/server_spec.rb @@ -339,6 +339,22 @@ expect(server.id).to eq 'test-3' end end + + context 'and with none tag argument' do + let(:server) { described_class.find_available('none') } + + it 'returns untagged server with lowest load' do + expect(server.id).to eq 'test-3' + end + end + + context 'and with none! tag argument' do + let(:server) { described_class.find_available('none!') } + + it 'returns untagged server with lowest load' do + expect(server.id).to eq 'test-3' + end + end end context 'with differently tagged servers' do @@ -366,6 +382,11 @@ redis.sadd?('servers', 'test-4') redis.sadd?('server_enabled', 'test-4') redis.zadd('server_load', 1, 'test-4') + redis.mapped_hmset('server:test-5', url: 'https://test-5.example.com/bigbluebutton/api', secret: 'test-5-secret', + tag: 'test-tag2', enabled: 'true') + redis.sadd?('servers', 'test-5') + redis.sadd?('server_enabled', 'test-5') + redis.zadd('server_load', 1, 'test-5') end end @@ -391,6 +412,29 @@ expect(server.tag).to eq 'test-tag' end end + + context 'and optional tag list argument' do + let(:server) { described_class.find_available('test-tag,test-tag2') } + + it 'returns matching tagged server with lowest load' do + expect(server.id).to eq 'test-5' + expect(server.url).to eq 'https://test-5.example.com/bigbluebutton/api' + expect(server.secret).to eq 'test-5-secret' + expect(server.tag).to eq 'test-tag2' + expect(server.enabled).to be true + expect(server.state).to be_nil + expect(server.load).to eq 1 + end + end + + context 'and required tag list argument' do + let(:server) { described_class.find_available('test-tag,test-tag2!') } + + it 'returns matching tagged server with lowest load' do + expect(server.id).to eq 'test-5' + expect(server.tag).to eq 'test-tag2' + end + end end context 'with no matching tagged servers' do diff --git a/test/models/server_test.rb b/test/models/server_test.rb index 57ef0070..190fb83d 100644 --- a/test/models/server_test.rb +++ b/test/models/server_test.rb @@ -243,7 +243,7 @@ class ServerTest < ActiveSupport::TestCase end end - test 'Server find_available without or with empty tag returns untagged server with lowest load' do + test 'Server find_available without or with empty tag or with none tag returns untagged server with lowest load' do RedisStore.with_connection do |redis| redis.mapped_hmset('server:test-1', url: 'https://test-1.example.com/bigbluebutton/api', secret: 'test-1-secret', tag: 'test-tag', enabled: 'true') @@ -276,6 +276,12 @@ class ServerTest < ActiveSupport::TestCase server = Server.find_available('!') assert_equal('test-3', server.id) + + server = Server.find_available('none') + assert_equal('test-3', server.id) + + server = Server.find_available('none!') + assert_equal('test-3', server.id) end test 'Server find_available with tag argument returns matching tagged server with lowest load' do @@ -316,6 +322,44 @@ class ServerTest < ActiveSupport::TestCase assert_equal('test-tag', server.tag) end + test 'Server find_available with multiple tag arguments returns matching tagged server with lowest load' do + RedisStore.with_connection do |redis| + redis.mapped_hmset('server:test-1', url: 'https://test-1.example.com/bigbluebutton/api', secret: 'test-1-secret', + enabled: 'true') + redis.sadd?('servers', 'test-1') + redis.sadd?('server_enabled', 'test-1') + redis.zadd('server_load', 1, 'test-1') + redis.mapped_hmset('server:test-2', url: 'https://test-2.example.com/bigbluebutton/api', secret: 'test-2-secret', + tag: 'test-tag', enabled: 'true') + redis.sadd?('servers', 'test-2') + redis.sadd?('server_enabled', 'test-2') + redis.zadd('server_load', 3, 'test-2') + redis.mapped_hmset('server:test-3', url: 'https://test-3.example.com/bigbluebutton/api', secret: 'test-3-secret', + tag: 'test-tag2', enabled: 'true') + redis.sadd?('servers', 'test-3') + redis.sadd?('server_enabled', 'test-3') + redis.zadd('server_load', 2, 'test-3') + redis.mapped_hmset('server:test-4', url: 'https://test-4.example.com/bigbluebutton/api', secret: 'test-4-secret', + tag: 'wrong-tag', enabled: 'true') + redis.sadd?('servers', 'test-4') + redis.sadd?('server_enabled', 'test-4') + redis.zadd('server_load', 1, 'test-4') + end + + server = Server.find_available('test-tag,test-tag2') + assert_equal('test-3', server.id) + assert_equal('https://test-3.example.com/bigbluebutton/api', server.url) + assert_equal('test-3-secret', server.secret) + assert_equal('test-tag2', server.tag) + assert(server.enabled) + assert_nil(server.state) + assert_equal(2, server.load) + + server = Server.find_available('test-tag,test-tag2!') + assert_equal('test-3', server.id) + assert_equal('test-tag2', server.tag) + end + test 'Server find_available with optional tag returns untagged server with lowest load if no matching tagged server available' do RedisStore.with_connection do |redis| redis.mapped_hmset('server:test-1', url: 'https://test-1.example.com/bigbluebutton/api', secret: 'test-1-secret',