github.com/rsyabuta/packer@v1.1.4-0.20180119234903-5ef0c2280f0b/common/step_create_floppy_test.go (about)

     1  package common
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  	"github.com/hashicorp/packer/packer"
     7  	"github.com/mitchellh/multistep"
     8  	"io/ioutil"
     9  	"log"
    10  	"os"
    11  	"path"
    12  	"path/filepath"
    13  	"strconv"
    14  	"strings"
    15  	"testing"
    16  )
    17  
    18  const TestFixtures = "test-fixtures"
    19  
    20  // utility function for returning a directory structure as a list of strings
    21  func getDirectory(path string) []string {
    22  	var result []string
    23  	walk := func(path string, info os.FileInfo, err error) error {
    24  		if err != nil {
    25  			return err
    26  		}
    27  		if info.IsDir() && !strings.HasSuffix(path, "/") {
    28  			path = path + "/"
    29  		}
    30  		result = append(result, filepath.ToSlash(path))
    31  		return nil
    32  	}
    33  	filepath.Walk(path, walk)
    34  	return result
    35  }
    36  
    37  func TestStepCreateFloppy_Impl(t *testing.T) {
    38  	var raw interface{}
    39  	raw = new(StepCreateFloppy)
    40  	if _, ok := raw.(multistep.Step); !ok {
    41  		t.Fatalf("StepCreateFloppy should be a step")
    42  	}
    43  }
    44  
    45  func testStepCreateFloppyState(t *testing.T) multistep.StateBag {
    46  	state := new(multistep.BasicStateBag)
    47  	state.Put("ui", &packer.BasicUi{
    48  		Reader: new(bytes.Buffer),
    49  		Writer: new(bytes.Buffer),
    50  	})
    51  	return state
    52  }
    53  
    54  func TestStepCreateFloppy(t *testing.T) {
    55  	state := testStepCreateFloppyState(t)
    56  	step := new(StepCreateFloppy)
    57  
    58  	dir, err := ioutil.TempDir("", "packer")
    59  	if err != nil {
    60  		t.Fatalf("err: %s", err)
    61  	}
    62  	defer os.RemoveAll(dir)
    63  
    64  	count := 10
    65  	expected := count
    66  	files := make([]string, count)
    67  
    68  	prefix := "exists"
    69  	ext := ".tmp"
    70  
    71  	for i := 0; i < expected; i++ {
    72  		files[i] = path.Join(dir, prefix+strconv.Itoa(i)+ext)
    73  
    74  		_, err := os.Create(files[i])
    75  		if err != nil {
    76  			t.Fatalf("err: %s", err)
    77  		}
    78  	}
    79  
    80  	lists := [][]string{
    81  		files,
    82  		{dir + string(os.PathSeparator) + prefix + "*" + ext},
    83  		{dir + string(os.PathSeparator) + prefix + "?" + ext},
    84  		{dir + string(os.PathSeparator) + prefix + "[0123456789]" + ext},
    85  		{dir + string(os.PathSeparator) + prefix + "[0-9]" + ext},
    86  		{dir + string(os.PathSeparator)},
    87  		{dir},
    88  	}
    89  
    90  	for _, step.Files = range lists {
    91  		if action := step.Run(state); action != multistep.ActionContinue {
    92  			t.Fatalf("bad action: %#v for %v", action, step.Files)
    93  		}
    94  
    95  		if _, ok := state.GetOk("error"); ok {
    96  			t.Fatalf("state should be ok for %v", step.Files)
    97  		}
    98  
    99  		floppy_path := state.Get("floppy_path").(string)
   100  
   101  		if _, err := os.Stat(floppy_path); err != nil {
   102  			t.Fatalf("file not found: %s for %v", floppy_path, step.Files)
   103  		}
   104  
   105  		if len(step.FilesAdded) != expected {
   106  			t.Fatalf("expected %d, found %d for %v", expected, len(step.FilesAdded), step.Files)
   107  		}
   108  
   109  		step.Cleanup(state)
   110  
   111  		if _, err := os.Stat(floppy_path); err == nil {
   112  			t.Fatalf("file found: %s for %v", floppy_path, step.Files)
   113  		}
   114  	}
   115  }
   116  
   117  func xxxTestStepCreateFloppy_missing(t *testing.T) {
   118  	state := testStepCreateFloppyState(t)
   119  	step := new(StepCreateFloppy)
   120  
   121  	dir, err := ioutil.TempDir("", "packer")
   122  	if err != nil {
   123  		t.Fatalf("err: %s", err)
   124  	}
   125  	defer os.RemoveAll(dir)
   126  
   127  	count := 2
   128  	expected := 0
   129  	files := make([]string, count)
   130  
   131  	prefix := "missing"
   132  
   133  	for i := 0; i < count; i++ {
   134  		files[i] = path.Join(dir, prefix+strconv.Itoa(i))
   135  	}
   136  
   137  	lists := [][]string{
   138  		files,
   139  	}
   140  
   141  	for _, step.Files = range lists {
   142  		if action := step.Run(state); action != multistep.ActionHalt {
   143  			t.Fatalf("bad action: %#v for %v", action, step.Files)
   144  		}
   145  
   146  		if _, ok := state.GetOk("error"); !ok {
   147  			t.Fatalf("state should not be ok for %v", step.Files)
   148  		}
   149  
   150  		floppy_path := state.Get("floppy_path")
   151  
   152  		if floppy_path != nil {
   153  			t.Fatalf("floppy_path is not nil for %v", step.Files)
   154  		}
   155  
   156  		if len(step.FilesAdded) != expected {
   157  			t.Fatalf("expected %d, found %d for %v", expected, len(step.FilesAdded), step.Files)
   158  		}
   159  	}
   160  }
   161  
   162  func xxxTestStepCreateFloppy_notfound(t *testing.T) {
   163  	state := testStepCreateFloppyState(t)
   164  	step := new(StepCreateFloppy)
   165  
   166  	dir, err := ioutil.TempDir("", "packer")
   167  	if err != nil {
   168  		t.Fatalf("err: %s", err)
   169  	}
   170  	defer os.RemoveAll(dir)
   171  
   172  	count := 2
   173  	expected := 0
   174  	files := make([]string, count)
   175  
   176  	prefix := "notfound"
   177  
   178  	for i := 0; i < count; i++ {
   179  		files[i] = path.Join(dir, prefix+strconv.Itoa(i))
   180  	}
   181  
   182  	lists := [][]string{
   183  		{dir + string(os.PathSeparator) + prefix + "*"},
   184  		{dir + string(os.PathSeparator) + prefix + "?"},
   185  		{dir + string(os.PathSeparator) + prefix + "[0123456789]"},
   186  		{dir + string(os.PathSeparator) + prefix + "[0-9]"},
   187  		{dir + string(os.PathSeparator)},
   188  		{dir},
   189  	}
   190  
   191  	for _, step.Files = range lists {
   192  		if action := step.Run(state); action != multistep.ActionContinue {
   193  			t.Fatalf("bad action: %#v for %v", action, step.Files)
   194  		}
   195  
   196  		if _, ok := state.GetOk("error"); ok {
   197  			t.Fatalf("state should be ok for %v", step.Files)
   198  		}
   199  
   200  		floppy_path := state.Get("floppy_path").(string)
   201  
   202  		if _, err := os.Stat(floppy_path); err != nil {
   203  			t.Fatalf("file not found: %s for %v", floppy_path, step.Files)
   204  		}
   205  
   206  		if len(step.FilesAdded) != expected {
   207  			t.Fatalf("expected %d, found %d for %v", expected, len(step.FilesAdded), step.Files)
   208  		}
   209  
   210  		step.Cleanup(state)
   211  
   212  		if _, err := os.Stat(floppy_path); err == nil {
   213  			t.Fatalf("file found: %s for %v", floppy_path, step.Files)
   214  		}
   215  	}
   216  }
   217  
   218  func TestStepCreateFloppyDirectories(t *testing.T) {
   219  	const TestName = "floppy-hier"
   220  
   221  	// file-system hierarchies
   222  	var basePath = filepath.Join(".", TestFixtures, TestName)
   223  
   224  	type contentsTest struct {
   225  		dirs   []string
   226  		result []string
   227  	}
   228  
   229  	// keep in mind that .FilesAdded doesn't keep track of the target filename or directories, but rather the source filename.
   230  	directories := [][]contentsTest{
   231  		{
   232  			{dirs: []string{"file1", "file2", "file3"}, result: []string{"file1", "file2", "file3"}},
   233  			{dirs: []string{"file?"}, result: []string{"file1", "file2", "file3"}},
   234  			{dirs: []string{"*"}, result: []string{"file1", "file2", "file3"}},
   235  		},
   236  		{
   237  			{dirs: []string{"dir1"}, result: []string{"dir1/file1", "dir1/file2", "dir1/file3"}},
   238  			{dirs: []string{"dir1/file1", "dir1/file2", "dir1/file3"}, result: []string{"dir1/file1", "dir1/file2", "dir1/file3"}},
   239  			{dirs: []string{"*"}, result: []string{"dir1/file1", "dir1/file2", "dir1/file3"}},
   240  			{dirs: []string{"*/*"}, result: []string{"dir1/file1", "dir1/file2", "dir1/file3"}},
   241  		},
   242  		{
   243  			{dirs: []string{"dir1"}, result: []string{"dir1/file1", "dir1/subdir1/file1", "dir1/subdir1/file2"}},
   244  			{dirs: []string{"dir2/*"}, result: []string{"dir2/subdir1/file1", "dir2/subdir1/file2"}},
   245  			{dirs: []string{"dir2/subdir1"}, result: []string{"dir2/subdir1/file1", "dir2/subdir1/file2"}},
   246  			{dirs: []string{"dir?"}, result: []string{"dir1/file1", "dir1/subdir1/file1", "dir1/subdir1/file2", "dir2/subdir1/file1", "dir2/subdir1/file2"}},
   247  		},
   248  	}
   249  
   250  	// create the hierarchy for each file
   251  	for i := 0; i < 2; i++ {
   252  		dir := filepath.Join(basePath, fmt.Sprintf("test-%d", i))
   253  
   254  		for _, test := range directories[i] {
   255  			// create a new state and step
   256  			state := testStepCreateFloppyState(t)
   257  			step := new(StepCreateFloppy)
   258  
   259  			// modify step.Directories with ones from testcase
   260  			step.Directories = []string{}
   261  			for _, c := range test.dirs {
   262  				step.Directories = append(step.Directories, filepath.Join(dir, filepath.FromSlash(c)))
   263  			}
   264  			log.Println(fmt.Sprintf("Trying against floppy_dirs : %v", step.Directories))
   265  
   266  			// run the step
   267  			if action := step.Run(state); action != multistep.ActionContinue {
   268  				t.Fatalf("bad action: %#v for %v : %v", action, step.Directories, state.Get("error"))
   269  			}
   270  
   271  			if _, ok := state.GetOk("error"); ok {
   272  				t.Fatalf("state should be ok for %v : %v", step.Directories, state.Get("error"))
   273  			}
   274  
   275  			floppy_path := state.Get("floppy_path").(string)
   276  			if _, err := os.Stat(floppy_path); err != nil {
   277  				t.Fatalf("file not found: %s for %v : %v", floppy_path, step.Directories, err)
   278  			}
   279  
   280  			// check the FilesAdded array to see if it matches
   281  			for _, rpath := range test.result {
   282  				fpath := filepath.Join(dir, filepath.FromSlash(rpath))
   283  				if !step.FilesAdded[fpath] {
   284  					t.Fatalf("unable to find file: %s for %v", fpath, step.Directories)
   285  				}
   286  			}
   287  
   288  			// cleanup the step
   289  			step.Cleanup(state)
   290  
   291  			if _, err := os.Stat(floppy_path); err == nil {
   292  				t.Fatalf("file found: %s for %v", floppy_path, step.Directories)
   293  			}
   294  		}
   295  	}
   296  }