Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Improve functional tests #14

Merged
merged 1 commit into from
Mar 29, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 35 additions & 40 deletions tests/proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,12 @@ import (
var _ = Describe("Token", func() {
const (
testHostname = "vm-console.test"
urlBase = testHostname + "/api/v1alpha1/"
urlBase = testHostname + "/api/v1alpha1"
httpsUrlBase = "https://" + urlBase

vmiName = "vm-cirros"
tokenEndpoint = "token"

tokenUrlTemplate = httpsUrlBase + "/" + testNamespace + "/%s/" + tokenEndpoint
)

var (
Expand Down Expand Up @@ -143,17 +144,9 @@ var _ = Describe("Token", func() {
})

Context("/token endpoint", func() {
var (
tokenUrl string
)

BeforeEach(func() {
var err error
tokenUrl, err = url.JoinPath(httpsUrlBase, testNamespace, vmiName, tokenEndpoint)
Expect(err).ToNot(HaveOccurred())
})

It("should fail if not authenticated", func() {
tokenUrl := fmt.Sprintf(tokenUrlTemplate, "test-vm")
code, body, err := httpGet(tokenUrl, "", httpClient)
Expect(err).ToNot(HaveOccurred())
Expect(code).To(Equal(http.StatusUnauthorized))
Expand All @@ -164,22 +157,29 @@ var _ = Describe("Token", func() {
err := ApiClient.RbacV1().RoleBindings(roleBinding.Namespace).Delete(context.TODO(), roleBinding.Name, metav1.DeleteOptions{})
Expect(err).ToNot(HaveOccurred())

tokenUrl := fmt.Sprintf(tokenUrlTemplate, "test-vm")
code, body, err := httpGet(tokenUrl, saToken, httpClient)
Expect(err).ToNot(HaveOccurred())
Expect(code).To(Equal(http.StatusUnauthorized))
Expect(string(body)).To(ContainSubstring("does not have permission to access virtualmachineinstances/vnc endpoint"))
})

It("should fail if VMI does not exist", func() {
tokenUrl := fmt.Sprintf(tokenUrlTemplate, "test-vm")
code, body, err := httpGet(tokenUrl, saToken, httpClient)
Expect(err).ToNot(HaveOccurred())
Expect(code).To(Equal(http.StatusNotFound))
Expect(string(body)).To(ContainSubstring("VirtualMachineInstance does no exist"))
})

Context("with running VM", func() {

var (
vmName string
)

BeforeEach(func() {
vm := testVm(vmiName)
vm := testVm("test-vm-")
vm, err := ApiClient.VirtualMachine(testNamespace).Create(vm)
Expect(err).ToNot(HaveOccurred())

Expand All @@ -188,19 +188,18 @@ var _ = Describe("Token", func() {
if err != nil && !errors.IsNotFound(err) {
Expect(err).ToNot(HaveOccurred())
}
Eventually(func() bool {
_, err := ApiClient.VirtualMachineInstance(vm.Namespace).Get(vm.Name, &metav1.GetOptions{})
return errors.IsNotFound(err)
}, time.Minute, time.Second).Should(BeTrue())
})

Eventually(func() error {
_, err := ApiClient.VirtualMachineInstance(vm.Namespace).Get(vm.Name, &metav1.GetOptions{})
return err
}, time.Minute, time.Second).Should(Succeed())
}, 10*time.Minute, time.Second).Should(Succeed())

vmName = vm.Name
})

It("should get token with default duration", func() {
tokenUrl := fmt.Sprintf(tokenUrlTemplate, vmName)
code, body, err := httpGet(tokenUrl, saToken, httpClient)
Expect(err).ToNot(HaveOccurred())
Expect(code).To(Equal(http.StatusOK))
Expand All @@ -211,7 +210,7 @@ var _ = Describe("Token", func() {
})

It("should get token with specified duration", func() {
tokenUrl, err := url.JoinPath(httpsUrlBase, testNamespace, vmiName, tokenEndpoint)
tokenUrl, err := url.JoinPath(httpsUrlBase, testNamespace, vmName, tokenEndpoint)
Expect(err).ToNot(HaveOccurred())

code, body, err := httpGet(tokenUrl+"?duration=24h", saToken, httpClient)
Expand Down Expand Up @@ -241,26 +240,24 @@ var _ = Describe("Token", func() {
wssUrlBase = "wss://" + urlBase
vncEndpoint = "vnc"

vncUrlTemplate = wssUrlBase + "/" + testNamespace + "/%s/" + vncEndpoint

subprotocolPrefix = "base64url.bearer.authorization.k8s.io."
)

var (
vncUrl string
dialer *websocket.Dialer
)

BeforeEach(func() {
var err error
vncUrl, err = url.JoinPath(wssUrlBase, testNamespace, vmiName, vncEndpoint)
Expect(err).ToNot(HaveOccurred())

dialer = &websocket.Dialer{
NetDialContext: portForwardDial,
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
})

It("should fail if no token is provided", func() {
vncUrl := fmt.Sprintf(vncUrlTemplate, "vm-1")
conn, response, err := dialer.Dial(vncUrl, nil)
if conn != nil {
_ = conn.Close()
Expand All @@ -273,6 +270,7 @@ var _ = Describe("Token", func() {
})

It("should fail if token is invalid", func() {
vncUrl := fmt.Sprintf(vncUrlTemplate, "vm-2")
conn, response, err := dialer.Dial(vncUrl, nil)
if conn != nil {
_ = conn.Close()
Expand All @@ -292,30 +290,26 @@ var _ = Describe("Token", func() {

BeforeEach(func() {
// Create and start VM
vm = testVm(vmiName)
vm, err := ApiClient.VirtualMachine(testNamespace).Create(vm)
vm = testVm("test-vm-")

var err error
vm, err = ApiClient.VirtualMachine(testNamespace).Create(vm)
Expect(err).ToNot(HaveOccurred())
DeferCleanup(func() {
err := ApiClient.VirtualMachine(testNamespace).Delete(vm.Name, &metav1.DeleteOptions{})
if err != nil && !errors.IsNotFound(err) {
Expect(err).ToNot(HaveOccurred())
}
Eventually(func() bool {
_, err := ApiClient.VirtualMachineInstance(vm.Namespace).Get(vm.Name, &metav1.GetOptions{})
return errors.IsNotFound(err)
}, time.Minute, time.Second).Should(BeTrue())
})

Eventually(func(g Gomega) {
vmi, err := ApiClient.VirtualMachineInstance(vm.Namespace).Get(vm.Name, &metav1.GetOptions{})
g.Expect(err).ToNot(HaveOccurred())
g.Expect(vmi.Status.Phase).To(Equal(kubevirtcorev1.Running))
}, time.Minute, time.Second).Should(Succeed())
}, 10*time.Minute, time.Second).Should(Succeed())

// Get the vnc token
tokenUrl, err := url.JoinPath(httpsUrlBase, testNamespace, vmiName, tokenEndpoint)
Expect(err).ToNot(HaveOccurred())

tokenUrl := fmt.Sprintf(tokenUrlTemplate, vm.Name)
code, body, err := httpGet(tokenUrl, saToken, httpClient)
Expect(err).ToNot(HaveOccurred())
Expect(code).To(Equal(http.StatusOK))
Expand All @@ -336,11 +330,12 @@ var _ = Describe("Token", func() {
Eventually(func() bool {
_, err := ApiClient.VirtualMachineInstance(testNamespace).Get(vm.Name, &metav1.GetOptions{})
return errors.IsNotFound(err)
}, time.Minute, time.Second).Should(BeTrue())
}, 10*time.Minute, time.Second).Should(BeTrue())

// Try to access the VNC
dialer.Subprotocols = []string{subprotocolPrefix + vncToken}

vncUrl := fmt.Sprintf(vncUrlTemplate, vm.Name)
conn, response, err := dialer.Dial(vncUrl, nil)
if conn != nil {
Expect(conn.Close()).To(Succeed())
Expand All @@ -355,6 +350,7 @@ var _ = Describe("Token", func() {
It("should proxy VNC connection", func() {
dialer.Subprotocols = []string{subprotocolPrefix + vncToken, "base64.binary.k8s.io"}

vncUrl := fmt.Sprintf(vncUrlTemplate, vm.Name)
conn, _, err := dialer.Dial(vncUrl, nil)
Expect(err).ToNot(HaveOccurred())

Expand All @@ -369,9 +365,7 @@ var _ = Describe("Token", func() {

It("should fail if token is expired", func() {
// Get the vnc token
tokenUrl, err := url.JoinPath(httpsUrlBase, testNamespace, vmiName, tokenEndpoint)
Expect(err).ToNot(HaveOccurred())

tokenUrl := fmt.Sprintf(tokenUrlTemplate, vm.Name)
code, tokenBody, err := httpGet(tokenUrl+"?duration=1s", saToken, httpClient)
Expect(err).ToNot(HaveOccurred())
Expect(code).To(Equal(http.StatusOK))
Expand All @@ -387,6 +381,7 @@ var _ = Describe("Token", func() {

dialer.Subprotocols = []string{subprotocolPrefix + vncTokenWithTimeout}

vncUrl := fmt.Sprintf(vncUrlTemplate, vm.Name)
conn, response, err := dialer.Dial(vncUrl, nil)
if conn != nil {
_ = conn.Close()
Expand Down Expand Up @@ -424,7 +419,7 @@ func httpGet(url string, authToken string, client *http.Client) (int, []byte, er
return response.StatusCode, body, nil
}

func testVm(name string) *kubevirtcorev1.VirtualMachine {
func testVm(namePrefix string) *kubevirtcorev1.VirtualMachine {
const (
containerDiskName = "containerdisk"
cloudinitDiskName = "cloudinitdisk"
Expand All @@ -434,8 +429,8 @@ func testVm(name string) *kubevirtcorev1.VirtualMachine {

return &kubevirtcorev1.VirtualMachine{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: testNamespace,
GenerateName: namePrefix,
Namespace: testNamespace,
},
Spec: kubevirtcorev1.VirtualMachineSpec{
Running: pointer.BoolPtr(true),
Expand Down