Skip to content

Commit

Permalink
test(api): add e2e vm configuration test (#373)
Browse files Browse the repository at this point in the history
- Increase SSH connection timeout(required in some cases).
- Refactor helper functions.
- Optimize config file fields.
- Fix golangci-lint errors.

Signed-off-by: Roman Sysoev <roman.sysoev@flant.com>
  • Loading branch information
hardcoretime authored Sep 19, 2024
1 parent f98dba8 commit 6219dcd
Show file tree
Hide file tree
Showing 30 changed files with 633 additions and 84 deletions.
1 change: 1 addition & 0 deletions tests/e2e/Taskfile.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ tasks:
mkdir /tmp/testdata
cp -a testdata/virtualization-resources /tmp/testdata/virtualization-resources
cp -a testdata/connectivity /tmp/testdata/connectivity
cp -a testdata/vm-configuration /tmp/testdata/vm-configuration
cp -a testdata/sshkeys /tmp/testdata/sshkeys
virtctl:
cmds:
Expand Down
48 changes: 37 additions & 11 deletions tests/e2e/complex_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,17 @@ import (
"fmt"
"strings"

kc "github.com/deckhouse/virtualization/tests/e2e/kubectl"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"

kc "github.com/deckhouse/virtualization/tests/e2e/kubectl"
)

var _ = Describe("Complex test", Ordered, ContinueOnFailure, func() {
Context("Virtualization resources", func() {
When("Resources applied", func() {
It("Result must have no error", func() {
res := kubectl.Kustomize(conf.VirtualizationResources, kc.KustomizeOptions{})
res := kubectl.Kustomize(conf.TestData.VirtualizationResources, kc.KustomizeOptions{})
Expect(res.WasSuccess()).To(Equal(true), res.StdErr())
})
})
Expand All @@ -38,15 +39,32 @@ var _ = Describe("Complex test", Ordered, ContinueOnFailure, func() {
Context("Virtual images", func() {
When("VI applied", func() {
It(fmt.Sprintf("Phase should be %s", PhaseReady), func() {
WaitPhase("vi", PhaseReady)
WaitPhase(kc.ResourceVI, PhaseReady, kc.GetOptions{
Namespace: conf.Namespace,
Output: "jsonpath='{.items[*].metadata.name}'",
})
})
})
})

Context("Cluster virtual images", func() {
When("CVI applied", func() {
It(fmt.Sprintf("Phase should be %s", PhaseReady), func() {
WaitPhase(kc.ResourceCVI, PhaseReady, kc.GetOptions{
Namespace: conf.Namespace,
Output: "jsonpath='{.items[*].metadata.name}'",
})
})
})
})

Context("Disks", func() {
When("VD applied", func() {
It(fmt.Sprintf("Phase should be %s", PhaseReady), func() {
WaitPhase("vd", PhaseReady)
WaitPhase(kc.ResourceVD, PhaseReady, kc.GetOptions{
Namespace: conf.Namespace,
Output: "jsonpath='{.items[*].metadata.name}'",
})
})
})
})
Expand All @@ -61,38 +79,46 @@ var _ = Describe("Complex test", Ordered, ContinueOnFailure, func() {
MergePatchResource(kc.ResourceVMIP, vmipMetadataName, mergePatch)
})
It(fmt.Sprintf("Phase should be %s", PhaseBound), func() {
WaitPhase("vmip", PhaseBound)
WaitPhase(kc.ResourceVMIP, PhaseBound, kc.GetOptions{
Namespace: conf.Namespace,
Output: "jsonpath='{.items[*].metadata.name}'",
})
})
})
})

Context("Virtual machines", func() {
When("VM applied", func() {
It(fmt.Sprintf("Phase should be %s", PhaseRunning), func() {
WaitPhase("vm", PhaseRunning)
WaitPhase(kc.ResourceVM, PhaseRunning, kc.GetOptions{
Namespace: conf.Namespace,
Output: "jsonpath='{.items[*].metadata.name}'",
})
})
})
})

Context("Virtualmachine block device attachments", func() {
When("VMBDA applied", func() {
It(fmt.Sprintf("Phase should be %s", PhaseAttached), func() {
WaitPhase("vmbda", PhaseAttached)
WaitPhase(kc.ResourceVMBDA, PhaseAttached, kc.GetOptions{
Namespace: conf.Namespace,
Output: "jsonpath='{.items[*].metadata.name}'",
})
})
})
})

Context("External connection", func() {
When("VMs are running", func() {
It("All VMs must have to be connected to external network", func() {
sshKeyPath := fmt.Sprintf("%s/id_ed", conf.Sshkeys)
resourceType := kc.Resource("vm")
sshKeyPath := fmt.Sprintf("%s/id_ed", conf.TestData.Sshkeys)
output := "jsonpath='{.items[*].metadata.name}'"

res := kubectl.List(resourceType, kc.GetOptions{
res := kubectl.List(kc.ResourceVM, kc.GetOptions{
Namespace: conf.Namespace,
Output: output,
Labels: map[string]string{"testcase": namePrefix},
Labels: map[string]string{"id": namePrefix},
})
Expect(res.WasSuccess()).To(Equal(true), res.StdErr())

Expand Down
31 changes: 18 additions & 13 deletions tests/e2e/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@ import (
"os"
"strconv"

yamlv3 "gopkg.in/yaml.v3"

gt "github.com/deckhouse/virtualization/tests/e2e/git"
kc "github.com/deckhouse/virtualization/tests/e2e/kubectl"
yamlv3 "gopkg.in/yaml.v3"
)

var (
Expand Down Expand Up @@ -115,15 +116,20 @@ type KustomizeLabel struct {
}

type Config struct {
ClusterTransport ClusterTransport `yaml:"clusterTransport"`
Disks DisksConf `yaml:"disks"`
VM VmConf `yaml:"vm"`
Ipam IpamConf `yaml:"ipam"`
HelperImages HelperImages `yaml:"helperImages"`
Namespace string `yaml:"namespaceSuffix"`
VirtualizationResources string `yaml:"virtualizationResources"`
Connectivity string `yaml:"connectivity"`
Sshkeys string `yaml:"sshKeys"`
ClusterTransport ClusterTransport `yaml:"clusterTransport"`
Disks DisksConf `yaml:"disks"`
VM VmConf `yaml:"vm"`
Ipam IpamConf `yaml:"ipam"`
HelperImages HelperImages `yaml:"helperImages"`
Namespace string `yaml:"namespaceSuffix"`
TestData TestData `yaml:"testData"`
}

type TestData struct {
VirtualizationResources string `yaml:"virtualizationResources"`
Connectivity string `yaml:"connectivity"`
VmConfiguration string `yaml:"vmConfiguration"`
Sshkeys string `yaml:"sshKeys"`
}

type ClusterTransport struct {
Expand Down Expand Up @@ -199,7 +205,6 @@ func (c *Config) setEnvs() error {
c.Ipam.TestDataDir = e
}
return nil

}

func GetNamePrefix() (string, error) {
Expand Down Expand Up @@ -233,13 +238,13 @@ func (k *Kustomize) SetParams(filePath, namespace, namePrefix string) error {

kustomizeFile.Namespace = namespace
kustomizeFile.NamePrefix = namePrefix + "-"
kustomizeFile.Labels[0].Pairs["testcase"] = namePrefix
kustomizeFile.Labels[0].Pairs["id"] = namePrefix
updatedKustomizeFile, marshalErr := yamlv3.Marshal(&kustomizeFile)
if marshalErr != nil {
return marshalErr
}

writeErr := os.WriteFile(filePath, updatedKustomizeFile, 0644)
writeErr := os.WriteFile(filePath, updatedKustomizeFile, 0o644)
if writeErr != nil {
return writeErr
}
Expand Down
8 changes: 5 additions & 3 deletions tests/e2e/default_config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ ipam:
testDataDir: "./testdata/ipam"
helperImages:
curlImage: "curlimages/curl"
virtualizationResources: "/tmp/testdata/virtualization-resources"
connectivity: "/tmp/testdata/connectivity"
sshKeys: "/tmp/testdata/sshkeys"
testData:
virtualizationResources: "/tmp/testdata/virtualization-resources"
connectivity: "/tmp/testdata/connectivity"
vmConfiguration: "/tmp/testdata/vm-configuration"
sshKeys: "/tmp/testdata/sshkeys"
7 changes: 4 additions & 3 deletions tests/e2e/executor/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ type Executor interface {
ExecContext(ctx context.Context, cmd string) *CMDResult
ExecWithSudo(cmd string) *CMDResult
ExecWithSudoContext(ctx context.Context, cmd string) *CMDResult
ExecuteContext(ctx context.Context, cmd string, stdout io.Writer, stderr io.Writer) error
ExecuteContext(ctx context.Context, cmd string, stdout, stderr io.Writer) error
}

func (e CMDExecutor) Exec(command string) *CMDResult {
Expand Down Expand Up @@ -66,11 +66,12 @@ func (e CMDExecutor) ExecWithSudoContext(ctx context.Context, command string) *C
return e.ExecContext(ctx, fmt.Sprintf("sudo %s", command))
}

func (e CMDExecutor) ExecuteContext(ctx context.Context, command string, stdout io.Writer, stderr io.Writer) error {
func (e CMDExecutor) ExecuteContext(ctx context.Context, command string, stdout, stderr io.Writer) error {
cmd := e.makeCMD(ctx, command, stdout, stderr)
return cmd.Run()
}
func (e CMDExecutor) makeCMD(ctx context.Context, command string, stdout io.Writer, stderr io.Writer) *exec.Cmd {

func (e CMDExecutor) makeCMD(ctx context.Context, command string, stdout, stderr io.Writer) *exec.Cmd {
cmd := exec.CommandContext(ctx, "bash", "-c", command)
cmd.Stdin = os.Stdin
cmd.Stdout = stdout
Expand Down
3 changes: 2 additions & 1 deletion tests/e2e/helper/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,11 @@ import (
"path/filepath"
"strings"

"github.com/deckhouse/virtualization/tests/e2e/kubectl"
. "github.com/onsi/ginkgo/v2"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/util/yaml"

"github.com/deckhouse/virtualization/tests/e2e/kubectl"
)

func GetFilesDir(yamlPath string) []string {
Expand Down
2 changes: 0 additions & 2 deletions tests/e2e/ipam_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ var _ = Describe("Ipam", func() {
DeleteVMIP := func(manifest string) {
res := kubectl.Delete(manifest, kc.DeleteOptions{})
Expect(res.Error()).NotTo(HaveOccurred(), "failed delete vmip from file %s.\n%s", manifest, res.StdErr())

}
When("reclaimPolicy Delete", func() {
filepath := ipamPath("vmip-delete.yaml")
Expand All @@ -56,7 +55,6 @@ var _ = Describe("Ipam", func() {
Expect(res.Error()).To(HaveOccurred())
Expect(res.StdErr()).To(ContainSubstring("not found"))
})

})
When("reclaimPolicy Retain", func() {
filepath := ipamPath("vmip-retain.yaml")
Expand Down
2 changes: 2 additions & 0 deletions tests/e2e/kubectl/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,6 @@ const (
ResourceVMIP Resource = "virtualmachineipaddresses.virtualization.deckhouse.io"
ResourceVMIPLease Resource = "virtualmachineipaddressleases.virtualization.deckhouse.io"
ResourceCVI Resource = "clustervirtualimages.virtualization.deckhouse.io"
ResourceVI Resource = "virtualimages.virtualization.deckhouse.io"
ResourceVMBDA Resource = "virtualmachineblockdeviceattachments.virtualization.deckhouse.io"
)
6 changes: 4 additions & 2 deletions tests/e2e/testdata/templates/kustomization.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
namespace: testcases
namePrefix: pr-number-or-commit-hash-
commonLabels:
testcase: pr-number-or-commit-hash
resources:
- ns.yaml
configurations:
- transformer.yaml
labels:
- includeSelectors: true
pairs:
id: pr-number-or-commit-hash
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ configurations:
labels:
- includeSelectors: true
pairs:
testcase: pr-number-or-commit-hash
id: pr-number-or-commit-hash
12 changes: 12 additions & 0 deletions tests/e2e/testdata/vm-configuration/base/cfg/cloudinit.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#cloud-config
users:
- name: cloud
# passwd: cloud
passwd: $6$rounds=4096$vln/.aPHBOI7BMYR$bBMkqQvuGs5Gyd/1H5DP4m9HjQSy.kgrxpaGEHwkX7KEFV8BS.HZWPitAtZ2Vd8ZqIZRqmlykRCagTgPejt1i.
shell: /bin/bash
sudo: ALL=(ALL) NOPASSWD:ALL
chpasswd: { expire: False }
lock_passwd: false
ssh_authorized_keys:
# testcases
- ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIFxcXHmwaGnJ8scJaEN5RzklBPZpVSic4GdaAsKjQoeA your_email@example.com
15 changes: 15 additions & 0 deletions tests/e2e/testdata/vm-configuration/base/kustomization.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- ./vm.yaml
- ./vd-root.yaml
- ./vd-blank.yaml
configurations:
- transformer.yaml
generatorOptions:
disableNameSuffixHash: true
secretGenerator:
- files:
- userdata=cfg/cloudinit.yaml
name: cloud-init
type: Opaque
54 changes: 54 additions & 0 deletions tests/e2e/testdata/vm-configuration/base/transformer.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# https://github.com/kubernetes-sigs/kustomize/blob/master/examples/transformerconfigs/README.md#transformer-configurations

namespace:
- kind: ClusterVirtualImage
path: spec/dataSource/objectRef/namespace
nameReference:
- kind: VirtualImage
version: v1alpha2 # optional
fieldSpecs:
- path: spec/dataSource/objectRef/name
kind: ClusterVirtualImage
- path: spec/dataSource/objectRef/name
kind: VirtualImage
- path: spec/dataSource/objectRef/name
kind: VirtualDisk
- path: spec/blockDeviceRefs/name
kind: VirtualMachine
- kind: ClusterVirtualImage
version: v1alpha2 # optional
fieldSpecs:
- path: spec/dataSource/objectRef/name
kind: ClusterVirtualImage
- path: spec/dataSource/objectRef/name
kind: VirtualImage
- path: spec/dataSource/objectRef/name
kind: VirtualDisk
- path: spec/blockDeviceRefs/name
kind: VirtualMachine
- kind: VirtualDisk
version: v1alpha2 # optional
fieldSpecs:
- path: spec/blockDeviceRefs/name
kind: VirtualMachine
- path: spec/blockDeviceRef/name
kind: VirtualMachineBlockDeviceAttachment
- kind: Secret
fieldSpecs:
- path: spec/provisioning/userDataRef/name
kind: VirtualMachine
- kind: VirtualMachineIPAddress
version: v1alpha2
fieldSpecs:
- path: spec/virtualMachineIPAddressName
kind: VirtualMachine
- kind: VirtualMachine
version: v1alpha2
fieldSpecs:
- path: spec/virtualMachineName
kind: VirtualMachineBlockDeviceAttachment
- kind: VirtualMachineClass
version: v1alpha2
fieldSpecs:
- path: spec/virtualMachineClassName
kind: VirtualMachine
7 changes: 7 additions & 0 deletions tests/e2e/testdata/vm-configuration/base/vd-attach.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
apiVersion: virtualization.deckhouse.io/v1alpha2
kind: VirtualDisk
metadata:
name: vd-attach
spec:
persistentVolumeClaim:
size: 100Mi
7 changes: 7 additions & 0 deletions tests/e2e/testdata/vm-configuration/base/vd-blank.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
apiVersion: virtualization.deckhouse.io/v1alpha2
kind: VirtualDisk
metadata:
name: vd-blank
spec:
persistentVolumeClaim:
size: 100Mi
12 changes: 12 additions & 0 deletions tests/e2e/testdata/vm-configuration/base/vd-root.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
apiVersion: virtualization.deckhouse.io/v1alpha2
kind: VirtualDisk
metadata:
name: vd-root
spec:
persistentVolumeClaim:
size: 512Mi
dataSource:
type: ObjectRef
objectRef:
kind: VirtualImage
name: vi-alpine-http
Loading

0 comments on commit 6219dcd

Please sign in to comment.