Example #1
0
 def test_calculates_subpath_with_parent_subpath_with_trailing_slash(self):
     subpath = KubernetesVolumeBuilder.calculate_subpath(
         '/prefix/1/foo', '/prefix/1/', 'basedir')
     self.assertEqual('basedir/foo', subpath)
Example #2
0
 def setUp(self):
     self.volume_builder = KubernetesVolumeBuilder()
Example #3
0
 def test_calculates_subpath(self):
     subpath = KubernetesVolumeBuilder.calculate_subpath(
         '/prefix/1/foo', '/prefix/1', None)
     self.assertEqual('foo', subpath)
Example #4
0
class KubernetesVolumeBuilderTestCase(TestCase):
    def setUp(self):
        self.volume_builder = KubernetesVolumeBuilder()

    def test_finds_persistent_volume(self):
        self.volume_builder.add_persistent_volume_entry(
            '/prefix/1', None, 'claim1')
        self.assertIsNotNone(
            self.volume_builder.find_persistent_volume('/prefix/1f'))
        self.assertIsNone(
            self.volume_builder.find_persistent_volume('/notfound'))

    def test_calculates_subpath(self):
        subpath = KubernetesVolumeBuilder.calculate_subpath(
            '/prefix/1/foo', '/prefix/1', None)
        self.assertEqual('foo', subpath)

    def test_calculates_subpath_with_parent_subpath(self):
        subpath = KubernetesVolumeBuilder.calculate_subpath(
            '/prefix/1/foo', '/prefix/1', 'basedir')
        self.assertEqual('basedir/foo', subpath)

    def test_calculates_subpath_with_parent_subpath_with_trailing_slash(self):
        subpath = KubernetesVolumeBuilder.calculate_subpath(
            '/prefix/1/foo', '/prefix/1/', 'basedir')
        self.assertEqual('basedir/foo', subpath)

    def test_add_rw_volume_binding(self):
        self.assertEqual(0, len(self.volume_builder.volumes))
        self.volume_builder.add_persistent_volume_entry(
            '/prefix/1', None, 'claim1')
        self.assertEqual(
            {
                'name': 'claim1',
                'persistentVolumeClaim': {
                    'claimName': 'claim1'
                }
            }, self.volume_builder.volumes[0])

        self.assertEqual(0, len(self.volume_builder.volume_mounts))
        self.volume_builder.add_volume_binding('/prefix/1/input1',
                                               '/input1-target', True)
        self.assertEqual(
            {
                'name': 'claim1',
                'mountPath': '/input1-target',
                'readOnly': False,
                'subPath': 'input1'
            }, self.volume_builder.volume_mounts[0])

    def test_add_ro_volume_binding(self):
        # read-only
        self.assertEqual(0, len(self.volume_builder.volumes))
        self.volume_builder.add_persistent_volume_entry(
            '/prefix/2', None, 'claim2')
        self.assertEqual(
            {
                'name': 'claim2',
                'persistentVolumeClaim': {
                    'claimName': 'claim2'
                }
            }, self.volume_builder.volumes[0])

        self.assertEqual(0, len(self.volume_builder.volume_mounts))
        self.volume_builder.add_volume_binding('/prefix/2/input2',
                                               '/input2-target', False)
        self.assertEqual(
            {
                'name': 'claim2',
                'mountPath': '/input2-target',
                'readOnly': True,
                'subPath': 'input2'
            }, self.volume_builder.volume_mounts[0])

    def test_volume_binding_exception_if_not_found(self):
        self.assertEqual(0, len(self.volume_builder.volumes))
        with self.assertRaises(VolumeBuilderException) as context:
            self.volume_builder.add_volume_binding('/prefix/2/input2',
                                                   '/input2-target', False)
        self.assertIn('Could not find a persistent volume',
                      str(context.exception))

    def test_add_emptydir_volume(self):
        self.assertEqual(0, len(self.volume_builder.emptydir_volume_names))
        self.volume_builder.add_emptydir_volume('empty-volume')
        self.assertIn('empty-volume',
                      self.volume_builder.emptydir_volume_names)

    def test_add_emptydir_volume_binding(self):
        self.volume_builder.add_emptydir_volume('empty-volume')
        self.volume_builder.add_emptydir_volume_binding(
            'empty-volume', '/path/to/empty')
        expected = {'name': 'empty-volume', 'mountPath': '/path/to/empty'}
        self.assertIn(expected, self.volume_builder.volume_mounts)

    def test_add_emptydir_volume_binding_exception_if_not_found(self):
        self.assertEqual(0, len(self.volume_builder.emptydir_volume_names))
        with self.assertRaises(VolumeBuilderException) as context:
            self.volume_builder.add_emptydir_volume_binding(
                'empty-volume', '/path/to/empty')

    @patch('calrissian.job.KubernetesPodVolumeInspector')
    def test_add_persistent_volume_entries_from_pod(
            self, mock_kubernetes_pod_inspector):
        mock_kubernetes_pod_inspector.return_value.get_mounted_persistent_volumes.return_value = [
            ('/tmp/data1', None, 'data1-claim'),
            ('/tmp/data2', '/basedir', 'data2-claim'),
        ]

        self.volume_builder.add_persistent_volume_entries_from_pod(
            'some-pod-data')

        pv_entries = self.volume_builder.persistent_volume_entries
        self.assertEqual(pv_entries.keys(), set(['/tmp/data1', '/tmp/data2']))
        expected_entry1 = {
            'prefix': '/tmp/data1',
            'subPath': None,
            'volume': {
                'name': 'data1-claim',
                'persistentVolumeClaim': {
                    'claimName': 'data1-claim'
                }
            }
        }
        expected_entry2 = {
            'prefix': '/tmp/data2',
            'subPath': '/basedir',
            'volume': {
                'name': 'data2-claim',
                'persistentVolumeClaim': {
                    'claimName': 'data2-claim'
                }
            }
        }
        self.assertEqual(pv_entries['/tmp/data1'], expected_entry1)
        self.assertEqual(pv_entries['/tmp/data2'], expected_entry2)
        volumes = self.volume_builder.volumes
        self.assertEqual(len(volumes), 2)
        self.assertEqual(volumes[0], expected_entry1['volume'])
        self.assertEqual(volumes[1], expected_entry2['volume'])