Allow token refresh for GCP (#402)
This commit is contained in:
28
tests/KubernetesClient.Tests/GcpTokenProviderTests.cs
Normal file
28
tests/KubernetesClient.Tests/GcpTokenProviderTests.cs
Normal file
@@ -0,0 +1,28 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using FluentAssertions;
|
||||
using k8s.Authentication;
|
||||
using Xunit;
|
||||
|
||||
namespace k8s.Tests
|
||||
{
|
||||
public class GcpTokenProviderTests
|
||||
{
|
||||
[OperatingSystemDependentFact(Exclude = OperatingSystem.OSX)]
|
||||
public async Task GetToken()
|
||||
{
|
||||
var isWindows = Environment.OSVersion.Platform == PlatformID.Win32NT;
|
||||
var cmd = Path.Combine(Directory.GetCurrentDirectory(), "assets", isWindows ? "mock-gcloud.cmd" : "mock-gcloud.sh");
|
||||
if (!isWindows)
|
||||
{
|
||||
System.Diagnostics.Process.Start("chmod", $"+x {cmd}").WaitForExit();
|
||||
}
|
||||
var sut = new GcpTokenProvider(cmd);
|
||||
var result = await sut.GetAuthenticationHeaderAsync(CancellationToken.None);
|
||||
result.Scheme.Should().Be("Bearer");
|
||||
result.Parameter.Should().Be("ACCESS-TOKEN");
|
||||
}
|
||||
}
|
||||
}
|
||||
12
tests/KubernetesClient.Tests/OperatingSystem.cs
Normal file
12
tests/KubernetesClient.Tests/OperatingSystem.cs
Normal file
@@ -0,0 +1,12 @@
|
||||
using System;
|
||||
|
||||
namespace k8s.Tests
|
||||
{
|
||||
[Flags]
|
||||
public enum OperatingSystem
|
||||
{
|
||||
Windows = 1,
|
||||
Linux = 2,
|
||||
OSX = 4
|
||||
}
|
||||
}
|
||||
37
tests/KubernetesClient.Tests/OperatingSystemFact.cs
Normal file
37
tests/KubernetesClient.Tests/OperatingSystemFact.cs
Normal file
@@ -0,0 +1,37 @@
|
||||
using System.Runtime.InteropServices;
|
||||
using Xunit;
|
||||
|
||||
namespace k8s.Tests
|
||||
{
|
||||
public class OperatingSystemDependentFactAttribute : FactAttribute
|
||||
{
|
||||
public OperatingSystem Include { get; set; } = OperatingSystem.Linux | OperatingSystem.Windows | OperatingSystem.OSX;
|
||||
public OperatingSystem Exclude { get; set; }
|
||||
|
||||
public override string Skip
|
||||
{
|
||||
get => IsOS(Include) && !IsOS(Exclude) ? null : "Not compatible with current OS";
|
||||
set { }
|
||||
}
|
||||
|
||||
private bool IsOS(OperatingSystem operatingSystem)
|
||||
{
|
||||
if (operatingSystem.HasFlag(OperatingSystem.Linux) && RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
if (operatingSystem.HasFlag(OperatingSystem.Windows) && RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
if (operatingSystem.HasFlag(OperatingSystem.OSX) && RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
{
|
||||
"configuration": {
|
||||
"active_configuration": "default",
|
||||
"properties": {
|
||||
"compute": {
|
||||
"region": "us-east1",
|
||||
"zone": "us-east1-b"
|
||||
},
|
||||
"core": {
|
||||
"account": "some@account.io",
|
||||
"disable_usage_reporting": "True",
|
||||
"project": "fe-astakhov"
|
||||
}
|
||||
}
|
||||
},
|
||||
"credential": {
|
||||
"access_token": "ACCESS-TOKEN",
|
||||
"token_expiry": "2020-03-20T07:09:20Z"
|
||||
},
|
||||
"sentinels": {
|
||||
"config_sentinel": "C:\\Users\\Andrew\\AppData\\Roaming\\gcloud\\config_sentinel"
|
||||
}
|
||||
}
|
||||
2
tests/KubernetesClient.Tests/assets/mock-gcloud.cmd
Normal file
2
tests/KubernetesClient.Tests/assets/mock-gcloud.cmd
Normal file
@@ -0,0 +1,2 @@
|
||||
@echo off
|
||||
type %~dp0\gcloud-config-helper.json
|
||||
5
tests/KubernetesClient.Tests/assets/mock-gcloud.sh
Normal file
5
tests/KubernetesClient.Tests/assets/mock-gcloud.sh
Normal file
@@ -0,0 +1,5 @@
|
||||
#!/bin/bash
|
||||
SCRIPT=$(readlink -f "$0")
|
||||
SCRIPTPATH=$(dirname "$SCRIPT")
|
||||
OUTPUT_JSON=$SCRIPTPATH/gcloud-config-helper.json
|
||||
cat $OUTPUT_JSON
|
||||
Reference in New Issue
Block a user