github.com/mitchellh/packer@v1.3.2/builder/azure/arm/azure_error_response_test.go (about)

     1  package arm
     2  
     3  import (
     4  	"strings"
     5  	"testing"
     6  
     7  	"github.com/approvals/go-approval-tests"
     8  	"github.com/hashicorp/packer/common/json"
     9  )
    10  
    11  const AzureErrorSimple = `{"error":{"code":"ResourceNotFound","message":"The Resource 'Microsoft.Compute/images/PackerUbuntuImage' under resource group 'packer-test00' was not found."}}`
    12  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}"}]}}`
    13  
    14  func TestAzureErrorSimpleShouldUnmarshal(t *testing.T) {
    15  	var azureErrorResponse azureErrorResponse
    16  	err := json.Unmarshal([]byte(AzureErrorSimple), &azureErrorResponse)
    17  	if err != nil {
    18  		t.Fatal(err)
    19  	}
    20  
    21  	if azureErrorResponse.ErrorDetails.Code != "ResourceNotFound" {
    22  		t.Errorf("Error.Code")
    23  	}
    24  	if azureErrorResponse.ErrorDetails.Message != "The Resource 'Microsoft.Compute/images/PackerUbuntuImage' under resource group 'packer-test00' was not found." {
    25  		t.Errorf("Error.Message")
    26  	}
    27  }
    28  
    29  func TestAzureErrorNestedShouldUnmarshal(t *testing.T) {
    30  	var azureError azureErrorResponse
    31  	err := json.Unmarshal([]byte(AzureErrorNested), &azureError)
    32  	if err != nil {
    33  		t.Fatal(err)
    34  	}
    35  
    36  	if azureError.ErrorDetails.Code != "DeploymentFailed" {
    37  		t.Errorf("Error.Code")
    38  	}
    39  	if !strings.HasPrefix(azureError.ErrorDetails.Message, "At least one resource deployment operation failed") {
    40  		t.Errorf("Error.Message")
    41  	}
    42  }
    43  
    44  func TestAzureErrorEmptyShouldFormat(t *testing.T) {
    45  	var aer azureErrorResponse
    46  	s := aer.Error()
    47  
    48  	if s != "" {
    49  		t.Fatalf("Expected \"\", but got %s", aer.Error())
    50  	}
    51  }
    52  
    53  func TestAzureErrorSimpleShouldFormat(t *testing.T) {
    54  	var azureErrorResponse azureErrorResponse
    55  	err := json.Unmarshal([]byte(AzureErrorSimple), &azureErrorResponse)
    56  	if err != nil {
    57  		t.Fatal(err)
    58  	}
    59  
    60  	err = approvaltests.VerifyString(t, azureErrorResponse.Error())
    61  	if err != nil {
    62  		t.Fatal(err)
    63  	}
    64  }
    65  
    66  func TestAzureErrorNestedShouldFormat(t *testing.T) {
    67  	var azureErrorResponse azureErrorResponse
    68  	err := json.Unmarshal([]byte(AzureErrorNested), &azureErrorResponse)
    69  	if err != nil {
    70  		t.Fatal(err)
    71  	}
    72  
    73  	err = approvaltests.VerifyString(t, azureErrorResponse.Error())
    74  	if err != nil {
    75  		t.Fatal(err)
    76  	}
    77  }