forked from berachain/polaris
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hive.go
153 lines (135 loc) · 4.54 KB
/
hive.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
// SPDX-License-Identifier: MIT
//
// Copyright (c) 2023 Berachain Foundation
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
package main
import (
"os"
"strings"
"github.com/magefile/mage/mg"
"github.com/magefile/mage/sh"
)
// custom tests for Polaris, struct follows {namespace, files_changed}
// note: this requires an existing instance of the namespace in the Hive repo
type tests struct {
Name string
Files []string
}
const (
baseImageVersion = "polard/base:v0.0.0"
baseHiveDockerPath = "./e2e/hive/"
)
var (
// Variables.
hiveClone = os.Getenv("GOPATH") + "/src/"
clonePath = hiveClone + ".hive-e2e/"
simulatorsPath = clonePath + "simulators/polaris/"
clientsPath = clonePath + "clients/polard/"
simulations = []tests{
{"rpc", []string{"init/genesis.json", "ethclient.hive"}},
{"rpc-compat", []string{"Dockerfile", "tests"}},
{"graphql", []string{"testcases", "init/testGenesis.json"}}}
)
type Hive mg.Namespace
func (h Hive) Setup() error {
LogGreen("Setting up Hive testing environment...")
if _, err := os.Stat(hiveClone); os.IsNotExist(err) {
LogGreen(hiveClone + " does not exist, creating....")
err = os.Mkdir(hiveClone, 0755) //#nosec
if err != nil {
return err
}
}
if err := ExecuteInDirectory(hiveClone, func(...string) error {
LogGreen("Removing existing files in .hive-e2e...")
return sh.RunV("rm", "-rf", clonePath)
}, false); err != nil {
return err
}
if _, err := os.Stat(clonePath); os.IsNotExist(err) {
LogGreen("Cloning ethereum/hive into " + clonePath + "...")
err = ExecuteInDirectory(hiveClone, func(...string) error {
return sh.RunV("git", "clone", "https://github.com/ethereum/hive", ".hive-e2e", "--depth=1")
}, false)
if err != nil {
return err
}
}
if err := h.copyFiles(); err != nil {
return err
}
return ExecuteInDirectory(clonePath, func(...string) error {
LogGreen("Building Hive...")
return goBuild(".")
}, false)
}
func (h Hive) Test(sim, client string) error {
return ExecuteInDirectory(clonePath, func(...string) error {
return sh.RunV("./hive", "--sim", sim, "--client", client)
}, false)
}
func (h Hive) TestV(sim, client string) error {
return ExecuteInDirectory(clonePath, func(...string) error {
return sh.RunV("./hive", "--sim", sim, "--client", client, "--docker.output")
}, false)
}
func (h Hive) View() error {
if err := ExecuteInDirectory(clonePath, func(...string) error {
LogGreen("Building HiveView...")
return sh.RunV("go", "build", "./cmd/hiveview")
}, false); err != nil {
return err
}
return ExecuteInDirectory(clonePath, func(...string) error {
LogGreen("Serving HiveView...")
return sh.RunV("./hiveview", "--serve")
}, false)
}
func (h Hive) copyFiles() error {
LogGreen("Copying Polaris Hive setup files...")
if err := sh.RunV("mkdir", simulatorsPath); err != nil {
return err
}
if err := sh.RunV("cp", "-rf", baseHiveDockerPath+"clients/polard", clientsPath); err != nil {
return err
}
for _, sim := range simulations {
if err := sh.RunV("cp", "-rf", clonePath+"simulators/ethereum/"+sim.Name, simulatorsPath+sim.Name); err != nil {
return err
}
for _, file := range sim.Files {
name := file
if ext := strings.Split(file, "."); len(ext) > 1 && ext[1] == "hive" {
name = strings.Split(file, ".")[0] + ".go"
}
if err := sh.RunV("rm", "-rf", simulatorsPath+sim.Name+"/"+name); err != nil {
return err
}
if err := sh.RunV("cp", "-rf", baseHiveDockerPath+"simulators/"+sim.Name+
"/"+file, simulatorsPath+sim.Name+"/"+name); err != nil {
return err
}
}
}
return nil
}