76 lines
2.8 KiB
Go
76 lines
2.8 KiB
Go
package service
|
|
|
|
import (
|
|
"context"
|
|
"strings"
|
|
"testing"
|
|
|
|
"oci-portal/internal/oci"
|
|
)
|
|
|
|
// vnicStubClient 覆写 VNIC 三方法并记录透传参数,其余行为沿用 fakeClient。
|
|
type vnicStubClient struct {
|
|
*fakeClient
|
|
|
|
vnics []oci.Vnic
|
|
attached oci.AttachVnicInput
|
|
attachInst string
|
|
detachedID string
|
|
ipv6Vnic string
|
|
}
|
|
|
|
func (f *vnicStubClient) ListInstanceVnics(ctx context.Context, cred oci.Credentials, region, instanceID string) ([]oci.Vnic, error) {
|
|
return f.vnics, nil
|
|
}
|
|
|
|
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
|
|
}
|
|
|
|
func (f *vnicStubClient) DetachVnic(ctx context.Context, cred oci.Credentials, region, attachmentID string) error {
|
|
f.detachedID = attachmentID
|
|
return nil
|
|
}
|
|
|
|
func (f *vnicStubClient) AddVnicIpv6(ctx context.Context, cred oci.Credentials, region, vnicID, address string) (string, error) {
|
|
f.ipv6Vnic = vnicID
|
|
return "2603:c022::1", nil
|
|
}
|
|
|
|
func TestVnicManagement(t *testing.T) {
|
|
client := &vnicStubClient{
|
|
fakeClient: &fakeClient{tenancy: oci.TenancyInfo{Name: "t"}},
|
|
vnics: []oci.Vnic{{AttachmentID: "att-0", IsPrimary: true, PrivateIP: "10.0.0.2"}},
|
|
}
|
|
svc := newTestService(t, client)
|
|
cfg := importAliveConfig(t, svc)
|
|
ctx := context.Background()
|
|
|
|
got, err := svc.InstanceVnics(ctx, cfg.ID, "ap-tokyo-1", "ocid1.instance.oc1..x")
|
|
if err != nil || len(got) != 1 || !got[0].IsPrimary {
|
|
t.Fatalf("InstanceVnics = %+v, %v", got, err)
|
|
}
|
|
// subnetId 缺失被拒
|
|
_, err = svc.AttachVnic(ctx, cfg.ID, "ap-tokyo-1", "ocid1.instance.oc1..x", oci.AttachVnicInput{})
|
|
if err == nil || !strings.Contains(err.Error(), "subnetId") {
|
|
t.Errorf("AttachVnic 无 subnet err = %v, want subnetId required", err)
|
|
}
|
|
in := oci.AttachVnicInput{SubnetID: "ocid1.subnet.oc1..s", AssignPublicIP: true, DisplayName: "vnic-2"}
|
|
vnic, err := svc.AttachVnic(ctx, cfg.ID, "ap-tokyo-1", "ocid1.instance.oc1..x", in)
|
|
if err != nil || vnic.AttachmentID != "att-1" || client.attached != in || client.attachInst != "ocid1.instance.oc1..x" {
|
|
t.Fatalf("AttachVnic = %+v, %v (passed %+v)", vnic, err, client.attached)
|
|
}
|
|
if err := svc.DetachVnic(ctx, cfg.ID, "ap-tokyo-1", "att-1"); err != nil || client.detachedID != "att-1" {
|
|
t.Fatalf("DetachVnic err = %v, detached = %q", err, client.detachedID)
|
|
}
|
|
// per-VNIC IPv6:vnicId 缺失被拒,正常路径透传
|
|
if _, err := svc.AddVnicIpv6(ctx, cfg.ID, "ap-tokyo-1", "", ""); err == nil {
|
|
t.Error("AddVnicIpv6 空 vnicId 应报错")
|
|
}
|
|
addr, err := svc.AddVnicIpv6(ctx, cfg.ID, "ap-tokyo-1", "ocid1.vnic.oc1..v", "")
|
|
if err != nil || addr == "" || client.ipv6Vnic != "ocid1.vnic.oc1..v" {
|
|
t.Fatalf("AddVnicIpv6 = %q, %v (vnic %q)", addr, err, client.ipv6Vnic)
|
|
}
|
|
}
|