|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "io" |
| 7 | + "net/http" |
| 8 | + "strconv" |
| 9 | + "testing" |
| 10 | + |
| 11 | + "github.com/stretchr/testify/assert" |
| 12 | +) |
| 13 | + |
| 14 | +func TestInfo(t *testing.T) { |
| 15 | + type Expected struct { |
| 16 | + res map[string]interface{} |
| 17 | + err error |
| 18 | + } |
| 19 | + |
| 20 | + cases := []struct { |
| 21 | + description string |
| 22 | + compose *Enviroment |
| 23 | + expected func(string, string) Expected |
| 24 | + }{ |
| 25 | + { |
| 26 | + description: "case 1", |
| 27 | + compose: New(). |
| 28 | + WithEnv("SHELLHUB_HTTP_PORT", strconv.Itoa(getFreePort(t))). |
| 29 | + WithEnv("SHELLHUB_SSH_PORT", strconv.Itoa(getFreePort(t))). |
| 30 | + WithEnv("SHELLHUB_VERSION", "v0.14.2"). |
| 31 | + Build(), |
| 32 | + expected: func(httpPort, sshPort string) Expected { |
| 33 | + return Expected{ |
| 34 | + res: map[string]interface{}{ |
| 35 | + "endpoints": map[string]interface{}{ |
| 36 | + "api": fmt.Sprintf("localhost:%s", httpPort), |
| 37 | + "ssh": fmt.Sprintf("localhost:%s", sshPort), |
| 38 | + }, |
| 39 | + "version": "v0.14.2", |
| 40 | + }, |
| 41 | + err: nil, |
| 42 | + } |
| 43 | + }, |
| 44 | + }, |
| 45 | + { |
| 46 | + description: "case 2", |
| 47 | + compose: New(). |
| 48 | + WithEnv("SHELLHUB_HTTP_PORT", strconv.Itoa(getFreePort(t))). |
| 49 | + WithEnv("SHELLHUB_SSH_PORT", strconv.Itoa(getFreePort(t))). |
| 50 | + WithEnv("SHELLHUB_VERSION", "v0.10.2"). |
| 51 | + Build(), |
| 52 | + expected: func(httpPort, sshPort string) Expected { |
| 53 | + return Expected{ |
| 54 | + res: map[string]interface{}{ |
| 55 | + "endpoints": map[string]interface{}{ |
| 56 | + "api": fmt.Sprintf("localhost:%s", httpPort), |
| 57 | + "ssh": fmt.Sprintf("localhost:%s", sshPort), |
| 58 | + }, |
| 59 | + "version": "v0.10.2", |
| 60 | + }, |
| 61 | + err: nil, |
| 62 | + } |
| 63 | + }, |
| 64 | + }, |
| 65 | + } |
| 66 | + |
| 67 | + for _, tt := range cases { |
| 68 | + // Avoid "loop variable <> captured by func literal" |
| 69 | + tc := tt |
| 70 | + |
| 71 | + t.Run(tc.description, func(t *testing.T) { |
| 72 | + t.Parallel() |
| 73 | + |
| 74 | + tc.compose.Run(t, func() { |
| 75 | + fmt.Printf("desciption %s\n", tc.description) |
| 76 | + |
| 77 | + fmt.Printf("%s request to %s\n", tc.description, fmt.Sprintf("http://localhost:%s/info", tc.compose.GetEnv("SHELLHUB_HTTP_PORT"))) |
| 78 | + |
| 79 | + // res, _ := resty. |
| 80 | + // New(). |
| 81 | + // R(). |
| 82 | + // Get(fmt.Sprintf("http://localhost:%s/info", tc.compose.GetEnv("SHELLHUB_HTTP_PORT"))) |
| 83 | + |
| 84 | + res, err := http.Get(fmt.Sprintf("http://localhost:%s/info", tc.compose.GetEnv("SHELLHUB_HTTP_PORT"))) |
| 85 | + assert.NoError(t, err) |
| 86 | + defer res.Body.Close() |
| 87 | + |
| 88 | + foo, err := io.ReadAll(res.Body) |
| 89 | + assert.NoError(t, err) |
| 90 | + |
| 91 | + body := map[string]interface{}{} |
| 92 | + assert.NoError(t, json.Unmarshal(foo, &body)) |
| 93 | + |
| 94 | + fmt.Printf("%s http: %+v\n", tc.description, body) |
| 95 | + fmt.Printf("%s env: %s %s %s\n", tc.description, tc.compose.GetEnv("SHELLHUB_HTTP_PORT"), tc.compose.GetEnv("SHELLHUB_SSH_PORT"), tc.compose.GetEnv("SHELLHUB_VERSION")) |
| 96 | + fmt.Printf("%s expected: %+v\n", tc.description, tc.expected(tc.compose.GetEnv("SHELLHUB_HTTP_PORT"), tc.compose.GetEnv("SHELLHUB_SSH_PORT")).res) |
| 97 | + fmt.Println("********************************************") |
| 98 | + // |
| 99 | + // assert.Equal(t, tc.expected(tc.compose.GetEnv("SHELLHUB_HTTP_PORT"), tc.compose.GetEnv("SHELLHUB_SSH_PORT")), Expected{body, err}) |
| 100 | + }) |
| 101 | + }) |
| 102 | + } |
| 103 | +} |
0 commit comments