Skip to content

chore: Add test helpers to improve coverage #166

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

Merged
merged 18 commits into from
Feb 6, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Fix coderd test leak
  • Loading branch information
kylecarbs committed Feb 5, 2022
commit 57efae22337182e8a82d16afafa83da42580b0fe
2 changes: 0 additions & 2 deletions coderd/coderdtest/coderdtest.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package coderdtest
import (
"context"
"database/sql"
"fmt"
"io"
"net/http/httptest"
"net/url"
Expand Down Expand Up @@ -154,7 +153,6 @@ func AwaitProjectVersionImported(t *testing.T, client *codersdk.Client, organiza
var err error
projectVersion, err = client.ProjectVersion(context.Background(), organization, project, version)
require.NoError(t, err)
fmt.Printf("GOT: %s\n", projectVersion.Import.Status)
return projectVersion.Import.Status.Completed()
}, 3*time.Second, 25*time.Millisecond)
return projectVersion
Expand Down
20 changes: 18 additions & 2 deletions coderd/coderdtest/coderdtest_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
package coderdtest_test

import (
"context"
"testing"

"go.uber.org/goleak"

"github.com/stretchr/testify/require"

"github.com/coder/coder/coderd"
"github.com/coder/coder/coderd/coderdtest"
"github.com/coder/coder/database"
)

func TestMain(m *testing.M) {
Expand All @@ -15,6 +20,17 @@ func TestMain(m *testing.M) {
func TestNew(t *testing.T) {
t.Parallel()
client := coderdtest.New(t)
_ = coderdtest.CreateInitialUser(t, client)
_ = coderdtest.NewProvisionerDaemon(t, client)
user := coderdtest.CreateInitialUser(t, client)
closer := coderdtest.NewProvisionerDaemon(t, client)
project := coderdtest.CreateProject(t, client, user.Organization)
version := coderdtest.CreateProjectVersion(t, client, user.Organization, project.Name, nil)
coderdtest.AwaitProjectVersionImported(t, client, user.Organization, project.Name, version.Name)
workspace := coderdtest.CreateWorkspace(t, client, "me", project.ID)
history, err := client.CreateWorkspaceHistory(context.Background(), "me", workspace.Name, coderd.CreateWorkspaceHistoryRequest{
ProjectVersionID: version.ID,
Transition: database.WorkspaceTransitionCreate,
})
require.NoError(t, err)
coderdtest.AwaitWorkspaceHistoryProvisioned(t, client, "me", workspace.Name, history.Name)
closer.Close()
}
5 changes: 4 additions & 1 deletion coderd/provisionerdaemons.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"reflect"
"time"
Expand Down Expand Up @@ -75,7 +76,9 @@ func (api *api) provisionerDaemonsServe(rw http.ResponseWriter, r *http.Request)
// Multiplexes the incoming connection using yamux.
// This allows multiple function calls to occur over
// the same connection.
session, err := yamux.Server(websocket.NetConn(r.Context(), conn, websocket.MessageBinary), nil)
config := yamux.DefaultConfig()
config.LogOutput = io.Discard
session, err := yamux.Server(websocket.NetConn(r.Context(), conn, websocket.MessageBinary), config)
if err != nil {
_ = conn.Close(websocket.StatusInternalError, fmt.Sprintf("multiplex server: %s", err))
return
Expand Down
5 changes: 4 additions & 1 deletion codersdk/provisioners.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package codersdk
import (
"context"
"encoding/json"
"io"
"net/http"

"github.com/hashicorp/yamux"
Expand Down Expand Up @@ -42,7 +43,9 @@ func (c *Client) ProvisionerDaemonClient(ctx context.Context) (proto.DRPCProvisi
}
return nil, readBodyAsError(res)
}
session, err := yamux.Client(websocket.NetConn(context.Background(), conn, websocket.MessageBinary), nil)
config := yamux.DefaultConfig()
config.LogOutput = io.Discard
session, err := yamux.Client(websocket.NetConn(ctx, conn, websocket.MessageBinary), config)
if err != nil {
return nil, xerrors.Errorf("multiplex client: %w", err)
}
Expand Down
6 changes: 4 additions & 2 deletions provisioner/echo/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ func (*echo) Parse(request *proto.Parse_Request, stream proto.DRPCProvisioner_Pa
return err
}
}
return nil
<-stream.Context().Done()
return stream.Context().Err()
}

// Provision reads requests from the provided directory to stream responses.
Expand Down Expand Up @@ -97,7 +98,8 @@ func (*echo) Provision(request *proto.Provision_Request, stream proto.DRPCProvis
return err
}
}
return nil
<-stream.Context().Done()
return stream.Context().Err()
}

type Responses struct {
Expand Down