func initTest() *testCtx { conn := db.New() md, dk := docker.NewMock() ctx := testCtx{supervisor{}, fakeDocker{dk, md}, nil, conn, conn.Trigger(db.MinionTable, db.EtcdTable)} ctx.sv.conn = ctx.conn ctx.sv.dk = ctx.fd.Client ctx.conn.Transact(func(view db.Database) error { m := view.InsertMinion() m.Self = true view.Commit(m) e := view.InsertEtcd() view.Commit(e) return nil }) ctx.sv.runSystemOnce() execRun = func(name string, args ...string) error { ctx.execs = append(ctx.execs, append([]string{name}, args...)) return nil } return &ctx }
func TestRunWorker(t *testing.T) { t.Parallel() md, dk := docker.NewMock() conn := db.New() conn.Transact(func(view db.Database) error { container := view.InsertContainer() container.Image = "Image" container.Minion = "1.2.3.4" view.Commit(container) m := view.InsertMinion() m.Self = true m.PrivateIP = "1.2.3.4" view.Commit(m) return nil }) // Wrong Minion IP, should do nothing. runWorker(conn, dk, "1.2.3.5", *subnet) dkcs, err := dk.List(nil) assert.NoError(t, err) assert.Len(t, dkcs, 0) // Run with a list error, should do nothing. md.ListError = true runWorker(conn, dk, "1.2.3.4", *subnet) md.ListError = false dkcs, err = dk.List(nil) assert.NoError(t, err) assert.Len(t, dkcs, 0) runWorker(conn, dk, "1.2.3.4", *subnet) dkcs, err = dk.List(nil) assert.NoError(t, err) assert.Len(t, dkcs, 1) assert.Equal(t, "Image", dkcs[0].Image) }
func TestSyncWorker(t *testing.T) { t.Parallel() md, dk := docker.NewMock() dbcs := []db.Container{ { ID: 1, Image: "Image1", Command: []string{"Cmd1"}, Env: map[string]string{"Env": "1"}, }, } md.StartError = true changed := runSync(dk, dbcs, nil, *subnet) md.StartError = false assert.Len(t, changed, 0) runSync(dk, dbcs, nil, *subnet) dkcs, err := dk.List(nil) changed, _, _ = syncWorker(dbcs, dkcs, *subnet) assert.NoError(t, err) if changed[0].DockerID != dkcs[0].ID { t.Error(spew.Sprintf("Incorrect DockerID: %v", changed)) } dbcs[0].DockerID = dkcs[0].ID assert.Equal(t, dbcs, changed) dkcsDB := []db.Container{ { ID: 1, DockerID: dkcs[0].ID, Image: dkcs[0].Image, Command: dkcs[0].Args, Env: dkcs[0].Env, }, } assert.Equal(t, dkcsDB, dbcs) dbcs[0].DockerID = "" changed = runSync(dk, dbcs, dkcs, *subnet) newDkcs, err := dk.List(nil) assert.NoError(t, err) assert.Equal(t, dkcs, newDkcs) dbcs[0].DockerID = dkcs[0].ID assert.Equal(t, dbcs, changed) // Atempt a failed remove md.RemoveError = true changed = runSync(dk, nil, dkcs, *subnet) md.RemoveError = false assert.Len(t, changed, 0) newDkcs, err = dk.List(nil) assert.NoError(t, err) assert.Equal(t, dkcs, newDkcs) changed = runSync(dk, nil, dkcs, *subnet) assert.Len(t, changed, 0) dkcs, err = dk.List(nil) assert.NoError(t, err) assert.Len(t, dkcs, 0) }