create_test.go 3.3 KB

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