From f53295d5261bf1dcccdc5ed064a16187cfdad2ab Mon Sep 17 00:00:00 2001 From: Kathryn Baldauf Date: Thu, 12 Aug 2021 10:02:30 -0700 Subject: [PATCH] Add ci step to validate that modules have been vendored in Signed-off-by: Kathryn Baldauf --- .github/workflows/ci.yml | 40 ++++++++++++++++++++++- scripts/Verify-GoModules.ps1 | 63 ++++++++++++++++++++++++++++++++++++ 2 files changed, 102 insertions(+), 1 deletion(-) create mode 100644 scripts/Verify-GoModules.ps1 diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 1e9794b716..4b37fc6c72 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -4,8 +4,8 @@ on: - pull_request env: - GOPROXY: off GOFLAGS: -mod=vendor + GOPROXY: off jobs: lint: @@ -22,6 +22,44 @@ jobs: version: v1.42.0 # Has fixes for stylecheck configuration https://github.com/golangci/golangci-lint/pull/2017/files args: --timeout=5m -v + verify-main-vendor: + runs-on: 'windows-2019' + env: + GOPROXY: "https://proxy.golang.org,direct" + steps: + - uses: actions/checkout@v2 + - uses: actions/setup-go@v2 + with: + go-version: '^1.15.0' + - name: Validate main modules + shell: powershell + run: | + $currentPath = (Get-Location).Path + $process = Start-Process powershell.exe -PassThru -Verb runAs -Wait -ArgumentList $currentPath/scripts/Verify-GoModules.ps1, $currentPath + if ($process.ExitCode -ne 0) { + Write-Error "Main modules are not up to date. Please run `go mod vendor` followed by `go mod tidy` in the repo root path." + } + exit $process.ExitCode + + verify-test-vendor: + runs-on: 'windows-2019' + env: + GOPROXY: "https://proxy.golang.org,direct" + steps: + - uses: actions/checkout@v2 + - uses: actions/setup-go@v2 + with: + go-version: '^1.15.0' + - name: Validate test modules + shell: powershell + run: | + $currentPath = (Get-Location).Path + $process = Start-Process powershell.exe -PassThru -Verb runAs -Wait -ArgumentList $currentPath/scripts/Verify-GoModules.ps1, $currentPath, "test" + if ($process.ExitCode -ne 0) { + Write-Error "Test package modules are not up to date. Please run `go mod vendor` followed by `go mod tidy` in hcsshim/test directory." + } + exit $process.ExitCode + test: runs-on: 'windows-2019' steps: diff --git a/scripts/Verify-GoModules.ps1 b/scripts/Verify-GoModules.ps1 new file mode 100644 index 0000000000..0be06e8506 --- /dev/null +++ b/scripts/Verify-GoModules.ps1 @@ -0,0 +1,63 @@ +function newTemporaryDirectory { + $parent = [System.IO.Path]::GetTempPath() + [string] $name = [System.Guid]::NewGuid() + New-Item -ItemType Directory -Path (Join-Path $parent $name) +} + +function updateVendor { + param ( + [string]$path + ) + $currentPath = (Get-Location).Path + Set-Location $path + go mod vendor + go mod tidy + Set-Location $currentPath +} + +function matchingHashes { + param ( + [string]$rootPath, + [string]$tempPath + ) + $rootHashes = Get-ChildItem -Recurse -Path $rootPath | foreach {Get-FileHash -Path $_.FullName -Algorithm SHA256} + if (-not $?) { + return $false + } + $tempHashes = Get-ChildItem -Recurse -Path $tempPath | foreach {Get-FileHash -Path $_.FullName -Algorithm SHA256} + if (-not $?) { + return $false + } + $diff = Compare-Object -ReferenceObject $rootHashes.Hash -DifferenceObject $tempHashes.Hash + if ($diff.Count -eq 0) { + return $true + } + return $false +} + +$rootPath = $args[0] +$subdir = $args[1] + +$tempDir = newTemporaryDirectory +Copy-Item -Path $rootPath/* -Destination $tempDir -Recurse +if (-not $?) { + Remove-Item $tempDir -Recurse + exit 1 +} + +updateVendor $tempDir/$subdir +if (-not $?) { + Remove-Item $tempDir -Recurse + exit 1 +} + +$hashesMatch = matchingHashes $rootPath/$subdir/vendor $tempDir/$subdir/vendor +if (-not $?) { + Remove-Item $tempDir -Recurse + exit 1 +} + +Remove-Item $tempDir -Recurse +if ($hashesMatch -ne $true) { + exit 1 +}