Exemple #1
0
// TestAvailability tests that the charmdir resource is properly checked.
func (s *ManifoldSuite) TestAvailability(c *gc.C) {
	recorder := &dummyRecorder{
		charmURL:         "cs:wordpress-37",
		unitTag:          "wp/0",
		isDeclaredMetric: true,
	}
	s.PatchValue(collect.NewRecorder,
		func(_ names.UnitTag, _ context.Paths, _ collect.UnitCharmLookup, _ spool.MetricFactory) (spool.MetricRecorder, error) {
			return recorder, nil
		})
	charmdir := &dummyCharmdir{available: false}
	s.dummyResources["charmdir-name"] = dt.StubResource{Output: charmdir}
	getResource := dt.StubGetResource(s.dummyResources)
	collectEntity, err := (*collect.NewCollect)(s.manifoldConfig, getResource)
	c.Assert(err, jc.ErrorIsNil)
	err = collectEntity.Do(nil)
	c.Assert(err, jc.ErrorIsNil)
	c.Assert(recorder.batches, gc.HasLen, 0)

	charmdir = &dummyCharmdir{available: true}
	s.dummyResources["charmdir-name"] = dt.StubResource{Output: charmdir}
	getResource = dt.StubGetResource(s.dummyResources)
	collectEntity, err = (*collect.NewCollect)(s.manifoldConfig, getResource)
	c.Assert(err, jc.ErrorIsNil)
	err = collectEntity.Do(nil)
	c.Assert(err, jc.ErrorIsNil)
	c.Assert(recorder.closed, jc.IsTrue)
	c.Assert(recorder.batches, gc.HasLen, 1)
}
Exemple #2
0
// TestAvailability tests that the charmdir resource is properly checked.
func (s *ManifoldSuite) TestAvailability(c *gc.C) {
	recorder := &dummyRecorder{
		charmURL:         "cs:wordpress-37",
		unitTag:          "wp/0",
		isDeclaredMetric: true,
	}
	s.PatchValue(collect.NewRecorder,
		func(_ names.UnitTag, _ context.Paths, _ spool.MetricFactory) (spool.MetricRecorder, error) {
			return recorder, nil
		})
	s.PatchValue(collect.ReadCharm,
		func(_ names.UnitTag, _ context.Paths) (*corecharm.URL, map[string]corecharm.Metric, error) {
			return corecharm.MustParseURL("cs:wordpress-37"), map[string]corecharm.Metric{"pings": corecharm.Metric{Description: "test metric", Type: corecharm.MetricTypeAbsolute}}, nil
		})
	charmdir := &dummyCharmdir{aborted: true}
	s.dummyResources["charmdir-name"] = dt.StubResource{Output: charmdir}
	getResource := dt.StubGetResource(s.dummyResources)
	collectEntity, err := collect.NewCollect(s.manifoldConfig, getResource)
	c.Assert(err, jc.ErrorIsNil)
	err = collectEntity.Do(nil)
	c.Assert(err, jc.ErrorIsNil)
	c.Assert(recorder.batches, gc.HasLen, 0)

	charmdir = &dummyCharmdir{aborted: false}
	s.dummyResources["charmdir-name"] = dt.StubResource{Output: charmdir}
	getResource = dt.StubGetResource(s.dummyResources)
	collectEntity, err = collect.NewCollect(s.manifoldConfig, getResource)
	c.Assert(err, jc.ErrorIsNil)
	err = collectEntity.Do(nil)
	c.Assert(err, jc.ErrorIsNil)
	c.Assert(recorder.closed, jc.IsTrue)
	c.Assert(recorder.batches, gc.HasLen, 1)
}
Exemple #3
0
func (s *ManifoldSuite) TestNoAgent(c *gc.C) {
	getResource := dt.StubGetResource(dt.StubResources{
		"agent": dt.StubResource{Error: dependency.ErrMissing},
	})
	_, err := s.manifold.Start(getResource)
	c.Assert(err, gc.Equals, dependency.ErrMissing)
}
Exemple #4
0
func (s *ManifoldSuite) TestOutput(c *gc.C) {
	stepsLock := gate.NewLock()
	checkLock := gate.NewLock()
	getResource := dt.StubGetResource(dt.StubResources{
		"steps-waiter": dt.StubResource{Output: stepsLock},
		"check-waiter": dt.StubResource{Output: checkLock},
	})
	w, err := s.manifold.Start(getResource)
	c.Assert(err, jc.ErrorIsNil)

	// Upgrades not completed yet so output is false.
	s.assertOutputFalse(c, w)

	// Unlock one of the upgrade gates, output should still be false.
	stepsLock.Unlock()
	s.assertOutputFalse(c, w)

	// Unlock the other gate, output should now be true.
	checkLock.Unlock()
	s.assertOutputTrue(c, w)

	// .. and the worker should exit with ErrBounce.
	checkStopWithError(c, w, dependency.ErrBounce)

	// Restarting the worker should result in the output immediately
	// being true.
	w2, err := s.manifold.Start(getResource)
	c.Assert(err, jc.ErrorIsNil)
	s.assertOutputTrue(c, w)
	checkStop(c, w2)
}
Exemple #5
0
func (s *ManifoldSuite) SetUpSuite(c *gc.C) {
	s.IsolationSuite.SetUpSuite(c)
	s.stubAgentApiManifoldConfig = util.AgentApiManifoldConfig{
		AgentName:     "wut",
		APICallerName: "exactly",
	}
	s.fakeAgent = &fakeAgent{tag: fakeTag}
	s.fakeCaller = &fakeCaller{}
	s.getResource = dt.StubGetResource(dt.StubResources{
		"wut":     dt.StubResource{Output: s.fakeAgent},
		"exactly": dt.StubResource{Output: s.fakeCaller},
	})
	s.newFacade = func(facade machineactions.Facade) func(base.APICaller) machineactions.Facade {
		s.fakeFacade = facade
		return func(apiCaller base.APICaller) machineactions.Facade {
			c.Assert(apiCaller, gc.Equals, s.fakeCaller)
			return facade
		}
	}
	s.newWorker = func(w worker.Worker, err error) func(machineactions.WorkerConfig) (worker.Worker, error) {
		s.fakeWorker = w
		return func(wc machineactions.WorkerConfig) (worker.Worker, error) {
			c.Assert(wc.Facade, gc.Equals, s.fakeFacade)
			c.Assert(wc.MachineTag, gc.Equals, fakeTag)
			c.Assert(wc.HandleAction, gc.Equals, fakeHandleAction)
			return w, err
		}
	}
}
Exemple #6
0
func (s *ManifoldSuite) TestStartOpenStateNil(c *gc.C) {
	s.config.OpenState = nil
	manifold := workerstate.Manifold(s.config)
	w, err := manifold.Start(dt.StubGetResource(s.resources))
	c.Check(w, gc.IsNil)
	c.Check(err, gc.ErrorMatches, "OpenState is nil in config")
}
Exemple #7
0
func (s *ManifoldSuite) SetUpTest(c *gc.C) {
	s.IsolationSuite.SetUpTest(c)
	s.Stub = testing.Stub{}
	s.manifold = apicaller.Manifold(apicaller.ManifoldConfig{
		AgentName: "agent-name",
	})

	s.agent = &mockAgent{
		stub: &s.Stub,
		env:  coretesting.EnvironmentTag,
	}
	s.getResource = dt.StubGetResource(dt.StubResources{
		"agent-name": dt.StubResource{Output: s.agent},
	})

	// Watch out for this: it uses its own Stub because Close calls are made from
	// the worker's loop goroutine. You should make sure to stop the worker before
	// checking the mock conn's calls (unless you know the connection will outlive
	// the test -- see setupMutatorTest).
	s.conn = &mockConn{
		stub:   &testing.Stub{},
		broken: make(chan struct{}),
	}
	s.PatchValue(apicaller.OpenConnection, func(a agent.Agent) (api.Connection, error) {
		s.AddCall("openConnection", a)
		if err := s.NextErr(); err != nil {
			return nil, err
		}
		return s.conn, nil
	})
}
Exemple #8
0
func (s *ManifoldSuite) SetUpTest(c *gc.C) {
	s.BaseSuite.SetUpTest(c)
	s.stub = &testing.Stub{}

	s.manifoldConfig = meterstatus.ManifoldConfig{
		AgentName:               "agent-name",
		APICallerName:           "apicaller-name",
		MachineLockName:         "machine-lock-name",
		NewHookRunner:           meterstatus.NewHookRunner,
		NewMeterStatusAPIClient: msapi.NewClient,

		NewConnectedStatusWorker: meterstatus.NewConnectedStatusWorker,
		NewIsolatedStatusWorker:  meterstatus.NewIsolatedStatusWorker,
	}
	s.manifold = meterstatus.Manifold(s.manifoldConfig)
	s.dataDir = c.MkDir()

	locksDir := c.MkDir()
	lock, err := fslock.NewLock(locksDir, "machine-lock", fslock.Defaults())
	c.Assert(err, jc.ErrorIsNil)

	s.dummyResources = dt.StubResources{
		"agent-name":        dt.StubResource{Output: &dummyAgent{dataDir: s.dataDir}},
		"apicaller-name":    dt.StubResource{Output: &dummyAPICaller{}},
		"machine-lock-name": dt.StubResource{Output: lock},
	}
	s.getResource = dt.StubGetResource(s.dummyResources)
}
Exemple #9
0
func (s *PatchedManifoldSuite) TestStatusWorkerDoesNotRerunAfterRestart(c *gc.C) {
	getResource := dt.StubGetResource(s.dummyResources)
	worker, err := s.manifold.Start(getResource)
	c.Assert(err, jc.ErrorIsNil)
	c.Assert(worker, gc.NotNil)

	s.msClient.changes <- struct{}{}

	// Kill worker.
	worker.Kill()
	err = worker.Wait()
	c.Assert(err, jc.ErrorIsNil)

	// Restart it.
	worker, err = s.manifold.Start(getResource)
	c.Assert(err, jc.ErrorIsNil)
	c.Assert(worker, gc.NotNil)

	running := make(chan struct{})
	meterstatus.PatchInit(worker, func() { close(running) })

	select {
	case <-running:
	case <-time.After(coretesting.LongWait):
		c.Fatal("timed out waiting for signal")
	}

	worker.Kill()
	err = worker.Wait()

	s.stub.CheckCallNames(c, "MeterStatus", "RunHook", "WatchMeterStatus", "MeterStatus", "MeterStatus", "WatchMeterStatus")
	c.Assert(err, jc.ErrorIsNil)
}
Exemple #10
0
func (s *ManifoldSuite) startManifold(c *gc.C) (worker.Worker, error) {
	w, err := s.manifold.Start(dt.StubGetResource(s.resources))
	if w != nil {
		s.AddCleanup(func(*gc.C) { worker.Stop(w) })
	}
	return w, err
}
Exemple #11
0
func (s *ManifoldSuite) TestStartNewWorkerError(c *gc.C) {
	expectFacade := &fakeFacade{}
	manifold := singular.Manifold(singular.ManifoldConfig{
		ClockName:     "clock",
		APICallerName: "api-caller",
		AgentName:     "agent",
		Duration:      time.Minute,
		NewFacade: func(_ base.APICaller, _ names.MachineTag) (singular.Facade, error) {
			return expectFacade, nil
		},
		NewWorker: func(config singular.FlagConfig) (worker.Worker, error) {
			c.Check(config.Facade, gc.Equals, expectFacade)
			err := config.Validate()
			c.Check(err, jc.ErrorIsNil)
			return nil, errors.New("blomp tik")
		},
	})
	getResource := dt.StubGetResource(dt.StubResources{
		"clock":      dt.StubResource{Output: &fakeClock{}},
		"api-caller": dt.StubResource{Output: &fakeAPICaller{}},
		"agent":      dt.StubResource{Output: &mockAgent{}},
	})

	worker, err := manifold.Start(getResource)
	c.Check(err, gc.ErrorMatches, "blomp tik")
	c.Check(worker, gc.IsNil)
}
func (s *StateWorkersSuite) TestNoStartStateWorkers(c *gc.C) {
	manifold := machine.StateWorkersManifold(machine.StateWorkersConfig{})
	worker, err := manifold.Start(dt.StubGetResource(nil))
	c.Check(worker, gc.IsNil)
	c.Check(err, gc.ErrorMatches, "StartStateWorkers not specified")
	c.Check(s.startCalled, jc.IsFalse)
}
Exemple #13
0
func (s *ManifoldSuite) TestStatusWorkerRunsHookOnChanges(c *gc.C) {
	msClient := &stubMeterStatusClient{stub: s.stub, changes: make(chan struct{})}
	s.PatchValue(meterstatus.NewMeterStatusClient,
		func(_ base.APICaller, _ names.UnitTag) msapi.MeterStatusClient {
			return msClient
		})
	s.PatchValue(meterstatus.NewRunner,
		func(_ runner.Context, _ context.Paths) runner.Runner {
			return &stubRunner{stub: s.stub}
		})

	getResource := dt.StubGetResource(s.dummyResources)

	worker, err := s.manifold.Start(getResource)
	c.Assert(err, jc.ErrorIsNil)
	c.Assert(worker, gc.NotNil)

	running := make(chan struct{})
	meterstatus.PatchInit(worker, func() { close(running) })

	select {
	case <-running:
	case <-time.After(coretesting.LongWait):
		c.Fatal("timed out waiting for signal")
	}
	msClient.changes <- struct{}{}
	msClient.code = "RED"

	worker.Kill()
	err = worker.Wait()
	c.Assert(err, jc.ErrorIsNil)
	s.stub.CheckCallNames(c, "MeterStatus", "RunHook", "WatchMeterStatus", "MeterStatus", "RunHook")

}
Exemple #14
0
func (s *ManifoldSuite) TestNewWorkerError(c *gc.C) {
	fakeClock := &fakeClock{}
	fakeFacade := &fakeFacade{}
	fakeAPICaller := &fakeAPICaller{}

	stub := testing.Stub{}
	manifold := charmrevisionmanifold.Manifold(charmrevisionmanifold.ManifoldConfig{
		APICallerName: "api-caller",
		ClockName:     "clock",
		NewFacade: func(apiCaller base.APICaller) (charmrevisionmanifold.Facade, error) {
			stub.AddCall("NewFacade", apiCaller)
			return fakeFacade, nil
		},
		NewWorker: func(config charmrevision.Config) (worker.Worker, error) {
			stub.AddCall("NewWorker", config)
			return nil, errors.New("snrght")
		},
	})

	_, err := manifold.Start(dt.StubGetResource(dt.StubResources{
		"api-caller": dt.StubResource{Output: fakeAPICaller},
		"clock":      dt.StubResource{Output: fakeClock},
	}))
	c.Check(err, gc.ErrorMatches, "cannot create worker: snrght")
	stub.CheckCalls(c, []testing.StubCall{{
		"NewFacade", []interface{}{fakeAPICaller},
	}, {
		"NewWorker", []interface{}{charmrevision.Config{
			RevisionUpdater: fakeFacade,
			Clock:           fakeClock,
		}},
	}})
}
func (s *ServingInfoSetterSuite) checkNotStateServer(c *gc.C, job multiwatcher.MachineJob) {
	a := &mockAgent{}
	apiCaller := basetesting.APICallerFunc(
		func(objType string, version int, id, request string, args, response interface{}) error {
			c.Assert(objType, gc.Equals, "Agent")
			switch request {
			case "GetEntities":
				c.Assert(args.(params.Entities).Entities, gc.HasLen, 1)
				result := response.(*params.AgentGetEntitiesResults)
				result.Entities = []params.AgentGetEntitiesResult{{
					Jobs: []multiwatcher.MachineJob{job},
				}}
			default:
				c.Fatalf("not sure how to handle: %q", request)
			}
			return nil
		},
	)
	w, err := s.manifold.Start(dt.StubGetResource(dt.StubResources{
		"agent":      dt.StubResource{Output: a},
		"api-caller": dt.StubResource{Output: apiCaller},
	}))
	c.Assert(w, gc.IsNil)
	c.Assert(err, gc.Equals, dependency.ErrUninstall)

	// State serving info shouldn't have been set for this job type.
	c.Assert(a.conf.ssiSet, jc.IsFalse)
}
func (s *ServingInfoSetterSuite) TestEntityLookupFailure(c *gc.C) {
	// Set up a fake Agent and APICaller
	a := &mockAgent{}
	apiCaller := basetesting.APICallerFunc(
		func(objType string, version int, id, request string, args, response interface{}) error {
			c.Assert(objType, gc.Equals, "Agent")
			switch request {
			case "GetEntities":
				c.Assert(args.(params.Entities).Entities, gc.HasLen, 1)
				result := response.(*params.AgentGetEntitiesResults)
				result.Entities = []params.AgentGetEntitiesResult{{
					Error: &params.Error{Message: "boom"},
				}}
			default:
				c.Fatalf("not sure how to handle: %q", request)
			}
			return nil
		},
	)
	// Call the manifold's start func with a fake resource getter that
	// returns the fake Agent and APICaller
	w, err := s.manifold.Start(dt.StubGetResource(dt.StubResources{
		"agent":      dt.StubResource{Output: a},
		"api-caller": dt.StubResource{Output: apiCaller},
	}))
	c.Assert(w, gc.IsNil)
	c.Assert(err, gc.ErrorMatches, "boom")
}
Exemple #17
0
func (s *ManifoldSuite) SetUpTest(c *gc.C) {
	spoolDir := c.MkDir()
	s.IsolationSuite.SetUpTest(c)
	s.factory = &stubMetricFactory{
		&testing.Stub{},
		spoolDir,
	}

	testAPIClient := func(apiCaller base.APICaller) metricsadder.MetricsAdderClient {
		return newTestAPIMetricSender()
	}
	s.PatchValue(&sender.NewMetricAdderClient, testAPIClient)

	s.manifold = sender.Manifold(sender.ManifoldConfig{
		AgentName:       "agent",
		APICallerName:   "api-caller",
		MetricSpoolName: "metric-spool",
	})

	dataDir := c.MkDir()
	// create unit agent base dir so that hooks can run.
	err := os.MkdirAll(filepath.Join(dataDir, "agents", "unit-u-0"), 0777)
	c.Assert(err, jc.ErrorIsNil)

	s.getResource = dt.StubGetResource(dt.StubResources{
		"agent":        dt.StubResource{Output: &dummyAgent{dataDir: dataDir}},
		"api-caller":   dt.StubResource{Output: &stubAPICaller{&testing.Stub{}}},
		"metric-spool": dt.StubResource{Output: s.factory},
	})
}
Exemple #18
0
func (s *ManifoldSuite) TestStartMissingAgent(c *gc.C) {
	getResource := dt.StubGetResource(dt.StubResources{
		"agent-name": dt.StubResource{Error: dependency.ErrMissing},
	})
	worker, err := s.manifold.Start(getResource)
	c.Check(worker, gc.IsNil)
	c.Check(err, gc.Equals, dependency.ErrMissing)
}
Exemple #19
0
func (*ManifoldSuite) TestUnlockerMissing(c *gc.C) {
	resources := resourcesMissing("unlocker")
	manifold := discoverspaces.Manifold(namesConfig())

	worker, err := manifold.Start(dt.StubGetResource(resources))
	c.Check(errors.Cause(err), gc.Equals, dependency.ErrMissing)
	c.Check(worker, gc.IsNil)
}
Exemple #20
0
func (*ManifoldSuite) TestClockMissing(c *gc.C) {
	resources := resourcesMissing("clock")
	manifold := undertaker.Manifold(namesConfig())

	worker, err := manifold.Start(dt.StubGetResource(resources))
	c.Check(errors.Cause(err), gc.Equals, dependency.ErrMissing)
	c.Check(worker, gc.IsNil)
}
func (s *ServingInfoSetterSuite) TestStartAgentMissing(c *gc.C) {
	getResource := dt.StubGetResource(dt.StubResources{
		"agent": dt.StubResource{Error: dependency.ErrMissing},
	})
	worker, err := s.manifold.Start(getResource)
	c.Check(worker, gc.IsNil)
	c.Check(err, gc.Equals, dependency.ErrMissing)
}
func (s *PostUpgradeManifoldSuite) TestUpgradeWaiterMissing(c *gc.C) {
	getResource := dt.StubGetResource(dt.StubResources{
		"upgradewaiter-name": dt.StubResource{Error: dependency.ErrMissing},
	})

	worker, err := s.manifold.Start(getResource)
	c.Check(worker, gc.IsNil)
	c.Check(err, gc.Equals, dependency.ErrMissing)
}
Exemple #23
0
func (s *FlagSuite) TestStartFalseFlag(c *gc.C) {
	wrapped := dependency.WithFlag(dependency.Manifold{}, "foo")
	getResource := dt.StubGetResource(dt.StubResources{
		"foo": dt.StubResource{Output: stubFlag(false)},
	})
	worker, err := wrapped.Start(getResource)
	c.Check(worker, gc.IsNil)
	c.Check(errors.Cause(err), gc.Equals, dependency.ErrMissing)
}
Exemple #24
0
// TestCollectWorkerStarts ensures that the manifold correctly sets up the worker.
func (s *ManifoldSuite) TestCollectWorkerStarts(c *gc.C) {
	getResource := dt.StubGetResource(s.dummyResources)
	worker, err := s.manifold.Start(getResource)
	c.Assert(err, jc.ErrorIsNil)
	c.Assert(worker, gc.NotNil)
	worker.Kill()
	err = worker.Wait()
	c.Assert(err, jc.ErrorIsNil)
}
Exemple #25
0
func (s *FlagSuite) TestStartNotFlag(c *gc.C) {
	wrapped := dependency.WithFlag(dependency.Manifold{}, "foo")
	getResource := dt.StubGetResource(dt.StubResources{
		"foo": dt.StubResource{Output: true},
	})
	worker, err := wrapped.Start(getResource)
	c.Check(worker, gc.IsNil)
	c.Check(err, gc.ErrorMatches, `cannot set true into \*dependency.Flag`)
}
func (s *StateWorkersSuite) TestStateMissing(c *gc.C) {
	getResource := dt.StubGetResource(dt.StubResources{
		"state": dt.StubResource{Error: dependency.ErrMissing},
	})
	worker, err := s.manifold.Start(getResource)
	c.Check(worker, gc.IsNil)
	c.Check(err, gc.Equals, dependency.ErrMissing)
	c.Check(s.startCalled, jc.IsFalse)
}
Exemple #27
0
func (s *ManifoldSuite) TestStartSuccess(c *gc.C) {
	getResource := dt.StubGetResource(dt.StubResources{
		"steps-waiter": dt.StubResource{Output: gate.NewLock()},
		"check-waiter": dt.StubResource{Output: gate.NewLock()},
	})
	w, err := s.manifold.Start(getResource)
	c.Assert(err, jc.ErrorIsNil)
	checkStop(c, w)
}
Exemple #28
0
func (s *ManifoldSuite) TestStartNoCheckWaiter(c *gc.C) {
	getResource := dt.StubGetResource(dt.StubResources{
		"steps-waiter": dt.StubResource{Output: gate.NewLock()},
		"check-waiter": dt.StubResource{Error: dependency.ErrMissing},
	})
	w, err := s.manifold.Start(getResource)
	c.Assert(w, gc.IsNil)
	c.Assert(err, gc.Equals, dependency.ErrMissing)
}
Exemple #29
0
func (s *ManifoldSuite) TestStartMissingAPICaller(c *gc.C) {
	getResource := dt.StubGetResource(dt.StubResources{
		"api-caller":   dt.StubResource{Error: dependency.ErrMissing},
		"metric-spool": dt.StubResource{Output: s.factory},
	})
	worker, err := s.manifold.Start(getResource)
	c.Check(worker, gc.IsNil)
	c.Check(err, gc.ErrorMatches, dependency.ErrMissing.Error())
}
Exemple #30
0
// TestStatusWorkerStarts ensures that the manifold correctly sets up the worker.
func (s *PatchedManifoldSuite) TestStatusWorkerStarts(c *gc.C) {
	getResource := dt.StubGetResource(s.dummyResources)
	worker, err := s.manifold.Start(getResource)
	c.Assert(err, jc.ErrorIsNil)
	c.Assert(worker, gc.NotNil)
	worker.Kill()
	err = worker.Wait()
	c.Assert(err, jc.ErrorIsNil)
	s.stub.CheckCallNames(c, "MeterStatus", "RunHook", "WatchMeterStatus")
}