github.com/sacloud/libsacloud/v2@v2.32.3/helper/builder/archive/blank_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  	"bytes"
    19  	"context"
    20  	"testing"
    21  
    22  	"github.com/stretchr/testify/require"
    23  
    24  	"github.com/sacloud/libsacloud/v2/sacloud"
    25  	"github.com/sacloud/libsacloud/v2/sacloud/testutil"
    26  	"github.com/sacloud/libsacloud/v2/sacloud/types"
    27  )
    28  
    29  func TestBlankArchiveBuilder_Validate(t *testing.T) {
    30  	builder := &BlankArchiveBuilder{
    31  		Name:         "test",
    32  		SizeGB:       20,
    33  		SourceReader: bytes.NewBufferString(""),
    34  		NoWait:       true,
    35  		Client:       NewAPIClient(testutil.SingletonAPICaller()),
    36  	}
    37  
    38  	err := builder.Validate(context.Background(), testutil.TestZone())
    39  	require.EqualError(t, err, "NoWait=true is not supported when uploading files and creating archives")
    40  }
    41  
    42  func TestBlankArchiveBuilder_Build(t *testing.T) {
    43  	if !testutil.IsAccTest() {
    44  		t.Skip("TestBlankArchiveBuilder_Build only exec when running an Acceptance Test")
    45  	}
    46  
    47  	testZone := testutil.TestZone()
    48  	testutil.RunCRUD(t, &testutil.CRUDTestCase{
    49  		SetupAPICallerFunc: func() sacloud.APICaller {
    50  			return testutil.SingletonAPICaller()
    51  		},
    52  		Parallel:          true,
    53  		IgnoreStartupWait: true,
    54  		Create: &testutil.CRUDTestFunc{
    55  			Func: func(ctx *testutil.CRUDTestContext, caller sacloud.APICaller) (interface{}, error) {
    56  				source := bytes.NewBufferString("dummy")
    57  
    58  				builder := &BlankArchiveBuilder{
    59  					Name:         testutil.ResourceName("archive-from-shared-builder"),
    60  					Description:  "description",
    61  					Tags:         types.Tags{"tag1", "tag2"},
    62  					SizeGB:       20,
    63  					SourceReader: source,
    64  					Client:       NewAPIClient(caller),
    65  				}
    66  				return builder.Build(ctx, testZone)
    67  			},
    68  		},
    69  		Read: &testutil.CRUDTestFunc{
    70  			Func: func(ctx *testutil.CRUDTestContext, caller sacloud.APICaller) (interface{}, error) {
    71  				return sacloud.NewArchiveOp(caller).Read(ctx, testZone, ctx.ID)
    72  			},
    73  			CheckFunc: func(t testutil.TestT, ctx *testutil.CRUDTestContext, value interface{}) error {
    74  				archive := value.(*sacloud.Archive)
    75  				return testutil.DoAsserts(
    76  					testutil.AssertNotNilFunc(t, archive, "Archive"),
    77  					testutil.AssertTrueFunc(t, archive.Availability.IsAvailable(), "Archive.Availability.IsAvailable"),
    78  				)
    79  			},
    80  		},
    81  		Delete: &testutil.CRUDTestDeleteFunc{
    82  			Func: func(ctx *testutil.CRUDTestContext, caller sacloud.APICaller) error {
    83  				return sacloud.NewArchiveOp(caller).Delete(ctx, testZone, ctx.ID)
    84  			},
    85  		},
    86  	})
    87  }