Skip to content

Commit

Permalink
Merge pull request #64 from DynamicsValue/2x-241-environmentreader
Browse files Browse the repository at this point in the history
Should not attempt to read subscription usage while in a CI build
  • Loading branch information
jordimontana82 authored Feb 7, 2024
2 parents 8e77a54 + 2b1092e commit 5da1e63
Show file tree
Hide file tree
Showing 11 changed files with 189 additions and 48 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## [2.4.1]

### Changed

- Should not read subscription usage while in a CI build

## [2.4.0]

## Added
Expand Down
16 changes: 16 additions & 0 deletions src/FakeXrmEasy.Core/CommercialLicense/EnvironmentReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,29 @@ namespace FakeXrmEasy.Core.CommercialLicense
internal interface IEnvironmentReader
{
string GetEnvironmentVariable(string variableName);
bool IsRunningInContinuousIntegration();
}

internal class EnvironmentReader: IEnvironmentReader
{
/// <summary>
/// Gets the value of environment variable with name variableName
/// </summary>
/// <param name="variableName"></param>
/// <returns></returns>
public string GetEnvironmentVariable(string variableName)
{
return Environment.GetEnvironmentVariable(variableName);
}

/// <summary>
/// Checks if the test run is running in a CI environment
/// </summary>
/// <returns></returns>
public bool IsRunningInContinuousIntegration()
{
return "1".Equals(GetEnvironmentVariable("FAKE_XRM_EASY_CI"))
|| "True".Equals(GetEnvironmentVariable("TF_BUILD"));
}
}
}
98 changes: 76 additions & 22 deletions src/FakeXrmEasy.Core/CommercialLicense/SubscriptionManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,46 +7,100 @@

namespace FakeXrmEasy.Core.CommercialLicense
{
internal static class SubscriptionManager
internal class SubscriptionManager
{
internal static ISubscriptionInfo _subscriptionInfo;
internal static readonly object _subscriptionInfoLock = new object();

internal static ISubscriptionUsage _subscriptionUsage;
internal static readonly object _subscriptionUsageLock = new object();
internal ISubscriptionInfo _subscriptionInfo;
internal ISubscriptionUsage _subscriptionUsage;
internal readonly SubscriptionUsageManager _subscriptionUsageManager;

private bool _renewalRequested = false;

internal static bool _renewalRequested = false;
internal static bool _upgradeRequested = false;
private static readonly object _subscriptionManagerLock = new object();
private static SubscriptionManager _instance = null;
private readonly IEnvironmentReader _environmentReader;
internal SubscriptionManager(IEnvironmentReader environmentReader,
ISubscriptionInfo subscriptionInfo,
ISubscriptionUsage subscriptionUsage,
SubscriptionUsageManager subscriptionUsageManager)
{
_environmentReader = environmentReader;
_subscriptionInfo = subscriptionInfo;
_subscriptionUsage = subscriptionUsage;
_subscriptionUsageManager = subscriptionUsageManager;
}

private static void SetLicenseKey(string licenseKey)
internal static SubscriptionManager Instance
{
lock (_subscriptionInfoLock)
get
{
if (_subscriptionInfo == null)
lock (_subscriptionManagerLock)
{
var subscriptionPlanManager = new SubscriptionPlanManager();
_subscriptionInfo = subscriptionPlanManager.GetSubscriptionInfoFromKey(licenseKey);
if (_instance == null)
{
_instance = new SubscriptionManager(
new EnvironmentReader(),
null,
null,
new SubscriptionUsageManager());
}
}

return _instance;
}
}

/// <summary>
/// For testing
/// </summary>
/// <param name="instance"></param>
internal static void SetFakeInstance(SubscriptionManager instance)
{
_instance = instance;
}

internal ISubscriptionInfo SubscriptionInfo => _subscriptionInfo;
internal ISubscriptionUsage SubscriptionUsage => _subscriptionUsage;
internal bool RenewalRequested => _renewalRequested;

internal void SetLicenseKey(string licenseKey)
{
if (_subscriptionInfo == null)
{
var subscriptionPlanManager = new SubscriptionPlanManager();
_subscriptionInfo = subscriptionPlanManager.GetSubscriptionInfoFromKey(licenseKey);
}
}

internal static void SetSubscriptionStorageProvider(ISubscriptionStorageProvider subscriptionStorageProvider,
internal void SetSubscriptionStorageProvider_Internal(ISubscriptionStorageProvider subscriptionStorageProvider,
IUserReader userReader,
bool upgradeRequested,
bool renewalRequested)

{
SetLicenseKey(subscriptionStorageProvider.GetLicenseKey());

lock (_subscriptionUsageLock)
if (_subscriptionUsage == null)
{
if (_subscriptionUsage == null)
if (_environmentReader.IsRunningInContinuousIntegration())
{
_upgradeRequested = upgradeRequested;
_renewalRequested = renewalRequested;

var usageManager = new SubscriptionUsageManager();
_subscriptionUsage = usageManager.ReadAndUpdateUsage(_subscriptionInfo, subscriptionStorageProvider, userReader, _upgradeRequested);
_subscriptionUsage = new SubscriptionUsage();
return;
}

_renewalRequested = renewalRequested;

_subscriptionUsage = _subscriptionUsageManager.ReadAndUpdateUsage(_subscriptionInfo, subscriptionStorageProvider, userReader, upgradeRequested);
}
}
internal void SetSubscriptionStorageProvider(ISubscriptionStorageProvider subscriptionStorageProvider,
IUserReader userReader,
bool upgradeRequested,
bool renewalRequested)
{
lock (_subscriptionManagerLock)
{
SetLicenseKey(subscriptionStorageProvider.GetLicenseKey());
SetSubscriptionStorageProvider_Internal(subscriptionStorageProvider, userReader, upgradeRequested,
renewalRequested);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ internal bool IsSubscriptionPlanValid()

internal bool IsUsageValid()
{
if (IsRunningInContinuousIntegration())
if (_environmentReader.IsRunningInContinuousIntegration())
{
return true;
}
Expand Down Expand Up @@ -118,11 +118,5 @@ internal bool IsUsageValid()
}
return true;
}

private bool IsRunningInContinuousIntegration()
{
return "1".Equals(_environmentReader.GetEnvironmentVariable("FAKE_XRM_EASY_CI"))
|| "True".Equals(_environmentReader.GetEnvironmentVariable("TF_BUILD"));
}
}
}
2 changes: 1 addition & 1 deletion src/FakeXrmEasy.Core/FakeXrmEasy.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<TargetFrameworks Condition="'$(Configuration)'=='FAKE_XRM_EASY_2013'">net452</TargetFrameworks>
<TargetFrameworks Condition="'$(Configuration)'=='FAKE_XRM_EASY'">net452</TargetFrameworks>
<PackageId>FakeXrmEasy.Core</PackageId>
<VersionPrefix>2.4.0</VersionPrefix>
<VersionPrefix>2.4.1</VersionPrefix>
<Authors>Jordi Montaña</Authors>
<Company>Dynamics Value</Company>
<Title>FakeXrmEasy Core</Title>
Expand Down
15 changes: 8 additions & 7 deletions src/FakeXrmEasy.Core/Middleware/MiddlewareBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,14 +91,14 @@ public IXrmFakedContext Build()

if (_context.LicenseContext == FakeXrmEasyLicense.Commercial)
{
var subscriptionInfo = SubscriptionManager._subscriptionInfo;
if (subscriptionInfo != null)
var subscriptionManager = SubscriptionManager.Instance;
if (subscriptionManager.SubscriptionInfo != null)
{
var subscriptionValidator = new SubscriptionValidator(
new EnvironmentReader(),
SubscriptionManager._subscriptionInfo,
SubscriptionManager._subscriptionUsage,
SubscriptionManager._renewalRequested);
subscriptionManager.SubscriptionInfo,
subscriptionManager.SubscriptionUsage,
subscriptionManager.RenewalRequested);

subscriptionValidator.IsValid();
}
Expand Down Expand Up @@ -146,8 +146,9 @@ public IMiddlewareBuilder SetSubscriptionStorageProvider(ISubscriptionStoragePro
var userReader = new UserReader();
Console.WriteLine($"Setting Subscription Storage Provider...");
Console.WriteLine($" -> Running as '{userReader.GetCurrentUserName()}' ...");

SubscriptionManager.SetSubscriptionStorageProvider(storageProvider, userReader, upgradeRequested, renewalRequested);

var subscriptionManagerInstance = SubscriptionManager.Instance;
subscriptionManagerInstance.SetSubscriptionStorageProvider(storageProvider, userReader, upgradeRequested, renewalRequested);

Console.WriteLine($"Setting Subscription Storage Provider ok.");
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ public string GetEnvironmentVariable(string variableName)
return variableValue;
}

public bool IsRunningInContinuousIntegration()
{
return "1".Equals(GetEnvironmentVariable("FAKE_XRM_EASY_CI"))
|| "True".Equals(GetEnvironmentVariable("TF_BUILD"));
}

public void SetEnvironmentVariable(string variableName, string variableValue)
{
_variables.AddOrUpdate(variableName, variableValue, (key, oldValue) => variableValue);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using FakeXrmEasy.Abstractions.CommercialLicense;
using FakeXrmEasy.Core.CommercialLicense;

namespace FakeXrmEasy.Core.Tests.CommercialLicense
{
public class FakeSubscriptionStorageProvider: ISubscriptionStorageProvider
{
public string GetLicenseKey()
{
return "license-key";
}

public ISubscriptionUsage Read()
{
return new SubscriptionUsage();
}

public void Write(ISubscriptionUsage currentUsage)
{
//Do nothing
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,47 @@
using FakeItEasy;
using FakeXrmEasy.Abstractions.CommercialLicense;
using FakeXrmEasy.Core.CommercialLicense;
using FakeXrmEasy.Core.CommercialLicense.Exceptions;
using Xunit;

namespace FakeXrmEasy.Core.Tests.CommercialLicense
{
public class SubscriptionManagerTests
{
private SubscriptionManager _subscriptionManager;
private readonly FakeEnvironmentReader _environmentReader;
private readonly SubscriptionUsageManager _subscriptionUsageManager;
private readonly ISubscriptionStorageProvider _fakeSubscriptionStorageProvider;
public SubscriptionManagerTests()
{
_environmentReader = new FakeEnvironmentReader();
_subscriptionUsageManager = new SubscriptionUsageManager();
_fakeSubscriptionStorageProvider = A.Fake<ISubscriptionStorageProvider>();
}

[Fact]
public void Should_set_license_key_and_throw_exception_when_invalid()
{
_subscriptionManager = new SubscriptionManager(_environmentReader, null, null, _subscriptionUsageManager);
Assert.Throws<InvalidLicenseKeyException>(() => _subscriptionManager.SetLicenseKey("dummy"));
}


[Fact]
public void Should_not_read_usage_when_running_in_CI()
{
var subscriptionInfo = new SubscriptionInfo()
{
NumberOfUsers = 1,
EndDate = DateTime.UtcNow.AddMonths(1)
};
_environmentReader.SetEnvironmentVariable("FAKE_XRM_EASY_CI", "1");

_subscriptionManager = new SubscriptionManager(_environmentReader, subscriptionInfo, null, _subscriptionUsageManager);
_subscriptionManager.SetSubscriptionStorageProvider_Internal(_fakeSubscriptionStorageProvider, new UserReader(), false, false);

Assert.NotNull(_subscriptionManager._subscriptionUsage);
A.CallTo(() => _fakeSubscriptionStorageProvider.Read()).MustNotHaveHappened();
}



Expand Down
14 changes: 7 additions & 7 deletions tests/FakeXrmEasy.Core.Tests/FakeXrmEasy.Core.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<IsPackable>true</IsPackable>

<PackageId>FakeXrmEasy.CoreTests</PackageId>
<VersionPrefix>2.4.0</VersionPrefix>
<VersionPrefix>2.4.1</VersionPrefix>
<Authors>Jordi Montaña</Authors>
<Company>Dynamics Value S.L.</Company>
<Title>Internal Unit test suite for FakeXrmEasy.Core package</Title>
Expand Down Expand Up @@ -137,22 +137,22 @@
</ItemGroup>

<ItemGroup Condition="'$(PackTests)' == 'true' And '$(Configuration)'=='FAKE_XRM_EASY'">
<PackageReference Include="FakeXrmEasy.Core.v2011" Version="[2.4.0-*,3.0)" />
<PackageReference Include="FakeXrmEasy.Core.v2011" Version="[2.4.1-*,3.0)" />
</ItemGroup>
<ItemGroup Condition="'$(PackTests)' == 'true' And '$(Configuration)'=='FAKE_XRM_EASY_2013'">
<PackageReference Include="FakeXrmEasy.Core.v2013" Version="[2.4.0-*,3.0)" />
<PackageReference Include="FakeXrmEasy.Core.v2013" Version="[2.4.1-*,3.0)" />
</ItemGroup>
<ItemGroup Condition="'$(PackTests)' == 'true' And '$(Configuration)'=='FAKE_XRM_EASY_2015'">
<PackageReference Include="FakeXrmEasy.Core.v2015" Version="[2.4.0-*,3.0)" />
<PackageReference Include="FakeXrmEasy.Core.v2015" Version="[2.4.1-*,3.0)" />
</ItemGroup>
<ItemGroup Condition="'$(PackTests)' == 'true' And '$(Configuration)'=='FAKE_XRM_EASY_2016'">
<PackageReference Include="FakeXrmEasy.Core.v2016" Version="[2.4.0-*,3.0)" />
<PackageReference Include="FakeXrmEasy.Core.v2016" Version="[2.4.1-*,3.0)" />
</ItemGroup>
<ItemGroup Condition="'$(PackTests)' == 'true' And '$(Configuration)'=='FAKE_XRM_EASY_365'">
<PackageReference Include="FakeXrmEasy.Core.v365" Version="[2.4.0-*,3.0)" />
<PackageReference Include="FakeXrmEasy.Core.v365" Version="[2.4.1-*,3.0)" />
</ItemGroup>
<ItemGroup Condition="'$(PackTests)' == 'true' And '$(Configuration)'=='FAKE_XRM_EASY_9'">
<PackageReference Include="FakeXrmEasy.Core.v9" Version="[2.4.0-*,3.0)" />
<PackageReference Include="FakeXrmEasy.Core.v9" Version="[2.4.1-*,3.0)" />
</ItemGroup>


Expand Down
15 changes: 12 additions & 3 deletions tests/FakeXrmEasy.Core.Tests/Middleware/MiddlewareBuilderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using FakeXrmEasy.Abstractions.Exceptions;
using FakeXrmEasy.Core.CommercialLicense;
using FakeXrmEasy.Core.CommercialLicense.Exceptions;
using FakeXrmEasy.Core.Tests.CommercialLicense;

namespace FakeXrmEasy.Core.Tests.Middleware
{
Expand Down Expand Up @@ -148,12 +149,20 @@ public void Should_throw_exception_when_building_without_a_license()
[Fact]
public void Should_not_throw_exception_when_using_commercial_license_with_custom_storage_and_valid_data()
{
SubscriptionManager._subscriptionInfo = _subscriptionInfo;
SubscriptionManager._subscriptionUsage = _subscriptionUsage;

var subscriptionManager = new SubscriptionManager(new FakeEnvironmentReader(),
new SubscriptionInfo()
{
NumberOfUsers = 1,
EndDate = DateTime.UtcNow.AddMonths(1)
}, null,
new SubscriptionUsageManager());

SubscriptionManager.SetFakeInstance(subscriptionManager);

var ctx = MiddlewareBuilder
.New()
.SetLicense(FakeXrmEasyLicense.Commercial)
.SetSubscriptionStorageProvider(new FakeSubscriptionStorageProvider())
.Build();

Assert.Equal(FakeXrmEasyLicense.Commercial, ctx.LicenseContext);
Expand Down

0 comments on commit 5da1e63

Please sign in to comment.