diff options
author | Joshua Watt <JPEWhacker@gmail.com> | 2023-11-03 08:26:38 -0600 |
---|---|---|
committer | Richard Purdie <richard.purdie@linuxfoundation.org> | 2023-11-09 17:33:03 +0000 |
commit | 1699870a0cf7b5ec3aadd9155cdca5dcf53ae273 (patch) | |
tree | 2a1f9b6e9c09e6192f2f6720b2e9e77cd8d386b0 | |
parent | 407afec92a959ded5339bbb217f185bc0b480de8 (diff) | |
download | poky-1699870a0cf7b5ec3aadd9155cdca5dcf53ae273.tar.gz |
bitbake: hashserv: tests: Allow authentication for external server tests
If BB_TEST_HASHSERV_USERNAME and BB_TEST_HASHSERV_PASSWORD are provided
for a server admin user, the authentication tests for the external
hashserver will run. In addition, any users that get created will now be
deleted when the test finishes.
(Bitbake rev: 0e945d3dec02479df1157f48fd44223c2bfb34a3)
Signed-off-by: Joshua Watt <JPEWhacker@gmail.com>
Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
-rw-r--r-- | bitbake/lib/hashserv/tests.py | 109 |
1 files changed, 74 insertions, 35 deletions
diff --git a/bitbake/lib/hashserv/tests.py b/bitbake/lib/hashserv/tests.py index 2d78f9e976..5d209ffb87 100644 --- a/bitbake/lib/hashserv/tests.py +++ b/bitbake/lib/hashserv/tests.py | |||
@@ -84,17 +84,13 @@ class HashEquivalenceTestSetup(object): | |||
84 | return self.server.address | 84 | return self.server.address |
85 | 85 | ||
86 | def start_auth_server(self): | 86 | def start_auth_server(self): |
87 | self.auth_server = self.start_server(self.server.dbpath, anon_perms=[], admin_username="admin", admin_password="password") | 87 | auth_server = self.start_server(self.server.dbpath, anon_perms=[], admin_username="admin", admin_password="password") |
88 | self.admin_client = self.start_client(self.auth_server.address, username="admin", password="password") | 88 | self.auth_server_address = auth_server.address |
89 | self.admin_client = self.start_client(auth_server.address, username="admin", password="password") | ||
89 | return self.admin_client | 90 | return self.admin_client |
90 | 91 | ||
91 | def auth_client(self, user): | 92 | def auth_client(self, user): |
92 | return self.start_client(self.auth_server.address, user["username"], user["token"]) | 93 | return self.start_client(self.auth_server_address, user["username"], user["token"]) |
93 | |||
94 | def auth_perms(self, *permissions): | ||
95 | self.client_index += 1 | ||
96 | user = self.admin_client.new_user(f"user-{self.client_index}", permissions) | ||
97 | return self.auth_client(user) | ||
98 | 94 | ||
99 | def setUp(self): | 95 | def setUp(self): |
100 | if sys.version_info < (3, 5, 0): | 96 | if sys.version_info < (3, 5, 0): |
@@ -120,11 +116,11 @@ class HashEquivalenceTestSetup(object): | |||
120 | }) | 116 | }) |
121 | 117 | ||
122 | def assertUserCanAuth(self, user): | 118 | def assertUserCanAuth(self, user): |
123 | with self.start_client(self.auth_server.address) as client: | 119 | with self.start_client(self.auth_server_address) as client: |
124 | client.auth(user["username"], user["token"]) | 120 | client.auth(user["username"], user["token"]) |
125 | 121 | ||
126 | def assertUserCannotAuth(self, user): | 122 | def assertUserCannotAuth(self, user): |
127 | with self.start_client(self.auth_server.address) as client, self.assertRaises(InvokeError): | 123 | with self.start_client(self.auth_server_address) as client, self.assertRaises(InvokeError): |
128 | client.auth(user["username"], user["token"]) | 124 | client.auth(user["username"], user["token"]) |
129 | 125 | ||
130 | def create_test_hash(self, client): | 126 | def create_test_hash(self, client): |
@@ -157,6 +153,26 @@ class HashEquivalenceTestSetup(object): | |||
157 | 153 | ||
158 | 154 | ||
159 | class HashEquivalenceCommonTests(object): | 155 | class HashEquivalenceCommonTests(object): |
156 | def auth_perms(self, *permissions): | ||
157 | self.client_index += 1 | ||
158 | user = self.create_user(f"user-{self.client_index}", permissions) | ||
159 | return self.auth_client(user) | ||
160 | |||
161 | def create_user(self, username, permissions, *, client=None): | ||
162 | def remove_user(username): | ||
163 | try: | ||
164 | self.admin_client.delete_user(username) | ||
165 | except bb.asyncrpc.InvokeError: | ||
166 | pass | ||
167 | |||
168 | if client is None: | ||
169 | client = self.admin_client | ||
170 | |||
171 | user = client.new_user(username, permissions) | ||
172 | self.addCleanup(remove_user, username) | ||
173 | |||
174 | return user | ||
175 | |||
160 | def test_create_hash(self): | 176 | def test_create_hash(self): |
161 | return self.create_test_hash(self.client) | 177 | return self.create_test_hash(self.client) |
162 | 178 | ||
@@ -571,14 +587,14 @@ class HashEquivalenceCommonTests(object): | |||
571 | def test_auth_no_token_refresh_from_anon_user(self): | 587 | def test_auth_no_token_refresh_from_anon_user(self): |
572 | self.start_auth_server() | 588 | self.start_auth_server() |
573 | 589 | ||
574 | with self.start_client(self.auth_server.address) as client, self.assertRaises(InvokeError): | 590 | with self.start_client(self.auth_server_address) as client, self.assertRaises(InvokeError): |
575 | client.refresh_token() | 591 | client.refresh_token() |
576 | 592 | ||
577 | def test_auth_self_token_refresh(self): | 593 | def test_auth_self_token_refresh(self): |
578 | admin_client = self.start_auth_server() | 594 | admin_client = self.start_auth_server() |
579 | 595 | ||
580 | # Create a new user with no permissions | 596 | # Create a new user with no permissions |
581 | user = admin_client.new_user("test-user", []) | 597 | user = self.create_user("test-user", []) |
582 | 598 | ||
583 | with self.auth_client(user) as client: | 599 | with self.auth_client(user) as client: |
584 | new_user = client.refresh_token() | 600 | new_user = client.refresh_token() |
@@ -601,7 +617,7 @@ class HashEquivalenceCommonTests(object): | |||
601 | def test_auth_token_refresh(self): | 617 | def test_auth_token_refresh(self): |
602 | admin_client = self.start_auth_server() | 618 | admin_client = self.start_auth_server() |
603 | 619 | ||
604 | user = admin_client.new_user("test-user", []) | 620 | user = self.create_user("test-user", []) |
605 | 621 | ||
606 | with self.auth_perms() as client, self.assertRaises(InvokeError): | 622 | with self.auth_perms() as client, self.assertRaises(InvokeError): |
607 | client.refresh_token(user["username"]) | 623 | client.refresh_token(user["username"]) |
@@ -617,7 +633,7 @@ class HashEquivalenceCommonTests(object): | |||
617 | def test_auth_self_get_user(self): | 633 | def test_auth_self_get_user(self): |
618 | admin_client = self.start_auth_server() | 634 | admin_client = self.start_auth_server() |
619 | 635 | ||
620 | user = admin_client.new_user("test-user", []) | 636 | user = self.create_user("test-user", []) |
621 | user_info = user.copy() | 637 | user_info = user.copy() |
622 | del user_info["token"] | 638 | del user_info["token"] |
623 | 639 | ||
@@ -632,7 +648,7 @@ class HashEquivalenceCommonTests(object): | |||
632 | def test_auth_get_user(self): | 648 | def test_auth_get_user(self): |
633 | admin_client = self.start_auth_server() | 649 | admin_client = self.start_auth_server() |
634 | 650 | ||
635 | user = admin_client.new_user("test-user", []) | 651 | user = self.create_user("test-user", []) |
636 | user_info = user.copy() | 652 | user_info = user.copy() |
637 | del user_info["token"] | 653 | del user_info["token"] |
638 | 654 | ||
@@ -649,7 +665,7 @@ class HashEquivalenceCommonTests(object): | |||
649 | def test_auth_reconnect(self): | 665 | def test_auth_reconnect(self): |
650 | admin_client = self.start_auth_server() | 666 | admin_client = self.start_auth_server() |
651 | 667 | ||
652 | user = admin_client.new_user("test-user", []) | 668 | user = self.create_user("test-user", []) |
653 | user_info = user.copy() | 669 | user_info = user.copy() |
654 | del user_info["token"] | 670 | del user_info["token"] |
655 | 671 | ||
@@ -665,7 +681,7 @@ class HashEquivalenceCommonTests(object): | |||
665 | def test_auth_delete_user(self): | 681 | def test_auth_delete_user(self): |
666 | admin_client = self.start_auth_server() | 682 | admin_client = self.start_auth_server() |
667 | 683 | ||
668 | user = admin_client.new_user("test-user", []) | 684 | user = self.create_user("test-user", []) |
669 | 685 | ||
670 | # No self service | 686 | # No self service |
671 | with self.auth_client(user) as client, self.assertRaises(InvokeError): | 687 | with self.auth_client(user) as client, self.assertRaises(InvokeError): |
@@ -685,7 +701,7 @@ class HashEquivalenceCommonTests(object): | |||
685 | def test_auth_set_user_perms(self): | 701 | def test_auth_set_user_perms(self): |
686 | admin_client = self.start_auth_server() | 702 | admin_client = self.start_auth_server() |
687 | 703 | ||
688 | user = admin_client.new_user("test-user", []) | 704 | user = self.create_user("test-user", []) |
689 | 705 | ||
690 | self.assertUserPerms(user, []) | 706 | self.assertUserPerms(user, []) |
691 | 707 | ||
@@ -710,7 +726,7 @@ class HashEquivalenceCommonTests(object): | |||
710 | def test_auth_get_all_users(self): | 726 | def test_auth_get_all_users(self): |
711 | admin_client = self.start_auth_server() | 727 | admin_client = self.start_auth_server() |
712 | 728 | ||
713 | user = admin_client.new_user("test-user", []) | 729 | user = self.create_user("test-user", []) |
714 | 730 | ||
715 | with self.auth_client(user) as client, self.assertRaises(InvokeError): | 731 | with self.auth_client(user) as client, self.assertRaises(InvokeError): |
716 | client.get_all_users() | 732 | client.get_all_users() |
@@ -744,10 +760,10 @@ class HashEquivalenceCommonTests(object): | |||
744 | permissions.sort() | 760 | permissions.sort() |
745 | 761 | ||
746 | with self.auth_perms() as client, self.assertRaises(InvokeError): | 762 | with self.auth_perms() as client, self.assertRaises(InvokeError): |
747 | client.new_user("test-user", permissions) | 763 | self.create_user("test-user", permissions, client=client) |
748 | 764 | ||
749 | with self.auth_perms("@user-admin") as client: | 765 | with self.auth_perms("@user-admin") as client: |
750 | user = client.new_user("test-user", permissions) | 766 | user = self.create_user("test-user", permissions, client=client) |
751 | self.assertIn("token", user) | 767 | self.assertIn("token", user) |
752 | self.assertEqual(user["username"], "test-user") | 768 | self.assertEqual(user["username"], "test-user") |
753 | self.assertEqual(user["permissions"], permissions) | 769 | self.assertEqual(user["permissions"], permissions) |
@@ -755,7 +771,7 @@ class HashEquivalenceCommonTests(object): | |||
755 | def test_auth_become_user(self): | 771 | def test_auth_become_user(self): |
756 | admin_client = self.start_auth_server() | 772 | admin_client = self.start_auth_server() |
757 | 773 | ||
758 | user = admin_client.new_user("test-user", ["@read", "@report"]) | 774 | user = self.create_user("test-user", ["@read", "@report"]) |
759 | user_info = user.copy() | 775 | user_info = user.copy() |
760 | del user_info["token"] | 776 | del user_info["token"] |
761 | 777 | ||
@@ -898,7 +914,7 @@ class TestHashEquivalenceClient(HashEquivalenceTestSetup, unittest.TestCase): | |||
898 | user = admin_client.new_user("test-user", ["@read", "@report"]) | 914 | user = admin_client.new_user("test-user", ["@read", "@report"]) |
899 | 915 | ||
900 | p = self.run_hashclient([ | 916 | p = self.run_hashclient([ |
901 | "--address", self.auth_server.address, | 917 | "--address", self.auth_server_address, |
902 | "--login", user["username"], | 918 | "--login", user["username"], |
903 | "--password", user["token"], | 919 | "--password", user["token"], |
904 | "refresh-token" | 920 | "refresh-token" |
@@ -916,7 +932,7 @@ class TestHashEquivalenceClient(HashEquivalenceTestSetup, unittest.TestCase): | |||
916 | print("New token is %r" % new_token) | 932 | print("New token is %r" % new_token) |
917 | 933 | ||
918 | self.run_hashclient([ | 934 | self.run_hashclient([ |
919 | "--address", self.auth_server.address, | 935 | "--address", self.auth_server_address, |
920 | "--login", user["username"], | 936 | "--login", user["username"], |
921 | "--password", new_token, | 937 | "--password", new_token, |
922 | "get-user" | 938 | "get-user" |
@@ -928,7 +944,7 @@ class TestHashEquivalenceClient(HashEquivalenceTestSetup, unittest.TestCase): | |||
928 | user = admin_client.new_user("test-user", ["@read"]) | 944 | user = admin_client.new_user("test-user", ["@read"]) |
929 | 945 | ||
930 | self.run_hashclient([ | 946 | self.run_hashclient([ |
931 | "--address", self.auth_server.address, | 947 | "--address", self.auth_server_address, |
932 | "--login", admin_client.username, | 948 | "--login", admin_client.username, |
933 | "--password", admin_client.password, | 949 | "--password", admin_client.password, |
934 | "set-user-perms", | 950 | "set-user-perms", |
@@ -946,7 +962,7 @@ class TestHashEquivalenceClient(HashEquivalenceTestSetup, unittest.TestCase): | |||
946 | user = admin_client.new_user("test-user", ["@read"]) | 962 | user = admin_client.new_user("test-user", ["@read"]) |
947 | 963 | ||
948 | p = self.run_hashclient([ | 964 | p = self.run_hashclient([ |
949 | "--address", self.auth_server.address, | 965 | "--address", self.auth_server_address, |
950 | "--login", admin_client.username, | 966 | "--login", admin_client.username, |
951 | "--password", admin_client.password, | 967 | "--password", admin_client.password, |
952 | "get-user", | 968 | "get-user", |
@@ -957,7 +973,7 @@ class TestHashEquivalenceClient(HashEquivalenceTestSetup, unittest.TestCase): | |||
957 | self.assertIn("Permissions:", p.stdout) | 973 | self.assertIn("Permissions:", p.stdout) |
958 | 974 | ||
959 | p = self.run_hashclient([ | 975 | p = self.run_hashclient([ |
960 | "--address", self.auth_server.address, | 976 | "--address", self.auth_server_address, |
961 | "--login", user["username"], | 977 | "--login", user["username"], |
962 | "--password", user["token"], | 978 | "--password", user["token"], |
963 | "get-user", | 979 | "get-user", |
@@ -973,7 +989,7 @@ class TestHashEquivalenceClient(HashEquivalenceTestSetup, unittest.TestCase): | |||
973 | admin_client.new_user("test-user2", ["@read"]) | 989 | admin_client.new_user("test-user2", ["@read"]) |
974 | 990 | ||
975 | p = self.run_hashclient([ | 991 | p = self.run_hashclient([ |
976 | "--address", self.auth_server.address, | 992 | "--address", self.auth_server_address, |
977 | "--login", admin_client.username, | 993 | "--login", admin_client.username, |
978 | "--password", admin_client.password, | 994 | "--password", admin_client.password, |
979 | "get-all-users", | 995 | "get-all-users", |
@@ -987,7 +1003,7 @@ class TestHashEquivalenceClient(HashEquivalenceTestSetup, unittest.TestCase): | |||
987 | admin_client = self.start_auth_server() | 1003 | admin_client = self.start_auth_server() |
988 | 1004 | ||
989 | p = self.run_hashclient([ | 1005 | p = self.run_hashclient([ |
990 | "--address", self.auth_server.address, | 1006 | "--address", self.auth_server_address, |
991 | "--login", admin_client.username, | 1007 | "--login", admin_client.username, |
992 | "--password", admin_client.password, | 1008 | "--password", admin_client.password, |
993 | "new-user", | 1009 | "new-user", |
@@ -1017,14 +1033,13 @@ class TestHashEquivalenceClient(HashEquivalenceTestSetup, unittest.TestCase): | |||
1017 | user = admin_client.new_user("test-user", ["@read"]) | 1033 | user = admin_client.new_user("test-user", ["@read"]) |
1018 | 1034 | ||
1019 | p = self.run_hashclient([ | 1035 | p = self.run_hashclient([ |
1020 | "--address", self.auth_server.address, | 1036 | "--address", self.auth_server_address, |
1021 | "--login", admin_client.username, | 1037 | "--login", admin_client.username, |
1022 | "--password", admin_client.password, | 1038 | "--password", admin_client.password, |
1023 | "delete-user", | 1039 | "delete-user", |
1024 | "-u", user["username"], | 1040 | "-u", user["username"], |
1025 | ], check=True) | 1041 | ], check=True) |
1026 | 1042 | ||
1027 | |||
1028 | self.assertIsNone(admin_client.get_user(user["username"])) | 1043 | self.assertIsNone(admin_client.get_user(user["username"])) |
1029 | 1044 | ||
1030 | def test_get_db_usage(self): | 1045 | def test_get_db_usage(self): |
@@ -1104,19 +1119,43 @@ class TestHashEquivalenceWebsocketsSQLAlchemyServer(TestHashEquivalenceWebsocket | |||
1104 | 1119 | ||
1105 | 1120 | ||
1106 | class TestHashEquivalenceExternalServer(HashEquivalenceTestSetup, HashEquivalenceCommonTests, unittest.TestCase): | 1121 | class TestHashEquivalenceExternalServer(HashEquivalenceTestSetup, HashEquivalenceCommonTests, unittest.TestCase): |
1107 | def start_test_server(self): | 1122 | def get_env(self, name): |
1108 | if 'BB_TEST_HASHSERV' not in os.environ: | 1123 | v = os.environ.get(name) |
1109 | self.skipTest('BB_TEST_HASHSERV not defined to test an external server') | 1124 | if not v: |
1125 | self.skipTest(f'{name} not defined to test an external server') | ||
1126 | return v | ||
1110 | 1127 | ||
1111 | return os.environ['BB_TEST_HASHSERV'] | 1128 | def start_test_server(self): |
1129 | return self.get_env('BB_TEST_HASHSERV') | ||
1112 | 1130 | ||
1113 | def start_server(self, *args, **kwargs): | 1131 | def start_server(self, *args, **kwargs): |
1114 | self.skipTest('Cannot start local server when testing external servers') | 1132 | self.skipTest('Cannot start local server when testing external servers') |
1115 | 1133 | ||
1134 | def start_auth_server(self): | ||
1135 | |||
1136 | self.auth_server_address = self.server_address | ||
1137 | self.admin_client = self.start_client( | ||
1138 | self.server_address, | ||
1139 | username=self.get_env('BB_TEST_HASHSERV_USERNAME'), | ||
1140 | password=self.get_env('BB_TEST_HASHSERV_PASSWORD'), | ||
1141 | ) | ||
1142 | return self.admin_client | ||
1143 | |||
1116 | def setUp(self): | 1144 | def setUp(self): |
1117 | super().setUp() | 1145 | super().setUp() |
1146 | if "BB_TEST_HASHSERV_USERNAME" in os.environ: | ||
1147 | self.client = self.start_client( | ||
1148 | self.server_address, | ||
1149 | username=os.environ["BB_TEST_HASHSERV_USERNAME"], | ||
1150 | password=os.environ["BB_TEST_HASHSERV_PASSWORD"], | ||
1151 | ) | ||
1118 | self.client.remove({"method": self.METHOD}) | 1152 | self.client.remove({"method": self.METHOD}) |
1119 | 1153 | ||
1120 | def tearDown(self): | 1154 | def tearDown(self): |
1121 | self.client.remove({"method": self.METHOD}) | 1155 | self.client.remove({"method": self.METHOD}) |
1122 | super().tearDown() | 1156 | super().tearDown() |
1157 | |||
1158 | |||
1159 | def test_auth_get_all_users(self): | ||
1160 | self.skipTest("Cannot test all users with external server") | ||
1161 | |||