create_test.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. // Copyright 2018 The go-ethereum Authors
  2. // This file is part of go-ethereum.
  3. //
  4. // go-ethereum is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // go-ethereum is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.
  16. package main
  17. import (
  18. "encoding/json"
  19. "fmt"
  20. "io/ioutil"
  21. "os"
  22. "sort"
  23. "strconv"
  24. "strings"
  25. "testing"
  26. "github.com/ethereum/go-ethereum/p2p/simulations"
  27. )
  28. // TestSnapshotCreate is a high level e2e test that tests for snapshot generation.
  29. // It runs a few "create" commands with different flag values and loads generated
  30. // snapshot files to validate their content.
  31. func TestSnapshotCreate(t *testing.T) {
  32. t.Skip("test is flaky. disabling until underlying problem is addressed")
  33. for _, v := range []struct {
  34. name string
  35. nodes int
  36. services string
  37. }{
  38. {
  39. name: "defaults",
  40. },
  41. {
  42. name: "more nodes",
  43. nodes: defaultNodes + 4,
  44. },
  45. {
  46. name: "services",
  47. services: "stream,pss,zorglub",
  48. },
  49. {
  50. name: "services with bzz",
  51. services: "bzz,pss",
  52. },
  53. } {
  54. t.Run(v.name, func(t *testing.T) {
  55. t.Parallel()
  56. file, err := ioutil.TempFile("", "swarm-snapshot")
  57. if err != nil {
  58. t.Fatal(err)
  59. }
  60. defer os.Remove(file.Name())
  61. if err = file.Close(); err != nil {
  62. t.Error(err)
  63. }
  64. args := []string{"create"}
  65. if v.nodes > 0 {
  66. args = append(args, "--nodes", strconv.Itoa(v.nodes))
  67. }
  68. if v.services != "" {
  69. args = append(args, "--services", v.services)
  70. }
  71. testCmd := runSnapshot(t, append(args, file.Name())...)
  72. testCmd.WaitExit()
  73. if code := testCmd.ExitStatus(); code != 0 {
  74. t.Fatalf("command exit code %v, expected 0", code)
  75. }
  76. f, err := os.Open(file.Name())
  77. if err != nil {
  78. t.Fatal(err)
  79. }
  80. defer func() {
  81. err := f.Close()
  82. if err != nil {
  83. t.Error("closing snapshot file", "err", err)
  84. }
  85. }()
  86. b, err := ioutil.ReadAll(f)
  87. if err != nil {
  88. t.Fatal(err)
  89. }
  90. var snap simulations.Snapshot
  91. err = json.Unmarshal(b, &snap)
  92. if err != nil {
  93. t.Fatal(err)
  94. }
  95. wantNodes := v.nodes
  96. if wantNodes == 0 {
  97. wantNodes = defaultNodes
  98. }
  99. gotNodes := len(snap.Nodes)
  100. if gotNodes != wantNodes {
  101. t.Errorf("got %v nodes, want %v", gotNodes, wantNodes)
  102. }
  103. if len(snap.Conns) == 0 {
  104. t.Error("no connections in a snapshot")
  105. }
  106. var wantServices []string
  107. if v.services != "" {
  108. wantServices = strings.Split(v.services, ",")
  109. } else {
  110. wantServices = []string{"bzz"}
  111. }
  112. // sort service names so they can be comparable
  113. // as strings to every node sorted services
  114. sort.Strings(wantServices)
  115. for i, n := range snap.Nodes {
  116. gotServices := n.Node.Config.Services
  117. sort.Strings(gotServices)
  118. if fmt.Sprint(gotServices) != fmt.Sprint(wantServices) {
  119. t.Errorf("got services %v for node %v, want %v", gotServices, i, wantServices)
  120. }
  121. }
  122. })
  123. }
  124. }