github.hscsec.cn/openshift/source-to-image@v1.2.0/pkg/util/injection_test.go (about)

     1  package util
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  	"io/ioutil"
     7  	"os"
     8  	"path/filepath"
     9  	"strings"
    10  	"testing"
    11  
    12  	"github.com/openshift/source-to-image/pkg/api"
    13  	"github.com/openshift/source-to-image/pkg/util/fs"
    14  )
    15  
    16  func TestCreateTruncateFilesScript(t *testing.T) {
    17  	files := []string{
    18  		"/foo",
    19  		"/bar/bar",
    20  	}
    21  	name, err := CreateTruncateFilesScript(files, "/tmp/rm-foo")
    22  	defer os.Remove(name)
    23  	if err != nil {
    24  		t.Errorf("Unexpected error: %v", name)
    25  	}
    26  	_, err = os.Stat(name)
    27  	if err != nil {
    28  		t.Errorf("Expected file %q to exists, got: %v", name, err)
    29  	}
    30  	data, err := ioutil.ReadFile(name)
    31  	if err != nil {
    32  		t.Errorf("Unable to read %q: %v", name, err)
    33  	}
    34  	for _, f := range files {
    35  		if !strings.Contains(string(data), fmt.Sprintf("truncate -s0 %q", f)) {
    36  			t.Errorf("Expected script to contain truncate -s0 %q, got: %q", f, string(data))
    37  		}
    38  	}
    39  
    40  	if !strings.Contains(string(data), fmt.Sprintf("truncate -s0 %q", "/tmp/rm-foo")) {
    41  		t.Errorf("Expected script to truncate itself, got: %q", string(data))
    42  	}
    43  }
    44  
    45  func TestListFilesToTruncate(t *testing.T) {
    46  	tmp, err := ioutil.TempDir("", "s2i-test-")
    47  	tmpKeep, err := ioutil.TempDir("", "s2i-test-")
    48  	tmpNested, err := ioutil.TempDir(tmp, "nested")
    49  	if err != nil {
    50  		t.Errorf("Unable to create temp directory: %v", err)
    51  	}
    52  	defer os.RemoveAll(tmp)
    53  	defer os.RemoveAll(tmpKeep)
    54  	list := api.VolumeList{
    55  		{Source: tmp, Destination: "/foo"},
    56  		{Source: tmpKeep, Destination: "/this", Keep: true},
    57  	}
    58  	f1, _ := ioutil.TempFile(tmp, "foo")
    59  	f2, _ := ioutil.TempFile(tmpNested, "bar")
    60  	ioutil.TempFile(tmpKeep, "that")
    61  	files, err := ListFilesToTruncate(fs.NewFileSystem(), list)
    62  	if err != nil {
    63  		t.Errorf("Unexpected error: %v", err)
    64  	}
    65  	expected := []string{"/foo/" + filepath.Base(f1.Name()), "/foo/" + filepath.Base(tmpNested) + "/" + filepath.Base(f2.Name())}
    66  	if len(expected) != len(files) {
    67  		t.Errorf("Expected %d files in resulting list, got %+v", len(expected), files)
    68  	}
    69  	for _, exp := range expected {
    70  		found := false
    71  		for _, f := range files {
    72  			if f == exp {
    73  				found = true
    74  				break
    75  			}
    76  		}
    77  		if !found {
    78  			t.Errorf("Expected %q in resulting file list, got %+v", exp, files)
    79  		}
    80  	}
    81  }
    82  
    83  func TestCreateInjectionResultFile(t *testing.T) {
    84  	type testCase struct {
    85  		Error   error
    86  		IsEmpty bool
    87  	}
    88  
    89  	testCases := []testCase{
    90  		{Error: nil, IsEmpty: true},
    91  		{Error: errors.New("test error"), IsEmpty: false},
    92  	}
    93  
    94  	for _, tc := range testCases {
    95  		name, err := CreateInjectionResultFile(tc.Error)
    96  		defer os.Remove(name)
    97  		if err != nil {
    98  			t.Errorf("Failed to create result file: %v", err)
    99  		}
   100  		_, err = os.Stat(name)
   101  		if err != nil {
   102  			t.Errorf("Expected file %q to exists, got: %v", name, err)
   103  		}
   104  		data, err := ioutil.ReadFile(name)
   105  		if err != nil {
   106  			t.Errorf("Unable to read %q: %v", name, err)
   107  		}
   108  		if tc.IsEmpty && len(data) > 0 {
   109  			t.Errorf("Expected test file to be empty, got %s", string(data))
   110  		}
   111  		if !tc.IsEmpty && !strings.Contains(string(data), tc.Error.Error()) {
   112  			t.Errorf("Expected test file to contain %s, got %s", tc.Error.Error(), string(data))
   113  		}
   114  	}
   115  }