github.com/altoros/juju-vmware@v0.0.0-20150312064031-f19ae857ccca/environs/manual/init_test.go (about)

     1  // Copyright 2013 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package manual_test
     5  
     6  import (
     7  	"strings"
     8  
     9  	jc "github.com/juju/testing/checkers"
    10  	gc "gopkg.in/check.v1"
    11  
    12  	"github.com/juju/juju/environs/manual"
    13  	"github.com/juju/juju/testing"
    14  )
    15  
    16  type initialisationSuite struct {
    17  	testing.BaseSuite
    18  }
    19  
    20  var _ = gc.Suite(&initialisationSuite{})
    21  
    22  func (s *initialisationSuite) TestDetectSeries(c *gc.C) {
    23  	response := strings.Join([]string{
    24  		"edgy",
    25  		"armv4",
    26  		"MemTotal: 4096 kB",
    27  		"processor: 0",
    28  	}, "\n")
    29  	defer installFakeSSH(c, manual.DetectionScript, response, 0)()
    30  	_, series, err := manual.DetectSeriesAndHardwareCharacteristics("whatever")
    31  	c.Assert(err, jc.ErrorIsNil)
    32  	c.Assert(series, gc.Equals, "edgy")
    33  }
    34  
    35  func (s *initialisationSuite) TestDetectionError(c *gc.C) {
    36  	scriptResponse := strings.Join([]string{
    37  		"edgy",
    38  		"armv4",
    39  		"MemTotal: 4096 kB",
    40  		"processor: 0",
    41  	}, "\n")
    42  	// if the script fails for whatever reason, then checkProvisioned
    43  	// will return an error. stderr will be included in the error message.
    44  	defer installFakeSSH(c, manual.DetectionScript, []string{scriptResponse, "oh noes"}, 33)()
    45  	hc, _, err := manual.DetectSeriesAndHardwareCharacteristics("hostname")
    46  	c.Assert(err, gc.ErrorMatches, "subprocess encountered error code 33 \\(oh noes\\)")
    47  	// if the script doesn't fail, stderr is simply ignored.
    48  	defer installFakeSSH(c, manual.DetectionScript, []string{scriptResponse, "non-empty-stderr"}, 0)()
    49  	hc, _, err = manual.DetectSeriesAndHardwareCharacteristics("hostname")
    50  	c.Assert(err, jc.ErrorIsNil)
    51  	c.Assert(hc.String(), gc.Equals, "arch=armhf cpu-cores=1 mem=4M")
    52  }
    53  
    54  func (s *initialisationSuite) TestDetectHardwareCharacteristics(c *gc.C) {
    55  	tests := []struct {
    56  		summary        string
    57  		scriptResponse []string
    58  		expectedHc     string
    59  	}{{
    60  		"Single CPU socket, single core, no hyper-threading",
    61  		[]string{"edgy", "armv4", "MemTotal: 4096 kB", "processor: 0"},
    62  		"arch=armhf cpu-cores=1 mem=4M",
    63  	}, {
    64  		"Single CPU socket, single core, hyper-threading",
    65  		[]string{
    66  			"edgy", "armv4", "MemTotal: 4096 kB",
    67  			"processor: 0",
    68  			"physical id: 0",
    69  			"cpu cores: 1",
    70  			"processor: 1",
    71  			"physical id: 0",
    72  			"cpu cores: 1",
    73  		},
    74  		"arch=armhf cpu-cores=1 mem=4M",
    75  	}, {
    76  		"Single CPU socket, dual-core, no hyper-threading",
    77  		[]string{
    78  			"edgy", "armv4", "MemTotal: 4096 kB",
    79  			"processor: 0",
    80  			"physical id: 0",
    81  			"cpu cores: 2",
    82  			"processor: 1",
    83  			"physical id: 0",
    84  			"cpu cores: 2",
    85  		},
    86  		"arch=armhf cpu-cores=2 mem=4M",
    87  	}, {
    88  		"Dual CPU socket, each single-core, hyper-threading",
    89  		[]string{
    90  			"edgy", "armv4", "MemTotal: 4096 kB",
    91  			"processor: 0",
    92  			"physical id: 0",
    93  			"cpu cores: 1",
    94  			"processor: 1",
    95  			"physical id: 0",
    96  			"cpu cores: 1",
    97  			"processor: 2",
    98  			"physical id: 1",
    99  			"cpu cores: 1",
   100  			"processor: 3",
   101  			"physical id: 1",
   102  			"cpu cores: 1",
   103  		},
   104  		"arch=armhf cpu-cores=2 mem=4M",
   105  	}}
   106  	for i, test := range tests {
   107  		c.Logf("test %d: %s", i, test.summary)
   108  		scriptResponse := strings.Join(test.scriptResponse, "\n")
   109  		defer installFakeSSH(c, manual.DetectionScript, scriptResponse, 0)()
   110  		hc, _, err := manual.DetectSeriesAndHardwareCharacteristics("hostname")
   111  		c.Assert(err, jc.ErrorIsNil)
   112  		c.Assert(hc.String(), gc.Equals, test.expectedHc)
   113  	}
   114  }
   115  
   116  func (s *initialisationSuite) TestCheckProvisioned(c *gc.C) {
   117  	defer installFakeSSH(c, manual.CheckProvisionedScript, "", 0)()
   118  	provisioned, err := manual.CheckProvisioned("example.com")
   119  	c.Assert(err, jc.ErrorIsNil)
   120  	c.Assert(provisioned, jc.IsFalse)
   121  
   122  	defer installFakeSSH(c, manual.CheckProvisionedScript, "non-empty", 0)()
   123  	provisioned, err = manual.CheckProvisioned("example.com")
   124  	c.Assert(err, jc.ErrorIsNil)
   125  	c.Assert(provisioned, jc.IsTrue)
   126  
   127  	// stderr should not affect result.
   128  	defer installFakeSSH(c, manual.CheckProvisionedScript, []string{"", "non-empty-stderr"}, 0)()
   129  	provisioned, err = manual.CheckProvisioned("example.com")
   130  	c.Assert(err, jc.ErrorIsNil)
   131  	c.Assert(provisioned, jc.IsFalse)
   132  
   133  	// if the script fails for whatever reason, then checkProvisioned
   134  	// will return an error. stderr will be included in the error message.
   135  	defer installFakeSSH(c, manual.CheckProvisionedScript, []string{"non-empty-stdout", "non-empty-stderr"}, 255)()
   136  	_, err = manual.CheckProvisioned("example.com")
   137  	c.Assert(err, gc.ErrorMatches, "subprocess encountered error code 255 \\(non-empty-stderr\\)")
   138  }
   139  
   140  func (s *initialisationSuite) TestInitUbuntuUserNonExisting(c *gc.C) {
   141  	defer installFakeSSH(c, "", "", 0)() // successful creation of ubuntu user
   142  	defer installFakeSSH(c, "", "", 1)() // simulate failure of ubuntu@ login
   143  	err := manual.InitUbuntuUser("testhost", "testuser", "", nil, nil)
   144  	c.Assert(err, jc.ErrorIsNil)
   145  }
   146  
   147  func (s *initialisationSuite) TestInitUbuntuUserExisting(c *gc.C) {
   148  	defer installFakeSSH(c, "", nil, 0)()
   149  	manual.InitUbuntuUser("testhost", "testuser", "", nil, nil)
   150  }
   151  
   152  func (s *initialisationSuite) TestInitUbuntuUserError(c *gc.C) {
   153  	defer installFakeSSH(c, "", []string{"", "failed to create ubuntu user"}, 123)()
   154  	defer installFakeSSH(c, "", "", 1)() // simulate failure of ubuntu@ login
   155  	err := manual.InitUbuntuUser("testhost", "testuser", "", nil, nil)
   156  	c.Assert(err, gc.ErrorMatches, "subprocess encountered error code 123 \\(failed to create ubuntu user\\)")
   157  }