go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/cipkg/base/generators/import_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 generators
    16  
    17  import (
    18  	"context"
    19  	"io/fs"
    20  	"os"
    21  	"path/filepath"
    22  	"testing"
    23  
    24  	"go.chromium.org/luci/cipkg/core"
    25  	"go.chromium.org/luci/cipkg/internal/testutils"
    26  
    27  	. "github.com/smartystreets/goconvey/convey"
    28  )
    29  
    30  func TestImport(t *testing.T) {
    31  	Convey("Test import", t, func() {
    32  		ctx := context.Background()
    33  		plats := Platforms{}
    34  
    35  		Convey("symlink", func() {
    36  			g := ImportTargets{
    37  				Targets: map[string]ImportTarget{
    38  					"test1":     {Source: "//path/to/host1", Mode: fs.ModeSymlink},
    39  					"dir/test2": {Source: "//path/to/host2", Mode: fs.ModeSymlink, Version: "v2"},
    40  				},
    41  			}
    42  			a, err := g.Generate(ctx, plats)
    43  			So(err, ShouldBeNil)
    44  
    45  			imports := testutils.Assert[*core.Action_Copy](t, a.Spec)
    46  			So(imports.Copy.Files, ShouldResemble, map[string]*core.ActionFilesCopy_Source{
    47  				"test1": {
    48  					Content: &core.ActionFilesCopy_Source_Local_{
    49  						Local: &core.ActionFilesCopy_Source_Local{Path: filepath.FromSlash("//path/to/host1")},
    50  					},
    51  					Mode: uint32(fs.ModeSymlink),
    52  				},
    53  				filepath.FromSlash("dir/test2"): {
    54  					Content: &core.ActionFilesCopy_Source_Local_{
    55  						Local: &core.ActionFilesCopy_Source_Local{Path: filepath.FromSlash("//path/to/host2"), Version: "v2"},
    56  					},
    57  					Mode: uint32(fs.ModeSymlink),
    58  				},
    59  				filepath.FromSlash("build-support/base_import.stamp"): {
    60  					Content: &core.ActionFilesCopy_Source_Raw{},
    61  					Mode:    0o666,
    62  				},
    63  			})
    64  		})
    65  		Convey("copy", func() {
    66  			dir := t.TempDir()
    67  			file := filepath.Join(dir, "file")
    68  			f, err := os.Create(file)
    69  			So(err, ShouldBeNil)
    70  			_, err = f.WriteString("something")
    71  			So(err, ShouldBeNil)
    72  			err = f.Close()
    73  			So(err, ShouldBeNil)
    74  
    75  			Convey("ok", func() {
    76  				g := ImportTargets{
    77  					Targets: map[string]ImportTarget{
    78  						"dir":  {Source: dir, Mode: fs.ModeDir},
    79  						"file": {Source: file},
    80  					},
    81  				}
    82  
    83  				a, err := g.Generate(ctx, plats)
    84  				So(err, ShouldBeNil)
    85  				imports := testutils.Assert[*core.Action_Copy](t, a.Spec)
    86  				So(imports.Copy.Files["dir"].GetLocal().Version, ShouldNotBeEmpty)
    87  				So(fs.FileMode(imports.Copy.Files["dir"].Mode).IsDir(), ShouldBeTrue)
    88  				So(imports.Copy.Files["file"].GetLocal().Version, ShouldNotBeEmpty)
    89  				So(fs.FileMode(imports.Copy.Files["file"].Mode).IsRegular(), ShouldBeTrue)
    90  			})
    91  			Convey("source path independent", func() {
    92  				g := ImportTargets{
    93  					Targets: map[string]ImportTarget{
    94  						"dir": {Source: dir, Mode: fs.ModeDir, Version: "123"},
    95  					},
    96  				}
    97  				a, err := g.Generate(ctx, plats)
    98  				So(err, ShouldBeNil)
    99  				imports1 := testutils.Assert[*core.Action_Copy](t, a.Spec)
   100  
   101  				err = os.Rename(dir, dir+".else")
   102  				So(err, ShouldBeNil)
   103  				g = ImportTargets{
   104  					Targets: map[string]ImportTarget{
   105  						"dir": {Source: dir + ".else", Mode: fs.ModeDir, Version: "123"},
   106  					},
   107  				}
   108  				a, err = g.Generate(ctx, plats)
   109  				So(err, ShouldBeNil)
   110  				imports2 := testutils.Assert[*core.Action_Copy](t, a.Spec)
   111  
   112  				So(imports1.Copy.Files["dir"].GetLocal().Version, ShouldEqual, imports2.Copy.Files["dir"].GetLocal().Version)
   113  			})
   114  			Convey("source path dependent", func() {
   115  				g := ImportTargets{
   116  					Targets: map[string]ImportTarget{
   117  						"dir": {Source: dir, Mode: fs.ModeDir, Version: "123", SourcePathDependent: true},
   118  					},
   119  				}
   120  				a, err := g.Generate(ctx, plats)
   121  				So(err, ShouldBeNil)
   122  				imports1 := testutils.Assert[*core.Action_Copy](t, a.Spec)
   123  
   124  				err = os.Rename(dir, dir+".else")
   125  				So(err, ShouldBeNil)
   126  				g = ImportTargets{
   127  					Targets: map[string]ImportTarget{
   128  						"dir": {Source: dir + ".else", Mode: fs.ModeDir, Version: "123", SourcePathDependent: true},
   129  					},
   130  				}
   131  				a, err = g.Generate(ctx, plats)
   132  				So(err, ShouldBeNil)
   133  				imports2 := testutils.Assert[*core.Action_Copy](t, a.Spec)
   134  
   135  				So(imports1.Copy.Files["dir"].GetLocal().Version, ShouldNotEqual, imports2.Copy.Files["dir"].GetLocal().Version)
   136  			})
   137  		})
   138  	})
   139  }