github.com/HashDataInc/packer@v1.3.2/common/step_create_floppy_test.go (about)

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