github.com/mmcquillan/packer@v1.1.1-0.20171009221028-c85cf0483a5d/builder/azure/arm/azure_error_response_test.go (about) 1 package arm 2 3 import ( 4 "github.com/approvals/go-approval-tests" 5 "github.com/hashicorp/packer/common/json" 6 "strings" 7 "testing" 8 ) 9 10 const AzureErrorSimple = `{"error":{"code":"ResourceNotFound","message":"The Resource 'Microsoft.Compute/images/PackerUbuntuImage' under resource group 'packer-test00' was not found."}}` 11 const AzureErrorNested = `{"status":"Failed","error":{"code":"DeploymentFailed","message":"At least one resource deployment operation failed. Please list deployment operations for details. Please see https://aka.ms/arm-debug for usage details.","details":[{"code":"BadRequest","message":"{\r\n \"error\": {\r\n \"code\": \"InvalidRequestFormat\",\r\n \"message\": \"Cannot parse the request.\",\r\n \"details\": [\r\n {\r\n \"code\": \"InvalidJson\",\r\n \"message\": \"Error converting value \\\"playground\\\" to type 'Microsoft.WindowsAzure.Networking.Nrp.Frontend.Contract.Csm.Public.IpAllocationMethod'. Path 'properties.publicIPAllocationMethod', line 1, position 130.\"\r\n }\r\n ]\r\n }\r\n}"}]}}` 12 13 func TestAzureErrorSimpleShouldUnmarshal(t *testing.T) { 14 var azureErrorReponse azureErrorResponse 15 err := json.Unmarshal([]byte(AzureErrorSimple), &azureErrorReponse) 16 if err != nil { 17 t.Fatal(err) 18 } 19 20 if azureErrorReponse.ErrorDetails.Code != "ResourceNotFound" { 21 t.Errorf("Error.Code") 22 } 23 if azureErrorReponse.ErrorDetails.Message != "The Resource 'Microsoft.Compute/images/PackerUbuntuImage' under resource group 'packer-test00' was not found." { 24 t.Errorf("Error.Message") 25 } 26 } 27 28 func TestAzureErrorNestedShouldUnmarshal(t *testing.T) { 29 var azureError azureErrorResponse 30 err := json.Unmarshal([]byte(AzureErrorNested), &azureError) 31 if err != nil { 32 t.Fatal(err) 33 } 34 35 if azureError.ErrorDetails.Code != "DeploymentFailed" { 36 t.Errorf("Error.Code") 37 } 38 if !strings.HasPrefix(azureError.ErrorDetails.Message, "At least one resource deployment operation failed") { 39 t.Errorf("Error.Message") 40 } 41 } 42 43 func TestAzureErrorEmptyShouldFormat(t *testing.T) { 44 var aer azureErrorResponse 45 s := aer.Error() 46 47 if s != "" { 48 t.Fatalf("Expected \"\", but got %s", aer.Error()) 49 } 50 } 51 52 func TestAzureErrorSimpleShouldFormat(t *testing.T) { 53 var azureErrorReponse azureErrorResponse 54 err := json.Unmarshal([]byte(AzureErrorSimple), &azureErrorReponse) 55 if err != nil { 56 t.Fatal(err) 57 } 58 59 err = approvaltests.VerifyString(t, azureErrorReponse.Error()) 60 if err != nil { 61 t.Fatal(err) 62 } 63 } 64 65 func TestAzureErrorNestedShouldFormat(t *testing.T) { 66 var azureErrorReponse azureErrorResponse 67 err := json.Unmarshal([]byte(AzureErrorNested), &azureErrorReponse) 68 if err != nil { 69 t.Fatal(err) 70 } 71 72 err = approvaltests.VerifyString(t, azureErrorReponse.Error()) 73 if err != nil { 74 t.Fatal(err) 75 } 76 }