Skip to content

Commit

Permalink
code base cleanup (#279)
Browse files Browse the repository at this point in the history
  • Loading branch information
samyfodil authored Dec 10, 2024
1 parent 9de33d7 commit 8a92750
Show file tree
Hide file tree
Showing 25 changed files with 194 additions and 495 deletions.
35 changes: 7 additions & 28 deletions clients/p2p/seer/tests/p2p_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,25 +30,17 @@ func TestSeerClient(t *testing.T) {
},
},
})
if err != nil {
t.Error(err)
return
}
assert.NilError(t, err)

simple, err := u.Simple("client")
if err != nil {
t.Error(err)
return
}
assert.NilError(t, err)

// Error reporting no peers providing but we are checking if its 0 so just not returning
seer, err := simple.Seer()
assert.NilError(t, err)

resp, err := seer.Geo().All()
if err != nil {
t.Error("Seer geo all err: ", err)
}
assert.NilError(t, err)

if len(resp) != 0 {
t.Error("Should return empty! returned:", resp)
Expand All @@ -60,18 +52,12 @@ func TestSeerClient(t *testing.T) {
// location of office in 12100 Ford Rd
fake_location := iface.Location{Latitude: 32.91264411258042, Longitude: -96.8907727708027}
err = seer.Geo().Set(fake_location)
if err != nil {
t.Error("Geo set: ", err)
return
}
assert.NilError(t, err)

/***** ALL *****/

resp, err = seer.Geo().All()
if err != nil {
t.Error("Returned Error ", err)
return
}
assert.NilError(t, err)

found_match := false
for _, p := range resp {
Expand All @@ -92,15 +78,8 @@ func TestSeerClient(t *testing.T) {
fake_now_location := iface.Location{Latitude: 32.900211956131386, Longitude: -97.04029425876429}

_, err = seer.Geo().Distance(fake_now_location, 15*1000)
if err != nil {
t.Error("Returned Error ", err)
return
}
assert.NilError(t, err)

_, err = seer.Geo().Distance(fake_now_location, 5*1000)
if err != nil {
t.Error("Returned Error ", err)
return
}

assert.NilError(t, err)
}
2 changes: 1 addition & 1 deletion clients/p2p/tns/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
commonIface "github.com/taubyte/tau/core/common"
"github.com/taubyte/tau/dream"
spec "github.com/taubyte/tau/pkg/specs/common"
"gotest.tools/assert"
"gotest.tools/v3/assert"
)

func TestCache(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion clients/p2p/tns/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
iface "github.com/taubyte/tau/core/services/tns"
"github.com/taubyte/tau/dream"
spec "github.com/taubyte/tau/pkg/specs/common"
"gotest.tools/assert"
"gotest.tools/v3/assert"
)

var _ iface.Client = &p2p.Client{}
Expand Down
2 changes: 1 addition & 1 deletion clients/p2p/tns/fetch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"github.com/taubyte/tau/dream"
spec "github.com/taubyte/tau/pkg/specs/common"
_ "github.com/taubyte/tau/services/tns"
"gotest.tools/assert"
"gotest.tools/v3/assert"
)

func TestFetch(t *testing.T) {
Expand Down
36 changes: 8 additions & 28 deletions dream/fixtures/decompile_prod_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,8 @@ func TestDecompileProd(t *testing.T) {
}

simple, err := u.Simple("me")
if err != nil {
t.Error(err)
return
}
assert.NilError(t, err)

tns, err := simple.TNS()
assert.NilError(t, err)

Expand All @@ -62,41 +60,23 @@ func TestDecompileProd(t *testing.T) {
Name: "tb_prodproject",
HookInfo: fakeMeta,
})
if err != nil {
t.Error(err)
return
}
assert.NilError(t, err)

// read with seer
projectIface, err := projectLib.Open(projectLib.SystemFS(gitRootConfig))
if err != nil {
t.Error(err)
return
}
assert.NilError(t, err)

rc, err := compile.CompilerConfig(projectIface, fakeMeta, generatedDomainRegExp)
if err != nil {
t.Error(err)
return
}
assert.NilError(t, err)

compiler, err := compile.New(rc, compile.Dev())
if err != nil {
t.Error(err)
return
}
assert.NilError(t, err)

err = compiler.Build()
if err != nil {
t.Error(err)
return
}
assert.NilError(t, err)

err = compiler.Publish(tns)
if err != nil {
t.Error(err)
return
}
assert.NilError(t, err)

test_obj, err := tns.Fetch(specs.ProjectPrefix(projectIface.Get().Id(), fakeMeta.Repository.Branch, fakeMeta.HeadCommit.ID))
if test_obj.Interface() == nil {
Expand Down
75 changes: 35 additions & 40 deletions pkg/mycelium/command/all_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ import (
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"golang.org/x/crypto/ssh"

"github.com/taubyte/tau/pkg/mycelium/command/mocks"
"gotest.tools/v3/assert"
)

func TestNewCommand(t *testing.T) {
Expand All @@ -21,11 +21,11 @@ func TestNewCommand(t *testing.T) {

mockSession.On("Setenv", mock.Anything, mock.Anything).Return(nil).Once()
_, err := New(ctx, mockSession, "ls", Env("fake", "var"))
assert.Nil(t, err, "Expected no error")
assert.NilError(t, err)

mockSession.On("Setenv", mock.Anything, mock.Anything).Return(errors.New("failed to set env")).Once()
_, err = New(ctx, mockSession, "ls", Env("fake", "var"))
assert.Error(t, err, "Expected an error from setting environment variable")
assert.Assert(t, err != nil)
}

func TestCommandRun(t *testing.T) {
Expand All @@ -38,11 +38,11 @@ func TestCommandRun(t *testing.T) {
mockSession.On("Run", mock.Anything).Return(nil).Once()
mockSession.On("Close").Return(nil).Twice() // Expect Close to be called twice
err := cmd.Run()
assert.Nil(t, err, "Expected no error when running command")
assert.NilError(t, err)

mockSession.On("Run", mock.Anything).Return(errors.New("command failed")).Once()
err = cmd.Run()
assert.Error(t, err, "Expected an error when command fails")
assert.Assert(t, err != nil)
}

func TestCommandCombinedOutput(t *testing.T) {
Expand All @@ -54,13 +54,13 @@ func TestCommandCombinedOutput(t *testing.T) {
mockSession.On("CombinedOutput", mock.Anything).Return(output, nil).Once()
mockSession.On("Close").Return(nil).Once() // Set expectation for Close method here
data, err := cmd.CombinedOutput()
assert.Nil(t, err, "Expected no error from CombinedOutput")
assert.Equal(t, output, data, "Expected output to match mock output")
assert.NilError(t, err)
assert.DeepEqual(t, output, data)

mockSession.On("CombinedOutput", mock.Anything).Return(nil, errors.New("command failed")).Once()
mockSession.On("Close").Return(nil).Once() // Expect Close to be called on error as well
_, err = cmd.CombinedOutput()
assert.Error(t, err, "Expected an error from CombinedOutput")
assert.Assert(t, err != nil)
}

func TestCommandPipes(t *testing.T) {
Expand All @@ -75,20 +75,20 @@ func TestCommandPipes(t *testing.T) {
mockSession.On("StdoutPipe").Return(io.NopCloser(nil), nil).Once()
mockSession.On("Close").Return(nil).Once()
stdout, err := cmd.StdoutPipe()
assert.Nil(t, err, "Expected no error from StdoutPipe")
assert.NotNil(t, stdout, "Expected stdout not to be nil")
assert.NilError(t, err)
assert.Equal(t, stdout != nil, true)

mockSession.On("StderrPipe").Return(io.NopCloser(nil), nil).Once()
mockSession.On("Close").Return(nil).Once()
stderr, err := cmd.StderrPipe()
assert.Nil(t, err, "Expected no error from StderrPipe")
assert.NotNil(t, stderr, "Expected stderr not to be nil")
assert.NilError(t, err)
assert.Equal(t, stderr != nil, true)

mockSession.On("StdinPipe").Return(mockWriteCloser, nil).Once()
mockSession.On("Close").Return(nil).Once()
stdin, err := cmd.StdinPipe()
assert.Nil(t, err, "Expected no error from StdinPipe")
assert.NotNil(t, stdin, "Expected stdin not to be nil")
assert.NilError(t, err)
assert.Equal(t, stdin != nil, true)
}

func TestCommandSessionClosed(t *testing.T) {
Expand All @@ -100,11 +100,11 @@ func TestCommandSessionClosed(t *testing.T) {

mockSession.On("Run", mock.Anything).Return(errors.New("session closed")).Once()
err := cmd.Run()
assert.Error(t, err, "Expected error when running with closed session")
assert.Assert(t, err != nil)

mockSession.On("Start", mock.Anything).Return(errors.New("session closed")).Once()
err = cmd.Start()
assert.Error(t, err, "Expected error when starting with closed session")
assert.Assert(t, err != nil)
}

func TestCommandsessionWrap(t *testing.T) {
Expand All @@ -117,8 +117,8 @@ func TestCommandsessionWrap(t *testing.T) {
err := cmd.sessionWrap(func() error {
return cmd.sess.Run("failing command")
})
assert.Error(t, err, "Expected an error from sessionWrap")
assert.Contains(t, err.Error(), "command failed", "Error should propagate from Run")
assert.Assert(t, err != nil)
assert.ErrorContains(t, err, "command failed", "Error should propagate from Run")
}
func TestCommandContextTimeout(t *testing.T) {
mockSession := new(mocks.RemoteSession)
Expand All @@ -137,7 +137,7 @@ func TestCommandContextTimeout(t *testing.T) {
if err == nil {
t.Error("Expected an error due to context timeout, but got nil")
} else {
assert.Contains(t, err.Error(), "context deadline exceeded", "Expected a timeout error")
assert.ErrorContains(t, err, "context deadline exceeded", "Expected a timeout error")
}
}

Expand All @@ -151,10 +151,10 @@ func TestCommandStartAndWait(t *testing.T) {

cmd, _ := New(ctx, mockSession, "long-running-process")
err := cmd.Start()
assert.Nil(t, err, "Failed to start the command")
assert.NilError(t, err)

err = cmd.Wait()
assert.Nil(t, err, "Failed to wait for the command to finish")
assert.NilError(t, err)
}

func TestCommandPipeStdout(t *testing.T) {
Expand All @@ -166,7 +166,7 @@ func TestCommandPipeStdout(t *testing.T) {

mockSession.On("StdoutPipe").Return(dummyReader, nil).Once()
stdout, err := cmd.StdoutPipe()
assert.Nil(t, err)
assert.NilError(t, err)
assert.Equal(t, stdout, dummyReader)
}

Expand All @@ -179,7 +179,7 @@ func TestCommandPipeStderr(t *testing.T) {

mockSession.On("StderrPipe").Return(dummyReader, nil).Once()
stdout, err := cmd.StderrPipe()
assert.Nil(t, err)
assert.NilError(t, err)
assert.Equal(t, stdout, dummyReader)
}

Expand All @@ -192,11 +192,10 @@ func TestCommandSingleExecution(t *testing.T) {
mockSession.On("Run", mock.Anything).Return(nil).Once()
mockSession.On("Close").Return(nil).Once()
err := cmd.Run()
assert.Nil(t, err, "Expected no error on first run")
assert.NilError(t, err)

err = cmd.Run()
assert.Error(t, err, "Expected error on second run")
assert.Equal(t, "session closed", err.Error(), "Error should indicate command was already executed")
assert.ErrorContains(t, err, "session closed")
}

func TestCommandsessionWrapContextCancellation(t *testing.T) {
Expand All @@ -216,8 +215,7 @@ func TestCommandsessionWrapContextCancellation(t *testing.T) {
time.Sleep(100 * time.Millisecond) // Simulate some work
return nil
})
assert.Error(t, err, "Expected context cancellation error")
assert.Contains(t, err.Error(), "context done", "Expected error to mention context being done")
assert.ErrorContains(t, err, "context done")
}

func TestCommandsessionWrapContextCancellationFailedInt(t *testing.T) {
Expand All @@ -238,8 +236,7 @@ func TestCommandsessionWrapContextCancellationFailedInt(t *testing.T) {
time.Sleep(100 * time.Millisecond) // Simulate some work
return nil
})
assert.Error(t, err, "Expected context cancellation error")
assert.Contains(t, err.Error(), "context done", "Expected error to mention context being done")
assert.ErrorContains(t, err, "context done")
}

func TestCommandStartAndWaitErrorHandling(t *testing.T) {
Expand All @@ -251,11 +248,11 @@ func TestCommandStartAndWaitErrorHandling(t *testing.T) {
mockSession.On("Close").Return(nil).Once()

err := cmd.Start()
assert.Error(t, err, "Expected error on start failure")
assert.Assert(t, err != nil)

mockSession.On("Wait").Return(errors.New("wait failed")).Once()
err = cmd.Wait()
assert.Error(t, err, "Expected error on wait failure")
assert.Assert(t, err != nil)
}

func TestCommandStdinPipesErrorHandling(t *testing.T) {
Expand All @@ -265,8 +262,8 @@ func TestCommandStdinPipesErrorHandling(t *testing.T) {

mockSession.On("StdinPipe").Return(nil, errors.New("stdin error")).Once()
stdin, err := cmd.StdinPipe()
assert.Nil(t, stdin, "stdin should be nil on error")
assert.Error(t, err, "Expected stdin pipe error")
assert.Equal(t, stdin, nil)
assert.Assert(t, err != nil)
}

func TestCommandStdoutPipesErrorHandling(t *testing.T) {
Expand All @@ -276,9 +273,8 @@ func TestCommandStdoutPipesErrorHandling(t *testing.T) {

mockSession.On("StdoutPipe").Return(nil, errors.New("stdin error")).Once()
stdin, err := cmd.StdoutPipe()
assert.Nil(t, stdin, "stdout should be nil on error")
assert.Error(t, err, "Expected stdin pipe error")

assert.Equal(t, stdin, nil)
assert.Assert(t, err != nil)
}

func TestCommandStderrPipesErrorHandling(t *testing.T) {
Expand All @@ -288,9 +284,8 @@ func TestCommandStderrPipesErrorHandling(t *testing.T) {

mockSession.On("StderrPipe").Return(nil, errors.New("stdin error")).Once()
stdin, err := cmd.StderrPipe()
assert.Nil(t, stdin, "stderr should be nil on error")
assert.Error(t, err, "Expected stdin pipe error")

assert.Equal(t, stdin, nil)
assert.Assert(t, err != nil)
}

func TestCommand_ArgumentsHandling(t *testing.T) {
Expand Down
4 changes: 3 additions & 1 deletion pkg/spore-drive/config/all_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,9 @@ func TestParserSchema(t *testing.T) {
assert.Equal(t, host1.SSH().Port(), uint16(4242))
assert.DeepEqual(t, host1.SSH().Auth().List(), []string{"main"})

assert.DeepEqual(t, host1.Shapes().List(), []string{"shape1", "shape2"})
for _, s := range []string{"shape1", "shape2"} {
assert.Assert(t, slices.Contains(host1.Shapes().List(), s))
}

lat, lng := host1.Location()
assert.Equal(t, lat, float32(1.25))
Expand Down
Loading

0 comments on commit 8a92750

Please sign in to comment.