github.com/opencontainers/runtime-tools@v0.9.0/generate/generate_test.go (about)

     1  package generate_test
     2  
     3  import (
     4  	"io/ioutil"
     5  	"os"
     6  	"path/filepath"
     7  	"runtime"
     8  	"testing"
     9  
    10  	rfc2119 "github.com/opencontainers/runtime-tools/error"
    11  	"github.com/opencontainers/runtime-tools/generate"
    12  	"github.com/opencontainers/runtime-tools/specerror"
    13  	"github.com/opencontainers/runtime-tools/validate"
    14  )
    15  
    16  // Smoke test to ensure that _at the very least_ our default configuration
    17  // passes the validation tests. If this test fails, something is _very_ wrong
    18  // and needs to be fixed immediately (as it will break downstreams that depend
    19  // on us for a "sane default" and do compliance testing -- such as umoci).
    20  func TestGenerateValid(t *testing.T) {
    21  	plat := "linux"
    22  	if runtime.GOOS == "windows" {
    23  		plat = "windows"
    24  	}
    25  
    26  	isolations := []string{"process", "hyperv"}
    27  	for _, isolation := range isolations {
    28  		if plat == "linux" && isolation == "hyperv" {
    29  			// Combination doesn't make sense.
    30  			continue
    31  		}
    32  
    33  		bundle, err := ioutil.TempDir("", "TestGenerateValid_bundle")
    34  		if err != nil {
    35  			t.Fatal(err)
    36  		}
    37  		defer os.RemoveAll(bundle)
    38  
    39  		// Create our toy bundle.
    40  		rootfsPath := filepath.Join(bundle, "rootfs")
    41  		if err := os.Mkdir(rootfsPath, 0755); err != nil {
    42  			t.Fatal(err)
    43  		}
    44  		configPath := filepath.Join(bundle, "config.json")
    45  		g, err := generate.New(plat)
    46  		if err != nil {
    47  			t.Fatal(err)
    48  		}
    49  		if runtime.GOOS == "windows" {
    50  			g.AddWindowsLayerFolders("C:\\fakelayer")
    51  			g.AddWindowsLayerFolders("C:\\fakescratch")
    52  			if isolation == "process" {
    53  				// Add the Rootfs section (note: fake volume guid)
    54  				g.SetRootPath("\\\\?\\Volume{ec84d99e-3f02-11e7-ac6c-00155d7682cf}\\")
    55  			} else {
    56  				// Add the Hyper-V section
    57  				g.SetWindowsHypervUntilityVMPath("")
    58  			}
    59  		}
    60  		if err := (&g).SaveToFile(configPath, generate.ExportOptions{Seccomp: false}); err != nil {
    61  			t.Fatal(err)
    62  		}
    63  
    64  		// Validate the bundle.
    65  		v, err := validate.NewValidatorFromPath(bundle, true, runtime.GOOS)
    66  		if err != nil {
    67  			t.Errorf("unexpected NewValidatorFromPath error: %+v", err)
    68  		}
    69  		if err := v.CheckAll(); err != nil {
    70  			levelErrors, err := specerror.SplitLevel(err, rfc2119.Must)
    71  			if err != nil {
    72  				t.Errorf("unexpected non-multierror: %+v", err)
    73  				return
    74  			}
    75  			for _, e := range levelErrors.Warnings {
    76  				t.Logf("unexpected warning: %v", e)
    77  			}
    78  			if err := levelErrors.Error; err != nil {
    79  				t.Errorf("unexpected MUST error(s): %+v", err)
    80  			}
    81  		}
    82  	}
    83  }
    84  
    85  func TestRemoveMount(t *testing.T) {
    86  	g, err := generate.New("linux")
    87  	if err != nil {
    88  		t.Fatal(err)
    89  	}
    90  	size := len(g.Mounts())
    91  	g.RemoveMount("/dev/shm")
    92  	if size-1 != len(g.Mounts()) {
    93  		t.Errorf("Unable to remove /dev/shm from mounts")
    94  	}
    95  }