Skip to content

Commit

Permalink
internal: UUIDs now randomly generated using pborman's uuid tool.
Browse files Browse the repository at this point in the history
  • Loading branch information
Rahuls0720 committed Jun 11, 2018
1 parent e661921 commit 60120e2
Show file tree
Hide file tree
Showing 14 changed files with 199 additions and 135 deletions.
8 changes: 4 additions & 4 deletions config/v2_2/translate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,15 +161,15 @@ func TestTranslateFromV2_1(t *testing.T) {
Size: 100,
Start: 50,
TypeGUID: "HI",
GUID: "4F68BCE3-E8CD-4DB1-96E7-FBCAF984B709",
GUID: "$uuid36",
},
{
Label: "DATA",
Number: 12,
Size: 1000,
Start: 300,
TypeGUID: "LO",
GUID: "3B8F8425-20E0-4F3B-907F-1A25A76F98E8",
GUID: "$uuid37",
},
},
},
Expand All @@ -194,15 +194,15 @@ func TestTranslateFromV2_1(t *testing.T) {
Size: 100,
Start: 50,
TypeGUID: "HI",
GUID: "4F68BCE3-E8CD-4DB1-96E7-FBCAF984B709",
GUID: "$uuid36",
},
{
Label: "DATA",
Number: 12,
Size: 1000,
Start: 300,
TypeGUID: "LO",
GUID: "3B8F8425-20E0-4F3B-907F-1A25A76F98E8",
GUID: "$uuid37",
},
},
},
Expand Down
8 changes: 4 additions & 4 deletions config/v2_3_experimental/translate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,15 +250,15 @@ func TestTranslate(t *testing.T) {
Size: 100,
Start: 50,
TypeGUID: "HI",
GUID: "4F68BCE3-E8CD-4DB1-96E7-FBCAF984B709",
GUID: "$uuid36",
},
{
Label: "DATA",
Number: 12,
Size: 1000,
Start: 300,
TypeGUID: "LO",
GUID: "3B8F8425-20E0-4F3B-907F-1A25A76F98E8",
GUID: "$uuid37",
},
},
},
Expand All @@ -283,15 +283,15 @@ func TestTranslate(t *testing.T) {
Size: util.IntToPtr(100),
Start: util.IntToPtr(50),
TypeGUID: "HI",
GUID: "4F68BCE3-E8CD-4DB1-96E7-FBCAF984B709",
GUID: "$uuid36",
},
{
Label: util.StrToPtrStrict("DATA"),
Number: 12,
Size: util.IntToPtr(1000),
Start: util.IntToPtr(300),
TypeGUID: "LO",
GUID: "3B8F8425-20E0-4F3B-907F-1A25A76F98E8",
GUID: "$uuid37",
},
},
},
Expand Down
8 changes: 4 additions & 4 deletions internal/config/translate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ func TestTranslate(t *testing.T) {
Size: util.IntToPtr(100),
Start: util.IntToPtr(50),
TypeGUID: "HI",
GUID: "4F68BCE3-E8CD-4DB1-96E7-FBCAF984B709",
GUID: "$uuid36",
WipePartitionEntry: true,
ShouldExist: util.BoolToPtr(true),
},
Expand All @@ -260,7 +260,7 @@ func TestTranslate(t *testing.T) {
Size: util.IntToPtr(1000),
Start: util.IntToPtr(300),
TypeGUID: "LO",
GUID: "3B8F8425-20E0-4F3B-907F-1A25A76F98E8",
GUID: "$uuid37",
WipePartitionEntry: false,
ShouldExist: util.BoolToPtr(false),
},
Expand All @@ -287,7 +287,7 @@ func TestTranslate(t *testing.T) {
Size: util.IntToPtr(100),
Start: util.IntToPtr(50),
TypeGUID: "HI",
GUID: "4F68BCE3-E8CD-4DB1-96E7-FBCAF984B709",
GUID: "$uuid36",
WipePartitionEntry: true,
ShouldExist: util.BoolToPtr(true),
},
Expand All @@ -297,7 +297,7 @@ func TestTranslate(t *testing.T) {
Size: util.IntToPtr(1000),
Start: util.IntToPtr(300),
TypeGUID: "LO",
GUID: "3B8F8425-20E0-4F3B-907F-1A25A76F98E8",
GUID: "$uuid37",
WipePartitionEntry: false,
ShouldExist: util.BoolToPtr(false),
},
Expand Down
63 changes: 62 additions & 1 deletion tests/blackbox_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@ import (
"io/ioutil"
"os"
"path/filepath"
"strconv"
"strings"
"testing"
"time"
"unicode"

config "github.com/coreos/ignition/config/v2_3_experimental"
"github.com/coreos/ignition/tests/register"
Expand Down Expand Up @@ -76,6 +78,9 @@ func TestIgnitionBlackBoxNegative(t *testing.T) {
func outer(t *testing.T, test types.Test, negativeTests bool) error {
t.Log(test.Name)

//Maps $uuid<num> such that two such variables with the same <num> have identical UUID
UUIDmap := make(map[int]string)

ctx, cancelFunc := context.WithDeadline(context.Background(), time.Now().Add(testTimeout))
defer cancelFunc()

Expand Down Expand Up @@ -123,7 +128,7 @@ func outer(t *testing.T, test types.Test, negativeTests bool) error {
// Finish data setup
for _, part := range disk.Partitions {
if part.GUID == "" {
part.GUID, err = generateUUID(t, ctx)
part.GUID, err = generateUUID(t)
if err != nil {
return err
}
Expand All @@ -143,6 +148,25 @@ func outer(t *testing.T, test types.Test, negativeTests bool) error {
}
test.Out[i].SetOffsets()

//Replace all UUID variables (format $uuid<num>) in configs and partitions with UUID
test.Config = replaceUUIDVars(t, test.Config, UUIDmap)

for _, disk := range test.In {
for _, partition := range disk.Partitions {
partition.TypeGUID = replaceUUIDVars(t, partition.TypeGUID, UUIDmap)
partition.GUID = replaceUUIDVars(t, partition.GUID, UUIDmap)
partition.FilesystemUUID = replaceUUIDVars(t, partition.FilesystemUUID, UUIDmap)
}
}

for _, disk := range test.Out {
for _, partition := range disk.Partitions {
partition.TypeGUID = replaceUUIDVars(t, partition.TypeGUID, UUIDmap)
partition.GUID = replaceUUIDVars(t, partition.GUID, UUIDmap)
partition.FilesystemUUID = replaceUUIDVars(t, partition.FilesystemUUID, UUIDmap)
}
}

// Creation
err = createVolume(t, ctx, tmpDirectory, i, disk.ImageFile, imageSize, disk.Partitions)
// Move value into the local scope, because disk.ImageFile will change
Expand Down Expand Up @@ -311,3 +335,40 @@ func outer(t *testing.T, test types.Test, negativeTests bool) error {
}
return nil
}

//Identify and replace $uuid<num> with correct UUID
//Variables with matching <num> should have identical UUIDs
func replaceUUIDVars(t *testing.T, str string, UUIDmap map[int]string) string {
var finalStr string

tokenizedArr := strings.Split(str, " ")
for _, str := range tokenizedArr {
if strings.Contains(strings.ToLower(str), "$uuid") {

var num_str string //holds string <num>
var num_int int //holds int conversion of <num>

//handle any sized <num>
for _, char := range str[strings.Index(str, "$uuid")+5:] {
if unicode.IsDigit(char) {
num_str += string(char)
} else {
break
}
}

num_int, _ = strconv.Atoi(num_str)
str = strings.Replace(str, "$uuid"+num_str, getUUID(t, num_int, UUIDmap), 1)
}
finalStr += str + " "
}
return finalStr[:len(finalStr)-1]
}

//Format: $uuid<num> where num is key; value is uuid
func getUUID(t *testing.T, num int, UUIDmap map[int]string) string {
if _, ok := UUIDmap[num]; !ok {
UUIDmap[num], _ = generateUUID(t)
}
return UUIDmap[num]
}
11 changes: 7 additions & 4 deletions tests/filesystem.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (

"github.com/coreos/ignition/internal/distro"
"github.com/coreos/ignition/tests/types"
"github.com/pborman/uuid"
)

func run(t *testing.T, ctx context.Context, command string, args ...string) ([]byte, error) {
Expand Down Expand Up @@ -374,12 +375,14 @@ func removeEmpty(strings []string) []string {
return r
}

func generateUUID(t *testing.T, ctx context.Context) (string, error) {
out, err := run(t, ctx, "uuidgen")
func generateUUID(t *testing.T) (string, error) {
out := uuid.NewRandom()

/*out, err := run(t, ctx, "uuidgen")
if err != nil {
return "", err
}
return strings.TrimSpace(string(out)), nil
}*/
return strings.TrimSpace(out.String()), nil
}

func createFilesForPartitions(t *testing.T, partitions []*types.Partition) error {
Expand Down
28 changes: 14 additions & 14 deletions tests/positive/filesystems/reformat_filesystem.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func ReformatToBTRFS_2_0_0() types.Test {
"format": "btrfs",
"create": {
"force": true,
"options": [ "--label=OEM", "--uuid=CA7D7CCB-63ED-4C53-861C-1742536059D4" ]
"options": [ "--label=OEM", "--uuid=$uuid12" ]
}
}
}]
Expand Down Expand Up @@ -85,7 +85,7 @@ func ReformatToXFS_2_0_0() types.Test {
"format": "xfs",
"create": {
"force": true,
"options": [ "-L", "OEM", "-m", "uuid=CA7D7CCB-63ED-4C53-861C-1742536059CD" ]
"options": [ "-L", "OEM", "-m", "uuid=$uuid13" ]
}
}
}]
Expand Down Expand Up @@ -121,14 +121,14 @@ func ReformatToVFAT_2_0_0() types.Test {
"format": "vfat",
"create": {
"force": true,
"options": [ "-n", "OEM", "-i", "CA7D7CCB-63ED-4C53-861C-1742536059CE" ]
"options": [ "-n", "OEM", "-i", "$uuid26" ]
}
}
}]
}
}`
out[0].Partitions.GetPartition("OEM").FilesystemType = "vfat"
out[0].Partitions.GetPartition("OEM").FilesystemUUID = "CA7D7CCB-63ED-4C53-861C-1742536059CE"
out[0].Partitions.GetPartition("OEM").FilesystemUUID = "$uuid26"

return types.Test{
Name: name,
Expand Down Expand Up @@ -158,15 +158,15 @@ func ReformatToEXT4_2_0_0() types.Test {
"format": "ext4",
"create": {
"force": true,
"options": [ "-L", "OEM", "-U", "CA7D7CCB-63ED-4C53-861C-1742536059CF" ]
"options": [ "-L", "OEM", "-U", "$uuid27" ]
}
}
}]
}
}`
in[0].Partitions.GetPartition("OEM").FilesystemType = "ext2"
out[0].Partitions.GetPartition("OEM").FilesystemType = "ext4"
out[0].Partitions.GetPartition("OEM").FilesystemUUID = "CA7D7CCB-63ED-4C53-861C-1742536059CF"
out[0].Partitions.GetPartition("OEM").FilesystemUUID = "$uuid27"

return types.Test{
Name: name,
Expand Down Expand Up @@ -195,14 +195,14 @@ func ReformatToBTRFS_2_1_0() types.Test {
"device": "$DEVICE",
"format": "btrfs",
"label": "OEM",
"uuid": "CA7D7CCB-63ED-4C53-861C-1742536059D0",
"uuid": "$uuid28",
"wipeFilesystem": true
}
}]
}
}`
out[0].Partitions.GetPartition("OEM").FilesystemType = "btrfs"
out[0].Partitions.GetPartition("OEM").FilesystemUUID = "CA7D7CCB-63ED-4C53-861C-1742536059D0"
out[0].Partitions.GetPartition("OEM").FilesystemUUID = "$uuid28"

return types.Test{
Name: name,
Expand Down Expand Up @@ -231,14 +231,14 @@ func ReformatToXFS_2_1_0() types.Test {
"device": "$DEVICE",
"format": "xfs",
"label": "OEM",
"uuid": "CA7D7CCB-63ED-4C53-861C-1742536059D1",
"uuid": "$uuid29",
"wipeFilesystem": true
}
}]
}
}`
out[0].Partitions.GetPartition("OEM").FilesystemType = "xfs"
out[0].Partitions.GetPartition("OEM").FilesystemUUID = "CA7D7CCB-63ED-4C53-861C-1742536059D1"
out[0].Partitions.GetPartition("OEM").FilesystemUUID = "$uuid29"

return types.Test{
Name: name,
Expand Down Expand Up @@ -303,15 +303,15 @@ func ReformatToEXT4_2_1_0() types.Test {
"device": "$DEVICE",
"format": "ext4",
"label": "OEM",
"uuid": "CA7D7CCB-63ED-4C53-861C-1742536059D2",
"uuid": "$uuid30",
"wipeFilesystem": true
}
}]
}
}`
in[0].Partitions.GetPartition("OEM").FilesystemType = "ext2"
out[0].Partitions.GetPartition("OEM").FilesystemType = "ext4"
out[0].Partitions.GetPartition("OEM").FilesystemUUID = "CA7D7CCB-63ED-4C53-861C-1742536059D2"
out[0].Partitions.GetPartition("OEM").FilesystemUUID = "$uuid30"

return types.Test{
Name: name,
Expand Down Expand Up @@ -340,15 +340,15 @@ func ReformatToSWAP_2_1_0() types.Test {
"device": "$DEVICE",
"format": "swap",
"label": "OEM",
"uuid": "CA7D7CCB-63ED-4C53-861C-1742536059D3",
"uuid": "$uuid31",
"wipeFilesystem": true
}
}]
}
}`
in[0].Partitions.GetPartition("OEM").FilesystemType = "ext2"
out[0].Partitions.GetPartition("OEM").FilesystemType = "swap"
out[0].Partitions.GetPartition("OEM").FilesystemUUID = "CA7D7CCB-63ED-4C53-861C-1742536059D3"
out[0].Partitions.GetPartition("OEM").FilesystemUUID = "$uuid31"

return types.Test{
Name: name,
Expand Down
6 changes: 3 additions & 3 deletions tests/positive/filesystems/reuse_filesystem.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func ReuseExistingFilesystem() types.Test {
"wipeFilesystem": false,
"format": "btrfs",
"label": "data",
"uuid": "8A7A6E26-5E8F-4CCA-A654-46215D4696AC"
"uuid": "$uuid2"
}
}
]
Expand All @@ -58,7 +58,7 @@ func ReuseExistingFilesystem() types.Test {
Length: 2621440,
FilesystemType: "btrfs",
FilesystemLabel: "data",
FilesystemUUID: "8A7A6E26-5E8F-4CCA-A654-46215D4696AC",
FilesystemUUID: "$uuid2",
Files: []types.File{
{
Node: types.Node{
Expand All @@ -80,7 +80,7 @@ func ReuseExistingFilesystem() types.Test {
Length: 2621440,
FilesystemType: "btrfs",
FilesystemLabel: "data",
FilesystemUUID: "8A7A6E26-5E8F-4CCA-A654-46215D4696AC",
FilesystemUUID: "$uuid2",
Files: []types.File{
{
Node: types.Node{
Expand Down
Loading

0 comments on commit 60120e2

Please sign in to comment.