gitops-pusher_test.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. // Copyright (c) Tailscale Inc & contributors
  2. // SPDX-License-Identifier: BSD-3-Clause
  3. package main
  4. import (
  5. "encoding/json"
  6. "strings"
  7. "testing"
  8. "tailscale.com/client/tailscale"
  9. )
  10. func TestEmbeddedTypeUnmarshal(t *testing.T) {
  11. var gitopsErr ACLGitopsTestError
  12. gitopsErr.Message = "gitops response error"
  13. gitopsErr.Data = []tailscale.ACLTestFailureSummary{
  14. {
  15. User: "GitopsError",
  16. Errors: []string{"this was initially created as a gitops error"},
  17. },
  18. }
  19. var aclTestErr tailscale.ACLTestError
  20. aclTestErr.Message = "native ACL response error"
  21. aclTestErr.Data = []tailscale.ACLTestFailureSummary{
  22. {
  23. User: "ACLError",
  24. Errors: []string{"this was initially created as an ACL error"},
  25. },
  26. }
  27. t.Run("unmarshal gitops type from acl type", func(t *testing.T) {
  28. b, _ := json.Marshal(aclTestErr)
  29. var e ACLGitopsTestError
  30. err := json.Unmarshal(b, &e)
  31. if err != nil {
  32. t.Fatal(err)
  33. }
  34. if !strings.Contains(e.Error(), "For user ACLError") { // the gitops error prints out the user, the acl error doesn't
  35. t.Fatalf("user heading for 'ACLError' not found in gitops error: %v", e.Error())
  36. }
  37. })
  38. t.Run("unmarshal acl type from gitops type", func(t *testing.T) {
  39. b, _ := json.Marshal(gitopsErr)
  40. var e tailscale.ACLTestError
  41. err := json.Unmarshal(b, &e)
  42. if err != nil {
  43. t.Fatal(err)
  44. }
  45. expectedErr := `Status: 0, Message: "gitops response error", Data: [{User:GitopsError Errors:[this was initially created as a gitops error] Warnings:[]}]`
  46. if e.Error() != expectedErr {
  47. t.Fatalf("got %v\n, expected %v", e.Error(), expectedErr)
  48. }
  49. })
  50. }