MCPcopy Index your code
hub / github.com/cortexproject/cortex / testSingle

Function testSingle

cmd/cortex/main_test.go:96–154  ·  view source on GitHub ↗
(t *testing.T, arguments []string, yaml string, stdoutMessage, stderrMessage, stdoutExcluded, stderrExcluded []byte)

Source from the content-addressed store, hash-verified

94}
95
96func testSingle(t *testing.T, arguments []string, yaml string, stdoutMessage, stderrMessage, stdoutExcluded, stderrExcluded []byte) {
97 t.Helper()
98 oldArgs, oldStdout, oldStderr, oldTestMode := os.Args, os.Stdout, os.Stderr, testMode
99 restored := false
100 restoreIfNeeded := func() {
101 if restored {
102 return
103 }
104 os.Stdout = oldStdout
105 os.Stderr = oldStderr
106 os.Args = oldArgs
107 testMode = oldTestMode
108 restored = true
109 }
110 defer restoreIfNeeded()
111
112 if yaml != "" {
113 tempFile, err := os.CreateTemp("", "test")
114 require.NoError(t, err)
115
116 defer func() {
117 require.NoError(t, tempFile.Close())
118 require.NoError(t, os.Remove(tempFile.Name()))
119 }()
120
121 _, err = tempFile.WriteString(yaml)
122 require.NoError(t, err)
123
124 arguments = append(arguments, "-"+configFileOption, tempFile.Name())
125 }
126
127 arguments = append([]string{"./cortex"}, arguments...)
128
129 testMode = true
130 os.Args = arguments
131 co := captureOutput(t)
132
133 // reset default flags
134 flag.CommandLine = flag.NewFlagSet(arguments[0], flag.ExitOnError)
135
136 main()
137
138 stdout, stderr := co.Done()
139
140 // Restore stdout and stderr before reporting errors to make them visible.
141 restoreIfNeeded()
142 if !bytes.Contains(stdout, stdoutMessage) {
143 t.Errorf("Expected on stdout: %q, stdout: %s\n", stdoutMessage, stdout)
144 }
145 if !bytes.Contains(stderr, stderrMessage) {
146 t.Errorf("Expected on stderr: %q, stderr: %s\n", stderrMessage, stderr)
147 }
148 if len(stdoutExcluded) > 0 && bytes.Contains(stdout, stdoutExcluded) {
149 t.Errorf("Unexpected output on stdout: %q, stdout: %s\n", stdoutExcluded, stdout)
150 }
151 if len(stderrExcluded) > 0 && bytes.Contains(stderr, stderrExcluded) {
152 t.Errorf("Unexpected output on stderr: %q, stderr: %s\n", stderrExcluded, stderr)
153 }

Callers 1

TestFlagParsingFunction · 0.85

Calls 6

captureOutputFunction · 0.85
WriteStringMethod · 0.80
DoneMethod · 0.80
mainFunction · 0.70
CloseMethod · 0.65
NameMethod · 0.65

Tested by

no test coverage detected