manager_e2e_test.go
1 // Copyright 2026 Alibaba Group Holding Ltd. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package e2e 16 17 import ( 18 "context" 19 "testing" 20 "time" 21 22 "github.com/alibaba/OpenSandbox/sdks/sandbox/go" 23 "github.com/stretchr/testify/require" 24 ) 25 26 func TestManager_ListSandboxes(t *testing.T) { 27 config := getConnectionConfig(t) 28 ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) 29 defer cancel() 30 31 mgr := opensandbox.NewSandboxManager(config) 32 defer mgr.Close() 33 34 result, err := mgr.ListSandboxInfos(ctx, opensandbox.ListOptions{ 35 Page: 1, 36 PageSize: 10, 37 }) 38 require.NoError(t, err) 39 40 t.Logf("Listed %d sandboxes (page %d/%d)", 41 len(result.Items), result.Pagination.Page, result.Pagination.TotalPages) 42 } 43 44 func TestManager_ListByState(t *testing.T) { 45 config := getConnectionConfig(t) 46 ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute) 47 defer cancel() 48 49 sb, err := opensandbox.CreateSandbox(ctx, config, opensandbox.SandboxCreateOptions{ 50 Image: getSandboxImage(), 51 Metadata: map[string]string{ 52 "test": "go-e2e-manager", 53 }, 54 }) 55 require.NoError(t, err) 56 defer sb.Kill(context.Background()) 57 58 mgr := opensandbox.NewSandboxManager(config) 59 defer mgr.Close() 60 61 result, err := mgr.ListSandboxInfos(ctx, opensandbox.ListOptions{ 62 States: []opensandbox.SandboxState{opensandbox.StateRunning}, 63 }) 64 require.NoError(t, err) 65 66 require.NotEmpty(t, result.Items, "expected at least one Running sandbox") 67 68 for _, item := range result.Items { 69 require.Equal(t, opensandbox.StateRunning, item.Status.State, "sandbox %s", item.ID) 70 } 71 t.Logf("Found %d Running sandboxes", len(result.Items)) 72 } 73 74 func TestManager_GetAndKill(t *testing.T) { 75 config := getConnectionConfig(t) 76 ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute) 77 defer cancel() 78 79 sb, err := opensandbox.CreateSandbox(ctx, config, opensandbox.SandboxCreateOptions{ 80 Image: getSandboxImage(), 81 }) 82 require.NoError(t, err) 83 84 mgr := opensandbox.NewSandboxManager(config) 85 defer mgr.Close() 86 87 info, err := mgr.GetSandboxInfo(ctx, sb.ID()) 88 require.NoError(t, err) 89 require.Equal(t, sb.ID(), info.ID) 90 t.Logf("Got sandbox %s via manager (state=%s)", info.ID, info.Status.State) 91 92 require.NoError(t, mgr.KillSandbox(ctx, sb.ID())) 93 t.Log("Killed sandbox via manager") 94 } 95 96 func TestManager_PauseAndResume(t *testing.T) { 97 config := getConnectionConfig(t) 98 ctx, cancel := context.WithTimeout(context.Background(), 3*time.Minute) 99 defer cancel() 100 101 sb, err := opensandbox.CreateSandbox(ctx, config, opensandbox.SandboxCreateOptions{ 102 Image: getSandboxImage(), 103 }) 104 require.NoError(t, err) 105 defer sb.Kill(context.Background()) 106 107 mgr := opensandbox.NewSandboxManager(config) 108 defer mgr.Close() 109 110 if err := mgr.PauseSandbox(ctx, sb.ID()); err != nil { 111 t.Skipf("PauseSandbox not supported in this environment: %v", err) 112 } 113 114 paused := false 115 for i := 0; i < 20; i++ { 116 info, infoErr := mgr.GetSandboxInfo(ctx, sb.ID()) 117 require.NoError(t, infoErr) 118 if info.Status.State == opensandbox.StatePaused { 119 paused = true 120 break 121 } 122 time.Sleep(500 * time.Millisecond) 123 } 124 require.True(t, paused, "sandbox did not reach Paused state") 125 126 require.NoError(t, mgr.ResumeSandbox(ctx, sb.ID())) 127 128 resumed := false 129 for i := 0; i < 20; i++ { 130 info, infoErr := mgr.GetSandboxInfo(ctx, sb.ID()) 131 require.NoError(t, infoErr) 132 if info.Status.State == opensandbox.StateRunning { 133 resumed = true 134 break 135 } 136 time.Sleep(500 * time.Millisecond) 137 } 138 require.True(t, resumed, "sandbox did not return to Running state") 139 } 140 141 func TestManager_RenewSandbox(t *testing.T) { 142 config := getConnectionConfig(t) 143 ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute) 144 defer cancel() 145 146 sb, err := opensandbox.CreateSandbox(ctx, config, opensandbox.SandboxCreateOptions{ 147 Image: getSandboxImage(), 148 }) 149 require.NoError(t, err) 150 defer sb.Kill(context.Background()) 151 152 mgr := opensandbox.NewSandboxManager(config) 153 defer mgr.Close() 154 155 resp, err := mgr.RenewSandbox(ctx, sb.ID(), 30*time.Minute) 156 if err != nil { 157 t.Skipf("RenewSandbox not supported in this environment: %v", err) 158 } 159 require.NotNil(t, resp) 160 require.False(t, resp.ExpiresAt.IsZero(), "renew response should include expiresAt") 161 }