github.com/stackdocker/rkt@v0.10.1-0.20151109095037-1aa827478248/tests/test-auth-server/aci/aci.go (about)

     1  // Copyright 2015 The rkt Authors
     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 aci
    16  
    17  import (
    18  	"bytes"
    19  	"fmt"
    20  	"io/ioutil"
    21  	"os"
    22  	"os/exec"
    23  	"path/filepath"
    24  	"time"
    25  )
    26  
    27  type aciToolkit struct {
    28  	acTool string
    29  	goTool string
    30  }
    31  
    32  func (t *aciToolkit) prepareACI() ([]byte, error) {
    33  	tmp := os.Getenv("FUNCTIONAL_TMP")
    34  	if tmp == "" {
    35  		return nil, fmt.Errorf("empty FUNCTIONAL_TMP env var")
    36  	}
    37  	taas, dir, err := t.createTree(tmp)
    38  	if taas != "" {
    39  		defer os.RemoveAll(taas)
    40  	}
    41  	if err != nil {
    42  		return nil, fmt.Errorf("failed to build ACI tree: %v", err)
    43  	}
    44  	if err := t.buildProg(dir); err != nil {
    45  		return nil, fmt.Errorf("failed to build test program: %v", err)
    46  	}
    47  	fn, err := t.buildACI(tmp, dir)
    48  	if err != nil {
    49  		return nil, fmt.Errorf("failed to build ACI: %v", err)
    50  	}
    51  	defer os.Remove(fn)
    52  	contents, err := ioutil.ReadFile(fn)
    53  	if err != nil {
    54  		return nil, fmt.Errorf("failed to read ACI to memory: %v", err)
    55  	}
    56  	return contents, nil
    57  }
    58  
    59  const (
    60  	manifestStr    = `{"acKind":"ImageManifest","acVersion":"0.7.1","name":"testprog","app":{"exec":["/prog"],"user":"0","group":"0"},"labels":[{"name":"os","value":"linux"},{"name":"arch","value":"amd64"}]}`
    61  	testProgSrcStr = `
    62  package main
    63  
    64  import "fmt"
    65  
    66  func main() {
    67  	fmt.Println("Authentication succeeded.")
    68  }
    69  `
    70  )
    71  
    72  func (t *aciToolkit) createTree(tmpDir string) (string, string, error) {
    73  	taasDir := filepath.Join(tmpDir, "taas")
    74  	aciDir := filepath.Join(taasDir, "ACI")
    75  	rootDir := filepath.Join(aciDir, "rootfs")
    76  	manifestFile := filepath.Join(aciDir, "manifest")
    77  	srcFile := filepath.Join(rootDir, "prog.go")
    78  	if err := os.Mkdir(taasDir, 0755); err != nil {
    79  		return "", "", fmt.Errorf("failed to create taas directory: %v", err)
    80  	}
    81  	if err := os.Mkdir(aciDir, 0755); err != nil {
    82  		return taasDir, "", fmt.Errorf("failed to create ACI directory: %v", err)
    83  	}
    84  	if err := os.Mkdir(rootDir, 0755); err != nil {
    85  		return taasDir, "", fmt.Errorf("failed to create rootfs directory: %v", err)
    86  	}
    87  	if err := ioutil.WriteFile(manifestFile, []byte(manifestStr), 0644); err != nil {
    88  		return taasDir, "", fmt.Errorf("failed to write manifest: %v", err)
    89  	}
    90  	if err := ioutil.WriteFile(srcFile, []byte(testProgSrcStr), 0644); err != nil {
    91  		return taasDir, "", fmt.Errorf("failed to write go source: %v", err)
    92  	}
    93  	return taasDir, aciDir, nil
    94  }
    95  
    96  func (t *aciToolkit) buildProg(aciDir string) error {
    97  	args := []string{
    98  		"go",
    99  		"build",
   100  		"-o",
   101  		"prog",
   102  		"./prog.go",
   103  	}
   104  	dir := filepath.Join(aciDir, "rootfs")
   105  	return runTool(t.goTool, args, dir)
   106  }
   107  
   108  func (t *aciToolkit) buildACI(tmpDir, aciDir string) (string, error) {
   109  	timedata, err := time.Now().MarshalBinary()
   110  	if err != nil {
   111  		return "", fmt.Errorf("failed to serialize current date to bytes: %v", err)
   112  	}
   113  	if err := ioutil.WriteFile(filepath.Join(aciDir, "rootfs", "stamp"), timedata, 0644); err != nil {
   114  		return "", fmt.Errorf("failed to write a stamp: %v", err)
   115  	}
   116  	fn := filepath.Join(tmpDir, "prog-build.aci")
   117  	args := []string{
   118  		"actool",
   119  		"build",
   120  		aciDir,
   121  		fn,
   122  	}
   123  	if err := runTool(t.acTool, args, ""); err != nil {
   124  		return "", err
   125  	}
   126  	return fn, nil
   127  }
   128  
   129  func runTool(tool string, args []string, dir string) error {
   130  	outBuf := new(bytes.Buffer)
   131  	errBuf := new(bytes.Buffer)
   132  	cmd := exec.Cmd{
   133  		Path:   tool,
   134  		Args:   args,
   135  		Dir:    dir,
   136  		Stdout: outBuf,
   137  		Stderr: errBuf,
   138  	}
   139  	if err := cmd.Run(); err != nil {
   140  		return fmt.Errorf("failed to execute `%s %s`: %v\nstdout:\n%v\n\nstderr:\n%v)", args[0], args[1], err, outBuf.String(), errBuf.String())
   141  	}
   142  	return nil
   143  }