From 3b89d997a938237a8984f8f9188faa6414633308 Mon Sep 17 00:00:00 2001 From: Aleksander Mistewicz Date: Tue, 17 Jul 2018 15:04:34 +0200 Subject: [PATCH] Fix tests in CI It skips tests when information on current running user is unavailable. Local IP address is retrieved by lookup of "localhost". IP addresses are used instead of nil values. Interface of uuid package has been changed so code using it has been adapted. Mock generated for stm.Interface has been updated. Change-Id: Ide48575fa7a589c0cad00478898c2a651fda77d5 Signed-off-by: Aleksander Mistewicz --- dryad/muxpi_mock_test.go | 61 +++++++++++++++++++++++++++++++++++++++++++++ dryad/rusalka_test.go | 4 ++- dryad/user_test.go | 4 ++- tunnels/tunnels_test.go | 22 +++++++++++----- workers/worker_list_test.go | 16 ++++++++---- workers/workers_test.go | 10 ++++++-- 6 files changed, 102 insertions(+), 15 deletions(-) diff --git a/dryad/muxpi_mock_test.go b/dryad/muxpi_mock_test.go index b89394a..8d03d08 100644 --- a/dryad/muxpi_mock_test.go +++ b/dryad/muxpi_mock_test.go @@ -71,6 +71,31 @@ func (mr *MockInterfaceMockRecorder) GetCurrent() *gomock.Call { return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetCurrent", reflect.TypeOf((*MockInterface)(nil).GetCurrent)) } +// GetCurrentRecord mocks base method +func (m *MockInterface) GetCurrentRecord() ([]int, error) { + ret := m.ctrl.Call(m, "GetCurrentRecord") + ret0, _ := ret[0].([]int) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// GetCurrentRecord indicates an expected call of GetCurrentRecord +func (mr *MockInterfaceMockRecorder) GetCurrentRecord() *gomock.Call { + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetCurrentRecord", reflect.TypeOf((*MockInterface)(nil).GetCurrentRecord)) +} + +// HDMI mocks base method +func (m *MockInterface) HDMI(arg0 bool) error { + ret := m.ctrl.Call(m, "HDMI", arg0) + ret0, _ := ret[0].(error) + return ret0 +} + +// HDMI indicates an expected call of HDMI +func (mr *MockInterfaceMockRecorder) HDMI(arg0 interface{}) *gomock.Call { + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "HDMI", reflect.TypeOf((*MockInterface)(nil).HDMI), arg0) +} + // PowerTick mocks base method func (m *MockInterface) PowerTick(arg0 time.Duration) error { ret := m.ctrl.Call(m, "PowerTick", arg0) @@ -95,6 +120,18 @@ func (mr *MockInterfaceMockRecorder) PrintText(arg0, arg1, arg2, arg3 interface{ return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "PrintText", reflect.TypeOf((*MockInterface)(nil).PrintText), arg0, arg1, arg2, arg3) } +// SetDyper mocks base method +func (m *MockInterface) SetDyper(arg0 stm.Dyper, arg1 bool) error { + ret := m.ctrl.Call(m, "SetDyper", arg0, arg1) + ret0, _ := ret[0].(error) + return ret0 +} + +// SetDyper indicates an expected call of SetDyper +func (mr *MockInterfaceMockRecorder) SetDyper(arg0, arg1 interface{}) *gomock.Call { + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "SetDyper", reflect.TypeOf((*MockInterface)(nil).SetDyper), arg0, arg1) +} + // SetLED mocks base method func (m *MockInterface) SetLED(arg0 stm.LED, arg1, arg2, arg3 byte) error { ret := m.ctrl.Call(m, "SetLED", arg0, arg1, arg2, arg3) @@ -107,6 +144,30 @@ func (mr *MockInterfaceMockRecorder) SetLED(arg0, arg1, arg2, arg3 interface{}) return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "SetLED", reflect.TypeOf((*MockInterface)(nil).SetLED), arg0, arg1, arg2, arg3) } +// StartCurrentRecord mocks base method +func (m *MockInterface) StartCurrentRecord(arg0 int, arg1 time.Duration) error { + ret := m.ctrl.Call(m, "StartCurrentRecord", arg0, arg1) + ret0, _ := ret[0].(error) + return ret0 +} + +// StartCurrentRecord indicates an expected call of StartCurrentRecord +func (mr *MockInterfaceMockRecorder) StartCurrentRecord(arg0, arg1 interface{}) *gomock.Call { + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "StartCurrentRecord", reflect.TypeOf((*MockInterface)(nil).StartCurrentRecord), arg0, arg1) +} + +// StopCurrentRecord mocks base method +func (m *MockInterface) StopCurrentRecord() error { + ret := m.ctrl.Call(m, "StopCurrentRecord") + ret0, _ := ret[0].(error) + return ret0 +} + +// StopCurrentRecord indicates an expected call of StopCurrentRecord +func (mr *MockInterfaceMockRecorder) StopCurrentRecord() *gomock.Call { + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "StopCurrentRecord", reflect.TypeOf((*MockInterface)(nil).StopCurrentRecord)) +} + // TS mocks base method func (m *MockInterface) TS() error { ret := m.ctrl.Call(m, "TS") diff --git a/dryad/rusalka_test.go b/dryad/rusalka_test.go index 16f8a7f..e479c55 100644 --- a/dryad/rusalka_test.go +++ b/dryad/rusalka_test.go @@ -75,7 +75,9 @@ var _ = Describe("Rusalka", func() { It("should prepare", func() { u, err := user.Current() - Expect(err).ToNot(HaveOccurred()) + if err != nil { + Skip("failed to get user info") + } if u.Uid != "0" { Skip("must be run as root") } diff --git a/dryad/user_test.go b/dryad/user_test.go index 4f655fb..06bf17a 100644 --- a/dryad/user_test.go +++ b/dryad/user_test.go @@ -33,7 +33,9 @@ var _ = Describe("user management", func() { } u, err := user.Current() - Expect(err).ToNot(HaveOccurred()) + if err != nil { + Skip("failed to get user info") + } if u.Uid != "0" { Skip("must be run as root") } diff --git a/tunnels/tunnels_test.go b/tunnels/tunnels_test.go index 99af08f..8ecbcd5 100644 --- a/tunnels/tunnels_test.go +++ b/tunnels/tunnels_test.go @@ -25,9 +25,11 @@ import ( var _ = Describe("Tunnels", func() { invalidIP := net.IPv4(255, 8, 8, 8) + var localIP net.IP - listen := func(done chan struct{}, in, out string) *net.TCPAddr { + listen := func(done chan struct{}, in, out string) net.TCPAddr { addr := new(net.TCPAddr) + addr.IP = localIP ln, err := net.ListenTCP("tcp", addr) go func() { defer close(done) @@ -49,12 +51,19 @@ var _ = Describe("Tunnels", func() { } }() Expect(err).ToNot(HaveOccurred()) - return ln.Addr().(*net.TCPAddr) + return *ln.Addr().(*net.TCPAddr) } var t *Tunnel BeforeEach(func() { + IPs, err := net.LookupIP("localhost") + Expect(err).ToNot(HaveOccurred()) + if len(IPs) == 0 { + Skip("Could not resolve localhost") + } + localIP = IPs[0] + t = new(Tunnel) Expect(t).NotTo(BeNil()) }) @@ -62,10 +71,11 @@ var _ = Describe("Tunnels", func() { It("should make a connection", func() { done := make(chan struct{}) lAddr := listen(done, "", "") - err := t.create(nil, nil, lAddr.Port) + err := t.create(localIP, localIP, lAddr.Port) Expect(err).ToNot(HaveOccurred()) - conn, err := net.DialTCP("tcp", nil, lAddr) + lAddr.IP = localIP + conn, err := net.DialTCP("tcp", nil, &lAddr) Expect(err).ToNot(HaveOccurred()) defer conn.Close() @@ -78,7 +88,7 @@ var _ = Describe("Tunnels", func() { testIn := "input test string" testOut := "output test string" lAddr := listen(done, testIn, testOut) - err := t.create(nil, nil, lAddr.Port) + err := t.create(localIP, localIP, lAddr.Port) Expect(err).ToNot(HaveOccurred()) conn, err := net.DialTCP("tcp", nil, t.Addr().(*net.TCPAddr)) Expect(err).ToNot(HaveOccurred()) @@ -101,7 +111,7 @@ var _ = Describe("Tunnels", func() { }) It("should fail to connect to invalid address", func() { - err := t.create(nil, nil, 0) + err := t.create(localIP, localIP, 0) Expect(err).ToNot(HaveOccurred()) conn, err := net.DialTCP("tcp", nil, t.Addr().(*net.TCPAddr)) diff --git a/workers/worker_list_test.go b/workers/worker_list_test.go index 93d29a7..b3b0ddd 100644 --- a/workers/worker_list_test.go +++ b/workers/worker_list_test.go @@ -46,6 +46,12 @@ var _ = Describe("WorkerList", func() { } }) + getUUID := func() string { + u, err := uuid.NewV4() + Expect(err).ToNot(HaveOccurred()) + return u.String() + } + Describe("Register", func() { var registeredWorkers []string @@ -81,7 +87,7 @@ var _ = Describe("WorkerList", func() { getRandomCaps := func() Capabilities { return Capabilities{ - UUID: uuid.NewV4().String(), + UUID: getUUID(), } } @@ -161,12 +167,12 @@ var _ = Describe("WorkerList", func() { randomUUID := func() WorkerUUID { newUUID := worker for newUUID == worker { - newUUID = WorkerUUID(uuid.NewV4().String()) + newUUID = WorkerUUID(getUUID()) } return newUUID } registerWorker := func() WorkerUUID { - capsUUID := uuid.NewV4().String() + capsUUID := getUUID() err := wl.Register(Capabilities{UUID: capsUUID}) Expect(err).ToNot(HaveOccurred()) wl.mutex.RLock() @@ -520,7 +526,7 @@ var _ = Describe("WorkerList", func() { var refWorkerList []WorkerInfo registerAndSetGroups := func(groups Groups, caps Capabilities) WorkerInfo { - capsUUID := uuid.NewV4().String() + capsUUID := getUUID() caps[UUID] = capsUUID err := wl.Register(caps) Expect(err).ToNot(HaveOccurred()) @@ -920,7 +926,7 @@ var _ = Describe("WorkerList", func() { }) Describe("TakeBestMatchingWorker", func() { addWorker := func(groups Groups, caps Capabilities) *mapWorker { - capsUUID := uuid.NewV4().String() + capsUUID := getUUID() workerUUID := WorkerUUID(capsUUID) caps[UUID] = capsUUID diff --git a/workers/workers_test.go b/workers/workers_test.go index 0c0f902..3a49000 100644 --- a/workers/workers_test.go +++ b/workers/workers_test.go @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017 Samsung Electronics Co., Ltd All Rights Reserved + * Copyright (c) 2017-2018 Samsung Electronics Co., Ltd All Rights Reserved * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -33,9 +33,15 @@ var _ = Describe("WorkerList", func() { wl = workers.NewWorkerList() }) + getUUID := func() string { + u, err := uuid.NewV4() + Expect(err).ToNot(HaveOccurred()) + return u.String() + } + getRandomCaps := func() boruta.Capabilities { return map[string]string{ - workers.UUID: uuid.NewV4().String(), + workers.UUID: getUUID(), } } -- 2.7.4