Skip to content

Commit

Permalink
test: add test for empty environment variables
Browse files Browse the repository at this point in the history
  • Loading branch information
aksiksi committed Aug 19, 2024
1 parent 6e1a885 commit a9e8b88
Show file tree
Hide file tree
Showing 4 changed files with 231 additions and 0 deletions.
20 changes: 20 additions & 0 deletions nix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,3 +237,23 @@ func TestNoRestart(t *testing.T) {
}
runSubtestsWithGenerator(t, g)
}

// Verifies that we adhere to spec.
// https://github.com/compose-spec/compose-spec/blob/main/spec.md#environment
func TestEmptyEnv(t *testing.T) {
composePath, _ := getPaths(t, false)

// Setup an env file that overrides an empty env var.
p := path.Join(t.TempDir(), "test.env")
content := "EMPTY_BUT_OVERRIDDEN_BY_ENV_FILE=abcde"
if err := os.WriteFile(p, []byte(content), 0666); err != nil {
t.Fatal(err)
}

g := &Generator{
Inputs: []string{composePath},
Project: NewProject("test"),
EnvFiles: []string{p},
}
runSubtestsWithGenerator(t, g)
}
14 changes: 14 additions & 0 deletions testdata/TestEmptyEnv.compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
services:
service-a:
image: nginx:latest
environment:
SOME_EMPTY_ENV_VAR:
EMPTY_BUT_OVERRIDDEN_BY_ENV_FILE:
NOT_EMPTY: ""
service-b:
image: nginx:latest
environment:
- ANOTHER_EMPTY_ENV_VAR
- EMPTY_BUT_OVERRIDDEN_BY_ENV_FILE
- ANOTHER_NOT_EMPTY=

96 changes: 96 additions & 0 deletions testdata/TestEmptyEnv.docker.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
{ pkgs, lib, ... }:

{
# Runtime
virtualisation.docker = {
enable = true;
autoPrune.enable = true;
};
virtualisation.oci-containers.backend = "docker";

# Containers
virtualisation.oci-containers.containers."test-service-a" = {
image = "nginx:latest";
environment = {
"EMPTY_BUT_OVERRIDDEN_BY_ENV_FILE" = "abcde";
"NOT_EMPTY" = "";
};
log-driver = "journald";
autoStart = false;
extraOptions = [
"--network-alias=service-a"
"--network=test_default"
];
};
systemd.services."docker-test-service-a" = {
serviceConfig = {
Restart = lib.mkOverride 500 "no";
};
after = [
"docker-network-test_default.service"
];
requires = [
"docker-network-test_default.service"
];
partOf = [
"docker-compose-test-root.target"
];
wantedBy = [
"docker-compose-test-root.target"
];
};
virtualisation.oci-containers.containers."test-service-b" = {
image = "nginx:latest";
environment = {
"ANOTHER_NOT_EMPTY" = "";
"EMPTY_BUT_OVERRIDDEN_BY_ENV_FILE" = "abcde";
};
log-driver = "journald";
autoStart = false;
extraOptions = [
"--network-alias=service-b"
"--network=test_default"
];
};
systemd.services."docker-test-service-b" = {
serviceConfig = {
Restart = lib.mkOverride 500 "no";
};
after = [
"docker-network-test_default.service"
];
requires = [
"docker-network-test_default.service"
];
partOf = [
"docker-compose-test-root.target"
];
wantedBy = [
"docker-compose-test-root.target"
];
};

# Networks
systemd.services."docker-network-test_default" = {
path = [ pkgs.docker ];
serviceConfig = {
Type = "oneshot";
RemainAfterExit = true;
ExecStop = "docker network rm -f test_default";
};
script = ''
docker network inspect test_default || docker network create test_default
'';
partOf = [ "docker-compose-test-root.target" ];
wantedBy = [ "docker-compose-test-root.target" ];
};

# Root service
# When started, this will automatically create all resources and start
# the containers. When stopped, this will teardown all resources.
systemd.targets."docker-compose-test-root" = {
unitConfig = {
Description = "Root target generated by compose2nix.";
};
};
}
101 changes: 101 additions & 0 deletions testdata/TestEmptyEnv.podman.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
{ pkgs, lib, ... }:

{
# Runtime
virtualisation.podman = {
enable = true;
autoPrune.enable = true;
dockerCompat = true;
defaultNetwork.settings = {
# Required for container networking to be able to use names.
dns_enabled = true;
};
};
virtualisation.oci-containers.backend = "podman";

# Containers
virtualisation.oci-containers.containers."test-service-a" = {
image = "nginx:latest";
environment = {
"EMPTY_BUT_OVERRIDDEN_BY_ENV_FILE" = "abcde";
"NOT_EMPTY" = "";
};
log-driver = "journald";
autoStart = false;
extraOptions = [
"--network-alias=service-a"
"--network=test_default"
];
};
systemd.services."podman-test-service-a" = {
serviceConfig = {
Restart = lib.mkOverride 500 "no";
};
after = [
"podman-network-test_default.service"
];
requires = [
"podman-network-test_default.service"
];
partOf = [
"podman-compose-test-root.target"
];
wantedBy = [
"podman-compose-test-root.target"
];
};
virtualisation.oci-containers.containers."test-service-b" = {
image = "nginx:latest";
environment = {
"ANOTHER_NOT_EMPTY" = "";
"EMPTY_BUT_OVERRIDDEN_BY_ENV_FILE" = "abcde";
};
log-driver = "journald";
autoStart = false;
extraOptions = [
"--network-alias=service-b"
"--network=test_default"
];
};
systemd.services."podman-test-service-b" = {
serviceConfig = {
Restart = lib.mkOverride 500 "no";
};
after = [
"podman-network-test_default.service"
];
requires = [
"podman-network-test_default.service"
];
partOf = [
"podman-compose-test-root.target"
];
wantedBy = [
"podman-compose-test-root.target"
];
};

# Networks
systemd.services."podman-network-test_default" = {
path = [ pkgs.podman ];
serviceConfig = {
Type = "oneshot";
RemainAfterExit = true;
ExecStop = "podman network rm -f test_default";
};
script = ''
podman network inspect test_default || podman network create test_default
'';
partOf = [ "podman-compose-test-root.target" ];
wantedBy = [ "podman-compose-test-root.target" ];
};

# Root service
# When started, this will automatically create all resources and start
# the containers. When stopped, this will teardown all resources.
systemd.targets."podman-compose-test-root" = {
unitConfig = {
Description = "Root target generated by compose2nix.";
};
};
}

0 comments on commit a9e8b88

Please sign in to comment.