Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: cleanup operator_test.go to only make local calls (no internet dependencies) #350

Draft
wants to merge 3 commits into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added types/.testdata/operator.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
96 changes: 89 additions & 7 deletions types/operator_test.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,97 @@
package types

import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"net"
"net/http"
"net/http/httptest"
"os"
"testing"
"time"

"github.com/Layr-Labs/eigensdk-go/utils"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestOperatorValidate(t *testing.T) {

operatorMetadata := OperatorMetadata{
Name: "Madhur 1",
Website: "https://www.facebook.com",
Description: "Madhur's first operator is best in this world",
Logo: "SET BELOW ONCE URL IS KNOWN",
Twitter: "https://twitter.com/shrimalmadhur",
}

operatorPngData, err := os.ReadFile("./.testdata/operator.png")
require.NoError(t, err, "Failed to read operator.png")
operatorPngBuffer := bytes.NewBuffer(operatorPngData)
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/404metadata.json" {
w.WriteHeader(http.StatusNotFound)
return
}
if r.URL.Path == "/operator.png" {
w.Header().Set("Content-Type", "image/png")
http.ServeContent(w, r, "operator.png", time.Now(), bytes.NewReader(operatorPngBuffer.Bytes()))
return
}
marshalledOperatorMetadata, err := json.Marshal(operatorMetadata)
require.NoError(t, err)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I forgot what was diff between require and assert?

_, err = w.Write(marshalledOperatorMetadata)
require.NoError(t, err)
}))
defer ts.Close()
fmt.Println(ts.URL)

resolver := &net.Resolver{
PreferGo: true,
Dial: func(ctx context.Context, network, address string) (net.Conn, error) {
return nil, nil
},
}

// Override the default resolver
net.DefaultResolver = resolver

// Create a custom dialer
dialer := &net.Dialer{
Resolver: resolver,
}

// Override the default HTTP transport
http.DefaultTransport.(*http.Transport).DialContext = func(ctx context.Context, network, addr string) (net.Conn, error) {
host, port, err := net.SplitHostPort(addr)
if err != nil {
return nil, err
}

// Redirect specific hostnames to localhost
switch host {
case "mylocalserver.com":
addr = net.JoinHostPort("127.0.0.1", port)
}

return dialer.DialContext(ctx, network, addr)
}

_, port, err := net.SplitHostPort(ts.URL[7:])
if err != nil {
panic(err)
}
url := fmt.Sprintf("http://mylocalserver.com:%s", port)
operatorMetadata.Logo = url + "/operator.png"

defer func() {
http.DefaultTransport = &http.Transport{}
}()

var tests = []struct {
name string
operator Operator
Expand All @@ -24,7 +106,7 @@ func TestOperatorValidate(t *testing.T) {
Address: "0xd5e099c71b797516c10ed0f0d895f429c2781142",
DelegationApproverAddress: "0xd5e099c71b797516c10ed0f0d895f429c2781142",
StakerOptOutWindowBlocks: 100,
MetadataUrl: "https://madhur-test-public.s3.us-east-2.amazonaws.com/metadata.json",
MetadataUrl: url + "/metadata.json",
},
wantErr: false,
},
Expand All @@ -34,7 +116,7 @@ func TestOperatorValidate(t *testing.T) {
Address: "0xd5e099c71b797516c10ed0f0d895f429c2781142",
DelegationApproverAddress: ZeroAddress,
StakerOptOutWindowBlocks: 100,
MetadataUrl: "https://madhur-test-public.s3.us-east-2.amazonaws.com/metadata.json",
MetadataUrl: url + "/metadata.json",
},
wantErr: false,
},
Expand All @@ -50,7 +132,7 @@ func TestOperatorValidate(t *testing.T) {
expectedErr: utils.WrapError(ErrInvalidMetadataUrl, utils.ErrEmptyUrl),
},
{
name: "failed operator validation - localhost metadata url",
name: "failed operator validation - localhost metadata url disallowed",
operator: Operator{
Address: "0xd5e099c71b797516c10ed0f0d895f429c2781142",
DelegationApproverAddress: "0xd5e099c71b797516c10ed0f0d895f429c2781142",
Expand All @@ -61,7 +143,7 @@ func TestOperatorValidate(t *testing.T) {
expectedErr: utils.WrapError(ErrInvalidMetadataUrl, utils.ErrUrlPointingToLocalServer),
},
{
name: "failed operator validation - 127.0.0.1 metadata url",
name: "failed operator validation - 127.0.0.1 metadata url disallowed",
operator: Operator{
Address: "0xd5e099c71b797516c10ed0f0d895f429c2781142",
DelegationApproverAddress: "0xd5e099c71b797516c10ed0f0d895f429c2781142",
Expand All @@ -77,7 +159,7 @@ func TestOperatorValidate(t *testing.T) {
Address: "0xd5e099c71b797516c10ed0f0d895f429c2781142",
DelegationApproverAddress: "0xd5e099c71b797516c10ed0f0d895f429c2781142",
StakerOptOutWindowBlocks: 100,
MetadataUrl: "https://example.com/metadata.json",
MetadataUrl: url + "/404metadata.json",
},
wantErr: true,
expectedErr: utils.WrapError(
Expand All @@ -91,7 +173,7 @@ func TestOperatorValidate(t *testing.T) {
Address: "0xa",
DelegationApproverAddress: "0xd5e099c71b797516c10ed0f0d895f429c2781142",
StakerOptOutWindowBlocks: 100,
MetadataUrl: "https://example.com/metadata.json",
MetadataUrl: url + "/metadata.json",
},
wantErr: true,
expectedErr: ErrInvalidOperatorAddress,
Expand All @@ -102,7 +184,7 @@ func TestOperatorValidate(t *testing.T) {
Address: "0xd5e099c71b797516c10ed0f0d895f429c2781142",
DelegationApproverAddress: "0x12",
StakerOptOutWindowBlocks: 100,
MetadataUrl: "https://example.com/metadata.json",
MetadataUrl: url + "/metadata.json",
},
wantErr: true,
expectedErr: ErrInvalidDelegationApproverAddress,
Expand Down