From 587488a7205048ab66095854db39d6d54afdb354 Mon Sep 17 00:00:00 2001 From: "Ruijie Pei (MSFT)" <49467823+JerryPei1997@users.noreply.github.com> Date: Thu, 26 Mar 2020 18:36:56 +0800 Subject: [PATCH] Enables live-tests on Azure-Pipeline for keyvault (#273) * Update livetest.yml --- .azure-pipelines/livetest.yml | 7 ++++ .azure-pipelines/steps/init_workspace.yml | 2 +- .azure-pipelines/steps/live_test.yml | 16 +++++++++ azure-pipelines.yml | 9 ++--- internal/config/env.go | 40 ++++++----------------- keyvault/keyvault_test.go | 2 ++ 6 files changed, 38 insertions(+), 38 deletions(-) create mode 100644 .azure-pipelines/livetest.yml create mode 100644 .azure-pipelines/steps/live_test.yml diff --git a/.azure-pipelines/livetest.yml b/.azure-pipelines/livetest.yml new file mode 100644 index 000000000..dcd1e3d29 --- /dev/null +++ b/.azure-pipelines/livetest.yml @@ -0,0 +1,7 @@ +jobs: + - job: + pool: + vmImage: 'Ubuntu 18.04' + steps: + - template: steps/init_workspace.yml + - template: steps/live_test.yml diff --git a/.azure-pipelines/steps/init_workspace.yml b/.azure-pipelines/steps/init_workspace.yml index 52a300779..03f1766d3 100644 --- a/.azure-pipelines/steps/init_workspace.yml +++ b/.azure-pipelines/steps/init_workspace.yml @@ -10,4 +10,4 @@ steps: go version curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin v1.23.6 golangci-lint --version - displayName: 'Install Dependencies' \ No newline at end of file + displayName: 'Install Dependencies' diff --git a/.azure-pipelines/steps/live_test.yml b/.azure-pipelines/steps/live_test.yml new file mode 100644 index 000000000..7875fdbb1 --- /dev/null +++ b/.azure-pipelines/steps/live_test.yml @@ -0,0 +1,16 @@ +steps: + - script: | + go test -count=1 -v ./keyvault/ + displayName: 'Live Test' + env: + AZURE_AUTH_LOCATION: $(AZURE_AUTH_LOCATION) + AZURE_BASE_GROUP_NAME: $(AZURE_BASE_GROUP_NAME) + AZURE_CLIENT_ID: $(AZURE_CLIENT_ID) + AZURE_CLIENT_SECRET: $(AZURE_CLIENT_SECRET) + AZURE_TENANT_ID: $(AZURE_TENANT_ID) + AZURE_SUBSCRIPTION_ID: $(AZURE_SUBSCRIPTION_ID) + AZURE_LOCATION_DEFAULT: $(AZURE_LOCATION_DEFAULT) + AZURE_SAMPLES_KEEP_RESOURCES: $(AZURE_SAMPLES_KEEP_RESOURCES) + AZURE_USE_DEVICEFLOW: $(AZURE_USE_DEVICEFLOW) + + diff --git a/azure-pipelines.yml b/azure-pipelines.yml index e8d99b6cd..c6825cd5d 100644 --- a/azure-pipelines.yml +++ b/azure-pipelines.yml @@ -11,13 +11,8 @@ stages: - stage: LintAndBuild jobs: - template: .azure-pipelines/lint_and_build.yml - + - stage: LiveTest condition: and(succeeded(), in(variables['Build.Reason'], 'IndividualCI')) jobs: - - job: - steps: - # the live test script steps. - - script: | - echo 'live test' - displayName: 'Live Test' + - template: .azure-pipelines/livetest.yml diff --git a/internal/config/env.go b/internal/config/env.go index 9ae21db8b..e4ad49209 100644 --- a/internal/config/env.go +++ b/internal/config/env.go @@ -1,37 +1,29 @@ package config import ( - "fmt" "log" + "os" "strconv" - - "github.com/Azure/go-autorest/autorest/azure" - "github.com/gobuffalo/envy" ) // ParseEnvironment loads a sibling `.env` file then looks through all environment // variables to set global configuration. func ParseEnvironment() error { - if err := envy.Load(); err != nil { - return err - } - azureEnv, _ := azure.EnvironmentFromName("AzurePublicCloud") // shouldn't fail - authorizationServerURL = azureEnv.ActiveDirectoryEndpoint // AZURE_GROUP_NAME and `config.GroupName()` are deprecated. // Use AZURE_BASE_GROUP_NAME and `config.GenerateGroupName()` instead. - groupName = envy.Get("AZURE_GROUP_NAME", "azure-go-samples") - baseGroupName = envy.Get("AZURE_BASE_GROUP_NAME", groupName) + groupName = os.Getenv("AZURE_GROUP_NAME") + baseGroupName = os.Getenv("AZURE_BASE_GROUP_NAME") - locationDefault = envy.Get("AZURE_LOCATION_DEFAULT", "westus2") + locationDefault = os.Getenv("AZURE_LOCATION_DEFAULT") var err error - useDeviceFlow, err = strconv.ParseBool(envy.Get("AZURE_USE_DEVICEFLOW", "0")) + useDeviceFlow, err = strconv.ParseBool(os.Getenv("AZURE_USE_DEVICEFLOW")) if err != nil { log.Printf("invalid value specified for AZURE_USE_DEVICEFLOW, disabling\n") useDeviceFlow = false } - keepResources, err = strconv.ParseBool(envy.Get("AZURE_SAMPLES_KEEP_RESOURCES", "0")) + keepResources, err = strconv.ParseBool(os.Getenv("AZURE_SAMPLES_KEEP_RESOURCES")) if err != nil { log.Printf("invalid value specified for AZURE_SAMPLES_KEEP_RESOURCES, discarding\n") keepResources = false @@ -39,28 +31,16 @@ func ParseEnvironment() error { // these must be provided by environment // clientID - clientID, err = envy.MustGet("AZURE_CLIENT_ID") - if err != nil { - return fmt.Errorf("expected env vars not provided: %+v", err) - } + clientID = os.Getenv("AZURE_CLIENT_ID") // clientSecret - clientSecret, err = envy.MustGet("AZURE_CLIENT_SECRET") - if err != nil && !useDeviceFlow { // don't need a secret for device flow - return fmt.Errorf("expected env vars not provided: %+v", err) - } + clientSecret = os.Getenv("AZURE_CLIENT_SECRET") // tenantID (AAD) - tenantID, err = envy.MustGet("AZURE_TENANT_ID") - if err != nil { - return fmt.Errorf("expected env vars not provided: %+v", err) - } + tenantID = os.Getenv("AZURE_TENANT_ID") // subscriptionID (ARM) - subscriptionID, err = envy.MustGet("AZURE_SUBSCRIPTION_ID") - if err != nil { - return fmt.Errorf("expected env vars not provided: %+v", err) - } + subscriptionID = os.Getenv("AZURE_SUBSCRIPTION_ID") return nil } diff --git a/keyvault/keyvault_test.go b/keyvault/keyvault_test.go index abd545898..c2421cf53 100644 --- a/keyvault/keyvault_test.go +++ b/keyvault/keyvault_test.go @@ -53,6 +53,7 @@ func ExampleSetVaultPermissions() { if err != nil { util.LogAndPanic(err) } + util.PrintAndLog("resource group created") _, err = CreateVault(ctx, kvName) if err != nil { @@ -73,6 +74,7 @@ func ExampleSetVaultPermissions() { util.PrintAndLog("created key") // Output: + // resource group created // vault created // set vault permissions // created key