~gnuoy/charm-helpers/bug-1605184

« back to all changes in this revision

Viewing changes to tests/contrib/hahelpers/test_apache_utils.py

  • Committer: Liam Young
  • Date: 2016-07-21 12:37:00 UTC
  • Revision ID: liam.young@canonical.com-20160721123700-8n90mlb0jjraong3
Only write out CA cert if it has changed

Only write out CA cert and run the update-ca-certificates if the cert has
actually changed. This reduces the risk of certs being pulled from under services
which are trying to do client side certificate validation on remote https
endpoints.

Show diffs side-by-side

added added

removed removed

Lines of Context:
110
110
        self.assertEquals('keystone_provided_ca',
111
111
                          result)
112
112
 
113
 
    def test_install_ca_cert(self):
 
113
    @patch.object(apache_utils.os.path, 'isfile')
 
114
    def test_retrieve_ca_cert(self, _isfile):
 
115
        _isfile.return_value = True
 
116
        with patch_open() as (_open, _file):
 
117
            _file.read.return_value = cert
 
118
            self.assertEqual(
 
119
                apache_utils.retrieve_ca_cert('mycertfile'),
 
120
                cert)
 
121
            _open.assert_called_once_with('mycertfile', 'r')
 
122
 
 
123
    @patch.object(apache_utils.os.path, 'isfile')
 
124
    def test_retrieve_ca_cert_no_file(self, _isfile):
 
125
        _isfile.return_value = False
 
126
        with patch_open() as (_open, _file):
 
127
            self.assertEqual(
 
128
                apache_utils.retrieve_ca_cert('mycertfile'),
 
129
                None)
 
130
            self.assertFalse(_open.called)
 
131
 
 
132
    @patch.object(apache_utils, 'retrieve_ca_cert')
 
133
    def test_install_ca_cert_new_cert(self, _retrieve_ca_cert):
 
134
        _retrieve_ca_cert.return_value = None
114
135
        with patch_open() as (_open, _file):
115
136
            apache_utils.install_ca_cert(cert)
116
 
            _open.assert_called_with('/usr/local/share/ca-certificates/keystone_juju_ca_cert.crt', 'w')
 
137
            _open.assert_called_once_with(
 
138
                '/usr/local/share/ca-certificates/keystone_juju_ca_cert.crt',
 
139
                'w')
117
140
            _file.write.assert_called_with(cert)
118
 
        self.subprocess.check_call.assert_called_with(['update-ca-certificates', '--fresh'])
 
141
        self.subprocess.check_call.assert_called_with(
 
142
            ['update-ca-certificates', '--fresh'])
 
143
 
 
144
    @patch.object(apache_utils, 'retrieve_ca_cert')
 
145
    def test_install_ca_cert_old_cert(self, _retrieve_ca_cert):
 
146
        _retrieve_ca_cert.return_value = cert
 
147
        with patch_open() as (_open, _file):
 
148
            apache_utils.install_ca_cert(cert)
 
149
            self.assertFalse(_open.called)
 
150
            self.assertFalse(_file.called)
 
151
        self.assertFalse(self.subprocess.check_call.called)