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