Skip to content

Commit

Permalink
Support importing external variables in the `setup.init-system-enviro…
Browse files Browse the repository at this point in the history
…nment` file (#129)
  • Loading branch information
mrproliu authored Dec 23, 2024
1 parent cf589b4 commit 5217e1c
Show file tree
Hide file tree
Showing 8 changed files with 92 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
17 changes: 17 additions & 0 deletions internal/util/testdata/env
Original file line number Diff line number Diff line change
@@ -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
18 changes: 17 additions & 1 deletion internal/util/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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<ENV>[_A-Z0-9]+):(?P<DEF>.*)}`)

// PathExist checks if a file/directory is exist.
func PathExist(_path string) bool {
_, err := os.Stat(_path)
Expand Down Expand Up @@ -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]
}
49 changes: 49 additions & 0 deletions internal/util/utils_test.go
Original file line number Diff line number Diff line change
@@ -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))
}
}
4 changes: 2 additions & 2 deletions test/e2e/concurrency/fail-fast/internal/expected.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 2 additions & 2 deletions test/e2e/concurrency/non-fail-fast/internal/expected.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 2 additions & 2 deletions test/e2e/non-concurrency/fail-fast/internal/expected.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 2 additions & 2 deletions test/e2e/non-concurrency/non-fail-fast/internal/expected.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 5217e1c

Please sign in to comment.