github.com/nak3/source-to-image@v1.1.10-0.20180319140719-2ed55639898d/pkg/util/injection_test.go (about)

     1  package util
     2  
     3  import (
     4  	"fmt"
     5  	"io/ioutil"
     6  	"os"
     7  	"path/filepath"
     8  	"strings"
     9  	"testing"
    10  
    11  	"github.com/openshift/source-to-image/pkg/api"
    12  	"github.com/openshift/source-to-image/pkg/util/fs"
    13  )
    14  
    15  func TestCreateInjectedFilesRemovalScript(t *testing.T) {
    16  	files := []string{
    17  		"/foo",
    18  		"/bar/bar",
    19  	}
    20  	name, err := CreateInjectedFilesRemovalScript(files, "/tmp/rm-foo")
    21  	defer os.Remove(name)
    22  	if err != nil {
    23  		t.Errorf("Unexpected error: %v", name)
    24  	}
    25  	_, err = os.Stat(name)
    26  	if err != nil {
    27  		t.Errorf("Expected file %q to exists, got: %v", name, err)
    28  	}
    29  	data, err := ioutil.ReadFile(name)
    30  	if err != nil {
    31  		t.Errorf("Unable to read %q: %v", name, err)
    32  	}
    33  	if !strings.Contains(string(data), fmt.Sprintf("truncate -s0 %q", "/foo")) {
    34  		t.Errorf("Expected script to contain truncate -s0 \"/foo\", got: %q", string(data))
    35  	}
    36  	if !strings.Contains(string(data), fmt.Sprintf("truncate -s0 %q", "/tmp/rm-foo")) {
    37  		t.Errorf("Expected script to truncate itself, got: %q", string(data))
    38  	}
    39  }
    40  
    41  func TestExpandInjectedFiles(t *testing.T) {
    42  	tmp, err := ioutil.TempDir("", "s2i-test-")
    43  	tmpNested, err := ioutil.TempDir(tmp, "nested")
    44  	if err != nil {
    45  		t.Errorf("Unable to create temp directory: %v", err)
    46  	}
    47  	defer os.RemoveAll(tmp)
    48  	list := api.VolumeList{{Source: tmp, Destination: "/foo"}}
    49  	f1, _ := ioutil.TempFile(tmp, "foo")
    50  	f2, _ := ioutil.TempFile(tmpNested, "bar")
    51  	files, err := ExpandInjectedFiles(fs.NewFileSystem(), list)
    52  	if err != nil {
    53  		t.Errorf("Unexpected error: %v", err)
    54  	}
    55  	expected := []string{"/foo/" + filepath.Base(f1.Name()), "/foo/" + filepath.Base(tmpNested) + "/" + filepath.Base(f2.Name())}
    56  	for _, exp := range expected {
    57  		found := false
    58  		for _, f := range files {
    59  			if f == exp {
    60  				found = true
    61  				break
    62  			}
    63  		}
    64  		if !found {
    65  			t.Errorf("Expected %q in resulting file list, got %+v", exp, files)
    66  		}
    67  	}
    68  }