From 1fb7f924d996f574a02dbb0c467294b40967bf22 Mon Sep 17 00:00:00 2001 From: Dave Tillman Date: Wed, 13 Jun 2018 10:15:29 -0600 Subject: [PATCH] Add Platform.IsCloudFoundry property to check for VCAP_APPLICATION --- src/Steeltoe.Common/Platform.cs | 4 +++ test/Steeltoe.Common.Test/PlatformTest.cs | 32 +++++++++++++++++++++++ 2 files changed, 36 insertions(+) create mode 100644 test/Steeltoe.Common.Test/PlatformTest.cs diff --git a/src/Steeltoe.Common/Platform.cs b/src/Steeltoe.Common/Platform.cs index daede63..1059cf6 100644 --- a/src/Steeltoe.Common/Platform.cs +++ b/src/Steeltoe.Common/Platform.cs @@ -12,6 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. +using System; using System.Runtime.InteropServices; namespace Steeltoe.Common @@ -20,9 +21,12 @@ namespace Steeltoe.Common { public const string NET_FRAMEWORK = ".NET Framework"; public const string NET_CORE = ".NET Core"; + public const string VCAP_APPLICATION = "VCAP_APPLICATION"; public static bool IsFullFramework => RuntimeInformation.FrameworkDescription.StartsWith(NET_FRAMEWORK); public static bool IsNetCore => RuntimeInformation.FrameworkDescription.StartsWith(NET_CORE); + + public static bool IsCloudFoundry => Environment.GetEnvironmentVariable(VCAP_APPLICATION) != null; } } diff --git a/test/Steeltoe.Common.Test/PlatformTest.cs b/test/Steeltoe.Common.Test/PlatformTest.cs new file mode 100644 index 0000000..ca4200a --- /dev/null +++ b/test/Steeltoe.Common.Test/PlatformTest.cs @@ -0,0 +1,32 @@ +// Copyright 2017 the original author or authors. +// +// Licensed 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; +using Xunit; + +namespace Steeltoe.Common.Test +{ + public class PlatformTest + { + [Fact] + public void IsCloudFoundry_ReturnsExpected() + { + Assert.False(Platform.IsCloudFoundry); + Environment.SetEnvironmentVariable("VCAP_APPLICATION", "somevalue"); + Assert.True(Platform.IsCloudFoundry); + Environment.SetEnvironmentVariable("VCAP_APPLICATION", null); + Assert.False(Platform.IsCloudFoundry); + } + } +}