Skip to content

Commit b243120

Browse files
committed
added test
1 parent ea45848 commit b243120

File tree

2 files changed

+186
-0
lines changed

2 files changed

+186
-0
lines changed

ibm/acctest/acctest.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,8 @@ var (
104104
InstanceName string
105105
InstanceProfileName string
106106
InstanceProfileNameUpdate string
107+
ISCatalogImageName string
108+
ISBootSnapshotID string
107109
IsBareMetalServerProfileName string
108110
IsBareMetalServerImage string
109111
IsBareMetalServerImage2 string
@@ -972,6 +974,18 @@ func init() {
972974
fmt.Println("[INFO] Set the environment variable SL_INSTANCE_PROFILE_UPDATE for testing ibm_is_instance resource else it is set to default value 'cx2-4x8'")
973975
}
974976

977+
ISCatalogImageName = os.Getenv("IS_CATALOG_IMAGE_NAME")
978+
if ISCatalogImageName == "" {
979+
ISCatalogImageName = "bx2-metal-96x384" // for next gen infrastructure
980+
fmt.Println("[INFO] Set the environment variable IS_CATALOG_IMAGE_NAME for testing ibm_is_instance_template resource else it is set to default value 'bx2-metal-96x384'")
981+
}
982+
983+
ISBootSnapshotID = os.Getenv("IS_BOOT_SNAPSHOT_ID")
984+
if ISBootSnapshotID == "" {
985+
ISBootSnapshotID = "bx2-metal-96x384" // for next gen infrastructure
986+
fmt.Println("[INFO] Set the environment variable IS_CATALOG_IMAGE_NAME for testing ibm_is_instance_template resource else it is set to default value 'bx2-metal-96x384'")
987+
}
988+
975989
IsBareMetalServerProfileName = os.Getenv("IS_BARE_METAL_SERVER_PROFILE")
976990
if IsBareMetalServerProfileName == "" {
977991
IsBareMetalServerProfileName = "bx2-metal-96x384" // for next gen infrastructure

ibm/service/vpc/resource_ibm_is_instance_template_test.go

Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1175,3 +1175,175 @@ func testAccCheckIBMISInstanceTemplateWithBootBandwidth(vpcName, subnetName, ssh
11751175
`, vpcName, subnetName, acc.ISZoneName, sshKeyName, publicKey, templateName, acc.IsImage, userTag, bandwidth, acc.ISZoneName)
11761176

11771177
}
1178+
1179+
func testAccCheckIBMISInstanceTemplateComprehensiveConfig(vpcName, subnetName, sshKeyName, publicKey, templateNameImg, templateNameSnapshot, templateNameCatalog string) string {
1180+
return fmt.Sprintf(`
1181+
resource "ibm_is_vpc" "vpc2" {
1182+
name = "%s"
1183+
}
1184+
1185+
resource "ibm_is_subnet" "subnet" {
1186+
name = "%s"
1187+
vpc = ibm_is_vpc.vpc2.id
1188+
zone = "%s"
1189+
total_ipv4_address_count = 64
1190+
}
1191+
1192+
resource "ibm_is_ssh_key" "sshkey" {
1193+
name = "%s"
1194+
public_key = "%s"
1195+
}
1196+
1197+
data "ibm_is_image" "catalog_image" {
1198+
name = "%s"
1199+
}
1200+
1201+
# Template 1: From Image
1202+
resource "ibm_is_instance_template" "template_from_image" {
1203+
name = "%s"
1204+
image = "%s"
1205+
profile = "cx2-2x4"
1206+
1207+
primary_network_interface {
1208+
subnet = ibm_is_subnet.subnet.id
1209+
}
1210+
1211+
vpc = ibm_is_vpc.vpc.id
1212+
zone = "%s"
1213+
keys = [ibm_is_ssh_key.sshkey.id]
1214+
1215+
user_data = base64encode(<<-EOF
1216+
#!/bin/bash
1217+
apt-get update
1218+
apt-get install -y nginx
1219+
systemctl start nginx
1220+
systemctl enable nginx
1221+
echo "Template from Image" > /var/www/html/index.html
1222+
EOF
1223+
)
1224+
}
1225+
1226+
# Template 2: From Snapshot
1227+
resource "ibm_is_instance_template" "template_from_snapshot" {
1228+
name = "%s"
1229+
profile = "cx2-2x4"
1230+
1231+
boot_volume {
1232+
source_snapshot = "%s"
1233+
}
1234+
1235+
primary_network_interface {
1236+
subnet = ibm_is_subnet.subnet.id
1237+
}
1238+
1239+
vpc = ibm_is_vpc.vpc.id
1240+
zone = "%s"
1241+
keys = [ibm_is_ssh_key.sshkey.id]
1242+
1243+
user_data = base64encode(<<-EOF
1244+
#!/bin/bash
1245+
apt-get update
1246+
apt-get install -y nginx
1247+
systemctl start nginx
1248+
systemctl enable nginx
1249+
echo "Template from Snapshot" > /var/www/html/index.html
1250+
EOF
1251+
)
1252+
1253+
depends_on = [ibm_is_snapshot.test_snapshot]
1254+
}
1255+
1256+
# Template 3: From Catalog Offering
1257+
resource "ibm_is_instance_template" "template_from_catalog" {
1258+
name = "%s"
1259+
profile = "cx2-2x4"
1260+
1261+
catalog_offering {
1262+
version_crn = data.ibm_is_image.catalog_image.catalog_offering.0.version.0.crn
1263+
}
1264+
1265+
primary_network_interface {
1266+
subnet = ibm_is_subnet.subnet.id
1267+
}
1268+
1269+
vpc = ibm_is_vpc.vpc.id
1270+
zone = "%s"
1271+
keys = [ibm_is_ssh_key.sshkey.id]
1272+
1273+
user_data = base64encode(<<-EOF
1274+
#!/bin/bash
1275+
apt-get update
1276+
apt-get install -y nginx
1277+
systemctl start nginx
1278+
systemctl enable nginx
1279+
echo "Template from Catalog" > /var/www/html/index.html
1280+
EOF
1281+
)
1282+
}
1283+
`, vpcName, subnetName, acc.ISZoneName, sshKeyName, publicKey, acc.ISCatalogImageName, acc.ISZoneName, templateNameImg, acc.ISZoneName, templateNameSnapshot, acc.ISBootSnapshotID, acc.ISZoneName, templateNameCatalog, acc.ISZoneName)
1284+
}
1285+
1286+
func TestAccIBMISInstanceTemplate_comprehensive(t *testing.T) {
1287+
randInt := acctest.RandIntRange(10, 100)
1288+
1289+
publicKey := strings.TrimSpace(`
1290+
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQDVtuCfWKVGKaRmaRG6JQZY8YdxnDgGzVOK93IrV9R5Hl0JP1oiLLWlZQS2reAKb8lBqyDVEREpaoRUDjqDqXG8J/kR42FKN51su914pjSBc86wJ02VtT1Wm1zRbSg67kT+g8/T1jCgB5XBODqbcICHVP8Z1lXkgbiHLwlUrbz6OZkGJHo/M/kD1Eme8lctceIYNz/Ilm7ewMXZA4fsidpto9AjyarrJLufrOBl4MRVcZTDSJ7rLP982aHpu9pi5eJAjOZc7Og7n4ns3NFppiCwgVMCVUQbN5GBlWhZ1OsT84ZiTf+Zy8ew+Yg5T7Il8HuC7loWnz+esQPf0s3xhC/kTsGgZreIDoh/rxJfD67wKXetNSh5RH/n5BqjaOuXPFeNXmMhKlhj9nJ8scayx/wsvOGuocEIkbyJSLj3sLUU403OafgatEdnJOwbqg6rUNNF5RIjpJpL7eEWlKIi1j9LyhmPJ+fEO7TmOES82VpCMHpLbe4gf/MhhJ/Xy8DKh9s= root@ffd8363b1226
1291+
`)
1292+
1293+
vpcName := fmt.Sprintf("tf-testvpc%d", randInt)
1294+
subnetName := fmt.Sprintf("tf-testsubnet%d", randInt)
1295+
templateNameImg := fmt.Sprintf("tf-testtemplate-img%d", randInt)
1296+
templateNameSnapshot := fmt.Sprintf("tf-testtemplate-snapshot%d", randInt)
1297+
templateNameCatalog := fmt.Sprintf("tf-testtemplate-catalog%d", randInt)
1298+
sshKeyName := fmt.Sprintf("tf-testsshkey%d", randInt)
1299+
1300+
resource.Test(t, resource.TestCase{
1301+
PreCheck: func() { acc.TestAccPreCheck(t) },
1302+
Providers: acc.TestAccProviders,
1303+
CheckDestroy: testAccCheckIBMISInstanceTemplateDestroy,
1304+
Steps: []resource.TestStep{
1305+
{
1306+
Config: testAccCheckIBMISInstanceTemplateComprehensiveConfig(vpcName, subnetName, sshKeyName, publicKey, templateNameImg, templateNameSnapshot, templateNameCatalog),
1307+
Check: resource.ComposeTestCheckFunc(
1308+
// Check Template from Image
1309+
resource.TestCheckResourceAttr(
1310+
"ibm_is_instance_template.template_from_image", "name", templateNameImg),
1311+
resource.TestCheckResourceAttrSet(
1312+
"ibm_is_instance_template.template_from_image", "image"),
1313+
resource.TestCheckResourceAttr(
1314+
"ibm_is_instance_template.template_from_image", "profile", "cx2-2x4"),
1315+
resource.TestCheckResourceAttr(
1316+
"ibm_is_instance_template.template_from_image", "zone", acc.ISZone),
1317+
1318+
// Check Template from Snapshot
1319+
resource.TestCheckResourceAttr(
1320+
"ibm_is_instance_template.template_from_snapshot", "name", templateNameSnapshot),
1321+
resource.TestCheckResourceAttrSet(
1322+
"ibm_is_instance_template.template_from_snapshot", "boot_volume.0.source_snapshot"),
1323+
resource.TestCheckResourceAttr(
1324+
"ibm_is_instance_template.template_from_snapshot", "profile", "cx2-2x4"),
1325+
resource.TestCheckResourceAttr(
1326+
"ibm_is_instance_template.template_from_snapshot", "zone", acc.ISZone),
1327+
1328+
// Check Template from Catalog
1329+
resource.TestCheckResourceAttr(
1330+
"ibm_is_instance_template.template_from_catalog", "name", templateNameCatalog),
1331+
resource.TestCheckResourceAttrSet(
1332+
"ibm_is_instance_template.template_from_catalog", "catalog_offering.0.version_crn"),
1333+
resource.TestCheckResourceAttr(
1334+
"ibm_is_instance_template.template_from_catalog", "profile", "cx2-2x4"),
1335+
resource.TestCheckResourceAttr(
1336+
"ibm_is_instance_template.template_from_catalog", "zone", acc.ISZone),
1337+
1338+
// Check common attributes for all templates
1339+
resource.TestCheckResourceAttrSet(
1340+
"ibm_is_instance_template.template_from_image", "vpc"),
1341+
resource.TestCheckResourceAttrSet(
1342+
"ibm_is_instance_template.template_from_snapshot", "vpc"),
1343+
resource.TestCheckResourceAttrSet(
1344+
"ibm_is_instance_template.template_from_catalog", "vpc"),
1345+
),
1346+
},
1347+
},
1348+
})
1349+
}

0 commit comments

Comments
 (0)