...

Source file src/victortoso.com/qapi-go/pkg/qapi/enums_test.go

Documentation: victortoso.com/qapi-go/pkg/qapi

     1  package qapi
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"strings"
     7  	"testing"
     8  )
     9  
    10  func testAudiodev(t *testing.T) {
    11  	values := [AudiodevDriverWav + 1]string{"none", "alsa",
    12  		"coreaudio", "dbus", "dsound", "jack", "oss", "pa", "sdl",
    13  		"spice", "wav"}
    14  
    15  	for index, name := range values {
    16  		quotedName := fmt.Sprintf("\"%s\"", name)
    17  		enum := AudiodevDriver(index)
    18  
    19  		if b, err := json.Marshal(&enum); err != nil {
    20  			t.Fatalf("Failed to marshal %s due %s", name, err.Error())
    21  		} else if strings.Compare(string(b), quotedName) != 0 {
    22  			t.Fatalf("Enum to JSON does not match:\nGot:%s\nHas:%s\n", string(b), quotedName)
    23  		}
    24  
    25  		var v AudiodevDriver
    26  		if err := json.Unmarshal([]byte(quotedName), &v); err != nil {
    27  			t.Fatalf("Failed to unmarshal %s due %s", name, err.Error())
    28  		} else if v != enum {
    29  			t.Fatalf("JSON to ENUM does not match:\nGot:%v\nHas:%v\n", v, enum)
    30  		}
    31  	}
    32  }
    33  
    34  func testBlockdevDriver(t *testing.T) {
    35  	values := [BlockdevDriverVvfat + 1]string{"blkdebug", "blklogwrites",
    36  		"blkreplay", "blkverify", "bochs", "cloop", "compress", "copy-before-write",
    37  		"copy-on-read", "dmg", "file", "snapshot-access", "ftp", "ftps", "gluster", "host_cdrom",
    38  		"host_device", "http", "https", "iscsi", "luks", "nbd", "nfs", "null-aio",
    39  		"null-co", "nvme", "parallels", "preallocate", "qcow", "qcow2", "qed", "quorum",
    40  		"raw", "rbd", "replication", "ssh", "throttle", "vdi", "vhdx", "vmdk", "vpc", "vvfat"}
    41  
    42  	for index, name := range values {
    43  		quotedName := fmt.Sprintf("\"%s\"", name)
    44  		enum := BlockdevDriver(index)
    45  
    46  		if b, err := json.Marshal(&enum); err != nil {
    47  			t.Fatalf("Failed to marshal %s due %s", name, err.Error())
    48  		} else if strings.Compare(string(b), quotedName) != 0 {
    49  			t.Fatalf("Enum to JSON does not match:\nGot:%s\nHas:%s\n", string(b), quotedName)
    50  		}
    51  
    52  		var v BlockdevDriver
    53  		if err := json.Unmarshal([]byte(quotedName), &v); err != nil {
    54  			t.Fatalf("Failed to unmarshal %s due %s", name, err.Error())
    55  		} else if v != enum {
    56  			t.Fatalf("JSON to ENUM does not match:\nGot:%v\nHas:%v\n", v, enum)
    57  		}
    58  	}
    59  }
    60  
    61  func testBlockdevQcow2EncryptionFormat(t *testing.T) {
    62  	values := [BlockdevQcow2EncryptionFormatLuks + 1]string{"aes", "luks"}
    63  
    64  	for index, name := range values {
    65  		quotedName := fmt.Sprintf("\"%s\"", name)
    66  		enum := BlockdevQcow2EncryptionFormat(index)
    67  
    68  		if b, err := json.Marshal(&enum); err != nil {
    69  			t.Fatalf("Failed to marshal %s due %s", name, err.Error())
    70  		} else if strings.Compare(string(b), quotedName) != 0 {
    71  			t.Fatalf("Enum to JSON does not match:\nGot:%s\nHas:%s\n", string(b), quotedName)
    72  		}
    73  
    74  		var v BlockdevQcow2EncryptionFormat
    75  		if err := json.Unmarshal([]byte(quotedName), &v); err != nil {
    76  			t.Fatalf("Failed to unmarshal %s due %s", name, err.Error())
    77  		} else if v != enum {
    78  			t.Fatalf("JSON to ENUM does not match:\nGot:%v\nHas:%v\n", v, enum)
    79  		}
    80  	}
    81  }
    82  
    83  func testImageInfoSpecificKind(t *testing.T) {
    84  	values := [ImageInfoSpecificKindRbd + 1]string{"qcow2", "vmdk", "luks", "rbd"}
    85  
    86  	for index, name := range values {
    87  		quotedName := fmt.Sprintf("\"%s\"", name)
    88  		enum := ImageInfoSpecificKind(index)
    89  
    90  		if b, err := json.Marshal(&enum); err != nil {
    91  			t.Fatalf("Failed to marshal %s due %s", name, err.Error())
    92  		} else if strings.Compare(string(b), quotedName) != 0 {
    93  			t.Fatalf("Enum to JSON does not match:\nGot:%s\nHas:%s\n", string(b), quotedName)
    94  		}
    95  
    96  		var v ImageInfoSpecificKind
    97  		if err := json.Unmarshal([]byte(quotedName), &v); err != nil {
    98  			t.Fatalf("Failed to unmarshal %s due %s", name, err.Error())
    99  		} else if v != enum {
   100  			t.Fatalf("JSON to ENUM does not match:\nGot:%v\nHas:%v\n", v, enum)
   101  		}
   102  	}
   103  }
   104  
   105  func testRbdImageEncryptionFormat(t *testing.T) {
   106  	values := [RbdImageEncryptionFormatLuks2 + 1]string{"luks", "luks2"}
   107  
   108  	for index, name := range values {
   109  		quotedName := fmt.Sprintf("\"%s\"", name)
   110  		enum := RbdImageEncryptionFormat(index)
   111  
   112  		if b, err := json.Marshal(&enum); err != nil {
   113  			t.Fatalf("Failed to marshal %s due %s", name, err.Error())
   114  		} else if strings.Compare(string(b), quotedName) != 0 {
   115  			t.Fatalf("Enum to JSON does not match:\nGot:%s\nHas:%s\n", string(b), quotedName)
   116  		}
   117  
   118  		var v RbdImageEncryptionFormat
   119  		if err := json.Unmarshal([]byte(quotedName), &v); err != nil {
   120  			t.Fatalf("Failed to unmarshal %s due %s", name, err.Error())
   121  		} else if v != enum {
   122  			t.Fatalf("JSON to ENUM does not match:\nGot:%v\nHas:%v\n", v, enum)
   123  		}
   124  	}
   125  }
   126  
   127  func testSshHostKeyCheckMode(t *testing.T) {
   128  	values := [SshHostKeyCheckModeKnown_Hosts + 1]string{"none", "hash", "known_hosts"}
   129  
   130  	for index, name := range values {
   131  		quotedName := fmt.Sprintf("\"%s\"", name)
   132  		enum := SshHostKeyCheckMode(index)
   133  
   134  		if b, err := json.Marshal(&enum); err != nil {
   135  			t.Fatalf("Failed to marshal %s due %s", name, err.Error())
   136  		} else if strings.Compare(string(b), quotedName) != 0 {
   137  			t.Fatalf("Enum to JSON does not match:\nGot:%s\nHas:%s\n", string(b), quotedName)
   138  		}
   139  
   140  		var v SshHostKeyCheckMode
   141  		if err := json.Unmarshal([]byte(quotedName), &v); err != nil {
   142  			t.Fatalf("Failed to unmarshal %s due %s", name, err.Error())
   143  		} else if v != enum {
   144  			t.Fatalf("JSON to ENUM does not match:\nGot:%v\nHas:%v\n", v, enum)
   145  		}
   146  	}
   147  }
   148  
   149  func testBlockExportType(t *testing.T) {
   150  	values := [BlockExportTypeFuse + 1]string{"nbd", "vhost-user-blk", "fuse"}
   151  
   152  	for index, name := range values {
   153  		quotedName := fmt.Sprintf("\"%s\"", name)
   154  		enum := BlockExportType(index)
   155  
   156  		if b, err := json.Marshal(&enum); err != nil {
   157  			t.Fatalf("Failed to marshal %s due %s", name, err.Error())
   158  		} else if strings.Compare(string(b), quotedName) != 0 {
   159  			t.Fatalf("Enum to JSON does not match:\nGot:%s\nHas:%s\n", string(b), quotedName)
   160  		}
   161  
   162  		var v BlockExportType
   163  		if err := json.Unmarshal([]byte(quotedName), &v); err != nil {
   164  			t.Fatalf("Failed to unmarshal %s due %s", name, err.Error())
   165  		} else if v != enum {
   166  			t.Fatalf("JSON to ENUM does not match:\nGot:%v\nHas:%v\n", v, enum)
   167  		}
   168  	}
   169  }
   170  
   171  func testChardevBackendKind(t *testing.T) {
   172  	values := [ChardevBackendKindMemory + 1]string{"file", "serial", "parallel",
   173  		"pipe", "socket", "udp", "pty", "null", "mux", "msmouse", "wctablet",
   174  		"braille", "testdev", "stdio", "console", "spicevmc", "spiceport",
   175  		"qemu-vdagent", "dbus", "vc", "ringbuf", "memory"}
   176  
   177  	for index, name := range values {
   178  		quotedName := fmt.Sprintf("\"%s\"", name)
   179  		enum := ChardevBackendKind(index)
   180  
   181  		if b, err := json.Marshal(&enum); err != nil {
   182  			t.Fatalf("Failed to marshal %s due %s", name, err.Error())
   183  		} else if strings.Compare(string(b), quotedName) != 0 {
   184  			t.Fatalf("Enum to JSON does not match:\nGot:%s\nHas:%s\n", string(b), quotedName)
   185  		}
   186  
   187  		var v ChardevBackendKind
   188  		if err := json.Unmarshal([]byte(quotedName), &v); err != nil {
   189  			t.Fatalf("Failed to unmarshal %s due %s", name, err.Error())
   190  		} else if v != enum {
   191  			t.Fatalf("JSON to ENUM does not match:\nGot:%v\nHas:%v\n", v, enum)
   192  		}
   193  	}
   194  }
   195  
   196  func testQCryptoBlockFormat(t *testing.T) {
   197  	values := [QCryptoBlockFormatLuks + 1]string{"qcow", "luks"}
   198  
   199  	for index, name := range values {
   200  		quotedName := fmt.Sprintf("\"%s\"", name)
   201  		enum := QCryptoBlockFormat(index)
   202  
   203  		if b, err := json.Marshal(&enum); err != nil {
   204  			t.Fatalf("Failed to marshal %s due %s", name, err.Error())
   205  		} else if strings.Compare(string(b), quotedName) != 0 {
   206  			t.Fatalf("Enum to JSON does not match:\nGot:%s\nHas:%s\n", string(b), quotedName)
   207  		}
   208  
   209  		var v QCryptoBlockFormat
   210  		if err := json.Unmarshal([]byte(quotedName), &v); err != nil {
   211  			t.Fatalf("Failed to unmarshal %s due %s", name, err.Error())
   212  		} else if v != enum {
   213  			t.Fatalf("JSON to ENUM does not match:\nGot:%v\nHas:%v\n", v, enum)
   214  		}
   215  	}
   216  }
   217  
   218  func TestTypeUnion(t *testing.T) {
   219  	testAudiodev(t)
   220  	testBlockdevDriver(t)
   221  	testBlockdevQcow2EncryptionFormat(t)
   222  	testImageInfoSpecificKind(t)
   223  	testRbdImageEncryptionFormat(t)
   224  	testSshHostKeyCheckMode(t)
   225  	testBlockExportType(t)
   226  	testChardevBackendKind(t)
   227  	testQCryptoBlockFormat(t)
   228  }
   229  

View as plain text