Best Python code snippet using autotest_python
test_cpuload.py
Source:test_cpuload.py
1##############################################################################2# Copyright (c) 2015 Ericsson AB and others.3#4# All rights reserved. This program and the accompanying materials5# are made available under the terms of the Apache License, Version 2.06# which accompanies this distribution, and is available at7# http://www.apache.org/licenses/LICENSE-2.08##############################################################################9# Unittest for yardstick.benchmark.scenarios.compute.lmbench.Lmbench10from __future__ import absolute_import11import mock12import unittest13import os14from yardstick.benchmark.scenarios.compute import cpuload15@mock.patch('yardstick.benchmark.scenarios.compute.cpuload.ssh')16class CPULoadTestCase(unittest.TestCase):17 def setUp(self):18 self.ctx = {19 'host': {20 'ip': '172.16.0.137',21 'user': 'cirros',22 'key_filename': "mykey.key"23 }24 }25 self.result = {}26 def test_setup_mpstat_installed(self, mock_ssh):27 options = {28 "interval": 1,29 "count": 130 }31 args = {'options': options}32 l = cpuload.CPULoad(args, self.ctx)33 mock_ssh.SSH.from_node().execute.return_value = (0, '', '')34 l.setup()35 self.assertIsNotNone(l.client)36 self.assertTrue(l.setup_done)37 self.assertTrue(l.has_mpstat)38 def test_setup_mpstat_not_installed(self, mock_ssh):39 options = {40 "interval": 1,41 "count": 142 }43 args = {'options': options}44 l = cpuload.CPULoad(args, self.ctx)45 mock_ssh.SSH.from_node().execute.return_value = (127, '', '')46 l.setup()47 self.assertIsNotNone(l.client)48 self.assertTrue(l.setup_done)49 self.assertFalse(l.has_mpstat)50 def test_execute_command_success(self, mock_ssh):51 options = {52 "interval": 1,53 "count": 154 }55 args = {'options': options}56 l = cpuload.CPULoad(args, self.ctx)57 mock_ssh.SSH.from_node().execute.return_value = (0, '', '')58 l.setup()59 expected_result = 'abcdefg'60 mock_ssh.SSH.from_node().execute.return_value = (0, expected_result, '')61 result = l._execute_command("foo")62 self.assertEqual(result, expected_result)63 def test_execute_command_failed(self, mock_ssh):64 options = {65 "interval": 1,66 "count": 167 }68 args = {'options': options}69 l = cpuload.CPULoad(args, self.ctx)70 mock_ssh.SSH.from_node().execute.return_value = (0, '', '')71 l.setup()72 mock_ssh.SSH.from_node().execute.return_value = (127, '', 'abcdefg')73 self.assertRaises(RuntimeError, l._execute_command,74 "cat /proc/loadavg")75 def test_get_loadavg(self, mock_ssh):76 options = {77 "interval": 1,78 "count": 179 }80 args = {'options': options}81 l = cpuload.CPULoad(args, self.ctx)82 mock_ssh.SSH.from_node().execute.return_value = (0, '', '')83 l.setup()84 mock_ssh.SSH.from_node().execute.return_value = \85 (0, '1.50 1.45 1.51 3/813 14322', '')86 result = l._get_loadavg()87 expected_result = \88 {'loadavg': ['1.50', '1.45', '1.51', '3/813', '14322']}89 self.assertEqual(result, expected_result)90 def test_get_cpu_usage_mpstat(self, mock_ssh):91 options = {92 "interval": 1,93 "count": 194 }95 args = {'options': options}96 l = cpuload.CPULoad(args, self.ctx)97 mock_ssh.SSH.from_node().execute.return_value = (0, '', '')98 l.setup()99 l.interval = 1100 l.count = 1101 mpstat_output = self._read_file("cpuload_sample_output1.txt")102 mock_ssh.SSH.from_node().execute.return_value = (0, mpstat_output, '')103 result = l._get_cpu_usage_mpstat()104 expected_result = \105 {"mpstat_minimum":106 {"cpu": {"%steal": "0.00", "%usr": "0.00", "%gnice": "0.00",107 "%idle": "100.00", "%guest": "0.00",108 "%iowait": "0.00", "%sys": "0.00", "%soft": "0.00",109 "%irq": "0.00", "%nice": "0.00"},110 "cpu0": {"%steal": "0.00", "%usr": "0.00", "%gnice": "0.00",111 "%idle": "100.00", "%guest": "0.00",112 "%iowait": "0.00", "%sys": "0.00", "%soft": "0.00",113 "%irq": "0.00", "%nice": "0.00"}},114 "mpstat_average":115 {"cpu": {"%steal": "0.00", "%usr": "0.00", "%gnice": "0.00",116 "%idle": "100.00", "%guest": "0.00",117 "%iowait": "0.00", "%sys": "0.00", "%soft": "0.00",118 "%irq": "0.00", "%nice": "0.00"},119 "cpu0": {"%steal": "0.00", "%usr": "0.00", "%gnice": "0.00",120 "%idle": "100.00", "%guest": "0.00",121 "%iowait": "0.00", "%sys": "0.00", "%soft": "0.00",122 "%irq": "0.00", "%nice": "0.00"}},123 "mpstat_maximun":124 {"cpu": {"%steal": "0.00", "%usr": "0.00", "%gnice": "0.00",125 "%idle": "100.00", "%guest": "0.00",126 "%iowait": "0.00", "%sys": "0.00", "%soft": "0.00",127 "%irq": "0.00", "%nice": "0.00"},128 "cpu0": {"%steal": "0.00", "%usr": "0.00", "%gnice": "0.00",129 "%idle": "100.00", "%guest": "0.00",130 "%iowait": "0.00", "%sys": "0.00", "%soft": "0.00",131 "%irq": "0.00", "%nice": "0.00"}}}132 self.assertDictEqual(result, expected_result)133 def test_get_cpu_usage(self, mock_ssh):134 options = {135 "interval": 0,136 "count": 1137 }138 args = {'options': options}139 l = cpuload.CPULoad(args, self.ctx)140 mock_ssh.SSH.from_node().execute.return_value = (0, '', '')141 l.setup()142 l.interval = 0143 output = self._read_file("cpuload_sample_output2.txt")144 mock_ssh.SSH.from_node().execute.return_value = (0, output, '')145 result = l._get_cpu_usage()146 expected_result = \147 {'mpstat':148 {'cpu':149 {'%steal': '0.00',150 '%usr': '11.31',151 '%gnice': '0.00',152 '%idle': '81.78',153 '%iowait': '0.18',154 '%guest': '5.50',155 '%sys': '1.19',156 '%soft': '0.01',157 '%irq': '0.00',158 '%nice': '0.03'},159 'cpu0':160 {'%steal': '0.00',161 '%usr': '20.00',162 '%gnice': '0.00',163 '%idle': '71.60',164 '%iowait': '0.33',165 '%guest': '6.61',166 '%sys': '1.37',167 '%soft': '0.06',168 '%irq': '0.00',169 '%nice': '0.03'}}}170 self.assertDictEqual(result, expected_result)171 def test_run_proc_stat(self, mock_ssh):172 options = {173 "interval": 1,174 "count": 1175 }176 args = {'options': options}177 l = cpuload.CPULoad(args, self.ctx)178 mock_ssh.SSH.from_node().execute.return_value = (1, '', '')179 l.setup()180 l.interval = 0181 stat_output = self._read_file("cpuload_sample_output2.txt")182 mock_ssh.SSH.from_node().execute.side_effect = \183 [(0, '1.50 1.45 1.51 3/813 14322', ''), (0, stat_output, '')]184 l.run(self.result)185 expected_result = {186 'loadavg': ['1.50', '1.45', '1.51', '3/813', '14322'],187 'mpstat':188 {'cpu':189 {'%steal': '0.00',190 '%usr': '11.31',191 '%gnice': '0.00',192 '%idle': '81.78',193 '%iowait': '0.18',194 '%guest': '5.50',195 '%sys': '1.19',196 '%soft': '0.01',197 '%irq': '0.00',198 '%nice': '0.03'},199 'cpu0':200 {'%steal': '0.00',201 '%usr': '20.00',202 '%gnice': '0.00',203 '%idle': '71.60',204 '%iowait': '0.33',205 '%guest': '6.61',206 '%sys': '1.37',207 '%soft': '0.06',208 '%irq': '0.00',209 '%nice': '0.03'}}}210 self.assertDictEqual(self.result, expected_result)211 def _read_file(self, filename):212 curr_path = os.path.dirname(os.path.abspath(__file__))213 output = os.path.join(curr_path, filename)214 with open(output) as f:215 sample_output = f.read()...
ui_cpuload.py
Source:ui_cpuload.py
1# -*- coding: utf-8 -*-2# Form implementation generated from reading ui file 'cpuload.ui'3#4# Created by: PyQt5 UI code generator 5.75#6# WARNING! All changes made in this file will be lost!7from PyQt5 import QtCore, QtGui, QtWidgets8class Ui_CpuLoad(object):9 def setupUi(self, CpuLoad):10 CpuLoad.setObjectName("CpuLoad")11 CpuLoad.resize(400, 300)12 self.gridLayout = QtWidgets.QGridLayout(CpuLoad)13 self.gridLayout.setObjectName("gridLayout")14 self.label = QtWidgets.QLabel(CpuLoad)15 self.label.setObjectName("label")16 self.gridLayout.addWidget(self.label, 0, 0, 1, 1)17 self.cpu1 = QtWidgets.QProgressBar(CpuLoad)18 self.cpu1.setStyleSheet("")19 self.cpu1.setProperty("value", 0)20 self.cpu1.setObjectName("cpu1")21 self.gridLayout.addWidget(self.cpu1, 0, 1, 1, 1)22 self.label_2 = QtWidgets.QLabel(CpuLoad)23 self.label_2.setObjectName("label_2")24 self.gridLayout.addWidget(self.label_2, 1, 0, 1, 1)25 self.cpu2 = QtWidgets.QProgressBar(CpuLoad)26 self.cpu2.setStyleSheet("")27 self.cpu2.setProperty("value", 0)28 self.cpu2.setObjectName("cpu2")29 self.gridLayout.addWidget(self.cpu2, 1, 1, 1, 1)30 self.label_3 = QtWidgets.QLabel(CpuLoad)31 self.label_3.setObjectName("label_3")32 self.gridLayout.addWidget(self.label_3, 2, 0, 1, 1)33 self.cpu3 = QtWidgets.QProgressBar(CpuLoad)34 self.cpu3.setStyleSheet("")35 self.cpu3.setProperty("value", 0)36 self.cpu3.setObjectName("cpu3")37 self.gridLayout.addWidget(self.cpu3, 2, 1, 1, 1)38 self.label_4 = QtWidgets.QLabel(CpuLoad)39 self.label_4.setObjectName("label_4")40 self.gridLayout.addWidget(self.label_4, 3, 0, 1, 1)41 self.cpu4 = QtWidgets.QProgressBar(CpuLoad)42 self.cpu4.setStyleSheet("")43 self.cpu4.setProperty("value", 0)44 self.cpu4.setObjectName("cpu4")45 self.gridLayout.addWidget(self.cpu4, 3, 1, 1, 1)46 self.lcdNumber = QtWidgets.QLCDNumber(CpuLoad)47 self.lcdNumber.setStyleSheet("")48 self.lcdNumber.setObjectName("lcdNumber")49 self.gridLayout.addWidget(self.lcdNumber, 4, 1, 1, 1)50 self.label_5 = QtWidgets.QLabel(CpuLoad)51 self.label_5.setObjectName("label_5")52 self.gridLayout.addWidget(self.label_5, 4, 0, 1, 1)53 self.retranslateUi(CpuLoad)54 QtCore.QMetaObject.connectSlotsByName(CpuLoad)55 def retranslateUi(self, CpuLoad):56 _translate = QtCore.QCoreApplication.translate57 CpuLoad.setWindowTitle(_translate("CpuLoad", "CPU Load"))58 self.label.setText(_translate("CpuLoad", "CPU 1"))59 self.label_2.setText(_translate("CpuLoad", "CPU 2"))60 self.label_3.setText(_translate("CpuLoad", "CPU 3"))61 self.label_4.setText(_translate("CpuLoad", "CPU 4"))...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!