-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: remove codersdk dependency (#194)
Part of #178 In order to update our branch of Kaniko, we need to first update to go1.22. This is not currently possible while depending on codersdk. - Manually vendored relevant parts of codersdk and agentsdk into internal/notcodersdk - Replaced existing usage of codersdk / agentsdk with internal/notcodersdk - Added test for coder log sending functionality
- Loading branch information
Showing
13 changed files
with
786 additions
and
725 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"net/http" | ||
"net/http/httptest" | ||
"path/filepath" | ||
"testing" | ||
"time" | ||
|
||
"cdr.dev/slog/sloggers/slogtest" | ||
"github.com/coder/envbuilder/internal/notcodersdk" | ||
"github.com/coder/serpent" | ||
"github.com/google/uuid" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func Test_sendLogs(t *testing.T) { | ||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) | ||
defer cancel() | ||
// Random token for testing log fowarding | ||
agentToken := uuid.NewString() | ||
|
||
// Server to read logs posted by envbuilder. Matched to backlog limit. | ||
logCh := make(chan notcodersdk.Log, 100) | ||
logs := make([]notcodersdk.Log, 0) | ||
go func() { | ||
for { | ||
select { | ||
case <-ctx.Done(): | ||
return | ||
case log, ok := <-logCh: | ||
if !ok { | ||
return | ||
} | ||
logs = append(logs, log) | ||
} | ||
} | ||
}() | ||
logSrv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
if !assert.Equal(t, http.MethodPatch, r.Method) { | ||
w.WriteHeader(http.StatusMethodNotAllowed) | ||
return | ||
} | ||
assert.Equal(t, agentToken, r.Header.Get(notcodersdk.SessionTokenHeader)) | ||
var res notcodersdk.PatchLogs | ||
if !assert.NoError(t, json.NewDecoder(r.Body).Decode(&res)) { | ||
w.WriteHeader(http.StatusInternalServerError) | ||
return | ||
} | ||
if !assert.Equal(t, notcodersdk.ExternalLogSourceID, res.LogSourceID) { | ||
w.WriteHeader(http.StatusInternalServerError) | ||
return | ||
} | ||
for _, log := range res.Logs { | ||
logCh <- log | ||
} | ||
w.WriteHeader(http.StatusOK) | ||
})) | ||
|
||
// Make an empty working directory | ||
tmpDir := t.TempDir() | ||
t.Setenv("ENVBUILDER_DEVCONTAINER_DIR", tmpDir) | ||
t.Setenv("ENVBUILDER_DOCKERFILE_DIR", filepath.Join(tmpDir, "Dockerfile")) | ||
t.Setenv("ENVBUILDER_WORKSPACE_FOLDER", tmpDir) | ||
t.Setenv("CODER_AGENT_TOKEN", agentToken) | ||
t.Setenv("CODER_AGENT_URL", logSrv.URL) | ||
|
||
testLogger := slogtest.Make(t, &slogtest.Options{IgnoreErrors: true}) | ||
cmd := envbuilderCmd() | ||
inv := &serpent.Invocation{ | ||
Command: &cmd, | ||
Args: []string{}, | ||
Logger: testLogger, | ||
Environ: serpent.Environ{}, | ||
} | ||
|
||
err := inv.WithOS().Run() | ||
require.ErrorContains(t, err, "no such file or directory") | ||
require.NotEmpty(t, logs) | ||
require.Contains(t, logs[len(logs)-1].Output, "no such file or directory") | ||
} |
Oops, something went wrong.