@@ -125,6 +125,84 @@ func TestProviderResourcesDatabaseUsesRDSDrivers(t *testing.T) {
125125 }
126126}
127127
128+ func TestProviderResourcesDomainUsesDNSDriver (t * testing.T ) {
129+ restoreCloudlist := setCloudlist ([]string {"domain" })
130+ defer restoreCloudlist ()
131+
132+ logger .SetOutput (io .Discard )
133+ t .Cleanup (func () {
134+ logger .SetOutput (nil )
135+ })
136+
137+ server := httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
138+ values , err := url .ParseQuery (r .URL .RawQuery )
139+ if err != nil {
140+ t .Fatalf ("ParseQuery() error = %v" , err )
141+ }
142+ if service := signedService (t , r ); service != "dns" {
143+ t .Fatalf ("unexpected service: %s" , service )
144+ }
145+ switch values .Get ("Action" ) {
146+ case "ListZones" :
147+ assertJSONBody (t , r , map [string ]any {
148+ "PageNumber" : float64 (1 ),
149+ "PageSize" : float64 (100 ),
150+ })
151+ _ , _ = w .Write ([]byte (`{"Total":2,"Zones":[{"ZoneName":"example.com","ZID":101},{"ZoneName":"example.org","ZID":102}]}` ))
152+ case "ListRecords" :
153+ body := decodeJSONBody (t , r )
154+ if body ["PageNumber" ] != float64 (1 ) || body ["PageSize" ] != float64 (100 ) {
155+ t .Fatalf ("unexpected ListRecords body: %v" , body )
156+ }
157+ switch body ["ZID" ] {
158+ case float64 (101 ):
159+ _ , _ = w .Write ([]byte (`{"PageNumber":1,"PageSize":100,"TotalCount":2,"Records":[{"Host":"@","Type":"A","Value":"1.1.1.1","Enable":true},{"Host":"www","Type":"CNAME","Value":"target.example.com","Enable":false}]}` ))
160+ case float64 (102 ):
161+ _ , _ = w .Write ([]byte (`{"PageNumber":1,"PageSize":100,"TotalCount":1,"Records":[{"Host":"api","Type":"A","Value":"2.2.2.2","Enable":true}]}` ))
162+ default :
163+ t .Fatalf ("unexpected ZID: %v" , body ["ZID" ])
164+ }
165+ default :
166+ t .Fatalf ("unexpected action: %s" , values .Get ("Action" ))
167+ }
168+ }))
169+ defer server .Close ()
170+
171+ provider , err := newProvider (testOptions (map [string ]string {
172+ utils .Provider : "volcengine" ,
173+ utils .Region : "cn-guangzhou" ,
174+ }), testClientOptions (server .URL )... )
175+ if err != nil {
176+ t .Fatalf ("newProvider() error = %v" , err )
177+ }
178+
179+ resources , err := provider .Resources (context .Background ())
180+ if err != nil {
181+ t .Fatalf ("Resources() error = %v" , err )
182+ }
183+ if len (resources .Errors ) != 0 {
184+ t .Fatalf ("unexpected resource errors: %+v" , resources .Errors )
185+ }
186+
187+ got := map [string ]schema.Domain {}
188+ for _ , asset := range resources .Assets {
189+ domain , ok := asset .(schema.Domain )
190+ if ! ok {
191+ t .Fatalf ("unexpected asset type: %T" , asset )
192+ }
193+ got [domain .DomainName ] = domain
194+ }
195+ if len (got ) != 2 {
196+ t .Fatalf ("unexpected domain count: %d" , len (got ))
197+ }
198+ if len (got ["example.com" ].Records ) != 2 || got ["example.com" ].Records [1 ].Status != "DISABLE" {
199+ t .Fatalf ("unexpected example.com records: %+v" , got ["example.com" ])
200+ }
201+ if len (got ["example.org" ].Records ) != 1 || got ["example.org" ].Records [0 ].Value != "2.2.2.2" {
202+ t .Fatalf ("unexpected example.org records: %+v" , got ["example.org" ])
203+ }
204+ }
205+
128206func testOptions (overrides map [string ]string ) schema.Options {
129207 options := schema.Options {
130208 utils .AccessKey : "AKID" ,
@@ -156,6 +234,19 @@ func setCloudlist(values []string) func() {
156234}
157235
158236func assertJSONBody (t * testing.T , r * http.Request , want map [string ]any ) {
237+ t .Helper ()
238+ got := decodeJSONBody (t , r )
239+ if len (got ) != len (want ) {
240+ t .Fatalf ("unexpected body map length: got=%v want=%v" , got , want )
241+ }
242+ for key , wantValue := range want {
243+ if got [key ] != wantValue {
244+ t .Fatalf ("unexpected body field %s: got=%v want=%v" , key , got [key ], wantValue )
245+ }
246+ }
247+ }
248+
249+ func decodeJSONBody (t * testing.T , r * http.Request ) map [string ]any {
159250 t .Helper ()
160251 defer r .Body .Close ()
161252 body , err := io .ReadAll (r .Body )
@@ -166,14 +257,7 @@ func assertJSONBody(t *testing.T, r *http.Request, want map[string]any) {
166257 if err := json .Unmarshal (body , & got ); err != nil {
167258 t .Fatalf ("Unmarshal() error = %v body=%s" , err , string (body ))
168259 }
169- if len (got ) != len (want ) {
170- t .Fatalf ("unexpected body map length: got=%v want=%v" , got , want )
171- }
172- for key , wantValue := range want {
173- if got [key ] != wantValue {
174- t .Fatalf ("unexpected body field %s: got=%v want=%v" , key , got [key ], wantValue )
175- }
176- }
260+ return got
177261}
178262
179263func signedService (t * testing.T , r * http.Request ) string {
0 commit comments