From a9e8b88a09af1576e4c87b5e0ef2ccd52a890ff1 Mon Sep 17 00:00:00 2001 From: Assil Ksiksi Date: Mon, 19 Aug 2024 13:12:49 -0400 Subject: [PATCH] test: add test for empty environment variables --- nix_test.go | 20 ++++++ testdata/TestEmptyEnv.compose.yml | 14 +++++ testdata/TestEmptyEnv.docker.nix | 96 ++++++++++++++++++++++++++++ testdata/TestEmptyEnv.podman.nix | 101 ++++++++++++++++++++++++++++++ 4 files changed, 231 insertions(+) create mode 100644 testdata/TestEmptyEnv.compose.yml create mode 100644 testdata/TestEmptyEnv.docker.nix create mode 100644 testdata/TestEmptyEnv.podman.nix diff --git a/nix_test.go b/nix_test.go index 9142b4f..cbb9b50 100644 --- a/nix_test.go +++ b/nix_test.go @@ -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) +} diff --git a/testdata/TestEmptyEnv.compose.yml b/testdata/TestEmptyEnv.compose.yml new file mode 100644 index 0000000..86a8e07 --- /dev/null +++ b/testdata/TestEmptyEnv.compose.yml @@ -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= + diff --git a/testdata/TestEmptyEnv.docker.nix b/testdata/TestEmptyEnv.docker.nix new file mode 100644 index 0000000..5e33749 --- /dev/null +++ b/testdata/TestEmptyEnv.docker.nix @@ -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."; + }; + }; +} diff --git a/testdata/TestEmptyEnv.podman.nix b/testdata/TestEmptyEnv.podman.nix new file mode 100644 index 0000000..9ef3acf --- /dev/null +++ b/testdata/TestEmptyEnv.podman.nix @@ -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."; + }; + }; +}