github.com/hpcng/singularity@v3.1.1+incompatible/cmd/singularity/deffile_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  	"html/template"
    10  	"io/ioutil"
    11  	"log"
    12  	"path"
    13  )
    14  
    15  type DefFileDetail struct {
    16  	Bootstrap   string
    17  	From        string
    18  	Registry    string
    19  	Namespace   string
    20  	Help        []string
    21  	Env         []string
    22  	Labels      map[string]string
    23  	Files       []FilePair
    24  	Pre         []string
    25  	Setup       []string
    26  	Post        []string
    27  	RunScript   []string
    28  	Test        []string
    29  	StartScript []string
    30  	Apps        []AppDetail
    31  }
    32  
    33  type AppDetail struct {
    34  	Name    string
    35  	Help    []string
    36  	Env     []string
    37  	Labels  map[string]string
    38  	Files   []FilePair
    39  	Install []string
    40  	Run     []string
    41  	Test    []string
    42  }
    43  
    44  type FilePair struct {
    45  	Src string
    46  	Dst string
    47  }
    48  
    49  // prepareDefFile reads a template from a file, applies data to it, writes the
    50  // contents to disk, and returns the path.
    51  func prepareDefFile(dfd DefFileDetail) (outputPath string) {
    52  	tmpl, err := template.ParseFiles(path.Join("testdata", "deffile.tmpl"))
    53  	if err != nil {
    54  		log.Fatalf("failed to parse template: %v", err)
    55  	}
    56  
    57  	f, err := ioutil.TempFile("", "TestTemplate-")
    58  	if err != nil {
    59  		log.Fatalf("failed to open temp file: %v", err)
    60  	}
    61  	defer f.Close()
    62  
    63  	if err := tmpl.Execute(f, dfd); err != nil {
    64  		log.Fatalf("failed to execute template: %v", err)
    65  	}
    66  
    67  	return f.Name()
    68  }