本文整理匯總了Golang中github.com/heketi/heketi/tests.Tempfile函數的典型用法代碼示例。如果您正苦於以下問題:Golang Tempfile函數的具體用法?Golang Tempfile怎麽用?Golang Tempfile使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了Tempfile函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: TestVolumeEntryExpandMaxBrickLimit
func TestVolumeEntryExpandMaxBrickLimit(t *testing.T) {
tmpfile := tests.Tempfile()
defer os.Remove(tmpfile)
// Create the app
app := NewTestApp(tmpfile)
defer app.Close()
// Create a large cluster
err := setupSampleDbWithTopology(app,
10, // clusters
4, // nodes_per_cluster
24, // devices_per_node,
600*GB, // disksize)
)
tests.Assert(t, err == nil)
// Create large volume
v := createSampleVolumeEntry(100)
err = v.Create(app.db, app.executor, app.allocator)
tests.Assert(t, err == nil)
// Add a bunch of bricks until the limit
fakebricks := make(sort.StringSlice, BRICK_MAX_NUM-len(v.Bricks))
v.Bricks = append(v.Bricks, fakebricks...)
// Try to expand the volume, but it will return that the max number
// of bricks has been reached
err = v.Expand(app.db, app.executor, app.allocator, 100)
tests.Assert(t, err == ErrMaxBricks, err)
}
示例2: TestNewBrickEntryNewInfoResponse
func TestNewBrickEntryNewInfoResponse(t *testing.T) {
tmpfile := tests.Tempfile()
defer os.Remove(tmpfile)
// Create the app
app := NewTestApp(tmpfile)
defer app.Close()
// Create a brick
b := NewBrickEntry(10, 20, 5, "abc", "def")
// Save element in database
err := app.db.Update(func(tx *bolt.Tx) error {
return b.Save(tx)
})
tests.Assert(t, err == nil)
var info *BrickInfo
err = app.db.View(func(tx *bolt.Tx) error {
brick, err := NewBrickEntryFromId(tx, b.Id())
if err != nil {
return err
}
info, err = brick.NewInfoResponse(tx)
return err
})
tests.Assert(t, err == nil)
tests.Assert(t, reflect.DeepEqual(*info, b.Info))
}
示例3: TestGlusterFSDBFileLoad
func TestGlusterFSDBFileLoad(t *testing.T) {
tmpfile := tests.Tempfile()
defer os.Remove(tmpfile)
db := NewGlusterFSDB(tmpfile)
db.nodes["one"] = &NodeEntry{
Info: requests.NodeInfoResp{
Name: "nodetest",
},
}
db.volumes["a"] = &VolumeEntry{
Info: requests.VolumeInfoResp{
Name: "volumetest",
},
}
err := db.Commit()
tests.Assert(t, err == nil)
newdb := NewGlusterFSDB(tmpfile)
tests.Assert(t, newdb != nil)
tests.Assert(t, newdb.nodes["one"].Info.Name == db.nodes["one"].Info.Name)
tests.Assert(t, newdb.volumes["a"].Info.Name == db.volumes["a"].Info.Name)
}
示例4: TestVolumeExpandIdNotFound
func TestVolumeExpandIdNotFound(t *testing.T) {
tmpfile := tests.Tempfile()
defer os.Remove(tmpfile)
// Create the app
app := NewTestApp(tmpfile)
defer app.Close()
router := mux.NewRouter()
app.SetRoutes(router)
// Setup the server
ts := httptest.NewServer(router)
defer ts.Close()
// JSON Request
request := []byte(`{
"expand_size" : 100
}`)
// Now that we have some data in the database, we can
// make a request for the clutser list
r, err := http.Post(ts.URL+"/volumes/12345/expand",
"application/json",
bytes.NewBuffer(request))
tests.Assert(t, err == nil)
tests.Assert(t, r.StatusCode == http.StatusNotFound, r.StatusCode)
body, err := ioutil.ReadAll(io.LimitReader(r.Body, r.ContentLength))
tests.Assert(t, err == nil)
r.Body.Close()
tests.Assert(t, strings.Contains(string(body), "Id not found"))
}
示例5: TestVolumeCreateSmallSize
func TestVolumeCreateSmallSize(t *testing.T) {
tmpfile := tests.Tempfile()
defer os.Remove(tmpfile)
// Create the app
app := NewTestApp(tmpfile)
defer app.Close()
router := mux.NewRouter()
app.SetRoutes(router)
// Setup the server
ts := httptest.NewServer(router)
defer ts.Close()
// VolumeCreate JSON Request
request := []byte(`{
"size" : 0
}`)
// Send request
r, err := http.Post(ts.URL+"/volumes", "application/json", bytes.NewBuffer(request))
tests.Assert(t, err == nil)
tests.Assert(t, r.StatusCode == http.StatusBadRequest)
body, err := ioutil.ReadAll(io.LimitReader(r.Body, r.ContentLength))
tests.Assert(t, err == nil)
r.Body.Close()
tests.Assert(t, strings.Contains(string(body), "Invalid volume size"))
}
示例6: TestNewBrickEntryFromId
func TestNewBrickEntryFromId(t *testing.T) {
tmpfile := tests.Tempfile()
defer os.Remove(tmpfile)
// Create the app
app := NewTestApp(tmpfile)
defer app.Close()
// Create a brick
b := NewBrickEntry(10, 20, 5, "abc", "def")
// Save element in database
err := app.db.Update(func(tx *bolt.Tx) error {
return b.Save(tx)
})
tests.Assert(t, err == nil)
var brick *BrickEntry
err = app.db.View(func(tx *bolt.Tx) error {
var err error
brick, err = NewBrickEntryFromId(tx, b.Info.Id)
return err
})
tests.Assert(t, err == nil)
tests.Assert(t, reflect.DeepEqual(brick, b))
}
示例7: TestVolumeEntryFromId
func TestVolumeEntryFromId(t *testing.T) {
tmpfile := tests.Tempfile()
defer os.Remove(tmpfile)
// Create the app
app := NewTestApp(tmpfile)
defer app.Close()
// Create a volume entry
v := createSampleVolumeEntry(1024)
// Save in database
err := app.db.Update(func(tx *bolt.Tx) error {
return v.Save(tx)
})
tests.Assert(t, err == nil)
// Load from database
var entry *VolumeEntry
err = app.db.View(func(tx *bolt.Tx) error {
var err error
entry, err = NewVolumeEntryFromId(tx, v.Info.Id)
return err
})
tests.Assert(t, err == nil)
tests.Assert(t, reflect.DeepEqual(entry, v))
}
示例8: TestVolumeListEmpty
func TestVolumeListEmpty(t *testing.T) {
tmpfile := tests.Tempfile()
defer os.Remove(tmpfile)
// Create the app
app := NewTestApp(tmpfile)
defer app.Close()
router := mux.NewRouter()
app.SetRoutes(router)
// Setup the server
ts := httptest.NewServer(router)
defer ts.Close()
// Get volumes, there should be none
r, err := http.Get(ts.URL + "/volumes")
tests.Assert(t, r.StatusCode == http.StatusOK)
tests.Assert(t, err == nil)
tests.Assert(t, r.Header.Get("Content-Type") == "application/json; charset=UTF-8")
// Read response
var msg VolumeListResponse
err = utils.GetJsonFromResponse(r, &msg)
tests.Assert(t, err == nil)
tests.Assert(t, len(msg.Volumes) == 0)
}
示例9: TestClientCluster
func TestClientCluster(t *testing.T) {
db := tests.Tempfile()
defer os.Remove(db)
// Create the app
app := glusterfs.NewTestApp(db)
defer app.Close()
// Setup the server
ts := setupHeketiServer(app)
defer ts.Close()
// Create cluster with unknown user
c := NewClient(ts.URL, "asdf", "")
tests.Assert(t, c != nil)
cluster, err := c.ClusterCreate()
tests.Assert(t, err != nil)
tests.Assert(t, cluster == nil)
// Create cluster with bad password
c = NewClient(ts.URL, "admin", "badpassword")
tests.Assert(t, c != nil)
cluster, err = c.ClusterCreate()
tests.Assert(t, err != nil)
tests.Assert(t, cluster == nil)
// Create cluster correctly
c = NewClient(ts.URL, "admin", TEST_ADMIN_KEY)
tests.Assert(t, c != nil)
cluster, err = c.ClusterCreate()
tests.Assert(t, err == nil)
tests.Assert(t, cluster.Id != "")
tests.Assert(t, len(cluster.Nodes) == 0)
tests.Assert(t, len(cluster.Volumes) == 0)
// Request bad id
info, err := c.ClusterInfo("bad")
tests.Assert(t, err != nil)
tests.Assert(t, info == nil)
// Get information about the client
info, err = c.ClusterInfo(cluster.Id)
tests.Assert(t, err == nil)
tests.Assert(t, reflect.DeepEqual(info, cluster))
// Get a list of clusters
list, err := c.ClusterList()
tests.Assert(t, err == nil)
tests.Assert(t, len(list.Clusters) == 1)
tests.Assert(t, list.Clusters[0] == info.Id)
// Delete non-existent cluster
err = c.ClusterDelete("badid")
tests.Assert(t, err != nil)
// Delete current cluster
err = c.ClusterDelete(info.Id)
tests.Assert(t, err == nil)
}
示例10: TestVolumeDelete
func TestVolumeDelete(t *testing.T) {
tmpfile := tests.Tempfile()
defer os.Remove(tmpfile)
// Create the app
app := NewTestApp(tmpfile)
defer app.Close()
router := mux.NewRouter()
app.SetRoutes(router)
// Setup the server
ts := httptest.NewServer(router)
defer ts.Close()
// Setup database
err := setupSampleDbWithTopology(app,
1, // clusters
10, // nodes_per_cluster
10, // devices_per_node,
5*TB, // disksize)
)
tests.Assert(t, err == nil)
// Create a volume
v := createSampleVolumeEntry(100)
tests.Assert(t, v != nil)
err = v.Create(app.db, app.executor, app.allocator)
tests.Assert(t, err == nil)
// Delete the volume
req, err := http.NewRequest("DELETE", ts.URL+"/volumes/"+v.Info.Id, nil)
tests.Assert(t, err == nil)
r, err := http.DefaultClient.Do(req)
tests.Assert(t, err == nil)
tests.Assert(t, r.StatusCode == http.StatusAccepted)
location, err := r.Location()
tests.Assert(t, err == nil)
// Query queue until finished
for {
r, err = http.Get(location.String())
tests.Assert(t, err == nil)
if r.Header.Get("X-Pending") == "true" {
tests.Assert(t, r.StatusCode == http.StatusOK)
time.Sleep(time.Millisecond * 10)
continue
} else {
tests.Assert(t, r.StatusCode == http.StatusNoContent)
tests.Assert(t, err == nil)
break
}
}
// Check it is not there
r, err = http.Get(ts.URL + "/volumes/" + v.Info.Id)
tests.Assert(t, r.StatusCode == http.StatusNotFound)
tests.Assert(t, err == nil)
}
示例11: TestVolumeList
func TestVolumeList(t *testing.T) {
tmpfile := tests.Tempfile()
defer os.Remove(tmpfile)
// Create the app
app := NewTestApp(tmpfile)
defer app.Close()
router := mux.NewRouter()
app.SetRoutes(router)
// Setup the server
ts := httptest.NewServer(router)
defer ts.Close()
// Create some volumes
numvolumes := 1000
err := app.db.Update(func(tx *bolt.Tx) error {
for i := 0; i < numvolumes; i++ {
v := createSampleVolumeEntry(100)
err := v.Save(tx)
if err != nil {
return err
}
}
return nil
})
tests.Assert(t, err == nil)
// Get volumes, there should be none
r, err := http.Get(ts.URL + "/volumes")
tests.Assert(t, r.StatusCode == http.StatusOK)
tests.Assert(t, err == nil)
tests.Assert(t, r.Header.Get("Content-Type") == "application/json; charset=UTF-8")
// Read response
var msg VolumeListResponse
err = utils.GetJsonFromResponse(r, &msg)
tests.Assert(t, err == nil)
tests.Assert(t, len(msg.Volumes) == numvolumes)
// Check that all the volumes are in the database
err = app.db.View(func(tx *bolt.Tx) error {
for _, id := range msg.Volumes {
_, err := NewVolumeEntryFromId(tx, id)
if err != nil {
return err
}
}
return nil
})
tests.Assert(t, err == nil)
}
示例12: TestVolumeCreateBadDispersionValues
func TestVolumeCreateBadDispersionValues(t *testing.T) {
tmpfile := tests.Tempfile()
defer os.Remove(tmpfile)
// Create the app
app := NewTestApp(tmpfile)
defer app.Close()
router := mux.NewRouter()
app.SetRoutes(router)
// Setup the server
ts := httptest.NewServer(router)
defer ts.Close()
// VolumeCreate JSON Request
request := []byte(`{
"size" : 100,
"durability": {
"type": "disperse",
"disperse": {
"data" : 8,
"redundancy" : 1
}
}
}`)
// Send request
r, err := http.Post(ts.URL+"/volumes", "application/json", bytes.NewBuffer(request))
tests.Assert(t, err == nil)
tests.Assert(t, r.StatusCode == http.StatusBadRequest)
body, err := ioutil.ReadAll(io.LimitReader(r.Body, r.ContentLength))
tests.Assert(t, err == nil)
r.Body.Close()
tests.Assert(t, strings.Contains(string(body), "Invalid dispersion combination"))
// VolumeCreate JSON Request
request = []byte(`{
"size" : 100,
"durability": {
"type": "disperse",
"disperse": {
"data" : 4,
"redundancy" : 3
}
}
}`)
// Send request
r, err = http.Post(ts.URL+"/volumes", "application/json", bytes.NewBuffer(request))
tests.Assert(t, err == nil)
tests.Assert(t, r.StatusCode == http.StatusBadRequest)
body, err = ioutil.ReadAll(io.LimitReader(r.Body, r.ContentLength))
tests.Assert(t, err == nil)
r.Body.Close()
tests.Assert(t, strings.Contains(string(body), "Invalid dispersion combination"))
}
示例13: TestDeviceAddBadRequests
func TestDeviceAddBadRequests(t *testing.T) {
tmpfile := tests.Tempfile()
defer os.Remove(tmpfile)
// Patch dbfilename so that it is restored at the end of the tests
defer tests.Patch(&dbfilename, tmpfile).Restore()
// Create the app
app := NewApp()
defer app.Close()
router := mux.NewRouter()
app.SetRoutes(router)
// Setup the server
ts := httptest.NewServer(router)
defer ts.Close()
// ClusterCreate JSON Request
request := []byte(`{
bad json
}`)
// Post bad JSON
r, err := http.Post(ts.URL+"/devices", "application/json", bytes.NewBuffer(request))
tests.Assert(t, err == nil)
tests.Assert(t, r.StatusCode == 422)
// Make a request with no devices
request = []byte(`{
"node" : "123",
"devices" : []
}`)
// Post bad JSON
r, err = http.Post(ts.URL+"/devices", "application/json", bytes.NewBuffer(request))
tests.Assert(t, err == nil)
tests.Assert(t, r.StatusCode == http.StatusBadRequest)
// Make a request with unknown node
request = []byte(`{
"node" : "123",
"devices" : [
{
"name" : "/dev/fake",
"weight" : 20
}
]
}`)
// Post bad JSON
r, err = http.Post(ts.URL+"/devices", "application/json", bytes.NewBuffer(request))
tests.Assert(t, err == nil)
tests.Assert(t, r.StatusCode == http.StatusNotFound)
}
示例14: TestNewNodeEntryNewInfoResponse
func TestNewNodeEntryNewInfoResponse(t *testing.T) {
tmpfile := tests.Tempfile()
defer os.Remove(tmpfile)
// Patch dbfilename so that it is restored at the end of the tests
defer tests.Patch(&dbfilename, tmpfile).Restore()
// Create the app
app := NewApp()
defer app.Close()
// Create a node
req := &NodeAddRequest{
ClusterId: "123",
Hostnames: HostAddresses{
Manage: []string{"manage"},
Storage: []string{"storage"},
},
Zone: 99,
}
n := NewNodeEntryFromRequest(req)
// Save element in database
err := app.db.Update(func(tx *bolt.Tx) error {
return n.Save(tx)
})
tests.Assert(t, err == nil)
var info *NodeInfoResponse
err = app.db.View(func(tx *bolt.Tx) error {
node, err := NewNodeEntryFromId(tx, n.Info.Id)
if err != nil {
return err
}
info, err = node.NewInfoReponse(tx)
if err != nil {
return err
}
return nil
})
tests.Assert(t, err == nil)
tests.Assert(t, info.ClusterId == n.Info.ClusterId)
tests.Assert(t, info.Id == n.Info.Id)
tests.Assert(t, info.Zone == n.Info.Zone)
tests.Assert(t, len(info.Hostnames.Manage) == 1)
tests.Assert(t, len(info.Hostnames.Storage) == 1)
tests.Assert(t, reflect.DeepEqual(info.Hostnames.Manage, n.Info.Hostnames.Manage))
tests.Assert(t, reflect.DeepEqual(info.Hostnames.Storage, n.Info.Hostnames.Storage))
}
示例15: TestVolumeInfo
func TestVolumeInfo(t *testing.T) {
tmpfile := tests.Tempfile()
defer os.Remove(tmpfile)
// Create the app
app := NewTestApp(tmpfile)
defer app.Close()
router := mux.NewRouter()
app.SetRoutes(router)
// Setup the server
ts := httptest.NewServer(router)
defer ts.Close()
// Setup database
err := setupSampleDbWithTopology(app,
1, // clusters
10, // nodes_per_cluster
10, // devices_per_node,
5*TB, // disksize)
)
tests.Assert(t, err == nil)
// Create a volume
req := &VolumeCreateRequest{}
req.Size = 100
req.Durability.Type = DURABILITY_STRING_EC
v := NewVolumeEntryFromRequest(req)
tests.Assert(t, v != nil)
err = v.Create(app.db, app.executor, app.allocator)
tests.Assert(t, err == nil)
// Now that we have some data in the database, we can
// make a request for the clutser list
r, err := http.Get(ts.URL + "/volumes/" + v.Info.Id)
tests.Assert(t, r.StatusCode == http.StatusOK)
tests.Assert(t, err == nil)
tests.Assert(t, r.Header.Get("Content-Type") == "application/json; charset=UTF-8")
// Read response
var msg VolumeInfoResponse
err = utils.GetJsonFromResponse(r, &msg)
tests.Assert(t, err == nil)
tests.Assert(t, msg.Id == v.Info.Id)
tests.Assert(t, msg.Cluster == v.Info.Cluster)
tests.Assert(t, msg.Name == v.Info.Name)
tests.Assert(t, msg.Size == v.Info.Size)
tests.Assert(t, reflect.DeepEqual(msg.Durability, v.Info.Durability))
tests.Assert(t, reflect.DeepEqual(msg.Snapshot, v.Info.Snapshot))
for _, brick := range msg.Bricks {
tests.Assert(t, utils.SortedStringHas(v.Bricks, brick.Id))
}
}