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