forked from mendersoftware/mender
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblock_device_test.go
137 lines (110 loc) · 3.36 KB
/
block_device_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
// Copyright 2017 Northern.tech AS
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"io/ioutil"
"os"
"path"
"syscall"
"testing"
"github.com/pkg/errors"
"github.com/stretchr/testify/assert"
)
func TestBlockDeviceFail(t *testing.T) {
bd := BlockDevice{Path: "/dev/somefile"}
// closing unopened device should not fail
err := bd.Close()
assert.NoError(t, err)
w, err := bd.Write([]byte("foo"))
assert.Equal(t, 0, w)
assert.Error(t, err)
err = bd.Close()
assert.NoError(t, err)
}
func makeBlockDeviceSize(t *testing.T, sz uint64, err error, name string) BlockDeviceGetSizeFunc {
return func(file *os.File) (uint64, error) {
t.Logf("block device size called: %v", file)
if assert.NotNil(t, file) {
assert.Equal(t, name, file.Name())
}
return sz, err
}
}
func createFile(path string) error {
f, err := os.Create(path)
if err != nil {
return err
}
defer f.Close()
return nil
}
func TestBlockDeviceWrite(t *testing.T) {
td, err := ioutil.TempDir("", "mender-block-device-")
assert.NoError(t, err)
defer os.RemoveAll(td)
// prepare fake block device file
bdpath := path.Join(td, "foo")
// temporarily override helper for getting block device size
old := BlockDeviceGetSizeOf
// pretend the device is only 10 bytes in size
BlockDeviceGetSizeOf = makeBlockDeviceSize(t, 10, nil, bdpath)
// test simple write
err = createFile(bdpath)
assert.NoError(t, err)
bd := BlockDevice{Path: bdpath}
n, err := bd.Write([]byte("foobar"))
assert.Equal(t, len([]byte("foobar")), n)
assert.NoError(t, err)
bd.Close()
data, err := ioutil.ReadFile(bdpath)
assert.NoError(t, err)
assert.Equal(t, []byte("foobar"), data)
os.Remove(bdpath)
// too large write
err = createFile(bdpath)
assert.NoError(t, err)
bd = BlockDevice{Path: bdpath}
n, err = bd.Write([]byte("foobarfoobar"))
assert.Equal(t, 10, n)
assert.EqualError(t, err, syscall.ENOSPC.Error())
bd.Close()
data, err = ioutil.ReadFile(bdpath)
assert.NoError(t, err)
// written only 10 bytes
assert.Equal(t, []byte("foobarfoob"), data)
os.Remove(bdpath)
BlockDeviceGetSizeOf = old
}
func TestBlockDeviceSize(t *testing.T) {
td, err := ioutil.TempDir("", "mender-block-device-")
assert.NoError(t, err)
defer os.RemoveAll(td)
// prepare fake block device file
bdpath := path.Join(td, "foo")
err = createFile(bdpath)
assert.NoError(t, err)
// temporarily override helper for getting block device size
old := BlockDeviceGetSizeOf
// pretend the device is only 10 bytes in size
BlockDeviceGetSizeOf = makeBlockDeviceSize(t, 10, nil, bdpath)
bd := BlockDevice{Path: bdpath}
sz, err := bd.Size()
assert.Equal(t, uint64(10), sz)
assert.NoError(t, err)
BlockDeviceGetSizeOf = makeBlockDeviceSize(t, 10, errors.New("failed"), bdpath)
bd = BlockDevice{Path: bdpath}
sz, err = bd.Size()
assert.EqualError(t, err, "failed")
BlockDeviceGetSizeOf = old
}