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