Skip to content

Commit

Permalink
Check in CI that the test/ module doesn't use internal APIs
Browse files Browse the repository at this point in the history
An idea mentioned in-passing in microsoft#1073, this would ensure that tests can
be looked at as usage examples, which use of internal APIs prevents.

vendor/ and internal/ are excluded, the former because we only care
about direct dependencies, and the latter so that if tests are done that
depend on an unpublished API, it's possible _and_ explicit.

Signed-off-by: Paul "TBBle" Hampson <[email protected]>
  • Loading branch information
TBBle committed Sep 24, 2021
1 parent 4275e49 commit 0b94625
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 12 deletions.
44 changes: 32 additions & 12 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ env:

jobs:
lint:
runs-on: 'windows-2019'
runs-on: "windows-2019"
steps:
- uses: actions/checkout@v2
- uses: actions/setup-go@v2
with:
go-version: '^1.15.0'
go-version: "^1.15.0"

- name: golangci-lint
uses: golangci/golangci-lint-action@v2
Expand All @@ -23,14 +23,14 @@ jobs:
args: --timeout=5m -v

verify-main-vendor:
runs-on: 'windows-2019'
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'
go-version: "^1.15.0"
- name: Validate main modules
shell: powershell
run: |
Expand All @@ -40,16 +40,16 @@ jobs:
Write-Error "Main modules are not up to date. Please validate your go version >= this job's and run `go mod vendor` followed by `go mod tidy` in the repo root path."
}
exit $process.ExitCode
verify-test-vendor:
runs-on: 'windows-2019'
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'
go-version: "^1.15.0"
- name: Validate test modules
shell: powershell
run: |
Expand All @@ -60,13 +60,33 @@ jobs:
}
exit $process.ExitCode
verify-test-doesnt-use-internal-apis:
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 module doesn't use internal APIs
shell: powershell
run: |
$currentPath = (Get-Location).Path
$failures = & ${currentPath}/test/scripts/Verify-NoInternalImports.ps1 "${currentPath}/test"
if ($failures.Length -gt 0) {
Write-Error "Test module should not depend on github.com/Microsoft/hcsshim/internal/... to function."
Write-Warning $failures
}
exit $failures.Length
test:
runs-on: 'windows-2019'
runs-on: "windows-2019"
steps:
- uses: actions/checkout@v2
- uses: actions/setup-go@v2
with:
go-version: '^1.15.0'
go-version: "^1.15.0"

- run: go test -gcflags=all=-d=checkptr -v ./... -tags admin
- run: go test -gcflags=all=-d=checkptr -v ./internal -tags admin
Expand All @@ -93,12 +113,12 @@ jobs:
test/sample-logging-driver.exe
build:
runs-on: 'windows-2019'
runs-on: "windows-2019"
steps:
- uses: actions/checkout@v2
- uses: actions/setup-go@v2
with:
go-version: '^1.15.0'
go-version: "^1.15.0"

- run: go build ./cmd/containerd-shim-runhcs-v1
- run: go build ./cmd/runhcs
Expand Down Expand Up @@ -135,7 +155,7 @@ jobs:
- name: Install go
uses: actions/setup-go@v2
with:
go-version: '^1.15.0'
go-version: "^1.15.0"

- name: Pull busybox image
run: docker pull busybox
Expand Down
7 changes: 7 additions & 0 deletions test/scripts/Verify-NoInternalImports.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
$FOUND = 0
Get-ChildItem -Path $args[0] -Recurse -Include '*.go' | Where-Object { $_.Directory -NotMatch 'vendor|internal' } | ForEach-Object {
$LISTDATA = (go list -tags functional -json $_.FullName | ConvertFrom-Json)
$INTERNALS = $LISTDATA.Imports.Where({ $_.StartsWith("github.com/Microsoft/hcsshim/internal") }) + $LISTDATA.TestImports.Where({ $_.StartsWith("github.com/Microsoft/hcsshim/internal") })
if ($INTERNALS.Length -gt 0) { Write-Output "$_ contains one or more imports from github.com/Microsoft/hcsshim/internal/..."; $FOUND += 1 }
}
exit $FOUND

0 comments on commit 0b94625

Please sign in to comment.