예제 #1
0
 def test__deals_fine_with_whitespace_in_filesystem_value(self):
     secret = self.write_secret()
     write_text_file(
         security.get_shared_secret_filesystem_path(),
         " %s\n" % security.to_hex(secret),
     )
     self.assertEqual(secret, security.get_shared_secret_from_filesystem())
예제 #2
0
 def test_uses_given_encoding(self):
     path = self.make_file()
     # Test input: "registered trademark" (ringed R) symbol.
     text = "\xae"
     write_text_file(path, text, encoding="utf-16")
     with open(path, "r", encoding="utf-16") as fd:
         self.assertThat(fd.read(), Equals(text))
예제 #3
0
 def test_defaults_to_utf8(self):
     path = self.make_file()
     # Test input: "registered trademark" (ringed R) symbol.
     text = '\xae'
     write_text_file(path, text)
     with open(path, "r", encoding="utf-8") as fd:
         self.assertThat(fd.read(), Equals(text))
예제 #4
0
def run(args):
    """Install a GRUB2 pre-boot loader config into the TFTP
    directory structure.
    """
    with ClusterConfiguration.open() as config:
        if not os.path.exists(config.grub_root):
            os.makedirs(config.grub_root)
        destination_file = os.path.join(config.grub_root, "grub.cfg")
    write_text_file(destination_file, CONFIG_FILE)
예제 #5
0
    def make_simplestreams_product_index(
        self,
        index_dir,
        stream,
        product,
        image_spec,
        os_release,
        download_file,
        contents,
        version,
    ):
        """Write a fake Simplestreams product index file.

        The image is written into the directory that holds the indexes.  It
        contains one downloadable file, as specified by the arguments.
        """
        assert isinstance(contents, bytes), repr(contents)
        index = {
            "format": "products:1.0",
            "data-type": "image-ids",
            "updated": "Tue, 25 Mar 2014 16:19:49 +0000",
            "content_id": stream,
            "products": {
                product: {
                    "versions": {
                        version: {
                            "items": {
                                "boot-kernel": {
                                    "ftype": "boot-kernel",
                                    "_fake": "fake-data: %s" % download_file,
                                    "version": os_release,
                                    "release": image_spec.release,
                                    "path": download_file,
                                    "sha256": checksum_sha256(contents),
                                    "arch": image_spec.arch,
                                    "subarches": image_spec.subarch,
                                    "size": len(contents),
                                }
                            }
                        }
                    },
                    "subarch": image_spec.subarch,
                    "krel": image_spec.release,
                    "label": image_spec.label,
                    "kflavor": image_spec.kflavor,
                    "version": os_release,
                    "subarches": [image_spec.subarch],
                    "release": image_spec.release,
                    "arch": image_spec.arch,
                    "os": image_spec.os,
                }
            },
        }
        write_text_file(os.path.join(index_dir, "%s.json" % stream),
                        json.dumps(index))
예제 #6
0
    def test_creates_real_fresh_directory(self):
        stored_text = factory.make_string()
        filename = factory.make_name("test-file")
        with tempdir() as directory:
            self.assertThat(directory, DirExists())
            write_text_file(os.path.join(directory, filename), stored_text)
            retrieved_text = read_text_file(os.path.join(directory, filename))
            files = os.listdir(directory)

        self.assertEqual(stored_text, retrieved_text)
        self.assertEqual([filename], files)
예제 #7
0
    def make_simplestreams_product_index(self, index_dir, stream, product,
                                         image_spec, os_release, download_file,
                                         contents, version):
        """Write a fake Simplestreams product index file.

        The image is written into the directory that holds the indexes.  It
        contains one downloadable file, as specified by the arguments.
        """
        assert isinstance(contents, bytes), repr(contents)
        index = {
            'format': 'products:1.0',
            'data-type': 'image-ids',
            'updated': 'Tue, 25 Mar 2014 16:19:49 +0000',
            'content_id': stream,
            'products': {
                product: {
                    'versions': {
                        version: {
                            'items': {
                                'boot-kernel': {
                                    'ftype': 'boot-kernel',
                                    '_fake': 'fake-data: %s' % download_file,
                                    'version': os_release,
                                    'release': image_spec.release,
                                    'path': download_file,
                                    'sha256': checksum_sha256(contents),
                                    'arch': image_spec.arch,
                                    'subarches': image_spec.subarch,
                                    'size': len(contents),
                                },
                            },
                        },
                    },
                    'subarch': image_spec.subarch,
                    'krel': image_spec.release,
                    'label': image_spec.label,
                    'kflavor': image_spec.kflavor,
                    'version': os_release,
                    'subarches': [image_spec.subarch],
                    'release': image_spec.release,
                    'arch': image_spec.arch,
                    'os': image_spec.os,
                },
            },
        }
        write_text_file(os.path.join(index_dir, '%s.json' % stream),
                        json.dumps(index))
예제 #8
0
def set_shared_secret_on_filesystem(secret):
    """Write the secret to the filesystem.

    `get_shared_secret_filesystem_path` defines where the file will be
    written. If the directory does not already exist, this will attempt to
    create it, including all parent directories.

    :type secret: A byte string of arbitrary length.
    """
    secret_path = get_shared_secret_filesystem_path()
    makedirs(dirname(secret_path), exist_ok=True)
    secret_hex = to_hex(secret)
    with FileLock(secret_path).wait(10):
        # Ensure that the file has sensible permissions.
        with open(secret_path, "ab") as secret_f:
            fchmod(secret_f.fileno(), 0o640)
        # Write secret to the filesystem.
        write_text_file(secret_path, secret_hex)
예제 #9
0
 def make_simplestreams_index(self, index_dir, stream, product):
     """Write a fake simplestreams index file.  Return its path."""
     index_file = os.path.join(index_dir, "index.json")
     index = {
         "format": "index:1.0",
         "updated": "Tue, 25 Mar 2014 16:19:49 +0000",
         "index": {
             stream: {
                 "datatype": "image-ids",
                 "path": "streams/v1/%s.json" % stream,
                 "updated": "Tue, 25 Mar 2014 16:19:49 +0000",
                 "format": "products:1.0",
                 "products": [product],
             }
         },
     }
     write_text_file(index_file, json.dumps(index))
     return index_file
예제 #10
0
 def make_simplestreams_index(self, index_dir, stream, product):
     """Write a fake simplestreams index file.  Return its path."""
     index_file = os.path.join(index_dir, 'index.json')
     index = {
         'format': 'index:1.0',
         'updated': 'Tue, 25 Mar 2014 16:19:49 +0000',
         'index': {
             stream: {
                 'datatype': 'image-ids',
                 'path': 'streams/v1/%s.json' % stream,
                 'updated': 'Tue, 25 Mar 2014 16:19:49 +0000',
                 'format': 'products:1.0',
                 'products': [product],
             },
         },
     }
     write_text_file(index_file, json.dumps(index))
     return index_file
예제 #11
0
 def test__errors_when_database_and_filesystem_values_differ(self):
     security.get_shared_secret()  # Ensure that the directory exists.
     Config.objects.set_config("rpc_shared_secret", "666f6f")
     write_text_file(security.get_shared_secret_filesystem_path(), "626172")
     self.assertRaises(AssertionError, security.get_shared_secret)
예제 #12
0
 def test__errors_when_filesystem_value_cannot_be_decoded(self):
     self.write_secret()
     write_text_file(security.get_shared_secret_filesystem_path(), "_")
     self.assertRaises(binascii.Error,
                       security.get_shared_secret_from_filesystem)
예제 #13
0
 def write_secret(self):
     secret = factory.make_bytes()
     secret_path = security.get_shared_secret_filesystem_path()
     makedirs(dirname(secret_path), exist_ok=True)
     write_text_file(secret_path, security.to_hex(secret))
     return secret
예제 #14
0
 def test_overwrites_file(self):
     path = self.make_file(contents="original text")
     text = factory.make_string()
     write_text_file(path, text)
     self.assertThat(path, FileContains(text, encoding="ascii"))
예제 #15
0
 def test_creates_file(self):
     path = os.path.join(self.make_dir(), factory.make_name("text"))
     text = factory.make_string()
     write_text_file(path, text)
     self.assertThat(path, FileContains(text, encoding="ascii"))