-
Notifications
You must be signed in to change notification settings - Fork 0
/
http_server_play_test.go
131 lines (98 loc) · 2.6 KB
/
http_server_play_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
// SPDX-FileCopyrightText: 2019 M. Shulhan <[email protected]>
// 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"
"strings"
"testing"
libhttp "git.sr.ht/~shulhan/pakakeh.go/lib/http"
"git.sr.ht/~shulhan/pakakeh.go/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,
//
// --- 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 ExecResponse
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), strings.Join(execRes.Output, "\n"))
}