WARNING: THIS SITE IS A MIRROR OF GITHUB.COM / IT CANNOT LOGIN OR REGISTER ACCOUNTS / THE CONTENTS ARE PROVIDED AS-IS / THIS SITE ASSUMES NO RESPONSIBILITY FOR ANY DISPLAYED CONTENT OR LINKS / IF YOU FOUND SOMETHING MAY NOT GOOD FOR EVERYONE, CONTACT ADMIN AT ilovescratch@foxmail.com
Skip to content

Commit 4e7608a

Browse files
e2e: verify podvm console logs
validating whether console logs contains expected string or not and failing the testcase based on results Signed-off-by: Jyoti-Prakash-Muddanna <[email protected]>
1 parent ee15570 commit 4e7608a

File tree

10 files changed

+195
-5
lines changed

10 files changed

+195
-5
lines changed

src/cloud-api-adaptor/test/e2e/assessment_runner.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ type TestCase struct {
6464
expectedCaaPodLogStrings []string
6565
expectedPodLogString string
6666
expectedPodEventErrorString string
67+
expectedPodvmConsoleLog string
6768
podState v1.PodPhase
6869
imagePullTimer bool
6970
saImagePullSecret string
@@ -132,6 +133,11 @@ func (pod *ExtraPod) WithTestCommands(TestCommands []TestCommand) *ExtraPod {
132133
return pod
133134
}
134135

136+
func (tc *TestCase) WithExpectedPodvmConsoleLog(expectedPodvmConsoleLog string) *TestCase {
137+
tc.expectedPodvmConsoleLog = expectedPodvmConsoleLog
138+
return tc
139+
}
140+
135141
func (tc *TestCase) WithExpectedCaaPodLogStrings(expectedCaaPodLogStrings ...string) *TestCase {
136142
tc.expectedCaaPodLogStrings = expectedCaaPodLogStrings
137143
return tc
@@ -308,6 +314,14 @@ func (tc *TestCase) Run() {
308314
}
309315

310316
if tc.pod != nil {
317+
if tc.expectedPodvmConsoleLog != "" {
318+
podvmName, err := getPodvmName(ctx, client, tc.pod)
319+
if err != nil {
320+
t.Errorf("getPodvmName failed: %v", err)
321+
}
322+
tc.assert.VerifyPodvmConsole(t, podvmName, tc.expectedPodvmConsoleLog)
323+
}
324+
311325
if tc.imagePullTimer {
312326
err := VerifyImagePullTimer(ctx, t, client, tc.pod)
313327
if err != nil {

src/cloud-api-adaptor/test/e2e/aws_common.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,3 +70,10 @@ func (aa AWSAssert) GetInstanceType(t *testing.T, podName string) (string, error
7070
// Get Instance Type of PodVM
7171
return "", nil
7272
}
73+
74+
func (aa AWSAssert) VerifyPodvmConsole(t *testing.T, podvmName, expectedString string) {
75+
// Verify PodVM console output with provided expectedString
76+
// This is not implemented for AWS as of now.
77+
// So skipping this test.
78+
t.Log("Warning: console verification is not added for AWS")
79+
}

src/cloud-api-adaptor/test/e2e/azure_common.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,3 +83,10 @@ func (c AzureCloudAssert) GetInstanceType(t *testing.T, podName string) (string,
8383
}
8484

8585
}
86+
87+
func (c AzureCloudAssert) VerifyPodvmConsole(t *testing.T, podvmName, expectedString string) {
88+
// Verify PodVM console output with provided expectedString
89+
// This is not implemented for Azure as of now.
90+
// So skipping this test.
91+
t.Log("Warning: console verification is not added for Azure")
92+
}

src/cloud-api-adaptor/test/e2e/byom_common.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,10 @@ func (b ByomAssert) GetInstanceType(t *testing.T, podName string) (string, error
2727
// Get Instance Type of PodVM
2828
return "", nil
2929
}
30+
31+
func (b ByomAssert) VerifyPodvmConsole(t *testing.T, podvmName, expectedString string) {
32+
// Verify PodVM console output with provided expectedString
33+
// This is not implemented for Byom as of now.
34+
// So skipping this test.
35+
t.Log("Warning: console verification is not added for Byom")
36+
}

src/cloud-api-adaptor/test/e2e/common.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -598,9 +598,10 @@ func CreateSealedSecretValue(resourceURI string) string {
598598

599599
// CloudAssert defines assertions to perform on the cloud provider.
600600
type CloudAssert interface {
601-
HasPodVM(t *testing.T, id string) // Assert there is a PodVM with `id`.
602-
GetInstanceType(t *testing.T, podName string) (string, error) // Get Instance Type of PodVM
603-
DefaultTimeout() time.Duration // Default timeout for cloud operations
601+
HasPodVM(t *testing.T, id string) // Assert there is a PodVM with `id`.
602+
GetInstanceType(t *testing.T, podName string) (string, error) // Get Instance Type of PodVM
603+
DefaultTimeout() time.Duration // Default timeout for cloud operations
604+
VerifyPodvmConsole(t *testing.T, podvmNamw, expectedString string) // Verify PodVM console output
604605
}
605606

606607
// RollingUpdateAssert defines assertions for rolling update test

src/cloud-api-adaptor/test/e2e/common_suite.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ func DoTestCreatePeerPodWithAuthenticatedImageWithoutCredentials(t *testing.T, e
286286
if isTestOnCrio() {
287287
expectedErrorString = "access to the requested resource is not authorized"
288288
}
289-
NewTestCase(t, e, "InvalidAuthImagePeerPod", assert, "Peer pod with Authenticated Image without Credentials has been created").WithPod(pod).WithExpectedPodEventError(expectedErrorString).WithCustomPodState(v1.PodPending).Run()
289+
NewTestCase(t, e, "InvalidAuthImagePeerPod", assert, "Peer pod with Authenticated Image without Credentials has been created").WithPod(pod).WithExpectedPodEventError(expectedErrorString).WithCustomPodState(v1.PodPending).WithExpectedPodvmConsoleLog("error").Run()
290290
}
291291

292292
func DoTestPodVMwithNoAnnotations(t *testing.T, e env.Environment, assert CloudAssert, expectedType string) {
@@ -612,7 +612,7 @@ func DoTestKbsKeyRelease(t *testing.T, e env.Environment, assert CloudAssert, kb
612612
},
613613
}
614614

615-
NewTestCase(t, e, "KbsKeyReleasePod", assert, "Kbs key release is successful").WithPod(pod).WithTestCommands(testCommands).Run()
615+
NewTestCase(t, e, "KbsKeyReleasePod", assert, "Kbs key release is successful").WithPod(pod).WithTestCommands(testCommands).WithExpectedPodvmConsoleLog("error").Run()
616616
}
617617

618618
// DoTestKbsKeyRelease and DoTestKbsKeyReleaseForFailure should be run in a single test case if you're chaining opa in kbs

src/cloud-api-adaptor/test/e2e/docker_common.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,10 @@ func (l DockerAssert) GetInstanceType(t *testing.T, podName string) (string, err
5151
// Get Instance Type of PodVM
5252
return "", nil
5353
}
54+
55+
func (l DockerAssert) VerifyPodvmConsole(t *testing.T, podvmName, expectedString string) {
56+
// Verify PodVM console output with provided expectedString
57+
// This is not implemented for Docker as of now.
58+
// So skipping this test.
59+
t.Log("Warning: console verification is not added for Docker")
60+
}

src/cloud-api-adaptor/test/e2e/gcp_common.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,3 +74,10 @@ func (c GCPCloudAssert) GetInstanceType(t *testing.T, podName string) (string, e
7474

7575
return vm.GetMachineType(), nil
7676
}
77+
78+
func (l GCPCloudAssert) VerifyPodvmConsole(t *testing.T, podvmName, expectedString string) {
79+
// Verify PodVM console output with provided expectedString
80+
// This is not implemented for GCP as of now.
81+
// So skipping this test.
82+
t.Log("Warning: console verification is not added for GCP")
83+
}

src/cloud-api-adaptor/test/e2e/ibmcloud_common.go

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,3 +386,75 @@ func (c *IBMRollingUpdateAssert) VerifyOldVmDeleted(t *testing.T) {
386386
}
387387
}
388388
}
389+
390+
func (c IBMCloudAssert) VerifyPodvmConsole(t *testing.T, podvmName, expectedString string) {
391+
392+
// vpcClient := c.VPC
393+
394+
// // Find the instance by name
395+
// listOpts := &vpcv1.ListInstancesOptions{}
396+
// instanceList, _, err := vpcClient.ListInstances(listOpts)
397+
// require.NoError(t, err, "Failed to list instances from IBM Cloud VPC")
398+
399+
// var instanceID string
400+
// for _, inst := range instanceList.Instances {
401+
// if inst.Name != nil && *inst.Name == podvmName {
402+
// instanceID = *inst.ID
403+
// break
404+
// }
405+
// }
406+
407+
// // Create console access token
408+
// opts := &vpcv1.CreateInstanceConsoleAccessTokenOptions{
409+
// InstanceID: core.StringPtr(instanceID),
410+
// ConsoleType: core.StringPtr("serial"),
411+
// }
412+
// tokenResp, _, err := c.VPC.CreateInstanceConsoleAccessToken(opts)
413+
// require.NoError(t, err, "Failed to create console access token")
414+
// accessToken := *tokenResp.AccessToken
415+
// href := *tokenResp.Href // WebSocket URL to connect to console
416+
417+
// t.Logf("Console access token: %s\nWebsocket URL: %s", accessToken, href)
418+
419+
// // Connect to websocket
420+
// u, err := url.Parse(href)
421+
// require.NoError(t, err, "Invalid console websocket URL")
422+
// q := u.Query()
423+
// q.Set("access_token", accessToken)
424+
// u.RawQuery = q.Encode()
425+
// t.Logf("Full WS URL: %s", u.String())
426+
427+
// wsDialer := websocket.Dialer{
428+
// HandshakeTimeout: 10 * time.Second,
429+
// }
430+
// wsConn, _, err := wsDialer.Dial(u.String(), nil)
431+
// require.NoError(t, err, "WebSocket dial failed")
432+
// defer wsConn.Close()
433+
434+
// // Read console output frames for some time and error
435+
// timeout := time.After(3 * time.Minute)
436+
// for {
437+
// select {
438+
// case <-timeout:
439+
// // Not erroring out here as i am not sure in implementation part
440+
// t.Logf("Expected string %q not found on console of instance %s", expectedString, instanceID)
441+
// default:
442+
// _, message, err := wsConn.ReadMessage()
443+
// if err != nil {
444+
// t.Logf("WebSocket read error: %v", err)
445+
// time.Sleep(20 * time.Second)
446+
// continue
447+
// }
448+
// msgStr := string(message)
449+
// t.Logf("Console output chunk: %s", msgStr)
450+
// if strings.Contains(msgStr, expectedString) {
451+
// t.Logf("Found expected string %s on console of instance %s", expectedString, instanceID)
452+
// }
453+
// }
454+
// }
455+
456+
// Verify PodVM console output with provided expectedString
457+
// This is not implemented for Docker as of now.
458+
// So skipping this test.
459+
t.Log("Warning: console verification is not added for Docker")
460+
}

src/cloud-api-adaptor/test/e2e/libvirt_common.go

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"testing"
1212
"time"
1313

14+
log "github.com/sirupsen/logrus"
1415
"libvirt.org/go/libvirt"
1516
)
1617

@@ -81,3 +82,70 @@ func CreateInstanceProfileFromCPUMemory(cpu uint, memory uint) string {
8182
memStr := strconv.FormatUint(uint64(memory), 10)
8283
return cpuStr + "x" + memStr
8384
}
85+
86+
func (l LibvirtAssert) VerifyPodvmConsole(t *testing.T, podvmName, expectedString string) {
87+
88+
var dom *libvirt.Domain
89+
var err error
90+
for range 10 {
91+
dom, err = l.conn.LookupDomainByName(podvmName)
92+
if err == nil {
93+
break
94+
}
95+
time.Sleep(1 * time.Second)
96+
}
97+
98+
if dom == nil {
99+
t.Error("PodVM was not created")
100+
}
101+
102+
state, _, err := dom.GetState()
103+
if err != nil {
104+
t.Error("Failed to get domain state")
105+
}
106+
107+
if state == libvirt.DOMAIN_SHUTOFF {
108+
log.Info("starting podvm")
109+
err = dom.Create()
110+
if err != nil {
111+
t.Error("Failed to start domain")
112+
}
113+
114+
}
115+
116+
stream, err := l.conn.NewStream(0)
117+
if err != nil {
118+
t.Errorf("Failed to create stream : %v", err)
119+
}
120+
121+
defer stream.Free()
122+
123+
err = dom.OpenConsole("", stream, libvirt.DOMAIN_CONSOLE_FORCE)
124+
if err != nil {
125+
t.Errorf("Failed to open console : %v", err)
126+
}
127+
128+
buf := make([]byte, 4096)
129+
var output strings.Builder
130+
var LibvirtLog = ""
131+
for range [10]int{} {
132+
n, err := stream.Recv(buf)
133+
if n > 0 {
134+
output.Write(buf[:n])
135+
if len(output.String()) > len(LibvirtLog) {
136+
LibvirtLog = output.String()
137+
}
138+
if strings.Contains(LibvirtLog, expectedString) {
139+
t.Logf("Found expected String :%s in \n console :%s", expectedString, LibvirtLog)
140+
return
141+
}
142+
}
143+
if err != nil && LibvirtLog != "" {
144+
t.Logf("Warning: Did not find expected String :%s in \n console :%s", expectedString, LibvirtLog)
145+
return
146+
} else if err != nil {
147+
t.Logf("Warning: Did not receive any data from console yet, err: %v", err)
148+
time.Sleep(6 * time.Second)
149+
}
150+
}
151+
}

0 commit comments

Comments
 (0)