github.com/sacloud/libsacloud/v2@v2.32.3/helper/service/archive/download_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  	"os"
    20  	"testing"
    21  
    22  	"github.com/stretchr/testify/require"
    23  
    24  	"github.com/sacloud/libsacloud/v2/sacloud/testutil"
    25  	"github.com/sacloud/libsacloud/v2/sacloud/types"
    26  )
    27  
    28  func TestArchiveService_downloadAfterBuild(t *testing.T) {
    29  	if !testutil.IsAccTest() {
    30  		t.SkipNow()
    31  	}
    32  
    33  	caller := testutil.SingletonAPICaller()
    34  	zone := testutil.TestZone()
    35  	svc := New(caller)
    36  
    37  	// file
    38  	filename := "test-archive-source.tmp"
    39  	if err := os.WriteFile(filename, []byte("test"), 0755); err != nil {
    40  		t.Fatal(err)
    41  	}
    42  	defer os.Remove(filename) // nolint
    43  
    44  	// create
    45  	archive, err := svc.Create(&CreateRequest{
    46  		Zone:        zone,
    47  		Name:        testutil.ResourceName("test-archive-service"),
    48  		Description: "desc",
    49  		Tags:        types.Tags{"tag1", "tag2"},
    50  		SizeGB:      20,
    51  		SourcePath:  filename,
    52  	})
    53  	if err != nil {
    54  		t.Fatal(err)
    55  	}
    56  
    57  	// update
    58  	updName := archive.Name + "-upd"
    59  	updArchive, err := svc.Update(&UpdateRequest{
    60  		Zone: zone,
    61  		ID:   archive.ID,
    62  		Name: &updName,
    63  	})
    64  	if err != nil {
    65  		t.Fatal(err)
    66  	}
    67  	require.Equal(t, updName, updArchive.Name)
    68  	require.Equal(t, archive.Description, updArchive.Description)
    69  	require.Equal(t, archive.Tags, updArchive.Tags)
    70  	require.Equal(t, archive.IconID, updArchive.IconID)
    71  
    72  	// download
    73  	buf := bytes.NewBuffer([]byte{})
    74  	err = svc.Download(&DownloadRequest{
    75  		Zone:   zone,
    76  		ID:     archive.ID,
    77  		Writer: buf,
    78  	})
    79  	if err != nil {
    80  		t.Fatal(err)
    81  	}
    82  
    83  	if buf.String() != "test" {
    84  		t.Fatalf("unexpected value: got:%s want:%s", buf.String(), "test")
    85  	}
    86  
    87  	// delete
    88  	if err := svc.Delete(&DeleteRequest{
    89  		Zone: zone,
    90  		ID:   archive.ID,
    91  	}); err != nil {
    92  		t.Fatal(err)
    93  	}
    94  }