telegraf_influx_test.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. // Copyright 2019 Yunion
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. package metadata
  6. import (
  7. "testing"
  8. )
  9. func TestRewriteInfluxLineTenant(t *testing.T) {
  10. resolve := func(vmId string) (string, bool) {
  11. if vmId == "vm-1" {
  12. return "tenant-correct", true
  13. }
  14. return "", false
  15. }
  16. line := `agent_cpu,vm_id=vm-1,tenant_id=wrong u=1i`
  17. out, ch, err := rewriteInfluxLineTenant(line, resolve)
  18. if err != nil {
  19. t.Fatal(err)
  20. }
  21. if !ch {
  22. t.Fatal("expected change")
  23. }
  24. want := `agent_cpu,vm_id=vm-1,tenant_id=tenant-correct u=1i`
  25. if out != want {
  26. t.Fatalf("got %q want %q", out, want)
  27. }
  28. }
  29. func TestRewriteInfluxLineTenantAddMissing(t *testing.T) {
  30. resolve := func(vmId string) (string, bool) {
  31. if vmId == "vm-1" {
  32. return "t1", true
  33. }
  34. return "", false
  35. }
  36. line := `agent_mem,vm_id=vm-1 used=1i`
  37. out, ch, err := rewriteInfluxLineTenant(line, resolve)
  38. if err != nil {
  39. t.Fatal(err)
  40. }
  41. if !ch {
  42. t.Fatal("expected change")
  43. }
  44. want := `agent_mem,vm_id=vm-1,tenant_id=t1 used=1i`
  45. if out != want {
  46. t.Fatalf("got %q want %q", out, want)
  47. }
  48. }