Skip to content

Commit

Permalink
Use t.Setenv() in vpncscript tests
Browse files Browse the repository at this point in the history
Signed-off-by: hwipl <33433250+hwipl@users.noreply.github.com>
  • Loading branch information
hwipl committed May 2, 2024
1 parent a221f01 commit 8dfdab1
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 24 deletions.
16 changes: 4 additions & 12 deletions internal/vpncscript/cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,22 +27,16 @@ func TestRun(t *testing.T) {
// prepare environment with not existing sockfile
os.Clearenv()
sockfile := filepath.Join(t.TempDir(), "sockfile")
if err := os.Setenv("oc_daemon_socket_file", sockfile); err != nil {
t.Fatal(err)
}
if err := os.Setenv("oc_daemon_verbose", "true"); err != nil {
t.Fatal(err)
}
t.Setenv("oc_daemon_socket_file", sockfile)
t.Setenv("oc_daemon_verbose", "true")

// test with errors
for _, v := range []string{
"connect",
"disconnect",
"invalid",
} {
if err := os.Setenv("reason", v); err != nil {
t.Fatal(err)
}
t.Setenv("reason", v)
if err := run([]string{"test"}); err == nil {
t.Errorf("%s: should return error", v)
}
Expand All @@ -54,9 +48,7 @@ func TestRun(t *testing.T) {
"attempt-reconnect",
"reconnect",
} {
if err := os.Setenv("reason", v); err != nil {
t.Fatal(err)
}
t.Setenv("reason", v)
if err := run([]string{"test"}); err != nil {
t.Errorf("%s: should not return error, got: %v", v, err)
}
Expand Down
16 changes: 4 additions & 12 deletions internal/vpncscript/env_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,15 @@ func TestParseEnvironmentSplit(t *testing.T) {
}

// test with invalid number in variable
if err := os.Setenv("CISCO_SPLIT_EXC", "invalid"); err != nil {
t.Fatal(err)
}
t.Setenv("CISCO_SPLIT_EXC", "invalid")
want := []string{}
got := parseEnvironmentSplit("CISCO_SPLIT_EXC")
if !reflect.DeepEqual(got, want) {
t.Errorf("got %v, want %v", got, want)
}

// test with not matching numbers
if err := os.Setenv("CISCO_SPLIT_EXC", "1"); err != nil {
t.Fatal(err)
}
t.Setenv("CISCO_SPLIT_EXC", "1")
want = []string{}
got = parseEnvironmentSplit("CISCO_SPLIT_EXC")
if !reflect.DeepEqual(got, want) {
Expand All @@ -53,9 +49,7 @@ func TestParseEnvironmentSplit(t *testing.T) {
"CISCO_SPLIT_EXC_2_ADDR": "10.0.0.0",
"CISCO_SPLIT_EXC_2_MASKLEN": "8",
} {
if err := os.Setenv(k, v); err != nil {
t.Fatal(err)
}
t.Setenv(k, v)
}

want = []string{
Expand Down Expand Up @@ -372,9 +366,7 @@ X-CSTP-Disable-Always-On-VPN=true`,
"oc_daemon_socket_file": "/run/oc-daemon/test.socket",
"oc_daemon_verbose": "true",
} {
if err := os.Setenv(k, v); err != nil {
t.Fatal(err)
}
t.Setenv(k, v)
}

// print env
Expand Down

0 comments on commit 8dfdab1

Please sign in to comment.