Skip to content

Commit

Permalink
Merge branch 'dev' into fix/vault-server
Browse files Browse the repository at this point in the history
  • Loading branch information
MatteoVoges committed Sep 28, 2023
2 parents 235a462 + df5ecce commit fee5492
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 11 deletions.
3 changes: 1 addition & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ test_docker:
.PHONY: test_coverage
test_coverage:
@echo ----- Testing code coverage -----
coverage run --source=kapitan --omit="*reclass*" -m unittest discover
coverage report --fail-under=65 -m
python tests/vault_server.py

.PHONY: test_formatting
test_formatting:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -380,4 +380,9 @@ def load_config(self, path: str):
return [], {}
classes = OmegaConf.create(config.get("classes", []))
parameters = OmegaConf.create(config.get("parameters", {}))

# add metadata to nodes
filename = os.path.splitext(os.path.split(path)[1])[0]
parameters._set_flag(["filename", "path"], [filename, path], recursive=True)

return classes, parameters
2 changes: 1 addition & 1 deletion kapitan/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import kapitan.cached as cached
from kapitan import __file__ as kapitan_install_path
from kapitan.errors import CompileError, InventoryError, KapitanError
from kapitan.inventory.omegaconf import OmegaConfBackend
from kapitan.inventory.omegaconf_inv import OmegaConfBackend
from kapitan.inventory.reclass import ReclassBackend
from kapitan.utils import (
PrettyDumper,
Expand Down
2 changes: 1 addition & 1 deletion tests/test_omegaconf.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import yaml

from kapitan.inventory.omegaconf import OmegaConfBackend
from kapitan.inventory.omegaconf_inv import OmegaConfBackend


class OmegaConfMigrationTest(unittest.TestCase):
Expand Down
103 changes: 96 additions & 7 deletions tests/vault_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class VaultServer:

def __init__(self, ref_path, name=None):
self.docker_client = docker.from_env()
self.port = self.find_free_port()
self.socket, self.port = self.find_free_port()
self.container = self.setup_container(name)

self.ref_path = ref_path
Expand Down Expand Up @@ -58,12 +58,11 @@ def setup_container(self, name=None):
return container

def find_free_port(self):
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
sock.bind(("", 0))
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
port = sock.getsockname()[1]
sock.close()
return port
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.bind(("", 0))
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
port = sock.getsockname()[1]
return sock, port

def setup_vault(self):
token = self.initialize()
Expand Down Expand Up @@ -141,3 +140,93 @@ def get_policy(self):
}
"""
return test_policy


# try:
# server = VaultServer("", "test_vaultkv")
# server.setup_vault()
# for log in server.container.logs().decode().split("\n"):
# print(log)
# except Exception as e:
# print("logs:")
# for log in server.container.logs().decode().split("\n"):
# print(log)
# raise e
# finally:
# server.socket.close()
# server.container.stop()
# server.docker_client.close()

# coverage run --source=kapitan --omit="*reclass*" -m unittest discover
# coverage report --fail-under=65 -m


def main():
try:
docker_client = docker.from_env()
env = {
"VAULT_LOCAL_CONFIG": '{"ui": true, "backend": {"file": {"path": "/vault/file"}}, "listener":{"tcp":{"address":"0.0.0.0:8200","tls_disable":"true"}}}'
}
port = 0
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
sock.bind(("", 0))
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
port = sock.getsockname()[1]

container = docker_client.containers.run(
image="hashicorp/vault",
cap_add=["IPC_LOCK"],
ports={"8200/tcp": port},
# environment=env,
detach=True,
remove=True,
command=["server", "-dev", "-dev-root-token-id=test", "-dev-listen-address=0.0.0.0:8200"],
name="test_vaultkv",
)
# make sure the container is up & running before testing
while container.status != "running":
sleep(1)
# print(vars(container))
container.reload()

print("CONTAINER RUNNING ON ", port)

print("logs after starting:")
for log in container.logs().decode().split("\n"):
print(log)

adress = vars(container)["attrs"]["NetworkSettings"]["IPAddress"]
import requests
try:
r = requests.get(f'http://{adress}:{port}/')
print("curl to address:", r.status_code)
except:
print("curl to address failed")

try:
r = requests.get(f'http://localhost:{port}/')
print("curl to localhost:", r.status_code)
except:
print("curl to locaholst failed")

initialized_client = hvac.Client(url=f"http://localhost:{port}", token="test")
print("logs after client:")
for log in container.logs().decode().split("\n"):
print(log)


print(initialized_client.is_authenticated())
print("logs after authenticate:")
for log in container.logs().decode().split("\n"):
print(log)
except Exception as e:
print("logs:")
for log in container.logs().decode().split("\n"):
print(log)
raise e
finally:
container.stop()
docker_client.close()


main()

0 comments on commit fee5492

Please sign in to comment.