launchpad.net/~rogpeppe/juju-core/500-errgo-fix@v0.0.0-20140213181702-000000002356/environs/manual/fakessh.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  	"launchpad.net/juju-core/testing/testbase"
    12  	sshtesting "launchpad.net/juju-core/utils/ssh/testing"
    13  )
    14  
    15  // installDetectionFakeSSH installs a fake SSH command, which will respond
    16  // to the series/hardware detection script with the specified
    17  // series/arch.
    18  func installDetectionFakeSSH(c *gc.C, series, arch string) testbase.Restorer {
    19  	if series == "" {
    20  		series = "precise"
    21  	}
    22  	if arch == "" {
    23  		arch = "amd64"
    24  	}
    25  	detectionoutput := strings.Join([]string{
    26  		series,
    27  		arch,
    28  		"MemTotal: 4096 kB",
    29  		"processor: 0",
    30  	}, "\n")
    31  	return sshtesting.InstallFakeSSH(c, detectionScript, detectionoutput, 0)
    32  }
    33  
    34  // FakeSSH wraps the invocation of InstallFakeSSH based on the parameters.
    35  type fakeSSH struct {
    36  	Series string
    37  	Arch   string
    38  
    39  	// Provisioned should be set to true if the fakeSSH script
    40  	// should respond to checkProvisioned with a non-empty result.
    41  	Provisioned bool
    42  
    43  	// exit code for the checkProvisioned script.
    44  	CheckProvisionedExitCode int
    45  
    46  	// exit code for the machine agent provisioning script.
    47  	ProvisionAgentExitCode int
    48  
    49  	// InitUbuntuUser should be set to true if the fakeSSH script
    50  	// should respond to an attempt to initialise the ubuntu user.
    51  	InitUbuntuUser bool
    52  
    53  	// there are conditions other than error in the above
    54  	// that might cause provisioning to not go ahead, such
    55  	// as tools being missing.
    56  	SkipProvisionAgent bool
    57  
    58  	// detection will be skipped if the series/hardware were
    59  	// detected ahead of time. This should always be set to
    60  	// true when testing Bootstrap.
    61  	SkipDetection bool
    62  }
    63  
    64  // install installs fake SSH commands, which will respond to
    65  // manual provisioning/bootstrapping commands with the specified
    66  // output and exit codes.
    67  func (r fakeSSH) install(c *gc.C) testbase.Restorer {
    68  	var restore testbase.Restorer
    69  	add := func(input, output interface{}, rc int) {
    70  		restore = restore.Add(sshtesting.InstallFakeSSH(c, input, output, rc))
    71  	}
    72  	if !r.SkipProvisionAgent {
    73  		add(nil, nil, r.ProvisionAgentExitCode)
    74  	}
    75  	if !r.SkipDetection {
    76  		restore.Add(installDetectionFakeSSH(c, r.Series, r.Arch))
    77  	}
    78  	var checkProvisionedOutput interface{}
    79  	if r.Provisioned {
    80  		checkProvisionedOutput = "/etc/init/jujud-machine-0.conf"
    81  	}
    82  	add(checkProvisionedScript, checkProvisionedOutput, r.CheckProvisionedExitCode)
    83  	if r.InitUbuntuUser {
    84  		add("", nil, 0)
    85  	}
    86  	return restore
    87  }