Ejemplo n.º 1
0
    def test_create_host_without_display_name_and_with_fqdn(self):
        """
        This test should verify that the display_name is set to the
        fqdn when a display_name is not passed in but the fqdn is passed in.
        """
        expected_display_name = "fred.flintstone.bedrock.com"

        host_data = HostWrapper(test_data(facts=None))
        del host_data.display_name
        host_data.fqdn = expected_display_name

        # Create the host
        response = self.post(HOST_URL, [host_data.data()], 207)

        self._verify_host_status(response, 0, 201)

        created_host = self._pluck_host_from_response(response, 0)

        original_id = created_host["id"]

        host_lookup_results = self.get(f"{HOST_URL}/{original_id}", 200)

        # Explicitly set the display_name ...this is expected here
        host_data.display_name = expected_display_name

        self._validate_host(host_lookup_results["results"][0], host_data, expected_id=original_id)
Ejemplo n.º 2
0
    def create_hosts(self):
        hosts_to_create = [
            ("host1", "12345", "host1.domain.test"),
            ("host2", "54321",
             "host1.domain.test"),  # the same fqdn is intentional
            ("host3", "56789", "host2.domain.test"),
        ]
        host_list = []

        for host in hosts_to_create:
            host_wrapper = HostWrapper()
            host_wrapper.account = ACCOUNT
            host_wrapper.tags = copy.deepcopy(TAGS)
            host_wrapper.display_name = host[0]
            host_wrapper.insights_id = host[1]
            host_wrapper.fqdn = host[2]
            host_wrapper.facts = [{
                "namespace": "ns1",
                "facts": {
                    "key1": "value1"
                }
            }]

            response_data = self.post(HOST_URL, host_wrapper.data(), 201)
            host_list.append(HostWrapper(response_data))

        return host_list
Ejemplo n.º 3
0
    def test_create_host_with_non_nullable_fields_as_none(self):
        non_nullable_field_names = (
            "display_name",
            "account",
            "insights_id",
            "rhel_machine_id",
            "subscription_manager_id",
            "satellite_id",
            "fqdn",
            "bios_uuid",
            "ip_addresses",
            "mac_addresses",
            "external_id",
            "ansible_host",
            "stale_timestamp",
            "reporter",
        )

        host_data = HostWrapper(test_data(facts=None))

        # Have at least one good canonical fact set
        host_data.insights_id = generate_uuid()
        host_data.rhel_machine_id = generate_uuid()

        host_dict = host_data.data()

        for field_name in non_nullable_field_names:
            with self.subTest(field_as_None=field_name):
                invalid_host_dict = copy.deepcopy(host_dict)

                invalid_host_dict[field_name] = None

                response_data = self.post(HOST_URL, [invalid_host_dict], 400)

                self.verify_error_response(response_data, expected_title="Bad Request")
Ejemplo n.º 4
0
    def test_create_host_without_ansible_host_then_update(self):
        # Create a host without ansible_host field
        # then update those fields
        host_data = HostWrapper(test_data(facts=None))
        del host_data.ansible_host

        # Create the host
        response = self.post(HOST_URL, [host_data.data()], 207)

        self._verify_host_status(response, 0, 201)

        created_host = self._pluck_host_from_response(response, 0)

        original_id = created_host["id"]

        ansible_hosts = ["ima_ansible_host_" + generate_uuid(), ""]

        # Update the ansible_host
        for ansible_host in ansible_hosts:
            with self.subTest(ansible_host=ansible_host):
                host_data.ansible_host = ansible_host

                # Update the hosts
                self.post(HOST_URL, [host_data.data()], 207)

                host_lookup_results = self.get(f"{HOST_URL}/{original_id}", 200)

                self._validate_host(host_lookup_results["results"][0], host_data, expected_id=original_id)
Ejemplo n.º 5
0
    def test_create_host_without_account(self):
        host_data = HostWrapper(test_data(facts=None))
        del host_data.account

        response_data = self.post(HOST_URL, [host_data.data()], 400)

        self.verify_error_response(
            response_data, expected_title="Bad Request", expected_detail="'account' is a required property - '0'"
        )
Ejemplo n.º 6
0
    def test_create_host_ignores_tags(self):
        host_data = HostWrapper(test_data(tags=[{"namespace": "ns", "key": "some_key", "value": "val"}]))
        create_response = self.post(HOST_URL, [host_data.data()], 207)
        self._verify_host_status(create_response, 0, 201)
        host = self._pluck_host_from_response(create_response, 0)
        host_id = host["id"]
        tags_response = self.get(f"{HOST_URL}/{host_id}/tags")

        self.assertEqual(tags_response["results"][host_id], [])
Ejemplo n.º 7
0
    def test_create_host_without_account(self):
        host_data = HostWrapper(test_data(facts=None))
        del host_data.account

        response_data = self.post(HOST_URL, host_data.data(), 400)

        assert "Bad Request" in response_data["title"]
        assert "status" in response_data
        assert "detail" in response_data
        assert "type" in response_data
Ejemplo n.º 8
0
    def test_create_host_without_required_fields(self):
        fields = ("account", "stale_timestamp", "reporter")
        for field in fields:
            with self.subTest(fields=fields):
                data = test_data()
                del data[field]

                host_data = HostWrapper(data)
                response_data = self.post(HOST_URL, [host_data.data()], 400)
                self.verify_error_response(response_data, expected_title="Bad Request")
Ejemplo n.º 9
0
    def test_create_host_with_mismatched_account_numbers(self):
        host_data = HostWrapper(test_data(facts=None))
        host_data.account = ACCOUNT[::-1]

        response_data = self.post(HOST_URL, host_data.data(), 400)

        assert "Invalid request" in response_data["title"]
        assert "status" in response_data
        assert "detail" in response_data
        assert "type" in response_data
Ejemplo n.º 10
0
    def test_create_with_branch_id(self):
        facts = None

        host_data = HostWrapper(test_data(facts=facts))

        post_url = HOST_URL + "?" + "branch_id=1234"

        # Create the host
        response = self.post(post_url, [host_data.data()], 207)

        self._verify_host_status(response, 0, 201)
Ejemplo n.º 11
0
    def test_create_host_with_invalid_facts_no_facts(self):
        facts = copy.deepcopy(FACTS)
        del facts[0]["facts"]
        host_data = HostWrapper(test_data(facts=facts))

        response_data = self.post(HOST_URL, host_data.data(), 400)

        assert response_data["title"] == "Invalid request"
        assert "status" in response_data
        assert "detail" in response_data
        assert "type" in response_data
Ejemplo n.º 12
0
    def test_create_and_update(self):
        facts = None
        tags = []

        host_data = HostWrapper(test_data(facts=facts, tags=tags))

        # Create the host
        created_host = self.post(HOST_URL, host_data.data(), 201)

        original_id = created_host["id"]

        self._validate_host(created_host, host_data, expected_id=original_id)
        created_time = dateutil.parser.parse(created_host["created"])
        self.assertGreater(datetime.now(timezone.utc), created_time)

        host_data.facts = copy.deepcopy(FACTS)

        # Replace facts under the first namespace
        host_data.facts[0]["facts"] = {"newkey1": "newvalue1"}

        # Add a new set of facts under a new namespace
        host_data.facts.append({
            "namespace": "ns2",
            "facts": {
                "key2": "value2"
            }
        })

        # Add a new canonical fact
        host_data.rhel_machine_id = "1234-56-789"
        host_data.ip_addresses = ["10.10.0.1", "10.0.0.2"]
        host_data.mac_addresses = ["c2:00:d0:c8:61:01"]
        host_data.insights_id = "0987-65-4321"

        # Update the host with the new data
        updated_host = self.post(HOST_URL, host_data.data(), 200)

        # Make sure the id from the update post matches the id from the create
        self.assertEqual(updated_host["id"], original_id)

        # Verify the timestamp has been modified
        self.assertIsNotNone(updated_host["updated"])
        modified_time = dateutil.parser.parse(updated_host["updated"])
        self.assertGreater(modified_time, created_time)

        host_lookup_results = self.get("%s/%s" % (HOST_URL, original_id), 200)

        # sanity check
        # host_lookup_results["results"][0]["facts"][0]["facts"]["key2"] = "blah"
        # host_lookup_results["results"][0]["insights_id"] = "1.2.3.4"
        self._validate_host(host_lookup_results["results"][0],
                            host_data,
                            expected_id=original_id)
Ejemplo n.º 13
0
    def test_create_host_with_display_name_as_None(self):
        host_data = HostWrapper(test_data(facts=None))

        # Explicitly set the display name to None
        host_data.display_name = None

        # initial create
        results = self.post(HOST_URL, host_data.data(), 201)

        self.assertIsNotNone(results["id"])

        self.assertIsNone(results["display_name"])
Ejemplo n.º 14
0
    def test_create_host_without_canonical_facts(self):
        host_data = HostWrapper(test_data(facts=None))
        del host_data.insights_id
        del host_data.rhel_machine_id
        del host_data.subscription_manager_id
        del host_data.satellite_id
        del host_data.bios_uuid
        del host_data.ip_addresses
        del host_data.fqdn
        del host_data.mac_addresses

        # FIXME: Verify response?
        response_data = self.post(HOST_URL, host_data.data(), 400)
 def setUp(self):
     super().setUp()
     host_wrapper = HostWrapper()
     host_wrapper.account = ACCOUNT
     host_wrapper.display_name = "host1"  # Same as self.added_hosts[0]
     host_wrapper.insights_id = generate_uuid()
     host_wrapper.stale_timestamp = now().isoformat()
     host_wrapper.reporter = "test"
     response_data = self.post(HOST_URL, [host_wrapper.data()], 207)
     self.added_hosts.append(HostWrapper(response_data["data"][0]["host"]))
Ejemplo n.º 16
0
    def test_create_host_with_empty_tags_facts_display_name_then_update(self):
        # Create a host with empty tags, facts, and display_name
        # then update those fields
        host_data = HostWrapper(test_data(facts=None))
        del host_data.tags
        del host_data.display_name
        del host_data.facts

        expected_tags = []

        # initial create
        results = self.post(HOST_URL, host_data.data(), 201)

        self.assertIsNotNone(results["id"])

        original_id = results["id"]

        host_data.tags = ["aws/new_tag_1:new_value_1", "aws/k:v"]
        host_data.facts = FACTS
        host_data.display_name = "expected_display_name"

        self.post(HOST_URL, host_data.data(), 200)

        data = self.get("%s/%s" % (HOST_URL, original_id), 200)
        results = HostWrapper(data["results"][0])

        self.assertListEqual(results.tags, expected_tags)

        self.assertListEqual(results.facts, host_data.facts)

        self.assertEqual(results.display_name, host_data.display_name)
Ejemplo n.º 17
0
    def test_create_host_with_valid_mac_address(self):
        valid_mac_arrays = [["blah"], ["11:22:33:44:55:66", "blah"]]

        for mac_array in valid_mac_arrays:
            with self.subTest(mac_array=mac_array):
                host_data = HostWrapper(test_data(facts=None))
                host_data.insights_id = generate_uuid()
                host_data.mac_addresses = mac_array

                response_data = self.post(HOST_URL, [host_data.data()], 207)

                error_host = response_data["data"][0]

                self.assertEqual(error_host["status"], 201)
Ejemplo n.º 18
0
    def add_2_hosts(self):
        # FIXME: make this more generic
        host_list = []

        canonical_facts = {'insights_id': '12345'}
        display_name = "host1"
        tags = TAGS
        host_data = HostWrapper(
            test_data(display_name=display_name,
                      canonical_facts=canonical_facts,
                      tags=tags))
        response_data = self.post(HOST_URL, host_data.data(), 201)
        host_list.append(HostWrapper(response_data))

        canonical_facts = {'insights_id': '54321'}
        display_name = "host2"
        tags = TAGS
        host_data = HostWrapper(
            test_data(display_name=display_name,
                      canonical_facts=canonical_facts,
                      tags=tags))
        response_data = self.post(HOST_URL, host_data.data(), 201)
        host_list.append(HostWrapper(response_data))

        return host_list
Ejemplo n.º 19
0
    def test_create_host_with_invalid_facts_no_namespace(self):
        facts = copy.deepcopy(FACTS)
        del facts[0]["namespace"]
        host_data = HostWrapper(test_data(facts=facts))

        response_data = self.post(HOST_URL, [host_data.data()], 207)

        self._verify_host_status(response_data, 0, 400)

        response_data = response_data["data"][0]

        assert response_data["title"] == "Invalid request"
        assert "status" in response_data
        assert "detail" in response_data
        assert "type" in response_data
Ejemplo n.º 20
0
    def test_create_host_with_display_name_as_None(self):
        host_data = HostWrapper(test_data(facts=None))

        # Explicitly set the display name to None
        host_data.display_name = None

        # Create the host
        created_host = self.post(HOST_URL, host_data.data(), 201)

        self.assertIsNotNone(created_host["id"])

        self._validate_host(created_host,
                            host_data,
                            expected_id=created_host["id"],
                            verify_tags=False)
Ejemplo n.º 21
0
    def _add_hosts(self, data):
        post = []
        for d in data:
            host = HostWrapper(test_data(insights_id=generate_uuid(), **d))
            post.append(host.data())

        response = self.post(HOST_URL, post, 207)

        hosts = []
        for i in range(len(data)):
            self._verify_host_status(response, i, 201)
            added_host = self._pluck_host_from_response(response, i)
            hosts.append(HostWrapper(added_host))

        return hosts
Ejemplo n.º 22
0
    def test_rest_post_disabling(self):
        host_data = HostWrapper(test_data())

        with self.app.app_context():
            config = self.app.config["INVENTORY_CONFIG"]
            config.rest_post_enabled = False
            response = self.post(HOST_URL, [host_data.data()], 405)

        self.verify_error_response(
            response,
            expected_status=405,
            expected_title="Method Not Allowed",
            expected_detail="The method is not allowed for the requested URL.",
            expected_type="about:blank",
        )
    def _basic_fact_test(self, input_facts, expected_facts, replace_facts):

        host_list = self.added_hosts

        # This test assumes the set of facts are the same across
        # the hosts in the host_list

        target_namespace = host_list[0].facts[0]["namespace"]

        url_host_id_list = self._build_host_id_list_for_url(host_list)

        patch_url = self._build_facts_url(host_list, target_namespace)

        if replace_facts:
            response = self.put(patch_url, input_facts, 200)
        else:
            response = self.patch(patch_url, input_facts, 200)

        response = self.get(f"{HOST_URL}/{url_host_id_list}", 200)

        self.assertEqual(len(response["results"]), len(host_list))

        for response_host in response["results"]:
            host_to_verify = HostWrapper(response_host)

            self.assertEqual(host_to_verify.facts[0]["facts"], expected_facts)

            self.assertEqual(host_to_verify.facts[0]["namespace"],
                             target_namespace)
Ejemplo n.º 24
0
    def test_add_facts_to_multiple_hosts(self):
        facts_to_add = {"newfact1": "newvalue1", "newfact2": "newvalue2"}

        host_list = self.add_2_hosts()

        # This test assumes the set of facts are the same across
        # the hosts in the host_list

        expected_facts = {**host_list[0].facts[0]["facts"], **facts_to_add}

        target_namespace = host_list[0].facts[0]["namespace"]

        url_host_id_list = self._build_host_id_list_for_url(host_list)

        patch_url = self._build_facts_url(host_list, target_namespace)

        response = self.patch(patch_url, facts_to_add, 200)

        response = self.get(f"{HOST_URL}/{url_host_id_list}", 200)

        self.assertEqual(len(response["results"]), len(host_list))

        for response_host in response["results"]:
            host_to_verify = HostWrapper(response_host)

            self.assertEqual(host_to_verify.facts[0]["facts"], expected_facts)

            self.assertEqual(host_to_verify.facts[0]["namespace"],
                             target_namespace)
Ejemplo n.º 25
0
    def test_create_host_with_mismatched_account_numbers(self):
        host_data = HostWrapper(test_data(facts=None))
        host_data.account = ACCOUNT[::-1]

        response_data = self.post(HOST_URL, [host_data.data()], 207)

        self._verify_host_status(response_data, 0, 400)

        response_data = response_data["data"][0]

        self.verify_error_response(
            response_data,
            expected_title="Invalid request",
            expected_detail="The account number associated with the user does not match the account number associated "
            "with the host",
        )
Ejemplo n.º 26
0
    def test_create_host_with_invalid_mac_address(self):
        invalid_mac_arrays = [[], [""], ["11:22:33:44:55:66", "a" * 256]]

        for mac_array in invalid_mac_arrays:
            with self.subTest(mac_array=mac_array):
                host_data = HostWrapper(test_data(facts=None))
                host_data.insights_id = generate_uuid()
                host_data.mac_addresses = mac_array

                response_data = self.post(HOST_URL, [host_data.data()], 207)

                error_host = response_data["data"][0]

                self.assertEqual(error_host["status"], 400)

                self.verify_error_response(error_host, expected_title="Bad Request")
Ejemplo n.º 27
0
    def test_create_host_with_invalid_display_name(self):
        host_data = HostWrapper(test_data(facts=None))

        invalid_display_names = ["", "a" * 201]

        for display_name in invalid_display_names:
            with self.subTest(display_name=display_name):
                host_data.display_name = display_name

                response = self.post(HOST_URL, [host_data.data()], 207)

                error_host = response["data"][0]

                self.assertEqual(error_host["status"], 400)

                self.verify_error_response(error_host, expected_title="Bad Request")
Ejemplo n.º 28
0
    def test_create_host_with_invalid_fqdn(self):
        host_data = HostWrapper(test_data(facts=None))

        invalid_fqdns = ["", "a" * 256]

        for fqdn in invalid_fqdns:
            with self.subTest(fqdn=fqdn):
                host_data.fqdn = fqdn

                response = self.post(HOST_URL, [host_data.data()], 207)

                error_host = response["data"][0]

                self.assertEqual(error_host["status"], 400)

                self.verify_error_response(error_host, expected_title="Bad Request")
Ejemplo n.º 29
0
    def test_create_host_with_invalid_external_id(self):
        host_data = HostWrapper(test_data(facts=None))

        invalid_external_ids = ["", "a" * 501]

        for external_id in invalid_external_ids:
            with self.subTest(external_id=external_id):
                host_data.external_id = external_id

                response = self.post(HOST_URL, [host_data.data()], 207)

                error_host = response["data"][0]

                self.assertEqual(error_host["status"], 400)

                self.verify_error_response(error_host, expected_title="Bad Request")
Ejemplo n.º 30
0
    def test_create_host_with_invalid_ansible_host(self):
        host_data = HostWrapper(test_data(facts=None))

        invalid_ansible_host = ["a" * 256]

        for ansible_host in invalid_ansible_host:
            with self.subTest(ansible_host=ansible_host):
                host_data.ansible_host = ansible_host

                response = self.post(HOST_URL, [host_data.data()], 207)

                error_host = response["data"][0]

                self.assertEqual(error_host["status"], 400)

                self.verify_error_response(error_host, expected_title="Bad Request")