test_openmetrics.cpp 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. #include <unity.h>
  2. #include <openmetrics.h>
  3. void test_createMetric()
  4. {
  5. // simple happy path
  6. const char *expected = "# HELP metric_name short description\n# TYPE metric_name gauge\nmetric_name 123.456\n";
  7. std::string result = createMetric("metric_name", "short description", "gauge", "123.456");
  8. TEST_ASSERT_EQUAL_STRING(expected, result.c_str());
  9. }
  10. /**
  11. * test the replaceString function as it's a dependency to sanitize sequence names
  12. */
  13. void test_replaceString()
  14. {
  15. std::string sample = "hello\\world\\";
  16. replaceAll(sample, "\\", "");
  17. TEST_ASSERT_EQUAL_STRING("helloworld", sample.c_str());
  18. sample = "hello\"world\"";
  19. replaceAll(sample, "\"", "");
  20. TEST_ASSERT_EQUAL_STRING("helloworld", sample.c_str());
  21. sample = "hello\nworld\n";
  22. replaceAll(sample, "\n", "");
  23. TEST_ASSERT_EQUAL_STRING("helloworld", sample.c_str());
  24. sample = "\\\\\\\\\\\\\\\\\\hello\\world\\\\\\\\\\\\\\\\\\\\";
  25. replaceAll(sample, "\\", "");
  26. TEST_ASSERT_EQUAL_STRING("helloworld", sample.c_str());
  27. }
  28. void test_createSequenceMetrics()
  29. {
  30. std::vector<NumberPost *> NUMBERS;
  31. NumberPost *number_1 = new NumberPost;
  32. number_1->name = "main";
  33. number_1->ReturnValue = "123.456";
  34. NUMBERS.push_back(number_1);
  35. const std::string metricNamePrefix = "ai_on_the_edge_device";
  36. const std::string metricName = metricNamePrefix + "_flow_value";
  37. std::string expected1 = "# HELP " + metricName + " current value of meter readout\n# TYPE " + metricName + " gauge\n" +
  38. metricName + "{sequence=\"" + number_1->name + "\"} " + number_1->ReturnValue + "\n";
  39. TEST_ASSERT_EQUAL_STRING(expected1.c_str(), createSequenceMetrics(metricNamePrefix, NUMBERS).c_str());
  40. NumberPost *number_2 = new NumberPost;
  41. number_2->name = "secondary";
  42. number_2->ReturnValue = "1.0";
  43. NUMBERS.push_back(number_2);
  44. std::string expected2 = "# HELP " + metricName + " current value of meter readout\n# TYPE " + metricName + " gauge\n" +
  45. metricName + "{sequence=\"" + number_1->name + "\"} " + number_1->ReturnValue + "\n" +
  46. metricName + "{sequence=\"" + number_2->name + "\"} " + number_2->ReturnValue + "\n";
  47. TEST_ASSERT_EQUAL_STRING(expected2.c_str(), createSequenceMetrics(metricNamePrefix, NUMBERS).c_str());
  48. }
  49. void test_openmetrics()
  50. {
  51. test_createMetric();
  52. test_replaceString();
  53. test_createSequenceMetrics();
  54. }