コード例 #1
0
    def test_single_cert_to_empty_existing_ca_file(self):
        """Test adding a single certificate to the trusted CAs
        when existing ca-certificates.conf is empty"""
        cert = "CERT1\nLINE2\nLINE3"

        expected = "cloud-init-ca-certs.crt\n"

        self.m_stat.return_value.st_size = 0

        for distro_name in cc_ca_certs.distros:
            conf = cc_ca_certs._distro_ca_certs_configs(distro_name)
            with mock.patch.object(
                util, "write_file", autospec=True
            ) as m_write:

                cc_ca_certs.add_ca_certs(conf, [cert])

                m_write.assert_has_calls(
                    [mock.call(conf["ca_cert_full_path"], cert, mode=0o644)]
                )
                if conf["ca_cert_config"] is not None:
                    m_write.assert_has_calls(
                        [
                            mock.call(
                                conf["ca_cert_config"], expected, omode="wb"
                            )
                        ]
                    )
コード例 #2
0
    def test_single_cert_no_trailing_cr(self):
        """Test adding a single certificate to the trusted CAs
        when existing ca-certificates has no trailing newline"""
        cert = "CERT1\nLINE2\nLINE3"

        ca_certs_content = "line1\nline2\nline3"

        with ExitStack() as mocks:
            mock_write = mocks.enter_context(
                mock.patch.object(util, 'write_file'))
            mock_load = mocks.enter_context(
                mock.patch.object(util, 'load_file',
                                  return_value=ca_certs_content))

            cc_ca_certs.add_ca_certs([cert])

            mock_write.assert_has_calls([
                mock.call("/usr/share/ca-certificates/cloud-init-ca-certs.crt",
                          cert, mode=0o644),
                mock.call("/etc/ca-certificates.conf",
                          "%s\n%s\n" % (ca_certs_content,
                                        "cloud-init-ca-certs.crt"),
                          omode="wb")])

            mock_load.assert_called_once_with("/etc/ca-certificates.conf")
コード例 #3
0
    def test_multiple_certs(self):
        """Test adding multiple certificates to the trusted CAs."""
        certs = ["CERT1\nLINE2\nLINE3", "CERT2\nLINE2\nLINE3"]
        expected_cert_file = "\n".join(certs)
        ca_certs_content = "line1\nline2\nline3"

        with ExitStack() as mocks:
            mock_write = mocks.enter_context(
                mock.patch.object(util, 'write_file'))
            mock_load = mocks.enter_context(
                mock.patch.object(util, 'load_file',
                                  return_value=ca_certs_content))

            cc_ca_certs.add_ca_certs(certs)

            mock_write.assert_has_calls([
                mock.call("/usr/share/ca-certificates/cloud-init-ca-certs.crt",
                          expected_cert_file, mode=0o644),
                mock.call("/etc/ca-certificates.conf",
                          "%s\n%s\n" % (ca_certs_content,
                                        "cloud-init-ca-certs.crt"),
                          omode='wb'),
                ])

            mock_load.assert_called_once_with("/etc/ca-certificates.conf")
コード例 #4
0
    def test_multiple_certs(self):
        """Test adding multiple certificates to the trusted CAs."""
        certs = ["CERT1\nLINE2\nLINE3", "CERT2\nLINE2\nLINE3"]
        expected_cert_file = "\n".join(certs)
        ca_certs_content = "line1\nline2\nline3"

        self.m_stat.return_value.st_size = 1

        for distro_name in cc_ca_certs.distros:
            conf = cc_ca_certs._distro_ca_certs_configs(distro_name)

            with ExitStack() as mocks:
                mock_write = mocks.enter_context(
                    mock.patch.object(util, 'write_file'))
                mock_load = mocks.enter_context(
                    mock.patch.object(util,
                                      'load_file',
                                      return_value=ca_certs_content))

                cc_ca_certs.add_ca_certs(conf, certs)

                mock_write.assert_has_calls([
                    mock.call(conf['ca_cert_full_path'],
                              expected_cert_file,
                              mode=0o644)
                ])
                if conf['ca_cert_config'] is not None:
                    mock_write.assert_has_calls([
                        mock.call(conf['ca_cert_config'],
                                  "%s\n%s\n" %
                                  (ca_certs_content, conf['ca_cert_filename']),
                                  omode='wb')
                    ])

                    mock_load.assert_called_once_with(conf['ca_cert_config'])
コード例 #5
0
    def test_multiple_certs(self):
        """Test adding multiple certificates to the trusted CAs."""
        certs = ["CERT1\nLINE2\nLINE3", "CERT2\nLINE2\nLINE3"]
        expected_cert_file = "\n".join(certs)
        ca_certs_content = "line1\nline2\nline3"

        with ExitStack() as mocks:
            mock_write = mocks.enter_context(
                mock.patch.object(util, 'write_file'))
            mock_load = mocks.enter_context(
                mock.patch.object(util,
                                  'load_file',
                                  return_value=ca_certs_content))

            cc_ca_certs.add_ca_certs(certs)

            mock_write.assert_has_calls([
                mock.call("/usr/share/ca-certificates/cloud-init-ca-certs.crt",
                          expected_cert_file,
                          mode=0o644),
                mock.call("/etc/ca-certificates.conf",
                          "%s\n%s\n" %
                          (ca_certs_content, "cloud-init-ca-certs.crt"),
                          omode='wb')
            ])

            mock_load.assert_called_once_with("/etc/ca-certificates.conf")
コード例 #6
0
    def test_single_cert_trailing_cr(self):
        """Test adding a single certificate to the trusted CAs
        when existing ca-certificates has trailing newline"""
        cert = "CERT1\nLINE2\nLINE3"

        ca_certs_content = "line1\nline2\ncloud-init-ca-certs.crt\nline3\n"
        expected = "line1\nline2\nline3\ncloud-init-ca-certs.crt\n"

        self.m_stat.return_value.st_size = 1

        for distro_name in cc_ca_certs.distros:
            conf = cc_ca_certs._distro_ca_certs_configs(distro_name)

            with ExitStack() as mocks:
                mock_write = mocks.enter_context(
                    mock.patch.object(util, "write_file"))
                mock_load = mocks.enter_context(
                    mock.patch.object(util,
                                      "load_file",
                                      return_value=ca_certs_content))

                cc_ca_certs.add_ca_certs(conf, [cert])

                mock_write.assert_has_calls(
                    [mock.call(conf["ca_cert_full_path"], cert, mode=0o644)])
                if conf["ca_cert_config"] is not None:
                    mock_write.assert_has_calls([
                        mock.call(conf["ca_cert_config"], expected, omode="wb")
                    ])
                    mock_load.assert_called_once_with(conf["ca_cert_config"])
コード例 #7
0
    def test_single_cert_no_trailing_cr(self):
        """Test adding a single certificate to the trusted CAs
        when existing ca-certificates has no trailing newline"""
        cert = "CERT1\nLINE2\nLINE3"

        ca_certs_content = "line1\nline2\nline3"

        self.m_stat.return_value.st_size = 1

        for distro_name in cc_ca_certs.distros:
            conf = cc_ca_certs._distro_ca_certs_configs(distro_name)

            with ExitStack() as mocks:
                mock_write = mocks.enter_context(
                    mock.patch.object(util, 'write_file'))
                mock_load = mocks.enter_context(
                    mock.patch.object(util,
                                      'load_file',
                                      return_value=ca_certs_content))

                cc_ca_certs.add_ca_certs(conf, [cert])

                mock_write.assert_has_calls(
                    [mock.call(conf['ca_cert_full_path'], cert, mode=0o644)])
                if conf['ca_cert_config'] is not None:
                    mock_write.assert_has_calls([
                        mock.call(conf['ca_cert_config'],
                                  "%s\n%s\n" %
                                  (ca_certs_content, conf['ca_cert_filename']),
                                  omode="wb")
                    ])

                    mock_load.assert_called_once_with(conf['ca_cert_config'])
コード例 #8
0
 def test_no_certs_in_list(self):
     """Test that no certificate are written if not provided."""
     for distro_name in cc_ca_certs.distros:
         conf = cc_ca_certs._distro_ca_certs_configs(distro_name)
         with mock.patch.object(util, 'write_file') as mockobj:
             cc_ca_certs.add_ca_certs(conf, [])
         self.assertEqual(mockobj.call_count, 0)
コード例 #9
0
    def test_single_cert_no_trailing_cr(self):
        """Test adding a single certificate to the trusted CAs
        when existing ca-certificates has no trailing newline"""
        cert = "CERT1\nLINE2\nLINE3"

        ca_certs_content = "line1\nline2\nline3"

        with ExitStack() as mocks:
            mock_write = mocks.enter_context(
                mock.patch.object(util, 'write_file'))
            mock_load = mocks.enter_context(
                mock.patch.object(util, 'load_file',
                                  return_value=ca_certs_content))

            cc_ca_certs.add_ca_certs([cert])

            mock_write.assert_has_calls([
                mock.call("/usr/share/ca-certificates/cloud-init-ca-certs.crt",
                          cert, mode=0o644),
                mock.call("/etc/ca-certificates.conf",
                          "%s\n%s\n" % (ca_certs_content,
                                        "cloud-init-ca-certs.crt"),
                          omode="wb")])

            mock_load.assert_called_once_with("/etc/ca-certificates.conf")
コード例 #10
0
    def test_single_cert_to_empty_existing_ca_file(self):
        """Test adding a single certificate to the trusted CAs
        when existing ca-certificates.conf is empty"""
        cert = "CERT1\nLINE2\nLINE3"

        expected = "cloud-init-ca-certs.crt\n"

        with mock.patch.object(util, 'write_file', autospec=True) as m_write:
            self.m_stat.return_value.st_size = 0

            cc_ca_certs.add_ca_certs([cert])

            m_write.assert_has_calls([
                mock.call("/usr/share/ca-certificates/cloud-init-ca-certs.crt",
                          cert,
                          mode=0o644),
                mock.call("/etc/ca-certificates.conf", expected, omode="wb")
            ])
コード例 #11
0
    def test_multiple_certs(self):
        """Test adding multiple certificates to the trusted CAs."""
        certs = ["CERT1\nLINE2\nLINE3", "CERT2\nLINE2\nLINE3"]
        expected_cert_file = "\n".join(certs)

        mock_write = self.mocker.replace(util.write_file, passthrough=False)
        mock_load = self.mocker.replace(util.load_file, passthrough=False)

        mock_write("/usr/share/ca-certificates/cloud-init-ca-certs.crt",
                   expected_cert_file, mode=0644)

        ca_certs_content = "line1\nline2\nline3"
        mock_load("/etc/ca-certificates.conf")
        self.mocker.result(ca_certs_content)

        out = "%s\n%s\n" % (ca_certs_content, "cloud-init-ca-certs.crt")
        mock_write("/etc/ca-certificates.conf", out, omode="wb")

        self.mocker.replay()

        cc_ca_certs.add_ca_certs(certs)
コード例 #12
0
    def test_single_cert_trailing_cr(self):
        """Test adding a single certificate to the trusted CAs
        when existing ca-certificates has trailing newline"""
        cert = "CERT1\nLINE2\nLINE3"

        ca_certs_content = "line1\nline2\ncloud-init-ca-certs.crt\nline3\n"
        expected = "line1\nline2\nline3\ncloud-init-ca-certs.crt\n"

        mock_write = self.mocker.replace(util.write_file, passthrough=False)
        mock_load = self.mocker.replace(util.load_file, passthrough=False)

        mock_write("/usr/share/ca-certificates/cloud-init-ca-certs.crt",
                   cert, mode=0644)

        mock_load("/etc/ca-certificates.conf")
        self.mocker.result(ca_certs_content)

        mock_write("/etc/ca-certificates.conf", expected, omode="wb")
        self.mocker.replay()

        cc_ca_certs.add_ca_certs([cert])
コード例 #13
0
    def test_multiple_certs(self):
        """Test adding multiple certificates to the trusted CAs."""
        certs = ["CERT1\nLINE2\nLINE3", "CERT2\nLINE2\nLINE3"]
        expected_cert_file = "\n".join(certs)

        mock_write = self.mocker.replace(util.write_file, passthrough=False)
        mock_load = self.mocker.replace(util.load_file, passthrough=False)

        mock_write("/usr/share/ca-certificates/cloud-init-ca-certs.crt",
                   expected_cert_file,
                   mode=0644)

        ca_certs_content = "line1\nline2\nline3"
        mock_load("/etc/ca-certificates.conf")
        self.mocker.result(ca_certs_content)

        out = "%s\n%s\n" % (ca_certs_content, "cloud-init-ca-certs.crt")
        mock_write("/etc/ca-certificates.conf", out, omode="wb")

        self.mocker.replay()

        cc_ca_certs.add_ca_certs(certs)
コード例 #14
0
    def test_single_cert_trailing_cr(self):
        """Test adding a single certificate to the trusted CAs
        when existing ca-certificates has trailing newline"""
        cert = "CERT1\nLINE2\nLINE3"

        ca_certs_content = "line1\nline2\ncloud-init-ca-certs.crt\nline3\n"
        expected = "line1\nline2\nline3\ncloud-init-ca-certs.crt\n"

        mock_write = self.mocker.replace(util.write_file, passthrough=False)
        mock_load = self.mocker.replace(util.load_file, passthrough=False)

        mock_write("/usr/share/ca-certificates/cloud-init-ca-certs.crt",
                   cert,
                   mode=0644)

        mock_load("/etc/ca-certificates.conf")
        self.mocker.result(ca_certs_content)

        mock_write("/etc/ca-certificates.conf", expected, omode="wb")
        self.mocker.replay()

        cc_ca_certs.add_ca_certs([cert])
コード例 #15
0
 def test_no_certs_in_list(self):
     """Test that no certificate are written if not provided."""
     self.mocker.replace(util.write_file, passthrough=False)
     self.mocker.replay()
     cc_ca_certs.add_ca_certs([])
コード例 #16
0
 def test_no_certs_in_list(self):
     """Test that no certificate are written if not provided."""
     with mock.patch.object(util, 'write_file') as mockobj:
         cc_ca_certs.add_ca_certs([])
     self.assertEqual(mockobj.call_count, 0)
コード例 #17
0
 def test_no_certs_in_list(self):
     """Test that no certificate are written if not provided."""
     self.mocker.replace(util.write_file, passthrough=False)
     self.mocker.replay()
     cc_ca_certs.add_ca_certs([])
コード例 #18
0
 def test_no_certs_in_list(self):
     """Test that no certificate are written if not provided."""
     with mock.patch.object(util, 'write_file') as mockobj:
         cc_ca_certs.add_ca_certs([])
     self.assertEqual(mockobj.call_count, 0)