create_test.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. for _, v := range []struct {
  33. name string
  34. nodes int
  35. services string
  36. }{
  37. {
  38. name: "defaults",
  39. },
  40. {
  41. name: "more nodes",
  42. nodes: defaultNodes + 5,
  43. },
  44. {
  45. name: "services",
  46. services: "stream,pss,zorglub",
  47. },
  48. {
  49. name: "services with bzz",
  50. services: "bzz,pss",
  51. },
  52. } {
  53. t.Run(v.name, func(t *testing.T) {
  54. t.Parallel()
  55. file, err := ioutil.TempFile("", "swarm-snapshot")
  56. if err != nil {
  57. t.Fatal(err)
  58. }
  59. defer os.Remove(file.Name())
  60. if err = file.Close(); err != nil {
  61. t.Error(err)
  62. }
  63. args := []string{"create"}
  64. if v.nodes > 0 {
  65. args = append(args, "--nodes", strconv.Itoa(v.nodes))
  66. }
  67. if v.services != "" {
  68. args = append(args, "--services", v.services)
  69. }
  70. testCmd := runSnapshot(t, append(args, file.Name())...)
  71. testCmd.ExpectExit()
  72. if code := testCmd.ExitStatus(); code != 0 {
  73. t.Fatalf("command exit code %v, expected 0", code)
  74. }
  75. f, err := os.Open(file.Name())
  76. if err != nil {
  77. t.Fatal(err)
  78. }
  79. defer func() {
  80. err := f.Close()
  81. if err != nil {
  82. t.Error("closing snapshot file", "err", err)
  83. }
  84. }()
  85. b, err := ioutil.ReadAll(f)
  86. if err != nil {
  87. t.Fatal(err)
  88. }
  89. var snap simulations.Snapshot
  90. err = json.Unmarshal(b, &snap)
  91. if err != nil {
  92. t.Fatal(err)
  93. }
  94. wantNodes := v.nodes
  95. if wantNodes == 0 {
  96. wantNodes = defaultNodes
  97. }
  98. gotNodes := len(snap.Nodes)
  99. if gotNodes != wantNodes {
  100. t.Errorf("got %v nodes, want %v", gotNodes, wantNodes)
  101. }
  102. if len(snap.Conns) == 0 {
  103. t.Error("no connections in a snapshot")
  104. }
  105. var wantServices []string
  106. if v.services != "" {
  107. wantServices = strings.Split(v.services, ",")
  108. } else {
  109. wantServices = []string{"bzz"}
  110. }
  111. // sort service names so they can be comparable
  112. // as strings to every node sorted services
  113. sort.Strings(wantServices)
  114. for i, n := range snap.Nodes {
  115. gotServices := n.Node.Config.Services
  116. sort.Strings(gotServices)
  117. if fmt.Sprint(gotServices) != fmt.Sprint(wantServices) {
  118. t.Errorf("got services %v for node %v, want %v", gotServices, i, wantServices)
  119. }
  120. }
  121. })
  122. }
  123. }