Skip to content

Commit

Permalink
roachprod: add ip expander func unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
DarrylWong committed Feb 6, 2025
1 parent 4140914 commit 3b1a0b0
Show file tree
Hide file tree
Showing 3 changed files with 303 additions and 0 deletions.
1 change: 1 addition & 0 deletions pkg/roachprod/install/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ go_test(
srcs = [
"cluster_synced_test.go",
"cockroach_test.go",
"expander_test.go",
"monitor_test.go",
"services_test.go",
"session_test.go",
Expand Down
89 changes: 89 additions & 0 deletions pkg/roachprod/install/expander_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
// Copyright 2025 The Cockroach Authors.
//
// Use of this software is governed by the CockroachDB Software License
// included in the /LICENSE file.

package install

import (
"context"
"encoding/json"
"os"
"testing"

"github.com/cockroachdb/cockroach/pkg/roachprod/cloud"
"github.com/cockroachdb/cockroach/pkg/roachprod/logger"
"github.com/cockroachdb/cockroach/pkg/testutils/datapathutils"
"github.com/stretchr/testify/require"
)

func newTestCluster(t *testing.T) *SyncedCluster {
testClusterFile := datapathutils.TestDataPath(t, "test_cluster.txt")
data, err := os.ReadFile(testClusterFile)
if err != nil {
t.Fatalf("could not read test cluster file: %s", err)
}
metadata := &cloud.Cluster{}
if err := json.Unmarshal(data, metadata); err != nil {
t.Fatalf("could not unmarshal test cluster file: %s", err)
}
c, err := NewSyncedCluster(metadata, "all", MakeClusterSettings())
if err != nil {
t.Fatalf("could not create synced cluster: %s", err)
}
return c
}

func TestIPExpander(t *testing.T) {
ctx := context.Background()
l := &logger.Logger{}

c := newTestCluster(t)
e := &expander{
node: c.Nodes[0],
}
cfg := ExpanderConfig{}

for _, tc := range []struct {
name string
command string
expected string
}{
{
name: "same node",
command: "{ip:1}",
expected: "10.142.1.1",
},
{
name: "different node",
command: "{ip:2}",
expected: "10.142.1.2",
},
{
name: "range of nodes",
command: "{ip:1-3}",
expected: "10.142.1.1 10.142.1.2 10.142.1.3",
},
{
name: "non consecutive nodes",
command: "{ip:2,4}",
expected: "10.142.1.2 10.142.1.4",
},
{
name: "public ip",
command: "{ip:1-4:public}",
expected: "35.196.120.1 35.227.25.2 34.75.95.3 34.139.54.4",
},
{
name: "private ip",
command: "{ip:1-4:private}",
expected: "10.142.1.1 10.142.1.2 10.142.1.3 10.142.1.4",
},
} {
t.Run(tc.name, func(t *testing.T) {
res, err := e.expand(ctx, l, c, cfg, tc.command)
require.NoError(t, err)
require.Equal(t, tc.expected, res)
})
}
}
213 changes: 213 additions & 0 deletions pkg/roachprod/install/testdata/test_cluster.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,213 @@
{
"name": "foo-bar",
"user": "baz",
"created_at": "2025-02-04T16:02:50.788732Z",
"lifetime": 43200000000000,
"vms": [
{
"name": "foo-bar-0001",
"created_at": "2025-02-04T08:02:52.385-08:00",
"errors": null,
"lifetime": 43200000000000,
"preemptible": false,
"labels": {
"cluster": "foo-bar",
"created": "2025-02-04t16_02_50z",
"lifetime": "12h0m0s",
"roachprod": "true",
"usage": "roachprod"
},
"dns": "foo-bar-0001.us-east1-d.unit-test",
"public_dns": "foo-bar-0001.roachprod.crdb.io",
"dns_provider": "gce",
"provider": "gce",
"provider_id": "foo-bar-0001",
"private_ip": "10.142.1.1",
"public_ip": "35.196.120.1",
"remote_user": "ubuntu",
"vpc": "default",
"machine_type": "n2-standard-4",
"cpu_architecture": "amd64",
"cpu_family": "ice lake",
"zone": "us-east1-d",
"project": "unit-test",
"non_bootable_volumes": null,
"bootable_volume": {
"ProviderResourceID": "foo-bar-0001",
"ProviderVolumeType": "pd-ssd",
"Zone": "us-east1-d",
"Encrypted": false,
"Name": "foo-bar-0001",
"Labels": null,
"Size": 10
},
"local_disks": [
{
"ProviderResourceID": "",
"ProviderVolumeType": "local-ssd",
"Zone": "",
"Encrypted": false,
"Name": "",
"Labels": null,
"Size": 375
}
],
"CostPerHour": 0,
"EmptyCluster": false
},
{
"name": "foo-bar-0002",
"created_at": "2025-02-04T08:02:52.633-08:00",
"errors": null,
"lifetime": 43200000000000,
"preemptible": false,
"labels": {
"cluster": "foo-bar",
"created": "2025-02-04t16_02_50z",
"lifetime": "12h0m0s",
"roachprod": "true",
"usage": "roachprod"
},
"dns": "foo-bar-0002.us-east1-d.unit-test",
"public_dns": "foo-bar-0002.roachprod.crdb.io",
"dns_provider": "gce",
"provider": "gce",
"provider_id": "foo-bar-0002",
"private_ip": "10.142.1.2",
"public_ip": "35.227.25.2",
"remote_user": "ubuntu",
"vpc": "default",
"machine_type": "n2-standard-4",
"cpu_architecture": "amd64",
"cpu_family": "ice lake",
"zone": "us-east1-d",
"project": "unit-test",
"non_bootable_volumes": null,
"bootable_volume": {
"ProviderResourceID": "foo-bar-0002",
"ProviderVolumeType": "pd-ssd",
"Zone": "us-east1-d",
"Encrypted": false,
"Name": "foo-bar-0002",
"Labels": null,
"Size": 10
},
"local_disks": [
{
"ProviderResourceID": "",
"ProviderVolumeType": "local-ssd",
"Zone": "",
"Encrypted": false,
"Name": "",
"Labels": null,
"Size": 375
}
],
"CostPerHour": 0,
"EmptyCluster": false
},
{
"name": "foo-bar-0003",
"created_at": "2025-02-04T08:02:52.516-08:00",
"errors": null,
"lifetime": 43200000000000,
"preemptible": false,
"labels": {
"cluster": "foo-bar",
"created": "2025-02-04t16_02_50z",
"lifetime": "12h0m0s",
"roachprod": "true",
"usage": "roachprod"
},
"dns": "foo-bar-0003.us-east1-d.unit-test",
"public_dns": "foo-bar-0003.roachprod.crdb.io",
"dns_provider": "gce",
"provider": "gce",
"provider_id": "foo-bar-0003",
"private_ip": "10.142.1.3",
"public_ip": "34.75.95.3",
"remote_user": "ubuntu",
"vpc": "default",
"machine_type": "n2-standard-4",
"cpu_architecture": "amd64",
"cpu_family": "ice lake",
"zone": "us-east1-d",
"project": "unit-test",
"non_bootable_volumes": null,
"bootable_volume": {
"ProviderResourceID": "foo-bar-0003",
"ProviderVolumeType": "pd-ssd",
"Zone": "us-east1-d",
"Encrypted": false,
"Name": "foo-bar-0003",
"Labels": null,
"Size": 10
},
"local_disks": [
{
"ProviderResourceID": "",
"ProviderVolumeType": "local-ssd",
"Zone": "",
"Encrypted": false,
"Name": "",
"Labels": null,
"Size": 375
}
],
"CostPerHour": 0,
"EmptyCluster": false
},
{
"name": "foo-bar-0004",
"created_at": "2025-02-04T08:02:52.54-08:00",
"errors": null,
"lifetime": 43200000000000,
"preemptible": false,
"labels": {
"cluster": "foo-bar",
"created": "2025-02-04t16_02_50z",
"lifetime": "12h0m0s",
"roachprod": "true",
"usage": "roachprod"
},
"dns": "foo-bar-0004.us-east1-d.unit-test",
"public_dns": "foo-bar-0004.roachprod.crdb.io",
"dns_provider": "gce",
"provider": "gce",
"provider_id": "foo-bar-0004",
"private_ip": "10.142.1.4",
"public_ip": "34.139.54.4",
"remote_user": "ubuntu",
"vpc": "default",
"machine_type": "n2-standard-4",
"cpu_architecture": "amd64",
"cpu_family": "ice lake",
"zone": "us-east1-d",
"project": "unit-testl",
"non_bootable_volumes": null,
"bootable_volume": {
"ProviderResourceID": "foo-bar-0004",
"ProviderVolumeType": "pd-ssd",
"Zone": "us-east1-d",
"Encrypted": false,
"Name": "foo-bar-0004",
"Labels": null,
"Size": 10
},
"local_disks": [
{
"ProviderResourceID": "",
"ProviderVolumeType": "local-ssd",
"Zone": "",
"Encrypted": false,
"Name": "",
"Labels": null,
"Size": 375
}
],
"CostPerHour": 0,
"EmptyCluster": false
}
],
"CostPerHour": 0
}

0 comments on commit 3b1a0b0

Please sign in to comment.