github.com/sacloud/libsacloud/v2@v2.32.3/helper/service/note/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 note
    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 TestNoteService_CRUD(t *testing.T) {
    28  	prefix := testutil.RandomPrefix()
    29  	name := prefix + "note-service"
    30  
    31  	var svc *Service
    32  	var note *sacloud.Note
    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
    42  			func(ctx context.Context, caller sacloud.APICaller) error {
    43  				created, err := svc.Create(&CreateRequest{
    44  					Name:    name,
    45  					Tags:    types.Tags{"tag1", "tag2"},
    46  					Class:   "shell",
    47  					Content: "#!/bin/bash",
    48  				})
    49  				if err != nil {
    50  					return err
    51  				}
    52  				note = created
    53  				return nil
    54  			},
    55  			// update
    56  			func(ctx context.Context, caller sacloud.APICaller) error {
    57  				updated, err := svc.Update(&UpdateRequest{
    58  					ID:   note.ID,
    59  					Name: pointer.NewString(name + "-upd"),
    60  					Tags: pointer.NewTags(types.Tags{"tag1-upd", "tag2-upd"}),
    61  				})
    62  				if err != nil {
    63  					return err
    64  				}
    65  				note = updated
    66  				return nil
    67  			},
    68  			// delete
    69  			func(ctx context.Context, caller sacloud.APICaller) error {
    70  				return svc.Delete(&DeleteRequest{ID: note.ID})
    71  			},
    72  		},
    73  		Cleanup:  testutil.ComposeCleanupResourceFunc(prefix, testutil.CleanupTargets.Note),
    74  		Parallel: true,
    75  	})
    76  }