github.com/thediveo/gons@v0.9.9/gons_test.go (about)

     1  // Copyright 2019 Harald Albrecht.
     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 gons_test
    16  
    17  import (
    18  	"fmt"
    19  	"os"
    20  	"strings"
    21  
    22  	"github.com/thediveo/gons"
    23  	"github.com/thediveo/gons/reexec"
    24  	"github.com/thediveo/lxkns/ops"
    25  	"github.com/thediveo/testbasher"
    26  
    27  	. "github.com/onsi/ginkgo/v2"
    28  	. "github.com/onsi/gomega"
    29  )
    30  
    31  func init() {
    32  	reexec.Register("foo", func() {})
    33  	reexec.Register("enter", func() {
    34  		ns := []string{}
    35  		for _, t := range []string{"user", "mnt", "net"} {
    36  			nsid, _ := ops.NamespacePath("/proc/self/ns/" + t).ID()
    37  			ns = append(ns, fmt.Sprintf("%d", nsid.Ino))
    38  		}
    39  		fmt.Fprintln(os.Stdout, "[", strings.Join(ns, ","), "]")
    40  	})
    41  }
    42  
    43  var _ = Describe("gons", func() {
    44  
    45  	// Re-execute with an invalid namespace reference.
    46  	It("aborts re-execution for invalid namespace reference", func() {
    47  		Expect(reexec.RunReexecAction(
    48  			"foo",
    49  			reexec.Namespaces([]reexec.Namespace{
    50  				{Type: "net", Path: "/foo"},
    51  			}),
    52  		)).To(MatchError(MatchRegexp(
    53  			`.* ReexecAction.Run: child failed with stderr message ` +
    54  				`".* invalid gons_net reference .*`)))
    55  	})
    56  
    57  	// Re-execute and switch into other namespaces especially created for this
    58  	// test.
    59  	It("switches namespaces when re-executing", func() {
    60  		b := testbasher.Basher{}
    61  		defer b.Done()
    62  		b.Script("unshare", `
    63  unshare -Umn $printinfo
    64  `)
    65  		b.Script("printinfo", `
    66  for nst in user mnt net; do
    67  	echo "\"/proc/$$/ns/$nst\""
    68  done
    69  read # wait for Proceed()
    70  `)
    71  		cmd := b.Start("unshare")
    72  		defer cmd.Close()
    73  		var userns, mntns, netns string
    74  		// read the filesystem path references to newly created namespaces.
    75  		cmd.Decode(&userns)
    76  		cmd.Decode(&mntns)
    77  		cmd.Decode(&netns)
    78  		var nsids []uint64
    79  		Expect(reexec.RunReexecAction(
    80  			"enter",
    81  			reexec.Namespaces([]reexec.Namespace{
    82  				{Type: "!user", Path: userns},
    83  				{Type: "!mnt", Path: mntns},
    84  				{Type: "!net", Path: netns},
    85  			}),
    86  			reexec.Result(&nsids),
    87  		)).ToNot(HaveOccurred())
    88  		Expect(nsids).To(Equal([]uint64{
    89  			ID(userns),
    90  			ID(mntns),
    91  			ID(netns),
    92  		}))
    93  	})
    94  
    95  	It("converts ns switch errors to text", func() {
    96  		nse := gons.NamespaceSwitchError{}
    97  		Expect(nse.Error()).To(Equal(""))
    98  		var n *gons.NamespaceSwitchError
    99  		Expect(n.Error()).To(Equal("<nil>"))
   100  	})
   101  
   102  })
   103  
   104  func ID(p string) uint64 {
   105  	id, _ := ops.NamespacePath(p).ID()
   106  	return id.Ino
   107  }