launchpad.net/~rogpeppe/juju-core/500-errgo-fix@v0.0.0-20140213181702-000000002356/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
     5  
     6  import (
     7  	"strings"
     8  
     9  	gc "launchpad.net/gocheck"
    10  
    11  	jc "launchpad.net/juju-core/testing/checkers"
    12  	"launchpad.net/juju-core/testing/testbase"
    13  	sshtesting "launchpad.net/juju-core/utils/ssh/testing"
    14  )
    15  
    16  type initialisationSuite struct {
    17  	testbase.LoggingSuite
    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 sshtesting.InstallFakeSSH(c, detectionScript, response, 0)()
    30  	_, series, err := DetectSeriesAndHardwareCharacteristics("whatever")
    31  	c.Assert(err, gc.IsNil)
    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 sshtesting.InstallFakeSSH(c, detectionScript, []string{scriptResponse, "oh noes"}, 33)()
    45  	hc, _, err := DetectSeriesAndHardwareCharacteristics("hostname")
    46  	c.Assert(err, gc.ErrorMatches, "rc: 33 \\(oh noes\\)")
    47  	// if the script doesn't fail, stderr is simply ignored.
    48  	defer sshtesting.InstallFakeSSH(c, detectionScript, []string{scriptResponse, "non-empty-stderr"}, 0)()
    49  	hc, _, err = DetectSeriesAndHardwareCharacteristics("hostname")
    50  	c.Assert(err, gc.IsNil)
    51  	c.Assert(hc.String(), gc.Equals, "arch=arm 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=arm 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=arm 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=arm 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=arm 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 sshtesting.InstallFakeSSH(c, detectionScript, scriptResponse, 0)()
   110  		hc, _, err := DetectSeriesAndHardwareCharacteristics("hostname")
   111  		c.Assert(err, gc.IsNil)
   112  		c.Assert(hc.String(), gc.Equals, test.expectedHc)
   113  	}
   114  }
   115  
   116  func (s *initialisationSuite) TestCheckProvisioned(c *gc.C) {
   117  	defer sshtesting.InstallFakeSSH(c, checkProvisionedScript, "", 0)()
   118  	provisioned, err := checkProvisioned("example.com")
   119  	c.Assert(err, gc.IsNil)
   120  	c.Assert(provisioned, jc.IsFalse)
   121  
   122  	defer sshtesting.InstallFakeSSH(c, checkProvisionedScript, "non-empty", 0)()
   123  	provisioned, err = checkProvisioned("example.com")
   124  	c.Assert(err, gc.IsNil)
   125  	c.Assert(provisioned, jc.IsTrue)
   126  
   127  	// stderr should not affect result.
   128  	defer sshtesting.InstallFakeSSH(c, checkProvisionedScript, []string{"", "non-empty-stderr"}, 0)()
   129  	provisioned, err = checkProvisioned("example.com")
   130  	c.Assert(err, gc.IsNil)
   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 sshtesting.InstallFakeSSH(c, checkProvisionedScript, []string{"non-empty-stdout", "non-empty-stderr"}, 255)()
   136  	_, err = checkProvisioned("example.com")
   137  	c.Assert(err, gc.ErrorMatches, "rc: 255 \\(non-empty-stderr\\)")
   138  }
   139  
   140  func (s *initialisationSuite) TestInitUbuntuUserNonExisting(c *gc.C) {
   141  	defer sshtesting.InstallFakeSSH(c, "", "", 0)() // successful creation of ubuntu user
   142  	defer sshtesting.InstallFakeSSH(c, "", "", 1)() // simulate failure of ubuntu@ login
   143  	err := InitUbuntuUser("testhost", "testuser", "", nil, nil)
   144  	c.Assert(err, gc.IsNil)
   145  }
   146  
   147  func (s *initialisationSuite) TestInitUbuntuUserExisting(c *gc.C) {
   148  	defer sshtesting.InstallFakeSSH(c, "", nil, 0)()
   149  	InitUbuntuUser("testhost", "testuser", "", nil, nil)
   150  }
   151  
   152  func (s *initialisationSuite) TestInitUbuntuUserError(c *gc.C) {
   153  	defer sshtesting.InstallFakeSSH(c, "", []string{"", "failed to create ubuntu user"}, 123)()
   154  	defer sshtesting.InstallFakeSSH(c, "", "", 1)() // simulate failure of ubuntu@ login
   155  	err := InitUbuntuUser("testhost", "testuser", "", nil, nil)
   156  	c.Assert(err, gc.ErrorMatches, "rc: 123 \\(failed to create ubuntu user\\)")
   157  }