Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…perations into dapr-tutorial
  • Loading branch information
ryanwinter committed Nov 13, 2023
2 parents 66f7f82 + c0e6ebd commit 1a5c37c
Show file tree
Hide file tree
Showing 96 changed files with 2,259 additions and 1,126 deletions.
7 changes: 5 additions & 2 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,14 @@
"RESOURCE_GROUP": {
"description": "Your Azure resource group"
},
"REGION": {
"LOCATION": {
"description": "Region to use, must be one of eastus, eastus2, westus, westus2, westus3, westeurope, or northeurope.",
"documentationUrl": "https://review.learn.microsoft.com/en-us/azure/iot-operations/deploy/howto-prepare-cluster"
"documentationUrl": "https://learn.microsoft.com/azure/iot-operations/deploy/howto-prepare-cluster"
}
},
"containerEnv": {
"K3D_FIX_MOUNTS": "1"
},
// Use 'forwardPorts' to make a list of ports inside the container available locally.
// "forwardPorts": [],
// Use 'postCreateCommand' to run commands after the container is created.
Expand Down
20 changes: 20 additions & 0 deletions .github/workflows/callout_build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
name: callout_build

on:
workflow_dispatch:
push:
tags:
- 'v*.*.*-callout'

jobs:
call-docker-build:
uses: ./.github/workflows/docker_build.yml
with:
registry: makocr.azurecr.io/callout
file: samples/callout/Dockerfile
context: .
secrets:
azureCredentials: ${{ secrets.AZURE_CREDENTIALS }}
registryLoginServer: ${{ secrets.REGISTRY_LOGIN_SERVER }}
registryUsername: ${{ secrets.REGISTRY_USERNAME }}
registryPassword: ${{ secrets.REGISTRY_PASSWORD }}
13 changes: 13 additions & 0 deletions .github/workflows/callout_test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
name: krill_test

on:
workflow_dispatch:
push:
paths:
- samples/callout/**

jobs:
call-mage-test:
uses: ./.github/workflows/mage_tests.yml
with:
workdir: samples/callout/
12 changes: 10 additions & 2 deletions .github/workflows/docker_build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,13 @@ jobs:
context: ${{ inputs.context }}
file: ${{ inputs.file }}
push: true
cache-from: type=gha
cache-to: type=gha,mode=max

- name: Build and Push if Pushed with Tag
uses: docker/build-push-action@v5
with:
tags: |
${{ inputs.registry }}:${{ github.ref_name }}
context: ${{ inputs.context }}
file: ${{ inputs.file }}
push: true
if: ${{ github.event_name == 'push' }}
10 changes: 5 additions & 5 deletions .github/workflows/krill_build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@ name: krill_build
on:
workflow_dispatch:
push:
paths:
- samples/krill/**
tags:
- 'v*.*.*-krill'

jobs:
call-docker-build:
uses: ./.github/workflows/docker_build.yml
with:
registry: azbluefin.azurecr.io/krill
file: Dockerfile
context: ./samples/krill/
registry: makocr.azurecr.io/krill
file: samples/krill/Dockerfile
context: .
secrets:
azureCredentials: ${{ secrets.AZURE_CREDENTIALS }}
registryLoginServer: ${{ secrets.REGISTRY_LOGIN_SERVER }}
Expand Down
13 changes: 13 additions & 0 deletions .github/workflows/krill_test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
name: krill_test

on:
workflow_dispatch:
push:
paths:
- samples/krill/**

jobs:
call-mage-test:
uses: ./.github/workflows/mage_tests.yml
with:
workdir: samples/krill/
36 changes: 36 additions & 0 deletions .github/workflows/mage_tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
name: mage_tests

on:
workflow_call:
inputs:
workdir:
required: true
type: string


jobs:
mage_test:
name: "Runs golang tests using mage command provided in libraries"
runs-on: ubuntu-latest

defaults:
run:
shell: bash

steps:
- name: Checkout
uses: actions/checkout@v4

- name: Setup Go 1.21
uses: actions/setup-go@v4
with:
go-version: 1.21

- name: Install Mage
run: |
go install github.com/magefile/mage@latest
- name: Run CI Verification
working-directory: ${{ inputs.workdir }}
run: |
mage ci
2 changes: 1 addition & 1 deletion lib/mage/go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module github.com/Azure-Samples/explore-iot-operations/lib/mage
module github.com/explore-iot-operations/lib/mage

go 1.21.3

Expand Down
2 changes: 1 addition & 1 deletion lib/mage/mageerrors.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package mage

import "fmt"

Expand Down
117 changes: 29 additions & 88 deletions lib/mage/magefile.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
//go:build mage
// +build mage

package main
package mage

import (
"encoding/json"
"fmt"
"io"
"strconv"
"strings"

"github.com/magefile/mage/sh"
"github.com/princjef/mageutil/bintool"
"github.com/princjef/mageutil/shellcmd"
)

var (
Expand All @@ -28,37 +27,6 @@ var (
"0.4.1",
"https://github.com/princjef/gomarkdoc/releases/download/v{{.Version}}/gomarkdoc_{{.Version}}_{{.GOOS}}_{{.GOARCH}}{{.ArchiveExt}}",
))
releaser = bintool.Must(bintool.NewGo(
"github.com/goreleaser/goreleaser",
"v1.18.2",
))
)

const (
KRILL project = "krill"
)

var projects = []project{
KRILL,
}

var buildProjectPathMap map[project]string = map[project]string{
KRILL: "./cmd/krill",
}

var releasePathMap map[project]string = map[project]string{
KRILL: "./releases/krill/.goreleaser.yaml",
}

const (
// UnitTestTimeoutMs specifies the maximum amount of time a unit test will be given before it is considered failed.
UnitTestTimeoutMs = 3000

// ExpectedBlockCoverage describes the minimum expected test coverage of each code block.
ExpectedBlockCoverage = 0.00

// ExpectedOverallCoverage describes the minimum expected test coverage of the overall codebase.
ExpectedOverallCoverage = 85.00
)

func ensureFormatter() error {
Expand All @@ -73,10 +41,6 @@ func ensureDocumenter() error {
return documenter.Ensure()
}

func ensureReleaser() error {
return releaser.Ensure()
}

func EnsureAllTools() error {
if err := ensureFormatter(); err != nil {
return err
Expand All @@ -90,10 +54,6 @@ func EnsureAllTools() error {
return err
}

if err := ensureReleaser(); err != nil {
return err
}

return nil
}

Expand Down Expand Up @@ -132,7 +92,8 @@ func Clean() error {

// Cover runs tests and generates coverage profiles for all tests.
// The tests run in atomic mode and check for race conditions.
func Cover() error {
// UnitTestTimeoutMs specifies the maximum amount of time a unit test will be given before it is considered failed.
func Cover(unitTestTimeoutMs int) error {
err := Clean()
if err != nil {
return err
Expand All @@ -142,7 +103,7 @@ func Cover() error {
"go",
"test",
"-timeout",
fmt.Sprintf("%dms", UnitTestTimeoutMs),
fmt.Sprintf("%dms", unitTestTimeoutMs),
"-cover",
"--coverprofile=cover.tmp.out",
"-covermode=atomic",
Expand All @@ -161,24 +122,18 @@ func Cover() error {
return sh.RunV("go", "tool", "cover", "-func=coverage.out")
}


// Package is a subset of the structure returned by the go list tool.
type Package struct {
TestGoFiles []string `json:"TestGoFiles"`
XTestGoFiles []string `json:"XTestGoFiles"`
ImportPath string `json:"ImportPath"`
}

// ImportPathRoot is the root import for all packages in this project.
const ImportPathRoot = "dev.azure.com/msazure/One/_git/Digital-Operations-Experience/service/"

// TestPackageExclusions is a set of packages which are excluded from the check for at least one test file.
// This should only include the main package and any packages which only define types or constants.
var TestPackageExclusions = map[string]any{}

// EnsureTests ensures that every package besides those excluded via the "TestPackageExclusions" variable defined above must contain at least one test file.
// This ensures that coverage will be measured for all packages and forces the creation of test files for all new packages.
func EnsureTests() error {
// TestPackageExclusions is a set of packages which are excluded from the check for at least one test file.
// This should only include the main package and any packages which only define types or constants.
func EnsureTests(importPathRoot string, testPackageExclusions map[string]any) error {
res, err := sh.Output("go", "list", "-json", "./...")
if err != nil {
return err
Expand All @@ -200,7 +155,7 @@ func EnsureTests() error {
}

for _, pack := range packages {
if _, ok := TestPackageExclusions[strings.TrimPrefix(pack.ImportPath, ImportPathRoot)]; ok {
if _, ok := testPackageExclusions[strings.TrimPrefix(pack.ImportPath, importPathRoot)]; ok {
continue
}
if len(pack.XTestGoFiles) < 1 && len(pack.TestGoFiles) < 1 {
Expand All @@ -221,7 +176,9 @@ func Bench() error {
// EvaluateCoverage takes a coverage file and evaluates the coverage of code block and overall app unit test coverage.
// If the coverage of any given block or the overall coverage of the application does not meet the above
// thresholds, an error will be returned. Otherwise a message describing coverage will be reported.
func EvaluateCoverage() error {
// ExpectedBlockCoverage describes the minimum expected test coverage of each code block.
// ExpectedOverallCoverage describes the minimum expected test coverage of the overall codebase.
func EvaluateCoverage(expectedBlockCoverage, expectedOverallCoverage float64) error {
res, err := sh.Output(
"go",
"tool",
Expand Down Expand Up @@ -261,20 +218,20 @@ func EvaluateCoverage() error {
path: components[0],
name: components[1],
percentage: percentage,
expected: ExpectedBlockCoverage,
expected: expectedBlockCoverage,
}
}

for _, coverage := range coverages {
if coverage.percentage < ExpectedBlockCoverage {
if coverage.percentage < expectedBlockCoverage {
return coverage
}
}

if totalCoverage.percentage < ExpectedOverallCoverage {
if totalCoverage.percentage < expectedOverallCoverage {
return &InadequateOverallCoverageError{
percentage: totalCoverage.percentage,
expected: ExpectedOverallCoverage,
expected: expectedOverallCoverage,
}
}

Expand All @@ -286,57 +243,41 @@ func EvaluateCoverage() error {
return nil
}

func Build(proj string) error {

path, ok := buildProjectPathMap[project(proj)]
if !ok {
return fmt.Errorf("invalid project name, must be one of the following: %v", projects)
}

err := EnsureAllTools()
if err != nil {
return err
}

err = Cover()
if err != nil {
return err
}

func Build(proj string, path string) error {
return sh.RunV("go", "build", "-o", proj, path)
}

func Release(proj string, version string, message string) error {

path, ok := releasePathMap[project(proj)]
if !ok {
return fmt.Errorf("invalid project name, must be one of the following: %v", projects)
}
func CI(
importPathRoot string,
testPackageExclusions map[string]any,
unitTestTimeoutMs int,
expectedBlockCoverage, expectedOverallCoverage float64,
) error {

err := EnsureAllTools()
if err != nil {
return err
}

err = Cover()
err = Lint()
if err != nil {
return err
}

err = sh.RunV("goreleaser", "check", path)
err = Format()
if err != nil {
return err
}

err = sh.RunV("git", "tag", "-a", version, "-m", message)
err = EnsureTests(importPathRoot, testPackageExclusions)
if err != nil {
return err
}

err = sh.RunV("git", "push", "origin", version)
err = Cover(unitTestTimeoutMs)
if err != nil {
return err
}

return sh.RunV("goreleaser", "release", "--clean", "-f", path)
return EvaluateCoverage(expectedBlockCoverage, expectedOverallCoverage)
}
Loading

0 comments on commit 1a5c37c

Please sign in to comment.