From 4d93717ae5615a7a8960b833a30fea2077805148 Mon Sep 17 00:00:00 2001 From: CrazyMax Date: Tue, 7 Dec 2021 14:50:16 +0100 Subject: [PATCH] ci: github actions test workflow Signed-off-by: CrazyMax --- .circleci/config.yml | 48 ----- .dockerignore | 1 - .github/workflows/e2e.yml | 6 + .github/workflows/test.yml | 75 ++++++++ .gitignore | 1 - Dockerfile | 38 ++-- Makefile | 11 +- README.md | 7 + appveyor.yml | 23 --- cli/command/cli_test.go | 6 +- cli/command/image/build/context_test.go | 2 + docker-bake.hcl | 10 ++ docker.Makefile | 34 ++-- scripts/make.ps1 | 229 ------------------------ 14 files changed, 161 insertions(+), 330 deletions(-) create mode 100644 .github/workflows/test.yml delete mode 100644 appveyor.yml delete mode 100644 scripts/make.ps1 diff --git a/.circleci/config.yml b/.circleci/config.yml index 1076620916..97c75c684a 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -2,53 +2,6 @@ version: 2 jobs: - test: - working_directory: /work - docker: [{image: 'docker:20.10-git'}] - environment: - DOCKER_BUILDKIT: 1 - steps: - - checkout - - setup_remote_docker: - version: 20.10.6 - reusable: true - exclusive: false - - run: - name: "Docker version" - command: docker version - - run: - name: "Docker info" - command: docker info - - run: - name: "Unit Test with Coverage - build image" - command: | - mkdir -p test-results/unit-tests - docker build --progress=plain -f dockerfiles/Dockerfile.dev --tag cli-builder:$CIRCLE_BUILD_NUM . - - run: - name: "Unit Test with Coverage" - command: | - docker run \ - -e GOTESTSUM_JUNITFILE=/tmp/junit.xml \ - --name \ - test-$CIRCLE_BUILD_NUM cli-builder:$CIRCLE_BUILD_NUM \ - make test-coverage - docker cp \ - test-$CIRCLE_BUILD_NUM:/tmp/junit.xml \ - ./test-results/unit-tests/junit.xml - - run: - name: "Upload to Codecov" - command: | - docker cp \ - test-$CIRCLE_BUILD_NUM:/go/src/github.com/docker/cli/coverage.txt \ - coverage.txt - apk add -U bash curl - curl -s https://codecov.io/bash | bash || \ - echo 'Codecov failed to upload' - - store_test_results: - path: test-results - - store_artifacts: - path: test-results - validate: working_directory: /work docker: [{image: 'docker:20.10-git'}] @@ -82,5 +35,4 @@ workflows: version: 2 ci: jobs: - - test - validate diff --git a/.dockerignore b/.dockerignore index c843af4447..c54474ee93 100644 --- a/.dockerignore +++ b/.dockerignore @@ -3,6 +3,5 @@ /cli/winresources/*.syso /man/man*/ /docs/yaml/gen/ -coverage.txt profile.out /vndr.log diff --git a/.github/workflows/e2e.yml b/.github/workflows/e2e.yml index a43924c65d..0fcfa67af8 100644 --- a/.github/workflows/e2e.yml +++ b/.github/workflows/e2e.yml @@ -52,3 +52,9 @@ jobs: env: BASE_VARIANT: ${{ matrix.base }} E2E_ENGINE_VERSION: ${{ matrix.engine-version }} + TESTFLAGS: -coverprofile=/tmp/coverage/coverage.txt + - + name: Send to Codecov + uses: codecov/codecov-action@v2 + with: + file: ./build/coverage/coverage.txt diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml new file mode 100644 index 0000000000..63bb798491 --- /dev/null +++ b/.github/workflows/test.yml @@ -0,0 +1,75 @@ +name: test + +on: + workflow_dispatch: + push: + branches: + - 'master' + - '[0-9]+.[0-9]{2}' + tags: + - 'v*' + pull_request: + +jobs: + ctn: + runs-on: ubuntu-latest + steps: + - + name: Checkout + uses: actions/checkout@v2 + - + name: Set up Docker Buildx + uses: docker/setup-buildx-action@v1 + - + name: Test + uses: docker/bake-action@v1 + with: + targets: test-coverage + - + name: Send to Codecov + uses: codecov/codecov-action@v2 + with: + file: ./build/coverage/coverage.txt + + host: + runs-on: ${{ matrix.os }} + env: + GOPATH: ${{ github.workspace }} + GOBIN: ${{ github.workspace }}/bin + GO111MODULE: auto + strategy: + fail-fast: false + matrix: + os: + - macos-latest +# - windows-latest # FIXME: some tests are failing on the Windows runner, as well as on Appveyor since June 24, 2018: https://ci.appveyor.com/project/docker/cli/history + steps: + - + name: Prepare git + if: matrix.os == 'windows-latest' + run: | + git config --system core.autocrlf false + git config --system core.eol lf + - + name: Checkout + uses: actions/checkout@v2 + with: + path: ${{ env.GOPATH }}/src/github.com/docker/cli + - + name: Set up Go + uses: actions/setup-go@v2 + with: + go-version: 1.16.11 + - + name: Test + run: | + go test -coverprofile=/tmp/coverage.txt $(go list ./... | grep -vE '/vendor/|/e2e/') + go tool cover -func=/tmp/coverage.txt + working-directory: ${{ env.GOPATH }}/src/github.com/docker/cli + shell: bash + - + name: Send to Codecov + uses: codecov/codecov-action@v2 + with: + file: /tmp/coverage.txt + working-directory: ${{ env.GOPATH }}/src/github.com/docker/cli diff --git a/.gitignore b/.gitignore index 8a2dcbf68e..1bce311c8b 100644 --- a/.gitignore +++ b/.gitignore @@ -14,6 +14,5 @@ Thumbs.db /man/man5/ /man/man8/ /docs/yaml/gen/ -coverage.txt profile.out /vndr.log diff --git a/Dockerfile b/Dockerfile index 1c58600182..5a0b141a97 100644 --- a/Dockerfile +++ b/Dockerfile @@ -4,6 +4,7 @@ ARG BASE_VARIANT=alpine ARG GO_VERSION=1.16.11 ARG XX_VERSION=1.0.0-rc.2 ARG GOVERSIONINFO_VERSION=v1.3.0 +ARG GOTESTSUM_VERSION=v1.7.0 FROM --platform=$BUILDPLATFORM golang:${GO_VERSION}-${BASE_VARIANT} AS gostable FROM --platform=$BUILDPLATFORM golang:1.17rc1-${BASE_VARIANT} AS golatest @@ -37,6 +38,19 @@ FROM build-base-buster AS build-buster ARG TARGETPLATFORM RUN xx-apt install --no-install-recommends -y libc6-dev libgcc-8-dev +FROM build-base-${BASE_VARIANT} AS goversioninfo +ARG GOVERSIONINFO_VERSION +RUN --mount=type=cache,target=/root/.cache/go-build \ + --mount=type=cache,target=/go/pkg/mod \ + GOBIN=/out GO111MODULE=on go install "github.com/josephspurrier/goversioninfo/cmd/goversioninfo@${GOVERSIONINFO_VERSION}" + +FROM build-base-${BASE_VARIANT} AS gotestsum +ARG GOTESTSUM_VERSION +RUN --mount=type=cache,target=/root/.cache/go-build \ + --mount=type=cache,target=/go/pkg/mod \ + GOBIN=/out GO111MODULE=on go install "gotest.tools/gotestsum@${GOTESTSUM_VERSION}" \ + && /out/gotestsum --version + FROM build-${BASE_VARIANT} AS build # GO_LINKMODE defines if static or dynamic binary should be produced ARG GO_LINKMODE=static @@ -50,11 +64,7 @@ ARG CGO_ENABLED ARG VERSION # COMPANY_NAME sets the company that produced the windows binary ARG COMPANY_NAME -# GOVERSIONINFO_VERSION defines goversioninfo tool version -ARG GOVERSIONINFO_VERSION -RUN --mount=type=cache,target=/root/.cache \ - # install goversioninfo tool - GO111MODULE=auto go install github.com/josephspurrier/goversioninfo/cmd/goversioninfo@${GOVERSIONINFO_VERSION} +COPY --from=goversioninfo /out/goversioninfo /usr/bin/goversioninfo RUN --mount=type=bind,target=.,ro \ --mount=type=cache,target=/root/.cache \ --mount=from=dockercore/golang-cross:xx-sdk-extras,target=/xx-sdk,src=/xx-sdk \ @@ -65,6 +75,17 @@ RUN --mount=type=bind,target=.,ro \ TARGET=/out ./scripts/build/binary && \ xx-verify $([ "$GO_LINKMODE" = "static" ] && echo "--static") /out/docker +FROM build-${BASE_VARIANT} AS test +COPY --from=gotestsum /out/gotestsum /usr/bin/gotestsum +ENV GO111MODULE=auto +RUN --mount=type=bind,target=.,rw \ + --mount=type=cache,target=/root/.cache \ + --mount=type=cache,target=/go/pkg/mod \ + gotestsum -- -coverprofile=/tmp/coverage.txt $(go list ./... | grep -vE '/vendor/|/e2e/') + +FROM scratch AS test-coverage +COPY --from=test /tmp/coverage.txt /coverage.txt + FROM build-${BASE_VARIANT} AS build-plugins ARG GO_LINKMODE=static ARG GO_BUILDTAGS @@ -85,13 +106,6 @@ ARG COMPOSE_VERSION=1.29.2 RUN curl -fsSL https://github.com/docker/compose/releases/download/${COMPOSE_VERSION}/docker-compose-$(uname -s)-$(uname -m) -o /usr/local/bin/docker-compose && \ chmod +x /usr/local/bin/docker-compose -FROM build-${BASE_VARIANT} AS gotestsum -ARG GOTESTSUM_VERSION=v1.7.0 -RUN --mount=type=cache,target=/root/.cache/go-build \ - --mount=type=cache,target=/go/pkg/mod \ - GOBIN=/out GO111MODULE=on go install "gotest.tools/gotestsum@${GOTESTSUM_VERSION}" \ - && /out/gotestsum --version - FROM e2e-base-${BASE_VARIANT} AS e2e ARG NOTARY_VERSION=v0.6.1 ADD --chmod=0755 https://github.com/theupdateframework/notary/releases/download/${NOTARY_VERSION}/notary-Linux-amd64 /usr/local/bin/notary diff --git a/Makefile b/Makefile index a41aca8ba7..9e0f85bcb7 100644 --- a/Makefile +++ b/Makefile @@ -13,16 +13,17 @@ _:=$(shell ./scripts/warn-outside-container $(MAKECMDGOALS)) clean: ## remove build artifacts rm -rf ./build/* cli/winresources/rsrc_* ./man/man[1-9] docs/yaml/gen -.PHONY: test-unit -test-unit: ## run unit tests, to change the output format use: GOTESTSUM_FORMAT=(dots|short|standard-quiet|short-verbose|standard-verbose) make test-unit - gotestsum $(TESTFLAGS) -- $${TESTDIRS:-$(shell go list ./... | grep -vE '/vendor/|/e2e/')} - .PHONY: test test: test-unit ## run tests +.PHONY: test-unit +test-unit: ## run unit tests, to change the output format use: GOTESTSUM_FORMAT=(dots|short|standard-quiet|short-verbose|standard-verbose) make test-unit + gotestsum -- $${TESTDIRS:-$(shell go list ./... | grep -vE '/vendor/|/e2e/')} $(TESTFLAGS) + .PHONY: test-coverage test-coverage: ## run test coverage - gotestsum -- -coverprofile=coverage.txt $(shell go list ./... | grep -vE '/vendor/|/e2e/') + mkdir -p $(CURDIR)/build/coverage + gotestsum -- $(shell go list ./... | grep -vE '/vendor/|/e2e/') -coverprofile=$(CURDIR)/build/coverage/coverage.txt .PHONY: lint lint: ## run all the lint tools diff --git a/README.md b/README.md index d04bfd0843..4ce0d6ed98 100644 --- a/README.md +++ b/README.md @@ -2,6 +2,7 @@ [![PkgGoDev](https://img.shields.io/badge/go.dev-docs-007d9c?logo=go&logoColor=white)](https://pkg.go.dev/github.com/docker/cli) [![Build Status](https://img.shields.io/github/workflow/status/docker/cli/build?logo=github)](https://github.com/docker/cli/actions?query=workflow%3Abuild) +[![Test Status](https://img.shields.io/github/workflow/status/docker/cli/test?logo=github)](https://github.com/docker/cli/actions?query=workflow%3Atest) [![CircleCI Status](https://img.shields.io/circleci/build/github/docker/cli/master?logo=circleci)](https://circleci.com/gh/docker/cli/tree/master) [![Go Report Card](https://goreportcard.com/badge/github.com/docker/cli)](https://goreportcard.com/report/github.com/docker/cli) [![Codecov](https://codecov.io/gh/docker/cli/branch/master/graph/badge.svg)](https://codecov.io/gh/docker/cli) @@ -45,6 +46,12 @@ Run all linting: docker buildx bake lint shellcheck ``` +Run test: + +```shell +docker buildx bake test +``` + List all the available targets: ```shell diff --git a/appveyor.yml b/appveyor.yml deleted file mode 100644 index 2313e0d074..0000000000 --- a/appveyor.yml +++ /dev/null @@ -1,23 +0,0 @@ -version: "{build}" - -clone_folder: c:\gopath\src\github.com\docker\cli - -environment: - GOPATH: c:\gopath - GOVERSION: 1.16.11 - DEPVERSION: v0.4.1 - -install: - - rmdir c:\go /s /q - - appveyor DownloadFile https://storage.googleapis.com/golang/go%GOVERSION%.windows-amd64.msi - - msiexec /i go%GOVERSION%.windows-amd64.msi /q - - go version - - go env - -deploy: false - -build_script: - - ps: .\scripts\make.ps1 -Binary - -test_script: - - ps: .\scripts\make.ps1 -TestUnit diff --git a/cli/command/cli_test.go b/cli/command/cli_test.go index d48470ecd8..12f5074a5b 100644 --- a/cli/command/cli_test.go +++ b/cli/command/cli_test.go @@ -39,10 +39,14 @@ func TestNewAPIClientFromFlags(t *testing.T) { func TestNewAPIClientFromFlagsForDefaultSchema(t *testing.T) { host := ":2375" + slug := "tcp://localhost" + if runtime.GOOS == "windows" { + slug = "tcp://127.0.0.1" + } opts := &flags.CommonOptions{Hosts: []string{host}} apiClient, err := NewAPIClientFromFlags(opts, &configfile.ConfigFile{}) assert.NilError(t, err) - assert.Equal(t, apiClient.DaemonHost(), "tcp://localhost"+host) + assert.Equal(t, apiClient.DaemonHost(), slug+host) assert.Equal(t, apiClient.ClientVersion(), api.DefaultVersion) } diff --git a/cli/command/image/build/context_test.go b/cli/command/image/build/context_test.go index f2169781e0..01c28c1dec 100644 --- a/cli/command/image/build/context_test.go +++ b/cli/command/image/build/context_test.go @@ -245,6 +245,8 @@ func TestValidateContextDirectoryWithOneFileExcludes(t *testing.T) { func createTestTempDir(t *testing.T, prefix string) (string, func()) { path, err := ioutil.TempDir("", prefix) assert.NilError(t, err) + path, err = filepath.EvalSymlinks(path) + assert.NilError(t, err) return path, func() { assert.NilError(t, os.RemoveAll(path)) } } diff --git a/docker-bake.hcl b/docker-bake.hcl index 38ea5300de..8b2b66b349 100644 --- a/docker-bake.hcl +++ b/docker-bake.hcl @@ -78,6 +78,16 @@ target "shellcheck" { output = ["type=cacheonly"] } +target "test" { + target = "test" + output = ["type=cacheonly"] +} + +target "test-coverage" { + target = "test-coverage" + output = ["build/coverage"] +} + target "e2e-image" { target = "e2e" output = ["type=docker"] diff --git a/docker.Makefile b/docker.Makefile index 8af66bf80e..cdb217a0a0 100644 --- a/docker.Makefile +++ b/docker.Makefile @@ -51,13 +51,6 @@ clean: build_docker_image ## clean build artifacts $(DOCKER_RUN) $(DEV_DOCKER_IMAGE_NAME) make clean docker volume rm -f $(CACHE_VOLUME_NAME) -.PHONY: test-unit -test-unit: build_docker_image ## run unit tests (using go test) - $(DOCKER_RUN) $(DEV_DOCKER_IMAGE_NAME) make test-unit - -.PHONY: test ## run unit and e2e tests -test: test-unit test-e2e - .PHONY: cross cross: COMPANY_NAME=$(COMPANY_NAME) docker buildx bake cross @@ -102,8 +95,20 @@ manpages: build_docker_image ## generate man pages from go source and markdown yamldocs: build_docker_image ## generate documentation YAML files consumed by docs repo $(DOCKER_RUN) -it $(DEV_DOCKER_IMAGE_NAME) make yamldocs +.PHONY: test ## run unit and e2e tests +test: test-unit test-e2e + +.PHONY: test-unit +test-unit: ## run unit tests + docker buildx bake test + +.PHONY: test-coverage +test-coverage: ## run test with coverage + docker buildx bake test-coverage + .PHONY: build-e2e-image build-e2e-image: + mkdir -p $(CURDIR)/build/coverage IMAGE_NAME=$(E2E_IMAGE_NAME) VERSION=$(VERSION) docker buildx bake e2e-image .PHONY: test-e2e @@ -111,15 +116,24 @@ test-e2e: test-e2e-non-experimental test-e2e-experimental test-e2e-connhelper-ss .PHONY: test-e2e-experimental test-e2e-experimental: build-e2e-image # run experimental e2e tests - docker run --rm --mount type=bind,src=/var/run/docker.sock,dst=/var/run/docker.sock $(ENVVARS) -e DOCKERD_EXPERIMENTAL=1 -e TEST_ENGINE_VERSION=$(E2E_ENGINE_VERSION) $(E2E_IMAGE_NAME) + docker run --rm $(ENVVARS) -e DOCKERD_EXPERIMENTAL=1 -e TEST_ENGINE_VERSION=$(E2E_ENGINE_VERSION) \ + --mount type=bind,src=$(CURDIR)/build/coverage,dst=/tmp/coverage \ + --mount type=bind,src=/var/run/docker.sock,dst=/var/run/docker.sock \ + $(E2E_IMAGE_NAME) .PHONY: test-e2e-non-experimental test-e2e-non-experimental: build-e2e-image # run non-experimental e2e tests - docker run --rm --mount type=bind,src=/var/run/docker.sock,dst=/var/run/docker.sock $(ENVVARS) -e TEST_ENGINE_VERSION=$(E2E_ENGINE_VERSION) $(E2E_IMAGE_NAME) + docker run --rm $(ENVVARS) -e TEST_ENGINE_VERSION=$(E2E_ENGINE_VERSION) \ + --mount type=bind,src=$(CURDIR)/build/coverage,dst=/tmp/coverage \ + --mount type=bind,src=/var/run/docker.sock,dst=/var/run/docker.sock \ + $(E2E_IMAGE_NAME) .PHONY: test-e2e-connhelper-ssh test-e2e-connhelper-ssh: build-e2e-image # run experimental SSH-connection helper e2e tests - docker run --rm --mount type=bind,src=/var/run/docker.sock,dst=/var/run/docker.sock $(ENVVARS) -e DOCKERD_EXPERIMENTAL=1 -e TEST_ENGINE_VERSION=$(E2E_ENGINE_VERSION) -e TEST_CONNHELPER=ssh $(E2E_IMAGE_NAME) + docker run --rm $(ENVVARS) -e DOCKERD_EXPERIMENTAL=1 -e TEST_ENGINE_VERSION=$(E2E_ENGINE_VERSION) -e TEST_CONNHELPER=ssh \ + --mount type=bind,src=$(CURDIR)/build/coverage,dst=/tmp/coverage \ + --mount type=bind,src=/var/run/docker.sock,dst=/var/run/docker.sock \ + $(E2E_IMAGE_NAME) .PHONY: help help: ## print this help diff --git a/scripts/make.ps1 b/scripts/make.ps1 deleted file mode 100644 index 7dd3fafb4d..0000000000 --- a/scripts/make.ps1 +++ /dev/null @@ -1,229 +0,0 @@ -<# -.NOTES - Summary: Windows native build script. - - It does however provided the minimum necessary to support parts of local Windows - development and Windows to Windows CI. - - Usage Examples (run from repo root): - "scripts/make.ps1 -Client" to build docker.exe client 64-bit binary (remote repo) - "scripts/make.ps1 -TestUnit" to run unit tests - "scripts/make.ps1 -Daemon -TestUnit" to build the daemon and run unit tests - "scripts/make.ps1 -All" to run everything this script knows about that can run in a container - "scripts/make.ps1" to build the daemon binary (same as -Daemon) - "scripts/make.ps1 -Binary" shortcut to -Client and -Daemon - -.PARAMETER Binary - Builds the client and daemon binaries. A convenient shortcut to `make.ps1 -Client -Daemon`. - -.PARAMETER Race - Use -race in go build and go test. - -.PARAMETER Noisy - Use -v in go build. - -.PARAMETER ForceBuildAll - Use -a in go build. - -.PARAMETER NoOpt - Use -gcflags -N -l in go build to disable optimisation (can aide debugging). - -.PARAMETER CommitSuffix - Adds a custom string to be appended to the commit ID (spaces are stripped). - -.PARAMETER TestUnit - Runs unit tests. - -.PARAMETER All - Runs everything this script knows about that can run in a container. - - -TODO -- Unify the head commit -- Add golint and other checks (swagger maybe?) - -#> - - -param( - [Parameter(Mandatory=$False)][switch]$Binary, - [Parameter(Mandatory=$False)][switch]$Race, - [Parameter(Mandatory=$False)][switch]$Noisy, - [Parameter(Mandatory=$False)][switch]$ForceBuildAll, - [Parameter(Mandatory=$False)][switch]$NoOpt, - [Parameter(Mandatory=$False)][string]$CommitSuffix, - [Parameter(Mandatory=$False)][switch]$TestUnit, - [Parameter(Mandatory=$False)][switch]$All -) - -$ErrorActionPreference = "Stop" -$ProgressPreference = "SilentlyContinue" -$pushed=$False # To restore the directory if we have temporarily pushed to one. - -# Utility function to get the commit ID of the repository -Function Get-GitCommit() { - if (-not (Test-Path ".\.git")) { - # If we don't have a .git directory, but we do have the environment - # variable DOCKER_GITCOMMIT set, that can override it. - if ($env:DOCKER_GITCOMMIT.Length -eq 0) { - Throw ".git directory missing and DOCKER_GITCOMMIT environment variable not specified." - } - Write-Host "INFO: Git commit ($env:DOCKER_GITCOMMIT) assumed from DOCKER_GITCOMMIT environment variable" - return $env:DOCKER_GITCOMMIT - } - $gitCommit=$(git rev-parse --short HEAD) - if ($(git status --porcelain --untracked-files=no).Length -ne 0) { - $gitCommit="$gitCommit-unsupported" - Write-Host "" - Write-Warning "This version is unsupported because there are uncommitted file(s)." - Write-Warning "Either commit these changes, or add them to .gitignore." - git status --porcelain --untracked-files=no | Write-Warning - Write-Host "" - } - return $gitCommit -} - -# Build a binary (client or daemon) -Function Execute-Build($additionalBuildTags, $directory) { - # Generate the build flags - $buildTags = "autogen" - if ($Noisy) { $verboseParm=" -v" } - if ($Race) { Write-Warning "Using race detector"; $raceParm=" -race"} - if ($ForceBuildAll) { $allParm=" -a" } - if ($NoOpt) { $optParm=" -gcflags "+""""+"-N -l"+"""" } - if ($additionalBuildTags -ne "") { $buildTags += $(" " + $additionalBuildTags) } - - - # Get the git commit. This will also verify if we are in a repo or not. Then add a custom string if supplied. - $gitCommit=Get-GitCommit - if ($CommitSuffix -ne "") { $gitCommit += "-"+$CommitSuffix -Replace ' ', '' } - if (Test-Path Env:\DOCKER_GITCOMMIT) {$gitCommit=$env:DOCKER_GITCOMMIT} - - # Get the version of docker (eg 17.04.0-dev) - $dockerVersion="0.0.0-dev" - if (Test-Path Env:\VERSION) {$dockerVersion=$env:VERSION} - - # Do the go build in the appropriate directory - # Note -linkmode=internal is required to be able to debug on Windows. - # https://github.com/golang/go/issues/14319#issuecomment-189576638 - Write-Host "INFO: Building..." - - $buildTime=$(Get-Date).ToUniversalTime() - $env:LDFLAGS="-linkmode=internal ` - -X \""github.com/docker/cli/cli/version.Version=$dockerVersion\"" ` - -X \""github.com/docker/cli/cli/version.GitCommit=$gitCommit\"" ` - -X \""github.com/docker/cli/cli/version.BuildTime=$buildTime\""" - if ($env:PLATFORM) { - $env:LDFLAGS="$env:LDFLAGS -X \""github.com/docker/cli/cli/version.PlatformName=$env:PLATFORM\""" - } - - # Generate a version in the form major,minor,patch,build - $versionQuad=$dockerVersion -replace "[^0-9.]*" -replace "\.", "," - - # If you really want to understand this madness below, search the Internet for powershell variables after verbatim arguments... Needed to get double-quotes passed through to the compiler options. - # Generate the .syso files containing all the resources and manifest needed to compile the final docker binaries. Both 32 and 64-bit clients. - $env:_ag_dockerVersion=$dockerVersion - $env:_ag_gitCommit=$gitCommit - - New-Item -ItemType Directory -Path .\tmp -Force | Out-Null - windres -i scripts/winresources/docker.rc -o cli/winresources/rsrc_amd64.syso -F pe-x86-64 --use-temp-file -I ./tmp -D DOCKER_VERSION_QUAD=$versionQuad --% -D DOCKER_VERSION=\"%_ag_dockerVersion%\" -D DOCKER_COMMIT=\"%_ag_gitCommit%\" - if ($LASTEXITCODE -ne 0) { Throw "Failed to compile client 64-bit resources" } - - windres -i scripts/winresources/docker.rc -o cli/winresources/rsrc_386.syso -F pe-i386 --use-temp-file -I ./tmp -D DOCKER_VERSION_QUAD=$versionQuad --% -D DOCKER_VERSION=\"%_ag_dockerVersion%\" -D DOCKER_COMMIT=\"%_ag_gitCommit%\" - if ($LASTEXITCODE -ne 0) { Throw "Failed to compile client 32-bit resources" } - Remove-Item .\tmp -Recurse -Force -ErrorAction SilentlyContinue | Out-Null - - Push-Location $root\cmd\$directory; $global:pushed=$True - - # By using --% we can use \"key=%foo%\" and have a environment variable foo that contains spaces - go build $raceParm $verboseParm $allParm $optParm -tags "$buildTags" ` - -o "$root\build\$directory.exe" ` - -ldflags --% "%LDFLAGS%" - - if ($LASTEXITCODE -ne 0) { Throw "Failed to compile" } - Pop-Location; $global:pushed=$False -} - -# Run the unit tests -Function Run-UnitTests() { - Write-Host "INFO: Running unit tests..." - $testPath="./..." - $goListCommand = "go list -e -f '{{if ne .Name """ + '\"github.com/docker/cli\"' + """}}{{.ImportPath}}{{end}}' $testPath" - $pkgList = $(Invoke-Expression $goListCommand) - if ($LASTEXITCODE -ne 0) { Throw "go list for unit tests failed" } - $pkgList = $pkgList | Select-String -Pattern "github.com/docker/cli" - $pkgList = $pkgList | Select-String -NotMatch "github.com/docker/cli/vendor" - $pkgList = $pkgList | Select-String -NotMatch "github.com/docker/cli/man" - $pkgList = $pkgList | Select-String -NotMatch "github.com/docker/cli/e2e" - $pkgList = $pkgList -replace "`r`n", " " - $goTestCommand = "go test" + $raceParm + " -cover -ldflags -w -tags """ + "autogen" + """ -a """ + "-test.timeout=10m" + """ $pkgList" - Invoke-Expression $goTestCommand - if ($LASTEXITCODE -ne 0) { Throw "Unit tests failed" } -} - -# Start of main code. -Try { - Write-Host -ForegroundColor Cyan "INFO: make.ps1 starting at $(Get-Date)" - - # Get to the root of the repo - $root = $(Split-Path $MyInvocation.MyCommand.Definition -Parent | Split-Path -Parent) - Push-Location $root - - # Handle the "-All" shortcut to turn on all things we can handle. - # Note we expressly only include the items which can run in a container - the validations tests cannot - # as they require the .git directory which is excluded from the image by .dockerignore - if ($All) { $Client=$True; $TestUnit=$True } - - # Handle the "-Binary" shortcut to build both client and daemon. - if ($Binary) { $Client = $True; } - - # Verify git is installed - if ($(Get-Command git -ErrorAction SilentlyContinue) -eq $nil) { Throw "Git does not appear to be installed" } - - # Verify go is installed - if ($(Get-Command go -ErrorAction SilentlyContinue) -eq $nil) { Throw "GoLang does not appear to be installed" } - - # Build the binaries - if ($Client) { - # Create the build directory if it doesn't exist - if (-not (Test-Path ".\build")) { New-Item ".\build" -ItemType Directory | Out-Null } - - # Perform the actual build - Execute-Build "" "docker" - } - - # Run unit tests - if ($TestUnit) { Run-UnitTests } - - # Gratuitous ASCII art. - if ($Client) { - Write-Host - Write-Host -ForegroundColor Green " ________ ____ __." - Write-Host -ForegroundColor Green " \_____ \ `| `|/ _`|" - Write-Host -ForegroundColor Green " / `| \`| `<" - Write-Host -ForegroundColor Green " / `| \ `| \" - Write-Host -ForegroundColor Green " \_______ /____`|__ \" - Write-Host -ForegroundColor Green " \/ \/" - Write-Host - } -} -Catch [Exception] { - Write-Host -ForegroundColor Red ("`nERROR: make.ps1 failed:`n$_") - - # More gratuitous ASCII art. - Write-Host - Write-Host -ForegroundColor Red "___________ .__.__ .___" - Write-Host -ForegroundColor Red "\_ _____/____ `|__`| `| ____ __`| _/" - Write-Host -ForegroundColor Red " `| __) \__ \ `| `| `| _/ __ \ / __ `| " - Write-Host -ForegroundColor Red " `| \ / __ \`| `| `|_\ ___// /_/ `| " - Write-Host -ForegroundColor Red " \___ / (____ /__`|____/\___ `>____ `| " - Write-Host -ForegroundColor Red " \/ \/ \/ \/ " - Write-Host - - Throw $_ -} -Finally { - Pop-Location # As we pushed to the root of the repo as the very first thing - if ($global:pushed) { Pop-Location } - Write-Host -ForegroundColor Cyan "INFO: make.ps1 ended at $(Get-Date)" -}