public void CreateIso() { // Verify that we can create a new ISO file from a folder. using (var tempFolder = new TempFolder()) { using (var tempIso = new TempFile(suffix: ".iso")) { for (int i = 0; i < 10; i++) { File.WriteAllText(Path.Combine(tempFolder.Path, $"{i}.txt"), $"{i}"); } KubeHelper.CreateIsoFile(tempFolder.Path, tempIso.Path, "TEST"); using (var file = new FileStream(tempIso.Path, FileMode.Open, FileAccess.Read)) { Assert.True(file.Length > 0); } } } }
/// <inheritdoc/> public override async Task RunAsync(CommandLine commandLine) { if (commandLine.Arguments.Length != 2) { Help(); Program.Exit(1); } var sourceFolder = commandLine.Arguments.ElementAtOrDefault(0); var isoPath = commandLine.Arguments.ElementAtOrDefault(1); var linux = commandLine.GetFlag("--linux"); var label = commandLine.GetOption("--label"); if (string.IsNullOrEmpty(sourceFolder) || string.IsNullOrEmpty(isoPath)) { Help(); Program.Exit(1); } if (linux) { foreach (var file in Directory.EnumerateFiles(sourceFolder)) { var text = File.ReadAllText(file); text = NeonHelper.ToLinuxLineEndings(text); File.WriteAllText(file, text); } } KubeHelper.CreateIsoFile(sourceFolder, isoPath, label); Program.Exit(0); await Task.CompletedTask; }
public void VirtualMachines(bool isAdmin) { try { using (CreateService(isAdmin)) { var hyperVProxy = new HyperVProxy(isAdmin, socketPath); // List VMs before we create any below. We had an issue once where we'd see // a [NullReferenceException] when there were no VMs. var vms = hyperVProxy.ListVms(); // Create a VM and verify. hyperVProxy.AddVm( machineName: TestMachineName1, memorySize: "1 GiB", processorCount: 2, drivePath: test1VhdxPath, checkpointDrives: false, templateDrivePath: templatePath, switchName: "External"); var vm = hyperVProxy.GetVm(machineName: TestMachineName1); Assert.NotNull(vm); Assert.Equal(TestMachineName1, vm.Name); Assert.Equal(VirtualMachineState.Off, vm.State); Assert.Equal("External", vm.SwitchName); // Start the VM and verify. hyperVProxy.StartVm(machineName: TestMachineName1); vm = hyperVProxy.GetVm(machineName: TestMachineName1); Assert.NotNull(vm); Assert.Equal(VirtualMachineState.Running, vm.State); // Fetch the VM network adapters. var adapters = hyperVProxy.GetVmNetworkAdapters(TestMachineName1); Assert.NotNull(adapters); Assert.NotEmpty(adapters); // Save the VM and verify. hyperVProxy.SaveVm(machineName: TestMachineName1); vm = hyperVProxy.GetVm(machineName: TestMachineName1); Assert.NotNull(vm); Assert.Equal(VirtualMachineState.Saved, vm.State); // Create and start another VM and verify. hyperVProxy.AddVm( machineName: TestMachineName2, memorySize: "1 GiB", processorCount: 2, drivePath: test2VhdxPath, checkpointDrives: false, templateDrivePath: templatePath, switchName: "External"); vm = hyperVProxy.GetVm(machineName: TestMachineName2); Assert.NotNull(vm); Assert.Equal(TestMachineName2, vm.Name); Assert.Equal(VirtualMachineState.Off, vm.State); Assert.Equal("External", vm.SwitchName); hyperVProxy.StartVm(machineName: TestMachineName2); vm = hyperVProxy.GetVm(machineName: TestMachineName2); Assert.Equal(VirtualMachineState.Running, vm.State); // List and check the VM existence. var list = hyperVProxy.ListVms(); Assert.Contains(list, item => item.Name == TestMachineName1); Assert.Contains(list, item => item.Name == TestMachineName2); Assert.True(hyperVProxy.VmExists(TestMachineName1)); Assert.True(hyperVProxy.VmExists(TestMachineName2)); Assert.False(hyperVProxy.VmExists(Guid.NewGuid().ToString("d"))); // Test DVD/CD insert and eject operations. using (var tempFolder = new TempFolder()) { var isoFolder = Path.Combine(tempFolder.Path, "iso-contents"); var isoPath = Path.Combine(tempFolder.Path, "data.iso"); Directory.CreateDirectory(isoFolder); File.WriteAllText(Path.Combine(isoFolder, "hello.txt"), "HELLO WORLD!"); KubeHelper.CreateIsoFile(isoFolder, isoPath); // $todo(jefflill): Eject is failing: // // https://github.com/nforgeio/neonKUBE/issues/1456 // hyperVProxy.InsertVmDvd(TestMachineName2, isoPath); // hyperVProxy.EjectVmDvd(TestMachineName2); } // Stop the second VM and verify. hyperVProxy.StopVm(machineName: TestMachineName2, turnOff: true); vm = hyperVProxy.GetVm(machineName: TestMachineName2); Assert.Equal(VirtualMachineState.Off, vm.State); // Add a drive to the second VM and verify. hyperVProxy.AddVmDrive(TestMachineName2, new VirtualDrive() { Path = extraVhdxPath, Size = 1 * ByteUnits.GibiBytes, IsDynamic = true }); var drives = hyperVProxy.GetVmDrives(machineName: TestMachineName1); // $todo(jefflill): We should be seeing two drives here: // // https://github.com/nforgeio/neonKUBE/issues/1455 Assert.NotEmpty(drives); // Assert.Equal(2, drives.Count); // Compact the extra drive we added to the second VM. hyperVProxy.CompactDrive(extraVhdxPath); // Remove the VMs and verify. hyperVProxy.RemoveVm(machineName: TestMachineName1, keepDrives: false); Assert.Null(hyperVProxy.GetVm(machineName: TestMachineName1)); } } finally { ClearState(); } }