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

     1  package chef
     2  
     3  import (
     4  	"path"
     5  	"testing"
     6  
     7  	"github.com/hashicorp/terraform/communicator"
     8  	"github.com/hashicorp/terraform/terraform"
     9  )
    10  
    11  func TestResourceProvider_linuxInstallChefClient(t *testing.T) {
    12  	cases := map[string]struct {
    13  		Config   *terraform.ResourceConfig
    14  		Commands map[string]bool
    15  	}{
    16  		"Sudo": {
    17  			Config: testConfig(t, map[string]interface{}{
    18  				"node_name":              "nodename1",
    19  				"run_list":               []interface{}{"cookbook::recipe"},
    20  				"server_url":             "https://chef.local",
    21  				"validation_client_name": "validator",
    22  				"validation_key_path":    "validator.pem",
    23  			}),
    24  
    25  			Commands: map[string]bool{
    26  				"sudo curl -LO https://www.chef.io/chef/install.sh": true,
    27  				"sudo bash ./install.sh -v \"\"":                    true,
    28  				"sudo rm -f install.sh":                             true,
    29  			},
    30  		},
    31  
    32  		"NoSudo": {
    33  			Config: testConfig(t, map[string]interface{}{
    34  				"node_name":              "nodename1",
    35  				"prevent_sudo":           true,
    36  				"run_list":               []interface{}{"cookbook::recipe"},
    37  				"server_url":             "https://chef.local",
    38  				"validation_client_name": "validator",
    39  				"validation_key_path":    "validator.pem",
    40  				"secret_key_path":        "encrypted_data_bag_secret",
    41  			}),
    42  
    43  			Commands: map[string]bool{
    44  				"curl -LO https://www.chef.io/chef/install.sh": true,
    45  				"bash ./install.sh -v \"\"":                    true,
    46  				"rm -f install.sh":                             true,
    47  			},
    48  		},
    49  
    50  		"HTTPProxy": {
    51  			Config: testConfig(t, map[string]interface{}{
    52  				"http_proxy":             "http://proxy.local",
    53  				"node_name":              "nodename1",
    54  				"prevent_sudo":           true,
    55  				"run_list":               []interface{}{"cookbook::recipe"},
    56  				"server_url":             "https://chef.local",
    57  				"validation_client_name": "validator",
    58  				"validation_key_path":    "validator.pem",
    59  			}),
    60  
    61  			Commands: map[string]bool{
    62  				"http_proxy='http://proxy.local' curl -LO https://www.chef.io/chef/install.sh": true,
    63  				"http_proxy='http://proxy.local' bash ./install.sh -v \"\"":                    true,
    64  				"http_proxy='http://proxy.local' rm -f install.sh":                             true,
    65  			},
    66  		},
    67  
    68  		"HTTPSProxy": {
    69  			Config: testConfig(t, map[string]interface{}{
    70  				"https_proxy":            "https://proxy.local",
    71  				"node_name":              "nodename1",
    72  				"prevent_sudo":           true,
    73  				"run_list":               []interface{}{"cookbook::recipe"},
    74  				"server_url":             "https://chef.local",
    75  				"validation_client_name": "validator",
    76  				"validation_key_path":    "validator.pem",
    77  			}),
    78  
    79  			Commands: map[string]bool{
    80  				"https_proxy='https://proxy.local' curl -LO https://www.chef.io/chef/install.sh": true,
    81  				"https_proxy='https://proxy.local' bash ./install.sh -v \"\"":                    true,
    82  				"https_proxy='https://proxy.local' rm -f install.sh":                             true,
    83  			},
    84  		},
    85  
    86  		"NoProxy": {
    87  			Config: testConfig(t, map[string]interface{}{
    88  				"http_proxy":             "http://proxy.local",
    89  				"no_proxy":               []interface{}{"http://local.local", "http://local.org"},
    90  				"node_name":              "nodename1",
    91  				"prevent_sudo":           true,
    92  				"run_list":               []interface{}{"cookbook::recipe"},
    93  				"server_url":             "https://chef.local",
    94  				"validation_client_name": "validator",
    95  				"validation_key_path":    "validator.pem",
    96  			}),
    97  
    98  			Commands: map[string]bool{
    99  				"http_proxy='http://proxy.local' no_proxy='http://local.local,http://local.org' " +
   100  					"curl -LO https://www.chef.io/chef/install.sh": true,
   101  				"http_proxy='http://proxy.local' no_proxy='http://local.local,http://local.org' " +
   102  					"bash ./install.sh -v \"\"": true,
   103  				"http_proxy='http://proxy.local' no_proxy='http://local.local,http://local.org' " +
   104  					"rm -f install.sh": true,
   105  			},
   106  		},
   107  
   108  		"Version": {
   109  			Config: testConfig(t, map[string]interface{}{
   110  				"node_name":              "nodename1",
   111  				"prevent_sudo":           true,
   112  				"run_list":               []interface{}{"cookbook::recipe"},
   113  				"server_url":             "https://chef.local",
   114  				"validation_client_name": "validator",
   115  				"validation_key_path":    "validator.pem",
   116  				"version":                "11.18.6",
   117  			}),
   118  
   119  			Commands: map[string]bool{
   120  				"curl -LO https://www.chef.io/chef/install.sh": true,
   121  				"bash ./install.sh -v \"11.18.6\"":             true,
   122  				"rm -f install.sh":                             true,
   123  			},
   124  		},
   125  	}
   126  
   127  	r := new(ResourceProvisioner)
   128  	o := new(terraform.MockUIOutput)
   129  	c := new(communicator.MockCommunicator)
   130  
   131  	for k, tc := range cases {
   132  		c.Commands = tc.Commands
   133  
   134  		p, err := r.decodeConfig(tc.Config)
   135  		if err != nil {
   136  			t.Fatalf("Error: %v", err)
   137  		}
   138  
   139  		p.useSudo = !p.PreventSudo
   140  
   141  		err = p.linuxInstallChefClient(o, c)
   142  		if err != nil {
   143  			t.Fatalf("Test %q failed: %v", k, err)
   144  		}
   145  	}
   146  }
   147  
   148  func TestResourceProvider_linuxCreateConfigFiles(t *testing.T) {
   149  	cases := map[string]struct {
   150  		Config   *terraform.ResourceConfig
   151  		Commands map[string]bool
   152  		Uploads  map[string]string
   153  	}{
   154  		"Sudo": {
   155  			Config: testConfig(t, map[string]interface{}{
   156  				"ohai_hints":             []interface{}{"test-fixtures/ohaihint.json"},
   157  				"node_name":              "nodename1",
   158  				"run_list":               []interface{}{"cookbook::recipe"},
   159  				"secret_key_path":        "test-fixtures/encrypted_data_bag_secret",
   160  				"server_url":             "https://chef.local",
   161  				"validation_client_name": "validator",
   162  				"validation_key_path":    "test-fixtures/validator.pem",
   163  			}),
   164  
   165  			Commands: map[string]bool{
   166  				"sudo mkdir -p " + linuxConfDir:                                    true,
   167  				"sudo chmod 777 " + linuxConfDir:                                   true,
   168  				"sudo mkdir -p " + path.Join(linuxConfDir, "ohai/hints"):           true,
   169  				"sudo chmod 777 " + path.Join(linuxConfDir, "ohai/hints"):          true,
   170  				"sudo chmod 755 " + path.Join(linuxConfDir, "ohai/hints"):          true,
   171  				"sudo chown -R root.root " + path.Join(linuxConfDir, "ohai/hints"): true,
   172  				"sudo chmod 755 " + linuxConfDir:                                   true,
   173  				"sudo chown -R root.root " + linuxConfDir:                          true,
   174  			},
   175  
   176  			Uploads: map[string]string{
   177  				linuxConfDir + "/client.rb":                 defaultLinuxClientConf,
   178  				linuxConfDir + "/encrypted_data_bag_secret": "SECRET-KEY-FILE",
   179  				linuxConfDir + "/first-boot.json":           `{"run_list":["cookbook::recipe"]}`,
   180  				linuxConfDir + "/ohai/hints/ohaihint.json":  "OHAI-HINT-FILE",
   181  				linuxConfDir + "/validation.pem":            "VALIDATOR-PEM-FILE",
   182  			},
   183  		},
   184  
   185  		"NoSudo": {
   186  			Config: testConfig(t, map[string]interface{}{
   187  				"node_name":              "nodename1",
   188  				"prevent_sudo":           true,
   189  				"run_list":               []interface{}{"cookbook::recipe"},
   190  				"secret_key_path":        "test-fixtures/encrypted_data_bag_secret",
   191  				"server_url":             "https://chef.local",
   192  				"validation_client_name": "validator",
   193  				"validation_key_path":    "test-fixtures/validator.pem",
   194  			}),
   195  
   196  			Commands: map[string]bool{
   197  				"mkdir -p " + linuxConfDir: true,
   198  			},
   199  
   200  			Uploads: map[string]string{
   201  				linuxConfDir + "/client.rb":                 defaultLinuxClientConf,
   202  				linuxConfDir + "/encrypted_data_bag_secret": "SECRET-KEY-FILE",
   203  				linuxConfDir + "/first-boot.json":           `{"run_list":["cookbook::recipe"]}`,
   204  				linuxConfDir + "/validation.pem":            "VALIDATOR-PEM-FILE",
   205  			},
   206  		},
   207  
   208  		"Proxy": {
   209  			Config: testConfig(t, map[string]interface{}{
   210  				"http_proxy":             "http://proxy.local",
   211  				"https_proxy":            "https://proxy.local",
   212  				"no_proxy":               []interface{}{"http://local.local", "https://local.local"},
   213  				"node_name":              "nodename1",
   214  				"prevent_sudo":           true,
   215  				"run_list":               []interface{}{"cookbook::recipe"},
   216  				"secret_key_path":        "test-fixtures/encrypted_data_bag_secret",
   217  				"server_url":             "https://chef.local",
   218  				"validation_client_name": "validator",
   219  				"validation_key_path":    "test-fixtures/validator.pem",
   220  			}),
   221  
   222  			Commands: map[string]bool{
   223  				"mkdir -p " + linuxConfDir: true,
   224  			},
   225  
   226  			Uploads: map[string]string{
   227  				linuxConfDir + "/client.rb":                 proxyLinuxClientConf,
   228  				linuxConfDir + "/encrypted_data_bag_secret": "SECRET-KEY-FILE",
   229  				linuxConfDir + "/first-boot.json":           `{"run_list":["cookbook::recipe"]}`,
   230  				linuxConfDir + "/validation.pem":            "VALIDATOR-PEM-FILE",
   231  			},
   232  		},
   233  
   234  		"Attributes": {
   235  			Config: testConfig(t, map[string]interface{}{
   236  				"attributes": []map[string]interface{}{
   237  					map[string]interface{}{
   238  						"key1": []map[string]interface{}{
   239  							map[string]interface{}{
   240  								"subkey1": []map[string]interface{}{
   241  									map[string]interface{}{
   242  										"subkey2a": []interface{}{
   243  											"val1", "val2", "val3",
   244  										},
   245  										"subkey2b": []map[string]interface{}{
   246  											map[string]interface{}{
   247  												"subkey3": "value3",
   248  											},
   249  										},
   250  									},
   251  								},
   252  							},
   253  						},
   254  						"key2": "value2",
   255  					},
   256  				},
   257  				"node_name":              "nodename1",
   258  				"prevent_sudo":           true,
   259  				"run_list":               []interface{}{"cookbook::recipe"},
   260  				"secret_key_path":        "test-fixtures/encrypted_data_bag_secret",
   261  				"server_url":             "https://chef.local",
   262  				"validation_client_name": "validator",
   263  				"validation_key_path":    "test-fixtures/validator.pem",
   264  			}),
   265  
   266  			Commands: map[string]bool{
   267  				"mkdir -p " + linuxConfDir: true,
   268  			},
   269  
   270  			Uploads: map[string]string{
   271  				linuxConfDir + "/client.rb":                 defaultLinuxClientConf,
   272  				linuxConfDir + "/encrypted_data_bag_secret": "SECRET-KEY-FILE",
   273  				linuxConfDir + "/validation.pem":            "VALIDATOR-PEM-FILE",
   274  				linuxConfDir + "/first-boot.json": `{"key1":{"subkey1":{"subkey2a":["val1","val2","val3"],` +
   275  					`"subkey2b":{"subkey3":"value3"}}},"key2":"value2","run_list":["cookbook::recipe"]}`,
   276  			},
   277  		},
   278  	}
   279  
   280  	r := new(ResourceProvisioner)
   281  	o := new(terraform.MockUIOutput)
   282  	c := new(communicator.MockCommunicator)
   283  
   284  	for k, tc := range cases {
   285  		c.Commands = tc.Commands
   286  		c.Uploads = tc.Uploads
   287  
   288  		p, err := r.decodeConfig(tc.Config)
   289  		if err != nil {
   290  			t.Fatalf("Error: %v", err)
   291  		}
   292  
   293  		p.useSudo = !p.PreventSudo
   294  
   295  		err = p.linuxCreateConfigFiles(o, c)
   296  		if err != nil {
   297  			t.Fatalf("Test %q failed: %v", k, err)
   298  		}
   299  	}
   300  }
   301  
   302  const defaultLinuxClientConf = `log_location            STDOUT
   303  chef_server_url         "https://chef.local"
   304  validation_client_name  "validator"
   305  node_name               "nodename1"`
   306  
   307  const proxyLinuxClientConf = `log_location            STDOUT
   308  chef_server_url         "https://chef.local"
   309  validation_client_name  "validator"
   310  node_name               "nodename1"
   311  
   312  
   313  
   314  
   315  http_proxy          "http://proxy.local"
   316  ENV['http_proxy'] = "http://proxy.local"
   317  ENV['HTTP_PROXY'] = "http://proxy.local"
   318  
   319  
   320  
   321  https_proxy          "https://proxy.local"
   322  ENV['https_proxy'] = "https://proxy.local"
   323  ENV['HTTPS_PROXY'] = "https://proxy.local"
   324  
   325  
   326  no_proxy "http://local.local,https://local.local"`