def test_nonclass_connection_factory_prevents_tracing(
            self, postgres_container):
        tracer = MockTracer()
        opentracing.tracer = tracer
        for i in range(480):
            try:
                conn = psycopg2.connect(host='127.0.0.1',
                                        user='******',
                                        password='******',
                                        dbname='test_db',
                                        port=5432,
                                        options='-c search_path=test_schema',
                                        connection_factory=lambda dsn: psycopg2
                                        .extensions.connection(dsn))
                break
            except psycopg2.OperationalError:
                sleep(.25)
            if i == 479:
                raise Exception('Failed to connect to Postgres: {}'.format(
                    postgres_container.logs()))

        assert isinstance(conn, psycopg2.extensions.connection)

        with conn.cursor(cursor_factory=DictCursor) as cursor:
            cursor.execute('insert into table_one values (%s, %s, %s, %s)',
                           (random_string(), random_string(), datetime.now(),
                            datetime.now()))
        conn.commit()
        assert not tracer.finished_spans()
Exemplo n.º 2
0
    def test_instrumented_sanity(self, connection_tracing):
        tracer, conn = connection_tracing
        with tracer.start_active_span('Parent'):
            with conn.cursor() as cursor:
                cursor.execute('insert into table_one values (%s, %s, %s, %s)',
                               (random_string(), random_string(),
                                datetime.now(), datetime.now()))
                cursor.execute('insert into table_two values (%s, %s, %s, %s)',
                               (random_int(0, 100000), random_int(
                                   0, 100000), random_float(), random_float()))
            conn.commit()
        spans = tracer.finished_spans()
        assert len(spans) == 4
        for span in spans[:3]:
            assert span.tags['some'] == 'tag'
            assert span.tags[tags.DATABASE_TYPE] == 'MySQL'
            assert span.tags[tags.DATABASE_INSTANCE] == 'test_db'

        first, second, commit, parent = spans
        for span in (first, second):
            assert span.operation_name == 'DictCursor.execute(insert)'
            assert span.tags['db.rows_produced'] == 1
            assert span.parent_id == parent.context.span_id
            assert tags.ERROR not in span.tags
        assert first.tags[
            tags.
            DATABASE_STATEMENT] == 'insert into table_one values (%s, %s, %s, %s)'
        assert second.tags[
            tags.
            DATABASE_STATEMENT] == 'insert into table_two values (%s, %s, %s, %s)'
        assert commit.operation_name == 'Connection.commit()'
        assert parent.operation_name == 'Parent'
Exemplo n.º 3
0
def test_container_release_cores(test_db):
    a = App.get_or_create('app', 'http://git.hunantv.com/group/app.git')
    v = a.add_version(random_sha1())
    p = Pod.create('pod', 'pod', 10, -1)
    host = Host.create(p, random_ipv4(), random_string(), random_uuid(), 200, 0)

    for core in host.cores:
        assert core.host_id == host.id
        assert core.remain == 10

    containers = []
    
    cores = sorted(host.cores, key=operator.attrgetter('label'))
    for fcores, pcores in zip(chunked(cores[:100], 10), chunked(cores[100:], 10)):
        used_cores = {'full': fcores, 'part': pcores}
        host.occupy_cores(used_cores, 5)
        c = Container.create(random_sha1(), host, v, random_string(), 'entrypoint', used_cores, 'env', nshare=5)
        containers.append(c)

    cores = sorted(host.cores, key=operator.attrgetter('label'))
    for fcores, pcores in zip(chunked(cores[:100], 10), chunked(cores[100:], 10)):
        for core in fcores:
            assert core.remain == 0
        for core in pcores:
            assert core.remain == 5

    for c in containers:
        c.delete()

    cores = sorted(host.cores, key=operator.attrgetter('label'))
    for fcores, pcores in zip(chunked(cores[:100], 10), chunked(cores[100:], 10)):
        for core in fcores:
            assert core.remain == 10
        for core in pcores:
            assert core.remain == 10
Exemplo n.º 4
0
def generate_user_form_dto(**kwargs) -> UserFormDto:
    """Generate a user form"""
    return UserFormDto(
        username=kwargs.get(
            'username', random_string(settings.rules.user.username_min_char)),
        full_name=kwargs.get('name',
                             random_string(settings.rules.user.name_min_char)),
        avatar_href=kwargs.get('avatar_href', random_string(10)),
        password=kwargs.get(
            'password', random_string(settings.rules.user.password_min_char)),
        email=kwargs.get('email', random_email()))
def generate_payload():
    return {
        # required
        'device_id': str(uuid.uuid4()),
        'vehicle_id': utils.random_string(255),
        'type': random.choice(VEHICLE_TYPE),
        'propulsion': [random.choice(PROPULSION_TYPE)],
        # optionnal
        'year': utils.random_year(),
        'mfgr': utils.random_string(255),
        'model': utils.random_string(255),
    }
Exemplo n.º 6
0
def test_container_transform(test_db):
    a = App.get_or_create('app', 'http://git.hunantv.com/group/app.git', '')
    assert a is not None

    v = a.add_version(random_sha1())
    v2 = a.add_version(random_sha1())
    assert v is not None
    assert v.app.id == a.id
    assert v.name == a.name
    assert len(v.containers.all()) == 0
    assert len(v.tasks.all()) == 0

    g = Group.create('group', 'group')
    p = Pod.create('pod', 'pod')
    assert p.assigned_to_group(g)
    hosts = [Host.create(p, random_ipv4(), random_string(prefix='host'),
        random_uuid(), 4, 4096) for i in range(6)]

    for host in hosts[:3]:
        host.assigned_to_group(g)

    assert g.get_max_containers(p, 3, 0) == 3
    host_cores = g.get_free_cores(p, 3, 3, 0)
    assert len(host_cores) == 3

    containers = []
    for (host, count), cores in host_cores.iteritems():
        cores_per_container = len(cores) / count
        for i in range(count):
            cid = random_sha1()
            used_cores = {'full': cores['full'][i*cores_per_container:(i+1)*cores_per_container]}
            c = Container.create(cid, host, v, random_string(), 'entrypoint', used_cores, 'env')
            assert c is not None
            containers.append(c)
        host.occupy_cores(cores, 0)

    for host in g.private_hosts.all():
        assert len(host.get_free_cores()[0]) == 1
        assert len(host.containers.all()) == 1
        assert host.count == 1

    assert len(containers) == 3
    assert len(v.containers.all()) == 3

    cids = [c.container_id for c in containers]
    for c in containers:
        host = c.host
        cid = c.container_id
        c.transform(v2, random_sha1(), random_string())
        assert c.container_id != cid

    new_cids = [c.container_id for c in containers]
    assert new_cids != cids
Exemplo n.º 7
0
def test_align_random_strings(random_seed, method):
    # seq part between two gaps should be presented in the seq.
    string1 = random_string(min_len=5)
    string2 = random_string(min_len=5)
    (alignment1, alignment2), score = align(string1,
                                            string2,
                                            reconstruct_answer=True,
                                            method=method,
                                            swap_case_on_mismatch=False)
    substring1 = random.choice([a for a in alignment1.split("-") if a])
    assert substring1 in string1
    substring2 = random.choice([a for a in alignment2.split("-") if a])
    assert substring2 in string2
Exemplo n.º 8
0
def test_needleman_wunsh_by_hamming_distance_random_strings(random_seed):
    seq1 = random_string()
    seq2 = random_string()
    method = alignments.NeedlemanWunsch(match_score=1,
                                        mismatch_score=-1,
                                        gap_score=-1)
    (line1, line2), score = align(seq1,
                                  seq2,
                                  reconstruct_answer=True,
                                  method=method,
                                  swap_case_on_mismatch=False)
    assert score == len(line1) - 2 * hamming_distance(line1, line2), (seq1,
                                                                      seq2)
Exemplo n.º 9
0
def generate_post_form_dto(**kwargs) -> PostFormDto:
    """Generate a post form"""
    return PostFormDto(
        title=kwargs.get('title',
                         random_string(settings.rules.post.title_min_char)),
        description=kwargs.get(
            'description', random_string(settings.rules.post.title_min_char)),
        content=kwargs.get('content',
                           random_string(
                               settings.rules.post.content_min_char)),
        tags=kwargs.get('tags', [
            random_string(settings.rules.post.tag_min_char)
            for _ in range(settings.rules.post.tag_max_count)
        ]))
Exemplo n.º 10
0
    def test_historical(self, mock_datareader):
        start = datetime(2017, 5, 8)
        end = datetime(2017, 5, 12)
        instance = mock_datareader.return_value

        symbol = test_utils.random_string()
        number_decimals = random.randint(0, 5)

        fake_values = []
        for count in range(1, 11):
            high = random.uniform(2.5, 10.2)
            low = random.uniform(1.7, 22.4)
            openy = random.uniform(22.1, 92.7)
            close = random.uniform(102.3, 107.4)
            volume = random.randint(0, 100)
            date = datetime(2014, 1, count)
            fake_values.append(
                (test_utils.PandasDatetimeMock(date.strftime('%Y-%m-%d')),
                 test_utils.PandaStockMock(high, low, openy, close, volume)))
        instance.iterrows.return_value = fake_values
        data = stocks.historical_data(symbol,
                                      start,
                                      end,
                                      number_decimals=number_decimals)[-1]
        self.assertEqual(data['high'],
                         utils.round_decimal(high, number_decimals))
        self.assertEqual(data['low'],
                         utils.round_decimal(low, number_decimals))
        self.assertEqual(data['close'],
                         utils.round_decimal(close, number_decimals))
        self.assertEqual(data['open'],
                         utils.round_decimal(openy, number_decimals))
        self.assertEqual(data['volume'], volume)
        self.assertEqual(data['datetime'].strftime('%Y-%m-%d'),
                         date.strftime('%Y-%m-%d'))
Exemplo n.º 11
0
def test_align_empty(random_seed, method, score_coef, score_delta):
    string = random_string(min_len=1)
    score = score_coef * len(string) + score_delta
    assert align(string, "", reconstruct_answer=True, method=method, swap_case_on_mismatch=False) == \
                ((string, "-" * len(string)), score)
    assert align("", string, reconstruct_answer=True, method=method, swap_case_on_mismatch=False) == \
                (("-" * len(string), string), score)
Exemplo n.º 12
0
def test_create_app(client, test_db, monkeypatch):
    monkeypatch.setattr('eru.models.appconfig.config_backend', FakeEtcd())

    appyaml = '''
appname: "test_app"
entrypoints:
    web:
        cmd: "python app.py --port 5000"
        ports:
            - 5000/tcp
    daemon:
        cmd: "python daemon.py --interval 5"
    service:
        cmd: "go run service.go"
build: "pip install -r ./req.txt"
'''
    data = {
        'version': random_sha1(),
        'git': 'http://git.huanntv.com/test_app.git',
        'token': random_string(random_size=10),
        'appyaml': yaml.load(appyaml),
    }
    rv = client.post('/api/app/register/', data=json.dumps(data), content_type='application/json')
    r = json.loads(rv.data)
    assert rv.status_code == 200
    assert r[u'r'] == 0
    assert r[u'msg'] == u'ok'
    
    rv = client.get('/api/app/test_app/')
    r = json.loads(rv.data)
    assert rv.status_code == 200
    assert r[u'name'] == u'test_app'
    assert r[u'git'] == data['git']
    assert r[u'token'] == data['token']
    assert r[u'group_id'] is None

    rv = client.get('/api/app/random_app_name/')
    r = json.loads(rv.data)
    assert rv.status_code == 404
    assert r[u'error'] == 'App random_app_name not found'

    rv = client.get('/api/app/{0}/{1}/'.format('test_app', data['version']))
    r = json.loads(rv.data)
    assert rv.status_code == 200
    assert r[u'name'] == u'test_app'
    assert r[u'sha'] == data['version']
    assert r[u'appconfig']['appname'] == u'test_app'
    assert len(r[u'appconfig']['entrypoints']) == 3
    assert r[u'appconfig']['entrypoints']['web']['cmd'] == u'python app.py --port 5000'
    assert r[u'appconfig']['entrypoints']['web']['ports'] == ['5000/tcp']
    assert r[u'appconfig']['entrypoints']['daemon']['cmd'] == u'python daemon.py --interval 5'
    assert r[u'appconfig']['entrypoints']['service']['cmd'] == u'go run service.go'
    assert r[u'appconfig']['build'] == u'pip install -r ./req.txt'

    # 短 version 试试
    rv = client.get('/api/app/{0}/{1}/'.format('test_app', data['version'][:7]))
    r = json.loads(rv.data)
    assert rv.status_code == 200
    assert r[u'name'] == u'test_app'
    assert r[u'sha'] == data['version']
Exemplo n.º 13
0
    def test_coefficient(self, mock_datareader):
        start = datetime(2017, 5, 8)
        end = datetime(2017, 5, 12)
        instance = mock_datareader.return_value

        symbol = utils.random_string()

        fake_values = []
        for count in range(1, 11):
            high = random.uniform(2.5, 10.2)
            low = random.uniform(1.7, 22.4)
            openy = random.uniform(22.1, 92.7)
            close = random.uniform(102.3, 107.4)
            volume = random.randint(0, 100)
            date = datetime(2014, 1, count)
            fake_values.append(
                (utils.PandasDatetimeMock(date.strftime('%Y-%m-%d')),
                 utils.PandaStockMock(high, low, openy, close, volume)))
        instance.iterrows.return_value = fake_values

        db_schema = {
            'sqlite': {
                'database_file': None,
            },
        }

        db = StockDatabase(db_schema)
        db.stock_update(symbol, start, end)

        results = db.stock_coefficient_determination()
        self.assertEqual(len(results), 1)
Exemplo n.º 14
0
    def test_load(self, entry, entry_dump):
        # flow like im test_loads
        path = entry.path
        entry.path = random_string()

        entry.load(StringIO(entry_dump))
        assert entry.path == path
Exemplo n.º 15
0
def random_recipe():
    return {
        "machine_id": random.uniform(1, 100),
        "max_speed": random.randint(0, 200),
        "recommended_speed": random.randint(0, 200),
        "name": utils.random_string(8),
    }
Exemplo n.º 16
0
def _create_data(core_share, max_share_core, host_count):
    group = Group.create('group', 'group')
    pod = Pod.create('pod', 'pod', core_share, max_share_core)
    for _ in range(host_count):
        host = Host.create(pod, random_ipv4(), random_string(), random_uuid(), 16, 4096)
        host.assigned_to_group(group)
    return group, pod
Exemplo n.º 17
0
def test_align_needleman_wunsch_as_edit_distance(random_seed):
    # tests that Needleman Wunsch works the same as edit distance with the same scores.
    string1 = random_string()
    string2 = random_string()
    (expected1, expected2), _ = edit_distance(string1,
                                              string2,
                                              reconstruct_answer=True,
                                              swap_case_on_mismatch=False)
    method = alignments.NeedlemanWunsch(match_score=0,
                                        mismatch_score=-1,
                                        gap_score=-1)
    (actual1, actual2), _ = align(string1,
                                  string2,
                                  reconstruct_answer=True,
                                  method=method,
                                  swap_case_on_mismatch=False)
    assert (expected1, expected2) == (actual1, actual2)
Exemplo n.º 18
0
def test_container_release_cores(test_db):
    a = App.get_or_create('app', 'http://git.hunantv.com/group/app.git')
    v = a.add_version(random_sha1())
    p = Pod.create('pod', 'pod', 10, -1)
    host = Host.create(p, random_ipv4(), random_string(), random_uuid(), 200,
                       0)

    for core in host.cores:
        assert core.host_id == host.id
        assert core.remain == 10

    containers = []

    cores = sorted(host.cores, key=operator.attrgetter('label'))
    for fcores, pcores in zip(chunked(cores[:100], 10),
                              chunked(cores[100:], 10)):
        used_cores = {'full': fcores, 'part': pcores}
        host.occupy_cores(used_cores, 5)
        c = Container.create(random_sha1(),
                             host,
                             v,
                             random_string(),
                             'entrypoint',
                             used_cores,
                             'env',
                             nshare=5)
        containers.append(c)

    cores = sorted(host.cores, key=operator.attrgetter('label'))
    for fcores, pcores in zip(chunked(cores[:100], 10),
                              chunked(cores[100:], 10)):
        for core in fcores:
            assert core.remain == 0
        for core in pcores:
            assert core.remain == 5

    for c in containers:
        c.delete()

    cores = sorted(host.cores, key=operator.attrgetter('label'))
    for fcores, pcores in zip(chunked(cores[:100], 10),
                              chunked(cores[100:], 10)):
        for core in fcores:
            assert core.remain == 10
        for core in pcores:
            assert core.remain == 10
Exemplo n.º 19
0
    def test_loads(self, entry, entry_dump):
        # change path before loading from dump
        path = entry.path
        entry.path = random_string()

        # verify it's correctly overwritten back
        entry.loads(entry_dump)
        assert entry.path == path
Exemplo n.º 20
0
def dotfile_in_home(home_dir, dotfile_name):
    """A dotfile inside home directory.
    :return: Path to a dotfile
    """
    path = os.path.join(home_dir, dotfile_name)
    with open(path, 'w') as f:
        print >>f, random_string()
    return path
Exemplo n.º 21
0
def dotdir_file_in_home(dotdir_in_home, filename):
    """A file inside dot-directory within home directory (~/.config/foo/bar).
    :return: Path to a dotfile
    """
    path = os.path.join(dotdir_in_home, filename)
    with open(path, 'w') as f:
        print >>f, random_string()
    return path
Exemplo n.º 22
0
def test_calculation_verifies() -> None:
    """Test that the calculated checkdigit passes verification."""
    d32 = Damm32()
    for i in range(1000):
        word = random_string(6)
        word += d32.calculate(word)
        print(word)
        assert d32.verify(word)
Exemplo n.º 23
0
def test_app_env(client, test_db, monkeypatch):
    monkeypatch.setattr('eru.models.appconfig.config_backend', FakeEtcd())

    appyaml = '''
appname: "test_app"
entrypoints:
    web:
        cmd: "python app.py --port 5000"
        ports:
            - 5000/tcp
    daemon:
        cmd: "python daemon.py --interval 5"
    service:
        cmd: "go run service.go"
build: "pip install -r ./req.txt"
'''
    data = {
        'version': random_sha1(),
        'git': 'http://git.huanntv.com/test_app.git',
        'token': random_string(random_size=10),
        'appyaml': yaml.load(appyaml),
    }
    client.post('/api/app/register/', data=json.dumps(data), content_type='application/json')
    rv = client.get('/api/app/test_app/')
    assert rv.status_code == 200

    url = '/api/app/{0}/env/'.format('test_app')
    envdata = {
        'env': 'prod',
        'TEST_APP_REDIS': 'test_app_redis',
        'TEST_APP_MYSQL': 'test_app_mysqldsn',
    }
    rv = client.put(url, data=json.dumps(envdata), content_type='application/json')
    r = json.loads(rv.data)
    assert rv.status_code == 200

    rv = client.get(url+'?env=prod')
    r = json.loads(rv.data)
    assert rv.status_code == 200
    assert r['TEST_APP_REDIS'] == 'test_app_redis'
    assert r['TEST_APP_MYSQL'] == 'test_app_mysqldsn'

    # 没有 env 应该挂
    envdata.pop('env')
    rv = client.put(url, data=json.dumps(envdata), content_type='application/json')
    r = json.loads(rv.data)
    assert rv.status_code == 400
    assert r[u'error'].startswith('env must be')

    rv = client.get(url)
    r = json.loads(rv.data)
    assert rv.status_code == 400
    assert r[u'error'].startswith('env must be in querystring')

    # 错误的 env 返回空的
    rv = client.get(url+'?env=xxx')
    r = json.loads(rv.data)
    assert rv.status_code == 200
Exemplo n.º 24
0
def create_test_suite():
    appyaml = {
        'appname': 'app',
        'entrypoints': {
            'web': {
                'cmd': 'python app.py',
                'ports': ['5000/tcp'],
            },
            'daemon': {
                'cmd': 'python daemon.py',
            },
            'service': {
                'cmd': 'python service.py'
            },
        },
        'build': 'pip install -r ./requirements.txt',
    }
    app = App.get_or_create('app', 'http://git.hunantv.com/group/app.git')
    version = app.add_version(random_sha1())
    appconfig = version.appconfig
    appconfig.update(**appyaml)
    appconfig.save()

    pod = Pod.create('pod', 'pod')

    hosts = [
        Host.create(pod, random_ipv4(), random_string(prefix='host'),
                    random_uuid(), 4, 4096) for i in range(4)
    ]

    containers = []
    for (host, count), cores in centralized_schedule(pod, 4, 4, 0).iteritems():
        cores_per_container = len(cores) / count
        for i in range(count):
            cid = random_sha1()
            used_cores = {
                'full':
                cores['full'][i * cores_per_container:(i + 1) *
                              cores_per_container]
            }
            c = Container.create(cid, host, version, random_string(), 'web',
                                 used_cores, 'env')
            containers.append(c)
        host.occupy_cores(cores, 0)
    return app, version, pod, hosts, containers
Exemplo n.º 25
0
def create_test_suite():
    appyaml = {
        'appname': 'app',
        'entrypoints': {
            'web': {
                'cmd': 'python app.py',
                'ports': ['5000/tcp'],
            },
            'daemon': {
                'cmd': 'python daemon.py',
            },
            'service': {
                'cmd': 'python service.py'
            },
        },
        'build': 'pip install -r ./requirements.txt',
    }
    app = App.get_or_create('app', 'http://git.hunantv.com/group/app.git', 'token')
    version = app.add_version(random_sha1())
    appconfig = version.appconfig
    appconfig.update(**appyaml)
    appconfig.save()

    group = Group.create('group', 'group')
    pod = Pod.create('pod', 'pod')
    pod.assigned_to_group(group)

    hosts = [Host.create(pod, random_ipv4(), random_string(prefix='host'),
        random_uuid(), 4, 4096) for i in range(4)]

    for host in hosts:
        host.assigned_to_group(group)

    containers = []
    for (host, count), cores in group.get_free_cores(pod, 4, 4, 0).iteritems():
        cores_per_container = len(cores) / count
        for i in range(count):
            cid = random_sha1()
            used_cores = cores['full'][i*cores_per_container:(i+1)*cores_per_container]
            c = Container.create(cid, host, version, random_string(), 'entrypoint', used_cores, 'env')
            containers.append(c)
        host.occupy_cores(cores, 0)
    return app, version, group, pod, hosts, containers
def test_download_file(file_ids):
    target_path = os.path.dirname(os.path.abspath(__file__)) + "/{}".format(
        random_string())

    print(file_ids["a.txt"])
    CLIENT.download_file(file_ids["a.txt"], None, target_path)
    assert Path(target_path).is_file()
    with open(target_path, "r") as f:
        assert "a" == f.read()
    os.remove(target_path)
Exemplo n.º 27
0
def test_random_align_with_affine_gap_in_distance_matrix(random_seed):
    # if we specify positive gap start score there should be more gaps in alignment
    seq1 = random_string(min_len=3, alphabet=["A", "B", "C"])
    seq2 = random_string(min_len=3, alphabet=["A", "B", "C"])

    dist_matrix = {
        'A': {
            'A': 5,
            'B': -5,
            'C': -5,
            '-': -5
        },
        'B': {
            'A': -5,
            'B': 5,
            'C': -15,
            '-': -5
        },
        'C': {
            'A': -5,
            'B': -15,
            'C': 5,
            '-': -5
        },
        '-': {
            'A': -5,
            'B': -5,
            'C': 3,
            '-': 0
        }
    }

    method = alignments.NeedlemanWunsch(score_matrix=dist_matrix)
    (alignment1, alignment2), score = align(seq1,
                                            seq2,
                                            reconstruct_answer=True,
                                            method=method)
    method = alignments.NeedlemanWunsch(score_matrix=dist_matrix, gap_start=20)
    (gap_start_alignment1, gap_start_alignment2), gap_start_score \
        = align(seq1, seq2, reconstruct_answer=True, method=method)
    assert len(alignment1.split("-")) <= len(gap_start_alignment1.split("-"))
    assert len(alignment2.split("-")) <= len(gap_start_alignment2.split("-"))
Exemplo n.º 28
0
def test_get_max_container_count_single_host(test_db):
    pod = Pod.create('pod', 'pod', 10, -1)
    Host.create(pod, random_ipv4(), random_string(), random_uuid(), 64, 4096)

    assert get_max_container_count(pod, ncore=1, nshare=0) == 64
    assert get_max_container_count(pod, ncore=2, nshare=0) == 32
    assert get_max_container_count(pod, ncore=3, nshare=0) == 21
    assert get_max_container_count(pod, ncore=4, nshare=0) == 16
    assert get_max_container_count(pod, ncore=5, nshare=0) == 12
    assert get_max_container_count(pod, ncore=1, nshare=5) == 42
    assert get_max_container_count(pod, ncore=2, nshare=5) == 25
Exemplo n.º 29
0
def test_get_max_container_count_single_host(test_db):
    pod = Pod.create('pod', 'pod', 10, -1)
    Host.create(pod, random_ipv4(), random_string(), random_uuid(), 64, 4096)

    assert get_max_container_count(pod, ncore=1, nshare=0) == 64
    assert get_max_container_count(pod, ncore=2, nshare=0) == 32
    assert get_max_container_count(pod, ncore=3, nshare=0) == 21
    assert get_max_container_count(pod, ncore=4, nshare=0) == 16
    assert get_max_container_count(pod, ncore=5, nshare=0) == 12
    assert get_max_container_count(pod, ncore=1, nshare=5) == 42
    assert get_max_container_count(pod, ncore=2, nshare=5) == 25
    def test_instrumented_sanity(self, connection_tracing):
        tracer, conn = connection_tracing

        # C arg validation
        assert isinstance(conn, psycopg2.extensions.connection)
        psycopg2.extensions.register_type(psycopg2.extensions.UNICODE, conn)

        with tracer.start_active_span('Parent'):
            with conn.cursor(cursor_factory=DictCursor) as cursor:
                # C arg validation
                assert isinstance(cursor, psycopg2.extensions.cursor)
                psycopg2.extras.register_uuid(None, cursor)

                cursor.execute('insert into table_one values (%s, %s, %s, %s)',
                               (random_string(), random_string(),
                                datetime.now(), datetime.now()))
                cursor.execute('insert into table_two values (%s, %s, %s, %s)',
                               (random_int(0, 100000), random_int(
                                   0, 100000), random_float(), random_float()))
            conn.commit()
        spans = tracer.finished_spans()
        assert len(spans) == 4
        for span in spans[:3]:
            assert span.tags['some'] == 'tag'
            assert span.tags[tags.DATABASE_TYPE] == 'PostgreSQL'
            assert span.tags[tags.DATABASE_INSTANCE] == 'test_db'

        first, second, commit, parent = spans
        for span in (first, second):
            assert span.operation_name == 'DictCursor.execute(insert)'
            assert span.tags['db.rows_produced'] == 1
            assert span.parent_id == parent.context.span_id
            assert tags.ERROR not in span.tags
        assert first.tags[
            tags.
            DATABASE_STATEMENT] == 'insert into table_one values (%s, %s, %s, %s)'
        assert second.tags[
            tags.
            DATABASE_STATEMENT] == 'insert into table_two values (%s, %s, %s, %s)'
        assert commit.operation_name == 'connection.commit()'
        assert parent.operation_name == 'Parent'
Exemplo n.º 31
0
def test_network(test_db):
    n = Network.create('net', '10.1.0.0/16')
    assert n is not None
    assert len(n.ips.all()) == 0
    assert n.hostmask_string == '16'
    assert n.pool_size == 65436
    assert n.used_count == 0
    assert n.used_gate_count == 0
    assert n.gate_pool_size == 100

    ip = n.acquire_ip()
    assert ip is not None
    assert ip.network_id == n.id
    assert ip.vethname == ''
    assert not ip.container_id
    assert ip.hostmask == n.hostmask_string
    assert ip.vlan_seq_id == n.id
    assert ip.address.startswith('10.1')

    assert len(n.ips.all()) == 1
    assert n.pool_size == 65435
    assert n.used_count == 1

    ip.release()
    assert len(n.ips.all()) == 0
    assert n.pool_size == 65436
    assert n.used_count == 0

    p = Pod.create('pod', 'pod', 10, -1)
    host = Host.create(p, random_ipv4(), random_string(prefix='host'),
                       random_uuid(), 4, 4096)

    gate = n.acquire_gateway_ip(host)
    assert gate is not None
    assert gate.network_id == n.id
    assert gate.vlan_address.startswith('10.1.0.')
    assert gate.vlan_seq_id == n.id
    assert gate.name == 'vlan.%02d.br' % n.id

    g = VLanGateway.get_by_host_and_network(host.id, n.id)
    assert g is not None
    assert g.id == gate.id
    assert len(host.list_vlans()) == 1

    assert n.used_gate_count == 1
    assert n.gate_pool_size == 99

    gate.release()
    assert n.used_gate_count == 0
    assert n.gate_pool_size == 100
    assert VLanGateway.get_by_host_and_network(host.id, n.id) is None
    assert len(host.list_vlans()) == 0
Exemplo n.º 32
0
def test_get_max_container_count_single_host(test_db):
    group = Group.create('group', 'group')
    pod = Pod.create('pod', 'pod', 10, -1)
    host = Host.create(pod, random_ipv4(), random_string(), random_uuid(), 64, 4096)
    host.assigned_to_group(group)

    assert get_max_container_count(group, pod, ncore=1, nshare=0) == 64
    assert get_max_container_count(group, pod, ncore=2, nshare=0) == 32
    assert get_max_container_count(group, pod, ncore=3, nshare=0) == 21
    assert get_max_container_count(group, pod, ncore=4, nshare=0) == 16
    assert get_max_container_count(group, pod, ncore=5, nshare=0) == 12
    assert get_max_container_count(group, pod, ncore=1, nshare=5) == 42
    assert get_max_container_count(group, pod, ncore=2, nshare=5) == 25
Exemplo n.º 33
0
def test_network(test_db):
    n = Network.create('net', '10.1.0.0/16')
    assert n is not None
    assert len(n.ips.all()) == 0
    assert n.hostmask_string == '16'
    assert n.pool_size == 65436
    assert n.used_count == 0
    assert n.used_gate_count == 0
    assert n.gate_pool_size == 100

    ip = n.acquire_ip()
    assert ip is not None
    assert ip.network_id == n.id
    assert ip.vethname == ''
    assert not ip.container_id
    assert ip.hostmask == n.hostmask_string
    assert ip.vlan_seq_id == n.id
    assert ip.address.startswith('10.1')

    assert len(n.ips.all()) == 1
    assert n.pool_size == 65435
    assert n.used_count == 1

    ip.release()
    assert len(n.ips.all()) == 0
    assert n.pool_size == 65436
    assert n.used_count == 0

    p = Pod.create('pod', 'pod', 10, -1)
    host = Host.create(p, random_ipv4(), random_string(prefix='host'), random_uuid(), 4, 4096)

    gate = n.acquire_gateway_ip(host)
    assert gate is not None
    assert gate.network_id == n.id
    assert gate.vlan_address.startswith('10.1.0.')
    assert gate.vlan_seq_id == n.id
    assert gate.name == 'vlan.%02d.br' % n.id

    g = VLanGateway.get_by_host_and_network(host.id, n.id)
    assert g is not None
    assert g.id == gate.id
    assert len(host.list_vlans()) == 1

    assert n.used_gate_count == 1
    assert n.gate_pool_size == 99

    gate.release()
    assert n.used_gate_count == 0
    assert n.gate_pool_size == 100
    assert VLanGateway.get_by_host_and_network(host.id, n.id) is None
    assert len(host.list_vlans()) == 0
Exemplo n.º 34
0
def test_host(test_db):
    g = Group.create('group', 'group')
    p = Pod.create('pod', 'pod')
    assert p.assigned_to_group(g)
    hosts = [Host.create(p, random_ipv4(), random_string(prefix='host'),
        random_uuid(), 4, 4096) for i in range(6)]
    for host in hosts:
        assert host is not None
        assert len(host.cores.all()) == 4
        assert len(host.get_free_cores()) == 4

    assert len(g.private_hosts.all()) == 0
    assert g.get_max_containers(p, 1) == 0
    assert g.get_free_cores(p, 1, 1) == {}

    for host in hosts[:3]:
        host.assigned_to_group(g)
    host_ids1 = {h.id for h in hosts[:3]}
    host_ids2 = {h.id for h in hosts[3:]}

    assert len(g.private_hosts.all()) == 3
    assert g.get_max_containers(p, 1) == 12
    host_cores = g.get_free_cores(p, 12, 1)
    assert len(host_cores) == 3
    
    for (host, count), cores in host_cores.iteritems():
        assert host.id in host_ids1
        assert host.id not in host_ids2
        assert count == 4
        assert len(cores) == 4

    assert g.get_max_containers(p, 3) == 3
    host_cores = g.get_free_cores(p, 3, 3)
    assert len(host_cores) == 3

    for (host, count), cores in host_cores.iteritems():
        assert host.id in host_ids1
        assert host.id not in host_ids2
        assert count == 1
        assert len(cores) == 3

    assert g.get_max_containers(p, 2) == 6
    host_cores = g.get_free_cores(p, 4, 2)
    assert len(host_cores) == 2

    for (host, count), cores in host_cores.iteritems():
        assert host.id in host_ids1
        assert host.id not in host_ids2
        assert count == 2
        assert len(cores) == 4
Exemplo n.º 35
0
def test_occupy_and_release_cores(test_db):
    g = Group.create('group', 'group')
    p = Pod.create('pod', 'pod', 10, -1)
    host = Host.create(p, random_ipv4(), random_string(), random_uuid(), 200, 0)
    assert p.assigned_to_group(g)
    assert host.assigned_to_group(g)

    for core in host.cores:
        assert core.host_id == host.id
        assert core.remain == 10

    cores = {
        'full': host.cores[:100],
        'part': host.cores[100:],
    }

    host.occupy_cores(cores, 5)
    for core in host.cores[:100]:
        assert core.remain == 0
    for core in host.cores[100:]:
        assert core.remain == 5

    host.release_cores(cores, 5)
    for core in host.cores:
        assert core.remain == 10

    host.occupy_cores(cores, 8)
    for core in host.cores[:100]:
        assert core.remain == 0
    for core in host.cores[100:]:
        assert core.remain == 2

    host.release_cores(cores, 8)
    for core in host.cores:
        assert core.remain == 10

    cores = {
        'full': host.cores[:50],
        'part': host.cores[50:100],
    }

    host.occupy_cores(cores, 8)
    for core in host.cores[:50]:
        assert core.remain == 0
    for core in host.cores[50:100]:
        assert core.remain == 2

    host.release_cores(cores, 8)
    for core in host.cores:
        assert core.remain == 10
Exemplo n.º 36
0
    def test_schema(self):
        database_schema = schema.DATABASE_SCHEMA
        valid_sqlite = {
            'sqlite' : {
                'database_file' : utils.random_string(suffix='.sql'),
            },
        }
        validate(valid_sqlite, database_schema)

        valid_mysql = {
            'mysql' : {
                'username' : utils.random_string(),
                'password' : utils.random_string(),
                'host' : utils.random_string(),
                'database_name' : utils.random_string(),
            },
        }
        validate(valid_mysql, database_schema)

        # make sure all args are required
        invalid_sqlite = {
            'sqlite' : {},
        }
        with self.assertRaises(ValidationError):
            validate(invalid_sqlite, database_schema)

        for key in valid_mysql['mysql']:
            invalid_mysql = deepcopy(valid_mysql)
            invalid_mysql['mysql'].pop(key)
            with self.assertRaises(ValidationError):
                validate(invalid_mysql, database_schema)

        for key in valid_mysql['mysql']:
            invalid_mysql = deepcopy(valid_mysql)
            invalid_mysql['mysql'][key] = None
            with self.assertRaises(ValidationError):
                validate(invalid_mysql, database_schema)
Exemplo n.º 37
0
def test_occupy_and_release_cores(test_db):
    p = Pod.create('pod', 'pod', 10, -1)
    host = Host.create(p, random_ipv4(), random_string(), random_uuid(), 200,
                       0)

    for core in host.cores:
        assert core.host_id == host.id
        assert core.remain == 10

    cores = {
        'full': host.cores[:100],
        'part': host.cores[100:],
    }

    host.occupy_cores(cores, 5)
    for core in host.cores[:100]:
        assert core.remain == 0
    for core in host.cores[100:]:
        assert core.remain == 5

    host.release_cores(cores, 5)
    for core in host.cores:
        assert core.remain == 10

    host.occupy_cores(cores, 8)
    for core in host.cores[:100]:
        assert core.remain == 0
    for core in host.cores[100:]:
        assert core.remain == 2

    host.release_cores(cores, 8)
    for core in host.cores:
        assert core.remain == 10

    cores = {
        'full': host.cores[:50],
        'part': host.cores[50:100],
    }

    host.occupy_cores(cores, 8)
    for core in host.cores[:50]:
        assert core.remain == 0
    for core in host.cores[50:100]:
        assert core.remain == 2

    host.release_cores(cores, 8)
    for core in host.cores:
        assert core.remain == 10
Exemplo n.º 38
0
def mock_user(full_name: str = None,
              email: pydantic.EmailStr = None,
              password: str = None,
              is_admin: bool = False):
    if full_name is None:
        full_name = f"{utils.random_string(length=10)} {utils.random_string(length=8)}"

    if email is None:
        email = str(full_name).replace(' ', '.')
        email += "@nymann.dev"

    if password is None:
        password = utils.random_string(length=28)
    return dict(email=email,
                name=full_name,
                is_admin=is_admin,
                password=password)
Exemplo n.º 39
0
    def test_big_value(self):
        store = MCStore(self.proxy.addr)
        key = 'largekey'
        size = 10 * 1024 * 1024
        rsize = (((size + len(key) + 24) >> 8) + 1) << 8
        string_large = random_string(size / 10) * 10

        self.assertTrue(store.set(key, string_large))
        self.assertEqual(store.get(key), string_large)
        self.update_pos(rsize)

        self.assertEqual(self.get_meta(store, key), (1, 0, self.last_pos))

        self.assertTrue(store.set(key, 'aaa'))
        self.update_pos(256)
        self.assertEqual(self.get_meta(store, key), (2, 0, self.last_pos))

        self.checkCounterZero()
Exemplo n.º 40
0
def create_multipart_form(data: bytes, fieldname: str, filename: str, content_type: str):
    """
    Basic emulation of a browser's multipart file upload
    """
    boundry = '----WebKitFormBoundary' + random_string(16)
    buff = io.BytesIO()
    buff.write(b'--')
    buff.write(boundry.encode())
    buff.write(b'\r\n')
    buff.write(('Content-Disposition: form-data; name="%s"; filename="%s"' % \
               (fieldname, filename)).encode())
    buff.write(b'\r\n')
    buff.write(('Content-Type: %s' % content_type).encode())
    buff.write(b'\r\n')
    buff.write(b'\r\n')
    buff.write(data)
    buff.write(b'\r\n')
    buff.write(boundry.encode())
    buff.write(b'--\r\n')
    headers = {'Content-Type': 'multipart/form-data; boundary=%s' %boundry}
    headers['Content-Length'] = str(buff.tell())
    return buff.getvalue(), headers
Exemplo n.º 41
0
def test_container(test_db):
    a = App.get_or_create('app', 'http://git.hunantv.com/group/app.git', '')
    assert a is not None

    v = a.add_version(random_sha1())
    assert v is not None
    assert v.app.id == a.id
    assert v.name == a.name
    assert len(v.containers.all()) == 0
    assert len(v.tasks.all()) == 0

    g = Group.create('group', 'group')
    p = Pod.create('pod', 'pod')
    assert p.assigned_to_group(g)
    hosts = [Host.create(p, random_ipv4(), random_string(prefix='host'),
        random_uuid(), 4, 4096) for i in range(6)]

    for host in hosts[:3]:
        host.assigned_to_group(g)
    host_ids1 = {h.id for h in hosts[:3]}
    host_ids2 = {h.id for h in hosts[3:]}

    assert g.get_max_containers(p, 3) == 3
    host_cores = g.get_free_cores(p, 3, 3)
    assert len(host_cores) == 3

    containers = []
    for (host, count), cores in host_cores.iteritems():
        cores_per_container = len(cores) / count
        for i in range(count):
            cid = random_sha1()
            used_cores = cores[i*cores_per_container:(i+1)*cores_per_container]
            # not using a port
            c = Container.create(cid, host, v, random_string(), 'entrypoint', used_cores, 'env')
            assert c is not None
            containers.append(c)
        host.occupy_cores(cores)

    for host in g.private_hosts.all():
        assert len(host.get_free_cores()) == 1
        assert len(host.containers.all()) == 1
        assert host.count == 1

    assert len(containers) == 3
    assert len(v.containers.all()) == 3

    for c in containers:
        assert c.host_id in host_ids1
        assert c.host_id not in host_ids2
        assert c.app.id == a.id
        assert c.version.id == v.id
        assert c.is_alive
        assert len(c.cores.all()) == 3
        all_core_labels = sorted(['0', '1', '2', '3', ])
        used_core_labels = [core.label for core in c.cores.all()]
        free_core_labels = [core.label for core in c.host.get_free_cores()]
        assert all_core_labels == sorted(used_core_labels + free_core_labels)

    for c in containers:
        c.delete()

    assert len(v.containers.all()) == 0
    assert g.get_max_containers(p, 3) == 3
    host_cores = g.get_free_cores(p, 3, 3)
    assert len(host_cores) == 3

    for host in g.private_hosts.all():
        assert len(host.get_free_cores()) == 4
        assert len(host.containers.all()) == 0
        assert host.count == 0
Exemplo n.º 42
0
def test_container(test_db):
    a = App.get_or_create('app', 'http://git.hunantv.com/group/app.git', '')
    assert a is not None
    assert a.id == a.user_id

    v = a.add_version(random_sha1())
    assert v is not None
    assert v.app.id == a.id
    assert v.name == a.name
    assert len(v.containers.all()) == 0
    assert len(v.tasks.all()) == 0

    g = Group.create('group', 'group')
    p = Pod.create('pod', 'pod', 10, -1)
    assert p.assigned_to_group(g)
    hosts = [Host.create(p, random_ipv4(), random_string(prefix='host'),
        random_uuid(), 4, 4096) for i in range(6)]

    for host in hosts[:3]:
        host.assigned_to_group(g)
    host_ids1 = {h.id for h in hosts[:3]}
    host_ids2 = {h.id for h in hosts[3:]}

    host_cores = g.get_free_cores(p, 3, 3, 0)

    #测试没有碎片核的情况
    #获取核
    containers = []
    for (host, count), cores in host_cores.iteritems():
        cores_per_container = len(cores['full']) / count
        for i in range(count):
            cid = random_sha1()
            used_cores = {'full': cores['full'][i*cores_per_container:(i+1)*cores_per_container]}
            # not using a port
            c = Container.create(cid, host, v, random_string(), 'entrypoint', used_cores, 'env')
            assert c is not None
            containers.append(c)
        host.occupy_cores(cores, 0)

    for host in g.private_hosts.all():
        full_cores, part_cores = host.get_free_cores()
        assert len(full_cores) == 1
        assert len(part_cores) == 0
        assert len(host.containers.all()) == 1
        assert host.count == 1

    assert len(containers) == 3
    assert len(v.containers.all()) == 3

    for c in containers:
        assert c.host_id in host_ids1
        assert c.host_id not in host_ids2
        assert c.app.id == a.id
        assert c.version.id == v.id
        assert c.is_alive
        assert len(c.full_cores.all()) == 3
        assert len(c.part_cores.all()) == 0
        all_core_labels = sorted(['0', '1', '2', '3', ])
        used_full_core_labels = [core.label for core in c.full_cores.all()]
        used_part_core_labels = [core.label for core in c.part_cores.all()]
        free_core_labels = [core.label for core in c.host.get_free_cores()[0]]
        assert all_core_labels == sorted(used_full_core_labels + used_part_core_labels + free_core_labels)

    #释放核
    for c in containers:
        c.delete()

    assert len(v.containers.all()) == 0
    assert g.get_max_containers(p, 3, 0) == 3
    host_cores = g.get_free_cores(p, 3, 3, 0)
    assert len(host_cores) == 3

    for host in g.private_hosts.all():
        full_cores, part_cores = host.get_free_cores()
        assert len(full_cores) == 4
        assert len(part_cores) == 0
        assert len(host.containers.all()) == 0
        assert host.count == 0

    #测试有碎片的情况
    #获取核
    host_cores = g.get_free_cores(p, 3, 3, 4)
    containers = []
    for (host, count), cores in host_cores.iteritems():
        cores_per_container = len(cores['full']) / count
        for i in range(count):
            cid = random_sha1()
            used_cores = {'full':  cores['full'][i*cores_per_container:(i+1)*cores_per_container],
                    'part': cores['part']}
            # not using a port
            c = Container.create(cid, host, v, random_string(), 'entrypoint', used_cores, 'env')
            assert c is not None
            containers.append(c)
        host.occupy_cores(cores, 4)

    for host in g.private_hosts.all():
        full_cores, part_cores = host.get_free_cores()
        assert len(full_cores) == 0
        assert len(part_cores) == 1
        assert part_cores[0].used == 4
        assert len(host.containers.all()) == 1
        assert host.count == 1

    assert len(containers) == 3
    assert len(v.containers.all()) == 3

    for c in containers:
        assert c.host_id in host_ids1
        assert c.host_id not in host_ids2
        assert c.app.id == a.id
        assert c.version.id == v.id
        assert c.is_alive
        assert len(c.full_cores.all()) == 3
        assert len(c.part_cores.all()) == 1
        all_core_labels = sorted(['0', '1', '2', '3', ])
        used_full_core_labels = [core.label for core in c.full_cores.all()]
        used_part_core_labels = [core.label for core in c.part_cores.all()]
        free_core_labels = [core.label for core in c.host.get_free_cores()[0]]
        assert all_core_labels == sorted(used_full_core_labels + used_part_core_labels + free_core_labels)


    #释放核
    for c in containers:
        c.delete(4)

    assert len(v.containers.all()) == 0
    assert g.get_max_containers(p, 3, 0) == 3
    host_cores = g.get_free_cores(p, 3, 3, 0)
    assert len(host_cores) == 3

    for host in g.private_hosts.all():
        full_cores, part_cores = host.get_free_cores()
        assert len(full_cores) == 4
        assert len(host.containers.all()) == 0
        assert host.count == 0

    #获取
    host_cores = g.get_free_cores(p, 6, 1, 5)
    containers = []
    for (host, count), cores in host_cores.iteritems():
        cores_per_container = len(cores['full']) / count
        for i in range(count):
            cid = random_sha1()
            used_cores = {'full':  cores['full'][i*cores_per_container:(i+1)*cores_per_container],
                    'part': cores['part'][i:i+1]}
            # not using a port
            c = Container.create(cid, host, v, random_string(), 'entrypoint', used_cores, 'env')
            assert c is not None
            containers.append(c)
            host.occupy_cores(used_cores, 5)

    for host in g.private_hosts.all():
        full_cores, part_cores = host.get_free_cores()
        assert len(full_cores) == 1
        assert len(part_cores) == 0
        assert len(host.containers.all()) == 2
        assert host.count == 2

    assert len(containers) == 6
    assert len(v.containers.all()) == 6

    for c in containers:
        assert c.host_id in host_ids1
        assert c.host_id not in host_ids2
        assert c.app.id == a.id
        assert c.version.id == v.id
        assert c.is_alive
        assert len(c.full_cores.all()) == 1
        assert len(c.part_cores.all()) == 1

    ##释放核
    for c in containers:
        c.delete(5)

    assert len(v.containers.all()) == 0
    assert g.get_max_containers(p, 3, 0) == 3
    host_cores = g.get_free_cores(p, 3, 3, 0)
    assert len(host_cores) == 3

    for host in g.private_hosts.all():
        full_cores, part_cores = host.get_free_cores()
        assert len(full_cores) == 4
        assert len(part_cores) == 0
        assert len(host.containers.all()) == 0
        assert host.count == 0
Exemplo n.º 43
0
#!/usr/bin/env python
# encoding: utf-8

from eru.app import create_app_with_celery
from eru.models import db, Group, Pod, Host, App
from tests.utils import random_ipv4, random_string, random_uuid, random_sha1

host_number = int(raw_input("how many hosts: "))

app, _ = create_app_with_celery
app.config['TESTING'] = True
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://root:@localhost:3306/erutest'

with app.app_context():
    db.create_all()
    
    a = App.get_or_create('app', 'http://git.hunantv.com/group/app.git', '')
    v = a.add_version(random_sha1())
    g = Group.create('group', 'group')
    p = Pod.create('pod', 'pod', 10, -1)
    p.assigned_to_group(g)
    hosts = [Host.create(p, random_ipv4(), random_string(), random_uuid(), 24, 4096) for i in range(host_number)]
    
    for host in hosts:
        host.assigned_to_group(g)
Exemplo n.º 44
0
def _create_data(core_share, max_share_core, host_count):
    pod = Pod.create('pod', 'pod', core_share, max_share_core)
    for _ in range(host_count):
        Host.create(pod, random_ipv4(), random_string(), random_uuid(), 16,
                    4096)
    return pod
Exemplo n.º 45
0
def dotfile_name():
    """Random name of a dotfile (without any path fragment)."""
    return "." + random_string(chars=string.ascii_letters, length=16)
Exemplo n.º 46
0
def test_host(test_db):
    g = Group.create('group', 'group')
    p = Pod.create('pod', 'pod', 10, -1)
    assert p.assigned_to_group(g)
    hosts = [Host.create(p, random_ipv4(), random_string(prefix='host'),
        random_uuid(), 4, 4096) for i in range(6)]
    for host in hosts:
        assert host is not None
        assert len(host.cores) == 4
        full_cores, part_cores = host.get_free_cores()
        assert len(full_cores) == 4
        assert len(part_cores) == 0

    assert len(g.private_hosts.all()) == 0
    assert get_max_container_count(g, p, 1, 0) == 0
    assert centralized_schedule(g, p, 1, 1, 0) == {}

    for host in hosts[:3]:
        host.assigned_to_group(g)
    host_ids1 = {h.id for h in hosts[:3]}
    host_ids2 = {h.id for h in hosts[3:]}

    assert len(g.private_hosts.all()) == 3
    assert get_max_container_count(g, p, 1, 0) == 12
    host_cores = centralized_schedule(g, p, 12, 1, 0)
    assert len(host_cores) == 3
    for (host, count), cores in host_cores.iteritems():
        assert host.id in host_ids1
        assert host.id not in host_ids2
        assert count == 4
        assert len(cores['full']) == 4

    assert get_max_container_count(g, p, 3, 0) == 3
    host_cores = centralized_schedule(g, p, 3, 3, 0)
    assert len(host_cores) == 3
    for (host, count), cores in host_cores.iteritems():
        assert host.id in host_ids1
        assert host.id not in host_ids2
        assert count == 1
        assert len(cores['full']) == 3

    assert get_max_container_count(g, p, 2, 0) == 6
    host_cores = centralized_schedule(g, p, 4, 2, 0)
    assert len(host_cores) == 2
    for (host, count), cores in host_cores.iteritems():
        assert host.id in host_ids1
        assert host.id not in host_ids2
        assert count == 2
        assert len(cores['full']) == 4

    assert get_max_container_count(g, p, 1, 1) == 9
    host_cores = centralized_schedule(g, p, 3, 1, 1)
    assert len(host_cores) == 1
    for (host, count), cores in host_cores.iteritems():
        assert host.id in host_ids1
        assert host.id not in host_ids2
        assert count == 3
        assert len(cores['full']) == 3
        assert len(cores['part']) == 3

    assert get_max_container_count(g, p, 2, 3) == 3
    host_cores = centralized_schedule(g, p, 3, 2, 3)
    assert len(host_cores) == 3
    for (host, count), cores in host_cores.iteritems():
        assert host.id in host_ids1
        assert host.id not in host_ids2
        assert count == 1
        assert len(cores['full']) == 2
        assert len(cores['part']) == 1
Exemplo n.º 47
0
def generate_comment_form_dto(**kwargs) -> CommentFormDto:
    """Generate a comment form"""
    return CommentFormDto(
        content=random_string(settings.rules.comment.content_min_char))
Exemplo n.º 48
0
def test_host(test_db):
    p = Pod.create('pod', 'pod', 10, -1)
    hosts = [
        Host.create(p, random_ipv4(), random_string(prefix='host'),
                    random_uuid(), 4, 4096) for i in range(6)
    ]
    for host in hosts:
        host.set_public()
        assert host is not None
        assert len(host.cores) == 4
        full_cores, part_cores = host.get_free_cores()
        assert len(full_cores) == 4
        assert len(part_cores) == 0

    assert len(p.get_private_hosts()) == 0
    assert get_max_container_count(p, 1, 0) == 0
    assert centralized_schedule(p, 1, 1, 0) == {}

    for host in hosts[:3]:
        host.set_private()
    host_ids1 = {h.id for h in hosts[:3]}
    host_ids2 = {h.id for h in hosts[3:]}

    assert get_max_container_count(p, 1, 0) == 12
    host_cores = centralized_schedule(p, 12, 1, 0)
    assert len(host_cores) == 3
    for (host, count), cores in host_cores.iteritems():
        assert host.id in host_ids1
        assert host.id not in host_ids2
        assert count == 4
        assert len(cores['full']) == 4

    assert get_max_container_count(p, 3, 0) == 3
    host_cores = centralized_schedule(p, 3, 3, 0)
    assert len(host_cores) == 3
    for (host, count), cores in host_cores.iteritems():
        assert host.id in host_ids1
        assert host.id not in host_ids2
        assert count == 1
        assert len(cores['full']) == 3

    assert get_max_container_count(p, 2, 0) == 6
    host_cores = centralized_schedule(p, 4, 2, 0)
    assert len(host_cores) == 2
    for (host, count), cores in host_cores.iteritems():
        assert host.id in host_ids1
        assert host.id not in host_ids2
        assert count == 2
        assert len(cores['full']) == 4

    assert get_max_container_count(p, 1, 1) == 9
    host_cores = centralized_schedule(p, 3, 1, 1)
    assert len(host_cores) == 1
    for (host, count), cores in host_cores.iteritems():
        assert host.id in host_ids1
        assert host.id not in host_ids2
        assert count == 3
        assert len(cores['full']) == 3
        assert len(cores['part']) == 3

    assert get_max_container_count(p, 2, 3) == 3
    host_cores = centralized_schedule(p, 3, 2, 3)
    assert len(host_cores) == 3
    for (host, count), cores in host_cores.iteritems():
        assert host.id in host_ids1
        assert host.id not in host_ids2
        assert count == 1
        assert len(cores['full']) == 2
        assert len(cores['part']) == 1
Exemplo n.º 49
0
def _create_data(core_share, max_share_core, host_count):
    pod = Pod.create('pod', 'pod', core_share, max_share_core)
    for _ in range(host_count):
        Host.create(pod, random_ipv4(), random_string(), random_uuid(), 16, 4096)
    return pod
Exemplo n.º 50
0
def test_container(test_db):
    a = App.get_or_create('app', 'http://git.hunantv.com/group/app.git')
    assert a is not None
    assert a.id == a.user_id

    v = a.add_version(random_sha1())
    assert v is not None
    assert v.app.id == a.id
    assert v.name == a.name
    assert len(v.containers.all()) == 0
    assert len(v.tasks.all()) == 0

    p = Pod.create('pod', 'pod', 10, -1)
    hosts = [
        Host.create(p, random_ipv4(), random_string(prefix='host'),
                    random_uuid(), 4, 4096) for i in range(6)
    ]

    for host in hosts[3:]:
        host.set_public()
    host_ids1 = {h.id for h in hosts[:3]}
    host_ids2 = {h.id for h in hosts[3:]}

    host_cores = centralized_schedule(p, 3, 3, 0)

    #测试没有碎片核的情况
    #获取核
    containers = []
    for (host, count), cores in host_cores.iteritems():
        host.occupy_cores(cores, 0)
        cores_per_container = len(cores['full']) / count
        for i in range(count):
            cid = random_sha1()
            used_cores = {
                'full':
                cores['full'][i * cores_per_container:(i + 1) *
                              cores_per_container]
            }
            c = Container.create(cid,
                                 host,
                                 v,
                                 random_string(),
                                 'entrypoint',
                                 used_cores,
                                 'env',
                                 nshare=0)
            assert c is not None
            containers.append(c)

    for host in p.get_private_hosts():
        full_cores, part_cores = host.get_free_cores()
        assert len(full_cores) == 1
        assert len(part_cores) == 0
        assert len(host.containers.all()) == 1
        assert host.count == 1

    assert len(containers) == 3
    assert len(v.containers.all()) == 3

    for c in containers:
        assert c.host_id in host_ids1
        assert c.host_id not in host_ids2
        assert c.app.id == a.id
        assert c.version.id == v.id
        assert c.is_alive
        assert len(c.full_cores) == 3
        assert len(c.part_cores) == 0
        all_core_labels = sorted([
            '0',
            '1',
            '2',
            '3',
        ])
        used_full_core_labels = [core.label for core in c.full_cores]
        used_part_core_labels = [core.label for core in c.part_cores]
        free_core_labels = [core.label for core in c.host.get_free_cores()[0]]
        assert all_core_labels == sorted(used_full_core_labels +
                                         used_part_core_labels +
                                         free_core_labels)

    #释放核
    for c in containers:
        c.delete()

    assert len(v.containers.all()) == 0
    assert get_max_container_count(p, 3, 0) == 3
    host_cores = centralized_schedule(p, 3, 3, 0)
    assert len(host_cores) == 3

    for host in p.get_private_hosts():
        full_cores, part_cores = host.get_free_cores()
        assert len(full_cores) == 4
        assert len(part_cores) == 0
        assert len(host.containers.all()) == 0
        assert host.count == 4

    #测试有碎片的情况
    #获取核
    host_cores = centralized_schedule(p, 3, 3, 4)
    containers = []
    for (host, count), cores in host_cores.iteritems():
        cores_per_container = len(cores['full']) / count
        host.occupy_cores(cores, 4)
        for i in range(count):
            cid = random_sha1()
            used_cores = {
                'full':
                cores['full'][i * cores_per_container:(i + 1) *
                              cores_per_container],
                'part':
                cores['part']
            }
            # not using a port
            c = Container.create(cid,
                                 host,
                                 v,
                                 random_string(),
                                 'entrypoint',
                                 used_cores,
                                 'env',
                                 nshare=4)
            assert c is not None
            containers.append(c)

    for host in p.get_private_hosts():
        full_cores, part_cores = host.get_free_cores()
        assert len(full_cores) == 0
        assert len(part_cores) == 1
        assert part_cores[0].remain == 6
        assert len(host.containers.all()) == 1
        assert host.count == D('0.6')

    assert len(containers) == 3
    assert len(v.containers.all()) == 3

    for c in containers:
        assert c.host_id in host_ids1
        assert c.host_id not in host_ids2
        assert c.app.id == a.id
        assert c.version.id == v.id
        assert c.is_alive
        assert len(c.full_cores) == 3
        assert len(c.part_cores) == 1
        all_core_labels = sorted([
            '0',
            '1',
            '2',
            '3',
        ])
        used_full_core_labels = [core.label for core in c.full_cores]
        used_part_core_labels = [core.label for core in c.part_cores]
        free_core_labels = [core.label for core in c.host.get_free_cores()[0]]
        assert all_core_labels == sorted(used_full_core_labels +
                                         used_part_core_labels +
                                         free_core_labels)

    #释放核
    for c in containers:
        c.delete()

    assert len(v.containers.all()) == 0
    assert get_max_container_count(p, 3, 0) == 3
    host_cores = centralized_schedule(p, 3, 3, 0)
    assert len(host_cores) == 3

    for host in p.get_private_hosts():
        full_cores, part_cores = host.get_free_cores()
        assert len(full_cores) == 4
        assert len(host.containers.all()) == 0
        assert host.count == 4

    #获取
    host_cores = centralized_schedule(p, 6, 1, 5)
    containers = []
    for (host, count), cores in host_cores.iteritems():
        cores_per_container = len(cores['full']) / count
        for i in range(count):
            cid = random_sha1()
            used_cores = {
                'full':
                cores['full'][i * cores_per_container:(i + 1) *
                              cores_per_container],
                'part':
                cores['part'][i:i + 1],
            }
            host.occupy_cores(used_cores, 5)
            # not using a port
            c = Container.create(cid,
                                 host,
                                 v,
                                 random_string(),
                                 'entrypoint',
                                 used_cores,
                                 'env',
                                 nshare=5)
            assert c is not None
            containers.append(c)

    for host in p.get_private_hosts():
        full_cores, part_cores = host.get_free_cores()
        assert len(full_cores) == 1
        assert len(part_cores) == 0
        assert len(host.containers.all()) == 2
        assert host.count == D('1')

    assert len(containers) == 6
    assert len(v.containers.all()) == 6

    for c in containers:
        assert c.host_id in host_ids1
        assert c.host_id not in host_ids2
        assert c.app.id == a.id
        assert c.version.id == v.id
        assert c.is_alive
        assert len(c.full_cores) == 1
        assert len(c.part_cores) == 1

    ##释放核
    for c in containers:
        c.delete()

    assert len(v.containers.all()) == 0
    assert get_max_container_count(p, 3, 0) == 3
    host_cores = centralized_schedule(p, 3, 3, 0)
    assert len(host_cores) == 3

    for host in p.get_private_hosts():
        full_cores, part_cores = host.get_free_cores()
        assert len(full_cores) == 4
        assert len(part_cores) == 0
        assert len(host.containers.all()) == 0
        assert host.count == 4
Exemplo n.º 51
0
def filename():
    """Random file name (without leading dot)."""
    return random_string(chars=string.ascii_letters, length=8)
Exemplo n.º 52
0
def random_inventory_status():
    return {
        "name": utils.random_string(8),
        "max_value": random.uniform(0, 200),
        "current_value": random.uniform(0, 200),
    }