Browse Source

Silence writing to stdout in fluxctl tests, so they are less noisy

Paul Bellamy 3 years ago
parent
commit
fa8afff549
No account linked to committer's email address

+ 14
- 13
cmd/fluxctl/check_release_cmd.go View File

@@ -72,16 +72,17 @@ func (opts *serviceCheckReleaseOpts) RunE(cmd *cobra.Command, args []string) err
72 72
 		if err != nil {
73 73
 			return err
74 74
 		}
75
-		_, err = os.Stdout.Write(buf)
75
+		_, err = cmd.OutOrStdout().Write(buf)
76 76
 		return err
77 77
 	}
78 78
 
79 79
 	var (
80
-		w    io.Writer = os.Stdout
80
+		w    io.Writer = cmd.OutOrStdout()
81 81
 		stop           = func() {}
82 82
 	)
83 83
 	if !opts.noTty && isatty.IsTerminal(os.Stdout.Fd()) {
84 84
 		liveWriter := uilive.New()
85
+		liveWriter.Out = cmd.OutOrStdout()
85 86
 		liveWriter.Start()
86 87
 		var stopOnce sync.Once
87 88
 		w, stop = liveWriter, func() { stopOnce.Do(liveWriter.Stop) }
@@ -109,9 +110,9 @@ func (opts *serviceCheckReleaseOpts) RunE(cmd *cobra.Command, args []string) err
109 110
 			if err, ok := errors.Cause(err).(*httperror.APIError); ok && err.IsUnavailable() {
110 111
 				if time.Since(lastSucceeded) > retryTimeout {
111 112
 					stop()
112
-					fmt.Fprintln(os.Stdout, "Giving up; you can try again with")
113
-					fmt.Fprintf(os.Stdout, "    fluxctl check-release -r %s\n", opts.releaseID)
114
-					fmt.Fprintln(os.Stdout)
113
+					fmt.Fprintln(cmd.OutOrStdout(), "Giving up; you can try again with")
114
+					fmt.Fprintf(cmd.OutOrStdout(), "    fluxctl check-release -r %s\n", opts.releaseID)
115
+					fmt.Fprintln(cmd.OutOrStdout())
115 116
 					break
116 117
 				}
117 118
 				retryCount++
@@ -160,22 +161,22 @@ func (opts *serviceCheckReleaseOpts) RunE(cmd *cobra.Command, args []string) err
160 161
 
161 162
 	spec := job.Params.(jobs.ReleaseJobParams)
162 163
 
163
-	fmt.Fprintf(os.Stdout, "\n")
164
+	fmt.Fprintf(cmd.OutOrStdout(), "\n")
164 165
 	if !job.Success {
165
-		fmt.Fprintf(os.Stdout, "Here's as far as we got:\n")
166
+		fmt.Fprintf(cmd.OutOrStdout(), "Here's as far as we got:\n")
166 167
 		for i, msg := range job.Log {
167
-			fmt.Fprintf(os.Stdout, " %d) %s\n", i+1, msg)
168
+			fmt.Fprintf(cmd.OutOrStdout(), " %d) %s\n", i+1, msg)
168 169
 		}
169 170
 	} else if spec.Kind == flux.ReleaseKindPlan {
170
-		fmt.Fprintf(os.Stdout, "Here's the plan:\n")
171
-		release.PrintResults(os.Stdout, job.Result.(flux.ReleaseResult), opts.verbose)
171
+		fmt.Fprintf(cmd.OutOrStdout(), "Here's the plan:\n")
172
+		release.PrintResults(cmd.OutOrStdout(), job.Result.(flux.ReleaseResult), opts.verbose)
172 173
 	} else {
173
-		fmt.Fprintf(os.Stdout, "Here's what happened:\n")
174
-		release.PrintResults(os.Stdout, job.Result.(flux.ReleaseResult), opts.verbose)
174
+		fmt.Fprintf(cmd.OutOrStdout(), "Here's what happened:\n")
175
+		release.PrintResults(cmd.OutOrStdout(), job.Result.(flux.ReleaseResult), opts.verbose)
175 176
 	}
176 177
 
177 178
 	if spec.Kind == flux.ReleaseKindExecute {
178
-		fmt.Fprintf(os.Stdout, "Took %s\n", job.Finished.Sub(job.Submitted))
179
+		fmt.Fprintf(cmd.OutOrStdout(), "Took %s\n", job.Finished.Sub(job.Submitted))
179 180
 	}
180 181
 
181 182
 	if job.Error != nil {

+ 5
- 1
cmd/fluxctl/check_release_cmd_test.go View File

@@ -1,11 +1,14 @@
1 1
 package main //+integration
2
+
2 3
 import (
4
+	"io/ioutil"
5
+	"testing"
6
+
3 7
 	"github.com/gorilla/mux"
4 8
 
5 9
 	"github.com/weaveworks/flux"
6 10
 	transport "github.com/weaveworks/flux/http"
7 11
 	"github.com/weaveworks/flux/jobs"
8
-	"testing"
9 12
 )
10 13
 
11 14
 func TestCheckReleaseCommand_CLIConversion(t *testing.T) {
@@ -65,6 +68,7 @@ func testCheckReleaseArgs(t *testing.T, args []string, shouldErr bool, errMsg st
65 68
 	client := newServiceCheckRelease(mockServiceOpts(svc))
66 69
 
67 70
 	cmd := client.Command()
71
+	cmd.SetOutput(ioutil.Discard)
68 72
 	cmd.SetArgs(args)
69 73
 	if err := cmd.Execute(); (err == nil) == shouldErr {
70 74
 		if errMsg != "" {

+ 3
- 3
cmd/fluxctl/format.go View File

@@ -3,13 +3,13 @@ package main
3 3
 import (
4 4
 	"bytes"
5 5
 	"fmt"
6
-	"os"
6
+	"io"
7 7
 	"strings"
8 8
 	"text/tabwriter"
9 9
 )
10 10
 
11
-func newTabwriter() *tabwriter.Writer {
12
-	return tabwriter.NewWriter(os.Stdout, 0, 2, 2, ' ', 0)
11
+func newTabwriter(w io.Writer) *tabwriter.Writer {
12
+	return tabwriter.NewWriter(w, 0, 2, 2, ' ', 0)
13 13
 }
14 14
 
15 15
 func makeExample(examples ...string) string {

+ 2
- 3
cmd/fluxctl/get_config_cmd.go View File

@@ -2,7 +2,6 @@ package main
2 2
 
3 3
 import (
4 4
 	"encoding/json"
5
-	"os"
6 5
 
7 6
 	"github.com/pkg/errors"
8 7
 	"github.com/spf13/cobra"
@@ -35,7 +34,7 @@ func (opts *getConfigOpts) Command() *cobra.Command {
35 34
 	return cmd
36 35
 }
37 36
 
38
-func (opts *getConfigOpts) RunE(_ *cobra.Command, args []string) error {
37
+func (opts *getConfigOpts) RunE(cmd *cobra.Command, args []string) error {
39 38
 	if len(args) > 0 {
40 39
 		return errorWantedNoArgs
41 40
 	}
@@ -62,6 +61,6 @@ func (opts *getConfigOpts) RunE(_ *cobra.Command, args []string) error {
62 61
 	if err != nil {
63 62
 		return errors.Wrap(err, "marshalling to output format "+opts.output)
64 63
 	}
65
-	os.Stdout.Write(bytes)
64
+	cmd.OutOrStdout().Write(bytes)
66 65
 	return nil
67 66
 }

+ 2
- 2
cmd/fluxctl/history_cmd.go View File

@@ -30,7 +30,7 @@ func (opts *serviceHistoryOpts) Command() *cobra.Command {
30 30
 	return cmd
31 31
 }
32 32
 
33
-func (opts *serviceHistoryOpts) RunE(_ *cobra.Command, args []string) error {
33
+func (opts *serviceHistoryOpts) RunE(cmd *cobra.Command, args []string) error {
34 34
 	if len(args) > 0 {
35 35
 		return errorWantedNoArgs
36 36
 	}
@@ -45,7 +45,7 @@ func (opts *serviceHistoryOpts) RunE(_ *cobra.Command, args []string) error {
45 45
 		return err
46 46
 	}
47 47
 
48
-	out := newTabwriter()
48
+	out := newTabwriter(cmd.OutOrStdout())
49 49
 
50 50
 	fmt.Fprintln(out, "TIME\tTYPE\tMESSAGE")
51 51
 	for _, event := range events {

+ 2
- 2
cmd/fluxctl/list_images_cmd.go View File

@@ -32,7 +32,7 @@ func (opts *serviceShowOpts) Command() *cobra.Command {
32 32
 	return cmd
33 33
 }
34 34
 
35
-func (opts *serviceShowOpts) RunE(_ *cobra.Command, args []string) error {
35
+func (opts *serviceShowOpts) RunE(cmd *cobra.Command, args []string) error {
36 36
 	if len(args) != 0 {
37 37
 		return errorWantedNoArgs
38 38
 	}
@@ -49,7 +49,7 @@ func (opts *serviceShowOpts) RunE(_ *cobra.Command, args []string) error {
49 49
 
50 50
 	sort.Sort(imageStatusByName(services))
51 51
 
52
-	out := newTabwriter()
52
+	out := newTabwriter(cmd.OutOrStdout())
53 53
 
54 54
 	fmt.Fprintln(out, "SERVICE\tCONTAINER\tIMAGE\tCREATED")
55 55
 	for _, service := range services {

+ 2
- 2
cmd/fluxctl/list_services_cmd.go View File

@@ -29,7 +29,7 @@ func (opts *serviceListOpts) Command() *cobra.Command {
29 29
 	return cmd
30 30
 }
31 31
 
32
-func (opts *serviceListOpts) RunE(_ *cobra.Command, args []string) error {
32
+func (opts *serviceListOpts) RunE(cmd *cobra.Command, args []string) error {
33 33
 	if len(args) != 0 {
34 34
 		return errorWantedNoArgs
35 35
 	}
@@ -41,7 +41,7 @@ func (opts *serviceListOpts) RunE(_ *cobra.Command, args []string) error {
41 41
 
42 42
 	sort.Sort(serviceStatusByName(services))
43 43
 
44
-	w := newTabwriter()
44
+	w := newTabwriter(cmd.OutOrStdout())
45 45
 	fmt.Fprintf(w, "SERVICE\tCONTAINER\tIMAGE\tRELEASE\tPOLICY\n")
46 46
 	for _, s := range services {
47 47
 		if len(s.Containers) > 0 {

+ 2
- 4
cmd/fluxctl/main.go View File

@@ -1,7 +1,6 @@
1 1
 package main
2 2
 
3 3
 import (
4
-	"io"
5 4
 	"os"
6 5
 
7 6
 	"github.com/pkg/errors"
@@ -9,10 +8,9 @@ import (
9 8
 	"github.com/weaveworks/flux"
10 9
 )
11 10
 
12
-func run(args []string, stderr io.Writer) int {
11
+func run(args []string) int {
13 12
 	rootCmd := newRoot().Command()
14 13
 	rootCmd.SetArgs(args)
15
-	rootCmd.SetOutput(stderr)
16 14
 	if cmd, err := rootCmd.ExecuteC(); err != nil {
17 15
 		err = errors.Cause(err)
18 16
 		switch err := err.(type) {
@@ -28,5 +26,5 @@ func run(args []string, stderr io.Writer) int {
28 26
 }
29 27
 
30 28
 func main() {
31
-	os.Exit(run(os.Args[1:], os.Stderr))
29
+	os.Exit(run(os.Args[1:]))
32 30
 }

+ 7
- 7
cmd/fluxctl/main_test.go View File

@@ -2,18 +2,18 @@
2 2
 package main
3 3
 
4 4
 import (
5
-	transport "github.com/weaveworks/flux/http"
6
-	"net/http"
7
-
8 5
 	"bytes"
9 6
 	"encoding/json"
10
-	"fmt"
11
-	"github.com/gorilla/mux"
12
-	"github.com/weaveworks/flux/http/client"
13 7
 	"io/ioutil"
8
+	"net/http"
14 9
 	"net/url"
15 10
 	"strings"
16 11
 	"testing"
12
+
13
+	"github.com/gorilla/mux"
14
+
15
+	transport "github.com/weaveworks/flux/http"
16
+	"github.com/weaveworks/flux/http/client"
17 17
 )
18 18
 
19 19
 func mockServiceOpts(trip *genericMockRoundTripper) *serviceOpts {
@@ -37,7 +37,6 @@ func (t *genericMockRoundTripper) RoundTrip(req *http.Request) (*http.Response,
37 37
 	var matched mux.RouteMatch
38 38
 	var b []byte
39 39
 	status := 404
40
-	fmt.Println(req.URL.String())
41 40
 	for k, v := range t.mockResponses {
42 41
 		if k.Match(req, &matched) {
43 42
 			queryParamsWithArrays := make(map[string]string, len(req.URL.Query()))
@@ -92,6 +91,7 @@ func testArgs(t *testing.T, args []string, shouldErr bool, errMsg string) *gener
92 91
 
93 92
 	// Run fluxctl release
94 93
 	cmd := releaseClient.Command()
94
+	cmd.SetOutput(ioutil.Discard)
95 95
 	cmd.SetArgs(args)
96 96
 	if err := cmd.Execute(); (err == nil) == shouldErr {
97 97
 		if errMsg != "" {

+ 7
- 8
cmd/fluxctl/release_cmd.go View File

@@ -2,7 +2,6 @@ package main
2 2
 
3 3
 import (
4 4
 	"fmt"
5
-	"os"
6 5
 	"os/user"
7 6
 
8 7
 	"github.com/spf13/cobra"
@@ -119,9 +118,9 @@ func (opts *serviceReleaseOpts) RunE(cmd *cobra.Command, args []string) error {
119 118
 	}
120 119
 
121 120
 	if opts.dryRun {
122
-		fmt.Fprintf(os.Stdout, "Submitting dry-run release job...\n")
121
+		fmt.Fprintf(cmd.OutOrStdout(), "Submitting dry-run release job...\n")
123 122
 	} else {
124
-		fmt.Fprintf(os.Stdout, "Submitting release job...\n")
123
+		fmt.Fprintf(cmd.OutOrStdout(), "Submitting release job...\n")
125 124
 	}
126 125
 
127 126
 	id, err := opts.API.PostRelease(noInstanceID, jobs.ReleaseJobParams{
@@ -140,12 +139,12 @@ func (opts *serviceReleaseOpts) RunE(cmd *cobra.Command, args []string) error {
140 139
 		return err
141 140
 	}
142 141
 
143
-	fmt.Fprintf(os.Stdout, "Release job submitted, ID %s\n", id)
142
+	fmt.Fprintf(cmd.OutOrStdout(), "Release job submitted, ID %s\n", id)
144 143
 	if opts.noFollow {
145
-		fmt.Fprintf(os.Stdout, "To check the status of this release job, run\n")
146
-		fmt.Fprintf(os.Stdout, "\n")
147
-		fmt.Fprintf(os.Stdout, "\tfluxctl check-release --release-id=%s\n", id)
148
-		fmt.Fprintf(os.Stdout, "\n")
144
+		fmt.Fprintf(cmd.OutOrStdout(), "To check the status of this release job, run\n")
145
+		fmt.Fprintf(cmd.OutOrStdout(), "\n")
146
+		fmt.Fprintf(cmd.OutOrStdout(), "\tfluxctl check-release --release-id=%s\n", id)
147
+		fmt.Fprintf(cmd.OutOrStdout(), "\n")
149 148
 		return nil
150 149
 	}
151 150
 

+ 4
- 1
cmd/fluxctl/release_cmd_test.go View File

@@ -1,10 +1,13 @@
1 1
 package main //+integration
2
+
2 3
 import (
4
+	"testing"
5
+
3 6
 	"github.com/gorilla/mux"
7
+
4 8
 	"github.com/weaveworks/flux"
5 9
 	transport "github.com/weaveworks/flux/http"
6 10
 	"github.com/weaveworks/flux/jobs"
7
-	"testing"
8 11
 )
9 12
 
10 13
 func TestReleaseCommand_CLIConversion(t *testing.T) {

+ 8
- 7
cmd/fluxctl/save_cmd.go View File

@@ -4,6 +4,7 @@ import (
4 4
 	"bufio"
5 5
 	"bytes"
6 6
 	"fmt"
7
+	"io"
7 8
 	"os"
8 9
 	"path/filepath"
9 10
 
@@ -82,7 +83,7 @@ func (opts *saveOpts) RunE(cmd *cobra.Command, args []string) error {
82 83
 		// e.g. .Spec and .Metadata.Annotations
83 84
 		filterObject(object)
84 85
 
85
-		if err := saveYAML(object, opts.path); err != nil {
86
+		if err := saveYAML(cmd.OutOrStdout(), object, opts.path); err != nil {
86 87
 			return errors.Wrap(err, "saving yaml object")
87 88
 		}
88 89
 	}
@@ -144,7 +145,7 @@ func deleteEmptyMapValues(i interface{}) bool {
144 145
 	return false
145 146
 }
146 147
 
147
-func outputFile(object saveObject, out string) (string, error) {
148
+func outputFile(stdout io.Writer, object saveObject, out string) (string, error) {
148 149
 	var path string
149 150
 	if object.Kind == "Namespace" {
150 151
 		path = fmt.Sprintf("%s-ns.yaml", object.Metadata.Name)
@@ -159,12 +160,12 @@ func outputFile(object saveObject, out string) (string, error) {
159 160
 	}
160 161
 
161 162
 	path = filepath.Join(out, path)
162
-	fmt.Printf("Saving %s '%s' to %s\n", object.Kind, object.Metadata.Name, path)
163
+	fmt.Fprintf(stdout, "Saving %s '%s' to %s\n", object.Kind, object.Metadata.Name, path)
163 164
 	return path, nil
164 165
 }
165 166
 
166 167
 // Save YAML to directory structure
167
-func saveYAML(object saveObject, out string) error {
168
+func saveYAML(stdout io.Writer, object saveObject, out string) error {
168 169
 	buf, err := yaml.Marshal(object)
169 170
 	if err != nil {
170 171
 		return errors.Wrap(err, "marshalling yaml")
@@ -172,13 +173,13 @@ func saveYAML(object saveObject, out string) error {
172 173
 
173 174
 	// to stdout
174 175
 	if out == "-" {
175
-		fmt.Fprintln(os.Stdout, "---")
176
-		fmt.Fprint(os.Stdout, string(buf))
176
+		fmt.Fprintln(stdout, "---")
177
+		fmt.Fprint(stdout, string(buf))
177 178
 		return nil
178 179
 	}
179 180
 
180 181
 	// to a directory
181
-	path, err := outputFile(object, out)
182
+	path, err := outputFile(stdout, object, out)
182 183
 	if err != nil {
183 184
 		return err
184 185
 	}

+ 2
- 3
cmd/fluxctl/status_cmd.go View File

@@ -2,7 +2,6 @@ package main
2 2
 
3 3
 import (
4 4
 	"encoding/json"
5
-	"os"
6 5
 
7 6
 	"github.com/pkg/errors"
8 7
 	"github.com/spf13/cobra"
@@ -31,7 +30,7 @@ func (opts *statusOpts) Command() *cobra.Command {
31 30
 	return cmd
32 31
 }
33 32
 
34
-func (opts *statusOpts) RunE(_ *cobra.Command, args []string) error {
33
+func (opts *statusOpts) RunE(cmd *cobra.Command, args []string) error {
35 34
 	if len(args) > 0 {
36 35
 		return errorWantedNoArgs
37 36
 	}
@@ -58,6 +57,6 @@ func (opts *statusOpts) RunE(_ *cobra.Command, args []string) error {
58 57
 	if err != nil {
59 58
 		return errors.Wrap(err, "marshalling to output format "+opts.output)
60 59
 	}
61
-	os.Stdout.Write(bytes)
60
+	cmd.OutOrStdout().Write(bytes)
62 61
 	return nil
63 62
 }

+ 2
- 2
cmd/fluxctl/version_cmd.go View File

@@ -12,11 +12,11 @@ func newVersionCommand() *cobra.Command {
12 12
 	return &cobra.Command{
13 13
 		Use:   "version",
14 14
 		Short: "Output the version of fluxctl",
15
-		RunE: func(_ *cobra.Command, args []string) error {
15
+		RunE: func(cmd *cobra.Command, args []string) error {
16 16
 			if version == "" {
17 17
 				version = "unversioned"
18 18
 			}
19
-			fmt.Println(version)
19
+			fmt.Fprintln(cmd.OutOrStdout(), version)
20 20
 			return nil
21 21
 		},
22 22
 	}

+ 0
- 1
registry/cache_test.go View File

@@ -27,7 +27,6 @@ func (s *stoppableMemcacheClient) Stop() {}
27 27
 
28 28
 // Setup sets up stuff for testing
29 29
 func Setup(t *testing.T) MemcacheClient {
30
-	fmt.Printf("Memcache IPs: %v\n", strings.Fields(*memcachedIPs))
31 30
 	mc := memcache.New(strings.Fields(*memcachedIPs)...)
32 31
 	if err := mc.FlushAll(); err != nil {
33 32
 		t.Fatal(err)

+ 0
- 3
release/context_test.go View File

@@ -1,11 +1,9 @@
1 1
 package release
2 2
 
3 3
 import (
4
-	"fmt"
5 4
 	"os"
6 5
 	"os/exec"
7 6
 	"path/filepath"
8
-	"strings"
9 7
 	"testing"
10 8
 
11 9
 	"github.com/weaveworks/flux/git"
@@ -81,7 +79,6 @@ func setupRepo(t *testing.T) (git.Repo, func()) {
81 79
 
82 80
 func execCommand(cmd string, args ...string) error {
83 81
 	c := exec.Command(cmd, args...)
84
-	fmt.Printf("exec: %s %s\n", cmd, strings.Join(args, " "))
85 82
 	c.Stderr = os.Stderr
86 83
 	c.Stdout = os.Stdout
87 84
 	return c.Run()

+ 0
- 2
release/releaser_test.go View File

@@ -1,7 +1,6 @@
1 1
 package release
2 2
 
3 3
 import (
4
-	"fmt"
5 4
 	"os"
6 5
 	"reflect"
7 6
 	"testing"
@@ -399,7 +398,6 @@ func testRelease(t *testing.T, releaser *Releaser, name string, spec flux.Releas
399 398
 				ReleaseSpec: spec,
400 399
 			},
401 400
 		}, func(f string, a ...interface{}) {
402
-			fmt.Printf(f+"\n", a...)
403 401
 		}, func(r flux.ReleaseResult) {
404 402
 			if r == nil {
405 403
 				t.Errorf("%s - result update called with nil value", name)

Loading…
Cancel
Save