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