github.com/torfuzx/docker@v1.8.1/opts/envfile_test.go (about) 1 package opts 2 3 import ( 4 "bufio" 5 "fmt" 6 "io/ioutil" 7 "os" 8 "reflect" 9 "strings" 10 "testing" 11 ) 12 13 func tmpFileWithContent(content string, t *testing.T) string { 14 tmpFile, err := ioutil.TempFile("", "envfile-test") 15 if err != nil { 16 t.Fatal(err) 17 } 18 defer tmpFile.Close() 19 20 tmpFile.WriteString(content) 21 return tmpFile.Name() 22 } 23 24 // Test ParseEnvFile for a file with a few well formatted lines 25 func TestParseEnvFileGoodFile(t *testing.T) { 26 content := `foo=bar 27 baz=quux 28 # comment 29 30 _foobar=foobaz 31 ` 32 33 tmpFile := tmpFileWithContent(content, t) 34 defer os.Remove(tmpFile) 35 36 lines, err := ParseEnvFile(tmpFile) 37 if err != nil { 38 t.Fatal(err) 39 } 40 41 expectedLines := []string{ 42 "foo=bar", 43 "baz=quux", 44 "_foobar=foobaz", 45 } 46 47 if !reflect.DeepEqual(lines, expectedLines) { 48 t.Fatal("lines not equal to expected_lines") 49 } 50 } 51 52 // Test ParseEnvFile for an empty file 53 func TestParseEnvFileEmptyFile(t *testing.T) { 54 tmpFile := tmpFileWithContent("", t) 55 defer os.Remove(tmpFile) 56 57 lines, err := ParseEnvFile(tmpFile) 58 if err != nil { 59 t.Fatal(err) 60 } 61 62 if len(lines) != 0 { 63 t.Fatal("lines not empty; expected empty") 64 } 65 } 66 67 // Test ParseEnvFile for a non existent file 68 func TestParseEnvFileNonExistentFile(t *testing.T) { 69 _, err := ParseEnvFile("foo_bar_baz") 70 if err == nil { 71 t.Fatal("ParseEnvFile succeeded; expected failure") 72 } 73 if _, ok := err.(*os.PathError); !ok { 74 t.Fatalf("Expected a PathError, got [%v]", err) 75 } 76 } 77 78 // Test ParseEnvFile for a badly formatted file 79 func TestParseEnvFileBadlyFormattedFile(t *testing.T) { 80 content := `foo=bar 81 f =quux 82 ` 83 84 tmpFile := tmpFileWithContent(content, t) 85 defer os.Remove(tmpFile) 86 87 _, err := ParseEnvFile(tmpFile) 88 if err == nil { 89 t.Fatalf("Expected a ErrBadEnvVariable, got nothing") 90 } 91 if _, ok := err.(ErrBadEnvVariable); !ok { 92 t.Fatalf("Expected a ErrBadEnvVariable, got [%v]", err) 93 } 94 expectedMessage := "poorly formatted environment: variable 'f ' is not a valid environment variable" 95 if err.Error() != expectedMessage { 96 t.Fatalf("Expected [%v], got [%v]", expectedMessage, err.Error()) 97 } 98 } 99 100 // Test ParseEnvFile for a file with a line exeeding bufio.MaxScanTokenSize 101 func TestParseEnvFileLineTooLongFile(t *testing.T) { 102 content := strings.Repeat("a", bufio.MaxScanTokenSize+42) 103 content = fmt.Sprint("foo=", content) 104 105 tmpFile := tmpFileWithContent(content, t) 106 defer os.Remove(tmpFile) 107 108 _, err := ParseEnvFile(tmpFile) 109 if err == nil { 110 t.Fatal("ParseEnvFile succeeded; expected failure") 111 } 112 } 113 114 // ParseEnvFile with a random file, pass through 115 func TestParseEnvFileRandomFile(t *testing.T) { 116 content := `first line 117 another invalid line` 118 tmpFile := tmpFileWithContent(content, t) 119 defer os.Remove(tmpFile) 120 121 _, err := ParseEnvFile(tmpFile) 122 123 if err == nil { 124 t.Fatalf("Expected a ErrBadEnvVariable, got nothing") 125 } 126 if _, ok := err.(ErrBadEnvVariable); !ok { 127 t.Fatalf("Expected a ErrBadEnvvariable, got [%v]", err) 128 } 129 expectedMessage := "poorly formatted environment: variable 'first line' is not a valid environment variable" 130 if err.Error() != expectedMessage { 131 t.Fatalf("Expected [%v], got [%v]", expectedMessage, err.Error()) 132 } 133 }