go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/client/cmd/cas/main_test.go (about)

     1  // Copyright 2021 The LUCI 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 main is a client to a Swarming server.
    16  //
    17  // The reference server python implementation documentation can be found at
    18  // https://github.com/luci/luci-py/tree/master/appengine/swarming/doc
    19  package main
    20  
    21  import (
    22  	"fmt"
    23  	"os"
    24  	"path/filepath"
    25  	"testing"
    26  
    27  	"github.com/maruel/subcommands"
    28  	. "github.com/smartystreets/goconvey/convey"
    29  	"go.chromium.org/luci/common/testing/testfs"
    30  )
    31  
    32  // integrationTestEnvVar is the name of the environment variable which controls
    33  // whether integaration tests are executed.
    34  // The value must be "1" for integration tests to run.
    35  const integrationTestEnvVar = "INTEGRATION_TESTS"
    36  
    37  // runIntegrationTests true if integration tests should run.
    38  func runIntegrationTests() bool {
    39  	return os.Getenv(integrationTestEnvVar) == "1"
    40  }
    41  
    42  // runCmd runs swarming commands appending common flags.
    43  // It skips if integration should not run.
    44  func runCmd(t *testing.T, cmd string, args ...string) int {
    45  	if !runIntegrationTests() {
    46  		t.Skipf("Skip integration tests")
    47  	}
    48  	args = append([]string{cmd, "-cas-instance", "chromium-swarm-dev"}, args...)
    49  	return subcommands.Run(getApplication(), args)
    50  }
    51  
    52  func TestArchiveAndDownloadCommand(t *testing.T) {
    53  	t.Parallel()
    54  
    55  	Convey("archive", t, func() {
    56  		uploadDir := t.TempDir()
    57  		os.WriteFile(filepath.Join(uploadDir, "foo"), []byte("hi"), 0600)
    58  
    59  		outDir := t.TempDir()
    60  		digestPath := filepath.Join(outDir, "out.digest")
    61  		uploadJSONPath := filepath.Join(outDir, "upload-stats.json")
    62  
    63  		archiveFlags := []string{
    64  			"-dump-digest",
    65  			digestPath,
    66  			"-dump-json",
    67  			uploadJSONPath,
    68  			"-paths",
    69  			fmt.Sprintf("%s:%s", uploadDir, "foo"),
    70  		}
    71  		So(runCmd(t, "archive", archiveFlags...), ShouldEqual, 0)
    72  
    73  		digestBytes, err := os.ReadFile(digestPath)
    74  		So(err, ShouldBeNil)
    75  		digest := string(digestBytes)
    76  		So(digest, ShouldEqual, "1ec750cf7c4ebc220a753a705229574873b3c650b939cf4a69df1af84b40b981/77")
    77  
    78  		Convey("and download", func() {
    79  			downloadJSONPath := filepath.Join(outDir, "download-stats.json")
    80  			downloadDir := filepath.Join(outDir, "out")
    81  
    82  			downloadFlags := []string{
    83  				"-digest",
    84  				digest,
    85  				"-dump-json",
    86  				downloadJSONPath,
    87  				"-dir",
    88  				downloadDir,
    89  			}
    90  			So(runCmd(t, "download", downloadFlags...), ShouldEqual, 0)
    91  			layout, err := testfs.Collect(downloadDir)
    92  			So(err, ShouldBeNil)
    93  			So(layout, ShouldResemble, map[string]string{"foo": "hi"})
    94  		})
    95  	})
    96  }