-
Notifications
You must be signed in to change notification settings - Fork 53
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
Draft: Add tests for importing templates #357
Changes from 1 commit
97b16d2
474f3c3
a2bcb88
149703c
039784a
5de5119
e0d077e
29981c6
796e718
b1cf5b7
d229fa4
8c1518b
c8b1100
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -424,9 +424,49 @@ func resourceOpennebulaTemplateReadCustom(ctx context.Context, d *schema.Resourc | |
d.Set("gid", tpl.GID) | ||
d.Set("uname", tpl.UName) | ||
d.Set("gname", tpl.GName) | ||
d.Set("group", tpl.GName) | ||
d.Set("reg_time", tpl.RegTime) | ||
d.Set("description", "") | ||
d.Set("sched_requirements", "") | ||
d.Set("permissions", permissionsUnixString(*tpl.Permissions)) | ||
|
||
getTags := make(map[string]interface{}, 0) | ||
for k, v := range pairsToMap(tpl.Template.Template) { | ||
if k != "MEMORY" && k != "CPU" && k != "VCPU" && k != "VROUTER" && k != "DESCRIPTION" && k != "SCHED_REQUIREMENTS" { | ||
getTags[strings.ToLower(k)] = v | ||
} | ||
} | ||
d.Set("tags", getTags) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we don't need this, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In the method |
||
|
||
for _, value := range tpl.Template.Elements { | ||
if pair, ok := value.(*dyn.Pair); ok { | ||
switch pair.XMLName.Local { | ||
case "DESCRIPTION", "SCHED_REQUIREMENTS": | ||
d.Set(strings.ToLower(pair.XMLName.Local), pair.Value) | ||
} | ||
} | ||
if vector, ok := value.(*dyn.Vector); ok { | ||
getOS := make(map[string]interface{}, 0) | ||
switch vector.XMLName.Local { | ||
case "OS", "FEATURES", "GRAPHICS", "CPU_MODEL": | ||
for _, pair := range vector.Pairs { | ||
getOS[strings.ToLower(pair.XMLName.Local)] = pair.Value | ||
} | ||
d.Set(strings.Replace(strings.ToLower(vector.XMLName.Local), "_", "", -1), append(make([]interface{}, 0), getOS)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please don't call to much methods in the same line this make you code more difficult to read, it's not a problem to add more code lines to make the code more readable/maintainable. However you get how to read There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This long line has been separated in multiple lines, and an explanation has been added. |
||
case "CONTEXT": | ||
for _, pair := range vector.Pairs { | ||
switch pair.XMLName.Local { | ||
case "SET_HOSTNAME", "NETWORK": | ||
getOS[pair.XMLName.Local] = pair.Value | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Reuse There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for the suggestion, now the name |
||
default: | ||
getOS[strings.ToLower(pair.XMLName.Local)] = pair.Value | ||
} | ||
} | ||
d.Set(strings.ToLower(vector.XMLName.Local), getOS) | ||
} | ||
} | ||
} | ||
|
||
err = flattenTemplateDisks(d, &tpl.Template) | ||
if err != nil { | ||
diags = append(diags, diag.Diagnostic{ | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,13 +25,18 @@ func TestAccTemplate(t *testing.T) { | |
resource.TestCheckResourceAttr("opennebula_template.template", "permissions", "660"), | ||
resource.TestCheckResourceAttr("opennebula_template.template", "group", "oneadmin"), | ||
resource.TestCheckResourceAttr("opennebula_template.template", "cpu", "0.5"), | ||
resource.TestCheckResourceAttr("opennebula_template.template", "vcpu", "1"), | ||
resource.TestCheckResourceAttr("opennebula_template.template", "memory", "512"), | ||
resource.TestCheckResourceAttr("opennebula_template.template", "graphics.#", "1"), | ||
resource.TestCheckResourceAttr("opennebula_template.template", "graphics.0.keymap", "en-us"), | ||
resource.TestCheckResourceAttr("opennebula_template.template", "graphics.0.listen", "0.0.0.0"), | ||
resource.TestCheckResourceAttr("opennebula_template.template", "graphics.0.type", "VNC"), | ||
resource.TestCheckResourceAttr("opennebula_template.template", "os.#", "1"), | ||
resource.TestCheckResourceAttr("opennebula_template.template", "os.0.arch", "x86_64"), | ||
resource.TestCheckResourceAttr("opennebula_template.template", "os.0.boot", ""), | ||
resource.TestCheckResourceAttr("opennebula_template.template", "context.%", "2"), | ||
resource.TestCheckResourceAttr("opennebula_template.template", "context.dns_hostname", "yes"), | ||
resource.TestCheckResourceAttr("opennebula_template.template", "context.NETWORK", "YES"), | ||
resource.TestCheckResourceAttr("opennebula_template.template", "tags.%", "2"), | ||
resource.TestCheckResourceAttr("opennebula_template.template", "tags.env", "prod"), | ||
resource.TestCheckResourceAttr("opennebula_template.template", "tags.customer", "test"), | ||
|
@@ -55,20 +60,30 @@ func TestAccTemplate(t *testing.T) { | |
}), | ||
), | ||
}, | ||
{ | ||
ResourceName: "opennebula_template.template", | ||
ImportState: true, | ||
ImportStateVerify: true, | ||
}, | ||
{ | ||
Config: testAccTemplateCPUModel, | ||
Check: resource.ComposeTestCheckFunc( | ||
resource.TestCheckResourceAttr("opennebula_template.template", "name", "terra-tpl-cpumodel"), | ||
resource.TestCheckResourceAttr("opennebula_template.template", "permissions", "660"), | ||
resource.TestCheckResourceAttr("opennebula_template.template", "group", "oneadmin"), | ||
resource.TestCheckResourceAttr("opennebula_template.template", "cpu", "0.5"), | ||
resource.TestCheckResourceAttr("opennebula_template.template", "vcpu", "1"), | ||
resource.TestCheckResourceAttr("opennebula_template.template", "memory", "512"), | ||
resource.TestCheckResourceAttr("opennebula_template.template", "graphics.#", "1"), | ||
resource.TestCheckResourceAttr("opennebula_template.template", "graphics.0.keymap", "en-us"), | ||
resource.TestCheckResourceAttr("opennebula_template.template", "graphics.0.listen", "0.0.0.0"), | ||
resource.TestCheckResourceAttr("opennebula_template.template", "graphics.0.type", "VNC"), | ||
resource.TestCheckResourceAttr("opennebula_template.template", "os.#", "1"), | ||
resource.TestCheckResourceAttr("opennebula_template.template", "os.0.arch", "x86_64"), | ||
resource.TestCheckResourceAttr("opennebula_template.template", "os.0.boot", ""), | ||
resource.TestCheckResourceAttr("opennebula_template.template", "context.%", "2"), | ||
resource.TestCheckResourceAttr("opennebula_template.template", "context.dns_hostname", "yes"), | ||
resource.TestCheckResourceAttr("opennebula_template.template", "context.NETWORK", "YES"), | ||
resource.TestCheckResourceAttr("opennebula_template.template", "cpumodel.#", "1"), | ||
resource.TestCheckResourceAttr("opennebula_template.template", "cpumodel.0.model", "host-passthrough"), | ||
resource.TestCheckResourceAttr("opennebula_template.template", "tags.%", "2"), | ||
|
@@ -88,20 +103,30 @@ func TestAccTemplate(t *testing.T) { | |
}), | ||
), | ||
}, | ||
{ | ||
ResourceName: "opennebula_template.template", | ||
ImportState: true, | ||
ImportStateVerify: true, | ||
}, | ||
{ | ||
Config: testAccTemplateConfigUpdate, | ||
Check: resource.ComposeTestCheckFunc( | ||
resource.TestCheckResourceAttr("opennebula_template.template", "name", "terratplupdate"), | ||
resource.TestCheckResourceAttr("opennebula_template.template", "permissions", "642"), | ||
resource.TestCheckResourceAttr("opennebula_template.template", "group", "oneadmin"), | ||
resource.TestCheckResourceAttr("opennebula_template.template", "cpu", "1"), | ||
resource.TestCheckResourceAttr("opennebula_template.template", "vcpu", "1"), | ||
resource.TestCheckResourceAttr("opennebula_template.template", "memory", "768"), | ||
resource.TestCheckResourceAttr("opennebula_template.template", "graphics.#", "1"), | ||
resource.TestCheckResourceAttr("opennebula_template.template", "graphics.0.keymap", "en-us"), | ||
resource.TestCheckResourceAttr("opennebula_template.template", "graphics.0.listen", "0.0.0.0"), | ||
resource.TestCheckResourceAttr("opennebula_template.template", "graphics.0.type", "VNC"), | ||
resource.TestCheckResourceAttr("opennebula_template.template", "os.#", "1"), | ||
resource.TestCheckResourceAttr("opennebula_template.template", "os.0.arch", "x86_64"), | ||
resource.TestCheckResourceAttr("opennebula_template.template", "os.0.boot", ""), | ||
resource.TestCheckResourceAttr("opennebula_template.template", "context.%", "2"), | ||
resource.TestCheckResourceAttr("opennebula_template.template", "context.dns_hostname", "yes"), | ||
resource.TestCheckResourceAttr("opennebula_template.template", "context.NETWORK", "YES"), | ||
resource.TestCheckResourceAttr("opennebula_template.template", "tags.%", "3"), | ||
resource.TestCheckResourceAttr("opennebula_template.template", "tags.env", "dev"), | ||
resource.TestCheckResourceAttr("opennebula_template.template", "tags.customer", "test"), | ||
|
@@ -124,20 +149,30 @@ func TestAccTemplate(t *testing.T) { | |
}), | ||
), | ||
}, | ||
{ | ||
ResourceName: "opennebula_template.template", | ||
ImportState: true, | ||
ImportStateVerify: true, | ||
}, | ||
{ | ||
Config: testAccTemplateConfigDelete, | ||
Check: resource.ComposeTestCheckFunc( | ||
resource.TestCheckResourceAttr("opennebula_template.template", "name", "terratplupdate"), | ||
resource.TestCheckResourceAttr("opennebula_template.template", "permissions", "642"), | ||
resource.TestCheckResourceAttr("opennebula_template.template", "group", "oneadmin"), | ||
resource.TestCheckResourceAttr("opennebula_template.template", "cpu", "1"), | ||
resource.TestCheckResourceAttr("opennebula_template.template", "vcpu", "1"), | ||
resource.TestCheckResourceAttr("opennebula_template.template", "memory", "768"), | ||
resource.TestCheckResourceAttr("opennebula_template.template", "graphics.#", "1"), | ||
resource.TestCheckResourceAttr("opennebula_template.template", "graphics.0.keymap", "en-us"), | ||
resource.TestCheckResourceAttr("opennebula_template.template", "graphics.0.listen", "0.0.0.0"), | ||
resource.TestCheckResourceAttr("opennebula_template.template", "graphics.0.type", "VNC"), | ||
resource.TestCheckResourceAttr("opennebula_template.template", "os.#", "1"), | ||
resource.TestCheckResourceAttr("opennebula_template.template", "os.0.arch", "x86_64"), | ||
resource.TestCheckResourceAttr("opennebula_template.template", "os.0.boot", ""), | ||
resource.TestCheckResourceAttr("opennebula_template.template", "context.%", "2"), | ||
resource.TestCheckResourceAttr("opennebula_template.template", "context.dns_hostname", "yes"), | ||
resource.TestCheckResourceAttr("opennebula_template.template", "context.NETWORK", "YES"), | ||
resource.TestCheckResourceAttr("opennebula_template.template", "tags.%", "2"), | ||
resource.TestCheckResourceAttr("opennebula_template.template", "tags.env", "dev"), | ||
resource.TestCheckResourceAttr("opennebula_template.template", "tags.customer", "test"), | ||
|
@@ -154,6 +189,11 @@ func TestAccTemplate(t *testing.T) { | |
}), | ||
), | ||
}, | ||
{ | ||
ResourceName: "opennebula_template.template", | ||
ImportState: true, | ||
ImportStateVerify: true, | ||
}, | ||
}, | ||
}) | ||
} | ||
|
@@ -233,7 +273,7 @@ resource "opennebula_template" "template" { | |
|
||
context = { | ||
dns_hostname = "yes" | ||
network = "YES" | ||
NETWORK = "YES" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why did you change the case ? is it for test purposes ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In the file |
||
} | ||
|
||
graphics { | ||
|
@@ -273,7 +313,7 @@ resource "opennebula_template" "template" { | |
|
||
context = { | ||
dns_hostname = "yes" | ||
network = "YES" | ||
NETWORK = "YES" | ||
} | ||
|
||
graphics { | ||
|
@@ -319,7 +359,7 @@ resource "opennebula_template" "template" { | |
|
||
context = { | ||
dns_hostname = "yes" | ||
network = "YES" | ||
NETWORK = "YES" | ||
} | ||
|
||
graphics { | ||
|
@@ -356,7 +396,7 @@ resource "opennebula_template" "template" { | |
|
||
context = { | ||
dns_hostname = "yes" | ||
network = "YES" | ||
NETWORK = "YES" | ||
} | ||
|
||
graphics { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm surprised that you set an empty value, you could retrieve the description from the template via
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The fields
description
andsched_requirements
were difficult to handle: if this field is removed from the Terraform-resource-configuration, it does not get removed from Terraform-state (it has an empty value, which gives problems in the unit-tests). I will write a new commit with an update of the methodflattenVMUserTemplate
: in this method, thedescription
-field is already taken care of