github.com/sacloud/libsacloud/v2@v2.32.3/helper/builder/archive/transfer_archive_builder_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 archive
    16  
    17  import (
    18  	"testing"
    19  
    20  	"github.com/sacloud/libsacloud/v2/helper/query"
    21  	"github.com/sacloud/libsacloud/v2/sacloud"
    22  	"github.com/sacloud/libsacloud/v2/sacloud/ostype"
    23  	"github.com/sacloud/libsacloud/v2/sacloud/testutil"
    24  	"github.com/sacloud/libsacloud/v2/sacloud/types"
    25  )
    26  
    27  func TestTransferArchiveBuilder_Build(t *testing.T) {
    28  	zoneFrom := "is1a"
    29  	zoneTo := "is1b"
    30  	var sourceArchive *sacloud.Archive
    31  
    32  	testutil.RunCRUD(t, &testutil.CRUDTestCase{
    33  		SetupAPICallerFunc: func() sacloud.APICaller {
    34  			return testutil.SingletonAPICaller()
    35  		},
    36  		Parallel:          true,
    37  		IgnoreStartupWait: true,
    38  		Setup: func(ctx *testutil.CRUDTestContext, caller sacloud.APICaller) error {
    39  			archiveOp := sacloud.NewArchiveOp(caller)
    40  			source, err := query.FindArchiveByOSType(ctx, archiveOp, zoneFrom, ostype.CentOS)
    41  			if err != nil {
    42  				return err
    43  			}
    44  
    45  			created, err := archiveOp.Create(ctx, zoneFrom, &sacloud.ArchiveCreateRequest{
    46  				SourceArchiveID: source.ID,
    47  				Name:            testutil.ResourceName("source-archive-for-transfer"),
    48  			})
    49  			if err != nil {
    50  				return err
    51  			}
    52  			sourceArchive = created
    53  			_, err = sacloud.WaiterForReady(func() (interface{}, error) {
    54  				return archiveOp.Read(ctx, zoneFrom, sourceArchive.ID)
    55  			}).WaitForState(ctx)
    56  
    57  			return err
    58  		},
    59  		Create: &testutil.CRUDTestFunc{
    60  			Func: func(ctx *testutil.CRUDTestContext, caller sacloud.APICaller) (interface{}, error) {
    61  				builder := &TransferArchiveBuilder{
    62  					Name:              testutil.ResourceName("archive-from-other-zone"),
    63  					Description:       "description",
    64  					Tags:              types.Tags{"tag1", "tag2"},
    65  					SourceArchiveID:   sourceArchive.ID,
    66  					SourceArchiveZone: zoneFrom,
    67  					Client:            NewAPIClient(caller),
    68  				}
    69  				return builder.Build(ctx, zoneTo)
    70  			},
    71  		},
    72  		Read: &testutil.CRUDTestFunc{
    73  			Func: func(ctx *testutil.CRUDTestContext, caller sacloud.APICaller) (interface{}, error) {
    74  				return sacloud.NewArchiveOp(caller).Read(ctx, zoneTo, ctx.ID)
    75  			},
    76  			CheckFunc: func(t testutil.TestT, ctx *testutil.CRUDTestContext, value interface{}) error {
    77  				archive := value.(*sacloud.Archive)
    78  				return testutil.DoAsserts(
    79  					testutil.AssertNotNilFunc(t, archive, "Archive"),
    80  				)
    81  			},
    82  		},
    83  		Delete: &testutil.CRUDTestDeleteFunc{
    84  			Func: func(ctx *testutil.CRUDTestContext, caller sacloud.APICaller) error {
    85  				archiveOp := sacloud.NewArchiveOp(caller)
    86  
    87  				_, err := sacloud.WaiterForReady(func() (interface{}, error) {
    88  					return archiveOp.Read(ctx, zoneTo, ctx.ID)
    89  				}).WaitForState(ctx)
    90  				if err != nil {
    91  					return err
    92  				}
    93  				if err := archiveOp.Delete(ctx, zoneTo, ctx.ID); err != nil {
    94  					return err
    95  				}
    96  
    97  				if sourceArchive != nil {
    98  					if err := archiveOp.Delete(ctx, zoneFrom, sourceArchive.ID); err != nil {
    99  						return err
   100  					}
   101  				}
   102  				return nil
   103  			},
   104  		},
   105  	})
   106  }