123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565 |
- package dao
- import (
- "testing"
- "time"
- "go-common/app/admin/ep/merlin/model"
- "go-common/library/log"
- . "github.com/smartystreets/goconvey/convey"
- )
- var tMachine *model.Machine
- func machineFunc(f func()) func() {
- return func() {
- beforeAllMT()
- defer afterAllMT()
- f()
- }
- }
- func beforeAllMT() {
- var err error
- tMachine = &model.Machine{
- Name: "test-machine-1",
- PodName: "test-machine-0-0",
- Status: 100,
- Username: "seanan",
- BusinessUnit: "test",
- Project: "ep",
- App: "merlin",
- ClusterID: 3,
- NetworkID: 5,
- Comment: "Unit test for the dao of merlin.",
- DelayStatus: 0,
- }
- if err = d.db.Create(tMachine).Error; err != nil {
- log.Error("Failed to init machine(%v) for test, err(%v)", tMachine, err)
- return
- }
- if err = d.db.Create(&model.MachineNode{MachineID: tMachine.ID, BusinessUnit: tMachine.BusinessUnit, Project: tMachine.Project, App: tMachine.App, TreeID: 1234}).Error; err != nil {
- log.Error("Failed to init machine node for test, err(%v)", tMachine, err)
- return
- }
- }
- func afterAllMT() {
- var err error
- if err = d.db.Where("name = ?", tMachine.Name).Delete(&model.Machine{}).Error; err != nil {
- log.Error("Failed to delete machine(%v) for test, err(%v)", tMachine, err)
- return
- }
- if err = d.db.Where("machine_id = ?", tMachine.ID).Delete(&model.MachineNode{}).Error; err != nil {
- log.Error("Failed to delete machine node for test, err(%v)", err)
- return
- }
- }
- func TestFindExpiredMachineByDay(t *testing.T) {
- Convey("The actualMachines must contain tMachine when deadline day is 0", t, machineFunc(func() {
- var err error
- if err = d.db.Model(&model.Machine{}).Where("id = ?", tMachine.ID).Update("end_time", time.Now()).Error; err != nil {
- log.Error("Cannot update the end_time of tMachine")
- return
- }
- actualMachines, err := d.FindExpiredMachineByDay(0)
- So(err, ShouldBeNil)
- for _, m := range actualMachines {
- if m.Name == tMachine.Name {
- return
- }
- }
- So(true, ShouldBeFalse)
- }))
- Convey("The actualMachines must not contain tMachine when deadline day is not 0", t, machineFunc(func() {
- var err error
- if err = d.db.Model(&model.Machine{}).Where("id = ?", tMachine.ID).Update("end_time", time.Now()).Error; err != nil {
- log.Error("Cannot update the end_time of tMachine")
- return
- }
- actualMachines, err := d.FindExpiredMachineByDay(1)
- So(err, ShouldBeNil)
- for _, m := range actualMachines {
- if m.Name == tMachine.Name {
- So(true, ShouldBeFalse)
- return
- }
- }
- }))
- }
- func TestFindExpiredMachine(t *testing.T) {
- Convey("The actualMachines must contain tMachine", t, machineFunc(func() {
- var err error
- if err = d.db.Model(&model.Machine{}).Where("id = ?", tMachine.ID).Update("end_time", time.Now().AddDate(0, 0, 1)).Error; err != nil {
- log.Error("Cannot update the end_time of tMachine")
- return
- }
- actualMachines, err := d.FindExpiredMachine()
- So(err, ShouldBeNil)
- for _, m := range actualMachines {
- if m.Name == tMachine.Name {
- return
- }
- }
- So(true, ShouldBeFalse)
- }))
- Convey("The actualMachines must not contain tMachine", t, machineFunc(func() {
- actualMachines, err := d.FindExpiredMachine()
- So(err, ShouldBeNil)
- for _, m := range actualMachines {
- if m.Name == tMachine.Name {
- So(true, ShouldBeFalse)
- return
- }
- }
- }))
- }
- func TestQueryMachine(t *testing.T) {
- Convey("The actualMachines must be tMachine when id is the id of tMachine", t, machineFunc(func() {
- actualMachines, err := d.QueryMachine(tMachine.ID)
- So(err, ShouldBeNil)
- So(actualMachines.ID, ShouldEqual, tMachine.ID)
- }))
- Convey("The actualMachines must not be tMachine when id is not the id of tMachine", t, machineFunc(func() {
- _, err := d.QueryMachine(0)
- So(err, ShouldNotBeNil)
- }))
- }
- // TestDelMachine this test only need positive test because negative case is filtered by service
- func TestDelMachine(t *testing.T) {
- Convey("The tMachine must be delete", t, machineFunc(func() {
- var (
- actual = &model.Machine{}
- updateBy = "seanan"
- )
- err := d.DelMachine(tMachine.ID, updateBy)
- So(err, ShouldBeNil)
- d.db.Where("id = ?", tMachine.ID).Find(actual)
- So(actual.Status, ShouldEqual, model.RemovedMachineInMerlin)
- So(actual.UpdateBy, ShouldEqual, updateBy)
- }))
- }
- // TestHasMachine HasMachine is fuzzy matching name which is end with -number
- func TestHasMachine(t *testing.T) {
- Convey("return true when machine existed", t, machineFunc(func() {
- b, err := d.HasMachine("test-machine")
- So(err, ShouldBeNil)
- So(b, ShouldBeTrue)
- }))
- Convey("return false when machine does not exist", t, machineFunc(func() {
- b, err := d.HasMachine("no-test-machine")
- So(err, ShouldBeNil)
- So(b, ShouldBeFalse)
- }))
- }
- func TestUpdateMachineStatus(t *testing.T) {
- Convey("The machine status must be boot", t, machineFunc(func() {
- var (
- err error
- actualMachine = &model.Machine{}
- )
- if err = d.db.Model(&model.Machine{}).Where("id = ?", tMachine.ID).Update("status", model.CreatingMachineInMerlin).Error; err != nil {
- return
- }
- err = d.UpdateMachineStatus(tMachine.ID, model.BootMachineInMerlin)
- So(err, ShouldBeNil)
- if err = d.db.Where("id = ?", tMachine.ID).First(actualMachine).Error; err != nil {
- log.Error("find machine err(%v)", err)
- return
- }
- So(actualMachine.Status, ShouldEqual, model.BootMachineInMerlin)
- }))
- }
- func TestQueryMachines(t *testing.T) {
- Convey("Find the boot machines by names", t, machineFunc(func() {
- var (
- err error
- qmr = &model.QueryMachineRequest{
- Pagination: model.Pagination{
- PageSize: 5,
- PageNum: 1,
- },
- }
- names = []string{"test-machine-0-0"}
- actualMachines []*model.Machine
- )
- _, actualMachines, err = d.QueryMachines(names, qmr)
- So(err, ShouldBeNil)
- for _, m := range actualMachines {
- if m.Name == tMachine.Name {
- return
- }
- }
- So(true, ShouldBeFalse)
- }))
- Convey("Find the creating machines by requester", t, machineFunc(func() {
- var (
- err error
- qmr = &model.QueryMachineRequest{
- Pagination: model.Pagination{
- PageSize: 5,
- PageNum: 1,
- },
- Requester: "seanan",
- }
- actualMachines []*model.Machine
- )
- if err = d.db.Model(&model.Machine{}).Where("id = ?", tMachine.ID).Update("status", model.CreatingMachineInMerlin).Error; err != nil {
- return
- }
- _, actualMachines, err = d.QueryMachines(nil, qmr)
- So(err, ShouldBeNil)
- for _, m := range actualMachines {
- if m.Name == tMachine.Name {
- return
- }
- }
- So(true, ShouldBeFalse)
- }))
- Convey("Cannot find the creating machines by other requester", t, machineFunc(func() {
- var (
- err error
- qmr = &model.QueryMachineRequest{
- Pagination: model.Pagination{
- PageSize: 5,
- PageNum: 1,
- },
- Requester: "other",
- }
- actualMachines []*model.Machine
- )
- if err = d.db.Model(&model.Machine{}).Where("id = ?", tMachine.ID).Update("status", model.CreatingMachineInMerlin).Error; err != nil {
- return
- }
- _, actualMachines, err = d.QueryMachines(nil, qmr)
- So(err, ShouldBeNil)
- for _, m := range actualMachines {
- if m.Name == tMachine.Name {
- So(true, ShouldBeFalse)
- return
- }
- }
- }))
- Convey("Find the machines filter by username", t, machineFunc(func() {
- var (
- err error
- qmr = &model.QueryMachineRequest{
- Pagination: model.Pagination{
- PageSize: 5,
- PageNum: 1,
- },
- Username: "sean",
- }
- names = []string{"test-machine-0-0"}
- actualMachines []*model.Machine
- )
- _, actualMachines, err = d.QueryMachines(names, qmr)
- So(err, ShouldBeNil)
- for _, m := range actualMachines {
- if m.Name == tMachine.Name {
- return
- }
- }
- So(true, ShouldBeFalse)
- }))
- Convey("Find the machines filter by machine name", t, machineFunc(func() {
- var (
- err error
- qmr = &model.QueryMachineRequest{
- Pagination: model.Pagination{
- PageSize: 5,
- PageNum: 1,
- },
- MachineName: "test-machine",
- }
- names = []string{"test-machine-0-0"}
- actualMachines []*model.Machine
- )
- _, actualMachines, err = d.QueryMachines(names, qmr)
- So(err, ShouldBeNil)
- for _, m := range actualMachines {
- if m.Name == tMachine.Name {
- return
- }
- }
- So(true, ShouldBeFalse)
- }))
- Convey("Find the machines filter by BusinessUnit", t, machineFunc(func() {
- var (
- err error
- qmr = &model.QueryMachineRequest{
- Pagination: model.Pagination{
- PageSize: 5,
- PageNum: 1,
- },
- TreeNode: model.TreeNode{
- BusinessUnit: "test",
- },
- }
- names = []string{"test-machine-0-0"}
- actualMachines []*model.Machine
- )
- _, actualMachines, err = d.QueryMachines(names, qmr)
- So(err, ShouldBeNil)
- for _, m := range actualMachines {
- if m.Name == tMachine.Name {
- return
- }
- }
- So(true, ShouldBeFalse)
- }))
- Convey("Find the machines filter by BusinessUnit and Project", t, machineFunc(func() {
- var (
- err error
- qmr = &model.QueryMachineRequest{
- Pagination: model.Pagination{
- PageSize: 5,
- PageNum: 1,
- },
- TreeNode: model.TreeNode{
- BusinessUnit: "test",
- Project: "ep",
- },
- }
- names = []string{"test-machine-0-0"}
- actualMachines []*model.Machine
- )
- _, actualMachines, err = d.QueryMachines(names, qmr)
- So(err, ShouldBeNil)
- for _, m := range actualMachines {
- if m.Name == tMachine.Name {
- return
- }
- }
- So(true, ShouldBeFalse)
- }))
- Convey("Find the machines filter by BusinessUnit, Project and App", t, machineFunc(func() {
- var (
- err error
- qmr = &model.QueryMachineRequest{
- Pagination: model.Pagination{
- PageSize: 5,
- PageNum: 1,
- },
- TreeNode: model.TreeNode{
- BusinessUnit: "test",
- Project: "ep",
- App: "merlin",
- },
- }
- names = []string{"test-machine-0-0"}
- actualMachines []*model.Machine
- )
- _, actualMachines, err = d.QueryMachines(names, qmr)
- So(err, ShouldBeNil)
- for _, m := range actualMachines {
- if m.Name == tMachine.Name {
- return
- }
- }
- So(true, ShouldBeFalse)
- }))
- }
- func TestUpdateMachineEndTime(t *testing.T) {
- Convey("The EndTime and DelayStatus should be updated", t, machineFunc(func() {
- var (
- actualMachine = &model.Machine{}
- expectTime = time.Now().AddDate(0, 1, 0)
- expectDelayStatus = 1
- err error
- )
- err = d.UpdateMachineEndTime(tMachine.ID, expectDelayStatus, expectTime)
- So(err, ShouldBeNil)
- if err = d.db.Where("id = ?", tMachine.ID).First(actualMachine).Error; err != nil {
- log.Error("find machine err(%v)", err)
- return
- }
- So(actualMachine.EndTime.Format("2006-01-02 15:04:05"), ShouldEqual, expectTime.Format("2006-01-02 15:04:05"))
- So(actualMachine.DelayStatus, ShouldEqual, expectDelayStatus)
- }))
- }
- func TestUpdateMachineDelayStatus(t *testing.T) {
- Convey("The DelayStatus should be updated", t, machineFunc(func() {
- var (
- actualMachine = &model.Machine{}
- expectDelayStatus = 1
- err error
- )
- err = d.UpdateMachineDelayStatus(tMachine.ID, expectDelayStatus)
- So(err, ShouldBeNil)
- if err = d.db.Where("id = ?", tMachine.ID).First(actualMachine).Error; err != nil {
- log.Error("find machine err(%v)", err)
- return
- }
- So(actualMachine.DelayStatus, ShouldEqual, expectDelayStatus)
- }))
- }
- func TestQueryPathAndPodNamesMapping(t *testing.T) {
- Convey("Find path and podNames mapping when the machine exceeds 20 minutes", t, machineFunc(func() {
- var (
- pathAndPodNames map[string][]string
- err error
- )
- if err = d.db.Model(&model.Machine{}).Where("id = ?", tMachine.ID).Update("status", model.CreatingMachineInMerlin).Update("ctime", time.Now().AddDate(0, 0, -1)).Error; err != nil {
- return
- }
- pathAndPodNames, err = d.QueryPathAndPodNamesMapping()
- So(err, ShouldBeNil)
- expectPath := tMachine.ToTreeNode().TreePath()
- for _, pn := range pathAndPodNames[expectPath] {
- if pn == tMachine.PodName {
- return
- }
- }
- So(true, ShouldBeFalse)
- }))
- Convey("cannot find path and podNames mapping when the machine does not exceed 20 minutes", t, machineFunc(func() {
- var (
- pathAndPodNames map[string][]string
- err error
- )
- if err = d.db.Model(&model.Machine{}).Where("id = ?", tMachine.ID).Update("status", model.CreatingMachineInMerlin).Error; err != nil {
- return
- }
- pathAndPodNames, err = d.QueryPathAndPodNamesMapping()
- So(err, ShouldBeNil)
- expectPath := tMachine.ToTreeNode().TreePath()
- for _, pn := range pathAndPodNames[expectPath] {
- if pn == tMachine.PodName {
- So(true, ShouldBeFalse)
- return
- }
- }
- }))
- }
- func TestUpdateMachineStatusByPodNames(t *testing.T) {
- Convey("cannot find path and podNames mapping when the machine does not exceed 20 minutes", t, machineFunc(func() {
- var (
- err error
- actualMachine *model.Machine
- expectStatus = model.BootMachineInMerlin
- )
- if err = d.db.Model(&model.Machine{}).Where("id = ?", tMachine.ID).Update("status", model.CreatingMachineInMerlin).Error; err != nil {
- return
- }
- err = d.UpdateMachineStatusByPodNames([]string{tMachine.PodName}, expectStatus)
- So(err, ShouldBeNil)
- if err = d.db.Where("id = ?", tMachine.ID).First(actualMachine).Error; err != nil {
- log.Error("find machine err(%v)", err)
- return
- }
- So(actualMachine.Status, ShouldEqual, expectStatus)
- }))
- }
- func TestQueryMachinesByPodNames(t *testing.T) {
- Convey("cannot find path and podNames mapping when the machine does not exceed 20 minutes", t, machineFunc(func() {
- var (
- err error
- actualMachines []*model.Machine
- )
- actualMachines, err = d.QueryMachinesByPodNames([]string{tMachine.PodName})
- So(err, ShouldBeNil)
- for _, m := range actualMachines {
- if m.ID == tMachine.ID {
- return
- }
- }
- So(true, ShouldBeFalse)
- }))
- }
- func TestInsertMachines(t *testing.T) {
- Convey("Insert 2 machines", t, machineFunc(func() {
- var (
- testMachine2 = "test-machine-2"
- testMachine3 = "test-machine-3"
- ins = []*model.CreateInstance{
- {
- Instance: model.Instance{
- InstanceName: testMachine2,
- },
- InstanceCreateStatus: model.CreatingMachineInPass,
- }, {
- Instance: model.Instance{
- InstanceName: testMachine3,
- },
- InstanceCreateStatus: model.CreateFailedMachineInPaas,
- },
- }
- u = "seanan"
- gmr = &model.GenMachinesRequest{
- Env: model.Env{
- ClusterID: 1,
- NetworkID: 1,
- },
- PaasMachine: model.PaasMachine{
- Name: "test-machine",
- Image: "test-Image",
- CPURequest: 1000,
- MemoryRequest: 1024,
- DiskRequest: 20,
- VolumnMount: "/data",
- },
- Nodes: []*model.Node{
- {
- BusinessUnit: "test",
- Project: "ep",
- App: "merlin",
- TreeID: 1234,
- },
- },
- Comment: "test",
- Amount: 1,
- }
- err error
- actualMachine []*model.Machine
- flag = 0
- )
- err = d.InsertMachines(u, gmr, ins)
- defer func() {
- if err = d.db.Where("name IN (?)", []string{testMachine2, testMachine3}).Delete(&model.Machine{}).Error; err != nil {
- log.Error("Failed to delete machine(%v) for test, err(%v)", tMachine, err)
- return
- }
- }()
- So(err, ShouldBeNil)
- if err = d.db.Where("name IN (?)", []string{testMachine2, testMachine3}).Find(&actualMachine).Error; err != nil {
- log.Error("Find machiens err(%v)", err)
- return
- }
- for _, m := range actualMachine {
- if m.Name == testMachine2 && m.Status == model.CreatingMachineInMerlin {
- flag++
- }
- if m.Name == testMachine3 && m.Status == model.ImmediatelyFailedMachineInMerlin {
- flag += 2
- }
- }
- So(flag, ShouldEqual, 3)
- }))
- }
|