Skip to content

Commit

Permalink
Configure and updated Runtime REST and CACHE setting using in CLI (#2435
Browse files Browse the repository at this point in the history
)

## Why make this change?
Resolved issue #2419 
This change is made to enable dab users to update/change values of Rest
and Cache runtime settings from CLI commands.
<img width="640" alt="image"
src="https://github.com/user-attachments/assets/9edaa021-58fb-494e-9c74-a8de10d20102">
<img width="647" alt="image"
src="https://github.com/user-attachments/assets/68535a67-fe8d-4396-b874-39ea893e1831">

## What is this change?
1. Made changes in ConfigureOptions.cs to include the additional
parameters for Rest and Cache.
2. Changes in ConfigureGenerator.cs to include the function to update
the newly incoming values in the parameters of Rest and Cache runtime
settings
3. Changes in Tests to add individual tests for Enabled, Path and
Request-Body-Strict for Rest Runtime settings and Enabled and TTL for
Cache runtime settings and have two test for multiple parameters in a
single command.

## How was this tested?

- [x] Integration Tests
- [x] Unit Tests

## Sample Request(s)
`dab configure --runtime.rest.enabled false -c {Config-File-Name}`
Updating the Rest.Enabled value  
<img width="629" alt="image"
src="https://github.com/user-attachments/assets/4132e372-f14f-4553-aa48-7c8fd5731cf2">

`dab configure --runtime.rest.path "/updating -c {Config-File-Name}`
Updating the Rest.Path value  
<img width="623" alt="image"
src="https://github.com/user-attachments/assets/23fa44de-9d02-4e0a-ba91-237e937908da">

Errors or Negative Test cases in updating Rest.Path
- updating
- /upda/ting
- /upda ting
- /upda@ting
<img width="670" alt="image"
src="https://github.com/user-attachments/assets/13f5b42e-33ff-4d0d-b5e3-452810d9a4f3">

Cache Sample Requests
Update Enabled
<img width="673" alt="image"
src="https://github.com/user-attachments/assets/ba5b041d-4f33-4e61-96ed-72927dec5b23">
Update TTL
<img width="677" alt="image"
src="https://github.com/user-attachments/assets/6b0a3c30-b7b8-4c69-9af2-353ea9fc851f">
  • Loading branch information
sezal98 authored Oct 31, 2024
1 parent fac7148 commit 62f9387
Show file tree
Hide file tree
Showing 5 changed files with 450 additions and 31 deletions.
207 changes: 190 additions & 17 deletions src/Cli.Tests/ConfigureOptionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,18 +55,19 @@ public void TestNoUpdateOnGraphQLDepthLimitInRuntimeSettings(object? depthLimit,

// Arrange
_fileSystem!.AddFile(TEST_RUNTIME_CONFIG_FILE, initialConfig);
Assert.IsTrue(_fileSystem!.File.Exists(TEST_RUNTIME_CONFIG_FILE));

// Act: Run Configure with no options
ConfigureOptions options = new(
config: TEST_RUNTIME_CONFIG_FILE
);
bool isSuccess = TryConfigureSettings(options, _runtimeConfigLoader!, _fileSystem!);

Assert.IsTrue(_fileSystem!.File.Exists(TEST_RUNTIME_CONFIG_FILE));
Assert.IsTrue(TryConfigureSettings(options, _runtimeConfigLoader!, _fileSystem!));

string updatedConfig = _fileSystem!.File.ReadAllText(TEST_RUNTIME_CONFIG_FILE);
// Assert
Assert.IsTrue(isSuccess);

// Assert that INITIAL_CONFIG is same as the updated config
string updatedConfig = _fileSystem!.File.ReadAllText(TEST_RUNTIME_CONFIG_FILE);
if (isDepthLimitProvidedInConfig)
{
Assert.IsTrue(updatedConfig.Contains(depthLimitSection));
Expand Down Expand Up @@ -100,9 +101,10 @@ public void TestAddDepthLimitForGraphQL()
depthLimit: maxDepthLimit,
config: TEST_RUNTIME_CONFIG_FILE
);
Assert.IsTrue(TryConfigureSettings(options, _runtimeConfigLoader!, _fileSystem!));
bool isSuccess = TryConfigureSettings(options, _runtimeConfigLoader!, _fileSystem!);

// Assert: Validate the Depth Limit is added
Assert.IsTrue(isSuccess);
string updatedConfig = _fileSystem!.File.ReadAllText(TEST_RUNTIME_CONFIG_FILE);
Assert.IsTrue(RuntimeConfigLoader.TryParseConfig(updatedConfig, out config));
Assert.IsNotNull(config.Runtime?.GraphQL?.DepthLimit);
Expand All @@ -114,8 +116,8 @@ public void TestAddDepthLimitForGraphQL()
/// in runtime. Takes in updated value for graphql.enabled and
/// validates whether the runtime config reflects those updated values
[DataTestMethod]
[DataRow(false, DisplayName = "Update enabled to be false for GraphQL.")]
[DataRow(true, DisplayName = "Update enabled to be true for GraphQL.")]
[DataRow(false, DisplayName = "Update GraphQL.Enabled to false.")]
[DataRow(true, DisplayName = "Validate GraphQL.Enabled to remain true.")]
public void TestUpdateEnabledForGraphQLSettings(bool updatedEnabledValue)
{
// Arrange -> all the setup which includes creating options.
Expand All @@ -126,9 +128,10 @@ public void TestUpdateEnabledForGraphQLSettings(bool updatedEnabledValue)
runtimeGraphQLEnabled: updatedEnabledValue,
config: TEST_RUNTIME_CONFIG_FILE
);
Assert.IsTrue(TryConfigureSettings(options, _runtimeConfigLoader!, _fileSystem!));
bool isSuccess = TryConfigureSettings(options, _runtimeConfigLoader!, _fileSystem!);

// Assert: Validate the Enabled Flag is updated
Assert.IsTrue(isSuccess);
string updatedConfig = _fileSystem!.File.ReadAllText(TEST_RUNTIME_CONFIG_FILE);
Assert.IsTrue(RuntimeConfigLoader.TryParseConfig(updatedConfig, out RuntimeConfig? runtimeConfig));
Assert.IsNotNull(runtimeConfig.Runtime?.GraphQL?.Enabled);
Expand All @@ -153,9 +156,10 @@ public void TestUpdatePathForGraphQLSettings(string updatedPathValue)
runtimeGraphQLPath: updatedPathValue,
config: TEST_RUNTIME_CONFIG_FILE
);
Assert.IsTrue(TryConfigureSettings(options, _runtimeConfigLoader!, _fileSystem!));
bool isSuccess = TryConfigureSettings(options, _runtimeConfigLoader!, _fileSystem!);

// Assert: Validate the Path update is updated
Assert.IsTrue(isSuccess);
string updatedConfig = _fileSystem!.File.ReadAllText(TEST_RUNTIME_CONFIG_FILE);
Assert.IsTrue(RuntimeConfigLoader.TryParseConfig(updatedConfig, out RuntimeConfig? runtimeConfig));
Assert.IsNotNull(runtimeConfig.Runtime?.GraphQL?.Path);
Expand All @@ -168,8 +172,8 @@ public void TestUpdatePathForGraphQLSettings(string updatedPathValue)
/// Takes in updated value for graphql.allow-introspection and
/// validates whether the runtime config reflects those updated values
[DataTestMethod]
[DataRow(false, DisplayName = "Update AllowIntrospection to be false for GraphQL.")]
[DataRow(true, DisplayName = "Update AllowIntrospection to be true for GraphQL.")]
[DataRow(false, DisplayName = "Update GraphQL.AllowIntrospection to be false.")]
[DataRow(true, DisplayName = "Validate GraphQL.AllowIntrospection to remain true.")]
public void TestUpdateAllowIntrospectionForGraphQLSettings(bool updatedAllowIntrospectionValue)
{
// Arrange -> all the setup which includes creating options.
Expand All @@ -180,9 +184,10 @@ public void TestUpdateAllowIntrospectionForGraphQLSettings(bool updatedAllowIntr
runtimeGraphQLAllowIntrospection: updatedAllowIntrospectionValue,
config: TEST_RUNTIME_CONFIG_FILE
);
Assert.IsTrue(TryConfigureSettings(options, _runtimeConfigLoader!, _fileSystem!));
bool isSuccess = TryConfigureSettings(options, _runtimeConfigLoader!, _fileSystem!);

// Assert: Validate the Allow-Introspection value is updated
Assert.IsTrue(isSuccess);
string updatedConfig = _fileSystem!.File.ReadAllText(TEST_RUNTIME_CONFIG_FILE);
Assert.IsTrue(RuntimeConfigLoader.TryParseConfig(updatedConfig, out RuntimeConfig? runtimeConfig));
Assert.IsNotNull(runtimeConfig.Runtime?.GraphQL?.AllowIntrospection);
Expand All @@ -195,8 +200,8 @@ public void TestUpdateAllowIntrospectionForGraphQLSettings(bool updatedAllowIntr
/// Takes in updated value for multiple mutations.create.enabled and
/// validates whether the runtime config reflects those updated values
[DataTestMethod]
[DataRow(false, DisplayName = "Update MultipleMutation.Create.Enabled to be false for GraphQL.")]
[DataRow(true, DisplayName = "Update MultipleMutation.Create.Enabled to be true for GraphQL.")]
[DataRow(false, DisplayName = "Update GraphQL.MultipleMutation.Create.Enabled to be false.")]
[DataRow(true, DisplayName = "Validate GraphQL.MultipleMutation.Create.Enabled to remain true.")]
public void TestUpdateMultipleMutationCreateEnabledForGraphQLSettings(bool updatedMultipleMutationsCreateEnabledValue)
{
// Arrange -> all the setup which includes creating options.
Expand All @@ -207,9 +212,10 @@ public void TestUpdateMultipleMutationCreateEnabledForGraphQLSettings(bool updat
runtimeGraphQLMultipleMutationsCreateEnabled: updatedMultipleMutationsCreateEnabledValue,
config: TEST_RUNTIME_CONFIG_FILE
);
Assert.IsTrue(TryConfigureSettings(options, _runtimeConfigLoader!, _fileSystem!));
bool isSuccess = TryConfigureSettings(options, _runtimeConfigLoader!, _fileSystem!);

// Assert: Validate the Multiple-Mutation.Create.Enabled is updated
Assert.IsTrue(isSuccess);
string updatedConfig = _fileSystem!.File.ReadAllText(TEST_RUNTIME_CONFIG_FILE);
Assert.IsTrue(RuntimeConfigLoader.TryParseConfig(updatedConfig, out RuntimeConfig? runtimeConfig));
Assert.IsNotNull(runtimeConfig.Runtime?.GraphQL?.MultipleMutationOptions?.MultipleCreateOptions?.Enabled);
Expand All @@ -235,9 +241,10 @@ public void TestUpdateMultipleParametersForGraphQLSettings()
runtimeGraphQLAllowIntrospection: updatedAllowIntrospectionValue,
config: TEST_RUNTIME_CONFIG_FILE
);
Assert.IsTrue(TryConfigureSettings(options, _runtimeConfigLoader!, _fileSystem!));
bool isSuccess = TryConfigureSettings(options, _runtimeConfigLoader!, _fileSystem!);

// Assert: Validate the path is updated and allow introspection is updated
Assert.IsTrue(isSuccess);
string updatedConfig = _fileSystem!.File.ReadAllText(TEST_RUNTIME_CONFIG_FILE);
Assert.IsTrue(RuntimeConfigLoader.TryParseConfig(updatedConfig, out RuntimeConfig? runtimeConfig));
Assert.IsNotNull(runtimeConfig.Runtime?.GraphQL?.Path);
Expand All @@ -246,6 +253,171 @@ public void TestUpdateMultipleParametersForGraphQLSettings()
Assert.AreEqual(updatedAllowIntrospectionValue, runtimeConfig.Runtime.GraphQL.AllowIntrospection);
}

/// <summary>
/// Tests that running "dab configure --runtime.rest.enabled {value}" on a config with various values results
/// in runtime config update. Takes in updated value for rest.enabled and
/// validates whether the runtime config reflects those updated values
[DataTestMethod]
[DataRow(false, DisplayName = "Update Rest.Enabled to false.")]
[DataRow(true, DisplayName = "Validate if Rest.Enabled remains true.")]
public void TestUpdateEnabledForRestSettings(bool updatedEnabledValue)
{
// Arrange -> all the setup which includes creating options.
SetupFileSystemWithInitialConfig(INITIAL_CONFIG);

// Act: Attempts to update enabled flag
ConfigureOptions options = new(
runtimeRestEnabled: updatedEnabledValue,
config: TEST_RUNTIME_CONFIG_FILE
);
bool isSuccess = TryConfigureSettings(options, _runtimeConfigLoader!, _fileSystem!);

// Assert: Validate the Enabled Flag is updated
Assert.IsTrue(isSuccess);
string updatedConfig = _fileSystem!.File.ReadAllText(TEST_RUNTIME_CONFIG_FILE);
Assert.IsTrue(RuntimeConfigLoader.TryParseConfig(updatedConfig, out RuntimeConfig? runtimeConfig));
Assert.IsNotNull(runtimeConfig.Runtime?.Rest?.Enabled);
Assert.AreEqual(updatedEnabledValue, runtimeConfig.Runtime.Rest.Enabled);
}

/// <summary>
/// Tests that running "dab configure --runtime.rest.path {value}" on a config with various values results
/// in runtime config update. Takes in updated value for rest.path and
/// validates whether the runtime config reflects those updated values
[DataTestMethod]
[DataRow("/updatedPath", DisplayName = "Update REST path to /updatedPath.")]
[DataRow("/updated_Path", DisplayName = "Ensure underscore is allowed in REST path.")]
[DataRow("/updated-Path", DisplayName = "Ensure hyphen is allowed in REST path.")]
public void TestUpdatePathForRestSettings(string updatedPathValue)
{
// Arrange -> all the setup which includes creating options.
SetupFileSystemWithInitialConfig(INITIAL_CONFIG);

// Act: Attempts to update path value
ConfigureOptions options = new(
runtimeRestPath: updatedPathValue,
config: TEST_RUNTIME_CONFIG_FILE
);
bool isSuccess = TryConfigureSettings(options, _runtimeConfigLoader!, _fileSystem!);

// Assert: Validate the Path update is updated
Assert.IsTrue(isSuccess);
string updatedConfig = _fileSystem!.File.ReadAllText(TEST_RUNTIME_CONFIG_FILE);
Assert.IsTrue(RuntimeConfigLoader.TryParseConfig(updatedConfig, out RuntimeConfig? runtimeConfig));
Assert.IsNotNull(runtimeConfig.Runtime?.Rest?.Path);
Assert.AreEqual(updatedPathValue, runtimeConfig.Runtime.Rest.Path);
}

/// <summary>
/// Tests that running "dab configure --runtime.rest.request-body-strict" on a config with various values results
/// in runtime config update. Takes in updated value for rest.request-body-strict and
/// validates whether the runtime config reflects those updated values
[DataTestMethod]
[DataRow(false, DisplayName = "Update Rest.Request-Body-Strict to false.")]
[DataRow(true, DisplayName = "Validate if Rest.Request-body-Strict remains true.")]
public void TestUpdateRequestBodyStrictForRestSettings(bool updatedRequestBodyStrictValue)
{
// Arrange -> all the setup which includes creating options.
SetupFileSystemWithInitialConfig(INITIAL_CONFIG);

// Act: Attempts to update request-body-strict value
ConfigureOptions options = new(
runtimeRestRequestBodyStrict: updatedRequestBodyStrictValue,
config: TEST_RUNTIME_CONFIG_FILE
);
bool isSuccess = TryConfigureSettings(options, _runtimeConfigLoader!, _fileSystem!);

// Assert: Validate the RequestBodyStrict Value is updated
Assert.IsTrue(isSuccess);
string updatedConfig = _fileSystem!.File.ReadAllText(TEST_RUNTIME_CONFIG_FILE);
Assert.IsTrue(RuntimeConfigLoader.TryParseConfig(updatedConfig, out RuntimeConfig? runtimeConfig));
Assert.IsNotNull(runtimeConfig.Runtime?.Rest?.RequestBodyStrict);
Assert.AreEqual(updatedRequestBodyStrictValue, runtimeConfig.Runtime.Rest.RequestBodyStrict);
}

/// <summary>
/// Tests that running "dab configure --runtime.rest.enabled {value} --runtime.rest.path {value}"
/// on a config with various values results in runtime config update.
/// Takes in updated value for enabled and path and further
/// validates whether the runtime config reflects those updated values
[DataTestMethod]
[DataRow(false, "/updatedPath", DisplayName = "Update enabled flag and path in Rest runtime settings.")]
public void TestUpdateMultipleParametersRestSettings(bool updatedEnabledValue, string updatedPathValue)
{
// Arrange -> all the setup which includes creating options.
SetupFileSystemWithInitialConfig(INITIAL_CONFIG);

// Act: Attempts to update the path value and enabled flag
ConfigureOptions options = new(
runtimeRestPath: updatedPathValue,
runtimeRestEnabled: updatedEnabledValue,
config: TEST_RUNTIME_CONFIG_FILE
);
bool isSuccess = TryConfigureSettings(options, _runtimeConfigLoader!, _fileSystem!);

// Assert: Validate the path is updated and enabled is updated
Assert.IsTrue(isSuccess);
string updatedConfig = _fileSystem!.File.ReadAllText(TEST_RUNTIME_CONFIG_FILE);
Assert.IsTrue(RuntimeConfigLoader.TryParseConfig(updatedConfig, out RuntimeConfig? runtimeConfig));
Assert.IsNotNull(runtimeConfig.Runtime?.Rest?.Path);
Assert.IsNotNull(runtimeConfig.Runtime?.Rest?.Enabled);
Assert.AreEqual(updatedPathValue, runtimeConfig.Runtime.Rest.Path);
Assert.AreEqual(updatedEnabledValue, runtimeConfig.Runtime.Rest.Enabled);
}

/// <summary>
/// Validates that running "dab configure --runtime.cache.enabled" on a config with various values results
/// in runtime config update. Takes in updated value for cache.enabled and
/// validates whether the runtime config reflects those updated values.
[DataTestMethod]
[DataRow(false, DisplayName = "Update Cache.Enabled to false.")]
[DataRow(true, DisplayName = "Validate if Cache.Enabled remains true.")]
public void TestUpdateEnabledForCacheSettings(bool updatedEnabledValue)
{
// Arrange -> all the setup which includes creating options.
SetupFileSystemWithInitialConfig(INITIAL_CONFIG);

// Act: Attempts to update cache enabled flag
ConfigureOptions options = new(
runtimeCacheEnabled: updatedEnabledValue,
config: TEST_RUNTIME_CONFIG_FILE
);
bool isSuccess = TryConfigureSettings(options, _runtimeConfigLoader!, _fileSystem!);

// Assert: Validate the cache Enabled Flag is updated
Assert.IsTrue(isSuccess);
string updatedConfig = _fileSystem!.File.ReadAllText(TEST_RUNTIME_CONFIG_FILE);
Assert.IsTrue(RuntimeConfigLoader.TryParseConfig(updatedConfig, out RuntimeConfig? runtimeConfig));
Assert.IsNotNull(runtimeConfig.Runtime?.Cache?.Enabled);
Assert.AreEqual(updatedEnabledValue, runtimeConfig.Runtime.Cache.Enabled);
}

/// <summary>
/// Tests that running "dab configure --runtime.cache.ttl-seconds" on a config with various values results
/// in runtime config update. Takes in updated value for cache.ttl-seconds and
/// validates whether the runtime config reflects those updated values
[DataTestMethod]
[DataRow(4, DisplayName = "Update global cache TTL to 4.")]
public void TestUpdateTTLForCacheSettings(int updatedTtlValue)
{
// Arrange -> all the setup which includes creating options.
SetupFileSystemWithInitialConfig(INITIAL_CONFIG);

// Act: Attempts to update TTL Value
ConfigureOptions options = new(
runtimeCacheTtl: updatedTtlValue,
config: TEST_RUNTIME_CONFIG_FILE
);
bool isSuccess = TryConfigureSettings(options, _runtimeConfigLoader!, _fileSystem!);

// Assert: Validate the TTL Value is updated
Assert.IsTrue(isSuccess);
string updatedConfig = _fileSystem!.File.ReadAllText(TEST_RUNTIME_CONFIG_FILE);
Assert.IsTrue(RuntimeConfigLoader.TryParseConfig(updatedConfig, out RuntimeConfig? runtimeConfig));
Assert.IsNotNull(runtimeConfig.Runtime?.Cache?.TtlSeconds);
Assert.AreEqual(updatedTtlValue, runtimeConfig.Runtime.Cache.TtlSeconds);
}

/// <summary>
/// Test to update the current depth limit for GraphQL and removal the depth limit using -1.
/// When runtime.graphql.depth-limit has an initial value of 8.
Expand Down Expand Up @@ -279,9 +451,10 @@ public void TestUpdateDepthLimitForGraphQL(int? newDepthLimit)
);

// Act: Update Depth Limit
Assert.IsTrue(TryConfigureSettings(options, _runtimeConfigLoader!, _fileSystem!));
bool isSuccess = TryConfigureSettings(options, _runtimeConfigLoader!, _fileSystem!);

// Assert: Validate the Depth Limit is updated
Assert.IsTrue(isSuccess);
string updatedConfig = _fileSystem!.File.ReadAllText(TEST_RUNTIME_CONFIG_FILE);
Assert.IsTrue(RuntimeConfigLoader.TryParseConfig(updatedConfig, out config));

Expand Down
Loading

0 comments on commit 62f9387

Please sign in to comment.