2014-08-28 17:30:49 +00:00
|
|
|
/*
|
2016-06-03 00:25:58 +00:00
|
|
|
Copyright 2014 The Kubernetes Authors.
|
2014-08-28 17:30:49 +00:00
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2015-10-10 08:18:12 +00:00
|
|
|
package ovirt
|
2014-08-28 17:30:49 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
2018-09-05 22:58:22 +00:00
|
|
|
cloudprovider "k8s.io/cloud-provider"
|
2014-08-28 17:30:49 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestOVirtCloudConfiguration(t *testing.T) {
|
|
|
|
config1 := (io.Reader)(nil)
|
|
|
|
|
|
|
|
_, err1 := cloudprovider.GetCloudProvider("ovirt", config1)
|
|
|
|
if err1 == nil {
|
|
|
|
t.Fatalf("An error is expected when the configuration is missing")
|
|
|
|
}
|
|
|
|
|
|
|
|
config2 := strings.NewReader("")
|
|
|
|
|
|
|
|
_, err2 := cloudprovider.GetCloudProvider("ovirt", config2)
|
|
|
|
if err2 == nil {
|
|
|
|
t.Fatalf("An error is expected when the configuration is empty")
|
|
|
|
}
|
|
|
|
|
|
|
|
config3 := strings.NewReader(`
|
|
|
|
[connection]
|
|
|
|
`)
|
|
|
|
|
|
|
|
_, err3 := cloudprovider.GetCloudProvider("ovirt", config3)
|
|
|
|
if err3 == nil {
|
|
|
|
t.Fatalf("An error is expected when the uri is missing")
|
|
|
|
}
|
|
|
|
|
|
|
|
config4 := strings.NewReader(`
|
|
|
|
[connection]
|
|
|
|
uri = https://localhost:8443/ovirt-engine/api
|
|
|
|
`)
|
|
|
|
|
|
|
|
_, err4 := cloudprovider.GetCloudProvider("ovirt", config4)
|
|
|
|
if err4 != nil {
|
|
|
|
t.Fatalf("Unexpected error creating the provider: %s", err4)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestOVirtCloudXmlParsing(t *testing.T) {
|
|
|
|
body1 := (io.Reader)(nil)
|
|
|
|
|
2018-10-28 13:38:32 +00:00
|
|
|
_, err1 := getInstancesFromXML(body1)
|
2014-08-28 17:30:49 +00:00
|
|
|
if err1 == nil {
|
|
|
|
t.Fatalf("An error is expected when body is missing")
|
|
|
|
}
|
|
|
|
|
|
|
|
body2 := strings.NewReader("")
|
|
|
|
|
2018-10-28 13:38:32 +00:00
|
|
|
_, err2 := getInstancesFromXML(body2)
|
2014-08-28 17:30:49 +00:00
|
|
|
if err2 == nil {
|
|
|
|
t.Fatalf("An error is expected when body is empty")
|
|
|
|
}
|
|
|
|
|
|
|
|
body3 := strings.NewReader(`
|
|
|
|
<vms>
|
|
|
|
<vm></vm>
|
|
|
|
</vms>
|
|
|
|
`)
|
|
|
|
|
2018-10-28 13:38:32 +00:00
|
|
|
instances3, err3 := getInstancesFromXML(body3)
|
2014-08-28 17:30:49 +00:00
|
|
|
if err3 != nil {
|
|
|
|
t.Fatalf("Unexpected error listing instances: %s", err3)
|
|
|
|
}
|
|
|
|
if len(instances3) > 0 {
|
2014-10-10 00:06:32 +00:00
|
|
|
t.Fatalf("Unexpected number of instance(s): %d", len(instances3))
|
2014-08-28 17:30:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
body4 := strings.NewReader(`
|
|
|
|
<vms>
|
|
|
|
<vm>
|
|
|
|
<status><state>Up</state></status>
|
|
|
|
<guest_info><fqdn>host1</fqdn></guest_info>
|
|
|
|
</vm>
|
|
|
|
<vm>
|
|
|
|
<!-- empty -->
|
|
|
|
</vm>
|
|
|
|
<vm>
|
|
|
|
<status><state>Up</state></status>
|
|
|
|
</vm>
|
|
|
|
<vm>
|
|
|
|
<status><state>Down</state></status>
|
|
|
|
<guest_info><fqdn>host2</fqdn></guest_info>
|
|
|
|
</vm>
|
|
|
|
<vm>
|
|
|
|
<status><state>Up</state></status>
|
|
|
|
<guest_info><fqdn>host3</fqdn></guest_info>
|
|
|
|
</vm>
|
|
|
|
</vms>
|
|
|
|
`)
|
|
|
|
|
2018-10-28 13:38:32 +00:00
|
|
|
instances4, err4 := getInstancesFromXML(body4)
|
2014-08-28 17:30:49 +00:00
|
|
|
if err4 != nil {
|
|
|
|
t.Fatalf("Unexpected error listing instances: %s", err4)
|
|
|
|
}
|
|
|
|
if len(instances4) != 2 {
|
2014-10-10 00:06:32 +00:00
|
|
|
t.Fatalf("Unexpected number of instance(s): %d", len(instances4))
|
2014-08-28 17:30:49 +00:00
|
|
|
}
|
2015-02-12 14:51:45 +00:00
|
|
|
|
|
|
|
names := instances4.ListSortedNames()
|
|
|
|
if names[0] != "host1" || names[1] != "host3" {
|
2014-08-28 17:30:49 +00:00
|
|
|
t.Fatalf("Unexpected instance(s): %s", instances4)
|
|
|
|
}
|
|
|
|
}
|