fix staticcheck issues

This commit is contained in:
张祖建 2021-09-10 10:30:28 +08:00 committed by Vish (Ishaya) Abrams
parent 6757be61c4
commit b8aac10bba
16 changed files with 32 additions and 35 deletions

View File

@ -62,14 +62,14 @@ func TestBridgeVlan(t *testing.T) {
if vInfo, ok := vlanMap[int32(bridge.Index)]; !ok {
t.Fatal("vlanMap should include foo port vlan info")
} else {
if "[{Flags:6 Vid:1}]" != fmt.Sprintf("%v", vInfo) {
if fmt.Sprintf("%v", vInfo) != "[{Flags:6 Vid:1}]" {
t.Fatalf("unexpected result %v", vInfo)
}
}
if vInfo, ok := vlanMap[int32(dummy.Index)]; !ok {
t.Fatal("vlanMap should include dum1 port vlan info")
} else {
if "[{Flags:4 Vid:1} {Flags:0 Vid:2} {Flags:6 Vid:3}]" != fmt.Sprintf("%v", vInfo) {
if fmt.Sprintf("%v", vInfo) != "[{Flags:4 Vid:1} {Flags:0 Vid:2} {Flags:6 Vid:3}]" {
t.Fatalf("unexpected result %v", vInfo)
}
}

View File

@ -191,9 +191,9 @@ func classPayload(req *nl.NetlinkRequest, class Class) error {
opt.Fsc.Set(fm1/8, fd, fm2/8)
um1, ud, um2 := hfsc.Usc.Attrs()
opt.Usc.Set(um1/8, ud, um2/8)
nl.NewRtAttrChild(options, nl.TCA_HFSC_RSC, nl.SerializeHfscCurve(&opt.Rsc))
nl.NewRtAttrChild(options, nl.TCA_HFSC_FSC, nl.SerializeHfscCurve(&opt.Fsc))
nl.NewRtAttrChild(options, nl.TCA_HFSC_USC, nl.SerializeHfscCurve(&opt.Usc))
options.AddRtAttr(nl.TCA_HFSC_RSC, nl.SerializeHfscCurve(&opt.Rsc))
options.AddRtAttr(nl.TCA_HFSC_FSC, nl.SerializeHfscCurve(&opt.Fsc))
options.AddRtAttr(nl.TCA_HFSC_USC, nl.SerializeHfscCurve(&opt.Usc))
}
req.AddData(options)
return nil

View File

@ -617,7 +617,7 @@ func TestClassHfsc(t *testing.T) {
}
// Check the amount of qdiscs
qdiscs, err = SafeQdiscList(link)
qdiscs, _ = SafeQdiscList(link)
if len(qdiscs) != 3 {
t.Fatal("Failed to add qdisc")
}
@ -641,7 +641,7 @@ func TestClassHfsc(t *testing.T) {
t.Fatal("Failed to delete classes")
}
// Check qdisc
qdiscs, err = SafeQdiscList(link)
qdiscs, _ = SafeQdiscList(link)
if len(qdiscs) != 2 {
t.Fatal("Failed to delete qdisc")
}

View File

@ -493,11 +493,11 @@ func (h *Handle) DevlinkPortFnSet(Bus string, Device string, PortIndex uint32, F
fnAttr := nl.NewRtAttr(nl.DEVLINK_ATTR_PORT_FUNCTION|unix.NLA_F_NESTED, nil)
if FnAttrs.HwAddrValid == true {
if FnAttrs.HwAddrValid {
fnAttr.AddRtAttr(nl.DEVLINK_PORT_FUNCTION_ATTR_HW_ADDR, []byte(FnAttrs.FnAttrs.HwAddr))
}
if FnAttrs.StateValid == true {
if FnAttrs.StateValid {
fnAttr.AddRtAttr(nl.DEVLINK_PORT_FN_ATTR_STATE, nl.Uint8Attr(FnAttrs.FnAttrs.State))
}
req.AddData(fnAttr)

View File

@ -328,7 +328,7 @@ func runParallelTests(t *testing.T, thread int) {
if err != nil {
t.Fatal(err)
}
l, err = handle1.LinkByName(ifName)
_, err = handle1.LinkByName(ifName)
if err != nil {
t.Fatal(err)
}

View File

@ -55,8 +55,6 @@ const (
VF_LINK_STATE_DISABLE uint32 = 2
)
var lookupByDump = false
var macvlanModes = [...]uint32{
0,
nl.MACVLAN_MODE_PRIVATE,
@ -591,13 +589,13 @@ func (h *Handle) LinkSetVfVlanQos(link Link, vf, vlan, qos int) error {
req.AddData(msg)
data := nl.NewRtAttr(unix.IFLA_VFINFO_LIST, nil)
info := nl.NewRtAttrChild(data, nl.IFLA_VF_INFO, nil)
info := data.AddRtAttr(nl.IFLA_VF_INFO, nil)
vfmsg := nl.VfVlan{
Vf: uint32(vf),
Vlan: uint32(vlan),
Qos: uint32(qos),
}
nl.NewRtAttrChild(info, nl.IFLA_VF_VLAN, vfmsg.Serialize())
info.AddRtAttr(nl.IFLA_VF_VLAN, vfmsg.Serialize())
req.AddData(data)
_, err := req.Execute(unix.NETLINK_ROUTE, 0)

View File

@ -25,7 +25,7 @@ const (
)
func testLinkAddDel(t *testing.T, link Link) {
links, err := LinkList()
_, err := LinkList()
if err != nil {
t.Fatal(err)
}
@ -215,7 +215,7 @@ func testLinkAddDel(t *testing.T, link Link) {
if bond.AdUserPortKey != other.AdUserPortKey {
t.Fatalf("Got unexpected AdUserPortKey: %d, expected: %d", other.AdUserPortKey, bond.AdUserPortKey)
}
if bytes.Compare(bond.AdActorSystem, other.AdActorSystem) != 0 {
if !bytes.Equal(bond.AdActorSystem, other.AdActorSystem) {
t.Fatalf("Got unexpected AdActorSystem: %d, expected: %d", other.AdActorSystem, bond.AdActorSystem)
}
case "balance-tlb":
@ -299,7 +299,7 @@ func testLinkAddDel(t *testing.T, link Link) {
t.Fatal(err)
}
links, err = LinkList()
links, err := LinkList()
if err != nil {
t.Fatal(err)
}

View File

@ -107,10 +107,10 @@ func setUpNetlinkTestWithLoopback(t *testing.T) tearDownNetlinkTest {
func setUpF(t *testing.T, path, value string) {
file, err := os.Create(path)
defer file.Close()
if err != nil {
t.Fatalf("Failed to open %s: %s", path, err)
}
defer file.Close()
file.WriteString(value)
}

View File

@ -87,7 +87,7 @@ func (h *Handle) getNetNsId(attrType int, val uint32) (int, error) {
rtgen := nl.NewRtGenMsg()
req.AddData(rtgen)
b := make([]byte, 4, 4)
b := make([]byte, 4)
native.PutUint32(b, val)
attr := nl.NewRtAttr(attrType, b)
req.AddData(attr)
@ -126,12 +126,12 @@ func (h *Handle) setNetNsId(attrType int, val uint32, newnsid uint32) error {
rtgen := nl.NewRtGenMsg()
req.AddData(rtgen)
b := make([]byte, 4, 4)
b := make([]byte, 4)
native.PutUint32(b, val)
attr := nl.NewRtAttr(attrType, b)
req.AddData(attr)
b1 := make([]byte, 4, 4)
b1 := make([]byte, 4)
native.PutUint32(b1, newnsid)
attr1 := nl.NewRtAttr(NETNSA_NSID, b1)
req.AddData(attr1)

View File

@ -33,7 +33,7 @@ func TestNetNsIdByFd(t *testing.T) {
CheckErrorFail(t, err)
// Get the ID back, make sure it matches
haveID, err := h.GetNetNsIdByFd(int(ns))
haveID, _ := h.GetNetNsIdByFd(int(ns))
if haveID != wantID {
t.Errorf("GetNetNsIdByFd returned %d, want %d", haveID, wantID)
}
@ -72,7 +72,7 @@ func TestNetNsIdByPid(t *testing.T) {
CheckErrorFail(t, err)
//Get the ID and see if it worked
haveID, err := h.GetNetNsIdByPid(syscall.Gettid())
haveID, _ := h.GetNetNsIdByPid(syscall.Gettid())
if haveID != wantID {
t.Errorf("GetNetNsIdByPid returned %d, want %d", haveID, wantID)
}

View File

@ -23,7 +23,7 @@ func (s1 *IPv6SrHdr) Equal(s2 IPv6SrHdr) bool {
return false
}
for i := range s1.Segments {
if s1.Segments[i].Equal(s2.Segments[i]) != true {
if !s1.Segments[i].Equal(s2.Segments[i]) {
return false
}
}
@ -89,7 +89,7 @@ func DecodeSEG6Encap(buf []byte) (int, []net.IP, error) {
}
buf = buf[12:]
if len(buf)%16 != 0 {
err := fmt.Errorf("DecodeSEG6Encap: error parsing Segment List (buf len: %d)\n", len(buf))
err := fmt.Errorf("DecodeSEG6Encap: error parsing Segment List (buf len: %d)", len(buf))
return mode, nil, err
}
for len(buf) > 0 {

View File

@ -1,6 +1,6 @@
package nl
// syscall package lack of rule atributes type.
// syscall package lack of rule attributes type.
// Thus there are defined below
const (
FRA_UNSPEC = iota

View File

@ -85,9 +85,9 @@ func TestRdmaSystemSetNetnsMode(t *testing.T) {
}
// Flip the mode from current mode
if mode == "exclusive" {
err = RdmaSystemSetNetnsMode("shared")
RdmaSystemSetNetnsMode("shared")
} else {
err = RdmaSystemSetNetnsMode("exclusive")
RdmaSystemSetNetnsMode("exclusive")
}
newMode, err = RdmaSystemGetNetnsMode()
if err != nil {

View File

@ -250,7 +250,7 @@ func (e *SEG6Encap) String() string {
segs := make([]string, 0, len(e.Segments))
// append segment backwards (from n to 0) since seg#0 is the last segment.
for i := len(e.Segments); i > 0; i-- {
segs = append(segs, fmt.Sprintf("%s", e.Segments[i-1]))
segs = append(segs, e.Segments[i-1].String())
}
str := fmt.Sprintf("mode %s segs %d [ %s ]", nl.SEG6EncapModeString(e.Mode),
len(e.Segments), strings.Join(segs, " "))
@ -420,7 +420,7 @@ func (e *SEG6LocalEncap) String() string {
segs := make([]string, 0, len(e.Segments))
//append segment backwards (from n to 0) since seg#0 is the last segment.
for i := len(e.Segments); i > 0; i-- {
segs = append(segs, fmt.Sprintf("%s", e.Segments[i-1]))
segs = append(segs, e.Segments[i-1].String())
}
strs = append(strs, fmt.Sprintf("segs %d [ %s ]", len(e.Segments), strings.Join(segs, " ")))
}
@ -530,7 +530,7 @@ func (e *BpfEncap) Decode(buf []byte) error {
case nl.LWT_BPF_PROG_FD:
bpfO.progFd = int(native.Uint32(parsedAttr.Value))
case nl.LWT_BPF_PROG_NAME:
bpfO.progName = fmt.Sprintf("%s", parsedAttr.Value)
bpfO.progName = string(parsedAttr.Value)
default:
return fmt.Errorf("lwt bpf decode: received unknown attribute: type: %d, len: %d", parsedAttr.Attr.Type, parsedAttr.Attr.Len)
}
@ -599,7 +599,7 @@ func (e *BpfEncap) Equal(x Encap) bool {
if e.headroom != o.headroom {
return false
}
for i, _ := range o.progs {
for i := range o.progs {
if o.progs[i] != e.progs[i] {
return false
}

View File

@ -1368,8 +1368,7 @@ func TestSEG6LocalRoute6AddDel(t *testing.T) {
t.Fatal(err)
}
// Confirm route is deleted.
routesFound, err = RouteGet(dst1.IP)
if err == nil {
if _, err = RouteGet(dst1.IP); err == nil {
t.Fatal("SEG6Local route still exists.")
}

View File

@ -111,7 +111,7 @@ func (h *Handle) xfrmStateAddOrUpdate(state *XfrmState, nlProto int) error {
// A state with spi 0 can't be deleted so don't allow it to be set
if state.Spi == 0 {
return fmt.Errorf("Spi must be set when adding xfrm state.")
return fmt.Errorf("Spi must be set when adding xfrm state")
}
req := h.newNetlinkRequest(nlProto, unix.NLM_F_CREATE|unix.NLM_F_EXCL|unix.NLM_F_ACK)