Skip to content

Commit 6861011

Browse files
committed
[apricot] YAML backend support for GetHost/CRU/Endpoint methods + tests
1 parent 4903a01 commit 6861011

File tree

3 files changed

+135
-55
lines changed

3 files changed

+135
-55
lines changed

apricot/local/service.go

Lines changed: 38 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -351,24 +351,20 @@ func (s *Service) RawGetRecursive(path string) (string, error) {
351351
func (s *Service) GetDetectorForHost(hostname string) (string, error) {
352352
s.logMethod()
353353

354-
if cSrc, ok := s.src.(*cfgbackend.ConsulSource); ok {
355-
keys, err := cSrc.GetKeysByPrefix(filepath.Join("o2/hardware", "detectors"))
356-
if err != nil {
357-
return "", err
358-
}
359-
for _, key := range keys {
360-
// key example: o2/hardware/detectors/TST/flps/some-hostname/
361-
splitKey := strings.Split(key, "/")
362-
if len(splitKey) == 7 {
363-
if splitKey[5] == hostname {
364-
return splitKey[3], nil
365-
}
354+
keys, err := s.src.GetKeysByPrefix(filepath.Join("o2/hardware", "detectors"))
355+
if err != nil {
356+
return "", err
357+
}
358+
for _, key := range keys {
359+
// key example: o2/hardware/detectors/TST/flps/some-hostname/
360+
splitKey := strings.Split(key, "/")
361+
if len(splitKey) == 7 {
362+
if splitKey[5] == hostname {
363+
return splitKey[3], nil
366364
}
367365
}
368-
return "", fmt.Errorf("detector not found for host %s", hostname)
369-
} else {
370-
return "", errors.New("runtime KV not supported with file backend")
371366
}
367+
return "", fmt.Errorf("detector not found for host %s", hostname)
372368
}
373369

374370
func (s *Service) GetDetectorsForHosts(hosts []string) ([]string, error) {
@@ -396,25 +392,21 @@ func (s *Service) GetDetectorsForHosts(hosts []string) ([]string, error) {
396392
func (s *Service) GetCRUCardsForHost(hostname string) ([]string, error) {
397393
s.logMethod()
398394

399-
if cSrc, ok := s.src.(*cfgbackend.ConsulSource); ok {
400-
var cards map[string]Card
401-
var serials []string
402-
cfgCards, err := cSrc.Get(filepath.Join("o2/hardware", "flps", hostname, "cards"))
403-
if err != nil {
404-
return nil, err
405-
}
406-
json.Unmarshal([]byte(cfgCards), &cards)
407-
unique := make(map[string]bool)
408-
for _, card := range cards {
409-
if _, value := unique[card.Serial]; !value {
410-
unique[card.Serial] = true
411-
serials = append(serials, card.Serial)
412-
}
395+
var cards map[string]Card
396+
var serials []string
397+
cfgCards, err := s.src.Get(filepath.Join("o2/hardware", "flps", hostname, "cards"))
398+
if err != nil {
399+
return nil, err
400+
}
401+
json.Unmarshal([]byte(cfgCards), &cards)
402+
unique := make(map[string]bool)
403+
for _, card := range cards {
404+
if _, value := unique[card.Serial]; !value {
405+
unique[card.Serial] = true
406+
serials = append(serials, card.Serial)
413407
}
414-
return serials, nil
415-
} else {
416-
return nil, errors.New("runtime KV not supported with file backend")
417408
}
409+
return serials, nil
418410
}
419411

420412
func (s *Service) GetEndpointsForCRUCard(hostname, cardSerial string) ([]string, error) {
@@ -427,26 +419,22 @@ func (s *Service) GetEndpointsForCRUCard(hostname, cardSerial string) ([]string,
427419
WithField("cardSerial", cardSerial).
428420
Debug("getting endpoints")
429421

430-
if cSrc, ok := s.src.(*cfgbackend.ConsulSource); ok {
431-
var cards map[string]Card
432-
var endpoints []string
433-
cfgCards, err := cSrc.Get(filepath.Join("o2/hardware", "flps", hostname, "cards"))
434-
if err != nil {
435-
return nil, err
436-
}
437-
err = json.Unmarshal([]byte(cfgCards), &cards)
438-
if err != nil {
439-
return nil, err
440-
}
441-
for _, card := range cards {
442-
if card.Serial == cardSerial {
443-
endpoints = append(endpoints, card.Endpoint)
444-
}
422+
var cards map[string]Card
423+
var endpoints []string
424+
cfgCards, err := s.src.Get(filepath.Join("o2/hardware", "flps", hostname, "cards"))
425+
if err != nil {
426+
return nil, err
427+
}
428+
err = json.Unmarshal([]byte(cfgCards), &cards)
429+
if err != nil {
430+
return nil, err
431+
}
432+
for _, card := range cards {
433+
if card.Serial == cardSerial {
434+
endpoints = append(endpoints, card.Endpoint)
445435
}
446-
return endpoints, nil
447-
} else {
448-
return nil, errors.New("runtime KV not supported with file backend")
449436
}
437+
return endpoints, nil
450438
}
451439

452440
func (s *Service) GetRuntimeEntry(component string, key string) (string, error) {

apricot/local/service_test.go

Lines changed: 91 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -392,11 +392,98 @@ var _ = Describe("local service", func() {
392392

393393
})
394394

395+
Describe("getting detector for host", func() {
396+
var (
397+
detector string
398+
err error
399+
)
400+
When("retrieving the detector for a host", func() {
401+
It("should return the correct detector", func() {
402+
detector, err = svc.GetDetectorForHost("flp001")
403+
Expect(err).NotTo(HaveOccurred())
404+
Expect(detector).To(Equal("ABC"))
405+
})
406+
})
407+
When("retrieving the detector for a non-existing host", func() {
408+
It("should produce an error", func() {
409+
detector, err = svc.GetDetectorForHost("NOPE")
410+
Expect(err).To(HaveOccurred())
411+
})
412+
})
413+
})
414+
Describe("getting detectors for hosts", func() {
415+
var (
416+
detectors []string
417+
err error
418+
)
419+
When("retrieving the detectors for a list of hosts", func() {
420+
It("should return the correct detectors", func() {
421+
detectors, err = svc.GetDetectorsForHosts([]string{"flp001", "flp002"})
422+
Expect(err).NotTo(HaveOccurred())
423+
Expect(detectors).To(ContainElements("ABC", "DEF"))
424+
})
425+
})
426+
When("retrieving the detectors for a non-existing host", func() {
427+
It("should produce an error", func() {
428+
detectors, err = svc.GetDetectorsForHosts([]string{"NOPE"})
429+
Expect(err).To(HaveOccurred())
430+
})
431+
})
432+
When("retrieving the detectors for a list of hosts with a non-existing host", func() {
433+
It("should produce an error", func() {
434+
detectors, err = svc.GetDetectorsForHosts([]string{"flp001", "NOPE"})
435+
Expect(err).To(HaveOccurred())
436+
})
437+
})
438+
})
439+
Describe("getting CRU cards for a host", func() {
440+
var (
441+
cards []string
442+
err error
443+
)
444+
When("retrieving the CRU cards for a host", func() {
445+
It("should return the correct CRU cards", func() {
446+
cards, err = svc.GetCRUCardsForHost("flp001")
447+
Expect(err).NotTo(HaveOccurred())
448+
Expect(cards).To(ContainElements("0228", "0229"))
449+
})
450+
})
451+
When("retrieving the CRU cards for a non-existing host", func() {
452+
It("should produce an error", func() {
453+
cards, err = svc.GetCRUCardsForHost("NOPE")
454+
Expect(err).To(HaveOccurred())
455+
})
456+
})
457+
})
458+
Describe("getting endpoints for a CRU card", func() {
459+
var (
460+
endpoints []string
461+
err error
462+
)
463+
When("retrieving the endpoints for a CRU card", func() {
464+
It("should return the correct endpoints", func() {
465+
endpoints, err = svc.GetEndpointsForCRUCard("flp001", "0228")
466+
Expect(err).NotTo(HaveOccurred())
467+
Expect(endpoints).To(ContainElements("0", "1"))
468+
})
469+
})
470+
When("retrieving the endpoints for a non-existing host", func() {
471+
It("should produce an error", func() {
472+
endpoints, err = svc.GetEndpointsForCRUCard("NOPE", "0228")
473+
Expect(err).To(HaveOccurred())
474+
})
475+
})
476+
When("retrieving the endpoints for a non-existing CRU card", func() {
477+
// fixme: probably incorrect behaviour, but I don't want to risk breaking something
478+
It("should not return an empty slice", func() {
479+
endpoints, err = svc.GetEndpointsForCRUCard("flp001", "NOPE")
480+
Expect(endpoints).To(BeEmpty())
481+
Expect(err).NotTo(HaveOccurred())
482+
})
483+
})
484+
})
485+
395486
// TODO:
396-
// GetDetectorForHost (currently not supporting yaml backend)
397-
// GetDetectorsForHosts (currently not supporting yaml backend)
398-
// GetCRUCardsForHost (currently not supporting yaml backend)
399-
// GetEndpointsForCRUCard (currently not supporting yaml backend)
400487
// GetRuntimeEntry (currently not supporting yaml backend)
401488
// SetRuntimeEntry (currently not supporting yaml backend)
402489
// GetRuntimeEntries (currently not supporting yaml backend)

apricot/local/service_test.yaml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,12 @@ o2:
4444
flps: {}
4545
flps:
4646
flp001:
47-
cards: "{ \"key\" : \"value\" }"
47+
cards: "{
48+
\"0\": {\"serial\": \"0228\", \"endpoint\": \"0\"},
49+
\"1\": {\"serial\": \"0229\", \"endpoint\": \"0\"},
50+
\"2\": {\"serial\": \"0228\", \"endpoint\": \"1\"},
51+
\"3\": {\"serial\": \"0229\", \"endpoint\": \"1\"}
52+
}"
4853
flp002:
4954
cards: "{ \"key\" : \"value\" }"
5055
flp003:

0 commit comments

Comments
 (0)