summaryrefslogtreecommitdiffstats
path: root/tests/carbon_tests/json_to_carbon_test.py
blob: 523ff77ccb23e3e9ebf4c21bb7f96b2a55cb3914 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
##############################################################################
# Copyright (c) 2015 EMC and others.
#
# All rights reserved. This program and the accompanying materials
# are made available under the terms of the Apache License, Version 2.0
# which accompanies this distribution, and is available at
# http://www.apache.org/licenses/LICENSE-2.0
##############################################################################

from storperf.carbon.converter import Converter
import json
import unittest


class JSONToCarbonTest(unittest.TestCase):

    single_json_text_element = """{ "key" : "value" }"""
    single_json_numeric_element = """{ "key" : 123 }"""
    single_json_key_with_spaces = """{ "key with spaces" : "value" }"""
    single_json_value_with_spaces = """{ "key" : "value with spaces" }"""
    json_map_name_with_spaces = \
        """{ "map with spaces" : { "key" : "value" } }"""
    json_list_name_with_spaces = \
        """{ "list with spaces" : [{ "key" : "value" }] }"""

    simple_fio_json = """
{
  "fio version" : "fio-2.2.10",
  "timestamp" : 1444144664,
  "time" : "Tue Oct  6 11:17:44 2015",
  "jobs" : [
    {
      "jobname" : "random-read",
      "groupid" : 0,
      "error" : 0,
      "eta" : 0,
      "elapsed" : 26,
      "read" : {
        "io_bytes" : 7116,
        "bw" : 275,
        "iops" : 68.99,
        "runtime" : 25788,
        "total_ios" : 1779,
        "short_ios" : 0,
        "drop_ios" : 0,
        "slat" : {
          "min" : 0,
          "max" : 0,
          "mean" : 0.00,
          "stddev" : 0.00
        }
      }
    }]
}
"""

    def setUp(self):
        pass

    def test_to_string(self):
        testconv = Converter()
        json_object = json.loads(self.simple_fio_json)
        result = testconv.convert_json_to_flat(json_object, "host.run-name")
        self.assertEqual("7116", result[
                         "host.run-name.jobs.1.read.io_bytes"],
                         result["host.run-name.jobs.1.read.io_bytes"])

    def test_single_text_element_no_prefix(self):
        testconv = Converter()
        result = testconv.convert_json_to_flat(
            json.loads(self.single_json_text_element))

        self.assertEqual("value", result["key"], result["key"])

    def test_single_numeric_element_no_prefix(self):
        testconv = Converter()
        result = testconv.convert_json_to_flat(
            json.loads(self.single_json_numeric_element))

        self.assertEqual("123", result["key"], result["key"])

    def test_single_text_key_space_element_no_prefix(self):
        testconv = Converter()
        result = testconv.convert_json_to_flat(
            json.loads(self.single_json_key_with_spaces))

        self.assertEqual(
            "value", result["key_with_spaces"], result["key_with_spaces"])

    def test_single_text_value_space_element_no_prefix(self):
        testconv = Converter()
        result = testconv.convert_json_to_flat(
            json.loads(self.single_json_value_with_spaces))

        self.assertEqual("value_with_spaces", result["key"], result["key"])

    def test_map_name_with_space_no_prefix(self):
        testconv = Converter()
        result = testconv.convert_json_to_flat(
            json.loads(self.json_map_name_with_spaces))

        self.assertEqual(
            "value", result["map_with_spaces.key"],
            result["map_with_spaces.key"])

    def test_list_name_with_space_no_prefix(self):
        testconv = Converter()
        result = testconv.convert_json_to_flat(
            json.loads(self.json_list_name_with_spaces))

        self.assertEqual(
            "value", result["list_with_spaces.1.key"],
            result["list_with_spaces.1.key"])

if __name__ == '__main__':
    unittest.main()