@@ -1820,6 +1820,137 @@ func TestVmSnapshot(t *testing.T) {
18201820 }
18211821}
18221822
1823+ func TestVmSnapshotEx (t * testing.T ) {
1824+ esx := ESX ()
1825+
1826+ Test (func (ctx context.Context , c * vim25.Client ) {
1827+ simVm := esx .Map ().Any ("VirtualMachine" )
1828+ vm := object .NewVirtualMachine (c , simVm .Reference ())
1829+
1830+ _ , err := fieldValue (reflect .ValueOf (simVm ), "snapshot" )
1831+ if err != errEmptyField {
1832+ t .Fatal ("snapshot property should be 'nil' if there are no snapshots" )
1833+ }
1834+
1835+ quiesceSpec := & types.VirtualMachineGuestQuiesceSpec {
1836+ Timeout : 100 ,
1837+ }
1838+
1839+ task , err := vm .CreateSnapshotEx (ctx , "root" , "description" , true , quiesceSpec )
1840+ if err != nil {
1841+ t .Fatal (err )
1842+ }
1843+
1844+ info , err := task .WaitForResult (ctx )
1845+ if err != nil {
1846+ t .Fatal (err )
1847+ }
1848+
1849+ snapRef , ok := info .Result .(types.ManagedObjectReference )
1850+ if ! ok {
1851+ t .Fatal ("expected ManagedObjectRefrence result for CreateSnapshot" )
1852+ }
1853+
1854+ _ , err = vm .FindSnapshot (ctx , snapRef .Value )
1855+ if err != nil {
1856+ t .Fatal (err , "snapshot should be found by result reference" )
1857+ }
1858+
1859+ _ , err = fieldValue (reflect .ValueOf (simVm ), "snapshot" )
1860+ if err == errEmptyField {
1861+ t .Fatal ("snapshot property should not be 'nil' if there are snapshots" )
1862+ }
1863+ // NOTE: fieldValue cannot be used for nil check
1864+ if len (simVm .(* VirtualMachine ).RootSnapshot ) == 0 {
1865+ t .Fatal ("rootSnapshot property should have elements if there are snapshots" )
1866+ }
1867+
1868+ task , err = vm .CreateSnapshotEx (ctx , "child" , "description" , true , quiesceSpec )
1869+ if err != nil {
1870+ t .Fatal (err )
1871+ }
1872+
1873+ err = task .Wait (ctx )
1874+ if err != nil {
1875+ t .Fatal (err )
1876+ }
1877+
1878+ _ , err = vm .FindSnapshot (ctx , "child" )
1879+ if err != nil {
1880+ t .Fatal (err )
1881+ }
1882+
1883+ task , err = vm .RevertToCurrentSnapshot (ctx , true )
1884+ if err != nil {
1885+ t .Fatal (err )
1886+ }
1887+
1888+ err = task .Wait (ctx )
1889+ if err != nil {
1890+ t .Fatal (err )
1891+ }
1892+
1893+ task , err = vm .RevertToSnapshot (ctx , "root" , true )
1894+ if err != nil {
1895+ t .Fatal (err )
1896+ }
1897+
1898+ err = task .Wait (ctx )
1899+ if err != nil {
1900+ t .Fatal (err )
1901+ }
1902+
1903+ task , err = vm .RemoveSnapshot (ctx , "child" , false , nil )
1904+ if err != nil {
1905+ t .Fatal (err )
1906+ }
1907+
1908+ err = task .Wait (ctx )
1909+ if err != nil {
1910+ t .Fatal (err )
1911+ }
1912+
1913+ _ , err = fieldValue (reflect .ValueOf (simVm ), "snapshot" )
1914+ if err == errEmptyField {
1915+ t .Fatal ("snapshot property should not be 'nil' if there are snapshots" )
1916+ }
1917+ // NOTE: fieldValue cannot be used for nil check
1918+ if len (simVm .(* VirtualMachine ).RootSnapshot ) == 0 {
1919+ t .Fatal ("rootSnapshot property should have elements if there are snapshots" )
1920+ }
1921+
1922+ _ , err = vm .FindSnapshot (ctx , "child" )
1923+ if err == nil {
1924+ t .Fatal ("child should be removed" )
1925+ }
1926+
1927+ task , err = vm .RemoveAllSnapshot (ctx , nil )
1928+ if err != nil {
1929+ t .Fatal (err )
1930+ }
1931+
1932+ err = task .Wait (ctx )
1933+ if err != nil {
1934+ t .Fatal (err )
1935+ }
1936+
1937+ _ , err = fieldValue (reflect .ValueOf (simVm ), "snapshot" )
1938+ if err != errEmptyField {
1939+ t .Fatal ("snapshot property should be 'nil' if there are no snapshots" )
1940+ }
1941+ // NOTE: fieldValue cannot be used for nil check
1942+ if len (simVm .(* VirtualMachine ).RootSnapshot ) != 0 {
1943+ t .Fatal ("rootSnapshot property should not have elements if there are no snapshots" )
1944+ }
1945+
1946+ _ , err = vm .FindSnapshot (ctx , "root" )
1947+ if err == nil {
1948+ t .Fatal ("all snapshots should be removed" )
1949+ }
1950+
1951+ }, esx )
1952+ }
1953+
18231954func TestVmMarkAsTemplate (t * testing.T ) {
18241955 ctx := context .Background ()
18251956
0 commit comments