def test_get_changed_files_with_exclusions(self): """ invoke_tools.vcs.git_scm.get_changed_files: Should return an array of all the changed files without exclusions """ def __se_commit(sha=None): second_commit = mock.Mock() if sha == "first_commit": first_commit = mock.Mock() diff1 = mock.Mock() diff1.a_path = "this/is/a/path" diff2 = mock.Mock() diff2.a_path = "this/is/also/a/path" diff3 = mock.Mock() diff3.a_path = "this/is/another/path" first_commit.diff = mock.Mock( return_value=[diff1, diff2, diff3]) return first_commit elif sha == "second_commit": return second_commit def __se_repo(search_parent_directories=False): repo = self.__se_repo_init(search_parent_directories) repo.commit = mock.Mock(side_effect=__se_commit) return repo with mock.patch('invoke_tools.vcs.git_scm.Repo', side_effect=__se_repo): git = vcs.Git() self.assertEqual( git.get_changed_files("first_commit", "second_commit", ["this/is/another"]), ["this/is/a/path", "this/is/also/a/path"])
def deploy(ctx, env=None): """ Deploys a given environment (or detects on Travis) to the cluster. """ if not env: env = __check_branch() lxc.Docker.pull(cli, "articulate/terragrunt:0.8.8") git = vcs.Git() version = git.get_version() lxc.Docker.login(cli) lxc.Docker.push(cli, [ "monarchsofcoding/chitchat:release-{0}".format(version), "monarchsofcoding/chitchat:release" ]) terragrunt_container = lxc.Docker.run( cli, "articulate/terragrunt:0.8.8", command="get", environment={ "AWS_ACCESS_KEY_ID": os.getenv("AWS_ACCESS_KEY_ID"), "AWS_SECRET_ACCESS_KEY": os.getenv("AWS_SECRET_ACCESS_KEY"), "TF_VAR_database_password": os.getenv("{0}_DB_PASSWORD".format(env)), "TF_VAR_secret_key_base": os.getenv("{0}_SECRET_KEY_BASE".format(env)), "TF_VAR_guardian_secret_key": os.getenv("{0}_GUARDIAN_SECRET_KEY".format(env)), "TF_VAR_container_version": version }, volumes=["{0}/terraform:/app".format(os.getcwd())], working_dir="/app/environments/{0}".format(env)) terragrunt_container = lxc.Docker.run( cli, "articulate/terragrunt:0.8.8", command="apply", environment={ "AWS_ACCESS_KEY_ID": os.getenv("AWS_ACCESS_KEY_ID"), "AWS_SECRET_ACCESS_KEY": os.getenv("AWS_SECRET_ACCESS_KEY"), "TF_VAR_database_password": os.getenv("{0}_DB_PASSWORD".format(env)), "TF_VAR_secret_key_base": os.getenv("{0}_SECRET_KEY_BASE".format(env)), "TF_VAR_guardian_secret_key": os.getenv("{0}_GUARDIAN_SECRET_KEY".format(env)), "TF_VAR_container_version": version }, volumes=["{0}/terraform:/app".format(os.getcwd())], working_dir="/app/environments/{0}".format(env)) pass
def test_get_branch(self): """ invoke_tools.vcs.git_scm.get_branch: Should return the current branch from the Git repository """ with mock.patch('invoke_tools.vcs.git_scm.Repo', side_effect=self.__se_repo_init): git = vcs.Git() self.assertEqual(git.get_branch(), "develop")
def test_init(self): """ invoke_tools.vcs.git_scm.init: Should initialise the Git object """ with mock.patch('invoke_tools.vcs.git_scm.Repo', side_effect=self.__se_repo_init): git = vcs.Git() self.assertIsInstance(git, vcs.Git)
def test_get_version(self): """ invoke_tools.vcs.git_scm.get_version: Should return the current version """ with mock.patch('invoke_tools.vcs.git_scm.Repo', side_effect=self.__se_repo_init): git = vcs.Git() self.assertEqual(git.get_version(), self.repo.commit())
def test_get_branch_detached(self): """ invoke_tools.vcs.git_scm.get_branch: Should return the HEAD if the current revision is detached """ def __se_repo(search_parent_directories=False): repo = self.__se_repo_init(search_parent_directories) repo.head.is_detached = True return repo with mock.patch('invoke_tools.vcs.git_scm.Repo', side_effect=__se_repo): git = vcs.Git() self.assertEqual(git.get_branch(), "HEAD")
def find_image(repository, tag): print("# Looking for {0}:{1}...".format(repository, tag)) imgs = cli.images(name=repository) git = vcs.Git() version = git.get_version() for img in imgs: if img['RepoTags']: for t in img['RepoTags']: if t == "{0}:{1}".format(repository, tag): print("# Found {0}:{1}".format(repository, tag)) return True print("# Could not find {0}:{1}".format(repository, tag)) return False
def destroy(ctx, env): """ Destroys a given environment on the cluster. """ env_dir = env lxc.Docker.pull(cli, "articulate/terragrunt:0.8.8") git = vcs.Git() version = git.get_version() terragrunt_container = lxc.Docker.run( cli, "articulate/terragrunt:0.8.8", command="get", environment={ "AWS_ACCESS_KEY_ID": os.getenv("AWS_ACCESS_KEY_ID"), "AWS_SECRET_ACCESS_KEY": os.getenv("AWS_SECRET_ACCESS_KEY"), "TF_VAR_database_password": os.getenv("{0}_DB_PASSWORD".format(env_dir)), "TF_VAR_secret_key_base": os.getenv("{0}_SECRET_KEY_BASE".format(env_dir)), "TF_VAR_guardian_secret_key": os.getenv("{0}_GUARDIAN_SECRET_KEY".format(env_dir)), "TF_VAR_container_version": version }, volumes=["{0}/terraform:/app".format(os.getcwd())], working_dir="/app/environments/{0}".format(env_dir)) terragrunt_container = lxc.Docker.run( cli, "articulate/terragrunt:0.8.8", command="destroy --force", environment={ "AWS_ACCESS_KEY_ID": os.getenv("AWS_ACCESS_KEY_ID"), "AWS_SECRET_ACCESS_KEY": os.getenv("AWS_SECRET_ACCESS_KEY"), "TF_VAR_database_password": "******", "TF_VAR_secret_key_base": "destroy", "TF_VAR_guardian_secret_key": "destroy", "TF_VAR_container_version": "destroy" }, volumes=["{0}/terraform:/app".format(os.getcwd())], working_dir="/app/environments/{0}".format(env_dir)) pass
def test_get_version_with_tags(self): """ invoke_tools.vcs.git_scm.get_version: Should return the current version tags if set """ tag1 = mock.Mock() tag2 = mock.Mock() def __se_repo(search_parent_directories=False): repo = self.__se_repo_init(search_parent_directories) tag1.commit = "asdasda" tag2.commit = repo.commit() repo.tags = [tag1, tag2] return repo with mock.patch('invoke_tools.vcs.git_scm.Repo', side_effect=__se_repo): git = vcs.Git() self.assertEqual(git.get_version(), tag2)
def test_get_jenkins2_branch(self): """ invoke_tools.vcs.git_scm.get_branch: Should return the current branch on Jenkins 2 """ def se_os_getenv(var): if var == 'BRANCH_NAME': return 'jenkins2' return None def __se_repo(search_parent_directories=False): repo = self.__se_repo_init(search_parent_directories) repo.head.is_detached = True return repo with mock.patch('invoke_tools.vcs.git_scm.Repo', side_effect=__se_repo): git = vcs.Git() with mock.patch('idflow.utils.os.getenv', side_effect=se_os_getenv): self.assertEqual(git.get_branch(), "jenkins2")
def build_dev_image(ctx): """ Builds development image to run tests on """ git = vcs.Git() version = git.get_version() lxc.Docker.build( cli, dockerfile='Dockerfile.dev', tag="monarchsofcoding/chitchat:android-dev-{0}".format(version)) cli.tag("monarchsofcoding/chitchat:android-dev-{0}".format(version), "monarchsofcoding/chitchat", "android-dev") lxc.Docker.login(cli) lxc.Docker.push(cli, [ "monarchsofcoding/chitchat:android-dev-{0}".format(version), "monarchsofcoding/chitchat:android-dev" ])
def build(ctx): """ Builds a Docker container for the Backend """ lxc.Docker.clean(cli, ["chit_chat/_build", "chit_chat/deps"]) git = vcs.Git() version = git.get_version() # TODO: Upgrade Phoenix as soon as it supports poision 3.0. Doing this stuff isn't pleasant. fix_gossip = "sed -i 's#\[opts\]#opts#' deps/libcluster/lib/strategy/gossip.ex" lxc.Docker.build(cli, dockerfile='Dockerfile.dev', tag="{0}-dev".format("chitchat-backend")) lxc.Docker.run( cli, tag="{0}-dev".format("chitchat-backend"), command= '/bin/sh -c "mix do deps.get && {0} && mix deps.compile && mix release --env=prod --verbose"' .format(fix_gossip), volumes=["{0}/chit_chat:/app".format(os.getcwd())], working_dir="/app", environment={ "TERM": "xterm", "MIX_ENV": "prod" }) src = "chit_chat/_build/prod/rel/{0}/releases/{1}/{0}.tar.gz".format( "chit_chat", "0.0.1") shutil.copyfile(src, "{0}.tar.gz".format("chit_chat")) lxc.Docker.build( cli, dockerfile='Dockerfile.app', tag="monarchsofcoding/chitchat:release-{0}".format(version)) cli.tag("monarchsofcoding/chitchat:release-{0}".format(version), "monarchsofcoding/chitchat", "release")
def test_infra(ctx): """ Tests that the terraform code is deployable by performing a plan. """ lxc.Docker.pull(cli, "articulate/terragrunt:0.8.8") git = vcs.Git() version = git.get_version() terragrunt_container = lxc.Docker.run( cli, "articulate/terragrunt:0.8.8", command="get", environment={ "AWS_ACCESS_KEY_ID": os.getenv("AWS_ACCESS_KEY_ID"), "AWS_SECRET_ACCESS_KEY": os.getenv("AWS_SECRET_ACCESS_KEY"), "TF_VAR_database_password": "******", "TF_VAR_secret_key_base": "test", "TF_VAR_guardian_secret_key": "test", "TF_VAR_container_version": version }, volumes=["{0}/terraform:/app".format(os.getcwd())], working_dir="/app/environments/alpha") terragrunt_container = lxc.Docker.run( cli, "articulate/terragrunt:0.8.8", command="plan", environment={ "AWS_ACCESS_KEY_ID": os.getenv("AWS_ACCESS_KEY_ID"), "AWS_SECRET_ACCESS_KEY": os.getenv("AWS_SECRET_ACCESS_KEY"), "TF_VAR_database_password": "******", "TF_VAR_secret_key_base": "test", "TF_VAR_guardian_secret_key": "test", "TF_VAR_container_version": version }, volumes=["{0}/terraform:/app".format(os.getcwd())], working_dir="/app/environments/alpha")
from invoke import task from docker import APIClient import os from invoke_tools import lxc, vcs, ci cli = APIClient(base_url='unix://var/run/docker.sock', timeout=600, version="auto") git = vcs.Git() git.print_all() @task def test(ctx): # jenkins = ci.Jenkins("https://ci.vjpatel.me", "invoke-tools", git) # # changed_files = git.get_changed_files( # jenkins.get_last_successful_build_sha(), # git.get_version(), # [ # "setup.py", # "tasks.py" # ] # ) # # if len(changed_files) <= 0: # print("No src or test files changed since last build") # return
def test(ctx): """ Tests the ChitChat Android client """ git = vcs.Git() version = git.get_version() if not find_image("monarchsofcoding/chitchat", "release-{0}".format(version)): print("Building Backend") ctx.run("cd ../../backend && invoke build") # os.chdir("{0}/../".format(os.getcwd())) # lxc.Docker.build(cli, # dockerfile='Dockerfile.dev', # tag="{0}-dev".format("chitchat-androidclient") # ) # os.chdir("{0}/e2e/".format(os.getcwd())) lxc.Docker.pull(cli, "monarchsofcoding/chitchat:android-dev") lxc.Docker.pull(cli, "postgres:latest") postgres_container = lxc.Docker.run(cli, 'postgres', command="", environment={ "POSTGRES_PASSWORD": "******", "POSTGRES_USER": "******", "POSTGRES_DB": "chit_chat_test" }, detach=True) backend_container = lxc.Docker.run( cli, "monarchsofcoding/chitchat:release-{0}".format(version), command='foreground', environment={ "SECRET_KEY_BASE": "G9XBaZMFhtWDxAaowHCBrrDVq8xVB3sfro8xiGnFXfidldnvf", "GUARDIAN_SECRET_KEY": "cQCWxKpcuixeB4ZAxCs04nrBGdKeJiHcmmCHbZPI6esGcLcfZVz1qw2796p3gWGA", "DATABASE_HOSTNAME": "postgres", "DATABASE_USERNAME": "******", "DATABASE_PASSWORD": "******", "DATABASE_NAME": "chit_chat_test", "DATABASE_PORT": "5432", "PORT": "4000", "MIX_ENV": "dev", "NODE_COOKIE": "test" }, detach=True, links={postgres_container.get('Id'): "postgres"}) preload_classes = "gradle compileUiTestReleaseSources" start_emulator = "screen -d -L -m -S emulator emulator64-x86 -avd nougat -noaudio -no-window -gpu off -verbose -qemu -vnc :1" print_screen_log = "sleep 5; cat screenlog.0" vnc_rec_start = "{0} && android-wait-for-emulator && screen -d -L -m -S vnc2flv flvrec.py -o ChitChatAndroid.flv :1".format( print_screen_log) instrumented_tests = "gradle connectedUiTestsDebugAndroidTest" vnc_rec_stop = "screen -X -S vnc2flv kill" stop_emulator = "screen -X -S emulator kill" avconv = "avconv -i ChitChatAndroid.flv -c:v libx264 -crf 19 -strict experimental ChitChatAndroid.mp4" try: lxc.Docker.run( cli, tag="monarchsofcoding/chitchat:android-dev", command= '/bin/bash -c "cd app && {0} && {1}; {2} && {3}; EXIT_CODE=$? && {4}; {5}; {6}; exit $EXIT_CODE"' .format(preload_classes, start_emulator, vnc_rec_start, instrumented_tests, vnc_rec_stop, stop_emulator, avconv), volumes=["{0}/../ChitChat:/app".format(os.getcwd())], working_dir="/app", environment={}, links={backend_container.get('Id'): "chitchat"}, privileged=True) finally: backend_logs = cli.logs(backend_container.get('Id'), stdout=True, stderr=True, timestamps=True) with open("backend_container.log", "w") as log_file: log_file.write(backend_logs.decode("utf-8").strip()) postgres_logs = cli.logs(postgres_container.get('Id'), stdout=True, stderr=True, timestamps=True) with open("postgres_container.log", "w") as log_file: log_file.write(postgres_logs.decode("utf-8").strip()) cli.stop(backend_container.get('Id')) cli.remove_container(backend_container.get('Id')) cli.stop(postgres_container.get('Id')) cli.remove_container(postgres_container.get('Id')) pass
def test(ctx): git = vcs.Git() version = git.get_version() if not find_image("monarchsofcoding/chitchat", "release-{0}".format(version)): print("Building Backend") ctx.run("cd ../../backend && invoke build") # os.chdir("{0}/../".format(os.getcwd())) # lxc.Docker.build(cli, # dockerfile='Dockerfile.dev', # tag="{0}-dev".format("chitchat-javaclient") # ) # os.chdir("{0}/e2e/".format(os.getcwd())) lxc.Docker.pull(cli, "monarchsofcoding/chitchat:desktop-dev") lxc.Docker.pull(cli, "postgres:latest") postgres_container = lxc.Docker.run(cli, 'postgres', command="", environment={ "POSTGRES_PASSWORD": "******", "POSTGRES_USER": "******", "POSTGRES_DB": "chit_chat_test" }, detach=True) backend_container = lxc.Docker.run( cli, "monarchsofcoding/chitchat:release-{0}".format(version), command='foreground', environment={ "SECRET_KEY_BASE": "G9XBaZMFhtWDxAaowHCBrrDVq8xVB3sfro8xiGnFXfidldnvf", "GUARDIAN_SECRET_KEY": "cQCWxKpcuixeB4ZAxCs04nrBGdKeJiHcmmCHbZPI6esGcLcfZVz1qw2796p3gWGA", "DATABASE_HOSTNAME": "postgres", "DATABASE_USERNAME": "******", "DATABASE_PASSWORD": "******", "DATABASE_NAME": "chit_chat_test", "DATABASE_PORT": "5432", "PORT": "4000", "MIX_ENV": "dev", "NODE_COOKIE": "test" }, detach=True, links={postgres_container.get('Id'): "postgres"}) vnc = "vnc4server -geometry 1920x1080 && export DISPLAY=:1" vnc_rec_start = "screen -d -L -m -S vnc2flv flvrec.py -P /vncpasswd -o ChitChatDesktop.flv :1" ui_tests = "gradle test --tests com.moc.chitchat.view.*" vnc_rec_stop = "screen -X -S vnc2flv kill" avconv = "avconv -i ChitChatDesktop.flv -c:v libx264 -crf 19 -strict experimental ChitChatDesktop.mp4" try: lxc.Docker.run( cli, tag="monarchsofcoding/chitchat:desktop-dev", command= '/bin/bash -c "{0} && {1} && {4}; EXIT_CODE=$? && {2} && {3}; exit $EXIT_CODE"' .format(vnc, vnc_rec_start, vnc_rec_stop, avconv, ui_tests), volumes=["{0}/../ChitChatDesktop:/app".format(os.getcwd())], working_dir="/app", environment={"CHITCHAT_ENV": "test"}, links={backend_container.get('Id'): "chitchat"}) finally: backend_logs = cli.logs(backend_container.get('Id'), stdout=True, stderr=True, timestamps=True) with open("backend_container.log", "w") as log_file: log_file.write(backend_logs.decode("utf-8").strip()) postgres_logs = cli.logs(postgres_container.get('Id'), stdout=True, stderr=True, timestamps=True) with open("postgres_container.log", "w") as log_file: log_file.write(postgres_logs.decode("utf-8").strip()) cli.stop(backend_container.get('Id')) cli.remove_container(backend_container.get('Id')) cli.stop(postgres_container.get('Id')) cli.remove_container(postgres_container.get('Id')) pass