github.com/ddnomad/packer@v1.3.2/provisioner/converge/provisioner_test.go (about)

     1  package converge
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/hashicorp/packer/packer"
     7  )
     8  
     9  func testConfig() map[string]interface{} {
    10  	return map[string]interface{}{
    11  		"module_dirs": []map[string]interface{}{
    12  			{
    13  				"source":      "from",
    14  				"destination": "/opt/converge",
    15  			},
    16  		},
    17  		"module": "/opt/converge/test.hcl",
    18  	}
    19  }
    20  
    21  func TestProvisioner_Impl(t *testing.T) {
    22  	var raw interface{}
    23  	raw = &Provisioner{}
    24  	if _, ok := raw.(packer.Provisioner); !ok {
    25  		t.Fatal("must be a Provisioner")
    26  	}
    27  }
    28  
    29  func TestProvisionerPrepare(t *testing.T) {
    30  	t.Run("defaults", func(t *testing.T) {
    31  		t.Run("working_directory", func(t *testing.T) {
    32  			var p Provisioner
    33  			config := testConfig()
    34  
    35  			delete(config, "working_directory")
    36  
    37  			if err := p.Prepare(config); err != nil {
    38  				t.Fatalf("err: %s", err)
    39  			}
    40  
    41  			if p.config.WorkingDirectory != "/tmp" {
    42  				t.Fatalf("unexpected module directory: %s", p.config.WorkingDirectory)
    43  			}
    44  		})
    45  
    46  		t.Run("execute_command", func(t *testing.T) {
    47  			var p Provisioner
    48  			config := testConfig()
    49  
    50  			delete(config, "execute_command")
    51  
    52  			if err := p.Prepare(config); err != nil {
    53  				t.Fatalf("err: %s", err)
    54  			}
    55  
    56  			if p.config.ExecuteCommand == "" {
    57  				t.Fatal("execute command unexpectedly blank")
    58  			}
    59  		})
    60  
    61  		t.Run("bootstrap_command", func(t *testing.T) {
    62  			var p Provisioner
    63  			config := testConfig()
    64  
    65  			delete(config, "bootstrap_command")
    66  
    67  			if err := p.Prepare(config); err != nil {
    68  				t.Fatalf("err: %s", err)
    69  			}
    70  
    71  			if p.config.BootstrapCommand == "" {
    72  				t.Fatal("bootstrap command unexpectedly blank")
    73  			}
    74  		})
    75  	})
    76  
    77  	t.Run("validate", func(t *testing.T) {
    78  		t.Run("module dir", func(t *testing.T) {
    79  			t.Run("missing source", func(t *testing.T) {
    80  				var p Provisioner
    81  				config := testConfig()
    82  				delete(config["module_dirs"].([]map[string]interface{})[0], "source")
    83  
    84  				err := p.Prepare(config)
    85  				if err == nil {
    86  					t.Error("expected error")
    87  				} else if err.Error() != "Source (\"source\" key) is required in Converge module dir #0" {
    88  					t.Errorf("bad error message: %s", err)
    89  				}
    90  			})
    91  
    92  			t.Run("missing destination", func(t *testing.T) {
    93  				var p Provisioner
    94  				config := testConfig()
    95  				delete(config["module_dirs"].([]map[string]interface{})[0], "destination")
    96  
    97  				err := p.Prepare(config)
    98  				if err == nil {
    99  					t.Error("expected error")
   100  				} else if err.Error() != "Destination (\"destination\" key) is required in Converge module dir #0" {
   101  					t.Errorf("bad error message: %s", err)
   102  				}
   103  			})
   104  		})
   105  
   106  		t.Run("no module specified", func(t *testing.T) {
   107  			var p Provisioner
   108  			config := testConfig()
   109  			delete(config, "module")
   110  
   111  			err := p.Prepare(config)
   112  			if err == nil {
   113  				t.Error("expected error")
   114  			} else if err.Error() != "Converge requires a module to provision the system" {
   115  				t.Errorf("bad error message: %s", err)
   116  			}
   117  		})
   118  	})
   119  }