Skip to content

Commit

Permalink
Make work command execution from winrmcli pod
Browse files Browse the repository at this point in the history
Signed-off-by: Lukianov Artyom <alukiano@redhat.com>
  • Loading branch information
Lukianov Artyom committed Mar 14, 2018
1 parent 30ad695 commit d67bde9
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 38 deletions.
50 changes: 29 additions & 21 deletions tests/utils.go
Expand Up @@ -42,16 +42,16 @@ import (
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/resource"

"k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/tools/remotecommand"


"kubevirt.io/kubevirt/pkg/api/v1"
"kubevirt.io/kubevirt/pkg/kubecli"
"kubevirt.io/kubevirt/pkg/log"
)

var KubeVirtVersionTag string = "latest"
var KubeVirtRepoPrefix string = "kubevirt"
var KubeVirtVersionTag = "latest"
var KubeVirtRepoPrefix = "kubevirt"

func init() {
flag.StringVar(&KubeVirtVersionTag, "tag", "latest", "Set the image tag or digest to use")
Expand All @@ -69,9 +69,9 @@ const defaultTestGracePeriod int64 = 0

const (
// tests.NamespaceTestDefault is the default namespace, to test non-infrastructure related KubeVirt objects.
NamespaceTestDefault string = "kubevirt-test-default"
NamespaceTestDefault = "kubevirt-test-default"
// NamespaceTestAlternative is used to test controller-namespace independency.
NamespaceTestAlternative string = "kubevirt-test-alternative"
NamespaceTestAlternative = "kubevirt-test-alternative"
)

var testNamespaces = []string{NamespaceTestDefault, NamespaceTestAlternative}
Expand Down Expand Up @@ -110,8 +110,6 @@ const (
defaultWindowsDiskSize = "30Gi"
)

const VmResource = "virtualmachines"

type ProcessFunc func(event *k8sv1.Event) (done bool)

type ObjectEventWatcher struct {
Expand Down Expand Up @@ -901,43 +899,53 @@ func LoggedInCirrosExpecter(vm *v1.VirtualMachine) (expect.Expecter, error) {
return expecter, err
}

func ExecuteCommandOnPod(virtCli kubecli.KubevirtClient, pod *k8sv1.Pod, containerName string, command string) (string, error) {
func ExecuteCommandOnPod(virtCli kubecli.KubevirtClient, pod *k8sv1.Pod, containerName string, command []string) (string, error) {
var (
execOut bytes.Buffer
execErr bytes.Buffer
stdout bytes.Buffer
stderr bytes.Buffer
)

execRequest := virtCli.CoreV1().RESTClient().Post().
req := virtCli.CoreV1().RESTClient().Post().
Resource("pods").
Name(pod.Name).
Namespace(pod.Namespace).
SubResource("exec").
Param("container", containerName).
Param("command", command).
Param("stdout", "true").
Param("stderr", "true")
Param("container", containerName)

req.VersionedParams(&k8sv1.PodExecOptions{
Container: containerName,
Command: command,
Stdin: false,
Stdout: true,
Stderr: true,
TTY: false,
}, scheme.ParameterCodec)

config, err := kubecli.GetKubevirtClientConfig()
if err != nil {
return "", err
}

exec, err := remotecommand.NewSPDYExecutor(config, "POST", execRequest.URL())
exec, err := remotecommand.NewSPDYExecutor(config, "POST", req.URL())
if err != nil {
return "", err
}

err = exec.Stream(remotecommand.StreamOptions{
Stdout: &execOut,
Stderr: &execErr,
Stdout: &stdout,
Stderr: &stderr,
Tty: false,
})

if err != nil {
return "", err
}
if execErr.Len() > 0 {
return "", fmt.Errorf("stderr: %v", execErr.String())

if stderr.Len() > 0 {
return "", fmt.Errorf("stderr: %v", stderr.String())
}
return execOut.String(), nil

return stdout.String(), nil
}

func BeforeAll(fn func()) {
Expand Down
76 changes: 59 additions & 17 deletions tests/windows_test.go
Expand Up @@ -22,7 +22,8 @@ package tests_test
import (
"flag"
"fmt"
//"time"
"strings"
"time"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
Expand Down Expand Up @@ -50,7 +51,7 @@ const (
winrmCliCmd = "/go/bin/winrm-cli"
)

var _ = FDescribe("Windows VM", func() {
var _ = Describe("Windows VM", func() {
flag.Parse()

virtClient, err := kubecli.GetKubevirtClient()
Expand Down Expand Up @@ -149,10 +150,11 @@ var _ = FDescribe("Windows VM", func() {

Context("with winrm connection", func() {
var winrmcliPod *k8sv1.Pod
var cli string
var cli []string
var output string

BeforeEach(func() {
tests.BeforeTestCleanup()
By("Creating winrm-cli pod for future use")
winrmcliPod = &k8sv1.Pod{
ObjectMeta: metav1.ObjectMeta{Name: winrmCli},
Spec: k8sv1.PodSpec{
Expand All @@ -169,31 +171,71 @@ var _ = FDescribe("Windows VM", func() {
winrmcliPod, err = virtClient.CoreV1().Pods(tests.NamespaceTestDefault).Create(winrmcliPod)
Expect(err).ToNot(HaveOccurred())

By("Starting windows VM")
vm, err := virtClient.VM(tests.NamespaceTestDefault).Create(windowsVm)
Expect(err).To(BeNil())
tests.WaitForSuccessfulVMStartWithTimeout(vm, 180)

vm, err = virtClient.VM(tests.NamespaceTestDefault).Get(vm.Name, metav1.GetOptions{})
cli = fmt.Sprintf("%s -hostname %s -username \"%s\" -password \"%s\"",
cli = []string{
winrmCliCmd,
"-hostname",
vm.Status.Interfaces[0].IP,
"-username",
windowsVmUser,
"-password",
windowsVmPassword,
)
}
})

XIt("should have correct UUID", func() {
command := fmt.Sprintf("%s 'wmic csproduct get \"UUID\"'", cli)
out, err := tests.ExecuteCommandOnPod(
virtClient,
winrmcliPod,
winrmcliPod.Spec.Containers[0].Name,
command,
)
Expect(err).To(BeNil())
Expect(out).To(Equal(windowsFirmware))
It("should have correct UUID", func() {
command := append(cli, "wmic csproduct get \"UUID\"")
By(fmt.Sprintf("Running \"%s\" command via winrm-cli", command))
Eventually(func() error {
output, err = tests.ExecuteCommandOnPod(
virtClient,
winrmcliPod,
winrmcliPod.Spec.Containers[0].Name,
command,
)
return err
}, time.Minute * 5, time.Second * 30).ShouldNot(HaveOccurred())
By("Checking that the Windows VM has expected UUID")
Expect(output).Should(ContainSubstring(strings.ToUpper(windowsFirmware)))
}, 300)

XIt("should have pod IP", func() {})
It("should have pod IP", func() {
command := append(cli, "ipconfig /all")
By(fmt.Sprintf("Running \"%s\" command via winrm-cli", command))
Eventually(func() error {
output, err = tests.ExecuteCommandOnPod(
virtClient,
winrmcliPod,
winrmcliPod.Spec.Containers[0].Name,
command,
)
return err
}, time.Minute * 5, time.Second * 30).ShouldNot(HaveOccurred())

By("Getting the Windows VM pod IP")
podIp := getWindowsVmPodIp(virtClient)

By("Checking that the Windows VM has expected IP address")
Expect(output).Should(ContainSubstring(podIp))
}, 300)
})
})

func getWindowsVmPodIp(virtClient kubecli.KubevirtClient) string {
vmsPods, err := virtClient.CoreV1().Pods(tests.NamespaceTestDefault).List(metav1.ListOptions{})
Expect(err).ToNot(HaveOccurred())

for _, pod := range vmsPods.Items {
if strings.Contains(pod.Name, fmt.Sprintf("virt-launcher-%s", windowsVm)) {
if pod.DeletionTimestamp == nil {
return pod.Status.PodIP
}
}
}
return ""
}

0 comments on commit d67bde9

Please sign in to comment.