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