github.com/mheon/docker@v0.11.2-0.20150922122814-44f47903a831/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 // Adding a newline + a line with pure whitespace. 33 // This is being done like this instead of the block above 34 // because it's common for editors to trim trailing whitespace 35 // from lines, which becomes annoying since that's the 36 // exact thing we need to test. 37 content += "\n \t " 38 tmpFile := tmpFileWithContent(content, t) 39 defer os.Remove(tmpFile) 40 41 lines, err := ParseEnvFile(tmpFile) 42 if err != nil { 43 t.Fatal(err) 44 } 45 46 expectedLines := []string{ 47 "foo=bar", 48 "baz=quux", 49 "_foobar=foobaz", 50 } 51 52 if !reflect.DeepEqual(lines, expectedLines) { 53 t.Fatal("lines not equal to expected_lines") 54 } 55 } 56 57 // Test ParseEnvFile for an empty file 58 func TestParseEnvFileEmptyFile(t *testing.T) { 59 tmpFile := tmpFileWithContent("", t) 60 defer os.Remove(tmpFile) 61 62 lines, err := ParseEnvFile(tmpFile) 63 if err != nil { 64 t.Fatal(err) 65 } 66 67 if len(lines) != 0 { 68 t.Fatal("lines not empty; expected empty") 69 } 70 } 71 72 // Test ParseEnvFile for a non existent file 73 func TestParseEnvFileNonExistentFile(t *testing.T) { 74 _, err := ParseEnvFile("foo_bar_baz") 75 if err == nil { 76 t.Fatal("ParseEnvFile succeeded; expected failure") 77 } 78 if _, ok := err.(*os.PathError); !ok { 79 t.Fatalf("Expected a PathError, got [%v]", err) 80 } 81 } 82 83 // Test ParseEnvFile for a badly formatted file 84 func TestParseEnvFileBadlyFormattedFile(t *testing.T) { 85 content := `foo=bar 86 f =quux 87 ` 88 89 tmpFile := tmpFileWithContent(content, t) 90 defer os.Remove(tmpFile) 91 92 _, err := ParseEnvFile(tmpFile) 93 if err == nil { 94 t.Fatalf("Expected a ErrBadEnvVariable, got nothing") 95 } 96 if _, ok := err.(ErrBadEnvVariable); !ok { 97 t.Fatalf("Expected a ErrBadEnvVariable, got [%v]", err) 98 } 99 expectedMessage := "poorly formatted environment: variable 'f ' is not a valid environment variable" 100 if err.Error() != expectedMessage { 101 t.Fatalf("Expected [%v], got [%v]", expectedMessage, err.Error()) 102 } 103 } 104 105 // Test ParseEnvFile for a file with a line exeeding bufio.MaxScanTokenSize 106 func TestParseEnvFileLineTooLongFile(t *testing.T) { 107 content := strings.Repeat("a", bufio.MaxScanTokenSize+42) 108 content = fmt.Sprint("foo=", content) 109 110 tmpFile := tmpFileWithContent(content, t) 111 defer os.Remove(tmpFile) 112 113 _, err := ParseEnvFile(tmpFile) 114 if err == nil { 115 t.Fatal("ParseEnvFile succeeded; expected failure") 116 } 117 } 118 119 // ParseEnvFile with a random file, pass through 120 func TestParseEnvFileRandomFile(t *testing.T) { 121 content := `first line 122 another invalid line` 123 tmpFile := tmpFileWithContent(content, t) 124 defer os.Remove(tmpFile) 125 126 _, err := ParseEnvFile(tmpFile) 127 128 if err == nil { 129 t.Fatalf("Expected a ErrBadEnvVariable, got nothing") 130 } 131 if _, ok := err.(ErrBadEnvVariable); !ok { 132 t.Fatalf("Expected a ErrBadEnvvariable, got [%v]", err) 133 } 134 expectedMessage := "poorly formatted environment: variable 'first line' is not a valid environment variable" 135 if err.Error() != expectedMessage { 136 t.Fatalf("Expected [%v], got [%v]", expectedMessage, err.Error()) 137 } 138 }