func TestCpuInfomation(t *testing.T) { tt.StartTest(t) defer tt.FinishTest(t) cpuMap := CpuInfo() tt.TestNotEqual(t, cpuMap["logicalCores"], "", "Number of logical cores in a system cannot be less than 1.") tt.TestNotEqual(t, cpuMap["physicalCores"], "", "Number of physical cores in a system cannot be less than 1.") tt.TestNotEqual(t, cpuMap["processorModel"], "", "Processor model must exist in the system.") }
func TestOtherSystemInfomation(t *testing.T) { tt.StartTest(t) defer tt.FinishTest(t) otherSystemInfoMap := OtherSystemInfo() tt.TestNotEqual(t, otherSystemInfoMap["hostname"], "", "Hostname of the system could not be retrieved.") tt.TestNotEqual(t, otherSystemInfoMap["ipv4_address"], "", "IPv4 address of the system could not be retrieved.") tt.TestNotEqual(t, otherSystemInfoMap["os_pagesize"], "", "OS page size of the system could not be retrieved.") tt.TestNotEqual(t, otherSystemInfoMap["target_architecture"], "", "Target architecture of the system could not be retrieved.") tt.TestNotEqual(t, otherSystemInfoMap["target_os"], "", "Target OS of the system could not be retrieved.") }
func TestRelease(t *testing.T) { ipr, err := ParseIPRange("192.168.1.10-19") tt.TestExpectSuccess(t, err) alloc := NewAllocator(ipr) // test releasing when empty alloc.Release(net.ParseIP("192.168.1.11")) tt.TestEqual(t, alloc.remaining, int64(10)) // consume everything for { if alloc.Remaining() == 0 { break } ip := alloc.Allocate() tt.TestNotEqual(t, ip, nil) } // release an IP tt.TestEqual(t, alloc.remaining, int64(0)) alloc.Release(net.ParseIP("192.168.1.11")) tt.TestEqual(t, alloc.remaining, int64(1)) // allocate one more and should get that one tt.TestEqual(t, alloc.Allocate().String(), "192.168.1.11") tt.TestEqual(t, alloc.remaining, int64(0)) }
func TestSubtract(t *testing.T) { ipr, err := ParseIPRange("192.168.1.10-19") tt.TestExpectSuccess(t, err) alloc := NewAllocator(ipr) tt.TestEqual(t, alloc.remaining, int64(10)) // create a smaller range within the same one ipr2, err := ParseIPRange("192.168.1.10-14") tt.TestExpectSuccess(t, err) // subtract it alloc.Subtract(ipr2) // validate it tt.TestEqual(t, alloc.remaining, int64(5)) tt.TestEqual(t, len(alloc.reserved), 5) // consume everything and ensure we don't get an IP in the second range. for { if alloc.Remaining() == 0 { break } ip := alloc.Allocate() tt.TestNotEqual(t, ip, nil) tt.TestEqual(t, ipr2.Contains(ip), false) } }
func TestReserve(t *testing.T) { ipr, err := ParseIPRange("192.168.1.10-19") tt.TestExpectSuccess(t, err) alloc := NewAllocator(ipr) // reserve an IP reservedIP := net.ParseIP("192.168.1.11") alloc.Reserve(reservedIP) tt.TestEqual(t, alloc.remaining, int64(9)) tt.TestEqual(t, len(alloc.reserved), 1) // consume everything and ensure we don't get that IP for { if alloc.Remaining() == 0 { break } ip := alloc.Allocate() tt.TestNotEqual(t, ip, nil) tt.TestNotEqual(t, ip, reservedIP) } }
func TestMemoryInfomation(t *testing.T) { tt.StartTest(t) defer tt.FinishTest(t) memoryMap := MemoryInfo() tt.TestNotEqual(t, memoryMap["memoryTotal"], "", "Memory information of the system could not be retrieved.") tt.TestNotEqual(t, memoryMap["memoryFree"], "", "Cache information of the system could not be retrieved.") tt.TestNotEqual(t, memoryMap["buffers"], "", "Memory information of the system could not be retrieved.") tt.TestNotEqual(t, memoryMap["cache"], "", "Cache information of the system could not be retrieved.") tt.TestNotEqual(t, memoryMap["cacheInfo"], "", "Memory information of the system could not be retrieved.") tt.TestNotEqual(t, memoryMap["virtualizationInfo"], "", "Cache information of the system could not be retrieved.") tt.TestNotEqual(t, memoryMap["hypervisorInfo"], "", "Memory information of the system could not be retrieved.") }
func TestTempFile(t *testing.T) { r := strings.NewReader("blah") f, err := New(r) tt.TestExpectSuccess(t, err) b, err := ioutil.ReadAll(f) tt.TestExpectSuccess(t, err) tt.TestEqual(t, string(b), "blah") tf := f.(*unlinkOnCloseFile) st, err := os.Stat(tf.File.Name()) tt.TestExpectSuccess(t, err) tt.TestNotEqual(t, st, nil) tt.TestExpectSuccess(t, f.Close()) _, err = os.Stat(tf.File.Name()) tt.TestExpectError(t, err) tt.TestEqual(t, os.IsNotExist(err), true) }
func TestInvalidJsonResponse(t *testing.T) { tt.StartTest(t) defer tt.FinishTest(t) // create a test server server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { w.Header().Set("Content-Type", "application/json") w.WriteHeader(200) io.WriteString(w, `"Name":"Molly","Age":45}`) })) defer server.Close() client, err := New(server.URL) tt.TestExpectSuccess(t, err) req := client.NewJsonRequest("GET", "/", nil) var responsePerson person err = client.Result(req, &responsePerson) tt.TestExpectError(t, err) tt.TestNotEqual(t, err.(*json.UnmarshalTypeError), nil, "Should have been a json unmarshal error") }
func TestNew(t *testing.T) { tt.StartTest(t) defer tt.FinishTest(t) tt.TestNotEqual(t, New("some-socket"), nil) }