예제 #1
0
    def test_run_and_flush(self):
        self.o = HDFWriterPart(name="m", mri="BLOCK:HDF5")

        def set_num_captured():
            # Sleep for 2.5 seconds to ensure 2 flushes, and then set value to finish
            cothread.Sleep(2.5)
            self.set_attributes(self.child,
                                numCapturedReadback=self.o.done_when_captured)

        self.o.done_when_captured = 5
        # Say that we're getting the first frame
        self.o.first_array_future = Future(None)
        self.o.first_array_future.set_result(None)
        self.o.start_future = Future(None)
        # Need a registrar object or we get AssertionError
        self.o.registrar = MagicMock()
        # Reduce frame timeout so we don't hang on this test for too long
        self.o.frame_timeout = 5
        # Spawn process to finish it after a few seconds
        self.process.spawn(set_num_captured)

        # Run
        self.o.on_run(self.context)

        # Check calls
        assert self.child.handled_requests.mock_calls == [
            call.post("flushNow"),
            call.post("flushNow"),
        ]
        assert self.o.registrar.report.called_once
        assert self.o.registrar.report.call_args_list[0][0][0].steps == 0
        assert self.o.registrar.report.call_args_list[1][0][0].steps == 5
예제 #2
0
 def do_check_sparse_output(self, user_programs=None):
     if user_programs is None:
         user_programs = [1, 8, 0, 0, 0, 1, 8, 0]
     assert self.child.handled_requests.mock_calls == [
         call.post("writeProfile",
                   csPort="CS1",
                   timeArray=[0.002],
                   userPrograms=[8]),
         call.post("executeProfile"),
         call.post("moveCS1", a=-0.1375, b=0.0, moveTime=1.0375),
         # pytest.approx to allow sensible compare with numpy arrays
         call.post(
             "writeProfile",
             a=pytest.approx([
                 -0.125, 0.625, 0.6375, 0.6375, 0.6375, 0.625, -0.125,
                 -0.1375
             ]),
             b=pytest.approx(
                 [0.0, 0.0, 0.0125, 0.05, 0.0875, 0.1, 0.1, 0.1]),
             csPort="CS1",
             timeArray=pytest.approx([
                 100000, 3000000, 100000, 100000, 100000, 100000, 3000000,
                 100000
             ]),
             userPrograms=pytest.approx(user_programs),
             # note the use of mode 2 AVERAGE_PREV_CURRENT at the end of each
             # sparse linear row
             velocityMode=pytest.approx([1, 2, 1, 1, 1, 1, 2, 3]),
         ),
     ]
     assert self.o.completed_steps_lookup == [0, 3, 3, 3, 3, 3, 6, 6]
예제 #3
0
 def test_multi_run(self):
     self.do_configure(axes_to_scan=["x"])
     assert self.o.completed_steps_lookup == ([0, 0, 1, 1, 2, 2, 3, 3])
     self.child.handled_requests.reset_mock()
     self.do_configure(axes_to_scan=["x"], completed_steps=3, x_pos=0.6375)
     assert self.child.handled_requests.mock_calls == [
         call.post("writeProfile",
                   csPort="CS1",
                   timeArray=[0.002],
                   userPrograms=[8]),
         call.post("executeProfile"),
         call.post("moveCS1", a=0.6375, moveTime=0.0),
         call.post(
             "writeProfile",
             csPort="CS1",
             a=pytest.approx(
                 [0.625, 0.5, 0.375, 0.25, 0.125, 0.0, -0.125, -0.1375]),
             timeArray=pytest.approx([
                 100000, 500000, 500000, 500000, 500000, 500000, 500000,
                 100000
             ]),
             userPrograms=pytest.approx([1, 4, 1, 4, 1, 4, 2, 8]),
             velocityMode=pytest.approx([1, 0, 0, 0, 0, 0, 1, 3]),
         ),
     ]
예제 #4
0
    def test_run_and_flush(self):
        self.o = HDFWriterPart(name="m", mri="BLOCK:HDF5")

        def set_unique_id():
            # Sleep for 2.5 seconds to ensure 2 flushes, and then set value to finish
            cothread.Sleep(2.5)
            self.set_attributes(self.child, uniqueId=self.o.done_when_reaches)

        self.o.done_when_reaches = 38
        self.o.completed_offset = 0
        # Say that we're getting the first frame
        self.o.array_future = Future(None)
        self.o.array_future.set_result(None)
        self.o.start_future = Future(None)
        self.o.registrar = MagicMock()
        self.o.frame_timeout = 60
        # Spawn process to finish it after a few seconds
        self.process.spawn(set_unique_id)
        # Run
        self.o.on_run(self.context)
        assert self.child.handled_requests.mock_calls == [
            call.post("flushNow"),
            call.post("flushNow"),
        ]
        assert self.o.registrar.report.called_once
        assert self.o.registrar.report.call_args_list[0][0][0].steps == 0
        assert self.o.registrar.report.call_args_list[1][0][0].steps == 38
예제 #5
0
    def test_configure(self):
        params = MagicMock()
        xs = LineGenerator("x", "mm", 0.0, 0.5, 3, alternate=True)
        ys = LineGenerator("y", "mm", 0.0, 0.1, 2)
        params.generator = CompoundGenerator([ys, xs], [], [])
        params.generator.prepare()
        completed_steps = 2
        steps_to_do = 4
        part_info = {"anything": [UniqueIdInfo(30)]}
        self.o.configure(self.context, completed_steps, steps_to_do, part_info,
                         params)
        expected_xml = """<?xml version="1.0" ?>
<pos_layout>
<dimensions>
<dimension name="d0" />
<dimension name="d1" />
<dimension name="FilePluginClose" />
</dimensions>
<positions>
<position FilePluginClose="0" d0="0" d1="2" />
<position FilePluginClose="0" d0="1" d1="2" />
<position FilePluginClose="0" d0="1" d1="1" />
<position FilePluginClose="1" d0="1" d1="0" />
</positions>
</pos_layout>""".replace("\n", "")
        # Need to wait for the spawned mock start call to run
        self.o.start_future.result()
        assert self.child.handled_requests.mock_calls == [
            call.post('delete'),
            call.put('enableCallbacks', True),
            call.put('idStart', 31),
            call.put('xml', expected_xml),
            call.post('start')
        ]
예제 #6
0
    def test_configure(self):
        xs = LineGenerator("x", "mm", 0.0, 0.5, 3, alternate=True)
        ys = LineGenerator("y", "mm", 0.0, 0.1, 2)
        generator = CompoundGenerator([ys, xs], [], [])
        generator.prepare()
        completed_steps = 2
        steps_to_do = 4
        self.o.done_when_reaches = 30
        self.o.on_configure(self.context, completed_steps, steps_to_do,
                            generator)
        expected_xml = """<?xml version="1.0" ?>
<pos_layout>
<dimensions>
<dimension name="d0" />
<dimension name="d1" />
</dimensions>
<positions>
<position d0="0" d1="2" />
<position d0="1" d1="2" />
<position d0="1" d1="1" />
<position d0="1" d1="0" />
</positions>
</pos_layout>""".replace("\n", "")
        # Wait for the start_future so the post gets through to our child
        # even on non-cothread systems
        self.o.start_future.result(timeout=1)
        assert self.child.handled_requests.mock_calls == [
            call.post("delete"),
            call.put("enableCallbacks", True),
            call.put("idStart", 31),
            call.put("xml", expected_xml),
            call.post("start"),
        ]
        assert self.o.done_when_reaches == 34
예제 #7
0
    def test_send_non_success_no_json(self):
        self.config['splunk_hec_url'] = 'https://splunk.url/foo'
        self.config['splunk_hec_token'] = 'stoken'

        def se_exc(*args, **kwargs):
            raise Exception('foo')

        m_resp = Mock(spec_set=requests.models.Response)
        type(m_resp).status_code = 200
        type(m_resp).text = '{"text": "Failure"}'
        type(m_resp).headers = {'H1': 'V1'}
        m_resp.json.side_effect = se_exc
        with patch('%s.requests' % pbm, autospec=True) as mock_req:
            mock_req.post.return_value = m_resp
            with pytest.raises(RuntimeError):
                self.cls._send_splunk('{"foo": "bar"}')
        assert mock_req.mock_calls == [
            call.post('https://splunk.url/foo',
                      headers={'Authorization': 'Splunk stoken'},
                      data='{"foo": "bar"}'),
            call.post().json()
        ]
        assert self.mock_logger.mock_calls == [
            call.debug('Send to Splunk (%s): %s', 'https://splunk.url/foo',
                       '{"foo": "bar"}'),
            call.debug('Splunk POST got response code %s HEADERS=%s BODY: %s',
                       200, {'H1': 'V1'}, '{"text": "Failure"}'),
            call.error('Splunk POST returned non-success response: %s',
                       {'text': '{"text": "Failure"}'})
        ]
예제 #8
0
 def test_split_in_a_long_step_lookup(self):
     self.do_configure(axes_to_scan=["x"],
                       completed_steps=3,
                       x_pos=0.62506,
                       duration=14.0)
     # Ignore the trigger reset and move to start, just look at the last call
     # which is the profile write
     assert self.child.handled_requests.mock_calls[-1] == call.post(
         "writeProfile",
         csPort="CS1",
         a=pytest.approx([
             0.625, 0.5625, 0.5, 0.4375, 0.375, 0.3125, 0.25, 0.1875, 0.125
         ]),
         timeArray=pytest.approx([
             7143,
             3500000,
             3500000,
             3500000,
             3500000,
             3500000,
             3500000,
             3500000,
             3500000,
         ]),
         userPrograms=pytest.approx([1, 0, 4, 0, 1, 0, 4, 0, 1]),
         velocityMode=pytest.approx([1, 0, 0, 0, 0, 0, 0, 0, 0]),
     )
     # The completed steps works on complete (not split) steps, so we expect
     # the last value to be the end of step 6, even though it doesn't
     # actually appear in the velocity arrays
     assert self.o.completed_steps_lookup == ([
         3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 6
     ])
     # Mock out the registrar that would have been registered when we
     # attached to a controller
     self.o.registrar = Mock()
     # Now call update step and get it to generate the next lot of points
     # scanned can be any index into completed_steps_lookup so that there
     # are less than PROFILE_POINTS left to go in it
     self.o.update_step(scanned=2, child=self.process.block_view("PMAC"))
     # Expect the rest of the points
     assert self.child.handled_requests.mock_calls[-1] == call.post(
         "writeProfile",
         a=pytest.approx(
             [0.0625, 0.0, -0.0625, -0.125, -0.12506377551020409]),
         timeArray=pytest.approx([3500000, 3500000, 3500000, 3500000,
                                  7143]),
         userPrograms=pytest.approx([0, 4, 0, 2, 8]),
         velocityMode=pytest.approx([0, 0, 0, 1, 3]),
     )
     assert self.o.registrar.report.call_count == 1
     assert self.o.registrar.report.call_args[0][0].steps == 3
     # And for the rest of the lookup table to be added
     assert self.o.completed_steps_lookup == ([
         3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6
     ])
예제 #9
0
 def test_configure_no_axes(self):
     self.set_motor_attributes()
     generator = CompoundGenerator([StaticPointGenerator(6)], [], [],
                                   duration=0.1)
     generator.prepare()
     self.o.on_configure(self.context, 0, 6, {}, generator, [])
     assert self.child.handled_requests.mock_calls == [
         call.post("writeProfile",
                   csPort="CS1",
                   timeArray=[0.002],
                   userPrograms=[8]),
         call.post("executeProfile"),
         # pytest.approx to allow sensible compare with numpy arrays
         call.post(
             "writeProfile",
             csPort="CS1",
             timeArray=pytest.approx([
                 2000,
                 50000,
                 50000,
                 50000,
                 50000,
                 50000,
                 50000,
                 50000,
                 50000,
                 50000,
                 50000,
                 50000,
                 50000,
                 2000,
             ]),
             userPrograms=pytest.approx(
                 [1, 4, 1, 4, 1, 4, 1, 4, 1, 4, 1, 4, 2, 8]),
             velocityMode=pytest.approx(
                 [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 3]),
         ),
     ]
     assert self.o.completed_steps_lookup == [
         0,
         0,
         1,
         1,
         2,
         2,
         3,
         3,
         4,
         4,
         5,
         5,
         6,
         6,
     ]
    def test_configure_from_armed_but_not_continuous_state(self):
        # Set the attributes to appear already armed in wrong image mode
        self.set_attributes(self.child, acquiring=True, imageMode="Single")

        self._do_configure()

        assert self.child.handled_requests.mock_calls == [
            call.post("stop"),
            call.when_value_matches("acquiring", False, None),
            call.put("imageMode", "Continuous"),
            call.post("start"),
            call.when_value_matches("acquiring", True, None),
        ]
예제 #11
0
    def do_check_output_settle(self):
        wp = self.child.handled_requests.mock_calls[3]

        # this is the same as do_check_output but with an extra time slot in the
        # turnaround: I jump through some hoops here to make it clear what the
        # difference is (rather than writing out the writeProfile parameters in full)
        a_pos = self.a_positions
        a_pos = a_pos[:10] + [0.627375] + a_pos[10:]

        b_pos = self.b_positions
        b_pos = b_pos[:9] + [0.089875, 0.1] + b_pos[10:]

        up = self.user_programs
        up = up[:10] + [8] + up[10:]

        ta = self.time_array
        ta = ta[:9] + [110000, 90000, 10000] + ta[11:]

        vm = self.velocity_mode
        vm = vm[:10] + [1] + vm[10:]

        assert wp == call.post(
            "writeProfile",
            a=pytest.approx(a_pos),
            b=pytest.approx(b_pos),
            csPort="CS1",
            timeArray=pytest.approx(ta),
            userPrograms=pytest.approx(up),
            velocityMode=pytest.approx(vm),
        )
예제 #12
0
 def test_configure_with_hardware_start_trigger_succeeds(self):
     xs = LineGenerator("x", "mm", 0.0, 0.5, 3, alternate=True)
     ys = LineGenerator("y", "mm", 0.0, 0.1, 2)
     generator = CompoundGenerator([ys, xs], [], [], 0.1)
     generator.prepare()
     completed_steps = 0
     steps_to_do = 6
     # We are not gated
     self.o.gated_trigger = False
     # We want to be armed and in a hardware trigger mode
     self.set_attributes(self.child,
                         acquiring=True,
                         triggerMode="Rising Edge")
     self.o.on_configure(self.context,
                         completed_steps,
                         steps_to_do, {},
                         generator,
                         fileDir="/tmp")
     assert self.child.handled_requests.mock_calls == [
         call.put("arrayCallbacks", True),
         call.put("arrayCounter", 0),
         call.put("imageMode", "Multiple"),
         call.put("numImages", 6),
         call.put("postCount", 999),
         call.post("start"),
         call.when_value_matches("acquiring", True, None),
     ]
예제 #13
0
    def test_connect(self, Session):
        session = MagicMock()
        session.post.return_value = get_mock_response({'key': 'some_key'})
        Session.return_value = session

        self._subject.connect()

        assert_that(
            session.mock_calls[0:1],
            equal_to([
                call.post(
                    data=
                    '{"username": "******", "password": "******"}',
                    headers={
                        'Content-Type': 'application/json',
                        'Authorization': 'Token some_key',
                        'Content-Encoding': 'gzip'
                    },
                    timeout=5,
                    url='https://1.2.3.4:5678/api/auth/login/',
                    verify=True)
            ]))

        assert_that(self._subject._session, equal_to(session))

        assert_that(
            self._subject._headers,
            equal_to({
                'Content-Type': 'application/json',
                'Authorization': 'Token some_key',
                'Content-Encoding': 'gzip'
            }))
예제 #14
0
    def test_configure(self):
        tmp_dir = mkdtemp() + os.path.sep
        vds_file = "odin2"

        start_time = datetime.now()
        self.o.on_configure(
            self.context,
            self.completed_steps,
            self.steps_to_do,
            generator=self.generator,
            fileDir=tmp_dir,
            formatName=vds_file,
        )
        assert self.child.handled_requests.mock_calls == [
            call.put("fileName", "odin2_raw_data"),
            call.put("filePath", tmp_dir),
            call.put("numCapture", self.steps_to_do),
            call.post("start"),
            call.when_value_matches("numCaptured", greater_than_zero, None),
        ]
        print(self.child.handled_requests.mock_calls)
        print("OdinWriter configure {} points took {} secs".format(
            self.steps_to_do,
            datetime.now() - start_time))
        rmtree(tmp_dir)
예제 #15
0
 def test_update_step(self):
     # Pretend to respond on demand values before they are actually set
     self.set_attributes(self.cs, demandA=-0.1375, demandB=0.0)
     self.do_configure(axes_to_scan=["x", "y"], x_pos=0.0, y_pos=0.2)
     positionsA = self.child.handled_requests.put.call_args_list[-5][0][1]
     assert len(positionsA) == 4
     assert positionsA[-1] == 0.25
     assert self.o.end_index == 2
     assert len(self.o.completed_steps_lookup) == 5
     assert len(self.o.profile["time_array"]) == 1
     self.o.registrar = Mock()
     self.child.handled_requests.reset_mock()
     self.o.update_step(3, self.context.block_view("PMAC:TRAJ"))
     self.o.registrar.report.assert_called_once()
     assert self.o.registrar.report.call_args[0][0].steps == 1
     assert not self.o.loading
     assert self.child.handled_requests.mock_calls == [
         call.put('pointsToBuild', 4),
         call.put('positionsA', pytest.approx([0.375, 0.5, 0.625, 0.6375])),
         call.put('positionsB', pytest.approx([0.0, 0.0, 0.0, 0.05])),
         call.put('timeArray',
                  pytest.approx([500000, 500000, 500000, 200000])),
         call.put('userPrograms', pytest.approx([1, 4, 2, 8])),
         call.put('velocityMode', pytest.approx([0, 0, 1, 0])),
         call.post('appendProfile')
     ]
     assert self.o.end_index == 3
     assert len(self.o.completed_steps_lookup) == 9
     assert len(self.o.profile["time_array"]) == 1
예제 #16
0
 def test_update_step(self):
     self.do_configure(axes_to_scan=["x", "y"], x_pos=0.0, y_pos=0.2)
     assert len(self.child.handled_requests.mock_calls) == 4
     # Check that the first trajectory moves to the first place
     positionsA = self.child.handled_requests.post.call_args_list[-1][1][
         "a"]
     assert len(positionsA) == 4
     assert positionsA[-1] == 0.25
     assert self.o.end_index == 2
     assert len(self.o.completed_steps_lookup) == 5
     assert len(self.o.profile["timeArray"]) == 1
     self.o.registrar = Mock()
     self.child.handled_requests.reset_mock()
     self.o.update_step(3, self.context.block_view("PMAC"))
     self.o.registrar.report.assert_called_once()
     assert self.o.registrar.report.call_args[0][0].steps == 1
     assert not self.o.loading
     assert self.child.handled_requests.mock_calls == [
         # pytest.approx to allow sensible compare with numpy arrays
         call.post(
             "writeProfile",
             a=pytest.approx([0.375, 0.5, 0.625, 0.6375]),
             b=pytest.approx([0.0, 0.0, 0.0, 0.0125]),
             timeArray=pytest.approx([500000, 500000, 500000, 100000]),
             userPrograms=pytest.approx([1, 4, 2, 8]),
             velocityMode=pytest.approx([0, 0, 1, 1]),
         )
     ]
     assert self.o.end_index == 3
     assert len(self.o.completed_steps_lookup) == 11
     assert len(self.o.profile["timeArray"]) == 3
예제 #17
0
 def test_configure(self):
     params = MagicMock()
     completed_steps = 1234
     steps_to_do = 456
     part_info = ANY
     self.o.post_configure = MagicMock()
     self.o.configure(
         self.context, completed_steps, steps_to_do, part_info, params)
     # Need to wait for the spawned mock start call to run
     self.o.start_future.result()
     assert self.child.handled_requests.mock_calls == [
         call.put('arrayCallbacks', True),
         call.put('arrayCounter', completed_steps),
         call.put('autoPixelsPerBuffer', 'Manual'),
         call.put('binsInSpectrum', 2048),
         call.put('collectMode', 'MCA mapping'),
         call.put('dxp1MaxEnergy', 4.096),
         call.put('dxp2MaxEnergy', 4.096),
         call.put('dxp3MaxEnergy', 4.096),
         call.put('dxp4MaxEnergy', 4.096),
         call.put('ignoreGate', 'No'),
         call.put('inputLogicPolarity', 'Normal'),
         call.put('pixelAdvanceMode', 'Gate'),
         call.put('pixelsPerBuffer', 1),
         call.put('pixelsPerRun', steps_to_do),
         call.put('presetMode', 'No preset'),
         call.post('start')]
예제 #18
0
 def test_configure(self):
     xs = LineGenerator("x", "mm", 0.0, 0.5, 3000, alternate=True)
     ys = LineGenerator("y", "mm", 0.0, 0.1, 2000)
     generator = CompoundGenerator([ys, xs], [], [], 0.1)
     generator.prepare()
     completed_steps = 0
     steps_to_do = 2000 * 3000
     # We wait to be armed, so set this here
     self.set_attributes(self.child, acquiring=True)
     # This is what the detector does when exposure and acquirePeriod are
     # both set to 0.1
     self.set_attributes(self.child, exposure=0.1, acquirePeriod=0.105)
     self.o.configure(self.context,
                      completed_steps,
                      steps_to_do, {},
                      generator=generator)
     assert self.child.handled_requests.mock_calls == [
         call.put('exposure', 0.1),
         call.put('acquirePeriod', 0.1),
         call.put('arrayCallbacks', True),
         call.put('arrayCounter', 0),
         # duration - readout - fudge_factor - crystal offset
         call.put('exposure', pytest.approx(0.1 - 0.005 - 0.0014 - 5e-6)),
         call.put('imageMode', 'Multiple'),
         call.put('numImages', 6000000),
         call.put('acquirePeriod', 0.1 - 5e-6),
         call.post('start')
     ]
예제 #19
0
    def test_comment_overflow(self,
                              mock_getenv,
                              mock_client_session):
        mock_args, fake_client_session = self.create_mock_pr(
            mock_getenv,
            mock_client_session)

        reporter = github_reporter.create_reporter(mock_args)

        problems = [Problem('another_file', x, 'Wat') for x in range(1, 13)]

        async_report = reporter.report(problems)

        loop = asyncio.get_event_loop()
        loop.run_until_complete(async_report)

        overflow_message = textwrap.dedent('''\
            :sparkles:Linty Fresh Says:sparkles::

            Too many lint errors to report inline!  12 lines have a problem.
            Only reporting the first 10.''')
        overflow_call = call.post(
            'https://api.github.com/repos/foo/bar/pulls/1234/comments',
            headers={
                'Authorization': 'token MY_TOKEN'
            },
            data=json.dumps({
                'body': overflow_message,
            }, sort_keys=True)
        )

        self.assertIn(overflow_call, fake_client_session.calls)
        self.assertEqual(3 + github_reporter.MAX_LINT_ERROR_REPORTS,
                         len(fake_client_session.calls))
 def test_execute_profile(self):
     self.mock_when_value_matches(self.child)
     self.b.executeProfile()
     assert self.child.handled_requests.mock_calls == [
         call.post("executeProfile"),
         call.when_value_matches("pointsScanned", 0, None),
     ]
예제 #21
0
 def test_update_step(self):
     self.do_configure(axes_to_scan=["x", "y"], x_pos=0.0, y_pos=0.2)
     positionsA = self.child.handled_requests.put.call_args_list[-5][0][1]
     assert len(positionsA) == 4
     assert positionsA[-1] == 0.25
     assert self.o.end_index == 2
     assert len(self.o.completed_steps_lookup) == 5
     assert len(self.o.profile["time_array"]) == 1
     update_completed_steps = Mock()
     self.child.handled_requests.reset_mock()
     self.o.update_step(3, update_completed_steps,
                        self.context.block_view("PMAC:TRAJ"))
     update_completed_steps.assert_called_once_with(1, self.o)
     assert not self.o.loading
     assert self.child.handled_requests.mock_calls == self.resolutions_and_use_call(
     ) + [
         call.put('pointsToBuild', 4),
         call.put('positionsA', pytest.approx([0.375, 0.5, 0.625, 0.6375])),
         call.put('positionsB', pytest.approx([0.0, 0.0, 0.0, 0.05])),
         call.put('timeArray', [500000, 500000, 500000, 200000]),
         call.put('userPrograms', [3, 4, 2, 8]),
         call.put('velocityMode', [0, 0, 1, 0]),
         call.post('appendProfile')
     ]
     assert self.o.end_index == 3
     assert len(self.o.completed_steps_lookup) == 9
     assert len(self.o.profile["time_array"]) == 1
예제 #22
0
 def test_send_bad_status(self):
     self.config['splunk_hec_url'] = 'https://splunk.url/foo'
     self.config['splunk_hec_token'] = 'stoken'
     m_resp = Mock(spec_set=requests.models.Response)
     type(m_resp).status_code = 403
     type(m_resp).text = '{"text": "Success"}'
     type(m_resp).headers = {'H1': 'V1'}
     m_resp.json.return_value = {'text': 'Success'}
     with patch('%s.requests' % pbm, autospec=True) as mock_req:
         mock_req.post.return_value = m_resp
         with pytest.raises(RuntimeError):
             self.cls._send_splunk('{"foo": "bar"}')
     assert mock_req.mock_calls == [
         call.post('https://splunk.url/foo',
                   headers={'Authorization': 'Splunk stoken'},
                   data='{"foo": "bar"}')
     ]
     assert self.mock_logger.mock_calls == [
         call.debug('Send to Splunk (%s): %s', 'https://splunk.url/foo',
                    '{"foo": "bar"}'),
         call.debug('Splunk POST got response code %s HEADERS=%s BODY: %s',
                    403, {'H1': 'V1'}, '{"text": "Success"}'),
         call.error(
             'Splunk POST returned non-20x response: %s HEADERS=%s BODY: %s',
             403, {'H1': 'V1'}, '{"text": "Success"}')
     ]
 def test_configure_with_hardware_trigger_and_breakpoints(self):
     xs = LineGenerator("x", "mm", 0.0, 0.5, 20, alternate=True)
     ys = LineGenerator("y", "mm", 0.0, 0.1, 3)
     generator = CompoundGenerator([ys, xs], [], [], 0.1)
     generator.prepare()
     completed_steps = 0
     steps_to_do = 15
     info = ExposureDeadtimeInfo(0.01, 1000, 0.0)
     part_info = dict(anyname=[info])
     self.set_attributes(self.child, triggerMode="Hardware")
     self.o.on_configure(
         self.context,
         completed_steps,
         steps_to_do,
         part_info,
         generator,
         fileDir="/tmp",
         breakpoints=[15, 35, 10],
         exposure=info.calculate_exposure(generator.duration),
     )
     assert self.child.handled_requests.mock_calls == [
         call.put("arrayCallbacks", True),
         call.put("arrayCounter", 0),
         call.put("exposure", 0.1 - 0.01 - 0.0001),
         call.put("imageMode", "Multiple"),
         call.put("numImages", 15),
         call.put("acquirePeriod", 0.1 - 0.0001),
         call.post("start"),
         call.when_value_matches("acquiring", True, None),
     ]
     assert self.o.is_hardware_triggered
예제 #24
0
 def test_run(self):
     update_completed_steps = MagicMock()
     self.o.start_future = MagicMock()
     self.o.done_when_reaches = MagicMock()
     self.o.completed_offset = 0
     self.o.run(self.context, update_completed_steps)
     assert self.child.handled_requests.mock_calls == [call.post('start')]
예제 #25
0
    def test_configure(self):
        xs = LineGenerator("x", "mm", 0.0, 0.5, 3000, alternate=True)
        ys = LineGenerator("y", "mm", 0.0, 0.1, 2000)
        generator = CompoundGenerator([ys, xs], [], [], 0.1)
        generator.prepare()
        completed_steps = 0
        steps_to_do = 2000 * 3000
        # We wait to be armed, so set this here
        self.set_attributes(self.child, acquiring=True, fanStateReady=1)
        self.o.on_configure(
            self.context,
            completed_steps,
            steps_to_do,
            {},
            generator=generator,
            fileDir="/tmp",
        )

        assert self.child.handled_requests.mock_calls == [
            call.put("arrayCallbacks", True),
            call.put("arrayCounter", 0),
            call.put("imageMode", "Multiple"),
            call.put("numImages", 6000000),
            call.put("numImagesPerSeries", 1),
            call.post("start"),
        ]
예제 #26
0
    def test_comment_overflow(self, mock_getenv, mock_client_session):
        mock_args, fake_client_session = self.create_mock_pr(
            mock_getenv, mock_client_session)

        reporter = github_reporter.create_reporter(mock_args)

        problems = [Problem('another_file', x, 'Wat') for x in range(1, 13)]

        async_report = reporter.report('unit-test-linter', problems)

        loop = asyncio.get_event_loop()
        loop.run_until_complete(async_report)

        overflow_message = textwrap.dedent('''\
            unit-test-linter says:

            Too many lint errors to report inline!  12 lines have a problem.
            Only reporting the first 10.''')
        overflow_call = call.post(
            'https://api.github.com/repos/foo/bar/issues/1234/comments',
            headers={'Authorization': 'token MY_TOKEN'},
            data=json.dumps({
                'body': overflow_message,
            }, sort_keys=True))

        self.assertIn(overflow_call, fake_client_session.calls)
        self.assertEqual(3 + github_reporter.MAX_LINT_ERROR_REPORTS,
                         len(fake_client_session.calls))
예제 #27
0
 def test_configure(self):
     completed_steps = 0
     steps_to_do = 456
     self.o.post_configure = MagicMock()
     # We wait to be armed, so set this here
     self.set_attributes(self.child, acquiring=True)
     self.o.configure(self.context, completed_steps, steps_to_do, {},
                      MagicMock())
     # Wait for the start_future so the post gets through to our child
     # even on non-cothread systems
     self.o.actions.start_future.result(timeout=1)
     assert self.child.handled_requests.mock_calls == [
         call.put('arrayCallbacks', True),
         call.put('arrayCounter', completed_steps),
         call.put('autoPixelsPerBuffer', 'Manual'),
         call.put('binsInSpectrum', 2048),
         call.put('collectMode', 'MCA mapping'),
         call.put('dxp1MaxEnergy', 4.096),
         call.put('dxp2MaxEnergy', 4.096),
         call.put('dxp3MaxEnergy', 4.096),
         call.put('dxp4MaxEnergy', 4.096),
         call.put('ignoreGate', 'No'),
         call.put('inputLogicPolarity', 'Normal'),
         call.put('pixelAdvanceMode', 'Gate'),
         call.put('pixelsPerBuffer', 1),
         call.put('pixelsPerRun', steps_to_do),
         call.put('presetMode', 'No preset'),
         call.post('start')
     ]
    def test_post_run_armed_with_hardware_trigger_and_breakpoints(self):
        xs = LineGenerator("x", "mm", 0.0, 0.5, 100, alternate=True)
        ys = LineGenerator("y", "mm", 0.0, 0.1, 5)
        generator = CompoundGenerator([ys, xs], [], [], 0.1)
        generator.prepare()
        info = ExposureDeadtimeInfo(0.01, 1000, 0.0)
        part_info = dict(anyname=[info])
        breakpoints = [100, 400]

        # This would have been done by initial configure
        self.o.is_hardware_triggered = True
        self.o.done_when_reaches = 100

        self.o.on_post_run_armed(self.context, 100, 400, part_info, generator,
                                 breakpoints)
        assert self.child.handled_requests.mock_calls == [
            call.put("arrayCallbacks", True),
            call.put("arrayCounter", 100),
            call.put("imageMode", "Multiple"),
            call.put("numImages", 400),
            call.put("acquirePeriod", 0.1 - 0.0001),
            call.post("start"),
            call.when_value_matches("acquiring", True, None),
        ]
        assert self.o.done_when_reaches == 500
예제 #29
0
 def test_long_steps_lookup(self):
     self.do_configure(axes_to_scan=["x"],
                       completed_steps=3,
                       x_pos=0.62506,
                       duration=14.0)
     assert self.child.handled_requests.mock_calls[-6:] == [
         call.put('pointsToBuild', 14),
         call.put(
             'positionsA',
             pytest.approx([
                 0.625, 0.5625, 0.5, 0.4375, 0.375, 0.3125, 0.25, 0.1875,
                 0.125, 0.0625, 0.0, -0.0625, -0.125, -0.12506377551020409
             ])),
         call.put('timeArray', [
             7143, 3500000, 3500000, 3500000, 3500000, 3500000, 3500000,
             3500000, 3500000, 3500000, 3500000, 3500000, 3500000, 7143
         ]),
         call.put('userPrograms',
                  [3, 0, 4, 0, 3, 0, 4, 0, 3, 0, 4, 0, 2, 8]),
         call.put('velocityMode',
                  [2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 3]),
         call.post('buildProfile')
     ]
     assert self.o.completed_steps_lookup == ([
         3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6
     ])
 def test_write_profile_build(self):
     self.b.writeProfile(
         [1, 5, 2],
         "BRICK2CS1",
         velocityMode=[0, 1, 2],
         userPrograms=[0, 8, 0],
         x=[1, 2, 3],
         z=[4, 4.1, 4.2],
     )
     assert self.child.handled_requests.mock_calls == [
         call.put("numPoints", 4000000),
         call.put("cs", "BRICK2CS1"),
         call.put("useA", False),
         call.put("useB", False),
         call.put("useC", False),
         call.put("useU", False),
         call.put("useV", False),
         call.put("useW", False),
         call.put("useX", True),
         call.put("useY", False),
         call.put("useZ", True),
         call.put("pointsToBuild", 3),
         call.put("positionsX", [1, 2, 3]),
         call.put("positionsZ", [4, 4.1, 4.2]),
         call.put("timeArray", [1, 5, 2]),
         call.put("userPrograms", [0, 8, 0]),
         call.put("velocityMode", [0, 1, 2]),
         call.post("buildProfile"),
     ]
예제 #31
0
    def test_configure_cycle(self):
        self.set_motor_attributes()
        nCycles = 1
        generator = CompoundGenerator([StaticPointGenerator(nCycles)], [], [],
                                      duration=4.0)
        generator.prepare()
        self.o.on_configure(self.context, 0, nCycles, {}, generator, [])

        assert generator.duration == 1.5

        assert self.child.handled_requests.mock_calls == [
            call.post("writeProfile",
                      csPort="CS1",
                      timeArray=[0.002],
                      userPrograms=[8]),
            call.post("executeProfile"),
            call.post("moveCS1",
                      a=-0.125,
                      moveTime=pytest.approx(0.790, abs=1e-3)),
            # pytest.approx to allow sensible compare with numpy arrays
            call.post(
                "writeProfile",
                a=pytest.approx([
                    0.0, 0.25, 0.5, 0.625, 0.625, 0.5, 0.25, 0.0, -0.125,
                    -0.125, 0.0
                ]),
                csPort="CS1",
                timeArray=pytest.approx([
                    250000,
                    250000,
                    250000,
                    250000,
                    1000000,
                    250000,
                    250000,
                    250000,
                    250000,
                    1000000,
                    250000,
                ]),
                userPrograms=pytest.approx([1, 4, 2, 8, 8, 1, 4, 2, 8, 8, 1]),
                velocityMode=pytest.approx([1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 3]),
            ),
        ]
        assert self.o.completed_steps_lookup == [
            0, 0, 1, 1, 1, 1, 1, 2, 3, 3, 3
        ]
예제 #32
0
    def test_ice_post_data(self, mock_requests, mock_logger):
        url = 'http://foo.com/dashboard/foobar'
        mock_result = Mock(status_code=200)
        mock_result.json.return_value = {"status": 200, "data": {'foo': 'bar'}}
        mock_requests.post.return_value = mock_result

        g = Groups('http://foo.com/', dry_run=False)
        mock_logger.reset_mock()
        res = g._ice_post('foobar', {'baz': 'blam'})
        self.assertEquals(mock_logger.mock_calls,
                          [call.debug("POSTing to http://foo.com/dashboard/foobar: {'baz': 'blam'}")]
        )
        self.assertEquals(mock_requests.mock_calls, [
            call.post('http://foo.com/dashboard/foobar', data='{"baz": "blam"}', auth=None),
            call.post().json()
        ])
        self.assertEquals(res, {'foo': 'bar'})
예제 #33
0
    def test_ice_post_response_error(self, mock_requests, mock_logger):
        url = 'http://foo.com/dashboard/foobar'
        mock_result = Mock(status_code=200)
        mock_result.json.return_value = {"status": 403}
        mock_requests.post.return_value = mock_result

        g = Groups('http://foo.com/', dry_run=False)
        mock_logger.reset_mock()
        self.assertRaises(APIRequestException,
                          g._ice_post,
                          'foobar',
                          {'baz': 'blam'}
        )
        self.assertEquals(mock_logger.mock_calls,
                          [call.debug("POSTing to http://foo.com/dashboard/foobar: {'baz': 'blam'}")]
        )
        self.assertEquals(mock_requests.mock_calls, [
            call.post('http://foo.com/dashboard/foobar', data='{"baz": "blam"}', auth=None),
            call.post().json()
        ])
예제 #34
0
 def post(self, url, *args, **kwargs):
     updated_kwargs = self._update_headers(kwargs)
     self.calls.append(call.post(url, *args, **updated_kwargs))
     return _RequestContextManager(self._get_stored_value(url, "post"))
예제 #35
0
    def test_run_test(self, capsys):
        conf = Mock()
        conf.get.return_value = {
            'ep1': {'method': 'GET'},
            'ep2': {'method': 'POST'}
        }
        args = Mock(endpoint_name=None)
        res1 = Mock(spec_set=Response)
        type(res1).status_code = 200
        type(res1).content = 'res1content'
        type(res1).headers = {
            'h1': 'h1val',
            'hz': 'hzval'
        }
        res2 = Mock(spec_set=Response)
        type(res2).status_code = 503
        type(res2).content = 'res2content'
        type(res2).headers = {
            'h21': 'h21val',
            'h2z': 'h2zval'
        }

        req_data = {
            'message': 'testing via webhook2lambda2sqs CLI',
            'version': VERSION,
            'host': 'mynode',
            'datetime': '2016-07-01T02:03:04.000000'
        }

        with patch.multiple(
            pbm,
            autospec=True,
            requests=DEFAULT,
            logger=DEFAULT,
            get_base_url=DEFAULT,
            node=DEFAULT
        ) as mocks:
            mocks['get_base_url'].return_value = 'mybase/'
            mocks['node'].return_value = 'mynode'
            mocks['requests'].get.return_value = res1
            mocks['requests'].post.return_value = res2
            run_test(conf, args)
        out, err = capsys.readouterr()
        assert err == ''
        expected_out = "=> Testing endpoint mybase/ep1/ with GET: "
        expected_out += pformat(req_data) + "\n"
        expected_out += "RESULT: HTTP 200\n"
        expected_out += "h1: h1val\n"
        expected_out += "hz: hzval\n"
        expected_out += "\nres1content\n\n"
        expected_out += "=> Testing endpoint mybase/ep2/ with POST: "
        expected_out += pformat(req_data) + "\n"
        expected_out += "RESULT: HTTP 503\n"
        expected_out += "h21: h21val\n"
        expected_out += "h2z: h2zval\n"
        expected_out += "\nres2content\n\n"
        assert out == expected_out
        assert conf.mock_calls == [call.get('endpoints')]
        assert mocks['get_base_url'].mock_calls == [call(conf, args)]
        assert mocks['logger'].mock_calls == [
            call.debug('API base url: %s', 'mybase/')
        ]
        assert mocks['requests'].mock_calls == [
            call.get('mybase/ep1/', params={
                'message': 'testing via webhook2lambda2sqs CLI',
                'version': VERSION,
                'host': 'mynode',
                'datetime': '2016-07-01T02:03:04.000000'
            }),
            call.post('mybase/ep2/', json={
                'message': 'testing via webhook2lambda2sqs CLI',
                'version': VERSION,
                'host': 'mynode',
                'datetime': '2016-07-01T02:03:04.000000'
            })
        ]
        assert mocks['node'].mock_calls == [call()]
예제 #36
0
    def test_comment_on_pr(self,
                           mock_getenv,
                           mock_client_session):
        mock_args, fake_client_session = self.create_mock_pr(
            mock_getenv,
            mock_client_session)

        reporter = github_reporter.create_reporter(mock_args)
        async_report = reporter.report([
            Problem('some_dir/some_file', 40, 'this made me sad'),
            Problem('some_dir/some_file', 40, 'really sad'),
            Problem('another_file', 2, 'This is OK'),
            Problem('another_file', 2, 'This is OK'),
            Problem('another_file', 3, 'I am a duplicate!'),
            Problem('missing_file', 42, "Don't report me!!!"),
        ])

        loop = asyncio.get_event_loop()
        loop.run_until_complete(async_report)

        diff_request = call.get(
            'https://api.github.com/repos/foo/bar/pulls/1234',
            headers={
                'Accept': 'application/vnd.github.diff',
                'Authorization': 'token MY_TOKEN'
            })
        existing_comments_request = call.get(
            'https://api.github.com/repos/foo/bar/pulls/1234/comments',
            headers={
                'Authorization': 'token MY_TOKEN'
            })
        first_comment = call.post(
            'https://api.github.com/repos/foo/bar/pulls/1234/comments',
            headers={
                'Authorization': 'token MY_TOKEN'
            },
            data=json.dumps({
                'commit_id': 'abc123',
                'path': 'another_file',
                'body': textwrap.dedent('''\
                    :sparkles:Linty Fresh Says:sparkles::

                    ```
                    This is OK
                    ```'''),
                'position': 2
            }, sort_keys=True)
        )
        second_comment = call.post(
            'https://api.github.com/repos/foo/bar/pulls/1234/comments',
            headers={
                'Authorization': 'token MY_TOKEN'
            },
            data=json.dumps({
                'commit_id': 'abc123',
                'path': 'some_dir/some_file',
                'body': textwrap.dedent('''\
                    :sparkles:Linty Fresh Says:sparkles::

                    ```
                    this made me sad
                    really sad
                    ```'''),
                'position': 3
            }, sort_keys=True)
        )

        self.assertEqual(4, len(fake_client_session.calls))
        self.assertIn(diff_request, fake_client_session.calls)
        self.assertIn(existing_comments_request, fake_client_session.calls)
        self.assertIn(first_comment, fake_client_session.calls)
        self.assertIn(second_comment, fake_client_session.calls)