參數檔案的測試案例

本文說明針對參數檔案,使用範本測試工具組執行的測試。 例如名為 azuredeploy.parameters.json 的檔案。 這些範例包括測試名稱和測試通過失敗的程式碼範例。 如需如何執行測試或如何執行特定測試的詳細資訊,請參閱測試參數

此工具組包含 Azure Resource Manager 範本 (ARM 範本) 的測試案例,以及名為 azuredeploy.jsonmaintemplate.json 的主要範本檔案。

使用有效的 contentVersion

測試名稱:DeploymentParameters 應該有 ContentVersion

contentVersion 必須包含格式為 1.0.0.0 的字串,並且僅使用數字。

由於遺漏 contentVersion,因此下列範例會失敗

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentParameters.json#",
  "parameters": {
    "stgAcctName": {
      "value": "demostorage01"
    }
  }
}

由於 contentVersion 不是字串,因此下列範例會失敗

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentParameters.json#",
  "contentVersion": {},
  "parameters": {
    "stgAcctName": {
      "value": "demostorage01"
    }
  }
}

下列範例會通過

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentParameters.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "stgAcctName": {
      "value": "demostorage01"
    }
  }
}

檔案必須包含參數

測試名稱:DeploymentParameters 應該有參數

參數檔案必須包含 parameters 區段。

下列範例會失敗

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentParameters.json#",
  "contentVersion": "1.0.0.0",
}

下列範例會通過

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentParameters.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "stgAcctName": {
      "value": "demostorage01"
    }
  }
}

使用有效的結構描述版本

測試名稱:DeploymentParameters 應該有結構描述

參數檔案必須包含有效的結構描述版本。

參數檔案有兩個有效的結構描述版本:

  • https://schema.management.azure.com/schemas/2015-01-01/deploymentParameters.json#
  • https://schema.management.azure.com/schemas/2019-04-01/deploymentParameters.json#

下列範例會失敗

{
  "$schema": "https://schema.management.azure.com/schemas/2021-01-01/deploymentParameters.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "stgAcctName": {
      "value": "demostorage01"
    }
  }
}

下列範例會通過

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentParameters.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "stgAcctName": {
      "value": "demostorage01"
    }
  }
}

參數必須包含值

測試名稱:DeploymentParameters 應該有值

參數必須包含 valuereference。 針對密碼這類祕密,金鑰保存庫會在參數檔案中使用 reference。 如需詳細資訊,請參閱在部署期間使用 Azure Key Vault 以傳遞安全的參數值

由於 stgAcctName 沒有 value,因此下列範例會失敗

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentParameters.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "stgAcctName": {}
  }
}

下列範例會通過

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentParameters.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "stgAcctName": {
      "value": "demostorage01"
    }
  }
}

下一步