add nick
[laas.git] / src / laas_dashboard / model_test.py
1 ##############################################################################
2 # Copyright (c) 2020 Sawyer Bergeron, Parker Berberian, Sean Smith, and others.
3 #
4 # All rights reserved. This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 # http://www.apache.org/licenses/LICENSE-2.0
8 ##############################################################################
9
10
11 from resource_inventory.models import (
12     ResourceProfile,
13     ResourceQuery,
14     Image,
15     DiskProfile,
16     CpuProfile,
17     RamProfile,
18     InterfaceProfile,
19 )
20
21
22 def rp_has_all_components():
23     """
24     Check that every ResourceProfile has an InterfaceProfile,
25     DiskProfile, CpuProfile, and RamProfile.
26     """
27
28     result = True
29
30     for rp in ResourceProfile.objects.all():
31         ip = InterfaceProfile.objects.filter(host=rp).exists()
32         dp = DiskProfile.objects.filter(host=rp).exists()
33         cp = CpuProfile.objects.filter(host=rp).exists()
34         ram = RamProfile.objects.filter(host=rp).exists()
35
36         if not ip:
37             print("No InterfaceProfiles for host", rp.name)
38             result = False
39
40         if not dp:
41             print("No DiskProfile for host", rp.name)
42             result = False
43
44         if not cp:
45             print("No CpuProfile for host", rp.name)
46             result = False
47
48         if not ram:
49             print("No RamProfile for host", rp.name)
50             result = False
51
52     return result
53
54
55 def ip_for_all_ifaces():
56     """
57     Check that every InterfaceProfile for a Resource has
58     an Interface.
59     """
60
61     result = True
62
63     for res in ResourceQuery.filter():
64         iface_set = res.get_interfaces()
65         iface_profile_set = InterfaceProfile.objects.filter(host=res.profile)
66
67         # find out what profiles we have
68         curr_profiles = [iface.profile for iface in iface_set]
69         missing_profiles = set(iface_profile_set) - set(curr_profiles)
70
71         if missing_profiles:
72             print('No interface for profiles', missing_profiles, 'for host', res.name)
73             result = False
74
75     return result
76
77
78 def rp_has_image():
79     """
80     Make sure every ResourceProfile has an Image.
81     """
82
83     result = True
84
85     rp_set = ResourceProfile.objects.all()
86     image_set = Image.objects.all()
87     image_profiles = set([image.host_type for image in image_set])
88
89     for rp in rp_set:
90         if rp not in image_profiles:
91             print("ResourceProfile", rp.name, "has no image associated with it.")
92             result = False
93     return result
94
95
96 def run_test(test):
97     print('RUNNING TEST', test)
98     result = test()
99     if result:
100         print(test, 'WAS A SUCCESS!')
101     else:
102         print(test, 'FAILED')
103     print('============================================')
104
105
106 def run_tests():
107     tests = [rp_has_all_components, ip_for_all_ifaces, rp_has_image]
108
109     for test in tests:
110         run_test(test)