Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[keyvault] update azadmin test #21305

Merged
merged 1 commit into from
Aug 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion sdk/security/keyvault/azadmin/assets.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
"AssetsRepo": "Azure/azure-sdk-assets",
"AssetsRepoPrefixPath": "go",
"TagPrefix": "go/security/keyvault/azadmin",
"Tag": "go/security/keyvault/azadmin_6b57e62028"
"Tag": "go/security/keyvault/azadmin_1bb2140fc8"
}
63 changes: 22 additions & 41 deletions sdk/security/keyvault/azadmin/backup/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,30 +35,6 @@ var (
)

func TestMain(m *testing.M) {
if recording.GetRecordMode() != recording.PlaybackMode {
hsmURL = os.Getenv("AZURE_MANAGEDHSM_URL")
blobURL = "https://" + os.Getenv("BLOB_STORAGE_ACCOUNT_NAME") + ".blob." + os.Getenv("KEYVAULT_STORAGE_ENDPOINT_SUFFIX") + "/" + os.Getenv("BLOB_CONTAINER_NAME")
token = os.Getenv("BLOB_STORAGE_SAS_TOKEN")
}
if hsmURL == "" {
if recording.GetRecordMode() != recording.PlaybackMode {
panic("no value for AZURE_MANAGEDHSM_URL")
}
hsmURL = fakeHsmURL
}
if blobURL == "" {
if recording.GetRecordMode() != recording.PlaybackMode {
panic("no value for blob url")
}
blobURL = fakeBlobURL
}
if token == "" {
if recording.GetRecordMode() != recording.PlaybackMode {
panic("no value for BLOB_STORAGE_SAS_TOKEN")
}
token = fakeToken
}

err := recording.ResetProxy(nil)
if err != nil {
panic(err)
Expand All @@ -74,25 +50,16 @@ func TestMain(m *testing.M) {
panic(err)
}
}

hsmURL = getEnvVar("AZURE_MANAGEDHSM_URL", fakeHsmURL)
blobURL = getEnvVar("BLOB_CONTAINER_URL", fakeBlobURL)
token = getEnvVar("BLOB_STORAGE_SAS_TOKEN", fakeToken)

if recording.GetRecordMode() == recording.RecordingMode {
err := recording.AddGeneralRegexSanitizer(fakeHsmURL, hsmURL, nil)
if err != nil {
panic(err)
}
err = recording.AddGeneralRegexSanitizer(fakeBlobURL, blobURL, nil)
if err != nil {
panic(err)
}
err = recording.AddBodyRegexSanitizer(fakeToken, `sv=[^"]*`, nil)
if err != nil {
panic(err)
}
defer func() {
err := recording.ResetProxy(nil)
if err != nil {
panic(err)
}
}()
}
code := m.Run()
os.Exit(code)
Expand Down Expand Up @@ -122,12 +89,26 @@ func startBackupTest(t *testing.T) (*backup.Client, backup.SASTokenParameters) {
return client, sasToken
}

func getEnvVar(lookupValue string, fakeValue string) string {
envVar := fakeValue
if recording.GetRecordMode() != recording.PlaybackMode {
envVar = lookupEnvVar(lookupValue)
}
if recording.GetRecordMode() == recording.RecordingMode {
err := recording.AddGeneralRegexSanitizer(fakeValue, envVar, nil)
if err != nil {
panic(err)
}
}
return envVar
}

func lookupEnvVar(s string) string {
ret, ok := os.LookupEnv(s)
if !ok {
v := os.Getenv(s)
if v == "" {
panic(fmt.Sprintf("Could not find env var: '%s'", s))
}
return ret
return v
}

type FakeCredential struct{}
Expand Down
46 changes: 21 additions & 25 deletions sdk/security/keyvault/azadmin/rbac/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,6 @@ var (
)

func TestMain(m *testing.M) {
if recording.GetRecordMode() != recording.PlaybackMode {
hsmURL = os.Getenv("AZURE_MANAGEDHSM_URL")
}
if hsmURL == "" {
if recording.GetRecordMode() != recording.PlaybackMode {
panic("no value for AZURE_MANAGEDHSM_URL")
}
hsmURL = fakeHsmURL
}

err := recording.ResetProxy(nil)
if err != nil {
panic(err)
Expand All @@ -56,18 +46,9 @@ func TestMain(m *testing.M) {
panic(err)
}
}
if recording.GetRecordMode() == recording.RecordingMode {
err := recording.AddGeneralRegexSanitizer(fakeHsmURL, hsmURL, nil)
if err != nil {
panic(err)
}
defer func() {
err := recording.ResetProxy(nil)
if err != nil {
panic(err)
}
}()
}

hsmURL = getEnvVar("AZURE_MANAGEDHSM_URL", fakeHsmURL)

code := m.Run()
os.Exit(code)
}
Expand All @@ -91,12 +72,27 @@ func startAccessControlTest(t *testing.T) *rbac.Client {
return client
}

func getEnvVar(lookupValue string, fakeValue string) string {
envVar := fakeValue
if recording.GetRecordMode() != recording.PlaybackMode {
envVar = lookupEnvVar(lookupValue)
}
if recording.GetRecordMode() == recording.RecordingMode {
err := recording.AddGeneralRegexSanitizer(fakeValue, envVar, nil)
if err != nil {
panic(err)
}
}

return envVar
}

func lookupEnvVar(s string) string {
ret, ok := os.LookupEnv(s)
if !ok {
v := os.Getenv(s)
if v == "" {
panic(fmt.Sprintf("Could not find env var: '%s'", s))
}
return ret
return v
}

type FakeCredential struct{}
Expand Down
2 changes: 1 addition & 1 deletion sdk/security/keyvault/azadmin/settings/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,6 @@ func TestUpdateSetting_InvalidSettingName(t *testing.T) {
require.Nil(t, res.Value)
var httpErr *azcore.ResponseError
require.ErrorAs(t, err, &httpErr)
require.Equal(t, "Nocontentprovided", httpErr.ErrorCode)
require.Equal(t, "InvalidSetting", httpErr.ErrorCode)
require.Equal(t, 400, httpErr.StatusCode)
}
45 changes: 20 additions & 25 deletions sdk/security/keyvault/azadmin/settings/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,6 @@ var (
)

func TestMain(m *testing.M) {
if recording.GetRecordMode() != recording.PlaybackMode {
hsmURL = os.Getenv("AZURE_MANAGEDHSM_URL")
}
if hsmURL == "" {
if recording.GetRecordMode() != recording.PlaybackMode {
panic("no value for AZURE_MANAGEDHSM_URL")
}
hsmURL = fakeHsmURL
}

err := recording.ResetProxy(nil)
if err != nil {
panic(err)
Expand All @@ -56,18 +46,9 @@ func TestMain(m *testing.M) {
panic(err)
}
}
if recording.GetRecordMode() == recording.RecordingMode {
err := recording.AddGeneralRegexSanitizer(fakeHsmURL, hsmURL, nil)
if err != nil {
panic(err)
}
defer func() {
err := recording.ResetProxy(nil)
if err != nil {
panic(err)
}
}()
}

hsmURL = getEnvVar("AZURE_MANAGEDHSM_URL", fakeHsmURL)

code := m.Run()
os.Exit(code)
}
Expand All @@ -91,12 +72,26 @@ func startSettingsTest(t *testing.T) *settings.Client {
return client
}

func getEnvVar(lookupValue string, fakeValue string) string {
envVar := fakeValue
if recording.GetRecordMode() != recording.PlaybackMode {
envVar = lookupEnvVar(lookupValue)
}
if recording.GetRecordMode() == recording.RecordingMode {
err := recording.AddGeneralRegexSanitizer(fakeValue, envVar, nil)
if err != nil {
panic(err)
}
}
return envVar
}

func lookupEnvVar(s string) string {
ret, ok := os.LookupEnv(s)
if !ok {
v := os.Getenv(s)
if v == "" {
panic(fmt.Sprintf("Could not find env var: '%s'", s))
}
return ret
return v
}

type FakeCredential struct{}
Expand Down
4 changes: 4 additions & 0 deletions sdk/security/keyvault/azadmin/test-resources.json
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,10 @@
"AZURE_KEYVAULT_ATTESTATION_URL": {
"type": "string",
"value": "[format('https://{0}/', reference(variables('attestationSite')).defaultHostName)]"
},
"BLOB_CONTAINER_URL": {
"type": "string",
"value": "[concat(reference(resourceId('Microsoft.Storage/storageAccounts', variables('primaryAccountName'))).primaryEndpoints.blob, variables('blobContainerName'))]"
}
}
}