aboutsummaryrefslogtreecommitdiff
path: root/http_server_play_test.go
diff options
context:
space:
mode:
authorShulhan <ms@kilabit.info>2023-11-16 11:22:15 +0700
committerShulhan <ms@kilabit.info>2023-11-16 11:22:15 +0700
commitf2b71ae4bf9d264d5c6ea51edc2d402e462e2033 (patch)
tree5975b1e5bf5c39d4bcdc94424cf1c9ae38b8198c /http_server_play_test.go
parentc96e06ab3b4e49dfe5bdb501d3a621e7deba29be (diff)
downloadawwan-f2b71ae4bf9d264d5c6ea51edc2d402e462e2033.tar.xz
all: always load SSH config when running Play
In case awwan run with "serve" and we modify the ".ssh/config", the changes does not detected by awwan because we only read ".ssh/config" once we Awwan instance created. This changes fix this issue by always loading SSH config everytime the Play method executed so the user CLI and WUI has the same experiences.
Diffstat (limited to 'http_server_play_test.go')
-rw-r--r--http_server_play_test.go129
1 files changed, 129 insertions, 0 deletions
diff --git a/http_server_play_test.go b/http_server_play_test.go
new file mode 100644
index 0000000..008e60b
--- /dev/null
+++ b/http_server_play_test.go
@@ -0,0 +1,129 @@
+// SPDX-FileCopyrightText: 2019 M. Shulhan <ms@kilabit.info>
+// SPDX-License-Identifier: GPL-3.0-or-later
+
+//go:build integration
+
+package awwan
+
+import (
+ "bytes"
+ "encoding/json"
+ "io"
+ "net/http"
+ "net/http/httptest"
+ "os"
+ "path/filepath"
+ "testing"
+
+ libhttp "github.com/shuLhan/share/lib/http"
+ "github.com/shuLhan/share/lib/test"
+)
+
+// Here is what happened,
+// 1) Run "awwan serve"
+// 2) Update ".ssh/config" with new host
+// 3) Create and run script with new host.
+//
+// This will cause the awwan play failed with empty SSH host,
+//
+// - play.aww --- SSH connection: @:22
+//
+// or an error,
+//
+// !!! initSSHClient: NewClientInteractive: dialWithSigners: ssh: handshake
+// failed: knownhosts: key is unknown from known_hosts files
+func TestHttpServerPlaySshConfigChanges(t *testing.T) {
+ var (
+ baseDir = `testdata/http_server/play_ssh_config_changes`
+ sshConfig = filepath.Join(baseDir, `.ssh`, `config`)
+
+ aww *Awwan
+ err error
+ )
+
+ var tdata *test.Data
+
+ tdata, err = test.LoadData(filepath.Join(baseDir, `test.data`))
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ // Write the oldhost in .ssh/config.
+
+ err = os.WriteFile(sshConfig, tdata.Input[`.ssh/config`], 0600)
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ // Start the "awwan serve".
+
+ aww, err = New(baseDir)
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ var httpd *httpServer
+
+ httpd, err = newHttpServer(aww, ``)
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ // Test execute play on oldhost first.
+
+ testHttpExecute(t, httpd, tdata, `play_on_oldhost`)
+
+ // Update .ssh/config with new host.
+
+ err = os.WriteFile(sshConfig, tdata.Input[`.ssh/config:newhost`], 0600)
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ // Test execute play on newhost.
+
+ testHttpExecute(t, httpd, tdata, `play_on_newhost`)
+}
+
+func testHttpExecute(t *testing.T, httpd *httpServer, tdata *test.Data, tag string) {
+ var reqBody bytes.Buffer
+
+ reqBody.Write(tdata.Input[tag])
+
+ var httpReq = httptest.NewRequest(http.MethodPost, pathAwwanApiExecute, &reqBody)
+ var httpWriter = httptest.NewRecorder()
+
+ httpd.ServeHTTP(httpWriter, httpReq)
+
+ var (
+ httpRes *http.Response
+ bbuf bytes.Buffer
+ rawb []byte
+ expResp []byte
+ )
+
+ httpRes = httpWriter.Result()
+ rawb, _ = io.ReadAll(httpRes.Body)
+
+ json.Indent(&bbuf, rawb, ``, ` `)
+
+ expResp = tdata.Output[tag+`:output:json`]
+
+ test.Assert(t, tag, string(expResp), bbuf.String())
+
+ var (
+ execRes HttpResponse
+ epRes libhttp.EndpointResponse
+ err error
+ )
+
+ epRes.Data = &execRes
+
+ err = json.Unmarshal(rawb, &epRes)
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ expResp = tdata.Output[tag+`:output`]
+ test.Assert(t, tag+`:output`, string(expResp), string(execRes.Output))
+}