Ejemplo n.º 1
0
 def test_unmount_unmounts_filesystem_as_user(self):
     node = factory.make_Node(status=NODE_STATUS.ALLOCATED, owner=self.user)
     partition = self.make_partition(node)
     filesystem = factory.make_Filesystem(
         partition=partition, mount_point="/mnt", acquired=True
     )
     uri = get_partition_uri(partition)
     response = self.client.post(uri, {"op": "unmount"})
     content = response.content.decode(settings.DEFAULT_CHARSET)
     self.assertEqual(http.client.OK, response.status_code, content)
     self.assertThat(
         json.loads(content)["filesystem"],
         ContainsDict({"mount_point": Is(None), "mount_options": Is(None)}),
     )
     self.assertThat(
         reload_object(filesystem),
         MatchesStructure(
             mount_point=Is(None),
             mount_options=Is(None),
             is_mounted=Is(False),
         ),
     )
Ejemplo n.º 2
0
 def test_read(self):
     subnet = factory.make_Subnet(cidr="10.0.0.0/24")
     iprange = factory.make_IPRange(subnet, "10.0.0.2", "10.0.0.10")
     factory.make_IPRange(subnet, "10.0.0.11", "10.0.0.20")
     factory.make_IPRange(subnet, "10.0.0.21", "10.0.0.30")
     uri = get_iprange_uri(iprange)
     response = self.client.get(uri)
     self.assertEqual(http.client.OK, response.status_code,
                      response.content)
     parsed_ipranges = json.loads(
         response.content.decode(settings.DEFAULT_CHARSET))
     self.assertThat(
         parsed_ipranges,
         ContainsDict({
             "id": Equals(iprange.id),
             "start_ip": Equals(iprange.start_ip),
             "end_ip": Equals(iprange.end_ip),
             "comment": Equals(iprange.comment),
             "type": Equals(iprange.type),
             "user": Equals(iprange.user),
         }),
     )
Ejemplo n.º 3
0
def MatchesNotification(notification):
    """Match the expected JSON rendering of `notification`."""
    return MatchesDict({
        "id":
        Equals(notification.id),
        "ident":
        Equals(notification.ident),
        "user": (Is(None) if notification.user_id is None else ContainsDict(
            {"username": Equals(notification.user.username)})),
        "users":
        Is(notification.users),
        "admins":
        Is(notification.admins),
        "message":
        Equals(notification.message),
        "context":
        Equals(notification.context),
        "category":
        Equals(notification.category),
        "resource_uri":
        Equals(get_notification_uri(notification)),
    })
Ejemplo n.º 4
0
 def test_mount_sets_mount_path_on_filesystem_as_user(self):
     node = factory.make_Node(status=NODE_STATUS.ALLOCATED, owner=self.user)
     block_device = factory.make_PhysicalBlockDevice(node=node)
     partition_table = factory.make_PartitionTable(
         block_device=block_device)
     partition = partition_table.add_partition()
     filesystem = factory.make_Filesystem(partition=partition,
                                          acquired=True)
     uri = get_partition_uri(partition)
     mount_point = "/mnt"
     mount_options = factory.make_name("mount-options")
     response = self.client.post(
         uri,
         {
             "op": "mount",
             "mount_point": mount_point,
             "mount_options": mount_options,
         },
     )
     self.assertEqual(http.client.OK, response.status_code,
                      response.content)
     parsed_device = json.loads(
         response.content.decode(settings.DEFAULT_CHARSET))
     self.assertThat(
         parsed_device["filesystem"],
         ContainsDict({
             "mount_point": Equals(mount_point),
             "mount_options": Equals(mount_options),
         }),
     )
     self.assertThat(
         reload_object(filesystem),
         MatchesStructure(
             mount_point=Equals(mount_point),
             mount_options=Equals(mount_options),
             is_mounted=Is(True),
         ),
     )
Ejemplo n.º 5
0
    def create_servers(self, rcs, num, wait_for=None):
        """
        Create some number of servers using just Nova, and wait until they
        are active.  This uses the same default server arguments as
        `create_group`.

        :param TestResources rcs: An instance of
            :class:`otter.integration.lib.resources.TestResources`
        :param int num: The number of servers to create.
        :param wait_for: What state to wait for for those servers - by default,
            it waits just for them to be active

        :return: an iterable of server details JSON of the created servers.
        """
        image_id = yield fetch_ubuntu_image_id(rcs, self.pool)
        as_args = create_scaling_group_dict(image_ref=image_id,
                                            flavor_ref=flavor_ref)
        server_args = as_args['launchConfiguration']['args']
        server_args['server']['name'] = "autogenerated-non-as-test-server"

        if wait_for is None:
            wait_for = ContainsDict({'status': Equals("ACTIVE")})

        server_ids = yield gatherResults(
            [create_server(rcs, self.pool, server_args) for _ in range(num)])

        self.test_case.addCleanup(delete_servers, server_ids, rcs, self.pool)

        servers = yield wait_for_servers(
            rcs,
            self.pool,
            # The list of active servers' ids has the created server ids
            AfterPreprocessing(
                lambda servers: [s for s in servers if s['id'] in server_ids],
                AllMatch(wait_for)))

        returnValue(
            [server for server in servers if server['id'] in server_ids])
Ejemplo n.º 6
0
 def test_unmount_unmounts_filesystem_as_admin(self):
     self.become_admin()
     node = factory.make_Node(status=NODE_STATUS.READY)
     partition = self.make_partition(node)
     filesystem = factory.make_Filesystem(
         partition=partition, mount_point="/mnt")
     uri = get_partition_uri(partition)
     response = self.client.post(uri, {'op': 'unmount'})
     content = response.content.decode(settings.DEFAULT_CHARSET)
     self.assertEqual(http.client.OK, response.status_code, content)
     self.assertThat(
         json.loads(content)["filesystem"],
         ContainsDict({
             "mount_point": Is(None),
             "mount_options": Is(None),
         }))
     self.assertThat(
         reload_object(filesystem),
         MatchesStructure(
             mount_point=Is(None),
             mount_options=Is(None),
             is_mounted=Is(False),
         ))
Ejemplo n.º 7
0
    def test_read(self):
        class_type = factory.make_name("class")
        fabric = factory.make_Fabric(class_type=class_type)
        for vid in range(1, 4):
            factory.make_VLAN(fabric=fabric, vid=vid).id
        uri = get_fabric_uri(fabric)
        response = self.client.get(uri)

        self.assertEqual(http.client.OK, response.status_code,
                         response.content)
        parsed_fabric = json_load_bytes(response.content)
        self.assertThat(
            parsed_fabric,
            ContainsDict({
                "id": Equals(fabric.id),
                "name": Equals(fabric.get_name()),
                "class_type": Equals(class_type),
            }),
        )
        self.assertItemsEqual(
            [vlan.id for vlan in fabric.vlan_set.all()],
            [vlan["id"] for vlan in parsed_fabric["vlans"]],
        )
Ejemplo n.º 8
0
    def test_read_without_space_returns_undefined_space(self):
        vlan = factory.make_VLAN(space=None)
        uri = get_vlan_uri(vlan, vlan.fabric)
        response = self.client.get(uri)

        self.assertEqual(
            http.client.OK, response.status_code, response.content
        )
        parsed_vlan = json.loads(
            response.content.decode(settings.DEFAULT_CHARSET)
        )
        self.assertThat(
            parsed_vlan,
            ContainsDict(
                {
                    "id": Equals(vlan.id),
                    "name": Equals(vlan.get_name()),
                    "vid": Equals(vlan.vid),
                    "space": Equals(Space.UNDEFINED),
                    "resource_uri": Equals(get_vlan_uri(vlan)),
                }
            ),
        )
Ejemplo n.º 9
0
 def test_good(self):
     magic_folder_initialize(
         self.temp.child("good"),
         u"tcp:1234",
         self.node_dir.path,
         u"tcp:localhost:1234",
     )
     stdout = StringIO()
     config = load_global_configuration(self.temp.child("good"))
     magic_folder_show_config(
         config,
         stdout=stdout,
     )
     self.assertThat(
         json.loads(stdout.getvalue()),
         ContainsDict({
             u'api_endpoint':
             Equals(u'tcp:1234'),
             u'tahoe_node_directory':
             Equals(self.node_dir.path.path),
             u'magic_folders':
             Equals({}),
         }))
    def _verify_port(self, port, subnet4=None, subnet6=None, **kwargs):
        has_ipv4_ip = False
        has_ipv6_ip = False

        for fixed_ip in port['fixed_ips']:
            ip_address = fixed_ip['ip_address']
            if subnet4 is not None and fixed_ip['subnet_id'] == subnet4['id']:
                self.verify_ip_in_allocation_pools(ip_address,
                                                   subnet4['allocation_pools'])
                has_ipv4_ip = True

            if subnet6 is not None and fixed_ip['subnet_id'] == subnet6['id']:
                self.verify_ip_in_allocation_pools(ip_address,
                                                   subnet6['allocation_pools'])
                has_ipv6_ip = True

        if subnet4:
            self.assertTrue(
                has_ipv4_ip,
                "Must have an IPv4 ip in subnet: %s" % subnet4['id'])

        if subnet6:
            self.assertTrue(
                has_ipv6_ip,
                "Must have an IPv6 ip in subnet: %s" % subnet6['id'])

        self.assertIsNotNone(port['mac_address'])

        # verify all other kwargs as attributes (key,value) pairs
        for key, value in iteritems(kwargs):
            if isinstance(value, dict):
                # compare dict
                raise NotImplementedError
            if isinstance(value, list):
                self.assertItemsEqual(port[key], value)
            else:
                self.assertThat(port, ContainsDict({key: Equals(value)}))
Ejemplo n.º 11
0
    def test_changing_disowned_server_is_not_converged_1(self):
        """
        Moving a disowned autoscale server to a different CLB and converging
        will not move the disowned server back on its intended CLB.

        1. Create an AS group with CLB and 2 servers.
        2. Disown 1 server.
        3. Remove both servers from group CLB and add to a different CLB.
        4. Converge group.
        5. Assert that the group's server is back on its CLB and it is not
           removed from other CLB. Disowned server remains on the other CLB.
        """
        group, clb_as, clb_other, gone_ip, stay_ip = (
            yield self._disown_change_and_converge(True))

        yield gatherResults([
            clb_as.wait_for_nodes(self.rcs,
                                  MatchesAll(ExcludesAllIPs([gone_ip]),
                                             ContainsAllIPs([stay_ip]),
                                             HasLength(1)),
                                  timeout=timeout_default),
            clb_other.wait_for_nodes(self.rcs,
                                     MatchesAll(
                                         ContainsAllIPs([stay_ip, gone_ip]),
                                         HasLength(2)),
                                     timeout=timeout_default),
            group.wait_for_state(self.rcs,
                                 MatchesAll(
                                     ContainsDict({
                                         'pendingCapacity': Equals(0),
                                         'desiredCapacity': Equals(1),
                                         'status': Equals('ACTIVE'),
                                         'active': HasLength(1)
                                     })),
                                 timeout=timeout_default),
        ])
Ejemplo n.º 12
0
class ExtraNetworkClientTests(TestCase):
    """
    Direct tests for ``_NetworkClient`` that go beyond the guarantees of
    ``IKubernetesClient``.
    """
    @capture_logging(
        lambda self, logger: self.expectThat(
            logger.messages,
            AnyMatch(ContainsDict({
                u"action_type": Equals(u"network-client:list"),
                u"apiVersion": Equals(u"v1"),
                u"kind": Equals(u"Pod"),
            })),
        ),
    )
    def test_list_logging(self, logger):
        """
        ``_NetworkClient.list`` logs an Eliot event describing its given type.
        """
        client = network_kubernetes(
            base_url=URL.fromText(u"http://127.0.0.1/"),
            agent=Agent(MemoryReactor()),
        ).client()
        client.list(v1.Pod)
Ejemplo n.º 13
0
 def test_run_failed(self):
     # A failed run sets the job status to FAILED and records the error
     # message.
     # The job requests builds and records the result.
     distroseries, processors = self.makeSeriesAndProcessors(
         ["avr2001", "sparc64", "x32"])
     [git_ref] = self.factory.makeGitRefs()
     snap = self.factory.makeSnap(
         git_ref=git_ref, distroseries=distroseries, processors=processors)
     expected_date_created = get_transaction_timestamp(IStore(snap))
     job = SnapRequestBuildsJob.create(
         snap, snap.registrant, distroseries.main_archive,
         PackagePublishingPocket.RELEASE, {"core": "stable"})
     self.useFixture(GitHostingFixture()).getBlob.failure = (
         CannotParseSnapcraftYaml("Nonsense on stilts"))
     with dbuser(config.ISnapRequestBuildsJobSource.dbuser):
         JobRunner([job]).runAll()
     now = get_transaction_timestamp(IStore(snap))
     [notification] = self.assertEmailQueueLength(1)
     self.assertThat(dict(notification), ContainsDict({
         "From": Equals(config.canonical.noreply_from_address),
         "To": Equals(format_address_for_person(snap.registrant)),
         "Subject": Equals(
             "Launchpad error while requesting builds of %s" % snap.name),
         }))
     self.assertEqual(
         "Launchpad encountered an error during the following operation: "
         "requesting builds of %s.  Nonsense on stilts" % snap.name,
         notification.get_payload(decode=True))
     self.assertThat(job, MatchesStructure(
         job=MatchesStructure.byEquality(status=JobStatus.FAILED),
         date_created=Equals(expected_date_created),
         date_finished=MatchesAll(
             GreaterThan(expected_date_created), LessThan(now)),
         error_message=Equals("Nonsense on stilts"),
         builds=AfterPreprocessing(set, MatchesSetwise())))
Ejemplo n.º 14
0
 def test_create_logical_volume_creates_logical_volume(self):
     self.become_admin()
     node = factory.make_Node(status=NODE_STATUS.READY)
     volume_group = factory.make_VolumeGroup(node=node)
     name = factory.make_name("lv")
     vguuid = "%s" % uuid.uuid4()
     size = random.randint(MIN_BLOCK_DEVICE_SIZE, volume_group.get_size())
     uri = get_volume_group_uri(volume_group)
     response = self.client.post(
         uri,
         {
             "op": "create_logical_volume",
             "name": name,
             "uuid": vguuid,
             "size": size,
         },
     )
     self.assertEqual(
         http.client.OK, response.status_code, response.content
     )
     logical_volume = json.loads(
         response.content.decode(settings.DEFAULT_CHARSET)
     )
     expected_size = round_size_to_nearest_block(
         size, PARTITION_ALIGNMENT_SIZE, False
     )
     self.assertThat(
         logical_volume,
         ContainsDict(
             {
                 "name": Equals("%s-%s" % (volume_group.name, name)),
                 "uuid": Equals(vguuid),
                 "size": Equals(expected_size),
             }
         ),
     )
Ejemplo n.º 15
0
    def test_checkLimit_check_token(self):
        _add_rate_limit_response("example.org")
        responses.add("GET", "http://example.org/", body="test")
        with self.rate_limit.checkLimit(
                "http://example.org/", 30, token="abc"):
            pass
        self.assertThat(responses.calls[0].request, MatchesStructure(
            path_url=Equals("/rate_limit"),
            headers=ContainsDict({"Authorization": Equals("token abc")})))
        limit = self.rate_limit._limits[("example.org", "abc")]
        self.assertEqual(3999, limit["remaining"])
        self.assertEqual(1000000000, limit["reset"])

        limit["remaining"] = 0
        responses.reset()
        with ExpectedException(
                GitHubExceededRateLimit,
                r"Rate limit for example\.org exceeded "
                r"\(resets at Sun Sep  9 07:16:40 2001\)"):
            with self.rate_limit.checkLimit(
                    "http://example.org/", 30, token="abc"):
                pass
        self.assertEqual(0, len(responses.calls))
        self.assertEqual(0, limit["remaining"])
Ejemplo n.º 16
0
    def test__tryConnection_logs_error(self):
        listener = PostgresListenerService()

        exception_type = factory.make_exception_type()
        exception_message = factory.make_name("message")

        startConnection = self.patch(listener, "startConnection")
        startConnection.side_effect = exception_type(exception_message)

        with TwistedLoggerFixture() as logger:
            with ExpectedException(exception_type):
                yield listener.tryConnection()

        self.assertThat(logger.events, HasLength(1))
        self.assertThat(
            logger.events[0],
            ContainsDict({
                "log_format":
                Equals("Unable to connect to database: {error}"),
                "log_level":
                Equals(LogLevel.error),
                "error":
                Equals(exception_message),
            }))
Ejemplo n.º 17
0
    def test_unmount_unmounts_filesystem_as_user(self):
        node = factory.make_Node(status=NODE_STATUS.ALLOCATED, owner=self.user)
        block_device = factory.make_VirtualBlockDevice(node=node)
        filesystem = factory.make_Filesystem(block_device=block_device,
                                             mount_point="/mnt",
                                             acquired=True)
        uri = get_blockdevice_uri(block_device)
        response = self.client.post(uri, {'op': 'unmount'})

        self.assertEqual(http.client.OK, response.status_code,
                         response.content)
        self.assertThat(
            json_load_bytes(response.content)['filesystem'],
            ContainsDict({
                "mount_point": Is(None),
                "mount_options": Is(None),
            }))
        self.assertThat(
            reload_object(filesystem),
            MatchesStructure(
                mount_point=Is(None),
                mount_options=Is(None),
                is_mounted=Is(False),
            ))
Ejemplo n.º 18
0
    def test_read_with_fabric(self):
        fabric = factory.make_Fabric()
        vlan = factory.make_VLAN(fabric=fabric)
        uri = get_vlan_uri(vlan, fabric)
        response = self.client.get(uri)

        self.assertEqual(
            http.client.OK, response.status_code, response.content
        )
        parsed_vlan = json.loads(
            response.content.decode(settings.DEFAULT_CHARSET)
        )
        self.assertThat(
            parsed_vlan,
            ContainsDict(
                {
                    "id": Equals(vlan.id),
                    "name": Equals(vlan.get_name()),
                    "vid": Equals(vlan.vid),
                    "fabric": Equals(fabric.get_name()),
                    "resource_uri": Equals(get_vlan_uri(vlan)),
                }
            ),
        )
Ejemplo n.º 19
0
 def test__defaults_to_process_environment(self):
     name = factory.make_name("name")
     value = factory.make_name("value")
     with EnvironmentVariable(name, value):
         self.assertThat(get_env_with_locale(),
                         ContainsDict({name: Equals(value)}))
Ejemplo n.º 20
0
def ContainsDictByEquality(expected):
    return ContainsDict(
        {key: Equals(value)
         for key, value in expected.items()})
Ejemplo n.º 21
0
 def test_compose_enlistment_preseed(self):
     rack_controller = factory.make_RackController()
     url = factory.make_simple_http_url()
     request = make_HttpRequest()
     apt_proxy = get_apt_proxy(request, rack_controller)
     preseed = yaml.safe_load(
         compose_enlistment_preseed(
             request,
             rack_controller,
             {
                 "metadata_enlist_url": url,
                 "syslog_host_port": url
             },
         ))
     self.assertDictEqual({"MAAS": {
         "metadata_url": url
     }}, preseed["datasource"])
     self.assertTrue(preseed["manage_etc_hosts"])
     self.assertDictEqual({"remotes": {"maas": url}}, preseed["rsyslog"])
     self.assertDictEqual(
         {
             "delay": "now",
             "mode": "poweroff",
             "timeout": 1800,
             "condition": "test ! -e /tmp/block-poweroff",
         },
         preseed["power_state"],
     )
     self.assertItemsEqual(
         [
             "python3-yaml",
             "python3-oauthlib",
             "freeipmi-tools",
             "ipmitool",
             "sshpass",
             "archdetect-deb",
             "jq",
         ],
         preseed["packages"],
     )
     default = PackageRepository.get_main_archive().url
     ports = PackageRepository.get_ports_archive().url
     self.assertThat(
         preseed,
         ContainsDict({
             "apt":
             ContainsDict({
                 "preserve_sources_list":
                 Equals(False),
                 "primary":
                 MatchesListwise([
                     MatchesDict({
                         "arches": Equals(["amd64", "i386"]),
                         "uri": Equals(default),
                     }),
                     MatchesDict({
                         "arches": Equals(["default"]),
                         "uri": Equals(ports),
                     }),
                 ]),
                 "proxy":
                 Equals(apt_proxy),
                 "security":
                 MatchesListwise([
                     MatchesDict({
                         "arches": Equals(["amd64", "i386"]),
                         "uri": Equals(default),
                     }),
                     MatchesDict({
                         "arches": Equals(["default"]),
                         "uri": Equals(ports),
                     }),
                 ]),
             })
         }),
     )
 def assertSystemInfo(self, config):
     self.assertThat(
         config,
         ContainsDict(
             {
                 "system_info": MatchesDict(
                     {
                         "package_mirrors": MatchesListwise(
                             [
                                 MatchesDict(
                                     {
                                         "arches": Equals(
                                             ["i386", "amd64"]
                                         ),
                                         "search": MatchesDict(
                                             {
                                                 "primary": Equals(
                                                     [
                                                         PackageRepository.get_main_archive().url
                                                     ]
                                                 ),
                                                 "security": Equals(
                                                     [
                                                         PackageRepository.get_main_archive().url
                                                     ]
                                                 ),
                                             }
                                         ),
                                         "failsafe": MatchesDict(
                                             {
                                                 "primary": Equals(
                                                     "http://archive.ubuntu.com/ubuntu"
                                                 ),
                                                 "security": Equals(
                                                     "http://security.ubuntu.com/ubuntu"
                                                 ),
                                             }
                                         ),
                                     }
                                 ),
                                 MatchesDict(
                                     {
                                         "arches": Equals(["default"]),
                                         "search": MatchesDict(
                                             {
                                                 "primary": Equals(
                                                     [
                                                         PackageRepository.get_ports_archive().url
                                                     ]
                                                 ),
                                                 "security": Equals(
                                                     [
                                                         PackageRepository.get_ports_archive().url
                                                     ]
                                                 ),
                                             }
                                         ),
                                         "failsafe": MatchesDict(
                                             {
                                                 "primary": Equals(
                                                     "http://ports.ubuntu.com/ubuntu-ports"
                                                 ),
                                                 "security": Equals(
                                                     "http://ports.ubuntu.com/ubuntu-ports"
                                                 ),
                                             }
                                         ),
                                     }
                                 ),
                             ]
                         )
                     }
                 )
             }
         ),
     )
Ejemplo n.º 23
0
    def test_read(self):
        node = factory.make_Node()
        block_devices = [
            factory.make_PhysicalBlockDevice(node=node) for _ in range(3)
        ]
        block_device_ids = [bd.id for bd in block_devices]
        bd_filesystems = [
            factory.make_Filesystem(fstype=FILESYSTEM_TYPE.RAID,
                                    block_device=bd) for bd in block_devices
        ]
        spare_block_devices = [
            factory.make_PhysicalBlockDevice(node=node) for _ in range(3)
        ]
        spare_block_device_ids = [bd.id for bd in spare_block_devices]
        spare_bd_filesystems = [
            factory.make_Filesystem(fstype=FILESYSTEM_TYPE.RAID_SPARE,
                                    block_device=bd)
            for bd in spare_block_devices
        ]
        partitions = [
            factory.make_Partition(partition_table=factory.make_PartitionTable(
                block_device=factory.make_PhysicalBlockDevice(node=node)))
            for _ in range(3)
        ]
        partitions_ids = [partition.id for partition in partitions]
        partition_filesystems = [
            factory.make_Filesystem(fstype=FILESYSTEM_TYPE.RAID,
                                    partition=partition)
            for partition in partitions
        ]
        spare_partitions = [
            factory.make_Partition(partition_table=factory.make_PartitionTable(
                block_device=factory.make_PhysicalBlockDevice(node=node)))
            for _ in range(3)
        ]
        spare_partitions_ids = [partition.id for partition in spare_partitions]
        spare_partition_filesystems = [
            factory.make_Filesystem(fstype=FILESYSTEM_TYPE.RAID_SPARE,
                                    partition=partition)
            for partition in spare_partitions
        ]
        raid = factory.make_FilesystemGroup(
            group_type=FILESYSTEM_GROUP_TYPE.RAID_5,
            filesystems=(bd_filesystems + spare_bd_filesystems +
                         partition_filesystems + spare_partition_filesystems))
        uri = get_raid_device_uri(raid)
        response = self.client.get(uri)

        self.assertEqual(http.client.OK, response.status_code,
                         response.content)
        parsed_raid = json.loads(
            response.content.decode(settings.DEFAULT_CHARSET))
        parsed_device_ids = [device["id"] for device in parsed_raid["devices"]]
        parsed_spare_device_ids = [
            device["id"] for device in parsed_raid["spare_devices"]
        ]
        self.assertThat(
            parsed_raid,
            ContainsDict({
                "id":
                Equals(raid.id),
                "uuid":
                Equals(raid.uuid),
                "name":
                Equals(raid.name),
                "level":
                Equals(raid.group_type),
                "size":
                Equals(raid.get_size()),
                "human_size":
                Equals(human_readable_bytes(raid.get_size())),
                "resource_uri":
                Equals(get_raid_device_uri(raid)),
                "system_id":
                Equals(node.system_id),
            }))
        self.assertItemsEqual(block_device_ids + partitions_ids,
                              parsed_device_ids)
        self.assertItemsEqual(spare_block_device_ids + spare_partitions_ids,
                              parsed_spare_device_ids)
        self.assertEqual(raid.virtual_device.id,
                         parsed_raid["virtual_device"]["id"])
Ejemplo n.º 24
0
 def test_compose_enlistment_preseed(self):
     rack_controller = factory.make_RackController()
     url = factory.make_simple_http_url()
     request = make_HttpRequest()
     apt_proxy = get_apt_proxy(request, rack_controller)
     preseed = yaml.safe_load(
         compose_enlistment_preseed(request, rack_controller, {
             'metadata_enlist_url': url,
             'syslog_host_port': url,
         }))
     self.assertDictEqual({'MAAS': {
         'metadata_url': url
     }}, preseed['datasource'])
     self.assertTrue(preseed['manage_etc_hosts'])
     self.assertDictEqual({'remotes': {'maas': url}}, preseed['rsyslog'])
     self.assertDictEqual(
         {
             'delay': 'now',
             'mode': 'poweroff',
             'timeout': 1800,
             'condition': 'test ! -e /tmp/block-poweroff',
         }, preseed['power_state'])
     self.assertItemsEqual([
         'python3-yaml', 'python3-oauthlib', 'freeipmi-tools', 'ipmitool',
         'sshpass', 'archdetect-deb', 'jq'
     ], preseed['packages'])
     default = PackageRepository.get_main_archive().url
     ports = PackageRepository.get_ports_archive().url
     self.assertThat(
         preseed,
         ContainsDict({
             'apt':
             ContainsDict({
                 'preserve_sources_list':
                 Equals(False),
                 'primary':
                 MatchesListwise([
                     MatchesDict({
                         "arches": Equals(["amd64", "i386"]),
                         "uri": Equals(default),
                     }),
                     MatchesDict({
                         "arches": Equals(["default"]),
                         "uri": Equals(ports),
                     }),
                 ]),
                 'proxy':
                 Equals(apt_proxy),
                 'security':
                 MatchesListwise([
                     MatchesDict({
                         "arches": Equals(["amd64", "i386"]),
                         "uri": Equals(default),
                     }),
                     MatchesDict({
                         "arches": Equals(["default"]),
                         "uri": Equals(ports),
                     }),
                 ]),
             })
         }))
Ejemplo n.º 25
0
    def test_create_port_with_vsd_floatingip(self):
        # Given I have a VSD-FloatingIP-pool
        vsd_fip_pool = self._create_vsd_floatingip_pool()

        # Given I have a VSD-L3-Managed subnet
        vsd_l3_domain, vsd_l3_subnet = self._given_vsd_l3subnet(
            cidr4=self.cidr4, cidr6=self.cidr6)
        network, subnet4, subnet6 = self._given_network_linked_to_vsd_subnet(
            vsd_l3_subnet, cidr4=self.cidr4, cidr6=self.cidr6)

        # And I have claimed a VSD-FloatingIP in the VSD-L3-Domain
        fip1 = self.nuage_client.claim_floatingip(vsd_l3_domain.id,
                                                  vsd_fip_pool['ID'])[0]
        fip2 = self.nuage_client.claim_floatingip(vsd_l3_domain.id,
                                                  vsd_fip_pool['ID'])[0]

        # When I retrieve the nuage-floatingIP-list of the OS IPv4 subnet
        fip_list = self.nuage_network_client.list_nuage_floatingip_by_subnet(
            subnet4['id'])
        # Then I expect the VSD-floatingIP in my list
        fip_present = self._check_fip_in_list(fip1['ID'], fip_list)
        self.assertTrue(fip_present,
                        msg="nuage floatingip not present in list, "
                        "while expected to be")

        # When I retrieve the nuage-floatingIP-list of the OS IPv6 subnet
        fip_list = self.nuage_network_client.list_nuage_floatingip_by_subnet(
            subnet6['id'])
        # Then I expect the VSD-floatingIP in my list
        self._check_fip_in_list(fip1['ID'], fip_list)
        self.assertTrue(fip_present,
                        msg="nuage floatingip not present in list, "
                        "while expected to be")

        self._check_fip_in_list(fip2['ID'], fip_list)
        self.assertTrue(fip_present,
                        msg="nuage floatingip not present in list, "
                        "while expected to be")

        # When I create a port in the network with FIP assigned
        kwargs = {"nuage_floatingip": {'id': fip1['ID']}}
        port = self.create_port(network, **kwargs)
        # Then this FIP is assigned to the port
        self.assertThat(port['nuage_floatingip'],
                        ContainsDict({'id': Equals(fip1['ID'])}))

        # And I associate this port to the claimed floating ip (via update)
        # self._associate_fip_to_port(port, claimed_fip[0]['ID'])

        # Then I expect the claimed floating ip in the port show response
        if not Topology.is_ml2:
            fip_present = self._check_fip_in_port_show(port['id'], fip1['ID'])
            self.assertTrue(fip_present,
                            msg="associated VSD claimed FIP (%s) not found "
                            "in port (%s)" % (fip1['ID'], port['id']))

        # When I disassociate the claimed fip from the port
        self._disassociate_fip_from_port(port)
        # Then I no longer expect the claimed floating ip in the
        # port show response
        if not Topology.is_ml2:
            fip_present = self._check_fip_in_port_show(port['id'],
                                                       fip1[0]['ID'])
            self.assertFalse(fip_present,
                             msg="disassociated VSD claimed FIP (%s) "
                             "still found in port (%s)" %
                             (fip1[0]['ID'], port['id']))
Ejemplo n.º 26
0
    def test_read(self):
        node = factory.make_Node()
        block_devices = [
            factory.make_PhysicalBlockDevice(node=node) for _ in range(3)
        ]
        block_device_ids = [bd.id for bd in block_devices]
        bd_filesystems = [
            factory.make_Filesystem(fstype=FILESYSTEM_TYPE.LVM_PV,
                                    block_device=bd) for bd in block_devices
        ]
        partitions = [
            factory.make_Partition(partition_table=factory.make_PartitionTable(
                block_device=factory.make_PhysicalBlockDevice(node=node)))
            for _ in range(3)
        ]
        partitions_ids = [partition.id for partition in partitions]
        partition_filesystems = [
            factory.make_Filesystem(fstype=FILESYSTEM_TYPE.LVM_PV,
                                    partition=partition)
            for partition in partitions
        ]
        volume_group = factory.make_FilesystemGroup(
            group_type=FILESYSTEM_GROUP_TYPE.LVM_VG,
            filesystems=bd_filesystems + partition_filesystems,
        )
        logical_volume_ids = [
            factory.make_VirtualBlockDevice(filesystem_group=volume_group,
                                            size=bd.size).id
            for bd in block_devices
        ]
        uri = get_volume_group_uri(volume_group)
        response = self.client.get(uri)

        self.assertEqual(http.client.OK, response.status_code,
                         response.content)
        parsed_volume_group = json.loads(
            response.content.decode(settings.DEFAULT_CHARSET))
        parsed_device_ids = [
            device["id"] for device in parsed_volume_group["devices"]
        ]
        parsed_logical_volume_ids = [
            lv["id"] for lv in parsed_volume_group["logical_volumes"]
        ]
        self.assertThat(
            parsed_volume_group,
            ContainsDict({
                "id":
                Equals(volume_group.id),
                "uuid":
                Equals(volume_group.uuid),
                "name":
                Equals(volume_group.name),
                "size":
                Equals(volume_group.get_size()),
                "human_size":
                Equals(human_readable_bytes(volume_group.get_size())),
                "available_size":
                Equals(volume_group.get_lvm_free_space()),
                "human_available_size":
                Equals(human_readable_bytes(
                    volume_group.get_lvm_free_space())),
                "used_size":
                Equals(volume_group.get_lvm_allocated_size()),
                "human_used_size":
                Equals(
                    human_readable_bytes(
                        volume_group.get_lvm_allocated_size())),
                "resource_uri":
                Equals(get_volume_group_uri(volume_group, test_plural=False)),
                "system_id":
                Equals(node.system_id),
            }),
        )
        self.assertItemsEqual(block_device_ids + partitions_ids,
                              parsed_device_ids)
        self.assertItemsEqual(logical_volume_ids, parsed_logical_volume_ids)
Ejemplo n.º 27
0
    def test_upload_fails_then_succeeds(self):
        """
        Some attempts to upload fail, then stop failing.
        """

        magic_path = FilePath(self.mktemp())
        magic_path.makedirs()
        relpath = "danger"

        from twisted.internet import reactor

        alice = MagicFolderNode.create(
            reactor=reactor,
            basedir=FilePath(self.mktemp()),
            folders={
                "default": {
                    "magic-path": magic_path,
                    "author-name": "alice",
                    "admin": True,
                    "poll-interval": 100,
                    "scan-interval": 100,
                }
            },
            start_folder_services=True,
        )
        self.addCleanup(alice.cleanup)

        service = alice.global_service.get_folder_service("default")

        # arrange for the "tahoe_client.create_immutable" call to fail
        # exactly twice and then succeed
        bad_stuff = Exception("bad stuff")

        def temporary_error(orig, count):
            temporary_error.remain = count

            def maybe_fail(*args, **kw):
                temporary_error.remain -= 1
                if temporary_error.remain >= 0:
                    d = Deferred()
                    d.errback(bad_stuff)
                    return d
                return orig(*args, **kw)

            return maybe_fail

        service.file_factory._uploader = service.uploader_service = wrap_frozen(
            service.uploader_service,
            upload_snapshot=temporary_error(
                service.uploader_service.upload_snapshot,
                2,
            ))

        local = magic_path.child(relpath)
        with local.open("w") as local_f:
            local_f.write(b"dummy\n" * 50)

        # simulate the update
        mf = service.file_factory.magic_file_for(local)
        yield mf.create_update()
        yield mf.when_idle()

        # status system should report our error
        self.assertThat(
            loads(alice.global_service.status_service._marshal_state()),
            ContainsDict({
                "state":
                ContainsDict({
                    "folders":
                    ContainsDict({
                        "default":
                        ContainsDict({
                            "errors":
                            AfterPreprocessing(
                                lambda errors:
                                [error["summary"] for error in errors],
                                Equals([
                                    "Error uploading danger: bad stuff",
                                    "Error uploading danger: bad stuff",
                                ]),
                            ),
                        }),
                    }),
                }),
            }))
        # ...as should the logger
        # XXX why does this only return _one_ error -- should be two!
        self.assertThat(
            self.eliot_logger.flush_tracebacks(Exception),
            AfterPreprocessing(
                lambda errors: [err["reason"] for err in errors],
                Equals([
                    bad_stuff,
                ])))
    def test_create_floatingip_with_rate_limiting(self):
        rate_limit = 10000
        # Create port
        port = self.create_port(network=self.network)

        # Associate a fip to the port
        body = self.floating_ips_client.create_floatingip(
            floating_network_id=self.ext_net_id,
            port_id=port['id'],
            fixed_ip_address=port['fixed_ips'][0]['ip_address'],
            nuage_egress_fip_rate_kbps=rate_limit)
        created_floating_ip = body['floatingip']
        self.addCleanup(self.floating_ips_client.delete_floatingip,
                        created_floating_ip['id'])
        self.assertIsNotNone(created_floating_ip['id'])

        fip_id = created_floating_ip['id']
        body = self.floating_ips_client.show_floatingip(fip_id)
        fip = body['floatingip']

        # rate_limit is in kbps now!
        self.assertThat(
            fip, ContainsDict({'nuage_ingress_fip_rate_kbps': Equals(-1)}))
        self.assertThat(
            fip,
            ContainsDict({'nuage_egress_fip_rate_kbps': Equals(rate_limit)}))

        # attribute 'nuage_fip_rate' is no longer in response
        self.assertIsNone(fip.get('nuage_fip_rate'))

        # Check vsd
        vsd_subnets = self.nuage_client.get_domain_subnet(
            None, None, by_subnet=self.subnet)
        self.assertEqual(1, len(vsd_subnets))
        vports = self.nuage_client.get_vport(constants.SUBNETWORK,
                                             vsd_subnets[0]['ID'],
                                             'externalID', port['id'])
        self.assertEqual(1, len(vports))
        qos = self.nuage_client.get_qos(constants.VPORT, vports[0]['ID'])
        if Topology.from_nuage('20.10'):
            self.assertEqual(0, len(qos))
            external_id_domain = self.nuage_client.get_vsd_external_id(
                created_floating_ip['router_id'])
            nuage_domain = self.nuage_client.get_l3domain(
                filters='externalID', filter_values=external_id_domain)
            nuage_domain_fips = self.nuage_client.get_floatingip(
                constants.DOMAIN, nuage_domain[0]['ID'])
            nuage_fip = [
                nuage_fip for nuage_fip in nuage_domain_fips
                if nuage_fip['externalID'].startswith(fip_id)
            ][0]

            associated_ingress_rate_limit = nuage_fip['ingressRateLimiterID']
            associated_egress_rate_limit = nuage_fip['egressRateLimiterID']
            # Infinity for egress expected
            self.assertIsNone(associated_egress_rate_limit)
            # Ingress:
            external_id = 'ingress_{}'.format(created_floating_ip['id'])
            ratelimiter = self.nuage_client.get_ratelimiter(external_id)
            self.assertEqual(str(float(rate_limit / 1000)),
                             ratelimiter['peakInformationRate'])
            self.assertEqual(associated_ingress_rate_limit, ratelimiter['ID'])
        else:
            self.assertEqual(1, len(qos))
            self.assertThat(
                qos[0],
                ContainsDict({
                    'externalID':
                    Equals(self.nuage_client.get_vsd_external_id(fip_id))
                }))
            self.assertThat(
                qos[0], ContainsDict({'FIPRateLimitingActive': Equals(True)}))
            self.assertThat(
                qos[0],
                ContainsDict({
                    'FIPPeakInformationRate':
                    Equals(str(float(rate_limit / 1000)))
                }))
            self.assertThat(
                qos[0], ContainsDict({'FIPPeakBurstSize': Equals(str(100))}))
            self.assertThat(
                qos[0],
                ContainsDict(
                    {'EgressFIPPeakInformationRate': Equals('INFINITY')}))
            self.assertThat(
                qos[0],
                ContainsDict({'EgressFIPPeakBurstSize': Equals(str(100))}))
Ejemplo n.º 29
0
    def test_only_autoscale_nodes_are_modified(self):
        """
        Autoscale only self-heals the nodes that it added, without touching
        any other nodes.  Assuming 1 CLB:
        1. Create two non-autoscaled servers and add them to the CLB.
        2. Wait for all servers to be on the CLB
        3. Create a scaling group with said CLB and 1 server
        4. Wait for AS server to be active and on the CLB.
        4. Delete autoscaled server and 1 non-autoscaled server from the CLB
        5. Converge
        6. Assert that the autoscaled server is put back on the CLB, the
           non-autoscaled server is left off the CLB, and the untouched
           non-autoscaled server is left on the CLB.
        """
        clb = self.helper.clbs[0]

        nodes = yield clb.list_nodes(self.rcs)
        self.assertEqual(len(nodes['nodes']), 0,
                         "There should be no nodes on the CLB yet.")

        # create the other two non-autoscaled servers - just wait until they
        # have servicenet addresses - don't bother waiting for them to be
        # active, which will take too long
        other_servers = yield self.helper.create_servers(
            self.rcs,
            2,
            wait_for=ContainsDict({
                "addresses":
                ContainsDict({
                    'private':
                    MatchesSetwise(
                        ContainsDict({"addr": MatchesRegex("(\d+\.){3}\d+")}))
                })
            }))
        # add non-autoscaled servers to the CLB
        clb_response = yield clb.add_nodes(
            self.rcs, [{
                'address': server['addresses']['private'][0]['addr'],
                'port': 8080,
                'condition': "ENABLED"
            } for server in other_servers])
        remove_non_as_node, untouch_non_as_node = clb_response['nodes']

        # set up the group and get the group's server's CLB node
        group, _ = self.helper.create_group(min_entities=1)
        yield self.helper.start_group_and_wait(group, self.rcs)

        # Should be 3 nodes now that all servers are added
        nodes = yield clb.wait_for_nodes(self.rcs,
                                         HasLength(3),
                                         timeout=timeout_default)
        as_node = [
            node for node in nodes
            if node not in (remove_non_as_node, untouch_non_as_node)
        ][0]

        # delete 1 autoscale node and 1 non-autoscale node
        yield clb.delete_nodes(self.rcs,
                               [as_node['id'], remove_non_as_node['id']])
        # There should be 1 node left
        yield clb.wait_for_nodes(self.rcs,
                                 HasLength(1),
                                 timeout=timeout_default)

        yield group.trigger_convergence(self.rcs)

        yield clb.wait_for_nodes(
            self.rcs,
            MatchesSetwise(  # means there are only these two nodes and no more
                # the untouched node should remain exactly the same
                Equals(untouch_non_as_node),
                # the AS node should have the same paramters, but not the same
                # ID since it was re-added
                ContainsDict({
                    k: Equals(v)
                    for k, v in as_node.items()
                    if k in ('address', 'port', 'weight'
                             'type', 'condition')
                })),
            timeout=timeout_default)
Ejemplo n.º 30
0
    def test_convergence_heals_two_groups_on_same_clb(self):
        """
        Autoscale puts a server back on the desired CLB, not touching any other
        autoscale servers on the same CLB.

        1. Create 2 AS groups on the same CLB, both with min 1 server,
           each group mapped to different ports
        2. Add group1's server to the CLB on group2's port and remove it from
           group1's port.
        3. Converge both groups.
        4. Group1's servers will be put back in group1's port but not removed
           from group2's port. group2 and its clb port will not be touched
        """
        clb = self.helper.clbs[0]
        group1, _ = self.helper.create_group(
            min_entities=1, use_lbs=[clb.scaling_group_spec(80)])
        group2, _ = self.helper.create_group(
            min_entities=1, use_lbs=[clb.scaling_group_spec(8080)])

        yield gatherResults([
            self.helper.start_group_and_wait(group, self.rcs)
            for group in (group1, group2)
        ])

        # assert that they're both on the CLB on the right ports
        groups = yield gatherResults(
            [group.get_servicenet_ips(self.rcs) for group in (group1, group2)])
        group1_ip, group2_ip = [g.values()[0] for g in groups]
        expected_nodes = [
            ContainsDict({
                'port': Equals(80),
                'address': Equals(group1_ip)
            }),
            ContainsDict({
                'port': Equals(8080),
                'address': Equals(group1_ip)
            }),
            ContainsDict({
                'port': Equals(8080),
                'address': Equals(group2_ip)
            })
        ]
        nodes = yield clb.wait_for_nodes(self.rcs,
                                         MatchesSetwise(
                                             expected_nodes[0],
                                             expected_nodes[2]),
                                         timeout=timeout_default)

        # add/remove another node
        perturb = [
            clb.add_nodes(self.rcs, [{
                'address': group1_ip,
                'port': 8080,
                'condition': 'ENABLED',
                'type': 'PRIMARY'
            }]),
            clb.delete_nodes(
                self.rcs,
                [n['id'] for n in nodes if n['address'] == group1_ip])
        ]
        yield gatherResults(perturb)
        yield clb.wait_for_nodes(self.rcs,
                                 MatchesSetwise(*expected_nodes[1:]),
                                 timeout=timeout_default)

        # Trigger convergence on both groups
        yield gatherResults([
            group.trigger_convergence(self.rcs) for group in (group1, group2)
        ])

        # Group1 server be back and group2 will have both
        yield clb.wait_for_nodes(self.rcs,
                                 MatchesSetwise(*expected_nodes),
                                 timeout=timeout_default)