-
-
Notifications
You must be signed in to change notification settings - Fork 140
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
We have around 20 location NGINX directives for 5 underlying microservices. This level of complexity slowed down our dev and release process because every change had to be verified manually in a separate test environment. So, we are introducing automated tests for the API gateway.
- Loading branch information
1 parent
162490a
commit c8f52e1
Showing
6 changed files
with
192 additions
and
12 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
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,10 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
) | ||
|
||
func TestDummy(t *testing.T) { | ||
fmt.Println("dummy") | ||
} |
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,5 @@ | ||
module github.com/shellhub-io/shellhub/gateway/tests | ||
|
||
go 1.16 | ||
|
||
require github.com/miekg/dns v1.1.42 // indirect |
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,11 @@ | ||
github.com/miekg/dns v1.1.42 h1:gWGe42RGaIqXQZ+r3WUGEKBEtvPHY2SXo4dqixDNxuY= | ||
github.com/miekg/dns v1.1.42/go.mod h1:+evo5L0630/F6ca/Z9+GAqzhjGyn8/c+TBaOyfEl0V4= | ||
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110 h1:qWPm9rbaAMKs8Bq/9LRpbMqxWRVUAQwMI9fVrssnTfw= | ||
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= | ||
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= | ||
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= | ||
golang.org/x/sys v0.0.0-20210303074136-134d130e1a04 h1:cEhElsAv9LUt9ZUUocxzWe05oFLVd+AA2nstydTeI8g= | ||
golang.org/x/sys v0.0.0-20210303074136-134d130e1a04/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= | ||
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= | ||
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= | ||
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= |
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,112 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"net" | ||
"os" | ||
"os/exec" | ||
"testing" | ||
"time" | ||
|
||
"github.com/miekg/dns" | ||
) | ||
|
||
type dnsHandler struct { | ||
records map[string]string | ||
} | ||
|
||
func (d *dnsHandler) ServeDNS(w dns.ResponseWriter, r *dns.Msg) { | ||
m := new(dns.Msg) | ||
m.SetReply(r) | ||
m.Compress = false | ||
|
||
switch r.Opcode { | ||
case dns.OpcodeQuery: | ||
d.parseQuery(m) | ||
} | ||
|
||
w.WriteMsg(m) | ||
} | ||
|
||
func (d *dnsHandler) parseQuery(m *dns.Msg) { | ||
for _, q := range m.Question { | ||
switch q.Qtype { | ||
case dns.TypeA: | ||
if ip, ok := d.records[q.Name]; ok { | ||
rr, err := dns.NewRR(fmt.Sprintf("%s A %s", q.Name, ip)) | ||
if err == nil { | ||
m.Answer = append(m.Answer, rr) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
func waitForConnection(proto, addr string) bool { | ||
for i := 0; i < 10; i++ { | ||
conn, err := net.Dial(proto, addr) | ||
if err == nil { | ||
conn.Close() | ||
return true | ||
} | ||
|
||
time.Sleep(time.Second) | ||
} | ||
|
||
return false | ||
} | ||
|
||
func TestMain(m *testing.M) { | ||
// Create a virtual network adapter | ||
if _, err := exec.Command("ifconfig", "eth0:0", "127.0.0.11").Output(); err != nil { | ||
panic(err) | ||
} | ||
|
||
server := &dns.Server{ | ||
Addr: "127.0.0.11:53", | ||
Net: "udp", | ||
Handler: &dnsHandler{ | ||
records: map[string]string{ | ||
"api.": "127.0.0.1", | ||
"ssh.": "127.0.0.1", | ||
"ui.": "127.0.0.1", | ||
}, | ||
}, | ||
} | ||
|
||
go func() { | ||
if err := server.ListenAndServe(); err != nil { | ||
panic(err) | ||
} | ||
}() | ||
|
||
// Wait for DNS test server to be started | ||
if !waitForConnection("udp", "127.0.0.11:53") { | ||
panic("Failed to connect to DNS test server") | ||
} | ||
|
||
// Start OpenResty daemon | ||
cmd := exec.Command("/entrypoint.sh", "/usr/local/openresty/bin/openresty", "-g", "daemon off;") | ||
cmd.Stdout = os.Stdout | ||
cmd.Stderr = os.Stderr | ||
|
||
if err := cmd.Start(); err != nil { | ||
panic(err) | ||
} | ||
|
||
// Wait for OpenResty to be started | ||
if !waitForConnection("tcp", "127.0.0.1:80") { | ||
panic("Failed to connect to OpenResty") | ||
} | ||
|
||
// Run unit test | ||
code := m.Run() | ||
|
||
server.Shutdown() | ||
|
||
if err := cmd.Process.Kill(); err != nil { | ||
panic(err) | ||
} | ||
|
||
os.Exit(code) | ||
} |