github.com/anth0d/nomad@v0.0.0-20221214183521-ae3a0a2cad06/plugins/drivers/utils_test.go (about) 1 package drivers 2 3 import ( 4 "testing" 5 6 "github.com/hashicorp/nomad/helper/uuid" 7 "github.com/hashicorp/nomad/nomad/structs" 8 "github.com/hashicorp/nomad/plugins/drivers/proto" 9 "github.com/stretchr/testify/assert" 10 "github.com/stretchr/testify/require" 11 ) 12 13 func TestResourceUsageRoundTrip(t *testing.T) { 14 input := &ResourceUsage{ 15 CpuStats: &CpuStats{ 16 SystemMode: 0, 17 UserMode: 0.9963907032120152, 18 TotalTicks: 21.920595295932515, 19 ThrottledPeriods: 2321, 20 ThrottledTime: 123, 21 Percent: 0.9963906952696598, 22 Measured: []string{"System Mode", "User Mode", "Percent"}, 23 }, 24 MemoryStats: &MemoryStats{ 25 RSS: 25681920, 26 Swap: 15681920, 27 Usage: 12, 28 MaxUsage: 23, 29 KernelUsage: 34, 30 KernelMaxUsage: 45, 31 Measured: []string{"RSS", "Swap"}, 32 }, 33 } 34 35 parsed := resourceUsageFromProto(resourceUsageToProto(input)) 36 37 require.EqualValues(t, parsed, input) 38 } 39 40 func TestTaskConfigRoundTrip(t *testing.T) { 41 42 input := &TaskConfig{ 43 ID: uuid.Generate(), 44 Name: "task", 45 JobName: "job", 46 TaskGroupName: "group", 47 Resources: &Resources{ 48 NomadResources: &structs.AllocatedTaskResources{ 49 Cpu: structs.AllocatedCpuResources{ 50 CpuShares: int64(100), 51 }, 52 Memory: structs.AllocatedMemoryResources{ 53 MemoryMB: int64(300), 54 }, 55 }, 56 LinuxResources: &LinuxResources{ 57 MemoryLimitBytes: 300 * 1024 * 1024, 58 CPUShares: 100, 59 PercentTicks: float64(100) / float64(3200), 60 }, 61 Ports: &structs.AllocatedPorts{ 62 { 63 Label: "port", 64 Value: 23456, 65 To: 8080, 66 HostIP: "10.0.0.1", 67 }, 68 }, 69 }, 70 Devices: []*DeviceConfig{ 71 { 72 TaskPath: "task", 73 HostPath: "host", 74 Permissions: "perms", 75 }, 76 }, 77 Mounts: []*MountConfig{ 78 { 79 TaskPath: "task", 80 HostPath: "host", 81 Readonly: true, 82 PropagationMode: "private", 83 }, 84 }, 85 Env: map[string]string{"gir": "zim"}, 86 DeviceEnv: map[string]string{"foo": "bar"}, 87 User: "user", 88 AllocDir: "allocDir", 89 StdoutPath: "stdout", 90 StderrPath: "stderr", 91 AllocID: uuid.Generate(), 92 NetworkIsolation: &NetworkIsolationSpec{ 93 Mode: NetIsolationModeGroup, 94 Path: "path", 95 Labels: map[string]string{"net": "abc"}, 96 }, 97 DNS: &DNSConfig{ 98 Servers: []string{"8.8.8.8"}, 99 Searches: []string{".consul"}, 100 Options: []string{"ndots:2"}, 101 }, 102 } 103 104 parsed := taskConfigFromProto(taskConfigToProto(input)) 105 106 require.EqualValues(t, input, parsed) 107 108 } 109 110 func Test_networkCreateRequestFromProto(t *testing.T) { 111 testCases := []struct { 112 inputPB *proto.CreateNetworkRequest 113 expectedOutput *NetworkCreateRequest 114 name string 115 }{ 116 { 117 inputPB: nil, 118 expectedOutput: nil, 119 name: "nil safety", 120 }, 121 { 122 inputPB: &proto.CreateNetworkRequest{ 123 AllocId: "59598b74-86e9-16ee-eb54-24c62935cc7c", 124 Hostname: "foobar", 125 }, 126 expectedOutput: &NetworkCreateRequest{ 127 Hostname: "foobar", 128 }, 129 name: "generic 1", 130 }, 131 } 132 133 for _, tc := range testCases { 134 t.Run(tc.name, func(t *testing.T) { 135 actualOutput := networkCreateRequestFromProto(tc.inputPB) 136 assert.Equal(t, tc.expectedOutput, actualOutput, tc.name) 137 }) 138 } 139 }