go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/lucictx/exported_test.go (about)

     1  // Copyright 2016 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 lucictx
    16  
    17  import (
    18  	"io/ioutil"
    19  	"os"
    20  	"os/exec"
    21  	"testing"
    22  
    23  	. "github.com/smartystreets/goconvey/convey"
    24  
    25  	"go.chromium.org/luci/common/system/environ"
    26  )
    27  
    28  func TestLiveExported(t *testing.T) {
    29  	// t.Parallel() because of os.Environ manipulation
    30  
    31  	dir, err := ioutil.TempDir(os.TempDir(), "exported_test")
    32  	if err != nil {
    33  		t.Fatalf("could not create tempdir! %s", err)
    34  	}
    35  	defer os.RemoveAll(dir)
    36  
    37  	Convey("LiveExports", t, func() {
    38  		os.Unsetenv(EnvKey)
    39  
    40  		tf, err := ioutil.TempFile(dir, "exported_test.liveExport")
    41  		tfn := tf.Name()
    42  		tf.Close()
    43  		So(err, ShouldBeNil)
    44  		defer os.Remove(tfn)
    45  
    46  		closed := false
    47  		le := &liveExport{
    48  			path:   tfn,
    49  			closer: func() { closed = true },
    50  		}
    51  
    52  		Convey("Can only be closed once", func() {
    53  			le.Close()
    54  			So(func() { le.Close() }, ShouldPanic)
    55  		})
    56  
    57  		Convey("Calls closer when it is closed", func() {
    58  			So(closed, ShouldBeFalse)
    59  			le.Close()
    60  			So(closed, ShouldBeTrue)
    61  		})
    62  
    63  		Convey("Can add to command", func() {
    64  			cmd := exec.Command("test", "arg")
    65  			cmd.Env = os.Environ()
    66  			le.SetInCmd(cmd)
    67  			So(len(cmd.Env), ShouldEqual, len(os.Environ())+1)
    68  			So(cmd.Env[len(cmd.Env)-1], ShouldStartWith, EnvKey)
    69  			So(cmd.Env[len(cmd.Env)-1], ShouldEndWith, le.path)
    70  		})
    71  
    72  		Convey("Can modify in command", func() {
    73  			cmd := exec.Command("test", "arg")
    74  			cmd.Env = os.Environ()
    75  			cmd.Env[0] = EnvKey + "=helloworld"
    76  			le.SetInCmd(cmd)
    77  			So(len(cmd.Env), ShouldEqual, len(os.Environ()))
    78  			So(cmd.Env[0], ShouldStartWith, EnvKey)
    79  			So(cmd.Env[0], ShouldEndWith, le.path)
    80  		})
    81  
    82  		Convey("Can add to environ", func() {
    83  			env := environ.System()
    84  			_, ok := env.Lookup(EnvKey)
    85  			So(ok, ShouldBeFalse)
    86  			le.SetInEnviron(env)
    87  			val, ok := env.Lookup(EnvKey)
    88  			So(ok, ShouldBeTrue)
    89  			So(val, ShouldEqual, le.path)
    90  		})
    91  	})
    92  }
    93  
    94  func TestNullExported(t *testing.T) {
    95  	t.Parallel()
    96  
    97  	n := nullExport{}
    98  	someEnv := []string{"SOME_STUFF=0"}
    99  
   100  	Convey("SetInCmd, no LUCI_CONTEXT", t, func() {
   101  		cmd := exec.Cmd{
   102  			Env: append([]string(nil), someEnv...),
   103  		}
   104  		n.SetInCmd(&cmd)
   105  		So(cmd.Env, ShouldResemble, someEnv)
   106  	})
   107  
   108  	Convey("SetInCmd, with LUCI_CONTEXT", t, func() {
   109  		cmd := exec.Cmd{
   110  			Env: append([]string{"LUCI_CONTEXT=abc"}, someEnv...),
   111  		}
   112  		n.SetInCmd(&cmd)
   113  		So(cmd.Env, ShouldResemble, someEnv) // no LUCI_CONTEXT anymore
   114  	})
   115  
   116  	Convey("SetInEnviron, no LUCI_CONTEXT", t, func() {
   117  		env := environ.New(someEnv)
   118  		n.SetInEnviron(env)
   119  		So(env.Sorted(), ShouldResemble, someEnv)
   120  	})
   121  
   122  	Convey("SetInEnviron, with LUCI_CONTEXT", t, func() {
   123  		env := environ.New(append([]string{"LUCI_CONTEXT=abc"}, someEnv...))
   124  		n.SetInEnviron(env)
   125  		So(env.Sorted(), ShouldResemble, someEnv) // no LUCI_CONTEXT anymore
   126  	})
   127  }