forked from kubevirt/kubevirt
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request kubevirt#3433 from EdDev/refactor-tests-vmi
functests: Refactor VMI helpers
- Loading branch information
Showing
9 changed files
with
380 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
load("@io_bazel_rules_go//go:def.bzl", "go_library") | ||
|
||
go_library( | ||
name = "go_default_library", | ||
srcs = [ | ||
"cloudinit.go", | ||
"factory.go", | ||
"network.go", | ||
"storage.go", | ||
"vmi.go", | ||
], | ||
importpath = "kubevirt.io/kubevirt/tests/libvmi", | ||
visibility = ["//visibility:public"], | ||
deps = [ | ||
"//staging/src/kubevirt.io/client-go/api/v1:go_default_library", | ||
"//tests/containerdisk:go_default_library", | ||
"//vendor/k8s.io/api/core/v1:go_default_library", | ||
"//vendor/k8s.io/apimachinery/pkg/api/resource:go_default_library", | ||
"//vendor/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library", | ||
"//vendor/k8s.io/apimachinery/pkg/util/rand:go_default_library", | ||
], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/* | ||
* This file is part of the KubeVirt project | ||
* | ||
* 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. | ||
* | ||
* Copyright 2020 Red Hat, Inc. | ||
* | ||
*/ | ||
|
||
package libvmi | ||
|
||
import ( | ||
"encoding/base64" | ||
|
||
kvirtv1 "kubevirt.io/client-go/api/v1" | ||
) | ||
|
||
// WithCloudInitNoCloudUserData adds cloud-init no-cloud user data. | ||
func WithCloudInitNoCloudUserData(data string, b64Encoding bool) Option { | ||
return func(vmi *kvirtv1.VirtualMachineInstance) { | ||
diskName, bus := "disk1", "virtio" | ||
addDiskVolumeWithCloudInitNoCloud(vmi, diskName, bus) | ||
|
||
volume := getVolume(vmi, diskName) | ||
if b64Encoding { | ||
encodedData := base64.StdEncoding.EncodeToString([]byte(data)) | ||
volume.CloudInitNoCloud.UserDataBase64 = encodedData | ||
} else { | ||
volume.CloudInitNoCloud.UserData = data | ||
} | ||
} | ||
} | ||
|
||
func addDiskVolumeWithCloudInitNoCloud(vmi *kvirtv1.VirtualMachineInstance, diskName, bus string) { | ||
addDisk(vmi, newDisk(diskName, bus)) | ||
v := newVolume(diskName) | ||
setCloudInitNoCloud(&v, &kvirtv1.CloudInitNoCloudSource{}) | ||
addVolume(vmi, v) | ||
} | ||
|
||
func setCloudInitNoCloud(volume *kvirtv1.Volume, source *kvirtv1.CloudInitNoCloudSource) { | ||
volume.VolumeSource = kvirtv1.VolumeSource{CloudInitNoCloud: source} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* | ||
* This file is part of the KubeVirt project | ||
* | ||
* 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. | ||
* | ||
* Copyright 2020 Red Hat, Inc. | ||
* | ||
*/ | ||
|
||
package libvmi | ||
|
||
import ( | ||
kvirtv1 "kubevirt.io/client-go/api/v1" | ||
|
||
cd "kubevirt.io/kubevirt/tests/containerdisk" | ||
) | ||
|
||
// Default VMI values | ||
const ( | ||
DefaultResourceMemory = "8192Ki" | ||
DefaultTestGracePeriod int64 = 0 | ||
DefaultVmiName = "testvmi" | ||
NamespaceTestDefault = "kubevirt-test-default" | ||
) | ||
|
||
// NewFedora instantiates a new Fedora based VMI configuration, | ||
// building its extra properties based on the specified With* options. | ||
func NewFedora(opts ...Option) *kvirtv1.VirtualMachineInstance { | ||
configurePassword := `#!/bin/bash | ||
echo "fedora" |passwd fedora --stdin | ||
echo ` | ||
|
||
opts = append( | ||
defaultOptions(), | ||
WithResourceMemory("512M"), | ||
WithRng(), | ||
WithContainerImage(cd.ContainerDiskFor(cd.ContainerDiskFedora)), | ||
WithCloudInitNoCloudUserData(configurePassword, true), | ||
) | ||
return New(NamespaceTestDefault, RandName(DefaultVmiName), opts...) | ||
} | ||
|
||
// defaultOptions returns a list of "default" options. | ||
func defaultOptions() []Option { | ||
return []Option{ | ||
WithInterface(InterfaceDeviceWithMasqueradeBinding()), | ||
WithNetwork(kvirtv1.DefaultPodNetwork()), | ||
WithTerminationGracePeriod(DefaultTestGracePeriod), | ||
WithResourceMemory(DefaultResourceMemory), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/* | ||
* This file is part of the KubeVirt project | ||
* | ||
* 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. | ||
* | ||
* Copyright 2020 Red Hat, Inc. | ||
* | ||
*/ | ||
|
||
package libvmi | ||
|
||
import ( | ||
kvirtv1 "kubevirt.io/client-go/api/v1" | ||
) | ||
|
||
// WithInterface adds a Domain Device Interface. | ||
func WithInterface(iface kvirtv1.Interface) Option { | ||
return func(vmi *kvirtv1.VirtualMachineInstance) { | ||
vmi.Spec.Domain.Devices.Interfaces = append( | ||
vmi.Spec.Domain.Devices.Interfaces, iface, | ||
) | ||
} | ||
} | ||
|
||
// WithNetwork adds a network object. | ||
func WithNetwork(network *kvirtv1.Network) Option { | ||
return func(vmi *kvirtv1.VirtualMachineInstance) { | ||
vmi.Spec.Networks = append(vmi.Spec.Networks, *network) | ||
} | ||
} | ||
|
||
// InterfaceDeviceWithMasqueradeBinding returns an Interface named "default" with masquerade binding. | ||
func InterfaceDeviceWithMasqueradeBinding() kvirtv1.Interface { | ||
return kvirtv1.Interface{ | ||
Name: "default", | ||
InterfaceBindingMethod: kvirtv1.InterfaceBindingMethod{ | ||
Masquerade: &kvirtv1.InterfaceMasquerade{}, | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
/* | ||
* This file is part of the KubeVirt project | ||
* | ||
* 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. | ||
* | ||
* Copyright 2020 Red Hat, Inc. | ||
* | ||
*/ | ||
|
||
package libvmi | ||
|
||
import ( | ||
kvirtv1 "kubevirt.io/client-go/api/v1" | ||
) | ||
|
||
// WithContainerImage specifies the name of the container image to be used. | ||
func WithContainerImage(name string) Option { | ||
return func(vmi *kvirtv1.VirtualMachineInstance) { | ||
diskName, bus := "disk0", "virtio" | ||
vmi.Spec.Domain.Devices.Disks = append(vmi.Spec.Domain.Devices.Disks, newDisk(diskName, bus)) | ||
vmi.Spec.Volumes = append(vmi.Spec.Volumes, newContainerVolume(diskName, name)) | ||
} | ||
} | ||
|
||
func addDisk(vmi *kvirtv1.VirtualMachineInstance, disk kvirtv1.Disk) { | ||
if !diskExists(vmi, disk) { | ||
vmi.Spec.Domain.Devices.Disks = append(vmi.Spec.Domain.Devices.Disks, disk) | ||
} | ||
} | ||
|
||
func addVolume(vmi *kvirtv1.VirtualMachineInstance, volume kvirtv1.Volume) { | ||
if !volumeExists(vmi, volume) { | ||
vmi.Spec.Volumes = append(vmi.Spec.Volumes, volume) | ||
} | ||
} | ||
|
||
func getVolume(vmi *kvirtv1.VirtualMachineInstance, name string) *kvirtv1.Volume { | ||
for i := range vmi.Spec.Volumes { | ||
if vmi.Spec.Volumes[i].Name == name { | ||
return &vmi.Spec.Volumes[i] | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func diskExists(vmi *kvirtv1.VirtualMachineInstance, disk kvirtv1.Disk) bool { | ||
for _, d := range vmi.Spec.Domain.Devices.Disks { | ||
if d.Name == disk.Name { | ||
return true | ||
} | ||
} | ||
return false | ||
} | ||
|
||
func volumeExists(vmi *kvirtv1.VirtualMachineInstance, volume kvirtv1.Volume) bool { | ||
for _, v := range vmi.Spec.Volumes { | ||
if v.Name == volume.Name { | ||
return true | ||
} | ||
} | ||
return false | ||
} | ||
|
||
func newDisk(name, bus string) kvirtv1.Disk { | ||
return kvirtv1.Disk{ | ||
Name: name, | ||
DiskDevice: kvirtv1.DiskDevice{ | ||
Disk: &kvirtv1.DiskTarget{ | ||
Bus: bus, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func newVolume(name string) kvirtv1.Volume { | ||
return kvirtv1.Volume{Name: name} | ||
} | ||
|
||
func newContainerVolume(name, image string) kvirtv1.Volume { | ||
return kvirtv1.Volume{ | ||
Name: name, | ||
VolumeSource: kvirtv1.VolumeSource{ | ||
ContainerDisk: &kvirtv1.ContainerDiskSource{ | ||
Image: image, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func newDataVolume(name, diskName string) kvirtv1.Volume { | ||
return kvirtv1.Volume{ | ||
Name: diskName, | ||
VolumeSource: kvirtv1.VolumeSource{ | ||
DataVolume: &kvirtv1.DataVolumeSource{ | ||
Name: name, | ||
}, | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
/* | ||
* This file is part of the KubeVirt project | ||
* | ||
* 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. | ||
* | ||
* Copyright 2020 Red Hat, Inc. | ||
* | ||
*/ | ||
|
||
package libvmi | ||
|
||
import ( | ||
k8sv1 "k8s.io/api/core/v1" | ||
"k8s.io/apimachinery/pkg/api/resource" | ||
k8smetav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/util/rand" | ||
|
||
kvirtv1 "kubevirt.io/client-go/api/v1" | ||
) | ||
|
||
// Option represents an action that enables an option. | ||
type Option func(vmi *kvirtv1.VirtualMachineInstance) | ||
|
||
// New instantiates a new VMI configuration, | ||
// building its properties based on the specified With* options. | ||
func New(namespace, name string, opts ...Option) *kvirtv1.VirtualMachineInstance { | ||
vmi := baseVmi(namespace, name) | ||
|
||
for _, f := range opts { | ||
f(vmi) | ||
} | ||
|
||
return vmi | ||
} | ||
|
||
// RandName returns a random name by concatanating the given name with a random string. | ||
func RandName(name string) string { | ||
return name + rand.String(48) | ||
} | ||
|
||
// WithTerminationGracePeriod specifies the termination grace period in seconds. | ||
func WithTerminationGracePeriod(seconds int64) Option { | ||
return func(vmi *kvirtv1.VirtualMachineInstance) { | ||
vmi.Spec.TerminationGracePeriodSeconds = &seconds | ||
} | ||
} | ||
|
||
// WithRng adds `rng` to the the vmi devices. | ||
func WithRng() Option { | ||
return func(vmi *kvirtv1.VirtualMachineInstance) { | ||
vmi.Spec.Domain.Devices.Rng = &kvirtv1.Rng{} | ||
} | ||
} | ||
|
||
// WithResourceMemory specifies the vmi memory resource. | ||
func WithResourceMemory(value string) Option { | ||
return func(vmi *kvirtv1.VirtualMachineInstance) { | ||
vmi.Spec.Domain.Resources.Requests = k8sv1.ResourceList{ | ||
k8sv1.ResourceMemory: resource.MustParse(value), | ||
} | ||
} | ||
} | ||
|
||
func baseVmi(namespace, name string) *kvirtv1.VirtualMachineInstance { | ||
vmi := kvirtv1.NewVMIReferenceFromNameWithNS(namespace, name) | ||
vmi.Spec = kvirtv1.VirtualMachineInstanceSpec{Domain: kvirtv1.DomainSpec{}} | ||
vmi.TypeMeta = k8smetav1.TypeMeta{ | ||
APIVersion: kvirtv1.GroupVersion.String(), | ||
Kind: "VirtualMachineInstance", | ||
} | ||
return vmi | ||
} |
Oops, something went wrong.