github.com/sacloud/libsacloud/v2@v2.32.3/helper/builder/archive/from_shared_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 TestFromSharedArchiveBuilder_Build(t *testing.T) {
    28  	zoneFrom := "is1a"
    29  	zoneTo := "is1b"
    30  	var sourceArchive *sacloud.Archive
    31  	var shareInfo *sacloud.ArchiveShareInfo
    32  
    33  	testutil.RunCRUD(t, &testutil.CRUDTestCase{
    34  		SetupAPICallerFunc: func() sacloud.APICaller {
    35  			return testutil.SingletonAPICaller()
    36  		},
    37  		Parallel:          true,
    38  		IgnoreStartupWait: true,
    39  		Setup: func(ctx *testutil.CRUDTestContext, caller sacloud.APICaller) error {
    40  			archiveOp := sacloud.NewArchiveOp(caller)
    41  			source, err := query.FindArchiveByOSType(ctx, archiveOp, zoneFrom, ostype.CentOS)
    42  			if err != nil {
    43  				return err
    44  			}
    45  
    46  			created, err := archiveOp.Create(ctx, zoneFrom, &sacloud.ArchiveCreateRequest{
    47  				SourceArchiveID: source.ID,
    48  				Name:            testutil.ResourceName("source-archive"),
    49  			})
    50  			if err != nil {
    51  				return err
    52  			}
    53  			sourceArchive = created
    54  			_, err = sacloud.WaiterForReady(func() (interface{}, error) {
    55  				return archiveOp.Read(ctx, zoneFrom, sourceArchive.ID)
    56  			}).WaitForState(ctx)
    57  			if err != nil {
    58  				return err
    59  			}
    60  
    61  			si, err := archiveOp.Share(ctx, zoneFrom, sourceArchive.ID)
    62  			if err != nil {
    63  				return err
    64  			}
    65  			shareInfo = si
    66  			return nil
    67  		},
    68  		Create: &testutil.CRUDTestFunc{
    69  			Func: func(ctx *testutil.CRUDTestContext, caller sacloud.APICaller) (interface{}, error) {
    70  				builder := &FromSharedArchiveBuilder{
    71  					Name:            testutil.ResourceName("archive-from-shared-builder"),
    72  					Description:     "description",
    73  					Tags:            types.Tags{"tag1", "tag2"},
    74  					SourceSharedKey: shareInfo.SharedKey,
    75  					Client:          NewAPIClient(caller),
    76  				}
    77  				return builder.Build(ctx, zoneTo)
    78  			},
    79  		},
    80  		Read: &testutil.CRUDTestFunc{
    81  			Func: func(ctx *testutil.CRUDTestContext, caller sacloud.APICaller) (interface{}, error) {
    82  				return sacloud.NewArchiveOp(caller).Read(ctx, zoneTo, ctx.ID)
    83  			},
    84  			CheckFunc: func(t testutil.TestT, ctx *testutil.CRUDTestContext, value interface{}) error {
    85  				archive := value.(*sacloud.Archive)
    86  				return testutil.DoAsserts(
    87  					testutil.AssertNotNilFunc(t, archive, "Archive"),
    88  				)
    89  			},
    90  		},
    91  		Delete: &testutil.CRUDTestDeleteFunc{
    92  			Func: func(ctx *testutil.CRUDTestContext, caller sacloud.APICaller) error {
    93  				archiveOp := sacloud.NewArchiveOp(caller)
    94  
    95  				_, err := sacloud.WaiterForReady(func() (interface{}, error) {
    96  					return archiveOp.Read(ctx, zoneTo, ctx.ID)
    97  				}).WaitForState(ctx)
    98  				if err != nil {
    99  					return err
   100  				}
   101  				if err := archiveOp.Delete(ctx, zoneTo, ctx.ID); err != nil {
   102  					return err
   103  				}
   104  
   105  				if sourceArchive != nil {
   106  					if sourceArchive.Availability.IsUploading() {
   107  						if err := archiveOp.CloseFTP(ctx, zoneFrom, sourceArchive.ID); err != nil {
   108  							return err
   109  						}
   110  					}
   111  					if err := archiveOp.Delete(ctx, zoneFrom, sourceArchive.ID); err != nil {
   112  						return err
   113  					}
   114  				}
   115  				return nil
   116  			},
   117  		},
   118  	})
   119  }