Browse Source

Add tests for os_vm_used_cores (#20)

Major Hayden 2 years ago
parent
commit
dc0ceffec2
2 changed files with 41 additions and 7 deletions
  1. 1
    1
      monitorstack/plugins/os_vm_used_cores.py
  2. 40
    6
      tests/test_os_vm.py

+ 1
- 1
monitorstack/plugins/os_vm_used_cores.py View File

@@ -46,8 +46,8 @@ def cli(ctx, config_file):
46 46
     used_collection = collections.Counter()
47 47
     nova_config = utils.read_config(config_file=config_file)['nova']
48 48
     _ost = ost.OpenStack(os_auth_args=nova_config)
49
-    flavors = _ost.get_flavors()
50 49
     try:
50
+        flavors = _ost.get_flavors()
51 51
         variables = output['variables']
52 52
         for used in _ost.get_consumer_usage():
53 53
             flavor = flavors[used['flavor']['id']]

+ 40
- 6
tests/test_os_vm.py View File

@@ -40,6 +40,28 @@ class MockProject(object):
40 40
         self.name = 'testing'
41 41
 
42 42
 
43
+def mock_get_consumer_usage(self):
44
+    """Mocked get_consumer_usage()."""
45
+    return [{
46
+        'name': 'test_name',
47
+        'flavor': {
48
+            'id': 1,
49
+            'name': 'flavor_one',
50
+        }
51
+
52
+    }]
53
+
54
+
55
+def mock_get_flavors(self):
56
+    """Mocked get_flavors()."""
57
+    return {
58
+        1: {
59
+            'name': 'flavor_one',
60
+            'vcpus': 2,
61
+        }
62
+    }
63
+
64
+
43 65
 def mock_get_projects(arg1):
44 66
     """Mocked get_projects()."""
45 67
     projects = MockProject()
@@ -85,13 +107,13 @@ class TestOs(object):
85 107
         assert result['meta'] == {'quotas': 'instances'}
86 108
 
87 109
     def test_os_vm_quota_instance_failure(self):
88
-        """Ensure the run() method works."""
110
+        """Ensure os_vm_quota_cores method works with failure."""
89 111
         result = _runner('os_vm_quota_instance')
90 112
         assert result['measurement_name'] == 'os_vm_quota_instance'
91 113
         assert result['meta'] == {'quotas': 'instances'}
92 114
 
93 115
     def test_os_vm_quota_ram_success(self, monkeypatch):
94
-        """Ensure os_vm_quota_cores method works with success."""
116
+        """Ensure os_vm_quota_ram method works with success."""
95 117
         monkeypatch.setattr(Ost, 'get_projects', mock_get_projects)
96 118
         monkeypatch.setattr(Ost, 'get_compute_limits', mock_get_compute_limits)
97 119
 
@@ -100,14 +122,26 @@ class TestOs(object):
100 122
         assert result['meta'] == {'quotas': 'ram'}
101 123
 
102 124
     def test_os_vm_quota_ram_failure(self):
103
-        """Ensure the run() method works."""
125
+        """Ensure os_vm_quota_ram method works with failure."""
104 126
         result = _runner('os_vm_quota_ram')
105 127
         assert result['measurement_name'] == 'os_vm_quota_ram'
106 128
         assert result['meta'] == {'quotas': 'ram'}
107 129
 
108
-    def test_os_vm_used_cores(self):
109
-        """Ensure the run() method works."""
110
-        pass
130
+    def test_os_vm_used_cores_success(self, monkeypatch):
131
+        """Ensure os_vm_used_cores method works with success."""
132
+        monkeypatch.setattr(Ost, 'get_flavors', mock_get_flavors)
133
+        monkeypatch.setattr(Ost, 'get_consumer_usage', mock_get_consumer_usage)
134
+
135
+        result = _runner('os_vm_used_cores')
136
+        assert result['measurement_name'] == 'os_vm_used_cores'
137
+        assert result['meta']['used'] == 'cores'
138
+        assert result['meta']['flavor_one']
139
+
140
+    def test_os_vm_used_cores_failure(self):
141
+        """Ensure os_vm_used_cores method works with failure."""
142
+        result = _runner('os_vm_used_cores')
143
+        assert result['measurement_name'] == 'os_vm_used_cores'
144
+        assert result['meta'] == {'used': 'cores'}
111 145
 
112 146
     def test_os_vm_used_disk(self):
113 147
         """Ensure the run() method works."""

Loading…
Cancel
Save