From a236fc5a0d9c2cb1968831bb8534d488751422e0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stefan=20K=C3=BCrzeder?= Date: Mon, 25 May 2020 20:34:48 +0200 Subject: [PATCH] Added Unit Tests --- .github/workflows/docker-build-release.yaml | 1 - .github/workflows/docker-build.yaml | 1 - .github/workflows/test.yaml | 22 ------- .github/workflows/unit-tests.yaml | 38 +++++++++++ .gitignore | 3 +- .vscode/launch.json | 2 +- main_test.go | 72 +++++++++++++++++++++ test/template.json | 8 +-- 8 files changed, 117 insertions(+), 30 deletions(-) delete mode 100644 .github/workflows/test.yaml create mode 100644 .github/workflows/unit-tests.yaml create mode 100644 main_test.go diff --git a/.github/workflows/docker-build-release.yaml b/.github/workflows/docker-build-release.yaml index 5dd1280..b7d4d5a 100644 --- a/.github/workflows/docker-build-release.yaml +++ b/.github/workflows/docker-build-release.yaml @@ -9,7 +9,6 @@ on: jobs: build: - name: build-release runs-on: ubuntu-latest steps: - name: Checkout Code diff --git a/.github/workflows/docker-build.yaml b/.github/workflows/docker-build.yaml index 9876bf2..09bd2cb 100644 --- a/.github/workflows/docker-build.yaml +++ b/.github/workflows/docker-build.yaml @@ -9,7 +9,6 @@ on: jobs: build: - name: build-release runs-on: ubuntu-latest steps: - name: Checkout Code diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml deleted file mode 100644 index fb9d72f..0000000 --- a/.github/workflows/test.yaml +++ /dev/null @@ -1,22 +0,0 @@ -on: [push] - -jobs: - test_action_job: - runs-on: ubuntu-latest - name: A job to test this action - steps: - # To use this repository's private action, you must check out the repository - - name: Checkout - uses: actions/checkout@v2 - - name: ARM deployment test - uses: ./ # Uses an action in the root directory - with: - creds: ${{ secrets.AZURE_CREDENTIALS }} - resourceGroupName: azurearmaction - templateLocation: ./test/template.json - parametersLocation: ./test/parameters.json - deploymentName: github-test - deploymentMode: Incremental - env: - LOG_LEVEL: DEBUG - diff --git a/.github/workflows/unit-tests.yaml b/.github/workflows/unit-tests.yaml new file mode 100644 index 0000000..0877162 --- /dev/null +++ b/.github/workflows/unit-tests.yaml @@ -0,0 +1,38 @@ +name: Unit Tests +on: + pull_request: + push: + branches: + - 'master' + +jobs: + test_action_job: + runs-on: ubuntu-latest + steps: + - name: Set up Go 1.14 + uses: actions/setup-go@v1 + with: + go-version: 1.14 + - name: Check out source code + uses: actions/checkout@v1 + - name: Build + env: + GOPROXY: "https://proxy.golang.org" + CGO_ENABLED: 0 + GOOS: linux + GOARCH: amd64 + run: go build -a -installsuffix cgo -ldflags="-w -s" . + - name: Test + env: + GOPROXY: "https://proxy.golang.org" + CGO_ENABLED: 0 + GOOS: linux + GOARCH: amd64 + LOG_LEVEL: DEBUG + INPUT_CREDS: ${{ secrets.AZURE_CREDENTIALS }} + INPUT_RESOURCEGROUPNAME: azurearmaction + INPUT_TEMPLATELOCATION: ./test/template.json + INPUT_PARAMETERSLOCATION: ./test/parameters.json + INPUT_DEPLOYMENTNAME: github-test + INPUT_DEPLOYMENTMODE: Incremental + run: go test -v -failfast . \ No newline at end of file diff --git a/.gitignore b/.gitignore index 156c4f5..5f2087e 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ sp.json arm-action -__debug_bin \ No newline at end of file +__debug_bin +run_tests.sh \ No newline at end of file diff --git a/.vscode/launch.json b/.vscode/launch.json index 182d895..ce971ee 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -20,6 +20,6 @@ "LOG_LEVEL": "DEBUG" }, "args": [] - } + }, ] } \ No newline at end of file diff --git a/main_test.go b/main_test.go new file mode 100644 index 0000000..8421aee --- /dev/null +++ b/main_test.go @@ -0,0 +1,72 @@ +package main + +import ( + "context" + "testing" + + "github.com/Azure/azure-sdk-for-go/services/resources/mgmt/2019-05-01/resources" + "github.com/Azure/go-autorest/autorest" + "github.com/whiteducksoftware/azure-arm-action/pkg/github" + "github.com/whiteducksoftware/azure-arm-action/pkg/github/actions" +) + +var ( + opts *github.Options + authorizer *autorest.Authorizer + deploymentResult resources.DeploymentExtended +) + +func TestLoadOptions(t *testing.T) { + var err error + opts, err = github.LoadOptions() + if err != nil { + t.Error(err.Error()) + } +} + +func TestAuthentication(t *testing.T) { + var err error + authorizer, err = actions.Authenticate(opts.Inputs) + if err != nil { + t.Error(err.Error()) + } +} + +func TestDeploy(t *testing.T) { + var err error + deploymentResult, err = actions.Deploy(context.Background(), opts.Inputs, authorizer) + if err != nil { + t.Error(err.Error()) + } +} + +func TestParseOutputs(t *testing.T) { + outputs, err := actions.ParseOutputs(deploymentResult.Properties.Outputs) + if err != nil { + t.Error(err.Error()) + } + + if len(outputs) != 2 { + t.Errorf("Got invalid count of outputs, expected 2 got %d", len(outputs)) + } + + // Test output key location + value, ok := outputs["location"] + if !ok { + t.Errorf("Test key is missing in the outputs, exptected the key location to be present") + } + + if value.Value != "westeurope" { + t.Errorf("Got invalid value for location key, expected %s got %s", "westeurope", value.Value) + } + + // Test output key containername + value, ok = outputs["containerName"] + if !ok { + t.Errorf("Test key is missing in the outputs, exptected the key containerName to be present") + } + + if value.Value != "github-action" { + t.Errorf("Got invalid value for location key, expected %s got %s", "github-action", value.Value) + } +} diff --git a/test/template.json b/test/template.json index 219d6cb..761eaea 100644 --- a/test/template.json +++ b/test/template.json @@ -79,13 +79,13 @@ } ], "outputs": { - "resourceID": { + "location": { "type": "string", - "value": "[resourceId('Microsoft.ContainerInstance/containerGroups', parameters('containerName'))]" + "value": "[parameters('location')]" }, - "resourceID2": { + "containerName": { "type": "string", - "value": "[resourceId('Microsoft.ContainerInstance/containerGroups', parameters('containerName'))]" + "value": "[parameters('containerName')]" } } }