github.com/2matz/terraform@v0.6.1-0.20150714181608-a03cbdb5d5bd/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 755 " + linuxConfDir:                         true,
   170  				"sudo chown -R root.root " + linuxConfDir:                true,
   171  			},
   172  
   173  			Uploads: map[string]string{
   174  				linuxConfDir + "/client.rb":                 defaultLinuxClientConf,
   175  				linuxConfDir + "/encrypted_data_bag_secret": "SECRET-KEY-FILE",
   176  				linuxConfDir + "/first-boot.json":           `{"run_list":["cookbook::recipe"]}`,
   177  				linuxConfDir + "/ohai/hints/ohaihint.json":  "OHAI-HINT-FILE",
   178  				linuxConfDir + "/validation.pem":            "VALIDATOR-PEM-FILE",
   179  			},
   180  		},
   181  
   182  		"NoSudo": {
   183  			Config: testConfig(t, map[string]interface{}{
   184  				"node_name":              "nodename1",
   185  				"prevent_sudo":           true,
   186  				"run_list":               []interface{}{"cookbook::recipe"},
   187  				"secret_key_path":        "test-fixtures/encrypted_data_bag_secret",
   188  				"server_url":             "https://chef.local",
   189  				"validation_client_name": "validator",
   190  				"validation_key_path":    "test-fixtures/validator.pem",
   191  			}),
   192  
   193  			Commands: map[string]bool{
   194  				"mkdir -p " + linuxConfDir: true,
   195  			},
   196  
   197  			Uploads: map[string]string{
   198  				linuxConfDir + "/client.rb":                 defaultLinuxClientConf,
   199  				linuxConfDir + "/encrypted_data_bag_secret": "SECRET-KEY-FILE",
   200  				linuxConfDir + "/first-boot.json":           `{"run_list":["cookbook::recipe"]}`,
   201  				linuxConfDir + "/validation.pem":            "VALIDATOR-PEM-FILE",
   202  			},
   203  		},
   204  
   205  		"Proxy": {
   206  			Config: testConfig(t, map[string]interface{}{
   207  				"http_proxy":             "http://proxy.local",
   208  				"https_proxy":            "https://proxy.local",
   209  				"no_proxy":               []interface{}{"http://local.local", "https://local.local"},
   210  				"node_name":              "nodename1",
   211  				"prevent_sudo":           true,
   212  				"run_list":               []interface{}{"cookbook::recipe"},
   213  				"secret_key_path":        "test-fixtures/encrypted_data_bag_secret",
   214  				"server_url":             "https://chef.local",
   215  				"validation_client_name": "validator",
   216  				"validation_key_path":    "test-fixtures/validator.pem",
   217  			}),
   218  
   219  			Commands: map[string]bool{
   220  				"mkdir -p " + linuxConfDir: true,
   221  			},
   222  
   223  			Uploads: map[string]string{
   224  				linuxConfDir + "/client.rb":                 proxyLinuxClientConf,
   225  				linuxConfDir + "/encrypted_data_bag_secret": "SECRET-KEY-FILE",
   226  				linuxConfDir + "/first-boot.json":           `{"run_list":["cookbook::recipe"]}`,
   227  				linuxConfDir + "/validation.pem":            "VALIDATOR-PEM-FILE",
   228  			},
   229  		},
   230  
   231  		"Attributes": {
   232  			Config: testConfig(t, map[string]interface{}{
   233  				"attributes": []map[string]interface{}{
   234  					map[string]interface{}{
   235  						"key1": []map[string]interface{}{
   236  							map[string]interface{}{
   237  								"subkey1": []map[string]interface{}{
   238  									map[string]interface{}{
   239  										"subkey2a": []interface{}{
   240  											"val1", "val2", "val3",
   241  										},
   242  										"subkey2b": []map[string]interface{}{
   243  											map[string]interface{}{
   244  												"subkey3": "value3",
   245  											},
   246  										},
   247  									},
   248  								},
   249  							},
   250  						},
   251  						"key2": "value2",
   252  					},
   253  				},
   254  				"node_name":              "nodename1",
   255  				"prevent_sudo":           true,
   256  				"run_list":               []interface{}{"cookbook::recipe"},
   257  				"secret_key_path":        "test-fixtures/encrypted_data_bag_secret",
   258  				"server_url":             "https://chef.local",
   259  				"validation_client_name": "validator",
   260  				"validation_key_path":    "test-fixtures/validator.pem",
   261  			}),
   262  
   263  			Commands: map[string]bool{
   264  				"mkdir -p " + linuxConfDir: true,
   265  			},
   266  
   267  			Uploads: map[string]string{
   268  				linuxConfDir + "/client.rb":                 defaultLinuxClientConf,
   269  				linuxConfDir + "/encrypted_data_bag_secret": "SECRET-KEY-FILE",
   270  				linuxConfDir + "/validation.pem":            "VALIDATOR-PEM-FILE",
   271  				linuxConfDir + "/first-boot.json": `{"key1":{"subkey1":{"subkey2a":["val1","val2","val3"],` +
   272  					`"subkey2b":{"subkey3":"value3"}}},"key2":"value2","run_list":["cookbook::recipe"]}`,
   273  			},
   274  		},
   275  	}
   276  
   277  	r := new(ResourceProvisioner)
   278  	o := new(terraform.MockUIOutput)
   279  	c := new(communicator.MockCommunicator)
   280  
   281  	for k, tc := range cases {
   282  		c.Commands = tc.Commands
   283  		c.Uploads = tc.Uploads
   284  
   285  		p, err := r.decodeConfig(tc.Config)
   286  		if err != nil {
   287  			t.Fatalf("Error: %v", err)
   288  		}
   289  
   290  		p.useSudo = !p.PreventSudo
   291  
   292  		err = p.linuxCreateConfigFiles(o, c)
   293  		if err != nil {
   294  			t.Fatalf("Test %q failed: %v", k, err)
   295  		}
   296  	}
   297  }
   298  
   299  const defaultLinuxClientConf = `log_location            STDOUT
   300  chef_server_url         "https://chef.local"
   301  validation_client_name  "validator"
   302  node_name               "nodename1"`
   303  
   304  const proxyLinuxClientConf = `log_location            STDOUT
   305  chef_server_url         "https://chef.local"
   306  validation_client_name  "validator"
   307  node_name               "nodename1"
   308  
   309  
   310  http_proxy          "http://proxy.local"
   311  ENV['http_proxy'] = "http://proxy.local"
   312  ENV['HTTP_PROXY'] = "http://proxy.local"
   313  
   314  
   315  
   316  https_proxy          "https://proxy.local"
   317  ENV['https_proxy'] = "https://proxy.local"
   318  ENV['HTTPS_PROXY'] = "https://proxy.local"
   319  
   320  
   321  no_proxy "http://local.local,https://local.local"`