github.com/sacloud/libsacloud/v2@v2.32.3/helper/service/dns/service_test.go (about)

     1  // Copyright 2016-2022 The Libsacloud Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package dns
    16  
    17  import (
    18  	"context"
    19  	"testing"
    20  
    21  	"github.com/sacloud/libsacloud/v2/sacloud"
    22  	"github.com/sacloud/libsacloud/v2/sacloud/pointer"
    23  	"github.com/sacloud/libsacloud/v2/sacloud/testutil"
    24  	"github.com/sacloud/libsacloud/v2/sacloud/types"
    25  )
    26  
    27  func TestService_CRUD(t *testing.T) {
    28  	prefix := testutil.RandomPrefix()
    29  	name := prefix + "dns-service.com"
    30  
    31  	var dns *sacloud.DNS
    32  	var svc *Service
    33  
    34  	testutil.RunResource(t, &testutil.ResourceTestCase{
    35  		SetupAPICallerFunc: testutil.SingletonAPICaller,
    36  		Setup: func(ctx context.Context, caller sacloud.APICaller) error {
    37  			svc = New(caller)
    38  			return nil
    39  		},
    40  		Tests: []testutil.ResourceTestFunc{
    41  			// create zone
    42  			func(ctx context.Context, caller sacloud.APICaller) error {
    43  				created, err := svc.Create(&CreateRequest{
    44  					Name:        name,
    45  					Description: "description",
    46  					Tags:        types.Tags{"tag1", "tag2"},
    47  				})
    48  				if err != nil {
    49  					return err
    50  				}
    51  				dns = created
    52  				return nil
    53  			},
    54  			// update zone
    55  			func(ctx context.Context, caller sacloud.APICaller) error {
    56  				updated, err := svc.Update(&UpdateRequest{
    57  					ID:           dns.ID,
    58  					Description:  pointer.NewString("description-upd"),
    59  					Tags:         pointer.NewTags(types.Tags{"tag1-upd", "tag2-upd"}),
    60  					SettingsHash: dns.SettingsHash,
    61  				})
    62  				if err != nil {
    63  					return err
    64  				}
    65  				return testutil.DoAsserts(
    66  					testutil.AssertEqualFunc(t, "description-upd", updated.Description, "Description"),
    67  					testutil.AssertEqualFunc(t, types.Tags{"tag1-upd", "tag2-upd"}, updated.Tags, "Tags"),
    68  				)
    69  			},
    70  			// delete zone
    71  			func(ctx context.Context, caller sacloud.APICaller) error {
    72  				return svc.Delete(&DeleteRequest{ID: dns.ID})
    73  			},
    74  		},
    75  		Cleanup:  testutil.ComposeCleanupResourceFunc(prefix, testutil.CleanupTargets.DNS),
    76  		Parallel: true,
    77  	})
    78  }