package oci import "testing" const sampleINI = `[DEFAULT] user=ocid1.user.oc1..aaaa fingerprint=e2:d4:5a:95 tenancy=ocid1.tenancy.oc1..bbbb region=eu-frankfurt-1 key_file= # TODO ` func TestParseConfigINI(t *testing.T) { cred, err := ParseConfigINI(sampleINI) if err != nil { t.Fatalf("ParseConfigINI: %v", err) } tests := []struct { field string got string want string }{ {field: "UserOCID", got: cred.UserOCID, want: "ocid1.user.oc1..aaaa"}, {field: "Fingerprint", got: cred.Fingerprint, want: "e2:d4:5a:95"}, {field: "TenancyOCID", got: cred.TenancyOCID, want: "ocid1.tenancy.oc1..bbbb"}, {field: "Region", got: cred.Region, want: "eu-frankfurt-1"}, } for _, tt := range tests { if tt.got != tt.want { t.Errorf("%s = %q, want %q", tt.field, tt.got, tt.want) } } } func TestParseConfigINIIgnoresOtherSections(t *testing.T) { text := "[OTHER]\nuser=ocid1.user.oc1..other\n[DEFAULT]\nuser=ocid1.user.oc1..default\ntenancy=ocid1.tenancy.oc1..t\n" cred, err := ParseConfigINI(text) if err != nil { t.Fatalf("ParseConfigINI: %v", err) } if got, want := cred.UserOCID, "ocid1.user.oc1..default"; got != want { t.Errorf("UserOCID = %q, want %q", got, want) } } func TestParseConfigINIEmpty(t *testing.T) { if _, err := ParseConfigINI("# only comments\n"); err == nil { t.Error("ParseConfigINI(comments only): got nil error, want failure") } } func TestCredentialsValidate(t *testing.T) { valid := Credentials{ TenancyOCID: "t", UserOCID: "u", Region: "r", Fingerprint: "f", PrivateKey: "-----BEGIN PRIVATE KEY-----\nx\n-----END PRIVATE KEY-----", } tests := []struct { name string mutate func(*Credentials) wantErr bool }{ {name: "完整凭据", mutate: func(*Credentials) {}, wantErr: false}, {name: "缺租户", mutate: func(c *Credentials) { c.TenancyOCID = "" }, wantErr: true}, {name: "缺用户", mutate: func(c *Credentials) { c.UserOCID = "" }, wantErr: true}, {name: "缺区域", mutate: func(c *Credentials) { c.Region = "" }, wantErr: true}, {name: "缺指纹", mutate: func(c *Credentials) { c.Fingerprint = "" }, wantErr: true}, {name: "私钥非 PEM", mutate: func(c *Credentials) { c.PrivateKey = "not a pem" }, wantErr: true}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { cred := valid tt.mutate(&cred) err := cred.Validate() if (err != nil) != tt.wantErr { t.Errorf("Validate() error = %v, wantErr %v", err, tt.wantErr) } }) } }