2018-01-24 06:04:39 -05:00
|
|
|
package clone
|
2017-06-12 14:08:25 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2018-01-24 13:52:29 -05:00
|
|
|
func TestCloneConfig_MinimalConfig(t *testing.T) {
|
2020-01-13 18:52:05 -05:00
|
|
|
c := new(Config)
|
|
|
|
warns, errs := c.Prepare(minimalConfig())
|
2017-06-12 14:08:25 -04:00
|
|
|
testConfigOk(t, warns, errs)
|
|
|
|
}
|
|
|
|
|
2018-01-24 13:52:29 -05:00
|
|
|
func TestCloneConfig_MandatoryParameters(t *testing.T) {
|
2017-07-01 21:18:46 -04:00
|
|
|
params := []string{"vcenter_server", "username", "password", "template", "vm_name", "host"}
|
|
|
|
for _, param := range params {
|
|
|
|
raw := minimalConfig()
|
|
|
|
raw[param] = ""
|
2020-01-13 18:52:05 -05:00
|
|
|
c := new(Config)
|
|
|
|
warns, err := c.Prepare(raw)
|
2017-07-01 21:18:46 -04:00
|
|
|
testConfigErr(t, param, warns, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-24 13:52:29 -05:00
|
|
|
func TestCloneConfig_Timeout(t *testing.T) {
|
2017-06-12 14:08:25 -04:00
|
|
|
raw := minimalConfig()
|
|
|
|
raw["shutdown_timeout"] = "3m"
|
2020-01-13 18:52:05 -05:00
|
|
|
conf := new(Config)
|
|
|
|
warns, err := conf.Prepare(raw)
|
2017-06-12 14:08:25 -04:00
|
|
|
testConfigOk(t, warns, err)
|
2018-10-31 17:42:24 -04:00
|
|
|
if conf.ShutdownConfig.Timeout != 3*time.Minute {
|
2017-07-01 20:06:02 -04:00
|
|
|
t.Fatalf("shutdown_timeout sould be equal 3 minutes, got %v", conf.ShutdownConfig.Timeout)
|
2017-06-12 14:08:25 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-24 13:52:29 -05:00
|
|
|
func TestCloneConfig_RAMReservation(t *testing.T) {
|
2017-07-01 17:50:01 -04:00
|
|
|
raw := minimalConfig()
|
|
|
|
raw["RAM_reservation"] = 1000
|
|
|
|
raw["RAM_reserve_all"] = true
|
2020-01-13 18:52:05 -05:00
|
|
|
c := new(Config)
|
|
|
|
warns, err := c.Prepare(raw)
|
2017-07-01 21:18:46 -04:00
|
|
|
testConfigErr(t, "RAM_reservation", warns, err)
|
2017-07-01 17:50:01 -04:00
|
|
|
}
|
|
|
|
|
2017-06-12 14:08:25 -04:00
|
|
|
func minimalConfig() map[string]interface{} {
|
|
|
|
return map[string]interface{}{
|
2017-07-01 11:54:10 -04:00
|
|
|
"vcenter_server": "vcenter.domain.local",
|
2018-10-31 17:42:24 -04:00
|
|
|
"username": "root",
|
|
|
|
"password": "vmware",
|
|
|
|
"template": "ubuntu",
|
|
|
|
"vm_name": "vm1",
|
|
|
|
"host": "esxi1.domain.local",
|
|
|
|
"ssh_username": "root",
|
|
|
|
"ssh_password": "secret",
|
2017-06-12 14:08:25 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testConfigOk(t *testing.T, warns []string, err error) {
|
|
|
|
if len(warns) > 0 {
|
2018-03-23 16:54:20 -04:00
|
|
|
t.Errorf("Should be no warnings: %#v", warns)
|
2017-06-12 14:08:25 -04:00
|
|
|
}
|
|
|
|
if err != nil {
|
2018-03-23 16:54:20 -04:00
|
|
|
t.Errorf("Unexpected error: %s", err)
|
2017-06-12 14:08:25 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-01 21:18:46 -04:00
|
|
|
func testConfigErr(t *testing.T, context string, warns []string, err error) {
|
2017-06-12 14:08:25 -04:00
|
|
|
if len(warns) > 0 {
|
2018-03-23 16:54:20 -04:00
|
|
|
t.Errorf("Should be no warnings: %#v", warns)
|
2017-06-12 14:08:25 -04:00
|
|
|
}
|
|
|
|
if err == nil {
|
2017-07-01 21:18:46 -04:00
|
|
|
t.Error("An error is not raised for", context)
|
2017-06-12 14:08:25 -04:00
|
|
|
}
|
|
|
|
}
|