Skip to content

Commit 57b83c1

Browse files
authored
Fix stop / start instance with network (#64)
* Fix stop / start instance with network * Initial networking for test
1 parent 9edbbfa commit 57b83c1

File tree

5 files changed

+22
-7
lines changed

5 files changed

+22
-7
lines changed

cmd/api/api/api_test.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,10 @@ import (
2525
// newTestService creates an ApiService for testing with automatic cleanup
2626
func newTestService(t *testing.T) *ApiService {
2727
cfg := &config.Config{
28-
DataDir: t.TempDir(),
28+
DataDir: t.TempDir(),
29+
BridgeName: "vmbr0",
30+
SubnetCIDR: "10.100.0.0/16",
31+
DNSServer: "1.1.1.1",
2932
}
3033

3134
p := paths.New(cfg.DataDir)
@@ -44,6 +47,11 @@ func newTestService(t *testing.T) *ApiService {
4447
}
4548
instanceMgr := instances.NewManager(p, imageMgr, systemMgr, networkMgr, deviceMgr, volumeMgr, limits, "", nil, nil)
4649

50+
// Initialize network manager (creates bridge for network-enabled tests)
51+
if err := networkMgr.Initialize(ctx(), nil); err != nil {
52+
t.Logf("Warning: failed to initialize network manager: %v (network tests may fail)", err)
53+
}
54+
4755
// Register cleanup for orphaned Cloud Hypervisor processes
4856
t.Cleanup(func() {
4957
cleanupOrphanedProcesses(t, cfg.DataDir)
@@ -54,6 +62,7 @@ func newTestService(t *testing.T) *ApiService {
5462
ImageManager: imageMgr,
5563
InstanceManager: instanceMgr,
5664
VolumeManager: volumeMgr,
65+
NetworkManager: networkMgr,
5766
DeviceManager: deviceMgr,
5867
ResourceManager: resourceMgr,
5968
}

cmd/api/api/instances_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ func TestInstanceLifecycle_StopStart(t *testing.T) {
148148

149149
// 1. Create instance
150150
t.Log("Creating instance...")
151-
networkEnabled := false
151+
networkEnabled := true
152152
createResp, err := svc.CreateInstance(ctx(), oapi.CreateInstanceRequestObject{
153153
Body: &oapi.CreateInstanceRequest{
154154
Name: "test-lifecycle",

lib/network/allocate.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ func (m *manager) CreateAllocation(ctx context.Context, req AllocateRequest) (*N
2727
return nil, fmt.Errorf("get default network: %w", err)
2828
}
2929

30-
// 2. Check name uniqueness
31-
exists, err := m.NameExists(ctx, req.InstanceName)
30+
// 2. Check name uniqueness (exclude current instance to allow restarts)
31+
exists, err := m.NameExists(ctx, req.InstanceName, req.InstanceID)
3232
if err != nil {
3333
return nil, fmt.Errorf("check name exists: %w", err)
3434
}

lib/network/derive.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,14 +113,20 @@ func (m *manager) ListAllocations(ctx context.Context) ([]Allocation, error) {
113113
return allocations, nil
114114
}
115115

116-
// NameExists checks if instance name is already used in the default network
117-
func (m *manager) NameExists(ctx context.Context, name string) (bool, error) {
116+
// NameExists checks if instance name is already used in the default network.
117+
// excludeInstanceID allows excluding a specific instance from the check (used when
118+
// starting an existing instance to avoid it conflicting with itself).
119+
func (m *manager) NameExists(ctx context.Context, name string, excludeInstanceID string) (bool, error) {
118120
allocations, err := m.ListAllocations(ctx)
119121
if err != nil {
120122
return false, err
121123
}
122124

123125
for _, alloc := range allocations {
126+
// Skip the excluded instance (e.g., when restarting an instance)
127+
if excludeInstanceID != "" && alloc.InstanceID == excludeInstanceID {
128+
continue
129+
}
124130
if alloc.InstanceName == name {
125131
return true, nil
126132
}

lib/network/manager.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ type Manager interface {
2929
// Queries (derive from CH/snapshots)
3030
GetAllocation(ctx context.Context, instanceID string) (*Allocation, error)
3131
ListAllocations(ctx context.Context) ([]Allocation, error)
32-
NameExists(ctx context.Context, name string) (bool, error)
32+
NameExists(ctx context.Context, name string, excludeInstanceID string) (bool, error)
3333

3434
// GetUploadBurstMultiplier returns the configured multiplier for upload burst ceiling.
3535
GetUploadBurstMultiplier() int

0 commit comments

Comments
 (0)