ref: Begin consolidating environment variables into a single package.
This commit starts addressing veyron/release-issues#1367.
Specifically, the plan is to:
(1) Have all the environment variables used by disparate parts of
the reference implementation be in a single "envvar" package at the
top-level. (Environment variables private to a particular component,
for example the v23test framework or the device manager will not
be moved to this top-level package).
(2) Name all environment variables consistently: i.e., with a V23_
prefix.
This particular commit takes care of:
NAMESPACE_ROOT* --> V23_NAMESPACE*
VEYRON_CREDENTIALS --> V23_CREDENTIALS
VANADIUM_I18N_CATALOGUE --> V23_I18N_CATALOGUE
Support for the old environment variables has been left where it was
deemed to be necessary for a smooth rollout (where one process might
start another process and one of them will be without this change).
This will be removed once veyron/release-issues#1367 is completely
resolved.
MultiPart: 1/6
Change-Id: I72d6fc4114c83d3ee33cb8a8b19ec4c05ac8cb01
diff --git a/envvar/envvar_test.go b/envvar/envvar_test.go
new file mode 100644
index 0000000..e0fa19c
--- /dev/null
+++ b/envvar/envvar_test.go
@@ -0,0 +1,57 @@
+// Copyright 2015 The Vanadium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package envvar
+
+import (
+ "os"
+ "reflect"
+ "testing"
+)
+
+// Set an environment variable and return a function to undo it.
+// Typical usage:
+// defer setenv(t, "VAR", "VALUE")()
+func setenv(t *testing.T, name, value string) func() {
+ oldval := os.Getenv(name)
+ if err := os.Setenv(name, value); err != nil {
+ t.Fatalf("Failed to set %q to %q: %v", name, value, err)
+ return func() {}
+ }
+ return func() {
+ if err := os.Setenv(name, oldval); err != nil {
+ t.Fatalf("Failed to restore %q to %q: %v", name, oldval, err)
+ }
+ }
+}
+
+func TestNamespaceRoots(t *testing.T) {
+ defer setenv(t, NamespacePrefix, "NS1")()
+ defer setenv(t, NamespacePrefix+"_BLAH", "NS_BLAH")()
+
+ wantm := map[string]string{
+ "V23_NAMESPACE": "NS1",
+ "V23_NAMESPACE_BLAH": "NS_BLAH",
+ }
+ wantl := []string{"NS1", "NS_BLAH"}
+
+ gotm, gotl := NamespaceRoots()
+ if !reflect.DeepEqual(wantm, gotm) {
+ t.Errorf("Got %v want %v", gotm, wantm)
+ }
+ if !reflect.DeepEqual(wantl, gotl) {
+ t.Errorf("Got %v want %v", gotl, wantl)
+ }
+}
+
+func TestClearCredentials(t *testing.T) {
+ defer setenv(t, Credentials, "FOO")()
+ if got, want := os.Getenv(Credentials), "FOO"; got != want {
+ t.Errorf("Got %q, want %q", got, want)
+ }
+ ClearCredentials()
+ if got := os.Getenv(Credentials); got != "" {
+ t.Errorf("Got %q, wanted empty string", got)
+ }
+}