|
| 1 | +using System.Collections.Generic; |
| 2 | +using FaunaDB.Client; |
| 3 | +using NUnit.Framework; |
| 4 | + |
| 5 | +namespace Test |
| 6 | +{ |
| 7 | + [TestFixture] |
| 8 | + public class EnvironmentHeaderTest |
| 9 | + { |
| 10 | + private IEnvironmentEditor environmentEditor; |
| 11 | + |
| 12 | + [OneTimeSetUp] |
| 13 | + public void SetUp() |
| 14 | + { |
| 15 | + environmentEditor = new EnvironmentEditorMock(); |
| 16 | + } |
| 17 | + |
| 18 | + [SetUp] |
| 19 | + public void DestroyRuntimeEnvironmentHeaderInstance() |
| 20 | + { |
| 21 | + RuntimeEnvironmentHeader.Destroy(); |
| 22 | + } |
| 23 | + |
| 24 | + [Test] |
| 25 | + public void TestRuntimeEnvironmentHeaderFormat() |
| 26 | + { |
| 27 | + string actual = RuntimeEnvironmentHeader.Construct(environmentEditor); |
| 28 | + Assert.That(actual, Does.Match("driver=csharp-.*; runtime=.*; env=.*; os=.*")); |
| 29 | + } |
| 30 | + |
| 31 | + [Test] |
| 32 | + public void TestNetlifyEnvironment() |
| 33 | + { |
| 34 | + environmentEditor.SetVariable("NETLIFY_IMAGES_CDN_DOMAIN", "some_value"); |
| 35 | + var actual = RuntimeEnvironmentHeader.Construct(environmentEditor); |
| 36 | + Assert.That(actual, Does.Contain("Netlify")); |
| 37 | + } |
| 38 | + |
| 39 | + [Test] |
| 40 | + public void TestVercelEnvironment() |
| 41 | + { |
| 42 | + environmentEditor.SetVariable("VERCEL", "some_value"); |
| 43 | + var actual = RuntimeEnvironmentHeader.Construct(environmentEditor); |
| 44 | + Assert.That(actual, Does.Contain("Vercel")); |
| 45 | + } |
| 46 | + |
| 47 | + [Test] |
| 48 | + public void TestHerokuEnvironment() |
| 49 | + { |
| 50 | + environmentEditor.SetVariable("PATH", "heroku"); |
| 51 | + var actual = RuntimeEnvironmentHeader.Construct(environmentEditor); |
| 52 | + Assert.That(actual, Does.Contain("Heroku")); |
| 53 | + } |
| 54 | + |
| 55 | + [Test] |
| 56 | + public void TestUnknownEnvironmentWithPathVariable() |
| 57 | + { |
| 58 | + environmentEditor.SetVariable("PATH", "some_value"); |
| 59 | + var actual = RuntimeEnvironmentHeader.Construct(environmentEditor); |
| 60 | + Assert.That(actual, Does.Contain("Unknown")); |
| 61 | + } |
| 62 | + |
| 63 | + [Test] |
| 64 | + public void TestAwsLambdaEnvironment() |
| 65 | + { |
| 66 | + environmentEditor.SetVariable("AWS_LAMBDA_FUNCTION_VERSION", "some_value"); |
| 67 | + var actual = RuntimeEnvironmentHeader.Construct(environmentEditor); |
| 68 | + Assert.That(actual, Does.Contain("AWS Lambda")); |
| 69 | + } |
| 70 | + |
| 71 | + [Test] |
| 72 | + public void TestGoogleFunctionsEnvironment() |
| 73 | + { |
| 74 | + environmentEditor.SetVariable("_", "google"); |
| 75 | + var actual = RuntimeEnvironmentHeader.Construct(environmentEditor); |
| 76 | + Assert.That(actual, Does.Contain("GCP Cloud Functions")); |
| 77 | + RuntimeEnvironmentHeader.Destroy(); |
| 78 | + } |
| 79 | + |
| 80 | + [Test] |
| 81 | + public void TestUnknownEnvironmentWithUnderscoreVariable() |
| 82 | + { |
| 83 | + environmentEditor.SetVariable("_", "some_value"); |
| 84 | + var actual = RuntimeEnvironmentHeader.Construct(environmentEditor); |
| 85 | + Assert.That(actual, Does.Contain("Unknown")); |
| 86 | + } |
| 87 | + |
| 88 | + [Test] |
| 89 | + public void TestGoogleCloudEnvironment() |
| 90 | + { |
| 91 | + environmentEditor.SetVariable("GOOGLE_CLOUD_PROJECT", "some_value"); |
| 92 | + var actual = RuntimeEnvironmentHeader.Construct(environmentEditor); |
| 93 | + Assert.That(actual, Does.Contain("GCP Compute Instances")); |
| 94 | + } |
| 95 | + |
| 96 | + [Test] |
| 97 | + public void TestAzureEnvironment() |
| 98 | + { |
| 99 | + environmentEditor.SetVariable("ORYX_ENV_TYPE", "AppService"); |
| 100 | + environmentEditor.SetVariable("WEBSITE_INSTANCE_ID", "some_value"); |
| 101 | + var actual = RuntimeEnvironmentHeader.Construct(environmentEditor); |
| 102 | + Assert.That(actual, Does.Contain("Azure Compute")); |
| 103 | + } |
| 104 | + |
| 105 | + [Test] |
| 106 | + public void TestUnknownEnvironmentWithOryx() |
| 107 | + { |
| 108 | + environmentEditor.SetVariable("ORYX_ENV_TYPE", "some_value"); |
| 109 | + environmentEditor.SetVariable("WEBSITE_INSTANCE_ID", "some_value"); |
| 110 | + var actual = RuntimeEnvironmentHeader.Construct(environmentEditor); |
| 111 | + Assert.That(actual, Does.Contain("Unknown")); |
| 112 | + } |
| 113 | + |
| 114 | + [Test] |
| 115 | + public void TestUnknownEnvironmentWithOryxWithoutWebsiteInstanceId() |
| 116 | + { |
| 117 | + environmentEditor.SetVariable("ORYX_ENV_TYPE", "AppService"); |
| 118 | + var actual = RuntimeEnvironmentHeader.Construct(environmentEditor); |
| 119 | + Assert.That(actual, Does.Contain("Unknown")); |
| 120 | + } |
| 121 | + |
| 122 | + [TearDown] |
| 123 | + public void RemoveEnvironmentVariables() |
| 124 | + { |
| 125 | + environmentEditor.RemoveVariable("NETLIFY_IMAGES_CDN_DOMAIN"); |
| 126 | + environmentEditor.RemoveVariable("VERCEL"); |
| 127 | + environmentEditor.RemoveVariable("PATH"); |
| 128 | + environmentEditor.RemoveVariable("AWS_LAMBDA_FUNCTION_VERSION"); |
| 129 | + environmentEditor.RemoveVariable("_"); |
| 130 | + environmentEditor.RemoveVariable("GOOGLE_CLOUD_PROJECT"); |
| 131 | + environmentEditor.RemoveVariable("ORYX_ENV_TYPE"); |
| 132 | + environmentEditor.RemoveVariable("WEBSITE_INSTANCE_ID"); |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + class EnvironmentEditorMock : IEnvironmentEditor |
| 137 | + { |
| 138 | + private Dictionary<string, string> mockEnvironment; |
| 139 | + |
| 140 | + public EnvironmentEditorMock() |
| 141 | + { |
| 142 | + mockEnvironment = new Dictionary<string, string>(); |
| 143 | + } |
| 144 | + |
| 145 | + public string GetVariable(string variableName) |
| 146 | + { |
| 147 | + if (!mockEnvironment.ContainsKey(variableName)) return null; |
| 148 | + return mockEnvironment[variableName]; |
| 149 | + } |
| 150 | + |
| 151 | + public void SetVariable(string variableName, string variableValue) |
| 152 | + { |
| 153 | + mockEnvironment[variableName] = variableValue; |
| 154 | + } |
| 155 | + |
| 156 | + public void RemoveVariable(string variableName) |
| 157 | + { |
| 158 | + mockEnvironment.Remove(variableName); |
| 159 | + } |
| 160 | + } |
| 161 | +} |
0 commit comments