github.com/victortrac/packer@v0.7.6-0.20160602180447-63c7fdb6e41f/packer/testing.go (about)

     1  package packer
     2  
     3  import (
     4  	"bytes"
     5  	"io/ioutil"
     6  	"testing"
     7  )
     8  
     9  func TestCoreConfig(t *testing.T) *CoreConfig {
    10  	// Create some test components
    11  	components := ComponentFinder{
    12  		Builder: func(n string) (Builder, error) {
    13  			if n != "test" {
    14  				return nil, nil
    15  			}
    16  
    17  			return &MockBuilder{}, nil
    18  		},
    19  	}
    20  
    21  	return &CoreConfig{
    22  		Components: components,
    23  	}
    24  }
    25  
    26  func TestCore(t *testing.T, c *CoreConfig) *Core {
    27  	core, err := NewCore(c)
    28  	if err != nil {
    29  		t.Fatalf("err: %s", err)
    30  	}
    31  
    32  	return core
    33  }
    34  
    35  func TestUi(t *testing.T) Ui {
    36  	var buf bytes.Buffer
    37  	return &BasicUi{
    38  		Reader:      &buf,
    39  		Writer:      ioutil.Discard,
    40  		ErrorWriter: ioutil.Discard,
    41  	}
    42  }
    43  
    44  // TestBuilder sets the builder with the name n to the component finder
    45  // and returns the mock.
    46  func TestBuilder(t *testing.T, c *CoreConfig, n string) *MockBuilder {
    47  	var b MockBuilder
    48  
    49  	c.Components.Builder = func(actual string) (Builder, error) {
    50  		if actual != n {
    51  			return nil, nil
    52  		}
    53  
    54  		return &b, nil
    55  	}
    56  
    57  	return &b
    58  }
    59  
    60  // TestProvisioner sets the prov. with the name n to the component finder
    61  // and returns the mock.
    62  func TestProvisioner(t *testing.T, c *CoreConfig, n string) *MockProvisioner {
    63  	var b MockProvisioner
    64  
    65  	c.Components.Provisioner = func(actual string) (Provisioner, error) {
    66  		if actual != n {
    67  			return nil, nil
    68  		}
    69  
    70  		return &b, nil
    71  	}
    72  
    73  	return &b
    74  }
    75  
    76  // TestPostProcessor sets the prov. with the name n to the component finder
    77  // and returns the mock.
    78  func TestPostProcessor(t *testing.T, c *CoreConfig, n string) *MockPostProcessor {
    79  	var b MockPostProcessor
    80  
    81  	c.Components.PostProcessor = func(actual string) (PostProcessor, error) {
    82  		if actual != n {
    83  			return nil, nil
    84  		}
    85  
    86  		return &b, nil
    87  	}
    88  
    89  	return &b
    90  }