|
| 1 | +# Copyright (c) Microsoft Corporation. |
| 2 | +# Licensed under the MIT License. |
| 3 | + |
| 4 | +Import-Module "$PSScriptRoot\..\PSGetTestUtils.psm1" -Force |
| 5 | + |
| 6 | +Describe 'Container registry config blob' -Tags 'CI' { |
| 7 | + BeforeAll { |
| 8 | + Add-Type -AssemblyName System.Net.Http |
| 9 | + $references = @( |
| 10 | + [System.Net.Http.HttpClient].Assembly.Location |
| 11 | + [System.Net.HttpStatusCode].Assembly.Location |
| 12 | + ) |
| 13 | + Add-Type -ReferencedAssemblies $references -TypeDefinition @' |
| 14 | +using System; |
| 15 | +using System.Net; |
| 16 | +using System.Net.Http; |
| 17 | +using System.Threading; |
| 18 | +using System.Threading.Tasks; |
| 19 | +
|
| 20 | +public class ConfigBlobUploadHandler : HttpMessageHandler |
| 21 | +{ |
| 22 | + public byte[] UploadedContent { get; private set; } |
| 23 | + public byte[] UploadedManifest { get; private set; } |
| 24 | + public Uri UploadUri { get; private set; } |
| 25 | + public HttpStatusCode UploadStatusCode = HttpStatusCode.Created; |
| 26 | +
|
| 27 | + protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) |
| 28 | + { |
| 29 | + if (request.Method == HttpMethod.Post) |
| 30 | + { |
| 31 | + var response = new HttpResponseMessage(HttpStatusCode.Accepted); |
| 32 | + response.Headers.Location = new Uri("/v2/test/blobs/uploads/id?upload=1", UriKind.Relative); |
| 33 | + return response; |
| 34 | + } |
| 35 | +
|
| 36 | + var content = await request.Content.ReadAsByteArrayAsync(); |
| 37 | + if (request.RequestUri.AbsolutePath.Contains("/manifests/")) |
| 38 | + { |
| 39 | + UploadedManifest = content; |
| 40 | + } |
| 41 | + else |
| 42 | + { |
| 43 | + UploadedContent = content; |
| 44 | + UploadUri = request.RequestUri; |
| 45 | + } |
| 46 | +
|
| 47 | + return new HttpResponseMessage(UploadStatusCode); |
| 48 | + } |
| 49 | +} |
| 50 | +'@ |
| 51 | + |
| 52 | + function Invoke-ContainerRegistryMethod { |
| 53 | + [CmdletBinding()] |
| 54 | + param($MethodName, [object[]] $MethodArguments, $Handler) |
| 55 | + |
| 56 | + $type = [Microsoft.PowerShell.PSResourceGet.Cmdlets.PublishPSResource].Assembly.GetType('Microsoft.PowerShell.PSResourceGet.ContainerRegistryServerAPICalls') |
| 57 | + $repository = [Microsoft.PowerShell.PSResourceGet.UtilClasses.PSRepositoryInfo]::new( |
| 58 | + 'Test', [uri]'https://registry.example', 50, $true, $null, 'None', 'ContainerRegistry', $true) |
| 59 | + $api = [Activator]::CreateInstance($type, @($repository, $PSCmdlet, $null, 'test')) |
| 60 | + $flags = [Reflection.BindingFlags]'Instance,NonPublic' |
| 61 | + $clientProperty = $type.GetProperty('_sessionClient', $flags) |
| 62 | + try { |
| 63 | + if ($Handler) { |
| 64 | + $clientProperty.GetValue($api).Dispose() |
| 65 | + $clientProperty.SetValue($api, [System.Net.Http.HttpClient]::new($Handler)) |
| 66 | + $type.GetField('_cachedContainterRegistryToken', $flags).SetValue($api, 'test-token') |
| 67 | + } |
| 68 | + |
| 69 | + for ($i = 0; $i -lt $MethodArguments.Length; $i++) { |
| 70 | + if ($null -ne $MethodArguments[$i]) { |
| 71 | + $MethodArguments[$i] = $MethodArguments[$i].PSObject.BaseObject |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + $type.GetMethod($MethodName, $flags).Invoke($api, $MethodArguments) |
| 76 | + } |
| 77 | + finally { |
| 78 | + $clientProperty.GetValue($api).Dispose() |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + BeforeEach { |
| 84 | + $configPath = Join-Path $TestDrive "$([guid]::NewGuid()).json" |
| 85 | + $configArguments = @($configPath, $null) |
| 86 | + $configDigest = Invoke-ContainerRegistryMethod 'CreateConfigFile' $configArguments |
| 87 | + } |
| 88 | + |
| 89 | + It 'Creates a config containing exactly an empty JSON object and hashes those bytes' { |
| 90 | + $configArguments[1] | Should -BeNullOrEmpty |
| 91 | + $content = [IO.File]::ReadAllBytes($configPath) |
| 92 | + $content.Length | Should -Be 2 |
| 93 | + [Text.Encoding]::UTF8.GetString($content) | Should -BeExactly '{}' |
| 94 | + $configDigest | Should -BeExactly (Get-FileHash $configPath -Algorithm SHA256).Hash.ToLowerInvariant() |
| 95 | + } |
| 96 | + |
| 97 | + It 'Describes the config bytes in the manifest without changing the package layer' { |
| 98 | + $manifestArguments = @( |
| 99 | + 'packageDigest', $configDigest, [long]123, 'test.nupkg', 'test', |
| 100 | + [Microsoft.PowerShell.PSResourceGet.UtilClasses.ResourceType]::Module, '{}') |
| 101 | + $manifest = Invoke-ContainerRegistryMethod 'CreateManifestContent' $manifestArguments | ConvertFrom-Json |
| 102 | + |
| 103 | + $manifest.config.mediaType | Should -BeExactly 'application/vnd.oci.image.config.v1+json' |
| 104 | + $manifest.config.digest | Should -BeExactly "sha256:$configDigest" |
| 105 | + $manifest.config.size | Should -Be (Get-Item $configPath).Length |
| 106 | + $manifest.layers.Count | Should -Be 1 |
| 107 | + $manifest.layers[0].digest | Should -BeExactly 'sha256:packageDigest' |
| 108 | + $manifest.layers[0].size | Should -Be 123 |
| 109 | + } |
| 110 | + |
| 111 | + It 'Uploads the config JSON with its matching digest' { |
| 112 | + $handler = [ConfigBlobUploadHandler]::new() |
| 113 | + $uploadArguments = @($configPath, $configDigest, 'test', 'test-token', $null) |
| 114 | + Invoke-ContainerRegistryMethod 'UploadConfigFile' $uploadArguments $handler |
| 115 | + |
| 116 | + $uploadArguments[4] | Should -BeNullOrEmpty |
| 117 | + $handler.UploadedContent.Length | Should -Be 2 |
| 118 | + [Text.Encoding]::UTF8.GetString($handler.UploadedContent) | Should -BeExactly '{}' |
| 119 | + $handler.UploadUri.Query | Should -Match "digest=sha256:$configDigest" |
| 120 | + } |
| 121 | + |
| 122 | + It 'Reports a failed config upload' { |
| 123 | + $handler = [ConfigBlobUploadHandler]::new() |
| 124 | + $handler.UploadStatusCode = [System.Net.HttpStatusCode]::BadRequest |
| 125 | + $uploadArguments = @($configPath, $configDigest, 'test', 'test-token', $null) |
| 126 | + Invoke-ContainerRegistryMethod 'UploadConfigFile' $uploadArguments $handler |
| 127 | + |
| 128 | + $uploadArguments[4].FullyQualifiedErrorId | Should -BeExactly 'UploadConfigFileError' |
| 129 | + } |
| 130 | + |
| 131 | + It 'Publishes matching config bytes and manifest when the module already contains config.json' { |
| 132 | + $outputDirectory = Join-Path $TestDrive 'nupkg' |
| 133 | + $null = New-Item $outputDirectory -ItemType Directory |
| 134 | + $existingConfigPath = Join-Path $outputDirectory 'config.json' |
| 135 | + $existingConfigContent = '{"module":true}' |
| 136 | + [IO.File]::WriteAllText($existingConfigPath, $existingConfigContent) |
| 137 | + [IO.File]::WriteAllText((Join-Path $outputDirectory 'test.1.0.0.nupkg'), 'package') |
| 138 | + |
| 139 | + $handler = [ConfigBlobUploadHandler]::new() |
| 140 | + $type = [Microsoft.PowerShell.PSResourceGet.Cmdlets.PublishPSResource].Assembly.GetType('Microsoft.PowerShell.PSResourceGet.ContainerRegistryServerAPICalls') |
| 141 | + $versionType = $type.GetMethod('PushNupkgContainerRegistry', [Reflection.BindingFlags]'Instance,NonPublic').GetParameters()[3].ParameterType |
| 142 | + $version = [Activator]::CreateInstance($versionType, @('1.0.0')) |
| 143 | + $publishArguments = @( |
| 144 | + $outputDirectory, 'test', $null, $version, |
| 145 | + [Microsoft.PowerShell.PSResourceGet.UtilClasses.ResourceType]::Module, |
| 146 | + @{ ModuleVersion = '1.0.0' }, @{}, $false, $null, $null) |
| 147 | + Push-Location $TestDrive |
| 148 | + try { |
| 149 | + $published = Invoke-ContainerRegistryMethod 'PushNupkgContainerRegistry' $publishArguments $handler |
| 150 | + } |
| 151 | + finally { |
| 152 | + Pop-Location |
| 153 | + } |
| 154 | + |
| 155 | + $published | Should -Be $true |
| 156 | + $publishArguments[9] | Should -BeNullOrEmpty |
| 157 | + [Text.Encoding]::UTF8.GetString($handler.UploadedContent) | Should -BeExactly '{}' |
| 158 | + $handler.UploadUri.Query | Should -Match "digest=sha256:$configDigest" |
| 159 | + $manifest = [Text.Encoding]::UTF8.GetString($handler.UploadedManifest) | ConvertFrom-Json |
| 160 | + $manifest.config.digest | Should -BeExactly "sha256:$configDigest" |
| 161 | + $manifest.config.size | Should -Be $handler.UploadedContent.Length |
| 162 | + [IO.File]::ReadAllText($existingConfigPath) | Should -BeExactly $existingConfigContent |
| 163 | + } |
| 164 | +} |
0 commit comments