From 0941caaf3ecbfe06b353dbb0b5417cb76b43cfa4 Mon Sep 17 00:00:00 2001 From: stsm Date: Fri, 2 Feb 2024 19:40:50 +0400 Subject: [PATCH] Initial commit --- .github/workflows/mertricstest.yml | 354 +++++++++++++++++++++++++++++ .github/workflows/statictest.yml | 33 +++ .gitignore | 25 ++ README.md | 32 +++ cmd/agent/README.md | 3 + cmd/agent/main.go | 3 + cmd/server/README.md | 3 + cmd/server/main.go | 3 + internal/README.md | 3 + 9 files changed, 459 insertions(+) create mode 100644 .github/workflows/mertricstest.yml create mode 100644 .github/workflows/statictest.yml create mode 100644 .gitignore create mode 100644 README.md create mode 100644 cmd/agent/README.md create mode 100644 cmd/agent/main.go create mode 100644 cmd/server/README.md create mode 100644 cmd/server/main.go create mode 100644 internal/README.md diff --git a/.github/workflows/mertricstest.yml b/.github/workflows/mertricstest.yml new file mode 100644 index 0000000..91478e4 --- /dev/null +++ b/.github/workflows/mertricstest.yml @@ -0,0 +1,354 @@ +name: autotests + +on: + pull_request: + push: + branches: + - main + +jobs: + + branchtest: + runs-on: ubuntu-latest + + steps: + - name: Check branch name + run: | + if [[ ! $GITHUB_HEAD_REF =~ ^iter[0-9]+$ ]] && [[ ${{ github.ref }} != "refs/heads/main" ]]; then echo "Branch name must match pattern 'iter' or github.ref must be 'refs/heads/main'" && echo "Your branch is $GITHUB_HEAD_REF and github.ref is ${{ github.ref }}" && exit 1; else echo "Your branch is $GITHUB_HEAD_REF and github.ref is ${{ github.ref }}"; fi + + metricstest: + runs-on: ubuntu-latest + container: golang:1.21 + needs: branchtest + + services: + postgres: + image: postgres + env: + POSTGRES_PASSWORD: postgres + POSTGRES_DB: praktikum + options: >- + --health-cmd pg_isready + --health-interval 5s + --health-timeout 5s + --health-retries 5 + + steps: + - name: Checkout code + uses: actions/checkout@v2 + + - name: Download autotests binaries + uses: robinraju/release-downloader@v1.8 + with: + repository: Yandex-Practicum/go-autotests + latest: true + fileName: "*" + out-file-path: .tools + token: ${{ secrets.GITHUB_TOKEN }} + + - name: Setup autotest binary + run: | + chmod -R +x $GITHUB_WORKSPACE/.tools + mv $GITHUB_WORKSPACE/.tools/metricstest /usr/local/bin/metricstest + mv $GITHUB_WORKSPACE/.tools/random /usr/local/bin/random + + - name: Setup go deps + run: | + go install golang.org/x/tools/cmd/goimports@latest + + + - name: Build server binary + run: | + cd cmd/server + go build -buildvcs=false -o server + + - name: "Code increment #1" + if: | + github.ref == 'refs/heads/main' || + github.head_ref == 'iter1' || + github.head_ref == 'iter2' || + github.head_ref == 'iter3' || + github.head_ref == 'iter4' || + github.head_ref == 'iter5' || + github.head_ref == 'iter6' || + github.head_ref == 'iter7' || + github.head_ref == 'iter8' || + github.head_ref == 'iter9' || + github.head_ref == 'iter10' || + github.head_ref == 'iter11' || + github.head_ref == 'iter12' || + github.head_ref == 'iter13' || + github.head_ref == 'iter14' + run: | + metricstest -test.v -test.run=^TestIteration1$ \ + -binary-path=cmd/server/server + + - name: Build agent binary + run: | + cd cmd/agent + go build -buildvcs=false -o agent + + - name: "Code increment #2" + if: | + github.ref == 'refs/heads/main' || + github.head_ref == 'iter2' || + github.head_ref == 'iter3' || + github.head_ref == 'iter4' || + github.head_ref == 'iter5' || + github.head_ref == 'iter6' || + github.head_ref == 'iter7' || + github.head_ref == 'iter8' || + github.head_ref == 'iter9' || + github.head_ref == 'iter10' || + github.head_ref == 'iter11' || + github.head_ref == 'iter12' || + github.head_ref == 'iter13' || + github.head_ref == 'iter14' + run: | + metricstest -test.v -test.run=^TestIteration2[AB]*$ \ + -source-path=. \ + -agent-binary-path=cmd/agent/agent + + - name: "Code increment #3" + if: | + github.ref == 'refs/heads/main' || + github.head_ref == 'iter3' || + github.head_ref == 'iter4' || + github.head_ref == 'iter5' || + github.head_ref == 'iter6' || + github.head_ref == 'iter7' || + github.head_ref == 'iter8' || + github.head_ref == 'iter9' || + github.head_ref == 'iter10' || + github.head_ref == 'iter11' || + github.head_ref == 'iter12' || + github.head_ref == 'iter13' || + github.head_ref == 'iter14' + run: | + metricstest -test.v -test.run=^TestIteration3[AB]*$ \ + -source-path=. \ + -agent-binary-path=cmd/agent/agent \ + -binary-path=cmd/server/server + + - name: "Code increment #4" + if: | + github.ref == 'refs/heads/main' || + github.head_ref == 'iter4' || + github.head_ref == 'iter5' || + github.head_ref == 'iter6' || + github.head_ref == 'iter7' || + github.head_ref == 'iter8' || + github.head_ref == 'iter9' || + github.head_ref == 'iter10' || + github.head_ref == 'iter11' || + github.head_ref == 'iter12' || + github.head_ref == 'iter13' || + github.head_ref == 'iter14' + run: | + SERVER_PORT=$(random unused-port) + ADDRESS="localhost:${SERVER_PORT}" + TEMP_FILE=$(random tempfile) + metricstest -test.v -test.run=^TestIteration4$ \ + -agent-binary-path=cmd/agent/agent \ + -binary-path=cmd/server/server \ + -server-port=$SERVER_PORT \ + -source-path=. + + - name: "Code increment #5" + if: | + github.ref == 'refs/heads/main' || + github.head_ref == 'iter5' || + github.head_ref == 'iter6' || + github.head_ref == 'iter7' || + github.head_ref == 'iter8' || + github.head_ref == 'iter9' || + github.head_ref == 'iter10' || + github.head_ref == 'iter11' || + github.head_ref == 'iter12' || + github.head_ref == 'iter13' || + github.head_ref == 'iter14' + run: | + SERVER_PORT=$(random unused-port) + ADDRESS="localhost:${SERVER_PORT}" + TEMP_FILE=$(random tempfile) + metricstest -test.v -test.run=^TestIteration5$ \ + -agent-binary-path=cmd/agent/agent \ + -binary-path=cmd/server/server \ + -server-port=$SERVER_PORT \ + -source-path=. + + - name: "Code increment #6" + if: | + github.ref == 'refs/heads/main' || + github.head_ref == 'iter6' || + github.head_ref == 'iter7' || + github.head_ref == 'iter8' || + github.head_ref == 'iter9' || + github.head_ref == 'iter10' || + github.head_ref == 'iter11' || + github.head_ref == 'iter12' || + github.head_ref == 'iter13' || + github.head_ref == 'iter14' + run: | + SERVER_PORT=$(random unused-port) + ADDRESS="localhost:${SERVER_PORT}" + TEMP_FILE=$(random tempfile) + metricstest -test.v -test.run=^TestIteration6$ \ + -agent-binary-path=cmd/agent/agent \ + -binary-path=cmd/server/server \ + -server-port=$SERVER_PORT \ + -source-path=. + + - name: "Code increment #7" + if: | + github.ref == 'refs/heads/main' || + github.head_ref == 'iter7' || + github.head_ref == 'iter8' || + github.head_ref == 'iter9' || + github.head_ref == 'iter10' || + github.head_ref == 'iter11' || + github.head_ref == 'iter12' || + github.head_ref == 'iter13' || + github.head_ref == 'iter14' + run: | + SERVER_PORT=$(random unused-port) + ADDRESS="localhost:${SERVER_PORT}" + TEMP_FILE=$(random tempfile) + metricstest -test.v -test.run=^TestIteration7$ \ + -agent-binary-path=cmd/agent/agent \ + -binary-path=cmd/server/server \ + -server-port=$SERVER_PORT \ + -source-path=. + + - name: "Code increment #8" + if: | + github.ref == 'refs/heads/main' || + github.head_ref == 'iter8' || + github.head_ref == 'iter9' || + github.head_ref == 'iter10' || + github.head_ref == 'iter11' || + github.head_ref == 'iter12' || + github.head_ref == 'iter13' || + github.head_ref == 'iter14' + run: | + SERVER_PORT=$(random unused-port) + ADDRESS="localhost:${SERVER_PORT}" + TEMP_FILE=$(random tempfile) + metricstest -test.v -test.run=^TestIteration8$ \ + -agent-binary-path=cmd/agent/agent \ + -binary-path=cmd/server/server \ + -server-port=$SERVER_PORT \ + -source-path=. + + - name: "Code increment #9" + if: | + github.ref == 'refs/heads/main' || + github.head_ref == 'iter9' || + github.head_ref == 'iter10' || + github.head_ref == 'iter11' || + github.head_ref == 'iter12' || + github.head_ref == 'iter13' || + github.head_ref == 'iter14' + run: | + SERVER_PORT=$(random unused-port) + ADDRESS="localhost:${SERVER_PORT}" + TEMP_FILE=$(random tempfile) + metricstest -test.v -test.run=^TestIteration9$ \ + -agent-binary-path=cmd/agent/agent \ + -binary-path=cmd/server/server \ + -file-storage-path=$TEMP_FILE \ + -server-port=$SERVER_PORT \ + -source-path=. + + - name: "Code increment #10" + if: | + github.ref == 'refs/heads/main' || + github.head_ref == 'iter10' || + github.head_ref == 'iter11' || + github.head_ref == 'iter12' || + github.head_ref == 'iter13' || + github.head_ref == 'iter14' + run: | + SERVER_PORT=$(random unused-port) + ADDRESS="localhost:${SERVER_PORT}" + TEMP_FILE=$(random tempfile) + metricstest -test.v -test.run=^TestIteration10[AB]$ \ + -agent-binary-path=cmd/agent/agent \ + -binary-path=cmd/server/server \ + -database-dsn='postgres://postgres:postgres@postgres:5432/praktikum?sslmode=disable' \ + -server-port=$SERVER_PORT \ + -source-path=. + + - name: "Code increment #11" + if: | + github.ref == 'refs/heads/main' || + github.head_ref == 'iter11' || + github.head_ref == 'iter12' || + github.head_ref == 'iter13' || + github.head_ref == 'iter14' + run: | + SERVER_PORT=$(random unused-port) + ADDRESS="localhost:${SERVER_PORT}" + TEMP_FILE=$(random tempfile) + metricstest -test.v -test.run=^TestIteration11$ \ + -agent-binary-path=cmd/agent/agent \ + -binary-path=cmd/server/server \ + -database-dsn='postgres://postgres:postgres@postgres:5432/praktikum?sslmode=disable' \ + -server-port=$SERVER_PORT \ + -source-path=. + + - name: "Code increment #12" + if: | + github.ref == 'refs/heads/main' || + github.head_ref == 'iter12' || + github.head_ref == 'iter13' || + github.head_ref == 'iter14' + run: | + SERVER_PORT=$(random unused-port) + ADDRESS="localhost:${SERVER_PORT}" + TEMP_FILE=$(random tempfile) + metricstest -test.v -test.run=^TestIteration12$ \ + -agent-binary-path=cmd/agent/agent \ + -binary-path=cmd/server/server \ + -database-dsn='postgres://postgres:postgres@postgres:5432/praktikum?sslmode=disable' \ + -server-port=$SERVER_PORT \ + -source-path=. + + - name: "Code increment #13" + if: | + github.ref == 'refs/heads/main' || + github.head_ref == 'iter13' || + github.head_ref == 'iter14' + run: | + SERVER_PORT=$(random unused-port) + ADDRESS="localhost:${SERVER_PORT}" + TEMP_FILE=$(random tempfile) + metricstest -test.v -test.run=^TestIteration13$ \ + -agent-binary-path=cmd/agent/agent \ + -binary-path=cmd/server/server \ + -database-dsn='postgres://postgres:postgres@postgres:5432/praktikum?sslmode=disable' \ + -server-port=$SERVER_PORT \ + -source-path=. + + - name: "Code increment #14" + if: | + github.ref == 'refs/heads/main' || + github.head_ref == 'iter14' + run: | + SERVER_PORT=$(random unused-port) + ADDRESS="localhost:${SERVER_PORT}" + TEMP_FILE=$(random tempfile) + metricstest -test.v -test.run=^TestIteration14$ \ + -agent-binary-path=cmd/agent/agent \ + -binary-path=cmd/server/server \ + -database-dsn='postgres://postgres:postgres@postgres:5432/praktikum?sslmode=disable' \ + -key="${TEMP_FILE}" \ + -server-port=$SERVER_PORT \ + -source-path=. + + - name: "Code increment #14 (race detection)" + if: | + github.ref == 'refs/heads/main' || + github.head_ref == 'iter14' + run: | + go test -v -race ./... diff --git a/.github/workflows/statictest.yml b/.github/workflows/statictest.yml new file mode 100644 index 0000000..d4aa708 --- /dev/null +++ b/.github/workflows/statictest.yml @@ -0,0 +1,33 @@ +name: go vet test + +on: + pull_request: + push: + branches: + - main + +jobs: + statictest: + runs-on: ubuntu-latest + container: golang:1.21 + steps: + - name: Checkout code + uses: actions/checkout@v2 + + - name: Download statictest binary + uses: robinraju/release-downloader@v1.8 + with: + repository: Yandex-Practicum/go-autotests + latest: true + fileName: statictest + out-file-path: .tools + token: ${{ secrets.GITHUB_TOKEN }} + + - name: Setup autotest binary + run: | + chmod -R +x $GITHUB_WORKSPACE/.tools/statictest + mv $GITHUB_WORKSPACE/.tools/statictest /usr/local/bin/statictest + + - name: Run statictest + run: | + go vet -vettool=$(which statictest) ./... diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d67e2ca --- /dev/null +++ b/.gitignore @@ -0,0 +1,25 @@ +# Binaries for programs and plugins +*.exe +*.exe~ +*.dll +*.so +*.dylib + +# Test binary, built with `go test -c` +*.test + +# Output of the go coverage tool, specifically when used with LiteIDE +*.out + +# Binaries +cmd/agent/main +cmd/agent/agent +cmd/server/main +cmd/server/server + +# Dependency directories (remove the comment below to include it) +vendor/ + +# IDEs directories +.idea +.vscode diff --git a/README.md b/README.md new file mode 100644 index 0000000..f15d8b1 --- /dev/null +++ b/README.md @@ -0,0 +1,32 @@ +# go-musthave-metrics-tpl + +Шаблон репозитория для трека «Сервер сбора метрик и алертинга». + +## Начало работы + +1. Склонируйте репозиторий в любую подходящую директорию на вашем компьютере. +2. В корне репозитория выполните команду `go mod init ` (где `` — адрес вашего репозитория на GitHub без префикса `https://`) для создания модуля. + +## Обновление шаблона + +Чтобы иметь возможность получать обновления автотестов и других частей шаблона, выполните команду: + +``` +git remote add -m main template https://github.com/Yandex-Practicum/go-musthave-metrics-tpl.git +``` + +Для обновления кода автотестов выполните команду: + +``` +git fetch template && git checkout template/main .github +``` + +Затем добавьте полученные изменения в свой репозиторий. + +## Запуск автотестов + +Для успешного запуска автотестов называйте ветки `iter`, где `` — порядковый номер инкремента. Например, в ветке с названием `iter4` запустятся автотесты для инкрементов с первого по четвёртый. + +При мёрже ветки с инкрементом в основную ветку `main` будут запускаться все автотесты. + +Подробнее про локальный и автоматический запуск читайте в [README автотестов](https://github.com/Yandex-Practicum/go-autotests). diff --git a/cmd/agent/README.md b/cmd/agent/README.md new file mode 100644 index 0000000..5d500a4 --- /dev/null +++ b/cmd/agent/README.md @@ -0,0 +1,3 @@ +# cmd/agent + +В данной директории будет содержаться код Агента, который скомпилируется в бинарное приложение diff --git a/cmd/agent/main.go b/cmd/agent/main.go new file mode 100644 index 0000000..38dd16d --- /dev/null +++ b/cmd/agent/main.go @@ -0,0 +1,3 @@ +package main + +func main() {} diff --git a/cmd/server/README.md b/cmd/server/README.md new file mode 100644 index 0000000..cf02a69 --- /dev/null +++ b/cmd/server/README.md @@ -0,0 +1,3 @@ +# cmd/agent + +В данной директории будет содержаться код Сервера, который скомпилируется в бинарное приложение diff --git a/cmd/server/main.go b/cmd/server/main.go new file mode 100644 index 0000000..38dd16d --- /dev/null +++ b/cmd/server/main.go @@ -0,0 +1,3 @@ +package main + +func main() {} diff --git a/internal/README.md b/internal/README.md new file mode 100644 index 0000000..081537e --- /dev/null +++ b/internal/README.md @@ -0,0 +1,3 @@ +# internal + +В данной директории и её поддиректориях будет содержаться имплементация вашего сервиса \ No newline at end of file