Proxy/Tests: Added test cases to check env variables

This commit is contained in:
MagnumOpus21 2018-07-09 12:27:34 -04:00
parent 6cecf2961d
commit 94e8ff55cf
2 changed files with 73 additions and 21 deletions

View File

@ -5,6 +5,8 @@ import (
"os"
"os/exec"
"path/filepath"
"reflect"
"sort"
"testing"
"time"
@ -274,31 +276,38 @@ func TestEnvironproxy(t *testing.T) {
m.State = state
defer m.Kill()
// Add the proxy
// Add Proxy for the test
td, closer := testTempDir(t)
defer closer()
path := filepath.Join(td, "env-variables")
d := &Daemon{
Command: helperProcess("parent", path),
Logger: testLogger,
}
var currentEnviron []string
currentEnviron = os.Environ()
// Environmental variable of the helper
if err := d.Start(); err != nil {
t.Error("Daemon failed to start")
defer d.Stop()
envProcess := d.Command.Env
var envData []byte
for _, data := range envProcess {
envData = append(envData, []byte(data)...)
envData = append(envData, []byte("\n")...)
}
t.Logf("Env Data:%s", envProcess)
//Get the parent environmental variables in a file
testStateProxy(t, state, "environTest", helperProcess("environ", path))
require.Equal(currentEnviron, envProcess)
//Run the manager
go m.Run()
//Get the environmental variables from the OS
//envCheck := os.Environ()
var fileContent Bytes
var err error
var data Bytes
envData := os.Environ()
sort.Strings(envData)
for _, envVariable := range envData {
data = append(data, envVariable...)
data = append(data, "\n"...)
}
// t.Log(data)
retry.Run(t, func(r *retry.R) {
if fileContent, err = ioutil.ReadFile(path); err != nil {
r.Fatalf("No file ya dummy")
}
})
t.Logf("Len (data) : %d , Len (fileContent) : %d", len(data), len(fileContent))
t.Logf("Type (data) : %s Type (fileContent) : %s", reflect.TypeOf(data), reflect.TypeOf(fileContent))
// t.Logf("File content: \n %s", fileContent)
// t.Logf("Data content: \n %s", data)
require.Equal(fileContent, data)
}
// Test the Snapshot/Restore works.

View File

@ -7,7 +7,9 @@ import (
"os"
"os/exec"
"os/signal"
"sort"
"strconv"
"strings"
"syscall"
"testing"
"time"
@ -17,6 +19,22 @@ import (
// *log.Logger instance.
var testLogger = log.New(os.Stderr, "logger: ", log.LstdFlags)
//SOrting interface
type Bytes []byte
//Length method for our Byte interface
func (b Bytes) Len() int {
return len(b)
}
func (b Bytes) Swap(i, j int) {
b[i], b[j] = b[j], b[i]
}
func (b Bytes) Less(i, j int) bool {
return b[i] < b[j]
}
// testTempDir returns a temporary directory and a cleanup function.
func testTempDir(t *testing.T) (string, func()) {
t.Helper()
@ -124,7 +142,6 @@ func TestHelperProcess(t *testing.T) {
default:
}
}
case "stop-kill":
// Setup listeners so it is ignored
ch := make(chan os.Signal, 1)
@ -139,6 +156,32 @@ func TestHelperProcess(t *testing.T) {
}
time.Sleep(25 * time.Millisecond)
}
// Check if the external process can access the enivironmental variables
case "environ":
stop := make(chan os.Signal, 1)
signal.Notify(stop, os.Interrupt)
defer signal.Stop(stop)
//Write the environmental variables into the file
path := args[0]
var data Bytes
// sort.Sort(data)
envData := os.Environ()
sort.Strings(envData)
for _, envVariable := range envData {
if strings.Contains(envVariable, "CONNECT_PROXY") {
continue
}
data = append(data, envVariable...)
data = append(data, "\n"...)
}
if err := ioutil.WriteFile(path, data, 0644); err != nil {
t.Fatalf("[Error] File write failed : %s", err)
}
// Clean up after we receive the signal to exit
defer os.Remove(path)
<-stop
case "output":
fmt.Fprintf(os.Stdout, "hello stdout\n")