Beispiel #1
0
    def test_device_deployments_already_installed(self):
        """Check case with already installed artifact
        """
        dev = Device()

        self.log.info('fake device with ID: %s', dev.devid)

        self.inventory_add_dev(dev)

        data = b'foo_bar'
        artifact_name = 'hammer-update-' + str(uuid4())
        # come up with an artifact
        with artifact_from_data(name=artifact_name, data=data, devicetype=dev.device_type) as art:
            ac = SimpleArtifactsClient()
            with ac.with_added_artifact(description='desc', size=art.size, data=art) as artid:

                newdep = self.make_new_deployment(name='foo', artifact_name=artifact_name,
                                                  devices=[dev.devid])

                with self.with_added_deployment(newdep) as depid:
                    dc = SimpleDeviceClient()
                    self.log.debug('device token %s', dev.fake_token)

                    # pretend we have the same artifact installed already
                    # NOTE: asking for a deployment while having it already
                    # installed is special in the sense that the status of
                    # deployment for this device will be marked as 'already-installed'
                    nextdep = dc.get_next_deployment(dev.fake_token, artifact_name=artifact_name,
                                                     device_type=dev.device_type)
                    self.log.info('device next: %s', nextdep)
                    assert nextdep == None
                    # verify that device status was properly recorded
                    self.verify_deployment_stats(depid, expected={
                        'already-installed': 1,
                    })
Beispiel #2
0
    def test_device_deployments_simple(self):
        """Check that device can get next deployment, simple cases:
        - bogus token
        - valid update
        - device type incompatible with artifact
        """
        dev = Device()

        self.log.info('fake device with ID: %s', dev.devid)

        self.inventory_add_dev(dev)

        data = b'foo_bar'
        artifact_name = 'hammer-update-' + str(uuid4())
        # come up with an artifact
        with artifact_from_data(name=artifact_name, data=data, devicetype=dev.device_type) as art:
            ac = SimpleArtifactsClient()
            with ac.with_added_artifact(description='desc', size=art.size, data=art) as artid:

                newdep = self.make_new_deployment(name='foo', artifact_name=artifact_name,
                                                  devices=[dev.devid])

                with self.with_added_deployment(newdep) as depid:
                    dc = SimpleDeviceClient()
                    self.log.debug('device token %s', dev.fake_token)

                    # try with some bogus token
                    try:
                        dc.get_next_deployment('foo-bar-baz', artifact_name=artifact_name,
                                                     device_type=dev.device_type)
                    except bravado.exception.HTTPError as err:
                        assert 400 <= err.response.status_code < 500
                    else:
                        raise AssertionError('expected to fail')

                    # pretend we have another artifact installed
                    nextdep = dc.get_next_deployment(dev.fake_token,
                                                     artifact_name='different {}'.format(artifact_name),
                                                     device_type=dev.device_type)
                    self.log.info('device next: %s', nextdep)
                    assert nextdep

                    assert dev.device_type in nextdep.artifact['device_types_compatible']

                    # pretend our device type is different than expected
                    nextdep = dc.get_next_deployment(dev.fake_token,
                                                     artifact_name='different {}'.format(artifact_name),
                                                     device_type='other {}'.format(dev.device_type))
                    self.log.info('device next: %s', nextdep)
                    assert nextdep == None
                    # verify that device status was properly recorded
                    self.verify_deployment_stats(depid, expected={
                        'noartifact': 1,
                    })
Beispiel #3
0
    def test_device_deployments_logs(self):
        """Check that device can get next deployment, full cycle
        """
        dev = Device()

        self.log.info('fake device with ID: %s', dev.devid)

        self.inventory_add_dev(dev)

        data = b'foo_bar'
        artifact_name = 'hammer-update ' + str(uuid4())
        # come up with an artifact
        with artifact_from_data(name=artifact_name,
                                data=data,
                                devicetype=dev.device_type) as art:
            ac = SimpleArtifactsClient()
            with ac.with_added_artifact(description='desc',
                                        size=art.size,
                                        data=art) as artid:

                newdep = self.make_new_deployment(name='foo',
                                                  artifact_name=artifact_name,
                                                  devices=[dev.devid])

                with self.with_added_deployment(newdep) as depid:
                    dc = SimpleDeviceClient()
                    self.log.debug('device token %s', dev.fake_token)

                    # pretend we have another artifact installed
                    nextdep = dc.get_next_deployment(
                        dev.fake_token,
                        artifact_name='different {}'.format(artifact_name),
                        device_type=dev.device_type)
                    self.log.info('device next: %s', nextdep)
                    assert nextdep

                    dc.upload_logs(dev.fake_token,
                                   nextdep.id,
                                   logs=[
                                       'foo bar baz',
                                       'lorem ipsum',
                                   ])

                    rsp = self.client.deployments.get_deployments_deployment_id_devices_device_id_log(
                        Authorization='foo',
                        deployment_id=depid,
                        device_id=dev.devid).result()[1]
                    logs = rsp.text
                    self.log.info('device logs\n%s', logs)

                    assert 'lorem ipsum' in logs
                    assert 'foo bar baz' in logs
Beispiel #4
0
    def test_device_deployments_logs(self):
        """Check that device can get next deployment, full cycle"""
        dev = Device()

        self.d.log.info("fake device with ID: %s", dev.devid)

        self.inventory_add_dev(dev)

        data = b"foo_bar"
        artifact_name = "hammer-update-" + str(uuid4())
        # come up with an artifact
        with artifact_from_data(name=artifact_name,
                                data=data,
                                devicetype=dev.device_type) as art:
            ac = SimpleArtifactsClient()
            with ac.with_added_artifact(description="desc",
                                        size=art.size,
                                        data=art) as artid:

                newdep = self.d.make_new_deployment(
                    name="foo",
                    artifact_name=artifact_name,
                    devices=[dev.devid])

                with self.d.with_added_deployment(newdep) as depid:
                    dc = SimpleDeviceClient()
                    self.d.log.debug("device token %s", dev.fake_token)

                    # pretend we have another artifact installed
                    nextdep = dc.get_next_deployment(
                        dev.fake_token,
                        artifact_name="different {}".format(artifact_name),
                        device_type=dev.device_type,
                    )
                    self.d.log.info("device next: %s", nextdep)
                    assert nextdep

                    dc.upload_logs(dev.fake_token,
                                   nextdep.id,
                                   logs=["foo bar baz", "lorem ipsum"])

                    rsp = self.d.client.Management_API.Get_Deployment_Log_for_Device(
                        Authorization="foo",
                        deployment_id=depid,
                        device_id=dev.devid).result()[1]
                    logs = rsp.text
                    self.d.log.info("device logs\n%s", logs)

                    assert "lorem ipsum" in logs
                    assert "foo bar baz" in logs
Beispiel #5
0
    def test_device_deployments_full(self):
        """Check that device can get next deployment, full cycle
        """
        dev = Device()

        self.log.info('fake device with ID: %s', dev.devid)

        self.inventory_add_dev(dev)

        data = b'foo_bar'
        artifact_name = 'hammer-update-' + str(uuid4())
        # come up with an artifact
        with artifact_from_data(name=artifact_name, data=data, devicetype=dev.device_type) as art:
            ac = SimpleArtifactsClient()
            with ac.with_added_artifact(description='desc', size=art.size, data=art) as artid:

                newdep = self.make_new_deployment(name='foo', artifact_name=artifact_name,
                                                  devices=[dev.devid])

                with self.with_added_deployment(newdep) as depid:
                    dc = SimpleDeviceClient()
                    self.log.debug('device token %s', dev.fake_token)

                    self.verify_deployment_stats(depid, expected={
                        'pending': 1,
                    })

                    # pretend we have another artifact installed
                    nextdep = dc.get_next_deployment(dev.fake_token,
                                                     artifact_name='different {}'.format(artifact_name),
                                                     device_type=dev.device_type)
                    self.log.info('device next: %s', nextdep)
                    assert nextdep

                    assert dev.device_type in nextdep.artifact['device_types_compatible']

                    for st in ['downloading', 'installing', 'rebooting']:
                        dc.report_status(token=dev.fake_token, devdepid=nextdep.id, status=st)
                        self.verify_deployment_stats(depid, expected={
                            st: 1,
                        })

                    # we have reported some statuses now, but not the final
                    # status, try to get the next deployment
                    againdep = dc.get_next_deployment(dev.fake_token,
                                                     artifact_name='different {}'.format(artifact_name),
                                                     device_type=dev.device_type)
                    assert againdep
                    assert againdep.id == nextdep.id

                    # deployment should be marked as inprogress
                    dep = self.client.deployments.get_deployments_id(Authorization='foo',
                                                                     id=depid).result()[0]
                    assert dep.status == 'inprogress'

                    # report final status
                    dc.report_status(token=dev.fake_token, devdepid=nextdep.id, status='success')
                    self.verify_deployment_stats(depid, expected={
                        'success': 1,
                    })

                    dep = self.client.deployments.get_deployments_id(Authorization='foo',
                                                                     id=depid).result()[0]
                    assert dep.status == 'finished'

                    # report failure as final status
                    dc.report_status(token=dev.fake_token, devdepid=nextdep.id, status='failure')
                    self.verify_deployment_stats(depid, expected={
                        'failure': 1,
                    })

                    # deployment is finished, should get no more updates
                    nodep = dc.get_next_deployment(dev.fake_token,
                                                     artifact_name='other {}'.format(artifact_name),
                                                     device_type=dev.device_type)
                    assert nodep == None

                    # as a joke, report rebooting now
                    dc.report_status(token=dev.fake_token, devdepid=nextdep.id, status='rebooting')
                    self.verify_deployment_stats(depid, expected={
                        'rebooting': 1,
                    })
                    # deployment is in progress again
                    dep = self.client.deployments.get_deployments_id(Authorization='foo',
                                                                     id=depid).result()[0]
                    assert dep.status == 'inprogress'

                    # go on, let's pretend that the artifact is already installed
                    nodep = dc.get_next_deployment(dev.fake_token,
                                                     artifact_name=artifact_name,
                                                     device_type=dev.device_type)
                    assert nodep == None
                    self.verify_deployment_stats(depid, expected={
                        'already-installed': 1,
                    })
Beispiel #6
0
    def test_device_deployments_full(self):
        """Check that device can get next deployment, full cycle"""
        dev = Device()

        self.d.log.info("fake device with ID: %s", dev.devid)

        self.inventory_add_dev(dev)

        data = b"foo_bar"
        artifact_name = "hammer-update-" + str(uuid4())
        # come up with an artifact
        with artifact_from_data(name=artifact_name,
                                data=data,
                                devicetype=dev.device_type) as art:
            ac = SimpleArtifactsClient()
            with ac.with_added_artifact(description="desc",
                                        size=art.size,
                                        data=art) as artid:

                newdep = self.d.make_new_deployment(
                    name="foo",
                    artifact_name=artifact_name,
                    devices=[dev.devid])

                with self.d.with_added_deployment(newdep) as depid:
                    dc = SimpleDeviceClient()
                    self.d.log.debug("device token %s", dev.fake_token)

                    # pretend we have another artifact installed
                    nextdep = dc.get_next_deployment(
                        dev.fake_token,
                        artifact_name="different {}".format(artifact_name),
                        device_type=dev.device_type,
                    )
                    self.d.log.info("device next: %s", nextdep)
                    assert nextdep

                    assert (dev.device_type
                            in nextdep.artifact["device_types_compatible"])

                    self.d.verify_deployment_stats(depid,
                                                   expected={"pending": 1})

                    for st in ["downloading", "installing", "rebooting"]:
                        dc.report_status(token=dev.fake_token,
                                         devdepid=nextdep.id,
                                         status=st)
                        self.d.verify_deployment_stats(depid, expected={st: 1})

                    # we have reported some statuses now, but not the final
                    # status, try to get the next deployment
                    againdep = dc.get_next_deployment(
                        dev.fake_token,
                        artifact_name="different {}".format(artifact_name),
                        device_type=dev.device_type,
                    )
                    assert againdep
                    assert againdep.id == nextdep.id

                    # deployment should be marked as inprogress
                    dep = self.d.client.Management_API.Show_Deployment(
                        Authorization="foo", id=depid).result()[0]
                    assert dep.status == "inprogress"

                    # report final status
                    dc.report_status(token=dev.fake_token,
                                     devdepid=nextdep.id,
                                     status="success")
                    self.d.verify_deployment_stats(depid,
                                                   expected={"success": 1})

                    dep = self.d.client.Management_API.Show_Deployment(
                        Authorization="foo", id=depid).result()[0]
                    assert dep.status == "finished"

                    # report failure as final status
                    dc.report_status(token=dev.fake_token,
                                     devdepid=nextdep.id,
                                     status="failure")
                    self.d.verify_deployment_stats(depid,
                                                   expected={"failure": 1})

                    # deployment is finished, should get no more updates
                    nodep = dc.get_next_deployment(
                        dev.fake_token,
                        artifact_name="other {}".format(artifact_name),
                        device_type=dev.device_type,
                    )
                    assert nodep == None

                    # as a joke, report rebooting now
                    dc.report_status(token=dev.fake_token,
                                     devdepid=nextdep.id,
                                     status="rebooting")
                    self.d.verify_deployment_stats(depid,
                                                   expected={"rebooting": 1})
                    # deployment is in progress again
                    dep = self.d.client.Management_API.Show_Deployment(
                        Authorization="foo", id=depid).result()[0]
                    assert dep.status == "inprogress"

                    # go on, let's pretend that the artifact is already installed
                    nodep = dc.get_next_deployment(
                        dev.fake_token,
                        artifact_name=artifact_name,
                        device_type=dev.device_type,
                    )
                    assert nodep == None
                    self.d.verify_deployment_stats(
                        depid, expected={"already-installed": 1})
Beispiel #7
0
    def test_device_deployments_simple(self):
        """Check that device can get next deployment, simple cases:
        - bogus token
        - valid update
        - already installed
        - device type incompatible with artifact
        """
        dev = Device()

        self.log.info('fake device with ID: %s', dev.devid)

        self.inventory_add_dev(dev)

        data = b'foo_bar'
        artifact_name = 'hammer-update ' + str(uuid4())
        # come up with an artifact
        with artifact_from_data(name=artifact_name,
                                data=data,
                                devicetype=dev.device_type) as art:
            ac = SimpleArtifactsClient()
            with ac.with_added_artifact(description='desc',
                                        size=art.size,
                                        data=art) as artid:

                newdep = self.make_new_deployment(name='foo',
                                                  artifact_name=artifact_name,
                                                  devices=[dev.devid])

                with self.with_added_deployment(newdep) as depid:
                    dc = SimpleDeviceClient()
                    self.log.debug('device token %s', dev.fake_token)

                    # try with some bogus token
                    try:
                        dc.get_next_deployment('foo-bar-baz',
                                               artifact_name=artifact_name,
                                               device_type=dev.device_type)
                    except bravado.exception.HTTPError as err:
                        assert 400 <= err.response.status_code < 500
                    else:
                        raise AssertionError('expected to fail')

                    # pretend we have another artifact installed
                    nextdep = dc.get_next_deployment(
                        dev.fake_token,
                        artifact_name='different {}'.format(artifact_name),
                        device_type=dev.device_type)
                    self.log.info('device next: %s', nextdep)
                    assert nextdep

                    assert dev.device_type in nextdep.artifact[
                        'device_types_compatible']

                    # pretend our device type is different than expected
                    nextdep = dc.get_next_deployment(
                        dev.fake_token,
                        artifact_name='different {}'.format(artifact_name),
                        device_type='other {}'.format(dev.device_type))
                    self.log.info('device next: %s', nextdep)
                    # TODO: this should fail or a deployment should come with a
                    # new artifact, come back when
                    # https://tracker.mender.io/browse/MEN-782 is resolved
                    # assert nextdep == None

                    # pretend we have the same artifact installed already
                    # NOTE: asking for a deployment while having it already
                    # installed is special in the sense that the status of
                    # deployment for this device will be marked as 'already-installed'
                    nextdep = dc.get_next_deployment(
                        dev.fake_token,
                        artifact_name=artifact_name,
                        device_type=dev.device_type)
                    self.log.info('device next: %s', nextdep)
                    assert nextdep == None
                    # verify that device status was properly recorded
                    self.verify_deployment_stats(depid,
                                                 expected={
                                                     'already-installed': 1,
                                                 })