secret_test.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. package formatter
  2. import (
  3. "bytes"
  4. "testing"
  5. "time"
  6. "github.com/docker/docker/api/types/swarm"
  7. "github.com/stretchr/testify/assert"
  8. )
  9. func TestSecretContextFormatWrite(t *testing.T) {
  10. // Check default output format (verbose and non-verbose mode) for table headers
  11. cases := []struct {
  12. context Context
  13. expected string
  14. }{
  15. // Errors
  16. {
  17. Context{Format: "{{InvalidFunction}}"},
  18. `Template parsing error: template: :1: function "InvalidFunction" not defined
  19. `,
  20. },
  21. {
  22. Context{Format: "{{nil}}"},
  23. `Template parsing error: template: :1:2: executing "" at <nil>: nil is not a command
  24. `,
  25. },
  26. // Table format
  27. {Context{Format: NewSecretFormat("table", false)},
  28. `ID NAME CREATED UPDATED
  29. 1 passwords Less than a second ago Less than a second ago
  30. 2 id_rsa Less than a second ago Less than a second ago
  31. `},
  32. {Context{Format: NewSecretFormat("table {{.Name}}", true)},
  33. `NAME
  34. passwords
  35. id_rsa
  36. `},
  37. {Context{Format: NewSecretFormat("{{.ID}}-{{.Name}}", false)},
  38. `1-passwords
  39. 2-id_rsa
  40. `},
  41. }
  42. secrets := []swarm.Secret{
  43. {ID: "1",
  44. Meta: swarm.Meta{CreatedAt: time.Now(), UpdatedAt: time.Now()},
  45. Spec: swarm.SecretSpec{Annotations: swarm.Annotations{Name: "passwords"}}},
  46. {ID: "2",
  47. Meta: swarm.Meta{CreatedAt: time.Now(), UpdatedAt: time.Now()},
  48. Spec: swarm.SecretSpec{Annotations: swarm.Annotations{Name: "id_rsa"}}},
  49. }
  50. for _, testcase := range cases {
  51. out := bytes.NewBufferString("")
  52. testcase.context.Output = out
  53. if err := SecretWrite(testcase.context, secrets); err != nil {
  54. assert.EqualError(t, err, testcase.expected)
  55. } else {
  56. assert.Equal(t, testcase.expected, out.String())
  57. }
  58. }
  59. }