public void Strings() { Assert.Equal("500", ByteUnits.ToByteString(500)); Assert.Equal("1000000", ByteUnits.ToByteString(1000000)); Assert.Equal("1KB", ByteUnits.ToKB(1000)); Assert.Equal("2KB", ByteUnits.ToKB(2000)); Assert.Equal("0.5KB", ByteUnits.ToKB(500)); Assert.Equal("1KiB", ByteUnits.ToKiB(1024)); Assert.Equal("2KiB", ByteUnits.ToKiB(2048)); Assert.Equal("0.5KiB", ByteUnits.ToKiB(512)); Assert.Equal("1MB", ByteUnits.ToMB(1000000)); Assert.Equal("2MB", ByteUnits.ToMB(2000000)); Assert.Equal("0.5MB", ByteUnits.ToMB(500000)); Assert.Equal("1MiB", ByteUnits.ToMiB(1 * ByteUnits.MebiBytes)); Assert.Equal("2MiB", ByteUnits.ToMiB(2 * ByteUnits.MebiBytes)); Assert.Equal("0.5MiB", ByteUnits.ToMiB(ByteUnits.MebiBytes / 2)); Assert.Equal("1GB", ByteUnits.ToGB(1000000000)); Assert.Equal("2GB", ByteUnits.ToGB(2000000000)); Assert.Equal("0.5GB", ByteUnits.ToGB(500000000)); Assert.Equal("1GiB", ByteUnits.ToGiB(1 * ByteUnits.GibiBytes)); Assert.Equal("2GiB", ByteUnits.ToGiB(2 * ByteUnits.GibiBytes)); Assert.Equal("0.5GiB", ByteUnits.ToGiB(ByteUnits.GibiBytes / 2)); Assert.Equal("1TB", ByteUnits.ToTB(1000000000000)); Assert.Equal("2TB", ByteUnits.ToTB(2000000000000)); Assert.Equal("0.5TB", ByteUnits.ToTB(500000000000)); Assert.Equal("1TiB", ByteUnits.ToTiB(1 * ByteUnits.TebiBytes)); Assert.Equal("2TiB", ByteUnits.ToTiB(2 * ByteUnits.TebiBytes)); Assert.Equal("0.5TiB", ByteUnits.ToTiB(ByteUnits.TebiBytes / 2)); Assert.Equal("1PB", ByteUnits.ToPB(1000000000000000)); Assert.Equal("2PB", ByteUnits.ToPB(2000000000000000)); Assert.Equal("0.5PB", ByteUnits.ToPB(500000000000000)); Assert.Equal("1PiB", ByteUnits.ToPiB(1 * ByteUnits.PebiBytes)); Assert.Equal("2PiB", ByteUnits.ToPiB(2 * ByteUnits.PebiBytes)); Assert.Equal("0.5PiB", ByteUnits.ToPiB(ByteUnits.PebiBytes / 2)); Assert.Equal("1EB", ByteUnits.ToEB(1000000000000000000)); Assert.Equal("2EB", ByteUnits.ToEB(2000000000000000000)); Assert.Equal("0.5EB", ByteUnits.ToEB(500000000000000000)); Assert.Equal("1EiB", ByteUnits.ToEiB(1 * ByteUnits.ExbiBytes)); Assert.Equal("2EiB", ByteUnits.ToEiB(2 * ByteUnits.ExbiBytes)); Assert.Equal("0.5EiB", ByteUnits.ToEiB(ByteUnits.ExbiBytes / 2)); }
/// <summary> /// Inspects the node to determine physical machine capabilities like /// processor count, RAM, and primary disk capacity and then sets the /// corresponding node labels in the cluster definition. /// </summary> /// <param name="node">The target node.</param> private void DetectLabels(NodeSshProxy <NodeDefinition> node) { CommandResponse result; // Download [/proc/meminfo] and extract the [MemTotal] value (in kB). result = node.SudoCommand("cat /proc/meminfo", RunOptions.FaultOnError); if (result.ExitCode == 0) { var memInfo = result.OutputText; var memTotalRegex = new Regex(@"^MemTotal:\s*(?<size>\d+)\s*kB", RegexOptions.Multiline); var memMatch = memTotalRegex.Match(memInfo); if (memMatch.Success && long.TryParse(memMatch.Groups["size"].Value, out var memSizeKiB)) { // Note that the RAM reported by Linux is somewhat less than the // physical RAM installed. node.Metadata.Labels.ComputeRam = (int)(memSizeKiB / 1024); // Convert KiB --> MiB } } // Download [/proc/cpuinfo] and count the number of processors. result = node.SudoCommand("cat /proc/cpuinfo", RunOptions.FaultOnError); if (result.ExitCode == 0) { var cpuInfo = result.OutputText; var processorRegex = new Regex(@"^processor\s*:\s*\d+", RegexOptions.Multiline); var processorMatches = processorRegex.Matches(cpuInfo); node.Metadata.Labels.ComputeCores = processorMatches.Count; } // Determine the primary disk size. // $hack(jefflill): // // I'm not entirely sure how to determine which block device is hosting // the primary file system for all systems. For now, I'm just going to // assume that this can be one of: // // /dev/sda1 // /dev/sda // /dev/xvda1 // /dev/xvda // // I'll try each of these in order and setting the label for the // first reasonable result we get back. var blockDevices = new string[] { "/dev/sda1", "/dev/sda", "/dev/xvda1", "/dev/xvda" }; foreach (var blockDevice in blockDevices) { result = node.SudoCommand($"lsblk -b --output SIZE -n -d {blockDevice}", RunOptions.LogOutput | RunOptions.FaultOnError); if (result.ExitCode == 0) { if (long.TryParse(result.OutputText.Trim(), out var deviceSize) && deviceSize > 0) { node.Metadata.Labels.StorageSize = ByteUnits.ToGiB(deviceSize); break; } } } }