From ba7b39c52f6022e61f0eb1be94a9a23ea4647fe7 Mon Sep 17 00:00:00 2001 From: Alex Goodman Date: Sat, 9 Nov 2019 15:11:58 -0500 Subject: [PATCH 1/3] add package install tests + basic execution of dive --- .data/.dive-ci | 2 +- .github/workflows/pipeline.yml | 76 ++++++++++++++++++++++++++++------ Makefile | 51 +++++++++++++++++++++++ runtime/run.go | 6 +-- runtime/run_test.go | 12 +++--- 5 files changed, 124 insertions(+), 23 deletions(-) diff --git a/.data/.dive-ci b/.data/.dive-ci index 27b426fb..7b3545eb 100644 --- a/.data/.dive-ci +++ b/.data/.dive-ci @@ -10,6 +10,6 @@ rules: highestWastedBytes: 20Mb # If the amount of wasted space makes up for X% of the image, mark as failed. (fail if the threshold is met or crossed; expressed as a percentage between 0-1) - highestUserWastedPercent: 0.10 + highestUserWastedPercent: 0.5 plugin1/rule1: error diff --git a/.github/workflows/pipeline.yml b/.github/workflows/pipeline.yml index 373b3036..b052e15a 100644 --- a/.github/workflows/pipeline.yml +++ b/.github/workflows/pipeline.yml @@ -1,5 +1,5 @@ name: 'app-pipeline' -on: [ push, pull_request ] +on: [ push ] env: DOCKER_CLI_VERSION: "19.03.1" jobs: @@ -26,9 +26,8 @@ jobs: uses: actions/cache@v1 with: path: ~/go/pkg/mod - key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }} - restore-keys: | - ${{ runner.os }}-go- + key: ${{ runner.os }}-go-${{ matrix.go-version }}-${{ hashFiles('**/go.sum') }} + restore-keys: ${{ runner.os }}-go-${{ matrix.go-version }}- - name: Install go dependencies if: steps.unit-cache-go-dependencies.outputs.cache-hit != 'true' @@ -54,9 +53,8 @@ jobs: uses: actions/cache@v1 with: path: ~/go/pkg/mod - key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }} - restore-keys: | - ${{ runner.os }}-go- + key: ${{ runner.os }}-go-prod-${{ hashFiles('**/go.sum') }} + restore-keys: ${{ runner.os }}-go-prod- - name: Install dependencies if: steps.package-cache-go-dependencies.outputs.cache-hit != 'true' @@ -70,8 +68,6 @@ jobs: with: version: latest args: release --snapshot --skip-publish --rm-dist - env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - run: docker images wagoodman/dive @@ -84,8 +80,63 @@ jobs: name: artifacts path: dist + + test-linux-artifacts: + needs: [ build-artifacts ] + runs-on: ubuntu-latest + steps: + + - uses: actions/checkout@master + + - uses: actions/download-artifact@master + with: + name: artifacts + path: dist + + - name: Test linux run + run: make ci-test-linux-run + + - name: Test DEB package installation + run: make ci-test-deb-package-install + + - name: Test RPM package installation + run: make ci-test-rpm-package-install + + + test-mac-artifacts: + needs: [ build-artifacts ] + runs-on: macos-latest + steps: + + - uses: actions/checkout@master + + - uses: actions/download-artifact@master + with: + name: artifacts + path: dist + + - name: Test darwin run + run: make ci-test-mac-run + + + test-windows-artifacts: + needs: [ build-artifacts ] + runs-on: windows-latest + steps: + + - uses: actions/checkout@master + + - uses: actions/download-artifact@master + with: + name: artifacts + path: dist + + - name: Test windows run + run: make ci-test-windows-run + + release: - needs: [ build-artifacts, unit-test ] + needs: [ unit-test, build-artifacts, test-linux-artifacts, test-mac-artifacts, test-windows-artifacts ] runs-on: ubuntu-latest if: startsWith(github.ref, 'refs/tags/v') steps: @@ -101,9 +152,8 @@ jobs: uses: actions/cache@v1 with: path: ~/go/pkg/mod - key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }} - restore-keys: | - ${{ runner.os }}-go- + key: ${{ runner.os }}-go-prod-${{ hashFiles('**/go.sum') }} + restore-keys: ${{ runner.os }}-go-prod- - name: Install dependencies if: steps.release-cache-go-dependencies.outputs.cache-hit != 'true' diff --git a/Makefile b/Makefile index 0ace2e15..5dcda3bc 100644 --- a/Makefile +++ b/Makefile @@ -40,6 +40,54 @@ ci-test-production-image: '${TEST_IMAGE}' \ --ci +ci-test-deb-package-install: + docker run \ + -v //var/run/docker.sock://var/run/docker.sock \ + -v /${PWD}://src \ + -w //src \ + ubuntu:latest \ + /bin/bash -x -c "\ + apt update && \ + apt install -y curl && \ + curl -L 'https://download.docker.com/linux/static/stable/x86_64/docker-${DOCKER_CLI_VERSION}.tgz' | \ + tar -vxzf - docker/docker --strip-component=1 && \ + mv docker /usr/local/bin/ &&\ + docker version && \ + apt install ./dist/dive_*_linux_amd64.deb -y && \ + dive --version && \ + dive '${TEST_IMAGE}' --ci \ + " + +ci-test-rpm-package-install: + docker run \ + -v //var/run/docker.sock://var/run/docker.sock \ + -v /${PWD}://src \ + -w //src \ + fedora:latest \ + /bin/bash -x -c "\ + curl -L 'https://download.docker.com/linux/static/stable/x86_64/docker-${DOCKER_CLI_VERSION}.tgz' | \ + tar -vxzf - docker/docker --strip-component=1 && \ + mv docker /usr/local/bin/ &&\ + docker version && \ + dnf install ./dist/dive_*_linux_amd64.rpm -y && \ + dive --version && \ + dive '${TEST_IMAGE}' --ci \ + " + +ci-test-linux-run: + chmod 755 ./dist/dive_linux_amd64/dive && \ + ./dist/dive_linux_amd64/dive '${TEST_IMAGE}' --ci + +# we're not attempting to test docker, just our ability to run on these systems. This avoids setting up docker in CI. +ci-test-mac-run: + chmod 755 ./dist/dive_darwin_amd64/dive && \ + ./dist/dive_darwin_amd64/dive --source docker-archive .data/test-docker-image.tar --ci --ci-config .data/.dive-ci + +# we're not attempting to test docker, just our ability to run on these systems. This avoids setting up docker in CI. +ci-test-windows-run: + ./dist/dive_windows_amd64/dive --source docker-archive .data/test-docker-image.tar --ci --ci-config .data/.dive-ci + + ## For development @@ -65,6 +113,9 @@ build: generate-test-data: docker build -t dive-test:latest -f .data/Dockerfile.test-image . && docker image save -o .data/test-docker-image.tar dive-test:latest && echo 'Exported test data!' +test: + ./.scripts/test-coverage.sh + dev: docker run -ti --rm -v $(PWD):/app -w /app -v dive-pkg:/go/pkg/ golang:1.13 bash diff --git a/runtime/run.go b/runtime/run.go index 0c8d08af..14c42af1 100644 --- a/runtime/run.go +++ b/runtime/run.go @@ -71,9 +71,9 @@ func run(enableUi bool, options Options, imageResolver image.Resolver, events ev } if options.Ci { - events.message(fmt.Sprintf(" efficiency: %2.4f %%\n", analysis.Efficiency*100)) - events.message(fmt.Sprintf(" wastedBytes: %d bytes (%s)\n", analysis.WastedBytes, humanize.Bytes(analysis.WastedBytes))) - events.message(fmt.Sprintf(" userWastedPercent: %2.4f %%\n", analysis.WastedUserPercent*100)) + events.message(fmt.Sprintf(" efficiency: %2.4f %%", analysis.Efficiency*100)) + events.message(fmt.Sprintf(" wastedBytes: %d bytes (%s)", analysis.WastedBytes, humanize.Bytes(analysis.WastedBytes))) + events.message(fmt.Sprintf(" userWastedPercent: %2.4f %%", analysis.WastedUserPercent*100)) evaluator := ci.NewCiEvaluator(options.CiConfig) pass := evaluator.Evaluate(analysis) diff --git a/runtime/run_test.go b/runtime/run_test.go index b188e224..faca3796 100644 --- a/runtime/run_test.go +++ b/runtime/run_test.go @@ -189,9 +189,9 @@ func TestRun(t *testing.T) { events: []testEvent{ {stdout: "Building image...", stderr: "", errorOnExit: false, errMessage: ""}, {stdout: "Analyzing image...", stderr: "", errorOnExit: false, errMessage: ""}, - {stdout: " efficiency: 98.4421 %\n", stderr: "", errorOnExit: false, errMessage: ""}, - {stdout: " wastedBytes: 32025 bytes (32 kB)\n", stderr: "", errorOnExit: false, errMessage: ""}, - {stdout: " userWastedPercent: 48.3491 %\n", stderr: "", errorOnExit: false, errMessage: ""}, + {stdout: " efficiency: 98.4421 %", stderr: "", errorOnExit: false, errMessage: ""}, + {stdout: " wastedBytes: 32025 bytes (32 kB)", stderr: "", errorOnExit: false, errMessage: ""}, + {stdout: " userWastedPercent: 48.3491 %", stderr: "", errorOnExit: false, errMessage: ""}, {stdout: "Inefficient Files:\nCount Wasted Space File Path\n 2 13 kB /root/saved.txt\n 2 13 kB /root/example/somefile1.txt\n 2 6.4 kB /root/example/somefile3.txt\nResults:\n FAIL: highestUserWastedPercent: too many bytes wasted, relative to the user bytes added (%-user-wasted-bytes=0.4834911001404049 > threshold=0.1)\n FAIL: highestWastedBytes: too many bytes wasted (wasted-bytes=32025 > threshold=1000)\n PASS: lowestEfficiency\nResult:FAIL [Total:3] [Passed:1] [Failed:2] [Warn:0] [Skipped:0]\n", stderr: "", errorOnExit: false, errMessage: ""}, {stdout: "", stderr: "", errorOnExit: true, errMessage: ""}, }, @@ -209,9 +209,9 @@ func TestRun(t *testing.T) { events: []testEvent{ {stdout: "Building image...", stderr: "", errorOnExit: false, errMessage: ""}, {stdout: "Analyzing image...", stderr: "", errorOnExit: false, errMessage: ""}, - {stdout: " efficiency: 98.4421 %\n", stderr: "", errorOnExit: false, errMessage: ""}, - {stdout: " wastedBytes: 32025 bytes (32 kB)\n", stderr: "", errorOnExit: false, errMessage: ""}, - {stdout: " userWastedPercent: 48.3491 %\n", stderr: "", errorOnExit: false, errMessage: ""}, + {stdout: " efficiency: 98.4421 %", stderr: "", errorOnExit: false, errMessage: ""}, + {stdout: " wastedBytes: 32025 bytes (32 kB)", stderr: "", errorOnExit: false, errMessage: ""}, + {stdout: " userWastedPercent: 48.3491 %", stderr: "", errorOnExit: false, errMessage: ""}, {stdout: "Inefficient Files:\nCount Wasted Space File Path\nNone\nResults:\n MISCONFIGURED: highestUserWastedPercent: invalid config value (''): strconv.ParseFloat: parsing \"\": invalid syntax\n MISCONFIGURED: highestWastedBytes: invalid config value (''): strconv.ParseFloat: parsing \"\": invalid syntax\n MISCONFIGURED: lowestEfficiency: invalid config value (''): strconv.ParseFloat: parsing \"\": invalid syntax\nCI Misconfigured\n", stderr: "", errorOnExit: false, errMessage: ""}, {stdout: "", stderr: "", errorOnExit: true, errMessage: ""}, }, From ba307fb811bdba7f7dbe0922950df1423aa82710 Mon Sep 17 00:00:00 2001 From: Alex Goodman Date: Wed, 20 Nov 2019 11:43:17 -0800 Subject: [PATCH 2/3] lock goreleaser version --- .github/workflows/pipeline.yml | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/.github/workflows/pipeline.yml b/.github/workflows/pipeline.yml index b052e15a..ab117180 100644 --- a/.github/workflows/pipeline.yml +++ b/.github/workflows/pipeline.yml @@ -3,8 +3,6 @@ on: [ push ] env: DOCKER_CLI_VERSION: "19.03.1" jobs: - # This will run on merge to master, and for PRs with folks that also are part of the GHA beta. - # Circle ci will also run for redundancy until GHA is out of beta, since circle checks will always run. unit-test: strategy: matrix: @@ -168,7 +166,7 @@ jobs: - name: Publish GitHub release uses: goreleaser/goreleaser-action@v1 with: - version: latest + version: "v0.123.0" args: release --rm-dist env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} From 74be19bf988efe92a83a8dcdececf993fbaf32e7 Mon Sep 17 00:00:00 2001 From: Alex Goodman Date: Wed, 20 Nov 2019 12:19:17 -0800 Subject: [PATCH 3/3] move goreleaser to makefile --- .github/workflows/pipeline.yml | 19 +++++++++---------- Makefile | 12 ++++++++++++ 2 files changed, 21 insertions(+), 10 deletions(-) diff --git a/.github/workflows/pipeline.yml b/.github/workflows/pipeline.yml index ab117180..30b075a7 100644 --- a/.github/workflows/pipeline.yml +++ b/.github/workflows/pipeline.yml @@ -43,8 +43,10 @@ jobs: - uses: actions/checkout@v1 - - name: Install go tools - run: make ci-install-go-tools + - name: Install tooling + run: | + make ci-install-go-tools + make ci-install-ci-tools - name: Cache go dependencies id: package-cache-go-dependencies @@ -62,10 +64,7 @@ jobs: run: make ci-static-analyses - name: Build snapshot artifacts - uses: goreleaser/goreleaser-action@v1 - with: - version: latest - args: release --snapshot --skip-publish --rm-dist + run: make ci-build-snapshot-packages - run: docker images wagoodman/dive @@ -145,6 +144,9 @@ jobs: - uses: actions/checkout@v1 + - name: Install tooling + run: make ci-install-ci-tools + - name: Cache go dependencies id: release-cache-go-dependencies uses: actions/cache@v1 @@ -164,10 +166,7 @@ jobs: DOCKER_PASSWORD: ${{ secrets.DOCKER_PASSWORD }} - name: Publish GitHub release - uses: goreleaser/goreleaser-action@v1 - with: - version: "v0.123.0" - args: release --rm-dist + run: make ci-release env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} diff --git a/Makefile b/Makefile index 5dcda3bc..df988368 100644 --- a/Makefile +++ b/Makefile @@ -21,6 +21,9 @@ ci-static-analyses: ci-install-go-tools: curl -sfL https://install.goreleaser.com/github.com/golangci/golangci-lint.sh | sudo sh -s -- -b /usr/local/bin/ latest +ci-install-ci-tools: + curl -sfL https://install.goreleaser.com/github.com/goreleaser/goreleaser.sh | sudo sh -s -- -b /usr/local/bin/ "v0.122.0" + ci-docker-login: echo '${DOCKER_PASSWORD}' | docker login -u '${DOCKER_USERNAME}' --password-stdin '${PRODUCTION_REGISTRY}' @@ -30,6 +33,15 @@ ci-docker-logout: ci-publish-release: goreleaser --rm-dist +ci-build-snapshot-packages: + goreleaser \ + --snapshot \ + --skip-publish \ + --rm-dist + +ci-release: + release --rm-dist + # todo: add --pull=never when supported by host box ci-test-production-image: docker run \