github.com/wallyworld/juju@v0.0.0-20161013125918-6cf1bc9d917a/cmd/jujud/agent/machine/manifolds_test.go (about)

     1  // Copyright 2015 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package machine_test
     5  
     6  import (
     7  	"sort"
     8  
     9  	jc "github.com/juju/testing/checkers"
    10  	"github.com/juju/utils/set"
    11  	gc "gopkg.in/check.v1"
    12  
    13  	"github.com/juju/juju/agent"
    14  	"github.com/juju/juju/cmd/jujud/agent/machine"
    15  	"github.com/juju/juju/testing"
    16  	"github.com/juju/juju/worker"
    17  	"github.com/juju/juju/worker/dependency"
    18  	"github.com/juju/juju/worker/gate"
    19  )
    20  
    21  type ManifoldsSuite struct {
    22  	testing.BaseSuite
    23  }
    24  
    25  var _ = gc.Suite(&ManifoldsSuite{})
    26  
    27  func (*ManifoldsSuite) TestStartFuncs(c *gc.C) {
    28  	manifolds := machine.Manifolds(machine.ManifoldsConfig{
    29  		Agent: fakeAgent{},
    30  	})
    31  	for name, manifold := range manifolds {
    32  		c.Logf("checking %q manifold", name)
    33  		c.Check(manifold.Start, gc.NotNil)
    34  	}
    35  }
    36  
    37  func (*ManifoldsSuite) TestManifoldNames(c *gc.C) {
    38  	manifolds := machine.Manifolds(machine.ManifoldsConfig{})
    39  	keys := make([]string, 0, len(manifolds))
    40  	for k := range manifolds {
    41  		keys = append(keys, k)
    42  	}
    43  	sort.Strings(keys)
    44  	expectedKeys := []string{
    45  		"agent",
    46  		"api-address-updater",
    47  		"api-caller",
    48  		"api-config-watcher",
    49  		"disk-manager",
    50  		"host-key-reporter",
    51  		"log-forwarder",
    52  		"log-sender",
    53  		"logging-config-updater",
    54  		"machine-action-runner",
    55  		"machiner",
    56  		"mgo-txn-resumer",
    57  		"migration-fortress",
    58  		"migration-minion",
    59  		"migration-inactive-flag",
    60  		"proxy-config-updater",
    61  		"reboot-executor",
    62  		"serving-info-setter",
    63  		"ssh-authkeys-updater",
    64  		"ssh-identity-writer",
    65  		"state",
    66  		"state-config-watcher",
    67  		"storage-provisioner",
    68  		"termination-signal-handler",
    69  		"tools-version-checker",
    70  		"unconverted-api-workers",
    71  		"unconverted-state-workers",
    72  		"unit-agent-deployer",
    73  		"upgrade-check-flag",
    74  		"upgrade-check-gate",
    75  		"upgrade-steps-flag",
    76  		"upgrade-steps-gate",
    77  		"upgrade-steps-runner",
    78  		"upgrader",
    79  	}
    80  	c.Assert(keys, jc.SameContents, expectedKeys)
    81  }
    82  
    83  func (*ManifoldsSuite) TestUpgradesBlockMigration(c *gc.C) {
    84  	manifolds := machine.Manifolds(machine.ManifoldsConfig{})
    85  	manifold, ok := manifolds["migration-fortress"]
    86  	c.Assert(ok, jc.IsTrue)
    87  
    88  	checkContains(c, manifold.Inputs, "upgrade-check-flag")
    89  	checkContains(c, manifold.Inputs, "upgrade-steps-flag")
    90  }
    91  
    92  func (*ManifoldsSuite) TestMigrationGuardsUsed(c *gc.C) {
    93  	exempt := set.NewStrings(
    94  		"agent",
    95  		"api-caller",
    96  		"api-config-watcher",
    97  		"log-forwarder",
    98  		"state",
    99  		"state-config-watcher",
   100  		"termination-signal-handler",
   101  		"unconverted-state-workers",
   102  		"migration-fortress",
   103  		"migration-inactive-flag",
   104  		"migration-minion",
   105  		"upgrade-check-flag",
   106  		"upgrade-check-gate",
   107  		"upgrade-steps-flag",
   108  		"upgrade-steps-gate",
   109  		"upgrade-steps-runner",
   110  		"upgrader",
   111  	)
   112  	manifolds := machine.Manifolds(machine.ManifoldsConfig{})
   113  	for name, manifold := range manifolds {
   114  		c.Logf(name)
   115  		if !exempt.Contains(name) {
   116  			checkContains(c, manifold.Inputs, "migration-fortress")
   117  			checkContains(c, manifold.Inputs, "migration-inactive-flag")
   118  		}
   119  	}
   120  }
   121  
   122  func checkContains(c *gc.C, names []string, seek string) {
   123  	for _, name := range names {
   124  		if name == seek {
   125  			return
   126  		}
   127  	}
   128  	c.Errorf("%q not found in %v", seek, names)
   129  }
   130  
   131  func (*ManifoldsSuite) TestUpgradeGates(c *gc.C) {
   132  	upgradeStepsLock := gate.NewLock()
   133  	upgradeCheckLock := gate.NewLock()
   134  	manifolds := machine.Manifolds(machine.ManifoldsConfig{
   135  		UpgradeStepsLock: upgradeStepsLock,
   136  		UpgradeCheckLock: upgradeCheckLock,
   137  	})
   138  	assertGate(c, manifolds["upgrade-steps-gate"], upgradeStepsLock)
   139  	assertGate(c, manifolds["upgrade-check-gate"], upgradeCheckLock)
   140  }
   141  
   142  func assertGate(c *gc.C, manifold dependency.Manifold, unlocker gate.Unlocker) {
   143  	w, err := manifold.Start(nil)
   144  	c.Assert(err, jc.ErrorIsNil)
   145  	defer worker.Stop(w)
   146  
   147  	var waiter gate.Waiter
   148  	err = manifold.Output(w, &waiter)
   149  	c.Assert(err, jc.ErrorIsNil)
   150  
   151  	select {
   152  	case <-waiter.Unlocked():
   153  		c.Fatalf("expected gate to be locked")
   154  	default:
   155  	}
   156  
   157  	unlocker.Unlock()
   158  
   159  	select {
   160  	case <-waiter.Unlocked():
   161  	default:
   162  		c.Fatalf("expected gate to be unlocked")
   163  	}
   164  }
   165  
   166  type fakeAgent struct {
   167  	agent.Agent
   168  }