tests: add runComposerCLIPlainText(). Refs #356

see this comment from @teg:
https://github.com/osbuild/osbuild-composer/issues/356#issuecomment-630766947

Most of the calls within these tests should be converted to
use the plain text version. However some functions need the
JSON b/c they parse it and return a response, e.g. startCompose(),
getComposeStatus().
This commit is contained in:
Alexander Todorov 2020-05-21 08:19:13 -04:00 committed by Alexander Todorov
parent eca999be4a
commit d8de74a7d1

View file

@ -214,8 +214,7 @@ func deleteBlueprint(t *testing.T, bp *blueprint.Blueprint) {
require.Truef(t, reply.Status, "Unexpected status %v", reply.Status)
}
func runComposerCLI(t *testing.T, quiet bool, command ...string) json.RawMessage {
command = append([]string{"--json"}, command...)
func runComposerCLIPlainText(t *testing.T, quiet bool, command ...string) []byte {
cmd := exec.Command("composer-cli", command...)
stdout, err := cmd.StdoutPipe()
require.Nilf(t, err, "Could not create command: %v", err)
@ -223,13 +222,23 @@ func runComposerCLI(t *testing.T, quiet bool, command ...string) json.RawMessage
err = cmd.Start()
require.Nilf(t, err, "Could not start command: %v", err)
contents, err := ioutil.ReadAll(stdout)
require.NoError(t, err, "Could not read stdout from command")
err = cmd.Wait()
require.NoErrorf(t, err, "Command failed: %v", err)
return contents
}
func runComposerCLI(t *testing.T, quiet bool, command ...string) json.RawMessage {
command = append([]string{"--json"}, command...)
contents := runComposerCLIPlainText(t, quiet, command...)
var result json.RawMessage
contents, err := ioutil.ReadAll(stdout)
require.Nilf(t, err, "Could not read stdout from command: %v", err)
if len(contents) != 0 {
err = json.Unmarshal(contents, &result)
err := json.Unmarshal(contents, &result)
if err != nil {
// We did not get JSON, try interpreting it as TOML
var data interface{}
@ -243,13 +252,10 @@ func runComposerCLI(t *testing.T, quiet bool, command ...string) json.RawMessage
}
}
err = cmd.Wait()
require.Nilf(t, err, "Command failed: %v", err)
buffer := bytes.Buffer{}
encoder := json.NewEncoder(&buffer)
encoder.SetIndent("", " ")
err = encoder.Encode(result)
err := encoder.Encode(result)
require.Nilf(t, err, "Could not remarshal the recevied JSON: %v", err)
return result