|
| 1 | +package conf |
| 2 | + |
| 3 | +import ( |
| 4 | + "io/ioutil" |
| 5 | + "os" |
| 6 | + "os/user" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/deis/builder/pkg/sys" |
| 10 | +) |
| 11 | + |
| 12 | +func TestGetStorageParams(t *testing.T) { |
| 13 | + usr, err := user.Current() |
| 14 | + if err != nil { |
| 15 | + t.Logf("could not retrieve current user: %v", err) |
| 16 | + t.SkipNow() |
| 17 | + } |
| 18 | + if usr.Uid != "0" { |
| 19 | + t.Logf("current user does not have UID of zero (got %s) "+ |
| 20 | + "so cannot create storage cred location, skipping", usr.Uid) |
| 21 | + t.SkipNow() |
| 22 | + } |
| 23 | + |
| 24 | + if err := os.MkdirAll(storageCredLocation, os.ModeDir); err != nil { |
| 25 | + t.Fatalf("could not create storage cred location: %v", err) |
| 26 | + } |
| 27 | + |
| 28 | + // start by writing out a file to storageCredLocation |
| 29 | + data := []byte("hello world\n") |
| 30 | + if err := ioutil.WriteFile(storageCredLocation+"foo", data, 0644); err != nil { |
| 31 | + t.Fatalf("could not write file to storage cred location: %v", err) |
| 32 | + } |
| 33 | + |
| 34 | + params, err := GetStorageParams(sys.NewFakeEnv()) |
| 35 | + if err != nil { |
| 36 | + t.Errorf("recevied error while retrieving storage params: %v", err) |
| 37 | + } |
| 38 | + |
| 39 | + val, ok := params["foo"] |
| 40 | + if !ok { |
| 41 | + t.Error("key foo does not exist in storage params") |
| 42 | + } |
| 43 | + if val != string(data) { |
| 44 | + t.Errorf("expected: %s got: %s", string(data), val) |
| 45 | + } |
| 46 | + |
| 47 | + // create a directory inside storage cred location, expecting it to pass |
| 48 | + if err := os.Mkdir(storageCredLocation+"bar", os.ModeDir); err != nil { |
| 49 | + t.Fatalf("could not create dir %s: %v", storageCredLocation+"bar", err) |
| 50 | + } |
| 51 | + |
| 52 | + _, err = GetStorageParams(sys.NewFakeEnv()) |
| 53 | + if err != nil { |
| 54 | + t.Errorf("recevied error while retrieving storage params: %v", err) |
| 55 | + } |
| 56 | +} |
0 commit comments