-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathconvert_test.go
More file actions
77 lines (65 loc) · 2.76 KB
/
convert_test.go
File metadata and controls
77 lines (65 loc) · 2.76 KB
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
package partitionresizer
import (
"strings"
"testing"
"github.com/diskfs/go-diskfs/backend"
"github.com/diskfs/go-diskfs/partition/gpt"
"github.com/diskfs/go-diskfs/partition/part"
)
// fakeTable implements partition.Table for testing.
// fakeTable implements partition.Table for testing.
// fakeTable implements partition.Table for testing.
type fakeTable struct {
parts []part.Partition
}
// Type satisfies partition.Table interface.
func (f *fakeTable) Type() string { return "" }
// Write satisfies partition.Table interface.
func (f *fakeTable) Write(_ backend.WritableFile, _ int64) error { return nil }
// Repair satisfies partition.Table interface (no-op).
func (f *fakeTable) Repair(_ uint64) error { return nil }
// Verify satisfies partition.Table interface (no-op).
func (f *fakeTable) Verify(_ backend.File, _ uint64) error { return nil }
// UUID satisfies partition.Table interface (no-op).
func (f *fakeTable) UUID() string { return "" }
func (f *fakeTable) GetPartitions() []part.Partition { return f.parts }
// TestPartitionIdentifiersToData_ByName verifies matching by partition name.
func TestPartitionIdentifiersToData_ByName(t *testing.T) {
// create one GPT partition
gp := &gpt.Partition{Start: 100, Size: 50 * 512, Name: "p1", GUID: "uuid1"}
tbl := &fakeTable{parts: []part.Partition{gp}}
// simulate sysfs data with same name
diskData := []partitionData{{name: "p1", label: "L", uuid: "uuid1", start: 100, size: 50 * 512, end: 149, number: 0}}
pi := NewPartitionIdentifier(IdentifierByName, "p1")
got, err := partitionIdentifiersToData(tbl, diskData, []PartitionIdentifier{pi})
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if len(got) != 1 {
t.Fatalf("got %d entries, want 1", len(got))
}
if got[0].start != 100*512 || got[0].size != 50*512 {
t.Errorf("unexpected partitionData %+v", got[0])
}
}
// TestPartitionIdentifiersToData_NotFound triggers an error for missing identifier.
func TestPartitionIdentifiersToData_NotFound(t *testing.T) {
tbl := &fakeTable{parts: []part.Partition{}}
diskData := []partitionData{}
pi := NewPartitionIdentifier(IdentifierByLabel, "nope")
_, err := partitionIdentifiersToData(tbl, diskData, []PartitionIdentifier{pi})
if err == nil {
t.Fatal("expected not-found error, got nil")
}
}
// TestPartitionChangesToResizeTarget_Mismatch verifies length-mismatch error.
func TestPartitionChangesToResizeTarget_Mismatch(t *testing.T) {
// no diskData => mismatch
tbl := &fakeTable{parts: []part.Partition{}}
diskData := []partitionData{}
pc := NewPartitionChange(IdentifierByName, "p", 123)
_, err := partitionChangesToResizeTarget(tbl, diskData, []PartitionChange{pc})
if err == nil || !strings.HasPrefix(err.Error(), "could not find partition for identifier:") {
t.Fatalf("unexpected error: %v", err)
}
}