Skip to content

Commit

Permalink
test: cleanup operator_test.go to only make local calls (no internet …
Browse files Browse the repository at this point in the history
…dependencies)
  • Loading branch information
samlaf committed Sep 24, 2024
1 parent 6f241c3 commit 970bafe
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 7 deletions.
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.
95 changes: 88 additions & 7 deletions types/operator_test.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,96 @@
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)
w.Write(marshalledOperatorMetadata)

Check failure on line 47 in types/operator_test.go

View workflow job for this annotation

GitHub Actions / Lint

Error return value of `w.Write` is not checked (errcheck)
}))
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 +105,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 +115,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 +131,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 +142,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 +158,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 +172,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 +183,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

0 comments on commit 970bafe

Please sign in to comment.