2013-11-18 19:04:38 -05:00
|
|
|
package vsphere
|
2016-05-18 21:26:46 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/url"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2020-07-15 14:39:04 -04:00
|
|
|
func getTestConfig() Config {
|
|
|
|
return Config{
|
|
|
|
Username: "me",
|
|
|
|
Password: "notpassword",
|
|
|
|
Host: "myhost",
|
|
|
|
Datacenter: "mydc",
|
|
|
|
Cluster: "mycluster",
|
|
|
|
VMName: "my vm",
|
|
|
|
Datastore: "my datastore",
|
|
|
|
Insecure: true,
|
|
|
|
DiskMode: "thin",
|
|
|
|
VMFolder: "my folder",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-18 21:26:46 -04:00
|
|
|
func TestArgs(t *testing.T) {
|
|
|
|
var p PostProcessor
|
|
|
|
|
2020-07-15 14:39:04 -04:00
|
|
|
p.config = getTestConfig()
|
2016-05-18 21:26:46 -04:00
|
|
|
|
|
|
|
source := "something.vmx"
|
|
|
|
ovftool_uri := fmt.Sprintf("vi://%s:%s@%s/%s/host/%s",
|
|
|
|
url.QueryEscape(p.config.Username),
|
|
|
|
url.QueryEscape(p.config.Password),
|
|
|
|
p.config.Host,
|
|
|
|
p.config.Datacenter,
|
|
|
|
p.config.Cluster)
|
|
|
|
|
|
|
|
if p.config.ResourcePool != "" {
|
|
|
|
ovftool_uri += "/Resources/" + p.config.ResourcePool
|
|
|
|
}
|
|
|
|
|
|
|
|
args, err := p.BuildArgs(source, ovftool_uri)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Error: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Logf("ovftool %s", strings.Join(args, " "))
|
|
|
|
}
|
2018-04-05 13:04:12 -04:00
|
|
|
|
2020-07-15 14:39:04 -04:00
|
|
|
func TestGenerateURI_Basic(t *testing.T) {
|
|
|
|
var p PostProcessor
|
|
|
|
|
|
|
|
p.config = getTestConfig()
|
|
|
|
|
2020-07-15 15:10:09 -04:00
|
|
|
uri, err := p.generateURI()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("had error: %s", err)
|
|
|
|
}
|
2020-07-15 14:39:04 -04:00
|
|
|
expected_uri := "vi://me:notpassword@myhost/mydc/host/mycluster"
|
2020-07-15 15:10:09 -04:00
|
|
|
if uri.String() != expected_uri {
|
2020-07-15 14:39:04 -04:00
|
|
|
t.Fatalf("URI did not match. Recieved: %s. Expected: %s", uri, expected_uri)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGenerateURI_PasswordEscapes(t *testing.T) {
|
|
|
|
type escapeCases struct {
|
|
|
|
Input string
|
|
|
|
Expected string
|
|
|
|
}
|
|
|
|
|
|
|
|
cases := []escapeCases{
|
|
|
|
{`this has spaces`, `this%20has%20spaces`},
|
|
|
|
{`exclaimation_!`, `exclaimation_%21`},
|
2020-07-15 15:10:09 -04:00
|
|
|
{`hash_#_dollar_$`, `hash_%23_dollar_$`},
|
|
|
|
{`ampersand_&awesome`, `ampersand_&awesome`},
|
2020-07-15 14:39:04 -04:00
|
|
|
{`single_quote_'_and_another_'`, `single_quote_%27_and_another_%27`},
|
|
|
|
{`open_paren_(_close_paren_)`, `open_paren_%28_close_paren_%29`},
|
2020-07-15 15:10:09 -04:00
|
|
|
{`asterisk_*_plus_+`, `asterisk_%2A_plus_+`},
|
|
|
|
{`comma_,slash_/`, `comma_,slash_%2F`},
|
|
|
|
{`colon_:semicolon_;`, `colon_%3Asemicolon_;`},
|
|
|
|
{`equal_=question_?`, `equal_=question_%3F`},
|
2020-07-15 14:39:04 -04:00
|
|
|
{`at_@`, `at_%40`},
|
|
|
|
{`open_bracket_[closed_bracket]`, `open_bracket_%5Bclosed_bracket%5D`},
|
2020-07-15 15:10:09 -04:00
|
|
|
{`user:password with $paces@host/name.foo`, `user%3Apassword%20with%20$paces%40host%2Fname.foo`},
|
2020-07-15 14:39:04 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, escapeCase := range cases {
|
|
|
|
var p PostProcessor
|
|
|
|
|
|
|
|
p.config = getTestConfig()
|
|
|
|
p.config.Password = escapeCase.Input
|
|
|
|
|
2020-07-15 15:10:09 -04:00
|
|
|
uri, err := p.generateURI()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("had error: %s", err)
|
|
|
|
}
|
2020-07-15 14:39:04 -04:00
|
|
|
expected_uri := fmt.Sprintf("vi://me:%s@myhost/mydc/host/mycluster", escapeCase.Expected)
|
|
|
|
|
2020-07-15 15:10:09 -04:00
|
|
|
if uri.String() != expected_uri {
|
2020-07-15 14:39:04 -04:00
|
|
|
t.Fatalf("URI did not match. Recieved: %s. Expected: %s", uri, expected_uri)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-07-24 18:09:49 -04:00
|
|
|
|
2020-07-24 19:36:12 -04:00
|
|
|
func TestGetEncodedPassword(t *testing.T) {
|
2020-07-24 18:09:49 -04:00
|
|
|
|
|
|
|
// Password is encoded, and contains a colon
|
|
|
|
ovftool_uri := fmt.Sprintf("vi://hostname/Datacenter/host/cluster")
|
|
|
|
|
|
|
|
u, _ := url.Parse(ovftool_uri)
|
|
|
|
u.User = url.UserPassword("us:ername", "P@ssW:rd")
|
|
|
|
|
2020-07-24 19:36:12 -04:00
|
|
|
encoded, isSet := getEncodedPassword(u)
|
|
|
|
expected := "P%40ssW%3Ard"
|
|
|
|
if !isSet {
|
|
|
|
t.Fatalf("Password is set but test said it is not")
|
|
|
|
}
|
|
|
|
if encoded != expected {
|
|
|
|
t.Fatalf("Should have successfully gotten encoded password. Expected: %s; recieved: %s", expected, encoded)
|
2020-07-24 18:09:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// There is no password
|
|
|
|
u.User = url.UserPassword("us:ername", "")
|
|
|
|
|
2020-07-24 19:36:12 -04:00
|
|
|
_, isSet = getEncodedPassword(u)
|
|
|
|
if isSet {
|
|
|
|
t.Fatalf("Should have determined that password was not set")
|
2020-07-24 18:09:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|