Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
migrated over 200 to webui tests
Browse files Browse the repository at this point in the history
aronchick committed Jan 6, 2024
1 parent e11573b commit 6c02d02
Showing 4 changed files with 102 additions and 45 deletions.
8 changes: 8 additions & 0 deletions .gitprecommit/codespell_ignore_words.txt
Original file line number Diff line number Diff line change
@@ -139,3 +139,11 @@ errgroup
stylecheck
Msgf
bodyclose
devstack
samber
pluggable
multierr
bidstrategy
rcmgr
strs
maddrs
48 changes: 5 additions & 43 deletions cmd/cli/serve/serve_test.go
Original file line number Diff line number Diff line change
@@ -10,7 +10,6 @@ import (
"testing"

"github.com/multiformats/go-multiaddr"
"github.com/phayes/freeport"
"github.com/stretchr/testify/suite"

"github.com/bacalhau-project/bacalhau/pkg/publicapi/client"
@@ -64,7 +63,7 @@ func (s *tServeSuite) SetupTest() {
s.IPFSPort = node.APIPort
}

func (s *tServeSuite) serve(extraArgs ...string) (uint16, error) {
func (s *tServeSuite) serveForCLI(extraArgs ...string) (uint16, error) {
extraArgs = append(extraArgs, "--repo", s.RepoPath,
"--peer", serve.DefaultPeerConnect,
"--private-internal-ipfs")
@@ -82,7 +81,7 @@ func (s *tServeSuite) serve(extraArgs ...string) (uint16, error) {
}

func (s *tServeSuite) TestHealthcheck() {
port, _ := s.serve()
port, _ := s.serveForCLI()
healthzText, statusCode, err := utilserve.CurlEndpoint(s.Ctx, fmt.Sprintf("http://127.0.0.1:%d/api/v1/healthz", port))
s.Require().NoError(err)
var healthzJSON types.HealthInfo
@@ -92,22 +91,22 @@ func (s *tServeSuite) TestHealthcheck() {
}

func (s *tServeSuite) TestAPIPrintedForComputeNode() {
port, _ := s.serve("--node-type", "compute", "--log-mode", string(logger.LogModeStation))
port, _ := s.serveForCLI("--node-type", "compute", "--log-mode", string(logger.LogModeStation))
expectedURL := fmt.Sprintf("API: http://0.0.0.0:%d/api/v1/compute/debug", port)
actualUrl := s.Out.String()
s.Require().Contains(actualUrl, expectedURL)
}

func (s *tServeSuite) TestAPINotPrintedForRequesterNode() {
port, _ := s.serve("--node-type", "requester", "--log-mode", string(logger.LogModeStation))
port, _ := s.serveForCLI("--node-type", "requester", "--log-mode", string(logger.LogModeStation))
expectedURL := fmt.Sprintf("API: http://0.0.0.0:%d/compute/debug", port)
s.Require().NotContains(s.Out.String(), expectedURL)
}

func (s *tServeSuite) TestCanSubmitJob() {
docker.MustHaveDocker(s.T())
ctx := context.Background()
port, _ := s.serve("--node-type", "requester", "--node-type", "compute")
port, _ := s.serveForCLI("--node-type", "requester", "--node-type", "compute")
client := client.NewAPIClient("localhost", port)
clientV2 := clientv2.New(clientv2.Options{
Address: fmt.Sprintf("http://127.0.0.1:%d", port),
@@ -208,40 +207,3 @@ func (s *tServeSuite) TestGetPeers() {
_, err = serve.GetPeers(peerConnect)
s.Require().Error(err)
}

// Begin WebUI Tests
func (s *tServeSuite) Test200ForNotStartingWebUI() {
port, err := s.serve()
s.Require().NoError(err, "Error starting server")

content, statusCode, err := utilserve.CurlEndpoint(s.Ctx, fmt.Sprintf("http://127.0.0.1:%d/", port))
_ = content
s.Require().NoError(err, "Error curling root endpoint")
s.Require().Equal(http.StatusOK, statusCode, "Did not return 200 OK.")
}

func (s *tServeSuite) Test200ForRoot() {
webUIPort, err := freeport.GetFreePort()
if err != nil {
s.T().Fatal(err, "Could not get port for web-ui")
}
_, err = s.serve("--web-ui", "--web-ui-port", fmt.Sprintf("%d", webUIPort))
s.Require().NoError(err, "Error starting server")

_, statusCode, err := utilserve.CurlEndpoint(s.Ctx, fmt.Sprintf("http://127.0.0.1:%d/", webUIPort))
s.Require().NoError(err, "Error curling root endpoint")
s.Require().Equal(http.StatusOK, statusCode, "Did not return 200 OK.")
}

// TODO: Can't figure out how to make this test work, it spits out the help text
// func (s *ServeSuite) TestBadBacalhauDir() {
// badDirString := "/BADDIR"

// // if we set the peer connect to "env" it should return the peers from the env
// originalEnv := os.Getenv("BACALHAU_ENVIRONMENT")
// defer os.Setenv("BACALHAU_ENVIRONMENT", originalEnv)
// os.Setenv("BACALHAU_DIR", badDirString)
// _, err := s.serve("--node-type", "requester", "--node-type", "compute", RETURN_ERROR_FLAG)
// s.Require().Contains(s.out.String(), "Could not write to")
// s.Error(err)
// }
2 changes: 1 addition & 1 deletion cmd/cli/serve/timeout_test.go
Original file line number Diff line number Diff line change
@@ -51,7 +51,7 @@ func (s *tServeSuite) TestNoTimeoutSetOrApplied() {
args = append(args, "--max-job-execution-timeout", tc.configuredMax.String())
}

port, err := s.serve(args...)
port, err := s.serveForCLI(args...)
s.Require().NoError(err)

client := client.NewAPIClient("localhost", port)
89 changes: 88 additions & 1 deletion webui/webui_test.go
Original file line number Diff line number Diff line change
@@ -1 +1,88 @@
package webui
package webui_test

import (
"context"
"fmt"
"net/http"
"testing"

types2 "github.com/bacalhau-project/bacalhau/pkg/config/types"
"github.com/phayes/freeport"
"github.com/stretchr/testify/suite"

"github.com/bacalhau-project/bacalhau/pkg/ipfs"
"github.com/bacalhau-project/bacalhau/pkg/logger"
"github.com/bacalhau-project/bacalhau/pkg/setup"
"github.com/bacalhau-project/bacalhau/pkg/system"
utilserve "github.com/bacalhau-project/bacalhau/pkg/util/serve"
)

type tWebUISuite struct {
*utilserve.ServeSuite
}

func TestWebUISuite(t *testing.T) {
suite.Run(t, &tWebUISuite{ServeSuite: new(utilserve.ServeSuite)})
}

func (s *tWebUISuite) SetupTest() {
logger.ConfigureTestLogging(s.T())
fsRepo := setup.SetupBacalhauRepoForTesting(s.T())
repoPath, err := fsRepo.Path()
s.Require().NoError(err)
s.RepoPath = repoPath

var cancel context.CancelFunc
s.Ctx, cancel = context.WithTimeout(context.Background(), utilserve.MaxTestTime)
s.T().Cleanup(func() {
cancel()
})

cm := system.NewCleanupManager()
s.T().Cleanup(func() {
cm.Cleanup(s.Ctx)
})

node, err := ipfs.NewNodeWithConfig(s.Ctx, cm, types2.IpfsConfig{PrivateInternal: true})
s.Require().NoError(err)
s.IPFSPort = node.APIPort
}
func (s *tWebUISuite) serveForWebUI(extraArgs ...string) (uint16, error) {
webUIPort, err := freeport.GetFreePort()
if err != nil {
s.T().Fatal(err, "Could not get port for web-ui")
}

extraArgs = append(extraArgs, "--web-ui", "--web-ui-port", fmt.Sprintf("%d", webUIPort))

// If the slice contains RETURN_ERROR_FLAG, take it out of the array and set returnError to true
// peer set to "none" to avoid accidentally talking to production endpoints (even though it's default)
// private-internal-ipfs to avoid accidentally talking to public IPFS nodes (even though it's default)
returnError, _, err := utilserve.StartServerForTesting(s.ServeSuite, extraArgs)

if returnError {
return uint16(webUIPort), err
}
s.NoError(err)
return uint16(webUIPort), nil
}

// Begin WebUI Tests
func (s *tWebUISuite) Test200ForNotStartingWebUI() {
webUIPort, err := s.serveForWebUI()
s.Require().NoError(err, "Error starting server")

content, statusCode, err := utilserve.CurlEndpoint(s.Ctx, fmt.Sprintf("http://127.0.0.1:%d/", webUIPort))
_ = content
s.Require().NoError(err, "Error curling root endpoint")
s.Require().Equal(http.StatusOK, statusCode, "Did not return 200 OK.")
}

func (s *tWebUISuite) Test200ForRoot() {
webUIPort, err := s.serveForWebUI()
s.Require().NoError(err, "Error starting server")

_, statusCode, err := utilserve.CurlEndpoint(s.Ctx, fmt.Sprintf("http://127.0.0.1:%d/", webUIPort))
s.Require().NoError(err, "Error curling root endpoint")
s.Require().Equal(http.StatusOK, statusCode, "Did not return 200 OK.")
}

0 comments on commit 6c02d02

Please sign in to comment.