go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/swarming/runner/command_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 runner
    16  
    17  import (
    18  	"context"
    19  	"path/filepath"
    20  	"runtime"
    21  	"testing"
    22  
    23  	. "github.com/smartystreets/goconvey/convey"
    24  )
    25  
    26  func TestReplaceCommandParameters(t *testing.T) {
    27  	t.Parallel()
    28  
    29  	Convey("ReplaceCommandParameters", t, func() {
    30  		ctx := context.Background()
    31  
    32  		Convey("test EXECUTABLE_SUFFIX", func() {
    33  			arg, err := ReplaceCommandParameters(ctx, "program${EXECUTABLE_SUFFIX}", "", "")
    34  			So(err, ShouldBeNil)
    35  			if runtime.GOOS == "windows" {
    36  				So(arg, ShouldEqual, "program.exe")
    37  			} else {
    38  				So(arg, ShouldEqual, "program")
    39  			}
    40  		})
    41  
    42  		Convey("test ISOLATED_OUTDIR", func() {
    43  			arg, err := ReplaceCommandParameters(ctx, "${ISOLATED_OUTDIR}/result.txt", "out", "")
    44  			So(err, ShouldBeNil)
    45  			So(arg, ShouldEqual, filepath.Join("out", "result.txt"))
    46  		})
    47  
    48  		Convey("test SWARMING_BOT_FILE", func() {
    49  			arg, err := ReplaceCommandParameters(ctx, "${SWARMING_BOT_FILE}/config", "", "cfgdir")
    50  			So(err, ShouldBeNil)
    51  			So(arg, ShouldEqual, filepath.Join("cfgdir", "config"))
    52  		})
    53  	})
    54  }
    55  
    56  func TestProcessCommand(t *testing.T) {
    57  	t.Parallel()
    58  
    59  	Convey("ProcessCommand", t, func() {
    60  		ctx := context.Background()
    61  		args, err := ProcessCommand(ctx, []string{
    62  			"program${EXECUTABLE_SUFFIX}",
    63  			"${ISOLATED_OUTDIR}/result.txt",
    64  			"${SWARMING_BOT_FILE}/config",
    65  		}, "out", "cfgdir")
    66  
    67  		So(err, ShouldBeNil)
    68  
    69  		executableSuffix := ""
    70  		if runtime.GOOS == "windows" {
    71  			executableSuffix = ".exe"
    72  		}
    73  
    74  		So(args, ShouldResemble, []string{
    75  			"program" + executableSuffix,
    76  			filepath.Join("out", "result.txt"),
    77  			filepath.Join("cfgdir", "config"),
    78  		})
    79  	})
    80  }