@@ -3,7 +3,9 @@ package service
|
||||
import (
|
||||
"context"
|
||||
"strings"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"oci-portal/internal/oci"
|
||||
)
|
||||
@@ -12,17 +14,35 @@ import (
|
||||
type vnicStubClient struct {
|
||||
*fakeClient
|
||||
|
||||
mu sync.Mutex
|
||||
vnics []oci.Vnic
|
||||
attached oci.AttachVnicInput
|
||||
attachInst string
|
||||
detachedID string
|
||||
ipv6Vnic string
|
||||
boundVnic string // AssignReservedIPToVnic 记录
|
||||
boundIP string
|
||||
}
|
||||
|
||||
func (f *vnicStubClient) ListInstanceVnics(ctx context.Context, cred oci.Credentials, region, instanceID string) ([]oci.Vnic, error) {
|
||||
f.mu.Lock()
|
||||
defer f.mu.Unlock()
|
||||
return f.vnics, nil
|
||||
}
|
||||
|
||||
func (f *vnicStubClient) AssignReservedIPToVnic(ctx context.Context, cred oci.Credentials, region, publicIPID, vnicID string) error {
|
||||
f.mu.Lock()
|
||||
defer f.mu.Unlock()
|
||||
f.boundIP, f.boundVnic = publicIPID, vnicID
|
||||
return nil
|
||||
}
|
||||
|
||||
func (f *vnicStubClient) bound() (string, string) {
|
||||
f.mu.Lock()
|
||||
defer f.mu.Unlock()
|
||||
return f.boundIP, f.boundVnic
|
||||
}
|
||||
|
||||
func (f *vnicStubClient) AttachVnic(ctx context.Context, cred oci.Credentials, region, instanceID string, in oci.AttachVnicInput) (oci.Vnic, error) {
|
||||
f.attachInst, f.attached = instanceID, in
|
||||
return oci.Vnic{AttachmentID: "att-1", SubnetID: in.SubnetID, LifecycleState: "ATTACHING"}, nil
|
||||
@@ -73,3 +93,27 @@ func TestVnicManagement(t *testing.T) {
|
||||
t.Fatalf("AddVnicIpv6 = %q, %v (vnic %q)", addr, err, client.ipv6Vnic)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAttachVnicWithReservedIP(t *testing.T) {
|
||||
client := &vnicStubClient{
|
||||
fakeClient: &fakeClient{tenancy: oci.TenancyInfo{Name: "t"}},
|
||||
// 附加返回 att-1;列表里它已就绪,后台编排应绑定到对应 VNIC
|
||||
vnics: []oci.Vnic{{AttachmentID: "att-1", VnicID: "ocid1.vnic.oc1..new", LifecycleState: "ATTACHED"}},
|
||||
}
|
||||
svc := newTestService(t, client)
|
||||
cfg := importAliveConfig(t, svc)
|
||||
|
||||
in := oci.AttachVnicInput{SubnetID: "ocid1.subnet.oc1..s", ReservedPublicIPID: "ocid1.publicip..r"}
|
||||
if _, err := svc.AttachVnic(context.Background(), cfg.ID, "ap-tokyo-1", "ocid1.instance.oc1..x", in); err != nil {
|
||||
t.Fatalf("AttachVnic: %v", err)
|
||||
}
|
||||
deadline := time.Now().Add(3 * time.Second)
|
||||
for time.Now().Before(deadline) {
|
||||
if ip, vnic := client.bound(); ip == "ocid1.publicip..r" && vnic == "ocid1.vnic.oc1..new" {
|
||||
return
|
||||
}
|
||||
time.Sleep(20 * time.Millisecond)
|
||||
}
|
||||
ip, vnic := client.bound()
|
||||
t.Fatalf("bound = (%q, %q), want reserved ip bound to new vnic", ip, vnic)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user