Skip to content

Commit

Permalink
Merge pull request #46 from GravityWolfNotAmused/dev
Browse files Browse the repository at this point in the history
1.0.9.3 - Docker Fix & Test Cases
  • Loading branch information
GravityWolfNotAmused authored Sep 11, 2022
2 parents 4811d6b + 817cae7 commit 27356b0
Show file tree
Hide file tree
Showing 14 changed files with 358 additions and 300 deletions.
26 changes: 26 additions & 0 deletions .github/workflows/RunTestCases.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# This is a basic workflow to help you get started with Actions

name: Run Test Cases

# Controls when the workflow will run
on:
pull_request:
branches: [ "latest", "dev" ]

# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
test:
runs-on: windows-latest
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Setup .NET
uses: actions/setup-dotnet@v2
with:
dotnet-version: 6.0.x
- name: Restore dependencies
run: dotnet restore
- name: Build
run: dotnet build --no-restore
- name: Test
run: dotnet test --no-build --verbosity normal
2 changes: 1 addition & 1 deletion .github/workflows/dotnet.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ jobs:
- name: Test
run: dotnet test --no-build --verbosity normal
- name: Publish
run: dotnet publish -p:PublishSingleFile=true -r win-x64 -c Release --self-contained true -p:PublishTrimmed=true
run: dotnet publish -p:PublishSingleFile=true -r win-x64 -c Release --self-contained true -p:PublishTrimmed=true -p:UseAppHost=true
- name: Create Artifact
uses: actions/upload-artifact@v3
with:
Expand Down
27 changes: 27 additions & 0 deletions DiscordPlayerCountBot.Tests/DiscordPlayerCountBot.Tests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.1.0" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="3.1.2">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\DiscordPlayerCountBot\DiscordPlayerCountBot.csproj" />
</ItemGroup>

</Project>
97 changes: 97 additions & 0 deletions DiscordPlayerCountBot.Tests/DockerConfigurationTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
using DiscordPlayerCountBot.Tests.Environment;

namespace DiscordPlayerCountBot.Tests;

[Collection("Configuration Test Suite")]
public class DockerConfigurationTests
{

[Fact(DisplayName = "Test Docker Configuration with all data", Timeout = 30)]
public async Task DockerConfigurationTestWithAllData()
{
EnivronmentHelper.SetTestEnvironmentWithAllVariables();

var bots = new Dictionary<string, Bot>();
var time = -1;

var dockerConfiguration = new DockerConfiguration();
var configuration = await dockerConfiguration.Configure(false);

bots = configuration.Item1;
time = configuration.Item2;

EnivronmentHelper.ClearTestEnvironmentVariables();

Assert.True(time > 0, $"Time was returned from config and not zero. Actual: {time}");
Assert.True(bots.Count > 0, $"More than one bot was created. Actual: {bots.Count}");
Assert.True(bots.ToList()[0].Value.ApplicationTokens.Count >= 2, $"More than 2 tokens in the dictionary for the bot. Actual: {bots.ToList()[0].Value.ApplicationTokens.Count}");
Assert.True(bots.ToList()[0].Value.ApplicationTokens.ContainsKey("SteamAPIKey"), $"Steam Key should be present.");
Assert.True(bots.ToList()[0].Value.ApplicationTokens.ContainsKey("BattleMetricsKey"), $"Battle Metrics Key should be present.");
}

[Fact(DisplayName = "Test Docker Configuration without Battle Metrics", Timeout = 30)]
public async Task DockerConfigurationTestWithoutBattleMetrics()
{
EnivronmentHelper.SetTestEnvironmentWithoutBattleMetrics();

var bots = new Dictionary<string, Bot>();
var time = -1;

var dockerConfiguration = new DockerConfiguration();
var configuration = await dockerConfiguration.Configure(false);

bots = configuration.Item1;
time = configuration.Item2;

EnivronmentHelper.ClearTestEnvironmentVariables();

Assert.True(time > 0, $"Time was returned from config and not zero. Actual: {time}");
Assert.True(bots.Count > 0, $"More than one bot was created. Actual: {bots.Count}");
Assert.True(bots.ToList()[0].Value.ApplicationTokens.Count == 1, $"1 token in the dictionary for the bots. Actual: {bots.ToList()[0].Value.ApplicationTokens.Count}");
Assert.True(bots.ToList()[0].Value.ApplicationTokens.ContainsKey("SteamAPIKey"), $"Steam Key should be present.");
Assert.False(bots.ToList()[0].Value.ApplicationTokens.ContainsKey("BattleMetricsKey"), $"Battle Metrics Key should not be present.");
}

[Fact(DisplayName = "Test Docker Configuration without Application Variables", Timeout = 30)]
public async Task DockerConfigurationTestWithoutApplicationVariables()
{
EnivronmentHelper.SetTestEnvironmentWithoutApplicationVariables();

var bots = new Dictionary<string, Bot>();
var time = -1;

var dockerConfiguration = new DockerConfiguration();
var configuration = await dockerConfiguration.Configure(false);

bots = configuration.Item1;
time = configuration.Item2;

EnivronmentHelper.ClearTestEnvironmentVariables();

Assert.True(time > 0, $"Time was returned from config and not zero. Actual: {time}");
Assert.True(bots.Count > 0, $"More than one bot was created. Actual: {bots.Count}");
Assert.True(bots.ToList()[0].Value.ApplicationTokens.Count == 0, $"Should be 0 variables in the dictionary. Actual: {bots.ToList()[0].Value.ApplicationTokens.Count}");
}

[Fact(DisplayName = "Test Docker Configuration without Steam variable", Timeout = 30)]
public async Task DockerConfigurationTestWithoutSteam()
{
EnivronmentHelper.SetTestEnvironmentWithoutSteam();

var bots = new Dictionary<string, Bot>();
var time = -1;

var dockerConfiguration = new DockerConfiguration();
var configuration = await dockerConfiguration.Configure(false);

bots = configuration.Item1;
time = configuration.Item2;

EnivronmentHelper.ClearTestEnvironmentVariables();

Assert.True(time > 0, $"Time was returned from config and not zero. Actual: {time}");
Assert.True(bots.Count > 0, $"More than one bot was created. Actual: {bots.Count}");
Assert.True(bots.ToList()[0].Value.ApplicationTokens.Count == 1, $"1 token in the dictionary for the bots. Actual: {bots.ToList()[0].Value.ApplicationTokens.Count}");
Assert.True(bots.ToList()[0].Value.ApplicationTokens.ContainsKey("BattleMetricsKey"), $"Battle Metrics Key should be present.");
}
}
73 changes: 73 additions & 0 deletions DiscordPlayerCountBot.Tests/Environment/EnivronmentHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
namespace DiscordPlayerCountBot.Tests.Environment
{
public static class EnivronmentHelper
{
public static void SetTestEnvironmentWithAllVariables()
{
System.Environment.SetEnvironmentVariable("ISDOCKER", "true");
System.Environment.SetEnvironmentVariable("BOT_NAMES", "Steam;CFX;Scum;Minecraft;BattleMetrics");
System.Environment.SetEnvironmentVariable("BOT_PUBADDRESSES", "51.222.191.212;96.43.133.194;178.63.74.20;minecraft.hypixel.net;15086629");
System.Environment.SetEnvironmentVariable("BOT_PORTS", "2403;30120;7042;25565;0");
System.Environment.SetEnvironmentVariable("BOT_DISCORD_TOKENS", "1;2;3;4;5");
System.Environment.SetEnvironmentVariable("BOT_USERNAMETAGS", "false;false;false;false;false");
System.Environment.SetEnvironmentVariable("BOT_STATUSES", "Steam;CFX;Scum;Minecraft;BattleMetrics");
System.Environment.SetEnvironmentVariable("BOT_PROVIDERTYPES", "3;3;3;3;3");
System.Environment.SetEnvironmentVariable("BOT_UPDATE_TIME", "10");
System.Environment.SetEnvironmentVariable("BOT_APPLICATION_VARIABLES", "SteamAPIKey,12345;BattleMetricsKey,12345");
}
public static void SetTestEnvironmentWithoutSteam()
{
System.Environment.SetEnvironmentVariable("ISDOCKER", "true");
System.Environment.SetEnvironmentVariable("BOT_NAMES", "CFX;Scum;Minecraft;BattleMetrics");
System.Environment.SetEnvironmentVariable("BOT_PUBADDRESSES", "96.43.133.194;178.63.74.20;minecraft.hypixel.net;15086629");
System.Environment.SetEnvironmentVariable("BOT_PORTS", "30120;7042;25565;0");
System.Environment.SetEnvironmentVariable("BOT_DISCORD_TOKENS", "2;3;4;5");
System.Environment.SetEnvironmentVariable("BOT_USERNAMETAGS", "false;false;false;false");
System.Environment.SetEnvironmentVariable("BOT_STATUSES", "CFX;Scum;Minecraft;BattleMetrics");
System.Environment.SetEnvironmentVariable("BOT_PROVIDERTYPES", "3;3;3;3");
System.Environment.SetEnvironmentVariable("BOT_UPDATE_TIME", "10");
System.Environment.SetEnvironmentVariable("BOT_APPLICATION_VARIABLES", "BattleMetricsKey,12345");
}

public static void SetTestEnvironmentWithoutBattleMetrics()
{
System.Environment.SetEnvironmentVariable("ISDOCKER", "true");
System.Environment.SetEnvironmentVariable("BOT_NAMES", "Steam;CFX;Scum;Minecraft");
System.Environment.SetEnvironmentVariable("BOT_PUBADDRESSES", "51.222.191.212;96.43.133.194;178.63.74.20;minecraft.hypixel.net");
System.Environment.SetEnvironmentVariable("BOT_PORTS", "2403;30120;7042;25565");
System.Environment.SetEnvironmentVariable("BOT_DISCORD_TOKENS", "1;2;3;4");
System.Environment.SetEnvironmentVariable("BOT_USERNAMETAGS", "false;false;false;false");
System.Environment.SetEnvironmentVariable("BOT_STATUSES", "Steam;CFX;Scum;Minecraft");
System.Environment.SetEnvironmentVariable("BOT_PROVIDERTYPES", "3;3;3;3");
System.Environment.SetEnvironmentVariable("BOT_UPDATE_TIME", "10");
System.Environment.SetEnvironmentVariable("BOT_APPLICATION_VARIABLES", "SteamAPIKey,12345");
}

public static void SetTestEnvironmentWithoutApplicationVariables()
{
System.Environment.SetEnvironmentVariable("ISDOCKER", "true");
System.Environment.SetEnvironmentVariable("BOT_NAMES", "Steam;CFX;Scum;Minecraft;BattleMetrics");
System.Environment.SetEnvironmentVariable("BOT_PUBADDRESSES", "51.222.191.212;96.43.133.194;178.63.74.20;minecraft.hypixel.net;15086629");
System.Environment.SetEnvironmentVariable("BOT_PORTS", "2403;30120;7042;25565;0");
System.Environment.SetEnvironmentVariable("BOT_DISCORD_TOKENS", "1;2;3;4;5");
System.Environment.SetEnvironmentVariable("BOT_USERNAMETAGS", "false;false;false;false;false");
System.Environment.SetEnvironmentVariable("BOT_STATUSES", "Steam;CFX;Scum;Minecraft;BattleMetrics");
System.Environment.SetEnvironmentVariable("BOT_PROVIDERTYPES", "3;3;3;3;3");
System.Environment.SetEnvironmentVariable("BOT_UPDATE_TIME", "10");
}

public static void ClearTestEnvironmentVariables()
{
System.Environment.SetEnvironmentVariable("ISDOCKER", null);
System.Environment.SetEnvironmentVariable("BOT_NAMES", null);
System.Environment.SetEnvironmentVariable("BOT_PUBADDRESSES", null);
System.Environment.SetEnvironmentVariable("BOT_PORTS", null);
System.Environment.SetEnvironmentVariable("BOT_DISCORD_TOKENS", null);
System.Environment.SetEnvironmentVariable("BOT_USERNAMETAGS", null);
System.Environment.SetEnvironmentVariable("BOT_STATUSES", null);
System.Environment.SetEnvironmentVariable("BOT_PROVIDERTYPES", null);
System.Environment.SetEnvironmentVariable("BOT_UPDATE_TIME", null);
System.Environment.SetEnvironmentVariable("BOT_APPLICATION_VARIABLES", null);
}
}
}
3 changes: 3 additions & 0 deletions DiscordPlayerCountBot.Tests/Usings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
global using Xunit;
global using DiscordPlayerCountBot.Configuration;
global using PlayerCountBot;
6 changes: 6 additions & 0 deletions DiscordPlayerCountBot.sln
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
.editorconfig = .editorconfig
EndProjectSection
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DiscordPlayerCountBot.Tests", "DiscordPlayerCountBot.Tests\DiscordPlayerCountBot.Tests.csproj", "{917FC191-C1C2-4667-8265-23BC68D259B5}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -20,6 +22,10 @@ Global
{BBBF5976-1AE8-4592-A67B-E5FF35A916FB}.Debug|Any CPU.Build.0 = Debug|Any CPU
{BBBF5976-1AE8-4592-A67B-E5FF35A916FB}.Release|Any CPU.ActiveCfg = Release|Any CPU
{BBBF5976-1AE8-4592-A67B-E5FF35A916FB}.Release|Any CPU.Build.0 = Release|Any CPU
{917FC191-C1C2-4667-8265-23BC68D259B5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{917FC191-C1C2-4667-8265-23BC68D259B5}.Debug|Any CPU.Build.0 = Debug|Any CPU
{917FC191-C1C2-4667-8265-23BC68D259B5}.Release|Any CPU.ActiveCfg = Release|Any CPU
{917FC191-C1C2-4667-8265-23BC68D259B5}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
35 changes: 4 additions & 31 deletions DiscordPlayerCountBot/Bot/Bot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
using log4net;
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;

namespace PlayerCountBot
Expand Down Expand Up @@ -48,15 +46,15 @@ public void InitDataProviders()
DataProviders.Add((int)DataProvider.BATTLEMETRICS, new BattleMetricsProvider());
}

public async Task StartAsync()
public async Task StartAsync(bool shouldStart)
{
if (Information.Address.Contains("hostname") || Information.Address.Contains("localhost"))
{
Information.Address = await AddressHelper.ResolveAddress(Information.Address);
}

Logger.Info($"[Bot] - Loaded {Information.Name} at address and port: {Information.Address}, {Information.ProviderType}");
await DiscordClient.LoginAndStartAsync(Information.Token, Information.Address);
await DiscordClient.LoginAndStartAsync(Information.Token, Information.Address, shouldStart);
}

public async Task StopAsync()
Expand All @@ -67,6 +65,7 @@ public async Task StopAsync()
public async Task UpdateAsync()
{
var dataProviderType = EnumHelper.GetDataProvider(Information.ProviderType);

if (dataProviderType != Information.ProviderType)
{
Logger.Warn($"[Bot] - Config for bot at address: {Information.Address} has an invalid provider type: {Information.ProviderType}");
Expand All @@ -90,33 +89,7 @@ public async Task UpdateAsync()

var activityType = (ActivityType)(activityInteger);
await DiscordClient.SetGameAsync(gameStatus, null, activityType);

if (Information.ChannelID.HasValue)
{
IDiscordClient socket = DiscordClient;
IGuildChannel channel = (IGuildChannel)await socket.GetChannelAsync(Information.ChannelID.Value);

if (channel is null)
{
var exception = new ArgumentException();
Logger.Error($"[Bot] - Invalid Channel Id: {Information.ChannelID}, Channel was not found.", exception);
throw exception;
}

if (channel != null)
{
if (channel is ITextChannel)
{
gameStatus = gameStatus.Replace('/', '-').Replace(' ', '-').Replace(':', '-');
}

//Keep in mind there is a massive rate limit on this call that is specific to discord, and not Discord.Net
//2x per 10 minutes
//https://discord.com/developers/docs/topics/rate-limits
//https://www.reddit.com/r/Discord_Bots/comments/qzrl5h/channel_name_edit_rate_limit/
await channel.ModifyAsync(prop => prop.Name = gameStatus);
}
}
await DiscordClient.SetChannelName(Information.ChannelID, gameStatus);
}
}
}
2 changes: 1 addition & 1 deletion DiscordPlayerCountBot/Configuration/Base/IConfigurable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ namespace DiscordPlayerCountBot.Configuration.Base
{
public interface IConfigurable
{
Task<Tuple<Dictionary<string, Bot>, int>> Configure();
Task<Tuple<Dictionary<string, Bot>, int>> Configure(bool shouldStart = true);
}
}
Loading

0 comments on commit 27356b0

Please sign in to comment.