forked from valkey-io/valkey-glide
-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
C#: Make IT to manage redis. #116
Merged
Yury-Fridlyand
merged 3 commits into
csharp/integ_yuryf_it_use_script
from
csharp/dev_yuryf_it_use_script
Mar 4, 2024
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
/** | ||
* Copyright GLIDE-for-Redis Project Contributors - SPDX Identifier: Apache-2.0 | ||
*/ | ||
|
||
using System.Diagnostics; | ||
|
||
// Note: All IT should be in the same namespace | ||
namespace tests.Integration; | ||
|
||
[SetUpFixture] | ||
public class IntegrationTestBase | ||
{ | ||
[OneTimeSetUp] | ||
public void SetUp() | ||
{ | ||
// Stop all if weren't stopped on previous test run | ||
StopRedis(false); | ||
|
||
// Delete dirs if stop failed due to https://github.com/aws/glide-for-redis/issues/849 | ||
Directory.Delete(Path.Combine(scriptDir, "clusters"), true); | ||
|
||
// Start cluster | ||
CLUSTER_PORTS = StartRedis(true); | ||
// Start standalone | ||
STANDALONE_PORTS = StartRedis(false); | ||
|
||
// Get redis version | ||
REDIS_VERSION = GetRedisVersion(); | ||
|
||
TestContext.Progress.WriteLine($"Cluster ports = {string.Join(',', CLUSTER_PORTS)}"); | ||
TestContext.Progress.WriteLine($"Standalone ports = {string.Join(',', STANDALONE_PORTS)}"); | ||
TestContext.Progress.WriteLine($"Redis version = {REDIS_VERSION}"); | ||
} | ||
|
||
[OneTimeTearDown] | ||
public void TearDown() | ||
{ | ||
// Stop all | ||
StopRedis(true); | ||
} | ||
|
||
public static List<uint> STANDALONE_PORTS { get; private set; } = new(); | ||
public static List<uint> CLUSTER_PORTS { get; private set; } = new(); | ||
public static Version REDIS_VERSION { get; private set; } = new(); | ||
|
||
private readonly string scriptDir; | ||
|
||
// Nunit requires a public default constructor. These variables would be set in SetUp method. | ||
public IntegrationTestBase() | ||
{ | ||
STANDALONE_PORTS = new(); | ||
CLUSTER_PORTS = new(); | ||
REDIS_VERSION = new(); | ||
|
||
string path = TestContext.CurrentContext.WorkDirectory; | ||
if (Path.GetFileName(path) != "csharp") | ||
throw new FileNotFoundException("`WorkDirectory` is incorrect or not defined. Please ensure the WorkDirectory was set by passing `-- NUnit.WorkDirectory=<path>` to the `dotnet test` command."); | ||
|
||
scriptDir = Path.Combine(path, "..", "utils"); | ||
} | ||
|
||
|
||
public List<uint> StartRedis(bool cluster, bool tls = false, string? name = null) | ||
{ | ||
var cmd = $"start {(cluster ? "--cluster-mode" : "-r 0")} {(tls ? " --tls" : "")} {(name != null ? " --prefix " + name : "")}"; | ||
return ParsePortsFromOutput(RunClusterManager(cmd, false)); | ||
} | ||
|
||
/// <summary> | ||
/// Stop <b>all</b> instances on the given <paramref name="name"/>. | ||
/// </summary> | ||
public void StopRedis(bool keepLogs, string? name = null) | ||
{ | ||
var cmd = $"stop --prefix {name ?? "redis-cluster"} {(keepLogs ? "--keep-folder" : "")}"; | ||
RunClusterManager(cmd, true); | ||
} | ||
|
||
|
||
private string RunClusterManager(string cmd, bool ignoreExitCode) | ||
{ | ||
var info = new ProcessStartInfo | ||
{ | ||
WorkingDirectory = scriptDir, | ||
FileName = "python3", | ||
Arguments = "cluster_manager.py " + cmd, | ||
UseShellExecute = false, | ||
RedirectStandardOutput = true, | ||
RedirectStandardError = true, | ||
}; | ||
var script = Process.Start(info); | ||
script?.WaitForExit(); | ||
var error = script?.StandardError.ReadToEnd(); | ||
var output = script?.StandardOutput.ReadToEnd(); | ||
var exit_code = script?.ExitCode; | ||
|
||
TestContext.Progress.WriteLine($"cluster_manager.py stdout\n====\n{output}\n====\ncluster_manager.py stderr\n====\n{error}\n====\n"); | ||
|
||
if (!ignoreExitCode && exit_code != 0) | ||
throw new ApplicationException($"cluster_manager.py script failed: exit code {exit_code}."); | ||
|
||
return output ?? ""; | ||
} | ||
|
||
private List<uint> ParsePortsFromOutput(string output) | ||
{ | ||
var ports = new List<uint>(); | ||
foreach (var line in output.Split("\n")) | ||
{ | ||
if (!line.StartsWith("CLUSTER_NODES=")) | ||
continue; | ||
|
||
var addresses = line.Split("=")[1].Split(","); | ||
foreach (var address in addresses) | ||
ports.Add(uint.Parse(address.Split(":")[1])); | ||
} | ||
return ports; | ||
} | ||
|
||
private Version GetRedisVersion() | ||
{ | ||
var info = new ProcessStartInfo | ||
{ | ||
FileName = "redis-server", | ||
Arguments = "-v", | ||
UseShellExecute = false, | ||
RedirectStandardOutput = true, | ||
}; | ||
var proc = Process.Start(info); | ||
proc?.WaitForExit(); | ||
var output = proc?.StandardOutput.ReadToEnd() ?? ""; | ||
|
||
// Redis server v=7.2.3 sha=00000000:0 malloc=jemalloc-5.3.0 bits=64 build=7504b1fedf883f2 | ||
return new Version(output.Split(" ")[2].Split("=")[1]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
/** | ||
* Copyright GLIDE-for-Redis Project Contributors - SPDX Identifier: Apache-2.0 | ||
*/ | ||
|
||
namespace tests.Unit; | ||
|
||
using Glide; | ||
|
||
// John Travolta is looking for tests | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.