Beispiel #1
0
def test_tell_release_name():
    _, content = run_under_click_context(tell_release_name, )
    assert content == DUMMY_APPNAME
    override_values = {'releaseName': DUMMY_OVERRIDE_RELEASE_NAME}
    yadu(override_values, f'{CHART_DIR_NAME}/values-{TEST_CLUSTER}.yaml')
    _, content = run_under_click_context(tell_release_name, )
    assert content == DUMMY_OVERRIDE_RELEASE_NAME
Beispiel #2
0
def test_registry():
    region_id = 'cn-hangzhou'
    repo_ns = 'big-company'
    registry = f'registry.{region_id}.aliyuncs.com/{repo_ns}'
    aliyun_registry = AliyunRegistry(
        access_key_id='hh',
        access_key_secret='hh',
        registry=registry,
    )
    tag = 'noway'
    _, image = run_under_click_context(
        aliyun_registry.make_image,
        args=[tag],
    )
    assert image == f'{registry}/{DUMMY_APPNAME}:{tag}'
    project = 'foo'
    registry_url = f'harbor.fake/{project}'
    harbor_registry = HarborRegistry(registry_url, 'fake-token')
    tag = 'noway'
    _, image = run_under_click_context(
        harbor_registry.make_image,
        args=[tag],
    )
    assert harbor_registry.registry == registry_url
    assert image == f'{registry_url}/{DUMMY_APPNAME}:{tag}'
Beispiel #3
0
def test_ignore_files():
    _, content = run_under_click_context(tell_git_ignore, )
    assert content.splitlines()

    _, content = run_under_click_context(make_docker_ignore, )
    with open(DOCKERIGNORE_NAME) as f:
        docker_ignore = f.read()
        docker_ignores = docker_ignore.splitlines()

    assert 'comment' not in docker_ignore
    assert '**/.git' in docker_ignores
    assert '!f1' in docker_ignores
    assert '!**/f2' in docker_ignores
    assert 'f3' in docker_ignores
    assert '**/f4' in docker_ignores
Beispiel #4
0
def test_schemas():
    bare_values = load_dummy_values()
    web_proc = bare_values['deployments']['web']
    # deploy is an alias for deployments
    bare_values['deploy'] = {'web': web_proc, 'another': web_proc}
    yadu(bare_values, DUMMY_VALUES_PATH)
    _, values = run_under_click_context(load_helm_values,
                                        (DUMMY_VALUES_PATH, ))
    assert values['deployments']['web'] == values['deployments']['another']
    assert values['cronjobs'] == {}
    build = values['build']
    assert build['prepare']['keep'] == [f'./{BUILD_TREASURE_NAME}']

    bare_values['volumeMounts'][0]['subPath'] = 'foo/bar'  # should be basename
    with pytest.raises(ValidationError) as e:
        HelmValuesSchema().load(bare_values)

    assert 'subPath should be' in str(e)

    false_ing = {'host': 'dummy', 'deployName': 'web'}
    with pytest.raises(ValidationError):
        IngressSchema().load(false_ing)

    bad_web = {'containerPort': 8000}
    with pytest.raises(ValidationError):
        IngressSchema().load(bad_web)
Beispiel #5
0
def test_load_helm_values():
    # test internal cluster values are correctly loaded
    _, values = run_under_click_context(load_helm_values, )
    assert values['registry'] == 'docker.io/timfeirg'
    assert values['domain'] == 'info'
    dummy_jobs = {
        'init': {
            'command': ['echo', 'nothing']
        },
    }
    override_values = {
        'jobs': dummy_jobs,
    }
    yadu(override_values, f'{CHART_DIR_NAME}/values-{TEST_CLUSTER}.yaml')
    _, values = run_under_click_context(load_helm_values, )
    assert values['jobs'] == dummy_jobs
Beispiel #6
0
def test_cluster_values_override():
    fake_registry = 'registry.example.com'
    override_values = {
        'registry': fake_registry,
    }
    yadu(override_values, f'{CHART_DIR_NAME}/values-{TEST_CLUSTER}.yaml')
    _, cc = run_under_click_context(tell_cluster_config, )
    assert cc['registry'] == fake_registry
Beispiel #7
0
def test_tell_job_names():
    override_values = {
        'jobs': DUMMY_JOBS_CLAUSE,
        'tests': DUMMY_TESTS_CLAUSE,
    }
    yadu(override_values, f'{CHART_DIR_NAME}/values-{TEST_CLUSTER}.yaml')
    _, content = run_under_click_context(tell_job_names, )
    assert set(content) == {'dummy-init'}
Beispiel #8
0
def test_lain_job_in_non_lain_app_directory():
    ensure_absent([CHART_DIR_NAME])
    command = ('which', 'lain')
    run(lain, args=['job', '--wait', '--force', *command])
    _, job_name = run_under_click_context(make_job_name,
                                          args=(command, ),
                                          obj={'appname': 'lain'})
    logs_res = kubectl('logs', f'-ljob-name={job_name}', capture_output=True)
    assert ensure_str(logs_res.stdout).strip() == '/usr/local/bin/lain'
Beispiel #9
0
def test_subprocess_run():
    cmd = ['helm', 'version', '--bad-flag']
    cmd_result, func_result = run_under_click_context(
        subprocess_run,
        args=[cmd],
        kwargs={'check': True},
        returncode=1,
    )
    # sensible output in stderr, rather than python traceback
    assert 'unknown flag: --bad-flag' in cmd_result.output

    cmd_result, func_result = run_under_click_context(
        subprocess_run,
        args=[cmd],
        kwargs={'abort_on_fail': True},
        returncode=1,
    )
    # abort_on_fail will not capture std
    assert 'unknown flag: --bad-flag' not in cmd_result.output

    cmd = ['helm', 'version']
    cmd_result, func_result = run_under_click_context(
        subprocess_run,
        args=[cmd],
        kwargs={
            'check': True,
            'capture_output': True
        },
    )
    assert 'version' in ensure_str(func_result.stdout)

    cmd = 'pwd | cat'
    _, func_result = run_under_click_context(
        subprocess_run,
        args=[cmd],
        kwargs={
            'shell': True,
            'capture_output': True,
            'check': True
        },
    )
    wd = ensure_str(func_result.stdout).strip()
    assert wd.endswith(DUMMY_REPO)
Beispiel #10
0
def test_lain_save():
    retag = f'{DUMMY_APPNAME}-again'
    run(lain, args=['save', '--retag', retag])
    _, meta = run_under_click_context(lain_meta)
    fname = docker_save_name(f'{retag}:{meta}')
    # file is generated in lain-cli repo root, not dummy dir, sorry
    exists_and_delete(join(CLI_DIR, '..', fname))
    retag = f'{DUMMY_APPNAME}-again:latest'
    run(lain, args=['save', '--retag', retag])
    fname = docker_save_name(retag)
    exists_and_delete(join(CLI_DIR, '..', fname))
Beispiel #11
0
def test_tell_helm_options():
    _, options = run_under_click_context(tell_helm_options, )
    internal_values_file = join(CLUSTER_VALUES_DIR,
                                f'values-{TEST_CLUSTER}.yaml')
    assert internal_values_file in set(options)
    set_values = parse_helm_set_clause_from_options(options)
    assert set_values['cluster'] == 'test'
    assert set_values.get('imageTag')

    def no_build_and_override_registry():
        obj = context().obj
        values = obj['values']
        del values['build']
        pairs = [('registry', RANDOM_STRING)]
        return tell_helm_options(pairs)

    _, options = run_under_click_context(no_build_and_override_registry, )
    set_values_again = parse_helm_set_clause_from_options(options)
    assert 'imageTag' not in set_values_again
    del set_values['imageTag']
    assert set_values_again.pop('registry', None) == RANDOM_STRING
    assert set_values_again == set_values

    def with_extra_values_file():
        obj = context().obj
        dic = {'labels': {'foo': 'bar'}}
        f = NamedTemporaryFile(prefix='values-extra', suffix='.yaml')
        yadu(dic, f)
        f.seek(0)
        obj['extra_values_file'] = f
        try:
            return tell_helm_options()
        finally:
            del f

    _, options = run_under_click_context(with_extra_values_file, )
    extra_values_file_name = basename(options.pop(-1))
    assert extra_values_file_name.startswith('values-extra')
    assert extra_values_file_name.endswith('.yaml')
Beispiel #12
0
def test_sts():
    # sts values are mostly the same with deploy, we just have to change a few
    # things to make it work
    values = load_dummy_values()
    sts = deepcopy(values['deployments']['web'])
    override_values = {
        'statefulSets': {
            'worker': sts,
        }
    }
    yadu(override_values, f'{CHART_DIR_NAME}/values-{TEST_CLUSTER}.yaml')
    run(lain, args=['deploy'])
    _, pod_name = run_under_click_context(pick_pod,
                                          kwargs={'proc_name': 'worker'})
    assert pod_name == f'{DUMMY_APPNAME}-worker-0'
Beispiel #13
0
def test_tell_all_clusters(mocker):
    test_cluster_values_file = f'values-{TEST_CLUSTER}.yaml'
    # we need at least two clusters to verify that tell_all_clusters are working correctly
    another_cluster_name = 'another'
    another_cluster_values_file = f'values-{another_cluster_name}.yaml'
    tempd = TemporaryDirectory()
    test_cluster_values_path = join(CLUSTER_VALUES_DIR,
                                    test_cluster_values_file)
    test_cluster_values = yalo(test_cluster_values_path)
    test_cluster_values['registry'] = 'another.example.com'
    shutil.copyfile(test_cluster_values_path,
                    join(tempd.name, test_cluster_values_file))
    yadu(test_cluster_values, join(tempd.name, another_cluster_values_file))
    mocker.patch('lain_cli.utils.KUBECONFIG_DIR', tempd.name)
    mocker.patch('lain_cli.utils.CLUSTER_VALUES_DIR', tempd.name)
    # touch kubeconfig-another
    Path(join(tempd.name, f'kubeconfig-{another_cluster_name}')).write_text('')
    Path(join(tempd.name, f'kubeconfig-{TEST_CLUSTER}')).write_text('')
    # now that kubeconfig and cluster values file are present, we can verify
    # CLUSTERS is correct
    _, ccs = run_under_click_context(tell_all_clusters, )
    assert set(ccs) == {TEST_CLUSTER, another_cluster_name}
    assert ccs['another']['registry'] == test_cluster_values['registry']
    tempd.cleanup()
Beispiel #14
0
def test_make_job_name():
    _, res = run_under_click_context(make_job_name, args=[''])
    assert res == 'dummy-5562bd9d33e0c6ce'  # this is a stable hash value
Beispiel #15
0
def test_tell_ingress_urls():
    _, urls = run_under_click_context(tell_ingress_urls, )
    assert set(urls) == set([f'{DUMMY_URL_HTTPS}/', f'{DUMMY_URL}/'])
Beispiel #16
0
def test_workflow(registry):
    # lain init should failed when chart directory already exists
    run(lain, args=['init'], returncode=1)
    # use -f to remove chart directory and redo
    run(lain, args=['init', '-f'])
    # lain use will switch current context switch to [TEST_CLUSTER]
    run(lain, args=['use', TEST_CLUSTER])
    # lain use will print current cluster
    res = run(lain, args=['use'])
    assert f'* {TEST_CLUSTER}' in ensure_str(res.stdout)
    # this makes sure lain-use can work when kubeconfig is absent
    ensure_absent(join(KUBECONFIG_DIR, 'config'))
    run(lain, args=['use', TEST_CLUSTER])
    # see if this image is actually present on registry
    res = run(lain, args=['image'])
    image_tag = res.stdout.strip().split(':')[-1]
    # should fail when using a bad image tag
    res = run(lain, args=['deploy', '--set', 'imageTag=noway'], returncode=1)
    assert 'image not found' in ensure_str(res.output).lower()
    cronjob_name = 'nothing'
    override_values = {
        # 随便加一个 job, 为了看下一次部署的时候能否顺利先清理掉这个 job
        'jobs': DUMMY_JOBS_CLAUSE,
        # 随便加一个 cronjob, 为了测试 lain create-job
        'cronjobs': {
            cronjob_name: {
                'schedule': '0 0 * * *',
                'command': ['echo', RANDOM_STRING],
            },
        },
    }
    yadu(override_values, f'{CHART_DIR_NAME}/values-{TEST_CLUSTER}.yaml')
    # use a built image to deploy
    run(lain,
        args=['--ignore-lint', 'deploy', '--set', f'imageTag={image_tag}'])
    res = run(lain, args=['create-job', cronjob_name])
    create_job_cmd = f'kubectl create job --from=cronjob/{DUMMY_APPNAME}-{cronjob_name} manual-test-{cronjob_name}'
    assert create_job_cmd in res.output
    # check service is up
    dummy_resp = url_get_json(DUMMY_URL)
    assert dummy_resp['env']['FOO'] == 'BAR'
    assert dummy_resp['secretfile'] == 'I\nAM\nBATMAN'
    # check if hostAliases is working
    assert 'localhost' in dummy_resp['hosts']
    assert 'local' in dummy_resp['hosts']
    # check imageTag is correct
    deployed_images = tell_deployed_images(DUMMY_APPNAME)
    assert len(deployed_images) == 1
    deployed_image = deployed_images.pop()
    assert deployed_image.endswith(image_tag)

    # check if init job succeeded
    wait_for_job_success()
    # run a extra job, to test lain job functionalities
    command = 'env'
    res = run(lain, args=['job', '--force', command])
    _, job_name = run_under_click_context(make_job_name, args=(command, ))
    pod_name = wait_for_job_success(job_name)
    _, pod_name_again = run_under_click_context(
        pick_pod, kwargs={'selector': f'job-name={job_name}'})
    # check if pick_pod works correctly
    assert pod_name == pod_name_again
    logs_res = kubectl('logs', pod_name, capture_output=True)
    logs = ensure_str(logs_res.stdout)
    assert 'FOO=BAR' in logs
    # 跑第二次只是为了看看清理过程能否顺利执行, 保证不会报错
    run(lain, args=['job', '--force', 'env'])

    values = load_dummy_values()
    web_proc = values['deployments']['web']
    web_proc.update({
        'imagePullPolicy': 'Always',
        'terminationGracePeriodSeconds': 1,
    })
    # add one extra ingress rule to values.yaml
    dev_host = f'{DUMMY_APPNAME}-dev'
    full_host = 'dummy.full.domain'
    values['ingresses'].extend([
        {
            'host': dev_host,
            'deployName': 'web-dev',
            'paths': ['/']
        },
        {
            'host': full_host,
            'deployName': 'web',
            'paths': ['/']
        },
    ])
    values['jobs'] = {'init': {'command': ['echo', 'migrate']}}
    yadu(values, DUMMY_VALUES_PATH)
    overrideReplicaCount = 3
    overrideImageTag = 'latest'
    # add another env
    run(lain, args=['env', 'add', 'SCALE=BANANA'])
    web_dev_proc = deepcopy(web_proc)
    web_dev_proc.update({
        'replicaCount': overrideReplicaCount,
        'imageTag': overrideImageTag,
    })
    # adjust replicaCount and imageTag in override values file
    override_values = {
        'deployments': {
            'web-dev': web_dev_proc,
        },
        # this is just used to ensure helm template rendering
        'ingressAnnotations': {
            'nginx.ingress.kubernetes.io/proxy-next-upstream-timeout': 1,
        },
        'externalIngresses': [
            {
                'host': 'dummy-public.foo.cn',
                'deployName': 'web',
                'paths': ['/']
            },
            {
                'host': 'dummy-public.bar.cn',
                'deployName': 'web',
                'paths': ['/']
            },
        ],
    }
    yadu(override_values, f'{CHART_DIR_NAME}/values-{TEST_CLUSTER}.yaml')

    def get_helm_values():
        ctx = context()
        helm_values = ctx.obj['values']
        return helm_values

    # check if values-[TEST_CLUSTER].yaml currectly overrides helm context
    _, helm_values = run_under_click_context(get_helm_values)
    assert helm_values['deployments']['web-dev'][
        'replicaCount'] == overrideReplicaCount

    # deploy again to create newly added ingress rule
    run(lain, args=['deploy', '--set', f'imageTag={DUMMY_IMAGE_TAG}'])
    # check if the new ingress rule is created
    res = kubectl(
        'get',
        'ing',
        '-l',
        f'app.kubernetes.io/name={DUMMY_APPNAME}',
        '-o=jsonpath={..metadata.name}',
        capture_output=True,
    )
    assert not res.returncode
    domain = TEST_CLUSTER_CONFIG['domain']
    assert set(res.stdout.decode('utf-8').split()) == {
        tell_ing_name(full_host, DUMMY_APPNAME, domain, 'web'),
        tell_ing_name(DUMMY_APPNAME, DUMMY_APPNAME, domain, 'web'),
        f'dummy-public-foo-cn-{DUMMY_APPNAME}-web',
        tell_ing_name(dev_host, DUMMY_APPNAME, domain, 'web-dev'),
        f'dummy-public-bar-cn-{DUMMY_APPNAME}-web',
    }
    # check pod name match its corresponding deploy name
    dummy_resp = url_get_json(DUMMY_URL)
    assert tell_pod_deploy_name(
        dummy_resp['env']['HOSTNAME']) == f'{DUMMY_APPNAME}-web'
    dummy_dev_resp = url_get_json(DUMMY_DEV_URL)
    assert (tell_pod_deploy_name(
        dummy_dev_resp['env']['HOSTNAME']) == f'{DUMMY_APPNAME}-web-dev')
    # env is overriden in dummy-dev, see default values.yaml
    assert dummy_dev_resp['env']['FOO'] == 'BAR'
    assert dummy_dev_resp['env']['SCALE'] == 'BANANA'
    assert dummy_dev_resp['env']['LAIN_CLUSTER'] == TEST_CLUSTER
    assert dummy_dev_resp['env']['K8S_NAMESPACE'] == TEST_CLUSTER_CONFIG.get(
        'namespace', 'default')
    assert dummy_dev_resp['env']['IMAGE_TAG'] == DUMMY_IMAGE_TAG
    # check if replicaCount is correctly overriden
    res = kubectl(
        'get',
        'deploy',
        f'{DUMMY_APPNAME}-web-dev',
        '-o=jsonpath={.spec.replicas}',
        capture_output=True,
    )
    assert res.stdout.decode('utf-8').strip() == str(overrideReplicaCount)
    # check if imageTag is correctly overriden
    web_image = get_deploy_image(f'{DUMMY_APPNAME}-web')
    assert web_image.endswith(DUMMY_IMAGE_TAG)
    web_dev_image = get_deploy_image(f'{DUMMY_APPNAME}-web-dev')
    assert web_dev_image.endswith(overrideImageTag)
    # rollback imageTag for web-dev using `lain update_image`
    run(lain, args=['update-image', 'web-dev'])
    # restart a few times to test lain restart functionalities
    run(lain, args=['restart', 'web', '--wait'])
    run(lain, args=['restart', '--graceful', '--wait'])
    dummy_dev_resp = url_get_json(DUMMY_DEV_URL)
    # if dummy-dev is at the correct imageTag, that means lain update-image is
    # working correctly, and lain restart too
    assert dummy_dev_resp['env']['IMAGE_TAG'] == DUMMY_IMAGE_TAG
    run(lain, args=['--auto-pilot', 'env', 'add', f'treasure={RANDOM_STRING}'])
    dummy_resp = url_get_json(DUMMY_URL)
    # --auto-pilot will trigger a graceful restart, verify by confirming the
    # added env inside the freshly created containers
    assert dummy_resp['env']['treasure'] == RANDOM_STRING
Beispiel #17
0
def test_banyun():
    cli_result, _ = run_under_click_context(banyun, ('not-a-image', ),
                                            returncode=1)
    assert 'not a valid image tag' in cli_result.stdout
Beispiel #18
0
def test_tell_cluster():
    _, func_result = run_under_click_context(tell_cluster)
    assert func_result == TEST_CLUSTER
Beispiel #19
0
def test_build(registry):
    stage = 'prepare'

    def _prepare():
        obj = context().obj
        values = obj['values']
        build_clause = values['build']
        build_clause['prepare']['env'] = {
            'prepare_env': BUILD_TREASURE_NAME,
            'escape_test': 'space test & newline \n test',
        }
        build_clause['prepare']['keep'].extend([
            'foo/thing.txt',
            'bar',
        ])
        build_clause['prepare']['script'].extend([
            f'echo {RANDOM_STRING} > {BUILD_TREASURE_NAME}',
            'mkdir foo bar',
            'touch foo/thing.txt bar/thing.txt',
        ])
        lain_build(stage=stage)

    run_under_click_context(_prepare)
    _, prepare_image = run_under_click_context(lain_image,
                                               kwargs={'stage': stage})
    res = docker_run(prepare_image, ['ls'])
    ls_result = parse_ls(res.stdout)
    # ensure keep clause works as expected
    assert ls_result == {'foo', 'bar', BUILD_TREASURE_NAME}
    res = docker_run(prepare_image, ['env'])
    envs = ensure_str(res.stdout).splitlines()
    assert f'prepare_env={BUILD_TREASURE_NAME}' in envs
    assert 'escape_test=space test & newline \\n test' in envs

    stage = 'build'

    def _build_without_prepare():
        obj = context().obj
        values = obj['values']
        build_clause = values['build']
        build_clause['env'] = {'build_env': BUILD_TREASURE_NAME}
        del build_clause['prepare']
        lain_build(stage=stage, push=False)

    run_under_click_context(_build_without_prepare)
    _, build_image = run_under_click_context(lain_image,
                                             kwargs={'stage': stage})
    # 这次 build 是虚假的, 没有经过 prepare 步骤, 所以肯定不会有 treasure.txt
    res = docker_run(build_image, ['ls'])
    ls_result = parse_ls(res.stdout)
    assert 'run.py' in ls_result
    assert BUILD_TREASURE_NAME not in ls_result
    res = docker_run(build_image, ['env'])
    assert f'build_env={BUILD_TREASURE_NAME}' in ensure_str(res.stdout)

    def _build():
        obj = context().obj
        values = obj['values']
        build_clause = values['build']
        build_clause['script'].append(
            f'echo {RANDOM_STRING} >> {BUILD_TREASURE_NAME}')
        lain_build(stage=stage)

    run_under_click_context(_build)
    _, build_image = run_under_click_context(lain_image,
                                             kwargs={'stage': stage})
    res = docker_run(build_image, ['env'])
    env_lines = ensure_str(res.stdout).splitlines()
    _, meta = run_under_click_context(lain_meta)
    assert f'LAIN_META={meta}' in env_lines
    res = docker_run(build_image, ['cat', BUILD_TREASURE_NAME])
    treasure = ensure_str(res.stdout).strip()
    # 这个文件被我打印了两次随机串进去, 因此应该就两行...无聊的测试
    assert treasure == f'{RANDOM_STRING}\n{RANDOM_STRING}'
    res = docker_run(build_image, ['ls'])
    ls_result = parse_ls(res.stdout)
    assert 'run.py' in ls_result
    run(lain, args=['push'])
    recent_tags = registry.list_tags(DUMMY_APPNAME)
    latest_tag = max(t for t in recent_tags if t != 'latest')
    assert build_image.rsplit(':', 1)[-1] == latest_tag

    stage = 'release'

    def _release():
        obj = context().obj
        values = obj['values']
        values['release'] = {
            'env': {
                'release_env': BUILD_TREASURE_NAME
            },
            'dest_base':
            'python:latest',
            'workdir':
            DEFAULT_WORKDIR,
            'script': [],
            'copy': [
                {
                    'src': '/lain/app/treasure.txt',
                    'dest': '/lain/app/treasure.txt'
                },
                {
                    'src': '/lain/app/treasure.txt',
                    'dest': '/etc'
                },
            ],
        }
        lain_build(stage=stage, push=False)

    run_under_click_context(_release)
    _, release_image = run_under_click_context(lain_image,
                                               kwargs={'stage': stage})
    res = docker_run(release_image, ['ls'])
    ls_result = parse_ls(res.stdout)
    assert ls_result == {BUILD_TREASURE_NAME}
    res = docker_run(release_image,
                     ['ls', '-alh', f'/etc/{BUILD_TREASURE_NAME}'])
    ls_result = ensure_str(res.stdout).strip()
    # check file permission
    assert '1001 1001' in ls_result
    assert ls_result.endswith(f'/etc/{BUILD_TREASURE_NAME}')
    res = docker_run(release_image, ['cat', BUILD_TREASURE_NAME])
    treasure = ensure_str(res.stdout).strip()
    # 构建 release 镜像的时候, 由于并没有超载 build.script, 因此 treasure
    # 里只有一行
    assert treasure == f'{RANDOM_STRING}'
    res = docker_run(release_image, ['env'])
    assert f'release_env={BUILD_TREASURE_NAME}' in ensure_str(res.stdout)