go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/cipkg/base/actions/command_test.go (about)

     1  // Copyright 2023 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 actions
    16  
    17  import (
    18  	"testing"
    19  
    20  	"go.chromium.org/luci/cipkg/core"
    21  	"go.chromium.org/luci/cipkg/internal/testutils"
    22  
    23  	. "github.com/smartystreets/goconvey/convey"
    24  )
    25  
    26  func TestProcessCommand(t *testing.T) {
    27  	Convey("Test action processor for cipd", t, func() {
    28  		ap := NewActionProcessor()
    29  		pm := testutils.NewMockPackageManage("")
    30  
    31  		Convey("ok", func() {
    32  			cmd := &core.ActionCommand{
    33  				Args: []string{"bin", "arg1", "arg2", "{{.something}}/{{.something}}"},
    34  				Env:  []string{"env1=var1", "env2=var2", "env3={{.something}}"},
    35  			}
    36  
    37  			pkg, err := ap.Process("", pm, &core.Action{
    38  				Name: "url",
    39  				Metadata: &core.Action_Metadata{
    40  					RuntimeDeps: []*core.Action{
    41  						{Name: "else", Spec: &core.Action_Command{Command: &core.ActionCommand{}}},
    42  					},
    43  				},
    44  				Deps: []*core.Action{
    45  					{Name: "something", Spec: &core.Action_Command{Command: &core.ActionCommand{}}},
    46  				},
    47  				Spec: &core.Action_Command{Command: cmd},
    48  			})
    49  			So(err, ShouldBeNil)
    50  
    51  			So(pkg.BuildDependencies, ShouldHaveLength, 1)
    52  			So(pkg.BuildDependencies[0].Action.Name, ShouldEqual, "something")
    53  			depOut := pkg.BuildDependencies[0].Handler.OutputDirectory()
    54  			So(pkg.Derivation.Inputs, ShouldHaveLength, 1)
    55  			So(pkg.Derivation.Args, ShouldEqual, []string{"bin", "arg1", "arg2", depOut + "/" + depOut})
    56  			So(pkg.Derivation.Env, ShouldEqual, []string{"env1=var1", "env2=var2", "env3=" + depOut})
    57  			So(pkg.RuntimeDependencies, ShouldHaveLength, 1)
    58  			So(pkg.RuntimeDependencies[0].Action.Name, ShouldEqual, "else")
    59  		})
    60  
    61  		Convey("invalid template", func() {
    62  			cmd := &core.ActionCommand{
    63  				Args: []string{"bin", "arg1", "arg2", "{{something}}"},
    64  			}
    65  
    66  			_, err := ap.Process("", pm, &core.Action{
    67  				Name: "url",
    68  				Deps: []*core.Action{
    69  					{Name: "something", Spec: &core.Action_Command{Command: &core.ActionCommand{}}},
    70  				},
    71  				Spec: &core.Action_Command{Command: cmd},
    72  			})
    73  			So(err, ShouldNotBeNil)
    74  		})
    75  
    76  		Convey("unknown key", func() {
    77  			cmd := &core.ActionCommand{
    78  				Env: []string{"{{.else}}"},
    79  			}
    80  
    81  			_, err := ap.Process("", pm, &core.Action{
    82  				Name: "url",
    83  				Deps: []*core.Action{
    84  					{Name: "something", Spec: &core.Action_Command{Command: &core.ActionCommand{}}},
    85  				},
    86  				Spec: &core.Action_Command{Command: cmd},
    87  			})
    88  			So(err, ShouldNotBeNil)
    89  		})
    90  	})
    91  }