def test_check_release_url(self, mock):
        url = self.RELEASE_URL % (self.DEFAULT_TREE, self.DEFAULT_BRANCH)
        base_url = '%s/%s/%s/%s/%s/%s' % (
            self.BASE_URL, self.DEFAULT_TREE, self.DEFAULT_BRANCH,
            self.DEFAULT_RELEASE, self.DEFAULT_ARCH, self.DEFAULT_DEFCONFIG)
        response = {
            'result': [{
                'kernel': self.DEFAULT_RELEASE,
                'created_on': {
                    '$date': time.time() * 1000
                }
            }],
        }
        cfg = {
            'api_token': self.DEFAULT_API_TOKEN,
        }
        headers = {
            'Authorization': self.DEFAULT_API_TOKEN,
        }

        mock.get(url, json=response, request_headers=headers)

        crawler = KernelCICrawler(cfg)
        assert_equal(
            base_url,
            crawler._get_base_url(self.DEFAULT_TREE, self.DEFAULT_BRANCH,
                                  self.DEFAULT_ARCH, self.DEFAULT_DEFCONFIG))
    def test_check_artifacts_all_missing_config(self, mock):
        release_url = self.RELEASE_URL % (self.DEFAULT_TREE,
                                          self.DEFAULT_BRANCH)
        config_url = '%s/%s/%s/%s/%s/%s' % (
            self.BASE_URL, self.DEFAULT_TREE, self.DEFAULT_BRANCH,
            self.DEFAULT_RELEASE, self.DEFAULT_ARCH, self.DEFAULT_DEFCONFIG)
        release_response = {
            'result': [{
                'kernel': self.DEFAULT_RELEASE,
                'created_on': {
                    '$date': time.time() * 1000
                }
            }],
        }
        release_headers = {
            'Authorization': self.DEFAULT_API_TOKEN,
        }
        board = {
            'arch': self.DEFAULT_ARCH,
            'dt': self.DEFAULT_DTB,
            'name': 'test'
        }
        cfg = {
            'api_token': self.DEFAULT_API_TOKEN,
        }

        mock.get(release_url,
                 json=release_response,
                 request_headers=release_headers)
        mock.get(config_url, status_code=404)

        crawler = KernelCICrawler(cfg)
        assert_raises(RemoteEmptyError, crawler.crawl, board,
                      self.DEFAULT_TREE, self.DEFAULT_BRANCH,
                      self.DEFAULT_DEFCONFIG)
    def test_check_artifacts_all(self, mock):
        release_url = self.RELEASE_URL % (self.DEFAULT_TREE,
                                          self.DEFAULT_BRANCH)
        config_url = '%s/%s/%s/%s/%s/%s' % (
            self.BASE_URL, self.DEFAULT_TREE, self.DEFAULT_BRANCH,
            self.DEFAULT_RELEASE, self.DEFAULT_ARCH, self.DEFAULT_DEFCONFIG)
        kernel_url = '%s/%s' % (config_url, self.DEFAULT_IMAGE)
        modules_url = '%s/%s' % (config_url, self.DEFAULT_MODULES)
        dtb_url = '%s/dtbs/%s.dtb' % (config_url, self.DEFAULT_DTB)
        release_response = {
            'result': [{
                'kernel': self.DEFAULT_RELEASE,
                'created_on': {
                    '$date': time.time() * 1000
                }
            }],
        }
        release_headers = {
            'Authorization': self.DEFAULT_API_TOKEN,
        }
        board = {
            'arch': self.DEFAULT_ARCH,
            'dt': self.DEFAULT_DTB,
            'name': 'test'
        }
        cfg = {
            'api_token': self.DEFAULT_API_TOKEN,
        }

        mock.get(release_url,
                 json=release_response,
                 request_headers=release_headers)
        mock.get(config_url)
        mock.get(kernel_url)
        mock.get(modules_url)
        mock.get(dtb_url)

        crawler = KernelCICrawler(cfg)
        items = crawler.crawl(board, self.DEFAULT_TREE, self.DEFAULT_BRANCH,
                              self.DEFAULT_DEFCONFIG)

        assert_equal(kernel_url, items['kernel'])
        assert_equal(dtb_url, items['dtb'])
        assert_equal(modules_url, items['modules'])
    def test_check_release_error(self, mock):
        url = self.RELEASE_URL % (self.DEFAULT_TREE, self.DEFAULT_BRANCH)
        cfg = {
            'api_token': self.DEFAULT_API_TOKEN,
        }

        mock.get(url, status_code=404)

        crawler = KernelCICrawler(cfg)
        assert_raises(RemoteEmptyError, crawler._get_latest_release,
                      self.DEFAULT_TREE, self.DEFAULT_BRANCH)
    def test_check_release_missing_result(self, mock):
        url = self.RELEASE_URL % (self.DEFAULT_TREE, self.DEFAULT_BRANCH)
        response = {}
        cfg = {
            'api_token': self.DEFAULT_API_TOKEN,
        }
        headers = {
            'Authorization': self.DEFAULT_API_TOKEN,
        }

        mock.get(url, json=response, request_headers=headers)

        crawler = KernelCICrawler(cfg)
        assert_raises(RemoteEmptyError, crawler._get_latest_release,
                      self.DEFAULT_TREE, self.DEFAULT_BRANCH)