github.com/singularityware/singularity@v3.1.1+incompatible/pkg/ocibundle/sif/bundle_test.go (about)

     1  // Copyright (c) 2019, 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 sifbundle
     7  
     8  import (
     9  	"io/ioutil"
    10  	"os"
    11  	"os/exec"
    12  	"path/filepath"
    13  	"runtime"
    14  	"testing"
    15  
    16  	"github.com/sylabs/singularity/pkg/ocibundle/tools"
    17  
    18  	"github.com/opencontainers/runtime-tools/generate"
    19  	"github.com/sylabs/singularity/internal/pkg/test"
    20  )
    21  
    22  func TestFromSif(t *testing.T) {
    23  	test.EnsurePrivilege(t)
    24  
    25  	// prepare bundle directory and download a SIF image
    26  	bundlePath, err := ioutil.TempDir("", "bundle")
    27  	if err != nil {
    28  		t.Fatal(err)
    29  	}
    30  	f, err := ioutil.TempFile("", "busybox")
    31  	if err != nil {
    32  		t.Fatal(err)
    33  	}
    34  	sifFile := f.Name()
    35  	f.Close()
    36  	defer os.Remove(sifFile)
    37  
    38  	sing, err := exec.LookPath("singularity")
    39  	if err != nil {
    40  		t.Fatal(err)
    41  	}
    42  	args := []string{"build", "-F", sifFile, "docker://busybox"}
    43  
    44  	// build SIF image
    45  	cmd := exec.Command(sing, args...)
    46  	if err := cmd.Run(); err != nil {
    47  		t.Fatal(err)
    48  	}
    49  
    50  	// test with a wrong image path
    51  	bundle, err := FromSif("/blah", bundlePath, false)
    52  	if err != nil {
    53  		t.Errorf("unexpected success while opening non existent image")
    54  	}
    55  	// create OCI bundle from SIF
    56  	if err := bundle.Create(nil); err == nil {
    57  		// check if cleanup occured
    58  		t.Errorf("unexpected success while creating OCI bundle")
    59  	}
    60  
    61  	// create OCI bundle from SIF
    62  	bundle, err = FromSif(sifFile, bundlePath, true)
    63  	if err != nil {
    64  		t.Fatal(err)
    65  	}
    66  	// generate a default configuration
    67  	g, err := generate.New(runtime.GOOS)
    68  	if err != nil {
    69  		t.Fatal(err)
    70  	}
    71  	// remove seccomp filter for CI
    72  	g.Config.Linux.Seccomp = nil
    73  	g.SetProcessArgs([]string{tools.RunScript, "id"})
    74  
    75  	if err := bundle.Create(g.Config); err != nil {
    76  		// check if cleanup occured
    77  		t.Fatal(err)
    78  	}
    79  
    80  	// execute oci run command
    81  	args = []string{"oci", "run", "-b", bundlePath, filepath.Base(sifFile)}
    82  	cmd = exec.Command(sing, args...)
    83  	if err := cmd.Run(); err != nil {
    84  		t.Error(err)
    85  	}
    86  
    87  	if err := bundle.Delete(); err != nil {
    88  		t.Error(err)
    89  	}
    90  }