From 5217e1c6cfe8c2a3c58a0d278b0d9097cab02b5e Mon Sep 17 00:00:00 2001 From: mrproliu <741550557@qq.com> Date: Mon, 23 Dec 2024 15:47:39 +0800 Subject: [PATCH] Support importing external variables in the `setup.init-system-environment` file (#129) --- CHANGES.md | 1 + internal/util/testdata/env | 17 +++++++ internal/util/utils.go | 18 ++++++- internal/util/utils_test.go | 49 +++++++++++++++++++ .../fail-fast/internal/expected.yaml | 4 +- .../non-fail-fast/internal/expected.yaml | 4 +- .../fail-fast/internal/expected.yaml | 4 +- .../non-fail-fast/internal/expected.yaml | 4 +- 8 files changed, 92 insertions(+), 9 deletions(-) create mode 100644 internal/util/testdata/env create mode 100644 internal/util/utils_test.go diff --git a/CHANGES.md b/CHANGES.md index fd201cde..5eb90cd9 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -7,6 +7,7 @@ Release Notes. #### Features * Adding `setup.kind.no-wait` to support should wait for the kind cluster to be ready or not. +* Support importing external variables in the `setup.init-system-environment` file. #### Bug Fixes diff --git a/internal/util/testdata/env b/internal/util/testdata/env new file mode 100644 index 00000000..a5427fcf --- /dev/null +++ b/internal/util/testdata/env @@ -0,0 +1,17 @@ +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You under the Apache License, Version 2.0 +# (the "License"); you may not use this file except in compliance with +# the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +TEST_ENV=${TEST:abc} +NORMAL_ENV=test \ No newline at end of file diff --git a/internal/util/utils.go b/internal/util/utils.go index a182ed8c..f27dc574 100644 --- a/internal/util/utils.go +++ b/internal/util/utils.go @@ -25,12 +25,15 @@ import ( "os" "os/exec" "path/filepath" + "regexp" "strings" "text/template" "github.com/apache/skywalking-infra-e2e/internal/logger" ) +var EnvRegularRegex = regexp.MustCompile(`\${(?P[_A-Z0-9]+):(?P.*)}`) + // PathExist checks if a file/directory is exist. func PathExist(_path string) bool { _, err := os.Stat(_path) @@ -117,10 +120,23 @@ func ExportEnvVars(envFile string) { if len(kv) != 2 { continue } - key, val := kv[0], kv[1] + key, val := kv[0], envOverwrite(kv[1]) // should only export env vars that are not already exist in parent process (Go process) if err := os.Setenv(key, val); err != nil { logger.Log.Warnf("failed to export environment variable %v=%v, %v", key, val, err) } } } + +// envOverwrite replaces the environment variable with the value from the parent process +func envOverwrite(val string) string { + groups := EnvRegularRegex.FindStringSubmatch(val) + if len(groups) == 0 { + return val + } + + if v := os.Getenv(groups[1]); v != "" { + return v + } + return groups[2] +} diff --git a/internal/util/utils_test.go b/internal/util/utils_test.go new file mode 100644 index 00000000..27b7b7f7 --- /dev/null +++ b/internal/util/utils_test.go @@ -0,0 +1,49 @@ +// Licensed to Apache Software Foundation (ASF) under one or more contributor +// license agreements. See the NOTICE file distributed with +// this work for additional information regarding copyright +// ownership. Apache Software Foundation (ASF) licenses this file to you under +// the Apache License, Version 2.0 (the "License"); you may +// not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. +// + +package util + +import ( + "os" + "testing" +) + +const ( + testEnvFile = "./testdata/env" + testEnvKey = "TEST_ENV" + normalEnvKey = "NORMAL_ENV" +) + +func TestExportEnvVars(t *testing.T) { + // default value + ExportEnvVars(testEnvFile) + if os.Getenv(testEnvKey) != "abc" { + t.Errorf("expected %s, got %s", "abc", os.Getenv(testEnvKey)) + } + if os.Getenv(normalEnvKey) != "test" { + t.Errorf("expected %s, got %s", "test", os.Getenv(normalEnvKey)) + } + + // override value from outside + var settingValFromOutside = "def" + os.Setenv("TEST", settingValFromOutside) + ExportEnvVars(testEnvFile) + if os.Getenv(testEnvKey) != settingValFromOutside { + t.Errorf("expected %s, got %s", settingValFromOutside, os.Getenv(testEnvKey)) + } +} diff --git a/test/e2e/concurrency/fail-fast/internal/expected.yaml b/test/e2e/concurrency/fail-fast/internal/expected.yaml index 1d9d4bc8..60aa84e1 100644 --- a/test/e2e/concurrency/fail-fast/internal/expected.yaml +++ b/test/e2e/concurrency/fail-fast/internal/expected.yaml @@ -20,6 +20,6 @@ args: headers: Accept: application/json Host: 127.0.0.1:8080 - User-Agent: curl/7.81.0 -origin: 172.18.0.1 + User-Agent: {{ notEmpty (index .headers "User-Agent") }} +origin: {{ notEmpty .origin }} url: http://127.0.0.1:8080/get?case=success \ No newline at end of file diff --git a/test/e2e/concurrency/non-fail-fast/internal/expected.yaml b/test/e2e/concurrency/non-fail-fast/internal/expected.yaml index 1d9d4bc8..60aa84e1 100644 --- a/test/e2e/concurrency/non-fail-fast/internal/expected.yaml +++ b/test/e2e/concurrency/non-fail-fast/internal/expected.yaml @@ -20,6 +20,6 @@ args: headers: Accept: application/json Host: 127.0.0.1:8080 - User-Agent: curl/7.81.0 -origin: 172.18.0.1 + User-Agent: {{ notEmpty (index .headers "User-Agent") }} +origin: {{ notEmpty .origin }} url: http://127.0.0.1:8080/get?case=success \ No newline at end of file diff --git a/test/e2e/non-concurrency/fail-fast/internal/expected.yaml b/test/e2e/non-concurrency/fail-fast/internal/expected.yaml index 1d9d4bc8..60aa84e1 100644 --- a/test/e2e/non-concurrency/fail-fast/internal/expected.yaml +++ b/test/e2e/non-concurrency/fail-fast/internal/expected.yaml @@ -20,6 +20,6 @@ args: headers: Accept: application/json Host: 127.0.0.1:8080 - User-Agent: curl/7.81.0 -origin: 172.18.0.1 + User-Agent: {{ notEmpty (index .headers "User-Agent") }} +origin: {{ notEmpty .origin }} url: http://127.0.0.1:8080/get?case=success \ No newline at end of file diff --git a/test/e2e/non-concurrency/non-fail-fast/internal/expected.yaml b/test/e2e/non-concurrency/non-fail-fast/internal/expected.yaml index 1d9d4bc8..60aa84e1 100644 --- a/test/e2e/non-concurrency/non-fail-fast/internal/expected.yaml +++ b/test/e2e/non-concurrency/non-fail-fast/internal/expected.yaml @@ -20,6 +20,6 @@ args: headers: Accept: application/json Host: 127.0.0.1:8080 - User-Agent: curl/7.81.0 -origin: 172.18.0.1 + User-Agent: {{ notEmpty (index .headers "User-Agent") }} +origin: {{ notEmpty .origin }} url: http://127.0.0.1:8080/get?case=success \ No newline at end of file