~peter-sabaini/charm-helpers/bcache-helpers

« back to all changes in this revision

Viewing changes to tests/contrib/openstack/test_os_contexts.py

  • Committer: james.page at ubuntu
  • Date: 2017-04-26 09:48:47 UTC
  • mfrom: (737.1.3 fix-wsgi-workers)
  • Revision ID: james.page@ubuntu.com-20170426094847-cyehqgpxlv9i91rl
Refactor worker multiplier code.

Ensure WSGI processes are capped by default when running in containers.

Skew public processes in favour of admin processes.

Show diffs side-by-side

added added

removed removed

Lines of Context:
2604
2604
        }
2605
2605
        self.assertEquals(result, expected)
2606
2606
 
2607
 
    @patch.object(context, 'psutil')
 
2607
    @patch.object(context, '_calculate_workers')
2608
2608
    @patch.object(context, 'git_determine_python_path')
2609
2609
    @patch.object(context, 'git_determine_usr_bin')
2610
 
    def test_wsgi_worker_config_context(self, usr_bin, python_path, psutil):
 
2610
    def test_wsgi_worker_config_context(self, usr_bin, python_path,
 
2611
                                        _calculate_workers):
2611
2612
        self.config.return_value = 2  # worker-multiplier=2
2612
2613
        usr_bin_path = '/usr/bin'
2613
2614
        usr_bin.return_value = usr_bin_path
2614
2615
        python_path.return_value = None
2615
 
        psutil.cpu_count.return_value = 4
 
2616
        _calculate_workers.return_value = 8
2616
2617
        service_name = 'service-name'
2617
2618
        script = '/usr/bin/script'
2618
2619
        ctxt = context.WSGIWorkerConfigContext(name=service_name,
2625
2626
            "admin_script": None,
2626
2627
            "public_script": None,
2627
2628
            "processes": 8,
2628
 
            "admin_processes": 6,
2629
 
            "public_processes": 2,
 
2629
            "admin_processes": 2,
 
2630
            "public_processes": 6,
2630
2631
            "threads": 1,
2631
2632
            "usr_bin": usr_bin_path,
2632
2633
            "python_path": None,
2688
2689
                          {'notifications': 'True'})
2689
2690
 
2690
2691
    @patch.object(context, 'psutil')
2691
 
    def test_workerconfig_context_xenial(self, _psutil):
2692
 
        self.config.side_effect = fake_config({
2693
 
            'worker-multiplier': 1,
2694
 
        })
 
2692
    def test_num_cpus_xenial(self, _psutil):
2695
2693
        _psutil.cpu_count.return_value = 4
2696
 
        worker = context.WorkerConfigContext()
2697
 
        self.assertTrue(worker.num_cpus, 4)
 
2694
        self.assertTrue(context._num_cpus(), 4)
2698
2695
 
2699
2696
    @patch.object(context, 'psutil')
2700
 
    def test_workerconfig_context_trusty(self, _psutil):
2701
 
        self.config.side_effect = fake_config({
2702
 
            'worker-multiplier': 2,
2703
 
        })
 
2697
    def test_num_cpus_trusty(self, _psutil):
2704
2698
        _psutil.NUM_CPUS = 4
2705
 
        worker = context.WorkerConfigContext()
2706
 
        self.assertTrue(worker.num_cpus, 2)
 
2699
        self.assertTrue(context._num_cpus(), 4)
2707
2700
 
2708
 
    def test_workerconfig_context_float(self):
 
2701
    @patch.object(context, '_num_cpus')
 
2702
    def test_calculate_workers_float(self, _num_cpus):
2709
2703
        self.config.side_effect = fake_config({
2710
2704
            'worker-multiplier': 0.3
2711
2705
        })
2712
 
        with patch.object(context.WorkerConfigContext, 'num_cpus', 4):
2713
 
            worker = context.WorkerConfigContext()
2714
 
            self.assertTrue(worker.num_cpus, 1)
 
2706
        _num_cpus.return_value = 4
 
2707
        self.assertTrue(context._calculate_workers(), 4)
2715
2708
 
2716
 
    def test_workerconfig_context_not_quite_0(self):
 
2709
    @patch.object(context, '_num_cpus')
 
2710
    def test_calculate_workers_not_quite_0(self, _num_cpus):
2717
2711
        # Make sure that the multiplier evaluating to somewhere between
2718
2712
        # 0 and 1 in the floating point range still has at least one
2719
2713
        # worker.
2720
2714
        self.config.side_effect = fake_config({
2721
2715
            'worker-multiplier': 0.001
2722
2716
        })
2723
 
        with patch.object(context.WorkerConfigContext, 'num_cpus', 100):
2724
 
            worker = context.WorkerConfigContext()
2725
 
            self.assertTrue(worker.num_cpus, 1)
 
2717
        _num_cpus.return_value = 100
 
2718
        self.assertTrue(context._calculate_workers(), 1)
2726
2719
 
2727
 
    def test_workerconfig_context_0(self):
 
2720
    @patch.object(context, 'psutil')
 
2721
    def test_calculate_workers_0(self, _psutil):
2728
2722
        self.config.side_effect = fake_config({
2729
2723
            'worker-multiplier': 0
2730
2724
        })
2731
 
        with patch.object(context.WorkerConfigContext, 'num_cpus', 2):
2732
 
            worker = context.WorkerConfigContext()
2733
 
            self.assertTrue(worker.num_cpus, 0)
2734
 
 
2735
 
    def test_workerconfig_context_noconfig(self):
2736
 
        self.config.return_value = None
2737
 
        with patch.object(context.WorkerConfigContext, 'num_cpus', 1):
2738
 
            worker = context.WorkerConfigContext()
2739
 
            self.assertEqual({'workers': 2}, worker())
2740
 
 
2741
 
    def test_workerconfig_context_noconfig_container(self):
2742
 
        self.config.return_value = None
2743
 
        self.is_container.return_value = True
2744
 
        with patch.object(context.WorkerConfigContext, 'num_cpus', 1):
2745
 
            worker = context.WorkerConfigContext()
2746
 
            self.assertEqual({'workers': 2}, worker())
2747
 
 
2748
 
    def test_workerconfig_context_noconfig_lotsa_cpus_container(self):
2749
 
        self.config.return_value = None
2750
 
        self.is_container.return_value = True
2751
 
        with patch.object(context.WorkerConfigContext, 'num_cpus', 32):
2752
 
            worker = context.WorkerConfigContext()
2753
 
            self.assertEqual({'workers': 4}, worker())
2754
 
 
2755
 
    def test_workerconfig_context_noconfig_lotsa_cpus_not_container(self):
2756
 
        self.config.return_value = None
2757
 
        with patch.object(context.WorkerConfigContext, 'num_cpus', 32):
2758
 
            worker = context.WorkerConfigContext()
2759
 
            self.assertEqual({'workers': 64}, worker())
2760
 
 
2761
 
    def test_workerconfig_context_withconfig(self):
2762
 
        self.config.side_effect = fake_config({
2763
 
            'worker-multiplier': 4,
2764
 
        })
2765
 
        with patch.object(context.WorkerConfigContext, 'num_cpus', 2):
2766
 
            worker = context.WorkerConfigContext()
2767
 
            self.assertEqual({'workers': 8}, worker())
 
2725
        _psutil.cpu_count.return_value = 2
 
2726
        self.assertTrue(context._calculate_workers(), 0)
 
2727
 
 
2728
    @patch.object(context, '_num_cpus')
 
2729
    def test_calculate_workers_noconfig(self, _num_cpus):
 
2730
        self.config.return_value = None
 
2731
        _num_cpus.return_value = 1
 
2732
        self.assertTrue(context._calculate_workers(), 2)
 
2733
 
 
2734
    @patch.object(context, '_num_cpus')
 
2735
    def test_calculate_workers_noconfig_container(self, _num_cpus):
 
2736
        self.config.return_value = None
 
2737
        self.is_container.return_value = True
 
2738
        _num_cpus.return_value = 1
 
2739
        self.assertTrue(context._calculate_workers(), 2)
 
2740
 
 
2741
    @patch.object(context, '_num_cpus')
 
2742
    def test_calculate_workers_noconfig_lotsa_cpus_container(self,
 
2743
                                                             _num_cpus):
 
2744
        self.config.return_value = None
 
2745
        self.is_container.return_value = True
 
2746
        _num_cpus.return_value = 32
 
2747
        self.assertTrue(context._calculate_workers(), 4)
 
2748
 
 
2749
    @patch.object(context, '_num_cpus')
 
2750
    def test_calculate_workers_noconfig_lotsa_cpus_not_container(self,
 
2751
                                                                 _num_cpus):
 
2752
        self.config.return_value = None
 
2753
        _num_cpus.return_value = 32
 
2754
        self.assertTrue(context._calculate_workers(), 64)
 
2755
 
 
2756
    @patch.object(context, '_calculate_workers', return_value=256)
 
2757
    def test_worker_context(self, calculate_workers):
 
2758
        self.assertEqual(context.WorkerConfigContext()(),
 
2759
                         {'workers': 256})
2768
2760
 
2769
2761
    def test_apache_get_addresses_no_network_splits(self):
2770
2762
        self.https.return_value = True