Browse Source

Implement configuration via env variables

pull/835/head
Stéphane Meny 3 years ago
parent
commit
a98d95fd3b
No known key found for this signature in database
GPG Key ID: 9D0AFEA988527923
  1. 28
      app/services/configuration/env_configuration_service.rb
  2. 2
      app/services/configuration/paas_configuration_service.rb
  3. 125
      spec/services/configuration/env_configuration_service_spec.rb
  4. 73
      spec/services/configuration/paas_configuration_service_spec.rb

28
app/services/configuration/env_configuration_service.rb

@ -3,11 +3,35 @@ module Configuration
private
def config_present?
raise NotImplementedError
!ENV["S3_CONFIG"].nil? || !ENV["REDIS_CONFIG"].nil?
end
def read_config
raise NotImplementedError
unless config_present?
@logger.warn("Could not find S3_CONFIG or REDIS_CONFIG in the environment variables!")
return {}
end
config = {}
assign_config(config, :"aws-s3-bucket", "S3_CONFIG")
assign_config(config, :redis, "REDIS_CONFIG")
config
end
def assign_config(config, symbol, env_variable)
config_hash = parse_json_config(env_variable)
config[symbol] = config_hash unless config_hash.empty?
end
def parse_json_config(env_variable_name)
if ENV[env_variable_name].present?
begin
return JSON.parse(ENV[env_variable_name], { symbolize_names: true })
rescue StandardError
@logger.warn("Could not parse #{env_variable_name}!")
end
end
{}
end
end
end

2
app/services/configuration/paas_configuration_service.rb

@ -8,7 +8,7 @@ module Configuration
def read_config
unless config_present?
@logger.warn("Could not find VCAP_SERVICES in the environment!")
@logger.warn("Could not find VCAP_SERVICES in the environment variables!")
return {}
end

125
spec/services/configuration/env_configuration_service_spec.rb

@ -1,17 +1,128 @@
require "rails_helper"
Rspec.describe Configuration::EnvConfigurationService do
RSpec.describe Configuration::EnvConfigurationService do
subject(:config_service) { described_class.new(logger) }
let(:logger) { instance_double(ActiveSupport::LogSubscriber) }
context "when environment configurations are unavailable" do
before { allow(logger).to receive(:warn) }
it "returns the S3 configuration as not present" do
expect(config_service.s3_config_present?).to be(false)
end
it "returns the redis configuration as not present" do
expect(config_service.redis_config_present?).to be(false)
end
it "does not retrieve any S3 bucket configuration" do
expect(config_service.s3_buckets).to be_a(Hash)
expect(config_service.s3_buckets).to be_empty
end
it "does not retrieve any redis configuration" do
expect(config_service.redis_uris).to be_a(Hash)
expect(config_service.redis_uris).to be_empty
end
end
context "when environment configurations are present but invalid" do
let(:env_variable) { "random text" }
before do
# Do nothing
allow(ENV).to receive(:[]).with("S3_CONFIG").and_return(env_variable)
allow(ENV).to receive(:[]).with("REDIS_CONFIG").and_return(env_variable)
allow(logger).to receive(:warn)
end
it "logs an error when checking if the S3 config is present" do
expect(logger).to receive(:warn).with("Could not parse S3_CONFIG!")
config_service.s3_config_present?
end
after do
# Do nothing
it "logs an error when checking if the Redis config is present" do
expect(logger).to receive(:warn).with("Could not parse REDIS_CONFIG!")
config_service.redis_config_present?
end
end
context "when environment configurations are present with S3 configured" do
let(:s3_config) do
<<~JSON
[
{
"instance_name": "bucket_1",
"credentials": {
"aws_access_key_id": "123",
"aws_secret_access_key": "456",
"aws_region": "eu-west-1",
"bucket_name": "my-bucket"
}
},
{
"instance_name": "bucket_2",
"credentials": {
"aws_access_key_id": "789",
"aws_secret_access_key": "012",
"aws_region": "eu-west-2",
"bucket_name": "my-bucket2"
}
}
]
JSON
end
before do
allow(ENV).to receive(:[]).with("REDIS_CONFIG")
allow(ENV).to receive(:[]).with("S3_CONFIG").and_return(s3_config)
end
it "returns the S3 configuration as present" do
expect(config_service.s3_config_present?).to be(true)
end
it "returns the redis configuration as not present" do
expect(config_service.redis_config_present?).to be(false)
end
it "does retrieve the S3 bucket configurations" do
s3_buckets = config_service.s3_buckets
expect(s3_buckets).not_to be_empty
expect(s3_buckets.count).to be(2)
expect(s3_buckets).to have_key(:bucket_1)
expect(s3_buckets).to have_key(:bucket_2)
end
end
context "when environment configurations are present with redis configured" do
let(:redis_config) do
<<-JSON
[{"instance_name": "redis_1", "credentials": {"uri": "redis_uri" }}]
JSON
end
before do
allow(ENV).to receive(:[]).with("S3_CONFIG")
allow(ENV).to receive(:[]).with("REDIS_CONFIG").and_return(redis_config)
end
it "returns the redis configuration as present" do
expect(config_service.redis_config_present?).to be(true)
end
it "returns the S3 configuration as not present" do
expect(config_service.s3_config_present?).to be(false)
end
it "does retrieve the redis configurations" do
redis_uris = config_service.redis_uris
context 'when condition' do
it 'succeeds' do
pending 'Not implemented'
expect(redis_uris).not_to be_empty
expect(redis_uris.count).to be(1)
expect(redis_uris).to have_key(:redis_1)
expect(redis_uris[:redis_1]).to eq("redis_uri")
end
end
end

73
spec/services/configuration/paas_configuration_service_spec.rb

@ -51,7 +51,7 @@ RSpec.describe Configuration::PaasConfigurationService do
end
end
context "when configuration is present but invalid" do
context "when the paas configuration is present but invalid" do
let(:vcap_services) { "random text" }
before do
@ -59,16 +59,40 @@ RSpec.describe Configuration::PaasConfigurationService do
allow(logger).to receive(:warn)
end
it "logs an error" do
it "logs an error when checking if the S3 config is present" do
expect(logger).to receive(:warn).with("Could not parse VCAP_SERVICES!")
config_service.s3_config_present?
end
it "logs an error when checking if the Redis config is present" do
expect(logger).to receive(:warn).with("Could not parse VCAP_SERVICES!")
config_service.redis_config_present?
end
end
context "when the paas configuration is present with S3 configured" do
let(:vcap_services) do
<<-JSON
{"aws-s3-bucket": [{"instance_name": "bucket_1"},{"instance_name": "bucket_2"}]}
<<~JSON
{"aws-s3-bucket":
[{
"instance_name": "bucket_1",
"credentials": {
"aws_access_key_id": "123",
"aws_secret_access_key": "456",
"aws_region": "eu-west-1",
"bucket_name": "my-bucket"
}
},
{
"instance_name": "bucket_2",
"credentials": {
"aws_access_key_id": "789",
"aws_secret_access_key": "012",
"aws_region": "eu-west-2",
"bucket_name": "my-bucket2"
}
}]
}
JSON
end
@ -122,45 +146,4 @@ RSpec.describe Configuration::PaasConfigurationService do
expect(redis_uris[:redis_1]).to eq("redis_uri")
end
end
context "when the paas configuration is present with both S3 and redis configured" do
let(:vcap_services) do
<<-JSON
{
"aws-s3-bucket": [{"instance_name": "bucket_1"},{"instance_name": "bucket_2"}],
"redis": [{"instance_name": "redis_1", "credentials": {"uri": "redis_uri" }}]
}
JSON
end
before do
allow(ENV).to receive(:[]).with("VCAP_SERVICES").and_return(vcap_services)
end
it "returns the S3configuration as present" do
expect(config_service.s3_config_present?).to be(true)
end
it "returns the redis configuration as present" do
expect(config_service.redis_config_present?).to be(true)
end
it "does retrieve the redis configurations" do
redis_uris = config_service.redis_uris
expect(redis_uris).not_to be_empty
expect(redis_uris.count).to be(1)
expect(redis_uris).to have_key(:redis_1)
expect(redis_uris[:redis_1]).to eq("redis_uri")
end
it "does retrieve the S3 bucket configurations" do
s3_buckets = config_service.s3_buckets
expect(s3_buckets).not_to be_empty
expect(s3_buckets.count).to be(2)
expect(s3_buckets).to have_key(:bucket_1)
expect(s3_buckets).to have_key(:bucket_2)
end
end
end

Loading…
Cancel
Save