github.com/turtlemonvh/terraform@v0.6.9-0.20151204001754-8e40b6b855e8/builtin/provisioners/chef/resource_provisioner_test.go (about)

     1  package chef
     2  
     3  import (
     4  	"fmt"
     5  	"path"
     6  	"testing"
     7  
     8  	"github.com/hashicorp/terraform/communicator"
     9  	"github.com/hashicorp/terraform/config"
    10  	"github.com/hashicorp/terraform/terraform"
    11  )
    12  
    13  func TestResourceProvisioner_impl(t *testing.T) {
    14  	var _ terraform.ResourceProvisioner = new(ResourceProvisioner)
    15  }
    16  
    17  func TestResourceProvider_Validate_good(t *testing.T) {
    18  	c := testConfig(t, map[string]interface{}{
    19  		"attributes":             []interface{}{"key1 { subkey1 = value1 }"},
    20  		"environment":            "_default",
    21  		"node_name":              "nodename1",
    22  		"run_list":               []interface{}{"cookbook::recipe"},
    23  		"server_url":             "https://chef.local",
    24  		"validation_client_name": "validator",
    25  		"validation_key":         "contentsofsomevalidator.pem",
    26  	})
    27  	r := new(ResourceProvisioner)
    28  	warn, errs := r.Validate(c)
    29  	if len(warn) > 0 {
    30  		t.Fatalf("Warnings: %v", warn)
    31  	}
    32  	if len(errs) > 0 {
    33  		t.Fatalf("Errors: %v", errs)
    34  	}
    35  }
    36  
    37  func TestResourceProvider_Validate_bad(t *testing.T) {
    38  	c := testConfig(t, map[string]interface{}{
    39  		"invalid": "nope",
    40  	})
    41  	p := new(ResourceProvisioner)
    42  	warn, errs := p.Validate(c)
    43  	if len(warn) > 0 {
    44  		t.Fatalf("Warnings: %v", warn)
    45  	}
    46  	if len(errs) == 0 {
    47  		t.Fatalf("Should have errors")
    48  	}
    49  }
    50  
    51  func testConfig(t *testing.T, c map[string]interface{}) *terraform.ResourceConfig {
    52  	r, err := config.NewRawConfig(c)
    53  	if err != nil {
    54  		t.Fatalf("bad: %s", err)
    55  	}
    56  
    57  	return terraform.NewResourceConfig(r)
    58  }
    59  
    60  func TestResourceProvider_runChefClient(t *testing.T) {
    61  	cases := map[string]struct {
    62  		Config   *terraform.ResourceConfig
    63  		ChefCmd  string
    64  		ConfDir  string
    65  		Commands map[string]bool
    66  	}{
    67  		"Sudo": {
    68  			Config: testConfig(t, map[string]interface{}{
    69  				"node_name":              "nodename1",
    70  				"run_list":               []interface{}{"cookbook::recipe"},
    71  				"server_url":             "https://chef.local",
    72  				"validation_client_name": "validator",
    73  				"validation_key_path":    "test-fixtures/validator.pem",
    74  			}),
    75  
    76  			ChefCmd: linuxChefCmd,
    77  
    78  			ConfDir: linuxConfDir,
    79  
    80  			Commands: map[string]bool{
    81  				fmt.Sprintf(`sudo %s -j %q -E "_default"`,
    82  					linuxChefCmd,
    83  					path.Join(linuxConfDir, "first-boot.json")): true,
    84  			},
    85  		},
    86  
    87  		"NoSudo": {
    88  			Config: testConfig(t, map[string]interface{}{
    89  				"node_name":              "nodename1",
    90  				"prevent_sudo":           true,
    91  				"run_list":               []interface{}{"cookbook::recipe"},
    92  				"server_url":             "https://chef.local",
    93  				"validation_client_name": "validator",
    94  				"validation_key_path":    "test-fixtures/validator.pem",
    95  			}),
    96  
    97  			ChefCmd: linuxChefCmd,
    98  
    99  			ConfDir: linuxConfDir,
   100  
   101  			Commands: map[string]bool{
   102  				fmt.Sprintf(`%s -j %q -E "_default"`,
   103  					linuxChefCmd,
   104  					path.Join(linuxConfDir, "first-boot.json")): true,
   105  			},
   106  		},
   107  
   108  		"Environment": {
   109  			Config: testConfig(t, map[string]interface{}{
   110  				"environment":            "production",
   111  				"node_name":              "nodename1",
   112  				"prevent_sudo":           true,
   113  				"run_list":               []interface{}{"cookbook::recipe"},
   114  				"server_url":             "https://chef.local",
   115  				"validation_client_name": "validator",
   116  				"validation_key_path":    "test-fixtures/validator.pem",
   117  			}),
   118  
   119  			ChefCmd: windowsChefCmd,
   120  
   121  			ConfDir: windowsConfDir,
   122  
   123  			Commands: map[string]bool{
   124  				fmt.Sprintf(`%s -j %q -E "production"`,
   125  					windowsChefCmd,
   126  					path.Join(windowsConfDir, "first-boot.json")): true,
   127  			},
   128  		},
   129  	}
   130  
   131  	r := new(ResourceProvisioner)
   132  	o := new(terraform.MockUIOutput)
   133  	c := new(communicator.MockCommunicator)
   134  
   135  	for k, tc := range cases {
   136  		c.Commands = tc.Commands
   137  
   138  		p, err := r.decodeConfig(tc.Config)
   139  		if err != nil {
   140  			t.Fatalf("Error: %v", err)
   141  		}
   142  
   143  		p.runChefClient = p.runChefClientFunc(tc.ChefCmd, tc.ConfDir)
   144  		p.useSudo = !p.PreventSudo
   145  
   146  		err = p.runChefClient(o, c)
   147  		if err != nil {
   148  			t.Fatalf("Test %q failed: %v", k, err)
   149  		}
   150  	}
   151  }