-
Notifications
You must be signed in to change notification settings - Fork 726
Expand file tree
/
Copy pathBehaviorOperatorFixture.cs
More file actions
114 lines (93 loc) · 3.36 KB
/
BehaviorOperatorFixture.cs
File metadata and controls
114 lines (93 loc) · 3.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
using System.Collections;
namespace DotOpenDAL.Tests;
public sealed class BehaviorOperatorFixture : IDisposable
{
private readonly Operator? op;
public string? Scheme { get; }
public BehaviorOperatorFixture()
{
Scheme = Environment.GetEnvironmentVariable("OPENDAL_TEST");
if (string.IsNullOrWhiteSpace(Scheme))
{
return;
}
var scheme = Scheme.ToLowerInvariant().Replace('_', '-');
var options = BuildConfigFromEnvironment(Scheme);
// Align with other bindings: isolate behavior tests with random roots by default.
if (!IsRandomRootDisabled())
{
var baseRoot = options.TryGetValue("root", out var root) && !string.IsNullOrWhiteSpace(root)
? root!
: "/";
options["root"] = BuildRandomRoot(baseRoot);
}
op = new Operator(scheme, options);
}
public bool IsEnabled => op is not null;
public Operator Op => op ?? throw new InvalidOperationException("Behavior operator is not initialized.");
public Capability Capability => Op.Info.FullCapability;
public void Dispose()
{
op?.Dispose();
}
private static Dictionary<string, string> BuildConfigFromEnvironment(string service)
{
var variables = Environment.GetEnvironmentVariables();
var prefix = $"opendal_{service.ToLowerInvariant()}_";
var config = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
foreach (DictionaryEntry entry in variables)
{
var key = entry.Key?.ToString();
var value = entry.Value?.ToString();
if (string.IsNullOrWhiteSpace(key) || value is null)
{
continue;
}
var normalized = key.ToLowerInvariant();
if (!normalized.StartsWith(prefix, StringComparison.Ordinal))
{
continue;
}
config[normalized[prefix.Length..]] = value;
}
return config;
}
private static bool IsRandomRootDisabled()
{
return string.Equals(
Environment.GetEnvironmentVariable("OPENDAL_DISABLE_RANDOM_ROOT"),
"true",
StringComparison.OrdinalIgnoreCase);
}
private static string BuildRandomRoot(string baseRoot)
{
var trimmed = baseRoot.Trim();
if (trimmed.Length == 0)
{
trimmed = "/";
}
if (!trimmed.EndsWith('/'))
{
trimmed += "/";
}
return $"{trimmed}{Guid.NewGuid():N}/";
}
}