Exemplo n.º 1
0
def test_list_packages():
    r = CnrClient()
    with requests_mock.mock() as m:
        response = '{"packages": "true"}'
        m.get(DEFAULT_REGISTRY + DEFAULT_PREFIX + "/api/v1/packages",
              text=response)
        assert json.dumps(r.list_packages({})) == response
Exemplo n.º 2
0
def test_get_version():
    r = CnrClient()
    response = '{"cnr-server": "0.23.0"}'
    with requests_mock.mock() as m:
        m.get(DEFAULT_REGISTRY + DEFAULT_PREFIX + "/version",
              complete_qs=True,
              text=response)
        assert json.dumps(r.version()) == json.dumps(json.loads(response))
Exemplo n.º 3
0
def test_list_packages_orga():
    r = CnrClient()
    with requests_mock.mock() as m:
        response = '{"packages": "true"}'
        m.get(DEFAULT_REGISTRY + DEFAULT_PREFIX +
              "/api/v1/packages?namespace=ant31",
              complete_qs=True,
              text=response)
        assert json.dumps(r.list_packages({'namespace': "ant31"})) == response
Exemplo n.º 4
0
def test_pull_version():
    r = CnrClient()
    with requests_mock.mock() as m:
        response = b'package_data'
        m.get(DEFAULT_REGISTRY + DEFAULT_PREFIX +
              "/api/v1/packages/orga/p1/0.8.1/helm/pull",
              content=response)
        assert r.pull("orga/p1", {
            "key": "version",
            "value": "0.8.1"
        }, "helm") == response
Exemplo n.º 5
0
def test_pull_digest():
    r = CnrClient()
    with requests_mock.mock() as m:
        response = b'package_data'
        m.get(DEFAULT_REGISTRY + DEFAULT_PREFIX +
              "/api/v1/packages/orga/p1/blobs/sha256/2432",
              content=response)
        assert r.pull("orga/p1", {
            "key": "digest",
            "value": "2432"
        }, "helm") == response
Exemplo n.º 6
0
def test_delete_package_unauthorized():
    r = CnrClient()
    with requests_mock.mock() as m:
        response = '{"packages": "true"}'
        m.delete(DEFAULT_REGISTRY + DEFAULT_PREFIX +
                 "/api/v1/packages/ant31/kube-ui/1.4.3/helm",
                 complete_qs=True,
                 text=response,
                 status_code=401)
        with pytest.raises(requests.HTTPError):
            r.delete_package("ant31/kube-ui", "1.4.3", "helm")
Exemplo n.º 7
0
def test_delete_package():
    r = CnrClient()
    with requests_mock.mock() as m:
        response = '{"packages": "true"}'
        m.delete(DEFAULT_REGISTRY + DEFAULT_PREFIX +
                 "/api/v1/packages/ant31/kube-ui/1.4.3/helm",
                 complete_qs=True,
                 text=response)
        assert r.delete_package("ant31/kube-ui", "1.4.3", "helm") == {
            "packages": "true"
        }
Exemplo n.º 8
0
def test_push_unauthorized():
    r = CnrClient()
    with requests_mock.mock() as m:
        body = {"blob": "fdsfds"}
        response = b'{"packages": "true"}'
        m.post(DEFAULT_REGISTRY + DEFAULT_PREFIX +
               "/api/v1/packages/ant31/kube-ui?force=false",
               complete_qs=True,
               content=response,
               status_code=401)
        with pytest.raises(requests.HTTPError):
            r.push(name="ant31/kube-ui", body=body)
Exemplo n.º 9
0
def test_push_force():
    body = {"blob": b64encode(b"foobar").decode('utf-8')}
    r = CnrClient()
    response = '{"packages": "true"}'
    with requests_mock.mock() as m:
        m.post(DEFAULT_REGISTRY + DEFAULT_PREFIX +
               "/api/v1/packages/ant31/kube-ui?force=true",
               complete_qs=True,
               text=response)
        assert json.dumps(r.push(name="ant31/kube-ui", body=body,
                                 force=True)) == json.dumps(
                                     json.loads(response))
Exemplo n.º 10
0
def test_pull_discovery_https(discovery_html):
    r = CnrClient()
    with requests_mock.mock() as m:
        response = b'package_data'
        m.get("https://cnr.sh/?cnr-discovery=1",
              text=discovery_html,
              complete_qs=True)
        m.get("https://api.kubespray.io/api/v1/packages/orga/p1/pull",
              content=response)
        assert r.pull("cnr.sh/orga/p1", {
            "key": "version",
            "value": "1.0.0"
        }, "helm") == response
Exemplo n.º 11
0
def test_pull_channel(channels_data):
    r = CnrClient()
    with requests_mock.mock() as m:
        response = b'package_data'
        m.get(DEFAULT_REGISTRY + DEFAULT_PREFIX +
              "/api/v1/packages/orga/p1/1.0.0-rc/helm/pull",
              content=response)
        m.get(DEFAULT_REGISTRY + DEFAULT_PREFIX +
              "/api/v1/packages/orga/p1/channels/dev",
              text=json.dumps(channels_data['dev']))
        assert r.pull("orga/p1", {
            "value": "dev",
            "key": "channel"
        }, "helm") == response
Exemplo n.º 12
0
def test_headers_with_auth():
    r = CnrClient()
    r.auth.add_token('*', 'titi')
    assert sorted(
        r.headers.keys()) == ["Authorization", 'Content-Type', 'User-Agent']
    assert r.headers["Authorization"] == "titi"
    assert r.headers["Content-Type"] == "application/json"
    assert r.headers["User-Agent"] == "cnrpy-cli/%s" % cnrclient.__version__
Exemplo n.º 13
0
    def download_appr_deps(self, deps, dest=DEFAULT_CHARTS, tarball=False):
        """
            Creates a directory 'dep_charts' to download and extract all dependencies
            fetched from the app-registry server.
            returns a helm dependency list
        """
        mkdir_p(dest)
        helm_deps = {}
        for dep in deps:
            package_parts = parse_package_name(dep['name'])
            name = package_parts['package']

            vparts = parse_version(dep['version'])
            client = CnrClient(package_parts['host'])
            package_name = '%s/%s' % (package_parts['namespace'], name)

            pullpack = client.pull_json(package_name,
                                        version_parts=vparts,
                                        media_type='helm')
            package = CnrPackage(pullpack['blob'], b64_encoded=True)
            release = pullpack['release']
            packagepath = os.path.join(dest, package_parts['namespace'])
            print('Pulled package: %s (%s) \n -> %s' %
                  (dep['name'], release, packagepath),
                  file=sys.stderr)
            if tarball:
                with open('%s-%s.tgz' % (name, release), 'wb') as tarfile:
                    tarfile.write(package.blob)
            package.extract(packagepath)

            helm_deps[name] = {
                'name': name,
                'version': release,
                'repository': 'file://%s/%s' % (packagepath, name)
            }
        return helm_deps
Exemplo n.º 14
0
def test_url_prefix():
    r = CnrClient(endpoint="http://test.com/test")
    assert r._url("/2") == "http://test.com/test/cnr/2"
Exemplo n.º 15
0
def test_url():
    r = CnrClient(endpoint="http://test.com")
    assert r._url("/test") == "http://test.com/cnr/test"
Exemplo n.º 16
0
def test_default_endpoint():
    r = CnrClient(endpoint=None)
    assert r.endpoint.geturl() == DEFAULT_REGISTRY + DEFAULT_PREFIX
Exemplo n.º 17
0
def test_headers_without_auth():
    r = CnrClient()
    assert sorted(r.headers.keys()) == ['Content-Type', 'User-Agent']
    assert r.headers["Content-Type"] == "application/json"
    assert r.headers["User-Agent"] == "cnrpy-cli/%s" % cnrclient.__version__