Skip to content

Commit

Permalink
Tests: add tests to cover azure snapshots
Browse files Browse the repository at this point in the history
  • Loading branch information
arstanaly committed Dec 11, 2023
1 parent ace0349 commit 9877716
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 2 deletions.
1 change: 1 addition & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ jobs:
github_access_token: ${{ secrets.KOSLI_GITHUB_TOKEN }}
gitlab_access_token: ${{ secrets.KOSLI_GITLAB_TOKEN }}
azure_access_token: ${{ secrets.KOSLI_AZURE_TOKEN }}
azure_service_token: ${{ secrets.KOSLI_AZURE_SERVICE_TOKEN }}
bitbucket_password: ${{ secrets.KOSLI_BITBUCKET_PASSWORD }}
jira_api_token: ${{ secrets.KOSLI_JIRA_API_TOKEN }}
slack_webhook: ${{ secrets.MERKELY_SLACK_CI_FAILURES_WEBHOOK }}
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ jobs:
github_access_token: ${{ secrets.KOSLI_GITHUB_TOKEN }}
gitlab_access_token: ${{ secrets.KOSLI_GITLAB_TOKEN }}
azure_access_token: ${{ secrets.KOSLI_AZURE_TOKEN }}
azure_service_token: ${{ secrets.KOSLI_AZURE_SERVICE_TOKEN }}
bitbucket_password: ${{ secrets.KOSLI_BITBUCKET_PASSWORD }}
slack_webhook: ${{ secrets.MERKELY_SLACK_CI_FAILURES_WEBHOOK }}
slack_channel: ${{ secrets.MERKELY_SLACK_CI_FAILURES_CHANNEL }}
Expand Down
3 changes: 3 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ on:
required: true
azure_access_token:
required: true
azure_service_token:
required: true
bitbucket_password:
required: true
jira_api_token:
Expand Down Expand Up @@ -66,6 +68,7 @@ jobs:
KOSLI_GITHUB_TOKEN: ${{ secrets.github_access_token }}
KOSLI_GITLAB_TOKEN: ${{ secrets.gitlab_access_token }}
KOSLI_AZURE_TOKEN: ${{ secrets.azure_access_token }}
KOSLI_AZURE_CLIENT_SECRET: ${{ secrets.azure_service_token }}
KOSLI_BITBUCKET_PASSWORD: ${{ secrets.bitbucket_password }}
KOSLI_JIRA_API_TOKEN: ${{ secrets.jira_api_token }}
run: |
Expand Down
2 changes: 1 addition & 1 deletion cmd/kosli/beginTrail_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func (suite *BeginTrailCommandTestSuite) TestBeginTrailCmd() {
wantError: true,
name: "fails when name is considered invalid by the server",
cmd: fmt.Sprintf("begin trail foo?$bar --flow %s %s", suite.flowName, suite.defaultKosliArguments),
golden: "Error: Input payload validation failed: map[name:'foo?$bar' does not match '^[a-zA-Z][a-zA-Z0-9\\\\-_\\\\.~]*$']\n",
golden: "Error: Input payload validation failed: map[name:'foo?$bar' does not match '^[a-zA-Z0-9][a-zA-Z0-9\\\\-_\\\\.~]*$']\n",
},
{
wantError: true,
Expand Down
2 changes: 1 addition & 1 deletion cmd/kosli/snapshotAzureApps.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func newSnapshotAzureAppsCmd(out io.Writer) *cobra.Command {
}

if o.azureStaticCredentials.DigestsSource != "acr" && o.azureStaticCredentials.DigestsSource != "logs" {
return ErrorBeforePrintingUsage(cmd, "invalid value for --digests-source flag. Valid values are 'acr' and 'logs'")
return fmt.Errorf("invalid value for --digests-source flag. Valid values are 'acr' and 'logs'")
}

return nil
Expand Down
103 changes: 103 additions & 0 deletions cmd/kosli/snapshotAzureApps_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
package main

import (
"fmt"
"testing"

"github.com/kosli-dev/cli/internal/testHelpers"
"github.com/stretchr/testify/suite"
)

// Define the suite, and absorb the built-in basic suite
// functionality from testify - including a T() method which
// returns the current testing context
type SnapshotAzureAppsTestSuite struct {
suite.Suite
defaultKosliArguments string
defaultAzureArguments string
envName string
}

func (suite *SnapshotAzureAppsTestSuite) SetupTest() {
testHelpers.SkipIfEnvVarUnset(suite.T(), []string{"KOSLI_AZURE_CLIENT_SECRET"})

suite.envName = "snapshot-azure-env"
global = &GlobalOpts{
ApiToken: "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpZCI6ImNkNzg4OTg5In0.e8i_lA_QrEhFncb05Xw6E_tkCHU9QfcY4OLTVUCHffY",
Org: "docs-cmd-test-user",
Host: "http://localhost:8001",
}
suite.defaultKosliArguments = fmt.Sprintf(" --host %s --org %s --api-token %s", global.Host, global.Org, global.ApiToken)
// AZURE-CLIENT-SECRET is set as a secret and passed as env variable to tests
suite.defaultAzureArguments = " --azure-client-id 0bfad7d5-eb5e-4144-95a0-0a0d66eb07cb --azure-tenant-id e52b5fba-43c2-4eaf-91c1-579dc6fae771 " +
"--azure-subscription-id 96cdee58-1fa8-419d-a65a-7233b3465632 --azure-resource-group-name EnvironmentReportingExperiment"
CreateEnv(global.Org, suite.envName, "azure-apps", suite.T())
}

func (suite *SnapshotAzureAppsTestSuite) TestSnapshotAzureAppsCmd() {
tests := []cmdTestCase{
{
wantError: true,
name: "snapshot azure fails if 2 args are provided",
cmd: fmt.Sprintf(`snapshot azure %s xxx %s %s`, suite.envName, suite.defaultKosliArguments, suite.defaultAzureArguments),
golden: "Error: accepts 1 arg(s), received 2\n",
},
{
wantError: true,
name: "snapshot azure fails if no args are set",
cmd: fmt.Sprintf(`snapshot azure %s %s`, suite.defaultKosliArguments, suite.defaultAzureArguments),
golden: "Error: accepts 1 arg(s), received 0\n",
},
{
wantError: true,
name: "snapshot azure fails if --digests-source flag is set to invalid value",
cmd: fmt.Sprintf(`snapshot azure %s %s %s --digests-source ghcr `, suite.envName, suite.defaultKosliArguments, suite.defaultAzureArguments),
golden: "Error: invalid value for --digests-source flag. Valid values are 'acr' and 'logs'\n",
},
{
name: "snapshot azure succeeds if all required flags are set",
cmd: fmt.Sprintf(`snapshot azure %s %s %s --dry-run`, suite.envName, suite.defaultKosliArguments, suite.defaultAzureArguments),
},
{
name: "snapshot azure succeeds when digests-source is set to acr if all required flags are set",
cmd: fmt.Sprintf(`snapshot azure %s %s %s --digests-source acr --dry-run`, suite.envName, suite.defaultKosliArguments, suite.defaultAzureArguments),
},
{
name: "snapshot azure succeeds when digests-source is set to logs if all required flags are set",
cmd: fmt.Sprintf(`snapshot azure %s %s %s --digests-source logs --dry-run`, suite.envName, suite.defaultKosliArguments, suite.defaultAzureArguments),
},

{
wantError: true,
name: "snapshot azure fails when Azure client ID is not set",
cmd: fmt.Sprintf(`snapshot azure %s %s --azure-client-secret xxx --azure-tenant-id xxx --azure-subscription-id xxx --azure-resource-group-name xxx`, suite.envName, suite.defaultKosliArguments),
golden: "Error: required flag(s) \"azure-client-id\" not set\n",
},
{
wantError: true,
name: "snapshot azure fails when Azure tenant ID is not set",
cmd: fmt.Sprintf(`snapshot azure %s %s --azure-client-id xxx --azure-client-secret xxx --azure-subscription-id xxx --azure-resource-group-name xxx`, suite.envName, suite.defaultKosliArguments),
golden: "Error: required flag(s) \"azure-tenant-id\" not set\n",
},
{
wantError: true,
name: "snapshot azure fails when Azure subscription ID is not set",
cmd: fmt.Sprintf(`snapshot azure %s %s --azure-client-id xxx --azure-client-secret xxx --azure-tenant-id xxx --azure-resource-group-name xxx`, suite.envName, suite.defaultKosliArguments),
golden: "Error: required flag(s) \"azure-subscription-id\" not set\n",
},
{
wantError: true,
name: "snapshot azure fails when Azure resource group name is not set",
cmd: fmt.Sprintf(`snapshot azure %s %s --azure-client-id xxx --azure-client-secret xxx --azure-tenant-id xxx --azure-subscription-id xxx`, suite.envName, suite.defaultKosliArguments),
golden: "Error: required flag(s) \"azure-resource-group-name\" not set\n",
},
}

runTestCmd(suite.T(), tests)
}

// In order for 'go test' to run this suite, we need to create
// a normal test function and pass our suite to suite.Run
func TestSnapshotAzureAppsTestSuite(t *testing.T) {
suite.Run(t, new(SnapshotAzureAppsTestSuite))
}

0 comments on commit 9877716

Please sign in to comment.