assert.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. /*
  2. Copyright 2022 Docker Compose CLI authors
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package e2e
  14. import (
  15. "encoding/json"
  16. "strings"
  17. "testing"
  18. "github.com/stretchr/testify/require"
  19. )
  20. // RequireServiceState ensures that the container is in the expected state
  21. // (running or exited).
  22. func RequireServiceState(t testing.TB, cli *CLI, service string, state string) {
  23. t.Helper()
  24. psRes := cli.RunDockerComposeCmd(t, "ps", "--format=json", service)
  25. var psOut []map[string]interface{}
  26. require.NoError(t, json.Unmarshal([]byte(psRes.Stdout()), &psOut),
  27. "Invalid `compose ps` JSON output")
  28. for _, svc := range psOut {
  29. require.Equal(t, service, svc["Service"],
  30. "Found ps output for unexpected service")
  31. require.Equalf(t,
  32. strings.ToLower(state),
  33. strings.ToLower(svc["State"].(string)),
  34. "Service %q (%s) not in expected state",
  35. service, svc["Name"],
  36. )
  37. }
  38. }