Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CSS-2794 Fix test watcher set controller unavailable #874

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions internal/jimm/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package jimm
import (
"context"

"github.com/CanonicalLtd/jimm/internal/db"
"github.com/CanonicalLtd/jimm/internal/dbmodel"
)

Expand All @@ -21,3 +22,12 @@ func (j *JIMM) AddAuditLogEntry(ale *dbmodel.AuditLogEntry) {
func (w *Watcher) PollControllerModels(ctx context.Context, ctl *dbmodel.Controller) {
w.pollControllerModels(ctx, ctl)
}

func NewWatcherWithControllerUnavailableChan(db db.Database, dialer Dialer, pubsub Publisher, testChannel chan error) *Watcher {
return &Watcher{
Pubsub: pubsub,
Database: db,
Dialer: dialer,
controllerUnavailableChan: testChannel,
}
}
20 changes: 18 additions & 2 deletions internal/jimm/watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ type Watcher struct {
// Pubsub is a pub-sub hub used to publish and subscribe
// model summaries.
Pubsub Publisher

controllerUnavailableChan chan error
}

// Watch starts the watcher which connects to all known controllers and
Expand Down Expand Up @@ -134,9 +136,16 @@ func (w *Watcher) dialController(ctx context.Context, ctl *dbmodel.Controller) (
if err != nil {
if !ctl.UnavailableSince.Valid {
ctl.UnavailableSince = db.Now()
if err := w.Database.UpdateController(ctx, ctl); err != nil {
var err error
if err = w.Database.UpdateController(ctx, ctl); err != nil {
zapctx.Error(ctx, "cannot set controller unavailable", zap.Error(err))
}
if w.controllerUnavailableChan != nil {
select {
case w.controllerUnavailableChan <- err:
default:
}
}
}
return nil, errors.E(op, err)
}
Expand Down Expand Up @@ -330,9 +339,16 @@ func (w *Watcher) watchAllModelSummaries(ctx context.Context, ctl *dbmodel.Contr
if err != nil {
if !ctl.UnavailableSince.Valid {
ctl.UnavailableSince = db.Now()
if err := w.Database.UpdateController(ctx, ctl); err != nil {
var err error
if err = w.Database.UpdateController(ctx, ctl); err != nil {
zapctx.Error(ctx, "cannot set controller unavailable", zap.Error(err))
}
if w.controllerUnavailableChan != nil {
select {
case w.controllerUnavailableChan <- err:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I get what u meant now!

default:
}
}
}
return errors.E(op, err)
}
Expand Down
35 changes: 22 additions & 13 deletions internal/jimm/watcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import (
"time"

qt "github.com/frankban/quicktest"
jujuparams "github.com/juju/juju/rpc/params"
"github.com/juju/juju/core/instance"
jujuparams "github.com/juju/juju/rpc/params"
"github.com/juju/names/v4"

"github.com/CanonicalLtd/jimm/internal/db"
Expand Down Expand Up @@ -752,15 +752,18 @@ func TestWatcherSetsControllerUnavailable(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

w := &jimm.Watcher{
Pubsub: &testPublisher{},
Database: db.Database{
controllerUnavailableChannel := make(chan error, 1)
w := jimm.NewWatcherWithControllerUnavailableChan(
db.Database{
DB: jimmtest.MemoryDB(c, nil),
},
Dialer: &jimmtest.Dialer{
&jimmtest.Dialer{
Err: errors.E("test error"),
},
}
&testPublisher{},
controllerUnavailableChannel,
)

env := jimmtest.ParseEnvironment(c, testWatcherEnv)
err := w.Database.Migrate(ctx, false)
c.Assert(err, qt.IsNil)
Expand All @@ -774,14 +777,20 @@ func TestWatcherSetsControllerUnavailable(t *testing.T) {
c.Check(err, qt.ErrorMatches, `context canceled`, qt.Commentf("unexpected error %s (%#v)", err, err))
}()

<-time.After(5 * time.Millisecond)
ctl := dbmodel.Controller{
Name: "controller-1",
// it appears that the jimm code does not treat failing to
// set a controller as unavailable as an error - so
// the test will not treat it as one either.
cerr := <-controllerUnavailableChannel
if cerr != nil {
ctl := dbmodel.Controller{
Name: "controller-1",
}
err = w.Database.GetController(ctx, &ctl)
c.Assert(err, qt.IsNil)
c.Check(ctl.UnavailableSince.Valid, qt.Equals, true)
c.Logf("%v %v", ctl.UnavailableSince.Time, time.Now())
c.Check(ctl.UnavailableSince.Time.After(time.Now().Add(-10*time.Millisecond)), qt.Equals, true)
}
err = w.Database.GetController(ctx, &ctl)
c.Assert(err, qt.IsNil)
c.Check(ctl.UnavailableSince.Valid, qt.Equals, true)
c.Check(ctl.UnavailableSince.Time.After(time.Now().Add(-10*time.Millisecond)), qt.Equals, true)
cancel()
wg.Wait()
}
Expand Down