Skip to content

Commit 3b1a0b0

Browse files
committed
roachprod: add ip expander func unit tests
1 parent 4140914 commit 3b1a0b0

File tree

3 files changed

+303
-0
lines changed

3 files changed

+303
-0
lines changed

pkg/roachprod/install/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ go_test(
5959
srcs = [
6060
"cluster_synced_test.go",
6161
"cockroach_test.go",
62+
"expander_test.go",
6263
"monitor_test.go",
6364
"services_test.go",
6465
"session_test.go",
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
// Copyright 2025 The Cockroach Authors.
2+
//
3+
// Use of this software is governed by the CockroachDB Software License
4+
// included in the /LICENSE file.
5+
6+
package install
7+
8+
import (
9+
"context"
10+
"encoding/json"
11+
"os"
12+
"testing"
13+
14+
"github.com/cockroachdb/cockroach/pkg/roachprod/cloud"
15+
"github.com/cockroachdb/cockroach/pkg/roachprod/logger"
16+
"github.com/cockroachdb/cockroach/pkg/testutils/datapathutils"
17+
"github.com/stretchr/testify/require"
18+
)
19+
20+
func newTestCluster(t *testing.T) *SyncedCluster {
21+
testClusterFile := datapathutils.TestDataPath(t, "test_cluster.txt")
22+
data, err := os.ReadFile(testClusterFile)
23+
if err != nil {
24+
t.Fatalf("could not read test cluster file: %s", err)
25+
}
26+
metadata := &cloud.Cluster{}
27+
if err := json.Unmarshal(data, metadata); err != nil {
28+
t.Fatalf("could not unmarshal test cluster file: %s", err)
29+
}
30+
c, err := NewSyncedCluster(metadata, "all", MakeClusterSettings())
31+
if err != nil {
32+
t.Fatalf("could not create synced cluster: %s", err)
33+
}
34+
return c
35+
}
36+
37+
func TestIPExpander(t *testing.T) {
38+
ctx := context.Background()
39+
l := &logger.Logger{}
40+
41+
c := newTestCluster(t)
42+
e := &expander{
43+
node: c.Nodes[0],
44+
}
45+
cfg := ExpanderConfig{}
46+
47+
for _, tc := range []struct {
48+
name string
49+
command string
50+
expected string
51+
}{
52+
{
53+
name: "same node",
54+
command: "{ip:1}",
55+
expected: "10.142.1.1",
56+
},
57+
{
58+
name: "different node",
59+
command: "{ip:2}",
60+
expected: "10.142.1.2",
61+
},
62+
{
63+
name: "range of nodes",
64+
command: "{ip:1-3}",
65+
expected: "10.142.1.1 10.142.1.2 10.142.1.3",
66+
},
67+
{
68+
name: "non consecutive nodes",
69+
command: "{ip:2,4}",
70+
expected: "10.142.1.2 10.142.1.4",
71+
},
72+
{
73+
name: "public ip",
74+
command: "{ip:1-4:public}",
75+
expected: "35.196.120.1 35.227.25.2 34.75.95.3 34.139.54.4",
76+
},
77+
{
78+
name: "private ip",
79+
command: "{ip:1-4:private}",
80+
expected: "10.142.1.1 10.142.1.2 10.142.1.3 10.142.1.4",
81+
},
82+
} {
83+
t.Run(tc.name, func(t *testing.T) {
84+
res, err := e.expand(ctx, l, c, cfg, tc.command)
85+
require.NoError(t, err)
86+
require.Equal(t, tc.expected, res)
87+
})
88+
}
89+
}
Lines changed: 213 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,213 @@
1+
{
2+
"name": "foo-bar",
3+
"user": "baz",
4+
"created_at": "2025-02-04T16:02:50.788732Z",
5+
"lifetime": 43200000000000,
6+
"vms": [
7+
{
8+
"name": "foo-bar-0001",
9+
"created_at": "2025-02-04T08:02:52.385-08:00",
10+
"errors": null,
11+
"lifetime": 43200000000000,
12+
"preemptible": false,
13+
"labels": {
14+
"cluster": "foo-bar",
15+
"created": "2025-02-04t16_02_50z",
16+
"lifetime": "12h0m0s",
17+
"roachprod": "true",
18+
"usage": "roachprod"
19+
},
20+
"dns": "foo-bar-0001.us-east1-d.unit-test",
21+
"public_dns": "foo-bar-0001.roachprod.crdb.io",
22+
"dns_provider": "gce",
23+
"provider": "gce",
24+
"provider_id": "foo-bar-0001",
25+
"private_ip": "10.142.1.1",
26+
"public_ip": "35.196.120.1",
27+
"remote_user": "ubuntu",
28+
"vpc": "default",
29+
"machine_type": "n2-standard-4",
30+
"cpu_architecture": "amd64",
31+
"cpu_family": "ice lake",
32+
"zone": "us-east1-d",
33+
"project": "unit-test",
34+
"non_bootable_volumes": null,
35+
"bootable_volume": {
36+
"ProviderResourceID": "foo-bar-0001",
37+
"ProviderVolumeType": "pd-ssd",
38+
"Zone": "us-east1-d",
39+
"Encrypted": false,
40+
"Name": "foo-bar-0001",
41+
"Labels": null,
42+
"Size": 10
43+
},
44+
"local_disks": [
45+
{
46+
"ProviderResourceID": "",
47+
"ProviderVolumeType": "local-ssd",
48+
"Zone": "",
49+
"Encrypted": false,
50+
"Name": "",
51+
"Labels": null,
52+
"Size": 375
53+
}
54+
],
55+
"CostPerHour": 0,
56+
"EmptyCluster": false
57+
},
58+
{
59+
"name": "foo-bar-0002",
60+
"created_at": "2025-02-04T08:02:52.633-08:00",
61+
"errors": null,
62+
"lifetime": 43200000000000,
63+
"preemptible": false,
64+
"labels": {
65+
"cluster": "foo-bar",
66+
"created": "2025-02-04t16_02_50z",
67+
"lifetime": "12h0m0s",
68+
"roachprod": "true",
69+
"usage": "roachprod"
70+
},
71+
"dns": "foo-bar-0002.us-east1-d.unit-test",
72+
"public_dns": "foo-bar-0002.roachprod.crdb.io",
73+
"dns_provider": "gce",
74+
"provider": "gce",
75+
"provider_id": "foo-bar-0002",
76+
"private_ip": "10.142.1.2",
77+
"public_ip": "35.227.25.2",
78+
"remote_user": "ubuntu",
79+
"vpc": "default",
80+
"machine_type": "n2-standard-4",
81+
"cpu_architecture": "amd64",
82+
"cpu_family": "ice lake",
83+
"zone": "us-east1-d",
84+
"project": "unit-test",
85+
"non_bootable_volumes": null,
86+
"bootable_volume": {
87+
"ProviderResourceID": "foo-bar-0002",
88+
"ProviderVolumeType": "pd-ssd",
89+
"Zone": "us-east1-d",
90+
"Encrypted": false,
91+
"Name": "foo-bar-0002",
92+
"Labels": null,
93+
"Size": 10
94+
},
95+
"local_disks": [
96+
{
97+
"ProviderResourceID": "",
98+
"ProviderVolumeType": "local-ssd",
99+
"Zone": "",
100+
"Encrypted": false,
101+
"Name": "",
102+
"Labels": null,
103+
"Size": 375
104+
}
105+
],
106+
"CostPerHour": 0,
107+
"EmptyCluster": false
108+
},
109+
{
110+
"name": "foo-bar-0003",
111+
"created_at": "2025-02-04T08:02:52.516-08:00",
112+
"errors": null,
113+
"lifetime": 43200000000000,
114+
"preemptible": false,
115+
"labels": {
116+
"cluster": "foo-bar",
117+
"created": "2025-02-04t16_02_50z",
118+
"lifetime": "12h0m0s",
119+
"roachprod": "true",
120+
"usage": "roachprod"
121+
},
122+
"dns": "foo-bar-0003.us-east1-d.unit-test",
123+
"public_dns": "foo-bar-0003.roachprod.crdb.io",
124+
"dns_provider": "gce",
125+
"provider": "gce",
126+
"provider_id": "foo-bar-0003",
127+
"private_ip": "10.142.1.3",
128+
"public_ip": "34.75.95.3",
129+
"remote_user": "ubuntu",
130+
"vpc": "default",
131+
"machine_type": "n2-standard-4",
132+
"cpu_architecture": "amd64",
133+
"cpu_family": "ice lake",
134+
"zone": "us-east1-d",
135+
"project": "unit-test",
136+
"non_bootable_volumes": null,
137+
"bootable_volume": {
138+
"ProviderResourceID": "foo-bar-0003",
139+
"ProviderVolumeType": "pd-ssd",
140+
"Zone": "us-east1-d",
141+
"Encrypted": false,
142+
"Name": "foo-bar-0003",
143+
"Labels": null,
144+
"Size": 10
145+
},
146+
"local_disks": [
147+
{
148+
"ProviderResourceID": "",
149+
"ProviderVolumeType": "local-ssd",
150+
"Zone": "",
151+
"Encrypted": false,
152+
"Name": "",
153+
"Labels": null,
154+
"Size": 375
155+
}
156+
],
157+
"CostPerHour": 0,
158+
"EmptyCluster": false
159+
},
160+
{
161+
"name": "foo-bar-0004",
162+
"created_at": "2025-02-04T08:02:52.54-08:00",
163+
"errors": null,
164+
"lifetime": 43200000000000,
165+
"preemptible": false,
166+
"labels": {
167+
"cluster": "foo-bar",
168+
"created": "2025-02-04t16_02_50z",
169+
"lifetime": "12h0m0s",
170+
"roachprod": "true",
171+
"usage": "roachprod"
172+
},
173+
"dns": "foo-bar-0004.us-east1-d.unit-test",
174+
"public_dns": "foo-bar-0004.roachprod.crdb.io",
175+
"dns_provider": "gce",
176+
"provider": "gce",
177+
"provider_id": "foo-bar-0004",
178+
"private_ip": "10.142.1.4",
179+
"public_ip": "34.139.54.4",
180+
"remote_user": "ubuntu",
181+
"vpc": "default",
182+
"machine_type": "n2-standard-4",
183+
"cpu_architecture": "amd64",
184+
"cpu_family": "ice lake",
185+
"zone": "us-east1-d",
186+
"project": "unit-testl",
187+
"non_bootable_volumes": null,
188+
"bootable_volume": {
189+
"ProviderResourceID": "foo-bar-0004",
190+
"ProviderVolumeType": "pd-ssd",
191+
"Zone": "us-east1-d",
192+
"Encrypted": false,
193+
"Name": "foo-bar-0004",
194+
"Labels": null,
195+
"Size": 10
196+
},
197+
"local_disks": [
198+
{
199+
"ProviderResourceID": "",
200+
"ProviderVolumeType": "local-ssd",
201+
"Zone": "",
202+
"Encrypted": false,
203+
"Name": "",
204+
"Labels": null,
205+
"Size": 375
206+
}
207+
],
208+
"CostPerHour": 0,
209+
"EmptyCluster": false
210+
}
211+
],
212+
"CostPerHour": 0
213+
}

0 commit comments

Comments
 (0)