@@ -20,98 +20,96 @@ import (
2020 "io"
2121 "net/http"
2222 "os"
23- "os/exec"
24- "strings"
2523 "testing"
2624 "time"
2725
26+ "github.com/stretchr/testify/require"
27+ corev1 "k8s.io/api/core/v1"
2828 "k8s.io/apimachinery/pkg/types"
29- " k8s.io/klog/v2 "
29+ sandboxv1alpha1 "sigs. k8s.io/agent-sandbox/api/v1alpha1 "
3030 "sigs.k8s.io/agent-sandbox/test/e2e/framework"
31+ "sigs.k8s.io/agent-sandbox/test/e2e/framework/predicates"
3132)
3233
34+ func chromeSandbox () * sandboxv1alpha1.Sandbox {
35+ sandbox := & sandboxv1alpha1.Sandbox {}
36+ sandbox .Name = "chrome-sandbox"
37+ sandbox .Spec .PodTemplate = sandboxv1alpha1.PodTemplate {
38+ Spec : corev1.PodSpec {
39+ Containers : []corev1.Container {
40+ {
41+ Name : "chrome-sandbox" ,
42+ // might be nice to remove the IMAGE_TAG env var so this is easier to run from IDE
43+ Image : fmt .Sprintf ("kind.local/chrome-sandbox:%s" , os .Getenv ("IMAGE_TAG" )),
44+ ImagePullPolicy : corev1 .PullIfNotPresent ,
45+ },
46+ },
47+ },
48+ }
49+ return sandbox
50+ }
51+
3352// TestRunChromeSandbox tests that we can run Chrome inside a Sandbox,
3453// it also measures how long it takes for Chrome to start serving the CDP protocol.
3554func TestRunChromeSandbox (t * testing.T ) {
36- ctx := context .Background ()
37- ctx , cancel := context .WithCancel (ctx )
38- defer cancel ()
39-
40- log := klog .FromContext (ctx )
41-
42- h := framework .NewTestContext (t )
55+ tc := framework .NewTestContext (t )
4356
44- ns := fmt .Sprintf ("chrome-sandbox-test-%d" , time .Now ().UnixNano ())
45- h .CreateTempNamespace (ctx , ns )
57+ ns := & corev1.Namespace {}
58+ ns .Name = fmt .Sprintf ("chrome-sandbox-test-%d" , time .Now ().UnixNano ())
59+ require .NoError (t , tc .CreateWithCleanup (t .Context (), ns ))
4660
4761 startTime := time .Now ()
48-
49- manifest := `
50- kind: Sandbox
51- apiVersion: agents.x-k8s.io/v1alpha1
52- metadata:
53- name: chrome-sandbox
54- spec:
55- podTemplate:
56- spec:
57- containers:
58- - name: chrome-sandbox
59- image: kind.local/chrome-sandbox:latest
60- imagePullPolicy: IfNotPresent
61- `
62-
63- manifest = strings .ReplaceAll (manifest , ":latest" , ":" + os .Getenv ("IMAGE_TAG" ))
64-
65- h .Apply (ctx , ns , manifest )
66-
67- sandboxID := types.NamespacedName {
68- Namespace : ns ,
69- Name : "chrome-sandbox" ,
70- }
71-
72- h .WaitForSandboxReady (ctx , sandboxID )
62+ sandboxObj := chromeSandbox ()
63+ sandboxObj .Namespace = ns .Name
64+ require .NoError (t , tc .CreateWithCleanup (t .Context (), sandboxObj ))
65+ require .NoError (t , tc .WaitForObject (t .Context (), sandboxObj , predicates .ReadyConditionIsTrue ))
7366
7467 podID := types.NamespacedName {
75- Namespace : ns ,
68+ Namespace : ns . Name ,
7669 Name : "chrome-sandbox" ,
7770 }
78-
71+ podObj := & corev1.Pod {}
72+ podObj .Name = podID .Name
73+ podObj .Namespace = podID .Namespace
7974 // Wait for the pod to be ready
80- {
81- waitForPodReady := exec .CommandContext (ctx , "kubectl" , "-n" , ns , "wait" , "pod/" + podID .Name , "--for=condition=Ready" , "--timeout=60s" )
82- log .Info ("waiting for pod to be ready" , "command" , waitForPodReady .String ())
83- waitForPodReady .Stdout = os .Stdout
84- waitForPodReady .Stderr = os .Stderr
85- if err := waitForPodReady .Run (); err != nil {
86- t .Fatalf ("failed to wait-for-pod-ready: %v" , err )
87- }
88- }
75+ require .NoError (t , tc .WaitForObject (t .Context (), podObj , predicates .ReadyConditionIsTrue ))
76+ // Wait for chrome to be ready
77+ require .NoError (t , waitForChromeReady (t .Context (), tc , podID ))
78+ duration := time .Since (startTime )
79+ t .Logf ("Test took %s" , duration )
80+ }
8981
82+ func waitForChromeReady (ctx context.Context , tc * framework.TestContext , podID types.NamespacedName ) error {
83+ tc .Helper ()
9084 // Loop until we can query chrome for its version via the debug port
85+ pollDuration := 100 * time .Millisecond
9186 for {
92- if ctx .Err () != nil {
93- t .Fatalf ("context cancelled" )
87+ select {
88+ case <- ctx .Done ():
89+ return fmt .Errorf ("context cancelled" )
90+ default :
91+ // We have to port-forward in the loop because port-forward exits when it sees an error
92+ // https://github.com/kubernetes/kubectl/issues/1249
93+ portForwardCtx , portForwardCancel := context .WithCancel (ctx )
94+ if err := tc .PortForward (portForwardCtx , podID , 9222 , 9222 ); err != nil {
95+ tc .Errorf ("failed to port forward: %s" , err )
96+ portForwardCancel ()
97+ time .Sleep (pollDuration )
98+ continue
99+ }
100+
101+ u := "http://localhost:9222/json/version"
102+ info , err := getChromeInfo (ctx , u )
103+ portForwardCancel ()
104+ if err != nil {
105+ tc .Errorf ("failed to get chrome info: %s" , err )
106+ time .Sleep (pollDuration )
107+ continue
108+ }
109+ tc .Logf ("Chrome is ready (%s). Response: %s" , u , info )
110+ return nil
94111 }
95-
96- // We have to port-forward in the loop because port-forward exits when it sees an error
97- // https://github.com/kubernetes/kubectl/issues/1249
98- portForwardCtx , portForwardCancel := context .WithCancel (ctx )
99- h .PortForward (portForwardCtx , podID , 9222 , 9222 )
100-
101- u := "http://localhost:9222/json/version"
102- info , err := getChromeInfo (ctx , u )
103- portForwardCancel ()
104- if err != nil {
105- log .Error (err , "failed to get Chrome info" )
106- time .Sleep (100 * time .Millisecond )
107- continue
108- }
109- log .Info ("Chrome is ready" , "url" , u , "response" , info )
110- break
111112 }
112-
113- duration := time .Since (startTime )
114- log .Info ("Test completed successfully" , "duration" , duration )
115113}
116114
117115// getChromeInfo connects to the Chrome Debug Port and retrieves the version information.
0 commit comments