func TestConsul_Services_Deleted_From_Task(t *testing.T) { c := newConsulService() task := structs.Task{ Name: "redis", Services: []*structs.Service{ &structs.Service{ Name: "example-cache-redis", Tags: []string{"global"}, PortLabel: "db", }, }, Resources: &structs.Resources{ Networks: []*structs.NetworkResource{ { IP: "10.10.0.1", DynamicPorts: []structs.Port{{"db", 20413}}, }, }, }, } c.Register(&task, "1") if len(c.serviceStates) != 1 { t.Fatalf("Expected tracked services: %v, Actual: %v", 1, len(c.serviceStates)) } task.Services = []*structs.Service{} c.performSync() if len(c.serviceStates) != 0 { t.Fatalf("Expected tracked services: %v, Actual: %v", 0, len(c.serviceStates)) } }
func parseServices(jobName string, taskGroupName string, task *structs.Task, serviceObjs *ast.ObjectList) error { task.Services = make([]*structs.Service, len(serviceObjs.Items)) var defaultServiceName bool for idx, o := range serviceObjs.Items { // Check for invalid keys valid := []string{ "name", "tags", "port", "check", } if err := checkHCLKeys(o.Val, valid); err != nil { return multierror.Prefix(err, fmt.Sprintf("service (%d) ->", idx)) } var service structs.Service var m map[string]interface{} if err := hcl.DecodeObject(&m, o.Val); err != nil { return err } delete(m, "check") if err := mapstructure.WeakDecode(m, &service); err != nil { return err } if defaultServiceName && service.Name == "" { return fmt.Errorf("Only one service block may omit the Name field") } if service.Name == "" { defaultServiceName = true service.Name = fmt.Sprintf("%s-%s-%s", jobName, taskGroupName, task.Name) } // Filter checks var checkList *ast.ObjectList if ot, ok := o.Val.(*ast.ObjectType); ok { checkList = ot.List } else { return fmt.Errorf("service '%s': should be an object", service.Name) } if co := checkList.Filter("check"); len(co.Items) > 0 { if err := parseChecks(&service, co); err != nil { return multierror.Prefix(err, fmt.Sprintf("service: '%s',", service.Name)) } } task.Services[idx] = &service } return nil }