Browse code

integration-cli: DockerSwarmSuite: rm redundant Fprintf, handle errors

Also fix some unhandled errors.

integration-cli/docker_cli_swarm_test.go:697:19: printf: non-constant format string in call to fmt.Fprintf (govet)
fmt.Fprintf(w, `{"Error":"failed to add veth pair: `+err.Error()+`"}`)
^
integration-cli/docker_cli_swarm_test.go:731:18: printf: non-constant format string in call to fmt.Fprintf (govet)
fmt.Fprintf(w, `{"LocalDefaultAddressSpace":"`+lAS+`", "GlobalDefaultAddressSpace": "`+gAS+`"}`)
^
integration-cli/docker_cli_swarm_test.go:742:19: printf: non-constant format string in call to fmt.Fprintf (govet)
fmt.Fprintf(w, `{"Error":"Unknown address space in pool request: `+poolRequest.AddressSpace+`"}`)
^
integration-cli/docker_cli_swarm_test.go:746:19: printf: non-constant format string in call to fmt.Fprintf (govet)
fmt.Fprintf(w, `{"PoolID":"`+poolID+`", "Pool":"`+pool+`"}`)
^
integration-cli/docker_cli_swarm_test.go:763:19: printf: non-constant format string in call to fmt.Fprintf (govet)
fmt.Fprintf(w, `{"Address":"`+gw+`"}`)
^

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>

Sebastiaan van Stijn authored on 2024/08/21 22:26:29
Showing 1 changed files
... ...
@@ -386,7 +386,8 @@ func (s *DockerSwarmSuite) TestSwarmContainerAttachByNetworkId(c *testing.T) {
386 386
 	out, err = d.Cmd("run", "-d", "--net", networkID, "busybox", "top")
387 387
 	assert.NilError(c, err, out)
388 388
 	cID := strings.TrimSpace(out)
389
-	d.WaitRun(cID)
389
+	err = d.WaitRun(cID)
390
+	assert.NilError(c, err)
390 391
 
391 392
 	out, err = d.Cmd("rm", "-f", cID)
392 393
 	assert.NilError(c, err, out)
... ...
@@ -640,16 +641,18 @@ const (
640 640
 	globalIPAMPlugin    = "global-ipam-plugin"
641 641
 )
642 642
 
643
-func setupRemoteGlobalNetworkPlugin(c *testing.T, mux *http.ServeMux, url, netDrv, ipamDrv string) {
643
+func setupRemoteGlobalNetworkPlugin(t *testing.T, mux *http.ServeMux, url, netDrv, ipamDrv string) {
644 644
 	mux.HandleFunc("/Plugin.Activate", func(w http.ResponseWriter, r *http.Request) {
645 645
 		w.Header().Set("Content-Type", plugins.VersionMimetype)
646
-		fmt.Fprintf(w, `{"Implements": ["%s", "%s"]}`, driverapi.NetworkPluginEndpointType, ipamapi.PluginEndpointType)
646
+		_, err := fmt.Fprintf(w, `{"Implements": ["%s", "%s"]}`, driverapi.NetworkPluginEndpointType, ipamapi.PluginEndpointType)
647
+		assert.NilError(t, err)
647 648
 	})
648 649
 
649 650
 	// Network driver implementation
650 651
 	mux.HandleFunc(fmt.Sprintf("/%s.GetCapabilities", driverapi.NetworkPluginEndpointType), func(w http.ResponseWriter, r *http.Request) {
651 652
 		w.Header().Set("Content-Type", plugins.VersionMimetype)
652
-		fmt.Fprintf(w, `{"Scope":"global"}`)
653
+		_, err := fmt.Fprint(w, `{"Scope":"global"}`)
654
+		assert.NilError(t, err)
653 655
 	})
654 656
 
655 657
 	mux.HandleFunc(fmt.Sprintf("/%s.AllocateNetwork", driverapi.NetworkPluginEndpointType), func(w http.ResponseWriter, r *http.Request) {
... ...
@@ -659,12 +662,14 @@ func setupRemoteGlobalNetworkPlugin(c *testing.T, mux *http.ServeMux, url, netDr
659 659
 			return
660 660
 		}
661 661
 		w.Header().Set("Content-Type", plugins.VersionMimetype)
662
-		fmt.Fprintf(w, "null")
662
+		_, err = fmt.Fprint(w, "null")
663
+		assert.NilError(t, err)
663 664
 	})
664 665
 
665 666
 	mux.HandleFunc(fmt.Sprintf("/%s.FreeNetwork", driverapi.NetworkPluginEndpointType), func(w http.ResponseWriter, r *http.Request) {
666 667
 		w.Header().Set("Content-Type", plugins.VersionMimetype)
667
-		fmt.Fprintf(w, "null")
668
+		_, err := fmt.Fprint(w, "null")
669
+		assert.NilError(t, err)
668 670
 	})
669 671
 
670 672
 	mux.HandleFunc(fmt.Sprintf("/%s.CreateNetwork", driverapi.NetworkPluginEndpointType), func(w http.ResponseWriter, r *http.Request) {
... ...
@@ -674,17 +679,20 @@ func setupRemoteGlobalNetworkPlugin(c *testing.T, mux *http.ServeMux, url, netDr
674 674
 			return
675 675
 		}
676 676
 		w.Header().Set("Content-Type", plugins.VersionMimetype)
677
-		fmt.Fprintf(w, "null")
677
+		_, err = fmt.Fprint(w, "null")
678
+		assert.NilError(t, err)
678 679
 	})
679 680
 
680 681
 	mux.HandleFunc(fmt.Sprintf("/%s.DeleteNetwork", driverapi.NetworkPluginEndpointType), func(w http.ResponseWriter, r *http.Request) {
681 682
 		w.Header().Set("Content-Type", plugins.VersionMimetype)
682
-		fmt.Fprintf(w, "null")
683
+		_, err := fmt.Fprint(w, "null")
684
+		assert.NilError(t, err)
683 685
 	})
684 686
 
685 687
 	mux.HandleFunc(fmt.Sprintf("/%s.CreateEndpoint", driverapi.NetworkPluginEndpointType), func(w http.ResponseWriter, r *http.Request) {
686 688
 		w.Header().Set("Content-Type", plugins.VersionMimetype)
687
-		fmt.Fprintf(w, `{"Interface":{"MacAddress":"a0:b1:c2:d3:e4:f5"}}`)
689
+		_, err := fmt.Fprint(w, `{"Interface":{"MacAddress":"a0:b1:c2:d3:e4:f5"}}`)
690
+		assert.NilError(t, err)
688 691
 	})
689 692
 
690 693
 	mux.HandleFunc(fmt.Sprintf("/%s.Join", driverapi.NetworkPluginEndpointType), func(w http.ResponseWriter, r *http.Request) {
... ...
@@ -694,23 +702,28 @@ func setupRemoteGlobalNetworkPlugin(c *testing.T, mux *http.ServeMux, url, netDr
694 694
 			LinkAttrs: netlink.LinkAttrs{Name: "randomIfName", TxQLen: 0}, PeerName: "cnt0",
695 695
 		}
696 696
 		if err := netlink.LinkAdd(veth); err != nil {
697
-			fmt.Fprintf(w, `{"Error":"failed to add veth pair: `+err.Error()+`"}`)
697
+			_, err = fmt.Fprint(w, `{"Error":"failed to add veth pair: `+err.Error()+`"}`)
698
+			assert.NilError(t, err)
698 699
 		} else {
699
-			fmt.Fprintf(w, `{"InterfaceName":{ "SrcName":"cnt0", "DstPrefix":"veth"}}`)
700
+			_, err = fmt.Fprint(w, `{"InterfaceName":{ "SrcName":"cnt0", "DstPrefix":"veth"}}`)
701
+			assert.NilError(t, err)
700 702
 		}
701 703
 	})
702 704
 
703 705
 	mux.HandleFunc(fmt.Sprintf("/%s.Leave", driverapi.NetworkPluginEndpointType), func(w http.ResponseWriter, r *http.Request) {
704 706
 		w.Header().Set("Content-Type", plugins.VersionMimetype)
705
-		fmt.Fprintf(w, "null")
707
+		_, err := fmt.Fprint(w, "null")
708
+		assert.NilError(t, err)
706 709
 	})
707 710
 
708 711
 	mux.HandleFunc(fmt.Sprintf("/%s.DeleteEndpoint", driverapi.NetworkPluginEndpointType), func(w http.ResponseWriter, r *http.Request) {
709 712
 		w.Header().Set("Content-Type", plugins.VersionMimetype)
710 713
 		if link, err := netlink.LinkByName("cnt0"); err == nil {
711
-			netlink.LinkDel(link)
714
+			err := netlink.LinkDel(link)
715
+			assert.NilError(t, err)
712 716
 		}
713
-		fmt.Fprintf(w, "null")
717
+		_, err := fmt.Fprint(w, "null")
718
+		assert.NilError(t, err)
714 719
 	})
715 720
 
716 721
 	// IPAM Driver implementation
... ...
@@ -719,16 +732,19 @@ func setupRemoteGlobalNetworkPlugin(c *testing.T, mux *http.ServeMux, url, netDr
719 719
 		poolReleaseReq    remoteipam.ReleasePoolRequest
720 720
 		addressRequest    remoteipam.RequestAddressRequest
721 721
 		addressReleaseReq remoteipam.ReleaseAddressRequest
722
-		lAS               = "localAS"
723
-		gAS               = "globalAS"
724
-		pool              = "172.28.0.0/16"
725
-		poolID            = lAS + "/" + pool
726
-		gw                = "172.28.255.254/16"
722
+	)
723
+	const (
724
+		lAS    = "localAS"
725
+		gAS    = "globalAS"
726
+		pool   = "172.28.0.0/16"
727
+		poolID = lAS + "/" + pool
728
+		gw     = "172.28.255.254/16"
727 729
 	)
728 730
 
729 731
 	mux.HandleFunc(fmt.Sprintf("/%s.GetDefaultAddressSpaces", ipamapi.PluginEndpointType), func(w http.ResponseWriter, r *http.Request) {
730 732
 		w.Header().Set("Content-Type", plugins.VersionMimetype)
731
-		fmt.Fprintf(w, `{"LocalDefaultAddressSpace":"`+lAS+`", "GlobalDefaultAddressSpace": "`+gAS+`"}`)
733
+		_, err := fmt.Fprint(w, `{"LocalDefaultAddressSpace":"`+lAS+`", "GlobalDefaultAddressSpace": "`+gAS+`"}`)
734
+		assert.NilError(t, err)
732 735
 	})
733 736
 
734 737
 	mux.HandleFunc(fmt.Sprintf("/%s.RequestPool", ipamapi.PluginEndpointType), func(w http.ResponseWriter, r *http.Request) {
... ...
@@ -739,11 +755,14 @@ func setupRemoteGlobalNetworkPlugin(c *testing.T, mux *http.ServeMux, url, netDr
739 739
 		}
740 740
 		w.Header().Set("Content-Type", plugins.VersionMimetype)
741 741
 		if poolRequest.AddressSpace != lAS && poolRequest.AddressSpace != gAS {
742
-			fmt.Fprintf(w, `{"Error":"Unknown address space in pool request: `+poolRequest.AddressSpace+`"}`)
742
+			_, err := fmt.Fprint(w, `{"Error":"Unknown address space in pool request: `+poolRequest.AddressSpace+`"}`)
743
+			assert.NilError(t, err)
743 744
 		} else if poolRequest.Pool != "" && poolRequest.Pool != pool {
744
-			fmt.Fprintf(w, `{"Error":"Cannot handle explicit pool requests yet"}`)
745
+			_, err := fmt.Fprint(w, `{"Error":"Cannot handle explicit pool requests yet"}`)
746
+			assert.NilError(t, err)
745 747
 		} else {
746
-			fmt.Fprintf(w, `{"PoolID":"`+poolID+`", "Pool":"`+pool+`"}`)
748
+			_, err := fmt.Fprint(w, `{"PoolID":"`+poolID+`", "Pool":"`+pool+`"}`)
749
+			assert.NilError(t, err)
747 750
 		}
748 751
 	})
749 752
 
... ...
@@ -756,11 +775,14 @@ func setupRemoteGlobalNetworkPlugin(c *testing.T, mux *http.ServeMux, url, netDr
756 756
 		w.Header().Set("Content-Type", plugins.VersionMimetype)
757 757
 		// make sure libnetwork is now querying on the expected pool id
758 758
 		if addressRequest.PoolID != poolID {
759
-			fmt.Fprintf(w, `{"Error":"unknown pool id"}`)
759
+			_, err := fmt.Fprint(w, `{"Error":"unknown pool id"}`)
760
+			assert.NilError(t, err)
760 761
 		} else if addressRequest.Address != "" {
761
-			fmt.Fprintf(w, `{"Error":"Cannot handle explicit address requests yet"}`)
762
+			_, err := fmt.Fprint(w, `{"Error":"Cannot handle explicit address requests yet"}`)
763
+			assert.NilError(t, err)
762 764
 		} else {
763
-			fmt.Fprintf(w, `{"Address":"`+gw+`"}`)
765
+			_, err := fmt.Fprint(w, `{"Address":"`+gw+`"}`)
766
+			assert.NilError(t, err)
764 767
 		}
765 768
 	})
766 769
 
... ...
@@ -773,11 +795,14 @@ func setupRemoteGlobalNetworkPlugin(c *testing.T, mux *http.ServeMux, url, netDr
773 773
 		w.Header().Set("Content-Type", plugins.VersionMimetype)
774 774
 		// make sure libnetwork is now asking to release the expected address from the expected poolid
775 775
 		if addressRequest.PoolID != poolID {
776
-			fmt.Fprintf(w, `{"Error":"unknown pool id"}`)
776
+			_, err := fmt.Fprint(w, `{"Error":"unknown pool id"}`)
777
+			assert.NilError(t, err)
777 778
 		} else if addressReleaseReq.Address != gw {
778
-			fmt.Fprintf(w, `{"Error":"unknown address"}`)
779
+			_, err := fmt.Fprint(w, `{"Error":"unknown address"}`)
780
+			assert.NilError(t, err)
779 781
 		} else {
780
-			fmt.Fprintf(w, "null")
782
+			_, err := fmt.Fprint(w, "null")
783
+			assert.NilError(t, err)
781 784
 		}
782 785
 	})
783 786
 
... ...
@@ -790,22 +815,24 @@ func setupRemoteGlobalNetworkPlugin(c *testing.T, mux *http.ServeMux, url, netDr
790 790
 		w.Header().Set("Content-Type", plugins.VersionMimetype)
791 791
 		// make sure libnetwork is now asking to release the expected poolid
792 792
 		if addressRequest.PoolID != poolID {
793
-			fmt.Fprintf(w, `{"Error":"unknown pool id"}`)
793
+			_, err := fmt.Fprint(w, `{"Error":"unknown pool id"}`)
794
+			assert.NilError(t, err)
794 795
 		} else {
795
-			fmt.Fprintf(w, "null")
796
+			_, err := fmt.Fprint(w, "null")
797
+			assert.NilError(t, err)
796 798
 		}
797 799
 	})
798 800
 
799 801
 	err := os.MkdirAll("/etc/docker/plugins", 0o755)
800
-	assert.NilError(c, err)
802
+	assert.NilError(t, err)
801 803
 
802 804
 	fileName := fmt.Sprintf("/etc/docker/plugins/%s.spec", netDrv)
803 805
 	err = os.WriteFile(fileName, []byte(url), 0o644)
804
-	assert.NilError(c, err)
806
+	assert.NilError(t, err)
805 807
 
806 808
 	ipamFileName := fmt.Sprintf("/etc/docker/plugins/%s.spec", ipamDrv)
807 809
 	err = os.WriteFile(ipamFileName, []byte(url), 0o644)
808
-	assert.NilError(c, err)
810
+	assert.NilError(t, err)
809 811
 }
810 812
 
811 813
 func (s *DockerSwarmSuite) TestSwarmNetworkPlugin(c *testing.T) {