github.com/tri-adam/singularity@v3.1.1+incompatible/cmd/singularity/cli_test.go (about)

     1  // Copyright (c) 2018, Sylabs Inc. All rights reserved.
     2  // This software is licensed under a 3-clause BSD license. Please consult the
     3  // LICENSE.md file distributed with the sources of this project regarding your
     4  // rights to use or distribute this software.
     5  
     6  package main
     7  
     8  import (
     9  	"flag"
    10  	"io/ioutil"
    11  	"log"
    12  	"os"
    13  	"os/exec"
    14  	"syscall"
    15  	"testing"
    16  
    17  	"github.com/sylabs/singularity/internal/pkg/buildcfg"
    18  	"github.com/sylabs/singularity/internal/pkg/test"
    19  )
    20  
    21  var (
    22  	cmdPath string
    23  	testDir string
    24  )
    25  
    26  var runDisabled = flag.Bool("run_disabled", false, "run tests that have been temporarily disabled")
    27  
    28  func TestSelfTest(t *testing.T) {
    29  	test.DropPrivilege(t)
    30  	defer test.ResetPrivilege(t)
    31  
    32  	cmd := exec.Command(cmdPath, "selftest")
    33  	if b, err := cmd.CombinedOutput(); err == nil {
    34  		t.Log(string(b))
    35  		t.Fatal("unexpected success running selftest")
    36  	}
    37  }
    38  
    39  func run(m *testing.M) int {
    40  
    41  	// Ensure binary is in $PATH
    42  	path, err := exec.LookPath("singularity")
    43  	if err != nil {
    44  		log.Fatalf("singularity is not installed on this system: %v", err)
    45  	}
    46  	cmdPath = path
    47  
    48  	// Ensure config is installed
    49  	if fi, err := os.Stat(buildcfg.SYSCONFDIR + "/singularity/singularity.conf"); err != nil {
    50  		log.Fatalf("singularity config is not installed on this system: %v", err)
    51  	} else if !fi.Mode().IsRegular() {
    52  		log.Fatalf("singularity config is not a regular file")
    53  	} else if fi.Sys().(*syscall.Stat_t).Uid != 0 {
    54  		log.Fatalf("singularity.conf must be owned by root")
    55  	}
    56  
    57  	// Make temp dir for tests
    58  	name, err := ioutil.TempDir("", "stest.")
    59  	if err != nil {
    60  		log.Fatalf("failed to create temporary directory: %v", err)
    61  	}
    62  	defer os.RemoveAll(name)
    63  	if err := os.Chmod(name, 0777); err != nil {
    64  		log.Fatalf("failed to chmod temporary directory: %v", err)
    65  	}
    66  	testDir = name
    67  
    68  	return m.Run()
    69  }
    70  
    71  func TestMain(m *testing.M) {
    72  	flag.Parse()
    73  
    74  	os.Exit(run(m))
    75  }